From ec7b2129ff5277b9a94270629470370b567d17d2 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Thu, 7 Nov 2019 00:12:40 +0200 Subject: [PATCH] UndoRedo vol 9 - Add/Remove/Clone/Move/Merge Layers now have UndoRedo --- Scripts/Canvas.gd | 1 - Scripts/Global.gd | 16 ++++++++++-- Scripts/Main.gd | 63 ++++++++++++++++++++++++++++++++++++----------- 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/Scripts/Canvas.gd b/Scripts/Canvas.gd index e4e3372e6..37657c575 100644 --- a/Scripts/Canvas.gd +++ b/Scripts/Canvas.gd @@ -283,7 +283,6 @@ func handle_redo(action : String) -> void: Global.undo_redo.add_do_property(Global, "selected_pixels", Global.selected_pixels) Global.undo_redo.add_do_method(Global, "redo", canvases, layer_index) Global.undo_redo.commit_action() - print("Do: ", Global.undo_redo.get_current_action_name()) func update_texture(layer_index : int) -> void: layers[layer_index][1].create_from_image(layers[layer_index][0], 0) diff --git a/Scripts/Global.gd b/Scripts/Global.gd index 4e010ee89..62649ce93 100644 --- a/Scripts/Global.gd +++ b/Scripts/Global.gd @@ -165,7 +165,7 @@ func find_node_by_name(root, node_name) -> Node: func undo(canvases : Array, layer_index : int = -1) -> void: undos -= 1 var action_name : String = undo_redo.get_current_action_name() - if action_name == "Draw" || action_name == "Rectangle Select" || action_name == "Scale": + if action_name == "Draw" || action_name == "Rectangle Select" || action_name == "Scale" || action_name == "Merge Layer": for c in canvases: if layer_index > -1: c.update_texture(layer_index) @@ -175,13 +175,19 @@ func undo(canvases : Array, layer_index : int = -1) -> void: if action_name == "Scale": c.camera_zoom() + if "Layer" in action_name: + var current_layer_index := canvas.current_layer_index + canvas.generate_layer_panels() + if action_name == "Change Layer Order": + canvas.current_layer_index = current_layer_index + canvas.get_layer_container(current_layer_index).changed_selection() print("Undo: ", action_name) func redo(canvases : Array, layer_index : int = -1) -> void: if undos < undo_redo.get_version(): #If we did undo and then redo undos = undo_redo.get_version() var action_name : String = undo_redo.get_current_action_name() - if action_name == "Draw" || action_name == "Rectangle Select" || action_name == "Scale": + if action_name == "Draw" || action_name == "Rectangle Select" || action_name == "Scale" || action_name == "Merge Layer": for c in canvases: if layer_index > -1: c.update_texture(layer_index) @@ -191,6 +197,12 @@ func redo(canvases : Array, layer_index : int = -1) -> void: if action_name == "Scale": c.camera_zoom() + if "Layer" in action_name: + var current_layer_index := canvas.current_layer_index + canvas.generate_layer_panels() + if action_name == "Change Layer Order": + canvas.current_layer_index = current_layer_index + canvas.get_layer_container(current_layer_index).changed_selection() print("Redo: ", action_name) func change_frame() -> void: diff --git a/Scripts/Main.gd b/Scripts/Main.gd index f114b707e..a0f7fe811 100644 --- a/Scripts/Main.gd +++ b/Scripts/Main.gd @@ -528,15 +528,30 @@ func add_layer(is_new := true) -> void: new_layer.lock() var new_layer_tex := ImageTexture.new() new_layer_tex.create_from_image(new_layer, 0) - Global.canvas.layers.append([new_layer, new_layer_tex, null, true]) - Global.canvas.generate_layer_panels() + + var new_layers := Global.canvas.layers.duplicate() + new_layers.append([new_layer, new_layer_tex, null, true]) + Global.undos += 1 + Global.undo_redo.create_action("Add Layer") + Global.undo_redo.add_do_property(Global.canvas, "layers", new_layers) + Global.undo_redo.add_undo_property(Global.canvas, "layers", Global.canvas.layers) + Global.undo_redo.add_undo_method(Global, "undo", [Global.canvas]) + Global.undo_redo.add_do_method(Global, "redo", [Global.canvas]) + Global.undo_redo.commit_action() func _on_AddLayerButton_pressed() -> void: add_layer() func _on_RemoveLayerButton_pressed() -> void: - Global.canvas.layers.remove(Global.canvas.current_layer_index) - Global.canvas.generate_layer_panels() + var new_layers := Global.canvas.layers.duplicate() + new_layers.remove(Global.canvas.current_layer_index) + Global.undos += 1 + Global.undo_redo.create_action("Remove Layer") + Global.undo_redo.add_do_property(Global.canvas, "layers", new_layers) + Global.undo_redo.add_undo_property(Global.canvas, "layers", Global.canvas.layers) + Global.undo_redo.add_undo_method(Global, "undo", [Global.canvas]) + Global.undo_redo.add_do_method(Global, "redo", [Global.canvas]) + Global.undo_redo.commit_action() func _on_MoveUpLayer_pressed() -> void: change_layer_order(1) @@ -547,23 +562,41 @@ func _on_MoveDownLayer_pressed() -> void: func change_layer_order(rate : int) -> void: var change = Global.canvas.current_layer_index + rate - var temp = Global.canvas.layers[Global.canvas.current_layer_index] - Global.canvas.layers[Global.canvas.current_layer_index] = Global.canvas.layers[change] - Global.canvas.layers[change] = temp - - Global.canvas.generate_layer_panels() - Global.canvas.current_layer_index = change - Global.canvas.get_layer_container(Global.canvas.current_layer_index).changed_selection() + var new_layers := Global.canvas.layers.duplicate() + var temp = new_layers[Global.canvas.current_layer_index] + new_layers[Global.canvas.current_layer_index] = new_layers[change] + new_layers[change] = temp + Global.undo_redo.create_action("Change Layer Order") + Global.undo_redo.add_do_property(Global.canvas, "layers", new_layers) + Global.undo_redo.add_do_property(Global.canvas, "current_layer_index", change) + Global.undo_redo.add_undo_property(Global.canvas, "layers", Global.canvas.layers) + Global.undo_redo.add_undo_property(Global.canvas, "current_layer_index", Global.canvas.current_layer_index) + Global.undo_redo.add_undo_method(Global, "undo", [Global.canvas]) + Global.undo_redo.add_do_method(Global, "redo", [Global.canvas]) + Global.undo_redo.commit_action() func _on_CloneLayer_pressed() -> void: add_layer(false) func _on_MergeLayer_pressed() -> void: + var new_layers := Global.canvas.layers.duplicate() + new_layers.remove(Global.canvas.current_layer_index) var selected_layer = Global.canvas.layers[Global.canvas.current_layer_index][0] - Global.canvas.layers[Global.canvas.current_layer_index - 1][0].blend_rect(selected_layer, Rect2(Global.canvas.position, Global.canvas.size), Vector2.ZERO) - Global.canvas.layers[Global.canvas.current_layer_index - 1][0].lock() - Global.canvas.update_texture(Global.canvas.current_layer_index - 1) - _on_RemoveLayerButton_pressed() + + var new_layer := Image.new() + new_layer.copy_from(Global.canvas.layers[Global.canvas.current_layer_index - 1][0]) + new_layer.blend_rect(selected_layer, Rect2(Global.canvas.position, Global.canvas.size), Vector2.ZERO) + + Global.undos += 1 + Global.undo_redo.create_action("Merge Layer") + Global.undo_redo.add_do_property(Global.canvas, "layers", new_layers) + Global.undo_redo.add_do_property(Global.canvas.layers[Global.canvas.current_layer_index - 1][0], "data", new_layer.data) + Global.undo_redo.add_undo_property(Global.canvas, "layers", Global.canvas.layers) + Global.undo_redo.add_undo_property(Global.canvas.layers[Global.canvas.current_layer_index - 1][0], "data", Global.canvas.layers[Global.canvas.current_layer_index - 1][0].data) + + Global.undo_redo.add_undo_method(Global, "undo", [Global.canvas]) + Global.undo_redo.add_do_method(Global, "redo", [Global.canvas]) + Global.undo_redo.commit_action() func _on_LeftIndicatorCheckbox_toggled(button_pressed) -> void: Global.left_square_indicator_visible = button_pressed