Move Canvas related files to a "Canvas" folder under "UI"

This commit is contained in:
OverloadedOrama 2020-08-18 16:21:31 +03:00
parent fd97191d56
commit 92332cc52e
14 changed files with 71 additions and 22 deletions

View file

@ -0,0 +1,228 @@
extends Camera2D
var tween : Tween
var zoom_min := Vector2(0.005, 0.005)
var zoom_max := Vector2.ONE
var viewport_container : ViewportContainer
var transparent_checker : ColorRect
var mouse_pos := Vector2.ZERO
var drag := false
func _ready() -> void:
viewport_container = get_parent().get_parent()
transparent_checker = get_parent().get_node("TransparentChecker")
tween = Tween.new()
add_child(tween)
tween.connect("tween_step", self, "_on_tween_step")
update_transparent_checker_offset()
func update_transparent_checker_offset() -> void:
var o = get_global_transform_with_canvas().get_origin()
var s = get_global_transform_with_canvas().get_scale()
o.y = get_viewport_rect().size.y - o.y
transparent_checker.update_offset(o, s)
# Get the speed multiplier for when you've pressed
# a movement key for the given amount of time
func dir_move_zoom_multiplier(press_time : float) -> float:
if press_time < 0:
return 0.0
if Input.is_key_pressed(KEY_SHIFT) and Input.is_key_pressed(KEY_CONTROL) :
return Global.high_speed_move_rate
elif Input.is_key_pressed(KEY_SHIFT):
return Global.medium_speed_move_rate
elif !Input.is_key_pressed(KEY_CONTROL):
# control + right/left is used to move frames so
# we do this check to ensure that there is no conflict
return Global.low_speed_move_rate
else:
return 0.0
func reset_dir_move_time(direction) -> void:
Global.key_move_press_time[direction] = 0.0
const key_move_action_names := ["ui_up", "ui_down", "ui_left", "ui_right"]
# Check if an event is a ui_up/down/left/right event-press :)
func is_action_direction_pressed(event : InputEvent, allow_echo: bool = true) -> bool:
for action in key_move_action_names:
if event.is_action_pressed(action, allow_echo):
return true
return false
# Check if an event is a ui_up/down/left/right event release nya
func is_action_direction_released(event: InputEvent) -> bool:
for action in key_move_action_names:
if event.is_action_released(action):
return true
return false
# get the Direction associated with the event.
# if not a direction event return null
func get_action_direction(event: InputEvent): # -> Optional[Direction]
if event.is_action("ui_up"):
return Global.Direction.UP
elif event.is_action("ui_down"):
return Global.Direction.DOWN
elif event.is_action("ui_left"):
return Global.Direction.LEFT
elif event.is_action("ui_right"):
return Global.Direction.RIGHT
return null
# Holds sign multipliers for the given directions nyaa
# (per the indices in Global.gd defined by Direction)
# UP, DOWN, LEFT, RIGHT in that order
const directional_sign_multipliers := [
Vector2(0.0, -1.0),
Vector2(0.0, 1.0),
Vector2(-1.0, 0.0),
Vector2(1.0, 0.0)
]
# Process an action event for a pressed direction
# action
func process_direction_action_pressed(event: InputEvent) -> void:
var dir = get_action_direction(event)
if dir == null:
return
var increment := get_process_delta_time()
# Count the total time we've been doing this ^.^
Global.key_move_press_time[dir] += increment
var this_direction_press_time : float = Global.key_move_press_time[dir]
var move_speed := dir_move_zoom_multiplier(this_direction_press_time)
offset = offset + move_speed * increment * directional_sign_multipliers[dir] * zoom
update_transparent_checker_offset()
# Process an action for a release direction action
func process_direction_action_released(event: InputEvent) -> void:
var dir = get_action_direction(event)
if dir == null:
return
reset_dir_move_time(dir)
func _input(event : InputEvent) -> void:
mouse_pos = viewport_container.get_local_mouse_position()
var viewport_size := viewport_container.rect_size
if event.is_action_pressed("middle_mouse") || event.is_action_pressed("space"):
drag = true
elif event.is_action_released("middle_mouse") || event.is_action_released("space"):
drag = false
if Global.can_draw && Rect2(Vector2.ZERO, viewport_size).has_point(mouse_pos):
if event.is_action_pressed("zoom_in"): # Wheel Up Event
zoom_camera(-1)
elif event.is_action_pressed("zoom_out"): # Wheel Down Event
zoom_camera(1)
elif event is InputEventMouseMotion && drag:
offset = offset - event.relative * zoom
update_transparent_checker_offset()
elif is_action_direction_pressed(event):
process_direction_action_pressed(event)
elif is_action_direction_released(event):
process_direction_action_released(event)
Global.horizontal_ruler.update()
Global.vertical_ruler.update()
save_values_to_project()
# Zoom Camera
func zoom_camera(dir : int) -> void:
var viewport_size := viewport_container.rect_size
if Global.smooth_zoom:
var zoom_margin = zoom * dir / 5
var new_zoom = zoom + zoom_margin
if new_zoom > zoom_min && new_zoom < zoom_max:
var new_offset = offset + (-0.5 * viewport_size + mouse_pos) * (zoom - new_zoom)
tween.interpolate_property(self, "zoom", zoom, new_zoom, 0.05, Tween.TRANS_LINEAR, Tween.EASE_IN)
tween.interpolate_property(self, "offset", offset, new_offset, 0.05, Tween.TRANS_LINEAR, Tween.EASE_IN)
tween.start()
if name == "Camera2D":
Global.zoom_level_label.text = str(round(100 / new_zoom.x)) + " %"
elif name == "CameraPreview":
Global.preview_zoom_slider.value = -new_zoom.x
else:
var prev_zoom := zoom
var zoom_margin = zoom * dir / 10
if zoom + zoom_margin > zoom_min:
zoom += zoom_margin
if zoom > zoom_max:
zoom = zoom_max
offset = offset + (-0.5 * viewport_size + mouse_pos) * (prev_zoom - zoom)
update_transparent_checker_offset()
if name == "Camera2D":
Global.zoom_level_label.text = str(round(100 / Global.camera.zoom.x)) + " %"
elif name == "CameraPreview":
Global.preview_zoom_slider.value = -zoom.x
func _on_tween_step(_object: Object, _key: NodePath, _elapsed: float, _value: Object) -> void:
Global.horizontal_ruler.update()
Global.vertical_ruler.update()
update_transparent_checker_offset()
func zoom_100():
zoom = Vector2.ONE
offset = Global.current_project.size / 2
update_transparent_checker_offset()
Global.zoom_level_label.text = str(round(100 / zoom.x)) + " %"
Global.horizontal_ruler.update()
Global.vertical_ruler.update()
func fit_to_frame(size : Vector2) -> void:
viewport_container = get_parent().get_parent()
var h_ratio := viewport_container.rect_size.x / size.x
var v_ratio := viewport_container.rect_size.y / size.y
var ratio := min(h_ratio, v_ratio)
if ratio == 0:
ratio = 0.1 # Set it to a non-zero value just in case
# If the ratio is 0, it means that the viewport container is hidden
# in that case, use the other viewport to get the ratio
if name == "Camera2D":
h_ratio = Global.second_viewport.rect_size.x / size.x
v_ratio = Global.second_viewport.rect_size.y / size.y
ratio = min(h_ratio, v_ratio)
elif name == "Camera2D2":
h_ratio = Global.main_viewport.rect_size.x / size.x
v_ratio = Global.main_viewport.rect_size.y / size.y
ratio = min(h_ratio, v_ratio)
zoom = Vector2(1 / ratio, 1 / ratio)
offset = size / 2
update_transparent_checker_offset()
if name == "Camera2D":
Global.zoom_level_label.text = str(round(100 / Global.camera.zoom.x)) + " %"
Global.horizontal_ruler.update()
Global.vertical_ruler.update()
elif name == "CameraPreview":
Global.preview_zoom_slider.value = -zoom.x
func save_values_to_project() -> void:
if name == "Camera2D":
Global.current_project.cameras_zoom[0] = zoom
Global.current_project.cameras_offset[0] = offset
elif name == "Camera2D2":
Global.current_project.cameras_zoom[1] = zoom
Global.current_project.cameras_offset[1] = offset
elif name == "CameraPreview":
Global.current_project.cameras_zoom[2] = zoom
Global.current_project.cameras_offset[2] = offset

