From 824860a2b544971afb1e5f47cb5abeea596e8281 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Mon, 2 Jan 2023 22:09:47 +0200 Subject: [PATCH] Add a change_cel() method to Project, instead of using a signal Using a signal to change the cel was not a good idea, because it was not being emitted with the undo/redo system. The signal should also fire *after* the cel changes, and this is what happens in this commit. Should be useful for future features and extensions to check whether a cel has been changed. --- src/Autoload/OpenSave.gd | 16 +++++----- src/Classes/Project.gd | 7 ++-- src/UI/Timeline/AnimationTimeline.gd | 48 ++++++++++++++-------------- src/UI/Timeline/CelButton.gd | 10 +++--- src/UI/Timeline/FrameButton.gd | 14 ++++---- src/UI/Timeline/LayerButton.gd | 12 +++---- 6 files changed, 52 insertions(+), 55 deletions(-) diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index 1b53fe69c..c627f9b56 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -549,14 +549,14 @@ func open_image_as_spritesheet_layer( project.undo_redo.add_do_method(project, "add_frames", frames, frame_indices) project.undo_redo.add_do_method(project, "add_layers", [layer], [project.layers.size()], [cels]) project.undo_redo.add_do_method( - project, "_cel_changed", new_frames_size - 1, project.layers.size() + project, "change_cel", new_frames_size - 1, project.layers.size() ) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.add_undo_method(project, "remove_layers", [project.layers.size()]) project.undo_redo.add_undo_method(project, "remove_frames", frame_indices) project.undo_redo.add_undo_method( - project, "_cel_changed", project.current_frame, project.current_layer + project, "change_cel", project.current_frame, project.current_layer ) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.commit_action() @@ -576,12 +576,12 @@ func open_image_at_cel(image: Image, layer_index := 0, frame_index := 0) -> void project.undo_redo.add_undo_property(cel, "image", cel.image) project.undo_redo.add_do_property(project, "selected_cels", []) - project.undo_redo.add_do_method(project, "_cel_changed", frame_index, layer_index) + project.undo_redo.add_do_method(project, "change_cel", frame_index, layer_index) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.add_undo_property(project, "selected_cels", []) project.undo_redo.add_undo_method( - project, "_cel_changed", project.current_frame, project.current_layer + project, "change_cel", project.current_frame, project.current_layer ) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.commit_action() @@ -603,12 +603,12 @@ func open_image_as_new_frame(image: Image, layer_index := 0) -> void: project.undo_redo.create_action("Add Frame") project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.add_do_method(project, "add_frames", [frame], [project.frames.size()]) - project.undo_redo.add_do_method(project, "_cel_changed", project.frames.size(), layer_index) + project.undo_redo.add_do_method(project, "change_cel", project.frames.size(), layer_index) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.add_undo_method(project, "remove_frames", [project.frames.size()]) project.undo_redo.add_undo_method( - project, "_cel_changed", project.current_frame, project.current_layer + project, "change_cel", project.current_frame, project.current_layer ) project.undo_redo.commit_action() @@ -629,11 +629,11 @@ func open_image_as_new_layer(image: Image, file_name: String, frame_index := 0) cels.append(layer.new_empty_cel()) project.undo_redo.add_do_method(project, "add_layers", [layer], [project.layers.size()], [cels]) - project.undo_redo.add_do_method(project, "_cel_changed", frame_index, project.layers.size()) + project.undo_redo.add_do_method(project, "change_cel", frame_index, project.layers.size()) project.undo_redo.add_undo_method(project, "remove_layers", [project.layers.size()]) project.undo_redo.add_undo_method( - project, "_cel_changed", project.current_frame, project.current_layer + project, "change_cel", project.current_frame, project.current_layer ) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) diff --git a/src/Classes/Project.gd b/src/Classes/Project.gd index 6b2d5eaab..7e29e740c 100644 --- a/src/Classes/Project.gd +++ b/src/Classes/Project.gd @@ -3,8 +3,7 @@ class_name Project extends Reference # A class for project properties. -# warning-ignore:unused_signal -signal cel_changed(frame, layer) +signal cel_changed var name := "" setget _name_changed var size: Vector2 setget _size_changed @@ -62,7 +61,6 @@ func _init(_frames := [], _name := tr("untitled"), _size := Vector2(64, 64)) -> size = _size tiles = Tiles.new(size) selection_map.create(size.x, size.y, false, Image.FORMAT_LA8) - connect("cel_changed", self, "_cel_changed") Global.tabs.add_tab(name) OpenSave.current_save_paths.append("") @@ -468,7 +466,7 @@ func _size_changed(value: Vector2) -> void: size = value -func _cel_changed(new_frame: int, new_layer := -1) -> void: +func change_cel(new_frame: int, new_layer := -1) -> void: if new_frame < 0: new_frame = current_frame if new_layer < 0: @@ -520,6 +518,7 @@ func _cel_changed(new_frame: int, new_layer := -1) -> void: Global.layer_opacity_slider.value = cel_opacity * 100 Global.canvas.update() Global.transparent_checker.update_rect() + emit_signal("cel_changed") func toggle_frame_buttons() -> void: diff --git a/src/UI/Timeline/AnimationTimeline.gd b/src/UI/Timeline/AnimationTimeline.gd index 7b6b5b8c8..3808b4ae0 100644 --- a/src/UI/Timeline/AnimationTimeline.gd +++ b/src/UI/Timeline/AnimationTimeline.gd @@ -160,8 +160,8 @@ func add_frame() -> void: project.undo_redo.add_undo_method(project, "remove_frames", [frame_add_index]) project.undo_redo.add_do_property(project, "animation_tags", new_animation_tags) project.undo_redo.add_undo_property(project, "animation_tags", project.animation_tags) - project.undo_redo.add_do_method(project, "_cel_changed", project.current_frame + 1) - project.undo_redo.add_undo_method(project, "_cel_changed", project.current_frame) + project.undo_redo.add_do_method(project, "change_cel", project.current_frame + 1) + project.undo_redo.add_undo_method(project, "change_cel", project.current_frame) project.undo_redo.commit_action() @@ -225,8 +225,8 @@ func delete_frames(indices := []) -> void: project.undo_redo.add_undo_method(project, "add_frames", frames, indices) project.undo_redo.add_do_property(project, "animation_tags", new_animation_tags) project.undo_redo.add_undo_property(project, "animation_tags", project.animation_tags) - project.undo_redo.add_do_method(project, "_cel_changed", current_frame) - project.undo_redo.add_undo_method(project, "_cel_changed", project.current_frame) + project.undo_redo.add_do_method(project, "change_cel", current_frame) + project.undo_redo.add_undo_method(project, "change_cel", project.current_frame) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.commit_action() @@ -301,8 +301,8 @@ func copy_frames(indices := []) -> void: project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.add_do_method(project, "add_frames", copied_frames, copied_indices) project.undo_redo.add_undo_method(project, "remove_frames", copied_indices) - project.undo_redo.add_do_method(project, "_cel_changed", indices[-1] + 1) - project.undo_redo.add_undo_method(project, "_cel_changed", indices[-1]) + project.undo_redo.add_do_method(project, "change_cel", indices[-1] + 1) + project.undo_redo.add_undo_method(project, "change_cel", indices[-1]) project.undo_redo.add_do_property(project, "animation_tags", new_animation_tags) project.undo_redo.add_undo_property(project, "animation_tags", project.animation_tags) project.undo_redo.commit_action() @@ -396,7 +396,7 @@ func _on_AnimationTimer_timeout() -> void: if animation_forward: if project.current_frame < last_frame: project.selected_cels.clear() - project.emit_signal("cel_changed", project.current_frame + 1, -1) + project.change_cel(project.current_frame + 1, -1) Global.animation_timer.wait_time = ( project.frames[project.current_frame].duration * (1 / fps) @@ -411,7 +411,7 @@ func _on_AnimationTimer_timeout() -> void: is_animation_running = false 1: # Cycle loop project.selected_cels.clear() - project.emit_signal("cel_changed", first_frame, -1) + project.change_cel(first_frame, -1) Global.animation_timer.wait_time = ( project.frames[project.current_frame].duration * (1 / fps) @@ -424,7 +424,7 @@ func _on_AnimationTimer_timeout() -> void: else: if project.current_frame > first_frame: project.selected_cels.clear() - project.emit_signal("cel_changed", project.current_frame - 1, -1) + project.change_cel(project.current_frame - 1, -1) Global.animation_timer.wait_time = ( project.frames[project.current_frame].duration * (1 / fps) @@ -439,7 +439,7 @@ func _on_AnimationTimer_timeout() -> void: is_animation_running = false 1: # Cycle loop project.selected_cels.clear() - project.emit_signal("cel_changed", last_frame, -1) + project.change_cel(last_frame, -1) Global.animation_timer.wait_time = ( project.frames[project.current_frame].duration * (1 / fps) @@ -501,7 +501,7 @@ func _on_NextFrame_pressed() -> void: Global.canvas.selection.transform_content_confirm() project.selected_cels.clear() if project.current_frame < project.frames.size() - 1: - project.emit_signal("cel_changed", project.current_frame + 1, -1) + project.change_cel(project.current_frame + 1, -1) func _on_PreviousFrame_pressed() -> void: @@ -509,19 +509,19 @@ func _on_PreviousFrame_pressed() -> void: Global.canvas.selection.transform_content_confirm() project.selected_cels.clear() if project.current_frame > 0: - project.emit_signal("cel_changed", project.current_frame - 1, -1) + project.change_cel(project.current_frame - 1, -1) func _on_LastFrame_pressed() -> void: Global.canvas.selection.transform_content_confirm() Global.current_project.selected_cels.clear() - Global.current_project.emit_signal("cel_changed", Global.current_project.frames.size() - 1, -1) + Global.current_project.change_cel(Global.current_project.frames.size() - 1, -1) func _on_FirstFrame_pressed() -> void: Global.canvas.selection.transform_content_confirm() Global.current_project.selected_cels.clear() - Global.current_project.emit_signal("cel_changed", 0, -1) + Global.current_project.change_cel(0, -1) func _on_FPSValue_value_changed(value: float) -> void: @@ -591,8 +591,8 @@ func add_layer(type: int) -> void: project.undo_redo.create_action("Add Layer") project.undo_redo.add_do_method(project, "add_layers", [l], [new_layer_idx], [cels]) project.undo_redo.add_undo_method(project, "remove_layers", [new_layer_idx]) - project.undo_redo.add_do_method(project, "_cel_changed", -1, new_layer_idx) - project.undo_redo.add_undo_method(project, "_cel_changed", -1, project.current_layer) + project.undo_redo.add_do_method(project, "change_cel", -1, new_layer_idx) + project.undo_redo.add_undo_method(project, "change_cel", -1, project.current_layer) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.commit_action() @@ -651,9 +651,9 @@ func _on_CloneLayer_pressed() -> void: project.undo_redo.add_do_method(project, "add_layers", clones, indices, cels) project.undo_redo.add_undo_method(project, "remove_layers", indices) project.undo_redo.add_do_method( - project, "_cel_changed", -1, project.current_layer + clones.size() + project, "change_cel", -1, project.current_layer + clones.size() ) - project.undo_redo.add_undo_method(project, "_cel_changed", -1, project.current_layer) + project.undo_redo.add_undo_method(project, "change_cel", -1, project.current_layer) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.commit_action() @@ -680,8 +680,8 @@ func _on_RemoveLayer_pressed() -> void: project.undo_redo.create_action("Remove Layer") project.undo_redo.add_do_method(project, "remove_layers", indices) project.undo_redo.add_undo_method(project, "add_layers", layers, indices, cels) - project.undo_redo.add_do_method(project, "_cel_changed", -1, max(indices[0] - 1, 0)) - project.undo_redo.add_undo_method(project, "_cel_changed", -1, project.current_layer) + project.undo_redo.add_do_method(project, "change_cel", -1, max(indices[0] - 1, 0)) + project.undo_redo.add_undo_method(project, "change_cel", -1, project.current_layer) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.commit_action() @@ -737,8 +737,8 @@ func change_layer_order(up: bool) -> void: project.undo_redo.add_undo_method( project, "move_layers", to_indices, from_indices, from_parents ) - project.undo_redo.add_do_method(project, "_cel_changed", -1, to_index + child_count) - project.undo_redo.add_undo_method(project, "_cel_changed", -1, project.current_layer) + project.undo_redo.add_do_method(project, "change_cel", -1, to_index + child_count) + project.undo_redo.add_undo_method(project, "change_cel", -1, project.current_layer) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.commit_action() @@ -797,8 +797,8 @@ func _on_MergeDownLayer_pressed() -> void: project.undo_redo.add_undo_method( project, "add_layers", [top_layer], [top_layer.index], [top_cels] ) - project.undo_redo.add_do_method(project, "_cel_changed", -1, bottom_layer.index) - project.undo_redo.add_undo_method(project, "_cel_changed", -1, top_layer.index) + project.undo_redo.add_do_method(project, "change_cel", -1, bottom_layer.index) + project.undo_redo.add_undo_method(project, "change_cel", -1, top_layer.index) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.commit_action() diff --git a/src/UI/Timeline/CelButton.gd b/src/UI/Timeline/CelButton.gd index 6ada9d3b0..b4db1d96f 100644 --- a/src/UI/Timeline/CelButton.gd +++ b/src/UI/Timeline/CelButton.gd @@ -78,11 +78,9 @@ func _on_CelButton_pressed() -> void: project.selected_cels.append(frame_layer) if change_cel: - project.emit_signal("cel_changed", frame, layer) + project.change_cel(frame, layer) else: - project.emit_signal( - "cel_changed", project.selected_cels[0][0], project.selected_cels[0][1] - ) + project.change_cel(project.selected_cels[0][0], project.selected_cels[0][1]) release_focus() elif Input.is_action_just_released("right_mouse"): @@ -264,9 +262,9 @@ func drop_data(_pos, data) -> void: project.undo_redo.add_do_method(project, "move_cel", drop_frame, to_frame, layer) project.undo_redo.add_undo_method(project, "move_cel", to_frame, drop_frame, layer) - project.undo_redo.add_do_method(project, "_cel_changed", frame, layer) + project.undo_redo.add_do_method(project, "change_cel", frame, layer) project.undo_redo.add_undo_method( - project, "_cel_changed", project.current_frame, project.current_layer + project, "change_cel", project.current_frame, project.current_layer ) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.add_do_method(Global, "undo_or_redo", false) diff --git a/src/UI/Timeline/FrameButton.gd b/src/UI/Timeline/FrameButton.gd index a17fe052e..6f6a7872c 100644 --- a/src/UI/Timeline/FrameButton.gd +++ b/src/UI/Timeline/FrameButton.gd @@ -46,7 +46,7 @@ func _button_pressed() -> void: if !Global.current_project.selected_cels.has(frame_layer): Global.current_project.selected_cels.append(frame_layer) - Global.current_project.emit_signal("cel_changed", frame, -1) + Global.current_project.change_cel(frame, -1) elif Input.is_action_just_released("right_mouse"): if Global.current_project.frames.size() == 1: @@ -94,11 +94,11 @@ func change_frame_order(rate: int) -> void: project.undo_redo.add_undo_method(project, "move_frame", change, frame) if project.current_frame == frame: - project.undo_redo.add_do_method(project, "_cel_changed", change) + project.undo_redo.add_do_method(project, "change_cel", change) else: - project.undo_redo.add_do_method(project, "_cel_changed", project.current_frame) + project.undo_redo.add_do_method(project, "change_cel", project.current_frame) - project.undo_redo.add_undo_method(project, "_cel_changed", project.current_frame) + project.undo_redo.add_undo_method(project, "change_cel", project.current_frame) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.commit_action() @@ -155,10 +155,10 @@ func drop_data(_pos, data) -> void: project.undo_redo.add_undo_method(project, "move_frame", to_frame, drop_frame) if project.current_frame == drop_frame: - project.undo_redo.add_do_method(project, "_cel_changed", frame) + project.undo_redo.add_do_method(project, "change_cel", frame) else: - project.undo_redo.add_do_method(project, "_cel_changed", project.current_frame) - project.undo_redo.add_undo_method(project, "_cel_changed", project.current_frame) + project.undo_redo.add_do_method(project, "change_cel", project.current_frame) + project.undo_redo.add_undo_method(project, "change_cel", project.current_frame) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.commit_action() diff --git a/src/UI/Timeline/LayerButton.gd b/src/UI/Timeline/LayerButton.gd index ddae2f177..ab28673c2 100644 --- a/src/UI/Timeline/LayerButton.gd +++ b/src/UI/Timeline/LayerButton.gd @@ -119,13 +119,13 @@ func _on_LayerContainer_gui_input(event: InputEvent) -> void: var frame_layer := [i, j] if !project.selected_cels.has(frame_layer): project.selected_cels.append(frame_layer) - project.emit_signal("cel_changed", -1, layer) + project.change_cel(-1, layer) elif Input.is_action_pressed("ctrl"): for i in range(0, project.frames.size()): var frame_layer := [i, layer] if !project.selected_cels.has(frame_layer): project.selected_cels.append(frame_layer) - project.emit_signal("cel_changed", -1, layer) + project.change_cel(-1, layer) else: # If the button is pressed without Shift or Control _select_current_layer() @@ -181,7 +181,7 @@ func _select_current_layer() -> void: if !Global.current_project.selected_cels.has(frame_layer): Global.current_project.selected_cels.append(frame_layer) - Global.current_project.emit_signal("cel_changed", -1, layer) + Global.current_project.change_cel(-1, layer) func get_drag_data(_position) -> Array: @@ -339,10 +339,10 @@ func drop_data(_pos, data) -> void: project, "move_layers", drop_to_indices, drop_from_indices, drop_from_parents ) if project.current_layer == drop_layer: - project.undo_redo.add_do_method(project, "_cel_changed", -1, layer) + project.undo_redo.add_do_method(project, "change_cel", -1, layer) else: - project.undo_redo.add_do_method(project, "_cel_changed", -1, project.current_layer) - project.undo_redo.add_undo_method(project, "_cel_changed", -1, project.current_layer) + project.undo_redo.add_do_method(project, "change_cel", -1, project.current_layer) + project.undo_redo.add_undo_method(project, "change_cel", -1, project.current_layer) project.undo_redo.add_undo_method(Global, "undo_or_redo", true) project.undo_redo.add_do_method(Global, "undo_or_redo", false) project.undo_redo.commit_action()