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,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"]

View file

@ -1,9 +0,0 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://src/UI/CanvasPreview.gd" type="Script" id=1]
[node name="CanvasPreview" type="Node2D"]
script = ExtResource( 1 )
[node name="AnimationTimer" type="Timer" parent="."]
[connection signal="timeout" from="AnimationTimer" to="." method="_on_AnimationTimer_timeout"]

View file

@ -1,9 +1,9 @@
[gd_scene load_steps=8 format=2]
[ext_resource path="res://src/UI/CanvasPreview.tscn" type="PackedScene" id=1]
[ext_resource path="res://src/UI/Canvas/CanvasPreview.tscn" type="PackedScene" id=1]
[ext_resource path="res://src/UI/TransparentChecker.tscn" type="PackedScene" id=2]
[ext_resource path="res://src/UI/CanvasPreviewContainer.gd" type="Script" id=3]
[ext_resource path="res://src/CameraMovement.gd" type="Script" id=4]
[ext_resource path="res://src/UI/Canvas/CameraMovement.gd" type="Script" id=4]
[ext_resource path="res://src/Shaders/TransparentChecker.shader" type="Shader" id=5]
[ext_resource path="res://assets/graphics/dark_themes/timeline/play.png" type="Texture" id=7]
@ -12,6 +12,11 @@ shader = ExtResource( 5 )
shader_param/size = 10.0
shader_param/color1 = Color( 0.7, 0.7, 0.7, 1 )
shader_param/color2 = Color( 1, 1, 1, 1 )
shader_param/offset = Vector2( 0, 0 )
shader_param/scale = Vector2( 0, 0 )
shader_param/rect_size = Vector2( 0, 0 )
shader_param/follow_movement = false
shader_param/follow_scale = false
[node name="CanvasPreviewContainer" type="HBoxContainer"]
margin_right = 332.0
@ -32,13 +37,27 @@ margin_top = 7.0
margin_right = 27.0
margin_bottom = 157.0
[node name="Label" type="Label" parent="SettingsContainer/VBoxContainer"]
margin_right = 20.0
margin_bottom = 14.0
text = "+"
align = 1
[node name="PreviewZoomSlider" type="VSlider" parent="SettingsContainer/VBoxContainer"]
margin_top = 18.0
margin_right = 16.0
margin_bottom = 126.0
margin_bottom = 108.0
mouse_default_cursor_shape = 2
size_flags_vertical = 3
step = 0.01
[node name="Label2" type="Label" parent="SettingsContainer/VBoxContainer"]
margin_top = 112.0
margin_right = 20.0
margin_bottom = 126.0
text = "-"
align = 1
[node name="PlayButton" type="Button" parent="SettingsContainer/VBoxContainer" groups=[
"UIButtons",
]]

View file

@ -1,12 +1,12 @@
[gd_scene load_steps=23 format=2]
[ext_resource path="res://src/UI/ToolButtons.gd" type="Script" id=1]
[ext_resource path="res://src/UI/CanvasPreview.tscn" type="PackedScene" id=2]
[ext_resource path="res://src/UI/Canvas/CanvasPreview.tscn" type="PackedScene" id=2]
[ext_resource path="res://src/UI/Tabs.gd" type="Script" id=3]
[ext_resource path="res://src/UI/Rulers/VerticalRuler.gd" type="Script" id=4]
[ext_resource path="res://src/UI/Canvas/Rulers/VerticalRuler.gd" type="Script" id=4]
[ext_resource path="res://src/UI/TransparentChecker.tscn" type="PackedScene" id=5]
[ext_resource path="res://src/UI/Rulers/HorizontalRuler.gd" type="Script" id=6]
[ext_resource path="res://src/CameraMovement.gd" type="Script" id=7]
[ext_resource path="res://src/UI/Canvas/Rulers/HorizontalRuler.gd" type="Script" id=6]
[ext_resource path="res://src/UI/Canvas/CameraMovement.gd" type="Script" id=7]
[ext_resource path="res://src/SelectionRectangle.gd" type="Script" id=8]
[ext_resource path="res://assets/graphics/dark_themes/tools/bucket.png" type="Texture" id=10]
[ext_resource path="res://assets/graphics/dark_themes/tools/colorpicker.png" type="Texture" id=11]
@ -17,7 +17,7 @@
[ext_resource path="res://src/UI/CanvasPreviewContainer.tscn" type="PackedScene" id=16]
[ext_resource path="res://src/UI/ColorAndToolOptions.tscn" type="PackedScene" id=17]
[ext_resource path="res://src/UI/Timeline/AnimationTimeline.tscn" type="PackedScene" id=18]
[ext_resource path="res://src/Canvas.tscn" type="PackedScene" id=19]
[ext_resource path="res://src/UI/Canvas/Canvas.tscn" type="PackedScene" id=19]
[ext_resource path="res://src/Palette/PalettePanelContainer.tscn" type="PackedScene" id=20]
[ext_resource path="res://assets/graphics/dark_themes/tools/zoom.png" type="Texture" id=21]
[ext_resource path="res://src/UI/ViewportContainer.gd" type="Script" id=23]