252
src/UI/Canvas/Canvas.gd Normal file
View file

@ -0,0 +1,252 @@
class_name Canvas
extends Node2D
var location := Vector2.ZERO
var fill_color := Color(0, 0, 0, 0)
var current_pixel := Vector2.ZERO # pretty much same as mouse_pos, but can be accessed externally
var can_undo := true
var cursor_image_has_changed := false
var sprite_changed_this_frame := false # for optimization purposes
onready var grid = $Grid
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var frame : Frame = new_empty_frame(true)
Global.current_project.frames.append(frame)
yield(get_tree().create_timer(0.2), "timeout")
camera_zoom()
func _draw() -> void:
Global.second_viewport.get_child(0).get_node("CanvasPreview").update()
Global.small_preview_viewport.get_child(0).get_node("CanvasPreview").update()
var current_cels : Array = Global.current_project.frames[Global.current_project.current_frame].cels
var size : Vector2 = Global.current_project.size
if Global.onion_skinning:
onion_skinning()
# Draw current frame layers
for i in range(Global.current_project.layers.size()):
var modulate_color := Color(1, 1, 1, current_cels[i].opacity)
if Global.current_project.layers[i].visible: # if it's visible
draw_texture(current_cels[i].image_texture, location, modulate_color)
if Global.tile_mode:
draw_texture(current_cels[i].image_texture, Vector2(location.x, location.y + size.y), modulate_color) # Down
draw_texture(current_cels[i].image_texture, Vector2(location.x - size.x, location.y + size.y), modulate_color) # Down Left
draw_texture(current_cels[i].image_texture, Vector2(location.x - size.x, location.y), modulate_color) # Left
draw_texture(current_cels[i].image_texture, location - size, modulate_color) # Up left
draw_texture(current_cels[i].image_texture, Vector2(location.x, location.y - size.y), modulate_color) # Up
draw_texture(current_cels[i].image_texture, Vector2(location.x + size.x, location.y - size.y), modulate_color) # Up right
draw_texture(current_cels[i].image_texture, Vector2(location.x + size.x, location.y), modulate_color) # Right
draw_texture(current_cels[i].image_texture, location + size, modulate_color) # Down right
# Draw rectangle to indicate the pixel currently being hovered on
if Global.has_focus and Global.can_draw:
Tools.draw_indicator()
func _input(event : InputEvent) -> void:
# Don't process anything below if the input isn't a mouse event, or Shift/Ctrl.
# This decreases CPU/GPU usage slightly.
if not event is InputEventMouse:
if not event is InputEventKey:
return
elif not event.scancode in [KEY_SHIFT, KEY_CONTROL]:
return
# elif not get_viewport_rect().has_point(event.position):
# return
current_pixel = get_local_mouse_position() + location
if Global.has_focus:
update()
sprite_changed_this_frame = false
var current_project : Project = Global.current_project
if Global.has_focus:
if !cursor_image_has_changed:
cursor_image_has_changed = true
if Global.show_left_tool_icon:
Global.left_cursor.visible = true
if Global.show_right_tool_icon:
Global.right_cursor.visible = true
else:
if cursor_image_has_changed:
cursor_image_has_changed = false
Global.left_cursor.visible = false
Global.right_cursor.visible = false
Tools.handle_draw(current_pixel.floor(), event)
if sprite_changed_this_frame:
update_texture(current_project.current_layer)
func camera_zoom() -> void:
# Set camera zoom based on the sprite size
var bigger_canvas_axis = max(Global.current_project.size.x, Global.current_project.size.y)
var zoom_max := Vector2(bigger_canvas_axis, bigger_canvas_axis) * 0.01
var cameras = [Global.camera, Global.camera2, Global.camera_preview]
for camera in cameras:
if zoom_max > Vector2.ONE:
camera.zoom_max = zoom_max
else:
camera.zoom_max = Vector2.ONE
if camera == Global.camera_preview:
Global.preview_zoom_slider.max_value = -camera.zoom_min.x
Global.preview_zoom_slider.min_value = -camera.zoom_max.x
camera.fit_to_frame(Global.current_project.size)
camera.save_values_to_project()
Global.transparent_checker._ready() # To update the rect size
func new_empty_frame(first_time := false, single_layer := false, size := Global.current_project.size) -> Frame:
var frame := Frame.new()
for l in Global.current_project.layers: # Create as many cels as there are layers
# The sprite itself
var sprite := Image.new()
if first_time:
if Global.config_cache.has_section_key("preferences", "default_image_width"):
Global.current_project.size.x = Global.config_cache.get_value("preferences", "default_image_width")
if Global.config_cache.has_section_key("preferences", "default_image_height"):
Global.current_project.size.y = Global.config_cache.get_value("preferences", "default_image_height")
if Global.config_cache.has_section_key("preferences", "default_fill_color"):
fill_color = Global.config_cache.get_value("preferences", "default_fill_color")
sprite.create(size.x, size.y, false, Image.FORMAT_RGBA8)
sprite.fill(fill_color)
sprite.lock()
frame.cels.append(Cel.new(sprite, 1))
if single_layer:
break
return frame
func handle_undo(action : String, project : Project = Global.current_project, layer_index := -2, frame_index := -2) -> void:
if !can_undo:
return
if layer_index <= -2:
layer_index = project.current_layer
if frame_index <= -2:
frame_index = project.current_frame
var cels := []
var frames := []
var layers := []
if frame_index == -1:
frames = project.frames
else:
frames.append(project.frames[frame_index])
if layer_index == -1:
layers = project.layers
else:
layers.append(project.layers[layer_index])
for f in frames:
for l in layers:
var index = project.layers.find(l)
cels.append(f.cels[index])
project.undos += 1
project.undo_redo.create_action(action)
for cel in cels:
# If we don't unlock the image, it doesn't work properly
cel.image.unlock()
var data = cel.image.data
cel.image.lock()
project.undo_redo.add_undo_property(cel.image, "data", data)
project.undo_redo.add_undo_method(Global, "undo", frame_index, layer_index, project)
can_undo = false
func handle_redo(_action : String, project : Project = Global.current_project, layer_index := -2, frame_index := -2) -> void:
can_undo = true
if project.undos < project.undo_redo.get_version():
return
if layer_index <= -2:
layer_index = project.current_layer
if frame_index <= -2:
frame_index = project.current_frame
var cels := []
var frames := []
var layers := []
if frame_index == -1:
frames = project.frames
else:
frames.append(project.frames[frame_index])
if layer_index == -1:
layers = project.layers
else:
layers.append(project.layers[layer_index])
for f in frames:
for l in layers:
var index = project.layers.find(l)
cels.append(f.cels[index])
for cel in cels:
project.undo_redo.add_do_property(cel.image, "data", cel.image.data)
project.undo_redo.add_do_method(Global, "redo", frame_index, layer_index, project)
project.undo_redo.commit_action()
func update_texture(layer_index : int, frame_index := -1, project : Project = Global.current_project) -> void:
if frame_index == -1:
frame_index = project.current_frame
var current_cel : Cel = project.frames[frame_index].cels[layer_index]
current_cel.image_texture.create_from_image(current_cel.image, 0)
if project == Global.current_project:
var frame_texture_rect : TextureRect
frame_texture_rect = Global.find_node_by_name(project.layers[layer_index].frame_container.get_child(frame_index), "CelTexture")
frame_texture_rect.texture = current_cel.image_texture
func onion_skinning() -> void:
# Past
if Global.onion_skinning_past_rate > 0:
var color : Color
if Global.onion_skinning_blue_red:
color = Color.blue
else:
color = Color.white
for i in range(1, Global.onion_skinning_past_rate + 1):
if Global.current_project.current_frame >= i:
var layer_i := 0
for layer in Global.current_project.frames[Global.current_project.current_frame - i].cels:
if Global.current_project.layers[layer_i].visible:
color.a = 0.6 / i
draw_texture(layer.image_texture, location, color)
layer_i += 1
# Future
if Global.onion_skinning_future_rate > 0:
var color : Color
if Global.onion_skinning_blue_red:
color = Color.red
else:
color = Color.white
for i in range(1, Global.onion_skinning_future_rate + 1):
if Global.current_project.current_frame < Global.current_project.frames.size() - i:
var layer_i := 0
for layer in Global.current_project.frames[Global.current_project.current_frame + i].cels:
if Global.current_project.layers[layer_i].visible:
color.a = 0.6 / i
draw_texture(layer.image_texture, location, color)
layer_i += 1

