From 80e351a2a1ff15634f2d6f95361d69f3f2d061fc Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Tue, 5 Dec 2023 01:46:42 +0200 Subject: [PATCH] Move some code away from Global and Project to AnimationTimeline --- src/Autoload/Global.gd | 12 ----- src/Classes/Project.gd | 23 -------- src/Main.gd | 1 - src/UI/Timeline/AnimationTimeline.gd | 72 ++++++++++++++++++-------- src/UI/Timeline/AnimationTimeline.tscn | 18 ++++--- 5 files changed, 61 insertions(+), 65 deletions(-) diff --git a/src/Autoload/Global.gd b/src/Autoload/Global.gd index ec117729b..4d2070e23 100644 --- a/src/Autoload/Global.gd +++ b/src/Autoload/Global.gd @@ -489,16 +489,6 @@ var cel_button_scene: PackedScene = load("res://src/UI/Timeline/CelButton.tscn") @onready var cel_vbox: VBoxContainer = animation_timeline.find_child("CelVBox") ## The container of animation tags. @onready var tag_container: Control = animation_timeline.find_child("TagContainer") -## Play forward button. -@onready var play_forward: BaseButton = animation_timeline.find_child("PlayForward") -## Play backward button. -@onready var play_backwards: BaseButton = animation_timeline.find_child("PlayBackwards") -## Remove frame button. -@onready var remove_frame_button: BaseButton = animation_timeline.find_child("DeleteFrame") -## Move frame left button. -@onready var move_left_frame_button: BaseButton = animation_timeline.find_child("MoveLeft") -## Move frame right button. -@onready var move_right_frame_button: BaseButton = animation_timeline.find_child("MoveRight") ## Remove layer button. @onready var remove_layer_button: BaseButton = animation_timeline.find_child("RemoveLayer") ## Move layer up button. @@ -507,8 +497,6 @@ var cel_button_scene: PackedScene = load("res://src/UI/Timeline/CelButton.tscn") @onready var move_down_layer_button: BaseButton = animation_timeline.find_child("MoveDownLayer") ## Merge with layer below button. @onready var merge_down_layer_button: BaseButton = animation_timeline.find_child("MergeDownLayer") -## Layer opacity slider. -@onready var layer_opacity_slider: ValueSlider = animation_timeline.find_child("OpacitySlider") ## The brushes popup dialog used to display brushes. ## It has the [param BrushesPopup.gd] script attached. diff --git a/src/Classes/Project.gd b/src/Classes/Project.gd index b1f1d85db..01544990e 100644 --- a/src/Classes/Project.gd +++ b/src/Classes/Project.gd @@ -178,12 +178,6 @@ func change_project() -> void: Global.animation_timeline.project_changed() Global.current_frame_mark_label.text = "%s/%s" % [str(current_frame + 1), frames.size()] - - Global.disable_button(Global.remove_frame_button, frames.size() == 1) - Global.disable_button(Global.move_left_frame_button, frames.size() == 1 or current_frame == 0) - Global.disable_button( - Global.move_right_frame_button, frames.size() == 1 or current_frame == frames.size() - 1 - ) toggle_layer_buttons() animation_tags = animation_tags @@ -519,20 +513,11 @@ func change_cel(new_frame: int, new_layer := -1) -> void: if new_frame != current_frame: # If the frame has changed current_frame = new_frame Global.current_frame_mark_label.text = "%s/%s" % [str(current_frame + 1), frames.size()] - toggle_frame_buttons() if new_layer != current_layer: # If the layer has changed current_layer = new_layer toggle_layer_buttons() - if current_frame < frames.size(): # Set opacity slider - var cel_opacity := frames[current_frame].cels[current_layer].opacity - Global.layer_opacity_slider.value = cel_opacity * 100 - var blend_mode_index: int = Global.animation_timeline.blend_modes_button.get_item_index( - layers[current_layer].blend_mode - ) - Global.animation_timeline.blend_modes_button.selected = blend_mode_index - Global.transparent_checker.update_rect() Global.cel_changed.emit() if get_current_cel() is Cel3D: @@ -542,14 +527,6 @@ func change_cel(new_frame: int, new_layer := -1) -> void: Global.canvas.queue_redraw() -func toggle_frame_buttons() -> void: - Global.disable_button(Global.remove_frame_button, frames.size() == 1) - Global.disable_button(Global.move_left_frame_button, frames.size() == 1 or current_frame == 0) - Global.disable_button( - Global.move_right_frame_button, frames.size() == 1 or current_frame == frames.size() - 1 - ) - - func toggle_layer_buttons() -> void: if layers.is_empty() or current_layer >= layers.size(): return diff --git a/src/Main.gd b/src/Main.gd index 61e219401..5e11cfa9b 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -26,7 +26,6 @@ func _ready() -> void: Global.current_project.layers.append(PixelLayer.new(Global.current_project)) Global.current_project.frames.append(Global.current_project.new_empty_frame()) Global.animation_timeline.project_changed() - Global.current_project.toggle_frame_buttons() Import.import_brushes(Global.path_join_array(Global.data_directories, "Brushes")) Import.import_patterns(Global.path_join_array(Global.data_directories, "Patterns")) diff --git a/src/UI/Timeline/AnimationTimeline.gd b/src/UI/Timeline/AnimationTimeline.gd index f71d7ac1a..87d3d4f3e 100644 --- a/src/UI/Timeline/AnimationTimeline.gd +++ b/src/UI/Timeline/AnimationTimeline.gd @@ -19,12 +19,18 @@ var future_above_canvas := true @onready var tag_spacer := %TagSpacer as Control @onready var layer_settings_container := %LayerSettingsContainer as VBoxContainer @onready var layer_container := %LayerContainer as VBoxContainer -@onready var add_layer_list := $"%AddLayerList" as MenuButton +@onready var add_layer_list := %AddLayerList as MenuButton @onready var blend_modes_button := %BlendModes as OptionButton +@onready var opacity_slider: ValueSlider = %OpacitySlider @onready var frame_scroll_container := %FrameScrollContainer as Control @onready var frame_scroll_bar := %FrameScrollBar as HScrollBar @onready var tag_scroll_container := %TagScroll as ScrollContainer @onready var layer_frame_h_split := %LayerFrameHSplit as HSplitContainer +@onready var delete_frame: Button = %DeleteFrame +@onready var move_frame_left: Button = %MoveFrameLeft +@onready var move_frame_right: Button = %MoveFrameRight +@onready var play_backwards := %PlayBackwards as Button +@onready var play_forward := %PlayForward as Button @onready var fps_spinbox := %FPSValue as ValueSlider @onready var onion_skinning_button := %OnionSkinning as BaseButton @onready var onion_skinning_settings := $OnionSkinningSettings as Popup @@ -93,6 +99,7 @@ func _ready() -> void: # emit signals that were supposed to be emitted (Check if it's still required in godot 4) %PastPlacement.item_selected.emit(0 if past_above else 1) %FuturePlacement.item_selected.emit(0 if future_above else 1) + Global.cel_changed.connect(_cel_changed) # Makes sure that the frame and tag scroll bars are in the right place: Global.layer_vbox.emit_signal.call_deferred("resized") @@ -510,25 +517,25 @@ func _on_LoopAnim_pressed() -> void: func _on_PlayForward_toggled(button_pressed: bool) -> void: if button_pressed: - Global.change_button_texturerect(Global.play_forward.get_child(0), "pause.png") + Global.change_button_texturerect(play_forward.get_child(0), "pause.png") else: - Global.change_button_texturerect(Global.play_forward.get_child(0), "play.png") + Global.change_button_texturerect(play_forward.get_child(0), "play.png") play_animation(button_pressed, true) func _on_PlayBackwards_toggled(button_pressed: bool) -> void: if button_pressed: - Global.change_button_texturerect(Global.play_backwards.get_child(0), "pause.png") + Global.change_button_texturerect(play_backwards.get_child(0), "pause.png") else: - Global.change_button_texturerect(Global.play_backwards.get_child(0), "play_backwards.png") + Global.change_button_texturerect(play_backwards.get_child(0), "play_backwards.png") play_animation(button_pressed, false) -# Called on each frame of the animation +## Called on each frame of the animation func _on_AnimationTimer_timeout() -> void: if first_frame == last_frame: - Global.play_forward.button_pressed = false - Global.play_backwards.button_pressed = false + play_forward.button_pressed = false + play_backwards.button_pressed = false Global.animation_timer.stop() return @@ -546,8 +553,8 @@ func _on_AnimationTimer_timeout() -> void: else: match animation_loop: 0: # No loop - Global.play_forward.button_pressed = false - Global.play_backwards.button_pressed = false + play_forward.button_pressed = false + play_backwards.button_pressed = false Global.animation_timer.stop() is_animation_running = false 1: # Cycle loop @@ -572,8 +579,8 @@ func _on_AnimationTimer_timeout() -> void: else: match animation_loop: 0: # No loop - Global.play_backwards.button_pressed = false - Global.play_forward.button_pressed = false + play_backwards.button_pressed = false + play_forward.button_pressed = false Global.animation_timer.stop() is_animation_running = false 1: # Cycle loop @@ -605,21 +612,21 @@ func play_animation(play: bool, forward_dir: bool) -> void: if first_frame == last_frame: if forward_dir: - Global.play_forward.button_pressed = false + play_forward.button_pressed = false else: - Global.play_backwards.button_pressed = false + play_backwards.button_pressed = false return if forward_dir: - Global.play_backwards.toggled.disconnect(_on_PlayBackwards_toggled) - Global.play_backwards.button_pressed = false - Global.change_button_texturerect(Global.play_backwards.get_child(0), "play_backwards.png") - Global.play_backwards.toggled.connect(_on_PlayBackwards_toggled) + play_backwards.toggled.disconnect(_on_PlayBackwards_toggled) + play_backwards.button_pressed = false + Global.change_button_texturerect(play_backwards.get_child(0), "play_backwards.png") + play_backwards.toggled.connect(_on_PlayBackwards_toggled) else: - Global.play_forward.toggled.disconnect(_on_PlayForward_toggled) - Global.play_forward.button_pressed = false - Global.change_button_texturerect(Global.play_forward.get_child(0), "play.png") - Global.play_forward.toggled.connect(_on_PlayForward_toggled) + play_forward.toggled.disconnect(_on_PlayForward_toggled) + play_forward.button_pressed = false + Global.change_button_texturerect(play_forward.get_child(0), "play.png") + play_forward.toggled.connect(_on_PlayForward_toggled) if play: Global.animation_timer.set_one_shot(true) # wait_time can't change correctly if it's playing @@ -984,8 +991,27 @@ func _on_onion_skinning_settings_visibility_changed() -> void: # Methods to update the UI in response to changes in the current project +func _cel_changed() -> void: + _toggle_frame_buttons() + var project := Global.current_project + var cel_opacity := project.get_current_cel().opacity + opacity_slider.value = cel_opacity * 100 + var blend_mode_index := blend_modes_button.get_item_index( + project.layers[project.current_layer].blend_mode + ) + blend_modes_button.selected = blend_mode_index + + +func _toggle_frame_buttons() -> void: + var project := Global.current_project + Global.disable_button(delete_frame, project.frames.size() == 1) + Global.disable_button(move_frame_left, project.current_frame == 0) + Global.disable_button(move_frame_right, project.current_frame == project.frames.size() - 1) + + func project_changed() -> void: var project := Global.current_project + _toggle_frame_buttons() # These must be removed from tree immediately to not mess up the indices of # the new buttons, so use either free or queue_free + parent.remove_child for layer_button in Global.layer_vbox.get_children(): @@ -1029,7 +1055,7 @@ func project_frame_added(frame: int) -> void: Global.frame_hbox.move_child(button, frame) # Make it visible, yes 3 call_deferreds are required frame_scroll_container.call_deferred( - "call_deferred", "call_deferred", "ensure_control_visible", button + &"call_deferred", &"call_deferred", &"ensure_control_visible", button ) var layer := Global.cel_vbox.get_child_count() - 1 for cel_hbox in Global.cel_vbox.get_children(): diff --git a/src/UI/Timeline/AnimationTimeline.tscn b/src/UI/Timeline/AnimationTimeline.tscn index 1bd4a1d2c..dad49fd41 100644 --- a/src/UI/Timeline/AnimationTimeline.tscn +++ b/src/UI/Timeline/AnimationTimeline.tscn @@ -442,6 +442,7 @@ offset_bottom = 6.0 texture = ExtResource("19") [node name="DeleteFrame" type="Button" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons" groups=["UIButtons"]] +unique_name_in_owner = true custom_minimum_size = Vector2(24, 24) layout_mode = 2 size_flags_horizontal = 0 @@ -506,7 +507,8 @@ offset_right = 7.0 offset_bottom = 7.0 texture = ExtResource("28") -[node name="MoveLeft" type="Button" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons" groups=["UIButtons"]] +[node name="MoveFrameLeft" type="Button" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons" groups=["UIButtons"]] +unique_name_in_owner = true custom_minimum_size = Vector2(24, 24) layout_mode = 2 size_flags_horizontal = 0 @@ -515,7 +517,7 @@ focus_mode = 0 mouse_default_cursor_shape = 2 shortcut = SubResource("Shortcut_kwn1u") -[node name="TextureRect" type="TextureRect" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/MoveLeft"] +[node name="TextureRect" type="TextureRect" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/MoveFrameLeft"] layout_mode = 0 anchor_left = 0.5 anchor_top = 0.5 @@ -528,7 +530,8 @@ offset_bottom = 5.5 texture = ExtResource("8") flip_h = true -[node name="MoveRight" type="Button" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons" groups=["UIButtons"]] +[node name="MoveFrameRight" type="Button" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons" groups=["UIButtons"]] +unique_name_in_owner = true custom_minimum_size = Vector2(24, 24) layout_mode = 2 size_flags_horizontal = 0 @@ -537,7 +540,7 @@ focus_mode = 0 mouse_default_cursor_shape = 2 shortcut = SubResource("Shortcut_m2mvi") -[node name="TextureRect" type="TextureRect" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/MoveRight"] +[node name="TextureRect" type="TextureRect" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/MoveFrameRight"] layout_mode = 0 anchor_left = 0.5 anchor_top = 0.5 @@ -593,6 +596,7 @@ offset_bottom = 6.0 texture = ExtResource("23") [node name="PlayBackwards" type="Button" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/PlaybackButtons" groups=["UIButtons"]] +unique_name_in_owner = true custom_minimum_size = Vector2(24, 24) layout_mode = 2 tooltip_text = "Play the animation backwards (from end to beginning)" @@ -614,6 +618,7 @@ offset_bottom = 6.0 texture = ExtResource("24") [node name="PlayForward" type="Button" parent="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/PlaybackButtons" groups=["UIButtons"]] +unique_name_in_owner = true custom_minimum_size = Vector2(24, 24) layout_mode = 2 size_flags_horizontal = 0 @@ -806,6 +811,7 @@ unique_name_in_owner = true layout_mode = 2 [node name="OpacitySlider" parent="TimelineContainer/MainBodyPanel/MainBodyVBoxContainer/TimelineScroll/LayerFrameHSplit/LayerContainer" instance=ExtResource("9")] +unique_name_in_owner = true custom_minimum_size = Vector2(207, 30) layout_mode = 2 size_flags_vertical = 0 @@ -952,8 +958,8 @@ script = ExtResource("12") [connection signal="pressed" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/DeleteFrame" to="." method="_on_DeleteFrame_pressed"] [connection signal="pressed" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/CopyFrame" to="." method="_on_CopyFrame_pressed"] [connection signal="pressed" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/FrameTagButton" to="." method="_on_FrameTagButton_pressed"] -[connection signal="pressed" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/MoveLeft" to="." method="_on_MoveLeft_pressed"] -[connection signal="pressed" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/MoveRight" to="." method="_on_MoveRight_pressed"] +[connection signal="pressed" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/MoveFrameLeft" to="." method="_on_MoveLeft_pressed"] +[connection signal="pressed" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/FrameButtons/MoveFrameRight" to="." method="_on_MoveRight_pressed"] [connection signal="pressed" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/PlaybackButtons/FirstFrame" to="." method="_on_FirstFrame_pressed"] [connection signal="pressed" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/PlaybackButtons/PreviousFrame" to="." method="_on_PreviousFrame_pressed"] [connection signal="toggled" from="TimelineContainer/TimelineButtons/VBoxContainer/AnimationToolsScrollContainer/AnimationTools/AnimationButtons/PlaybackButtons/PlayBackwards" to="." method="_on_PlayBackwards_toggled"]