From 8bc08798149d00d647523bb994cbb94c7c402a74 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Tue, 5 Nov 2019 03:14:03 +0200 Subject: [PATCH] UndoRedo vol 7 - New/Open/Import clear history, Crop has undo/redo - New, Open and Import now clear undo/redo history - Crop Image now has undo/redo - Fixed bug where redo wasn't working properly in multiple frames Found bug when drawing while animating - undo/redo isn't being registered properly. --- Scripts/Canvas.gd | 7 ++++--- Scripts/Global.gd | 6 ++++++ Scripts/Main.gd | 28 +++++++++++++++++----------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Scripts/Canvas.gd b/Scripts/Canvas.gd index c3d36edbf..c0f0bd65e 100644 --- a/Scripts/Canvas.gd +++ b/Scripts/Canvas.gd @@ -96,7 +96,7 @@ func _process(delta) -> void: #Handle Undo/Redo - var can_handle : bool = mouse_in_canvas && Global.can_draw && Global.has_focus && Global.current_frame == frame + var can_handle : bool = mouse_in_canvas && Global.can_draw && Global.has_focus var mouse_pressed : bool = (Input.is_action_just_pressed("left_mouse") && !Input.is_action_pressed("right_mouse")) || (Input.is_action_just_pressed("right_mouse") && !Input.is_action_pressed("left_mouse")) #If we're already pressing a mouse button and we haven't handled undo yet,... @@ -107,16 +107,17 @@ func _process(delta) -> void: mouse_pressed = true if mouse_pressed: - if can_handle: + if can_handle && Global.current_frame == frame: if current_action != "None": if current_action == "RectSelect": handle_undo("Rectangle Select") else: handle_undo("Draw") elif (Input.is_action_just_released("left_mouse") && !Input.is_action_pressed("right_mouse")) || (Input.is_action_just_released("right_mouse") && !Input.is_action_pressed("left_mouse")): - if can_handle || Global.undos == Global.undo_redo.get_version(): + if (can_handle || Global.undos == Global.undo_redo.get_version()) && Global.current_frame == frame: if previous_action != "None" && previous_action != "RectSelect": handle_redo("Draw") + print(layers[0][0].data["data"][0]) match current_action: #Handle current tool "Pencil": diff --git a/Scripts/Global.gd b/Scripts/Global.gd index 191712955..8ffd73b42 100644 --- a/Scripts/Global.gd +++ b/Scripts/Global.gd @@ -171,6 +171,9 @@ func undo(canvas : Canvas, layer_index : int = -1) -> void: else: for i in canvas.layers.size(): canvas.update_texture(i) + + if action_name == "Scale": + canvas.camera_zoom() print("Undo: ", action_name) func redo(canvas : Canvas, layer_index : int = -1) -> void: @@ -183,6 +186,9 @@ func redo(canvas : Canvas, layer_index : int = -1) -> void: else: for i in canvas.layers.size(): canvas.update_texture(i) + + if action_name == "Scale": + canvas.camera_zoom() print("Redo: ", action_name) func change_frame() -> void: diff --git a/Scripts/Main.gd b/Scripts/Main.gd index d48b28bd7..1953a51e3 100644 --- a/Scripts/Main.gd +++ b/Scripts/Main.gd @@ -170,18 +170,21 @@ func edit_menu_id_pressed(id : int) -> void: if used_rect == Rect2(0, 0, 0, 0): return + var width := used_rect.size.x + var height := used_rect.size.y + Global.undos += 1 + Global.undo_redo.create_action("Scale") + Global.undo_redo.add_do_property(Global.canvas, "size", Vector2(width, height).floor()) #Loop through all the layers to crop them for j in range(Global.canvas.layers.size() - 1, -1, -1): - var sprite := Image.new() - sprite = Global.canvas.layers[j][0].get_rect(used_rect) - Global.canvas.layers[j][0] = sprite - Global.canvas.layers[j][0].lock() - Global.canvas.update_texture(j) + var sprite : Image = Global.canvas.layers[j][0].get_rect(used_rect) + Global.undo_redo.add_do_property(Global.canvas.layers[j][0], "data", sprite.data) + Global.undo_redo.add_undo_property(Global.canvas.layers[j][0], "data", Global.canvas.layers[j][0].data) - var width = Global.canvas.layers[0][0].get_width() - var height = Global.canvas.layers[0][0].get_height() - Global.canvas.size = Vector2(width, height).floor() - Global.canvas.camera_zoom() + Global.undo_redo.add_undo_property(Global.canvas, "size", Global.canvas.size) + 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() 4: #Clear selection Global.canvas.handle_undo("Rectangle Select") Global.selection_rectangle.polygon[0] = Vector2.ZERO @@ -237,6 +240,7 @@ func _on_CreateNewImage_confirmed() -> void: Global.canvas.update_texture(0) Global.remove_frame_button.disabled = true Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN + Global.undo_redo.clear_history(false) func _on_OpenSprite_file_selected(path) -> void: var file := File.new() @@ -306,6 +310,8 @@ func _on_OpenSprite_file_selected(path) -> void: Global.create_brush_button(image) brush_line = file.get_line() + Global.undo_redo.clear_history(false) + file.close() func _on_SaveSprite_file_selected(path) -> void: @@ -394,6 +400,8 @@ func _on_ImportSprites_files_selected(paths) -> void: Global.remove_frame_button.disabled = true Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN + Global.undo_redo.clear_history(false) + func clear_canvases() -> void: for child in Global.vbox_layer_container.get_children(): if child is PanelContainer: @@ -506,13 +514,11 @@ func _on_ScaleImage_confirmed() -> void: sprite.resize(width, height, interpolation) Global.undo_redo.add_do_property(Global.canvas.layers[i][0], "data", sprite.data) Global.undo_redo.add_undo_property(Global.canvas.layers[i][0], "data", Global.canvas.layers[i][0].data) - Global.canvas.layers[i][0].lock() Global.undo_redo.add_undo_property(Global.canvas, "size", Global.canvas.size) 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() - Global.canvas.camera_zoom() func add_layer(is_new := true) -> void: var new_layer := Image.new()