10
src/UI/Canvas/Canvas.tscn Normal file
View file

@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://src/UI/Canvas/Canvas.gd" type="Script" id=1]
[ext_resource path="res://src/UI/Canvas/Grid.gd" type="Script" id=2]
[node name="Canvas" type="Node2D"]
script = ExtResource( 1 )
[node name="Grid" type="Node2D" parent="."]
script = ExtResource( 2 )

View file

@ -0,0 +1,29 @@
extends Node2D
var frame : int = 0
onready var animation_timer : Timer = $AnimationTimer
func _draw() -> void:
var current_project : Project = Global.current_project
$AnimationTimer.wait_time = Global.animation_timer.wait_time
if animation_timer.is_stopped():
frame = current_project.current_frame
var current_cels : Array = current_project.frames[frame].cels
# Draw current frame layers
for i in range(current_cels.size()):
var modulate_color := Color(1, 1, 1, current_cels[i].opacity)
if i < current_project.layers.size() and current_project.layers[i].visible:
draw_texture(current_cels[i].image_texture, Vector2.ZERO, modulate_color)
func _on_AnimationTimer_timeout() -> void:
var current_project : Project = Global.current_project
if frame < current_project.frames.size() - 1:
frame += 1
else:
frame = 0
update()

View file

@ -0,0 +1,39 @@
[gd_scene load_steps=2 format=2]
[sub_resource type="GDScript" id=1]
script/source = "extends Node2D
var frame : int = 0
onready var animation_timer : Timer = $AnimationTimer
func _draw() -> void:
var current_project : Project = Global.current_project
$AnimationTimer.wait_time = Global.animation_timer.wait_time
if animation_timer.is_stopped():
frame = current_project.current_frame
var current_cels : Array = current_project.frames[frame].cels
# Draw current frame layers
for i in range(current_cels.size()):
var modulate_color := Color(1, 1, 1, current_cels[i].opacity)
if i < current_project.layers.size() and current_project.layers[i].visible:
draw_texture(current_cels[i].image_texture, Vector2.ZERO, modulate_color)
func _on_AnimationTimer_timeout() -> void:
var current_project : Project = Global.current_project
if frame < current_project.frames.size() - 1:
frame += 1
else:
frame = 0
update()
"
[node name="CanvasPreview" type="Node2D"]
script = SubResource( 1 )
[node name="AnimationTimer" type="Timer" parent="."]
[connection signal="timeout" from="AnimationTimer" to="." method="_on_AnimationTimer_timeout"]

53
src/UI/Canvas/Grid.gd Normal file
View file

@ -0,0 +1,53 @@
extends Node2D
var location := Vector2.ZERO
var isometric_polylines := [] # An array of PoolVector2Arrays
func _draw() -> void:
if Global.draw_grid:
draw_grid(Global.grid_type)
func draw_grid(grid_type : int) -> void:
var size : Vector2 = Global.current_project.size
if grid_type == Global.Grid_Types.CARTESIAN || grid_type == Global.Grid_Types.ALL:
for x in range(Global.grid_width, size.x, Global.grid_width):
draw_line(Vector2(x, location.y), Vector2(x, size.y), Global.grid_color, true)
for y in range(Global.grid_height, size.y, Global.grid_height):
draw_line(Vector2(location.x, y), Vector2(size.x, y), Global.grid_color, true)
# Doesn't work properly yet
# Has problems when the canvas isn't a square, and with some grid sizes
if grid_type == Global.Grid_Types.ISOMETRIC || grid_type == Global.Grid_Types.ALL:
var i := 0
for x in range(Global.grid_width, size.x, Global.grid_width * 2):
for y in range(0, size.y, Global.grid_width):
draw_isometric_tile(i, Vector2(x, y))
i += 1
func draw_isometric_tile(i : int, origin := Vector2.RIGHT) -> void:
# A random value I found by trial and error, I have no idea why it "works"
var diff = 1.11754
var approx_30_degrees = deg2rad(26.565)
var pool := PoolVector2Array()
if i < isometric_polylines.size():
pool = isometric_polylines[i]
else:
var a = origin
var b = a + Vector2(cos(approx_30_degrees), sin(approx_30_degrees)) * Global.grid_width * diff
var c = a + Vector2.DOWN * Global.grid_width
var d = c - Vector2(cos(approx_30_degrees), sin(approx_30_degrees)) * Global.grid_width * diff
pool.append(a)
pool.append(b)
pool.append(c)
pool.append(d)
pool.append(a)
isometric_polylines.append(pool)
if pool.size() > 2:
draw_polyline(pool, Global.grid_color)

View file

@ -0,0 +1,78 @@
class_name Guide extends Line2D
enum Types {HORIZONTAL, VERTICAL}
var font := preload("res://assets/fonts/Roboto-Regular.tres")
var has_focus := true
var mouse_pos := Vector2.ZERO
var type = Types.HORIZONTAL
var project = Global.current_project
func _ready() -> void:
width = 0.1
default_color = Global.guide_color
project.guides.append(self)
func _input(_event : InputEvent):
width = Global.camera.zoom.x * 2
mouse_pos = get_local_mouse_position()
if points.size() < 2:
return
var point0 := points[0]
var point1 := points[1]
if type == Types.HORIZONTAL:
point0.y -= width * 3
point1.y += width * 3
else:
point0.x -= width * 3
point1.x += width * 3
if Global.can_draw and Global.has_focus and point_in_rectangle(mouse_pos, point0, point1) and Input.is_action_just_pressed("left_mouse"):
if !point_in_rectangle(Global.canvas.current_pixel, Global.canvas.location, Global.canvas.location + project.size):
has_focus = true
Global.has_focus = false
update()
if has_focus and visible:
if Input.is_action_pressed("left_mouse"):
if type == Types.HORIZONTAL:
points[0].y = round(mouse_pos.y)
points[1].y = round(mouse_pos.y)
else:
points[0].x = round(mouse_pos.x)
points[1].x = round(mouse_pos.x)
if Input.is_action_just_released("left_mouse"):
Global.has_focus = true
has_focus = false
if !outside_canvas():
update()
func _draw() -> void:
if has_focus:
var viewport_size: Vector2 = Global.main_viewport.rect_size
var zoom: Vector2 = Global.camera.zoom
if type == Types.HORIZONTAL:
draw_set_transform(Vector2(Global.camera.offset.x - (viewport_size.x / 2) * zoom.x, points[0].y + font.get_height() * zoom.x * 2), rotation, zoom * 2)
draw_string(font, Vector2.ZERO, "%spx" % str(round(mouse_pos.y)))
else:
draw_set_transform(Vector2(points[0].x + font.get_height() * zoom.y, Global.camera.offset.y - (viewport_size.y / 2.25) * zoom.y), rotation, zoom * 2)
draw_string(font, Vector2.ZERO, "%spx" % str(round(mouse_pos.x)))
func outside_canvas() -> bool:
if type == Types.HORIZONTAL:
if points[0].y < 0 || points[0].y > project.size.y:
project.guides.erase(self)
queue_free()
return true
else:
if points[0].x < 0 || points[0].x > project.size.x:
project.guides.erase(self)
queue_free()
return true
return false
func point_in_rectangle(p : Vector2, coord1 : Vector2, coord2 : Vector2) -> bool:
return p.x > coord1.x && p.y > coord1.y && p.x < coord2.x && p.y < coord2.y

View file

@ -0,0 +1,79 @@
extends Button
const RULER_WIDTH := 16
var font := preload("res://assets/fonts/Roboto-Small.tres")
var major_subdivision := 2
var minor_subdivision := 4
var first : Vector2
var last : Vector2
func _ready() -> void:
Global.main_viewport.connect("item_rect_changed", self, "update")
# Code taken and modified from Godot's source code
func _draw() -> void:
var transform := Transform2D()
var ruler_transform := Transform2D()
var major_subdivide := Transform2D()
var minor_subdivide := Transform2D()
var zoom: float = 1 / Global.camera.zoom.x
transform.x = Vector2(zoom, zoom)
transform.origin = Global.main_viewport.rect_size / 2 + Global.camera.offset * -zoom
var basic_rule := 100.0
var i := 0
while(basic_rule * zoom > 100):
basic_rule /= 5.0 if i % 2 else 2.0
i += 1
i = 0
while(basic_rule * zoom < 100):
basic_rule *= 2.0 if i % 2 else 5.0
i += 1
ruler_transform = ruler_transform.scaled(Vector2(basic_rule, basic_rule))
major_subdivide = major_subdivide.scaled(Vector2(1.0 / major_subdivision, 1.0 / major_subdivision))
minor_subdivide = minor_subdivide.scaled(Vector2(1.0 / minor_subdivision, 1.0 / minor_subdivision))
first = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Vector2.ZERO)
last = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Global.main_viewport.rect_size)
for j in range(ceil(first.x), ceil(last.x)):
var position : Vector2 = (transform * ruler_transform * major_subdivide * minor_subdivide).xform(Vector2(j, 0))
if j % (major_subdivision * minor_subdivision) == 0:
draw_line(Vector2(position.x + RULER_WIDTH, 0), Vector2(position.x + RULER_WIDTH, RULER_WIDTH), Color.white)
var val = (ruler_transform * major_subdivide * minor_subdivide).xform(Vector2(j, 0)).x
draw_string(font, Vector2(position.x + RULER_WIDTH + 2, font.get_height() - 4), str(int(val)))
else:
if j % minor_subdivision == 0:
draw_line(Vector2(position.x + RULER_WIDTH, RULER_WIDTH * 0.33), Vector2(position.x + RULER_WIDTH, RULER_WIDTH), Color.white)
else:
draw_line(Vector2(position.x + RULER_WIDTH, RULER_WIDTH * 0.66), Vector2(position.x + RULER_WIDTH, RULER_WIDTH), Color.white)
func _on_HorizontalRuler_pressed() -> void:
if !Global.show_guides:
return
var mouse_pos := get_local_mouse_position()
if mouse_pos.x < RULER_WIDTH: # For double guides
Global.vertical_ruler._on_VerticalRuler_pressed()
var guide := Guide.new()
guide.type = guide.Types.HORIZONTAL
guide.add_point(Vector2(-19999, Global.canvas.current_pixel.y))
guide.add_point(Vector2(19999, Global.canvas.current_pixel.y))
Global.canvas.add_child(guide)
Global.has_focus = false
update()
func _on_HorizontalRuler_mouse_entered() -> void:
var mouse_pos := get_local_mouse_position()
if mouse_pos.x < RULER_WIDTH: # For double guides
mouse_default_cursor_shape = Control.CURSOR_FDIAGSIZE
else:
mouse_default_cursor_shape = Control.CURSOR_VSPLIT

View file

@ -0,0 +1,37 @@
class_name SymmetryGuide extends Guide
var _texture = preload("res://assets/graphics/dotted_line.png")
func _ready() -> void:
._ready()
has_focus = false
visible = false
texture = _texture
texture_mode = Line2D.LINE_TEXTURE_TILE
width = Global.camera.zoom.x * 4
yield(get_tree().create_timer(0.01), "timeout")
modulate = Global.guide_color
func _input(_event : InputEvent) -> void:
._input(_event)
if type == Types.HORIZONTAL:
project.y_symmetry_point = points[0].y * 2 - 1
elif type == Types.VERTICAL:
project.x_symmetry_point = points[0].x * 2 - 1
yield(get_tree().create_timer(0.01), "timeout")
width = Global.camera.zoom.x * 4
func outside_canvas() -> bool:
if type == Types.HORIZONTAL:
points[0].y = clamp(points[0].y, 0, Global.current_project.size.y)
points[1].y = clamp(points[1].y, 0, Global.current_project.size.y)
elif type == Types.VERTICAL:
points[0].x = clamp(points[0].x, 0, Global.current_project.size.x)
points[1].x = clamp(points[1].x, 0, Global.current_project.size.x)
return false

View file

@ -0,0 +1,71 @@
extends Button
const RULER_WIDTH := 16
var font := preload("res://assets/fonts/Roboto-Small.tres")
var major_subdivision := 2
var minor_subdivision := 4
var first : Vector2
var last : Vector2
func _ready() -> void:
Global.main_viewport.connect("item_rect_changed", self, "update")
# Code taken and modified from Godot's source code
func _draw() -> void:
var transform := Transform2D()
var ruler_transform := Transform2D()
var major_subdivide := Transform2D()
var minor_subdivide := Transform2D()
var zoom: float = 1 / Global.camera.zoom.x
transform.y = Vector2(zoom, zoom)
transform.origin = Global.main_viewport.rect_size / 2 + Global.camera.offset * -zoom
var basic_rule := 100.0
var i := 0
while(basic_rule * zoom > 100):
basic_rule /= 5.0 if i % 2 else 2.0
i += 1
i = 0
while(basic_rule * zoom < 100):
basic_rule *= 2.0 if i % 2 else 5.0
i += 1
ruler_transform = ruler_transform.scaled(Vector2(basic_rule, basic_rule))
major_subdivide = major_subdivide.scaled(Vector2(1.0 / major_subdivision, 1.0 / major_subdivision))
minor_subdivide = minor_subdivide.scaled(Vector2(1.0 / minor_subdivision, 1.0 / minor_subdivision))
first = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Vector2.ZERO)
last = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Global.main_viewport.rect_size)
for j in range(ceil(first.y), ceil(last.y)):
var position : Vector2 = (transform * ruler_transform * major_subdivide * minor_subdivide).xform(Vector2(0, j))
if j % (major_subdivision * minor_subdivision) == 0:
draw_line(Vector2(0, position.y), Vector2(RULER_WIDTH, position.y), Color.white)
var text_xform = Transform2D(-PI / 2, Vector2(font.get_height() - 4, position.y - 2))
draw_set_transform_matrix(get_transform() * text_xform)
var val = (ruler_transform * major_subdivide * minor_subdivide).xform(Vector2(0, j)).y
draw_string(font, Vector2(), str(int(val)))
draw_set_transform_matrix(get_transform())
else:
if j % minor_subdivision == 0:
draw_line(Vector2(RULER_WIDTH * 0.33, position.y), Vector2(RULER_WIDTH, position.y), Color.white)
else:
draw_line(Vector2(RULER_WIDTH * 0.66, position.y), Vector2(RULER_WIDTH, position.y), Color.white)
func _on_VerticalRuler_pressed() -> void:
if !Global.show_guides:
return
var guide := Guide.new()
guide.type = guide.Types.VERTICAL
guide.add_point(Vector2(Global.canvas.current_pixel.x, -19999))
guide.add_point(Vector2(Global.canvas.current_pixel.x, 19999))
Global.canvas.add_child(guide)
Global.has_focus = false
update()