From 04c3173c4ca8a971c30d1953a13a45824f26393d Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Fri, 1 Nov 2019 01:41:02 +0200 Subject: [PATCH] UndoRedo vol 3 - Fixed bug when user clicked both mouse buttons Drawing outside canvas needs fixing next in our UndoRedo adventure --- Scripts/Canvas.gd | 7 +++++-- Scripts/Global.gd | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Scripts/Canvas.gd b/Scripts/Canvas.gd index 7e1583935..c2bf37fba 100644 --- a/Scripts/Canvas.gd +++ b/Scripts/Canvas.gd @@ -96,14 +96,14 @@ func _process(delta) -> void: #Handle Undo/Redo if point_in_rectangle(mouse_pos, location, location + size) && Global.can_draw && Global.has_focus && Global.current_frame == frame: - if Input.is_action_just_pressed("left_mouse") || (Input.is_action_just_pressed("right_mouse") && !Input.is_action_pressed("left_mouse")): + if (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 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_just_released("right_mouse") && !Input.is_action_pressed("left_mouse")): + 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 previous_action != "None" && previous_action != "RectSelect": handle_redo("Draw") @@ -230,6 +230,7 @@ func _process(delta) -> void: update_texture(current_layer_index) func handle_undo(action : String) -> void: + Global.undos += 1 #I'm not sure why I have to unlock it, but... #...if I don't, it doesn't work properly layers[current_layer_index][0].unlock() @@ -244,6 +245,8 @@ func handle_undo(action : String) -> void: Global.undo_redo.add_undo_method(Global, "undo", self, current_layer_index) func handle_redo(action : String) -> void: + if Global.undos < Global.undo_redo.get_version(): + return Global.undo_redo.add_do_property(layers[current_layer_index][0], "data", layers[current_layer_index][0].data) if action == "Rectangle Select": Global.undo_redo.add_do_property(Global.selection_rectangle, "polygon", Global.selection_rectangle.polygon) diff --git a/Scripts/Global.gd b/Scripts/Global.gd index 2f1c49043..5432fb422 100644 --- a/Scripts/Global.gd +++ b/Scripts/Global.gd @@ -1,6 +1,7 @@ extends Node var undo_redo : UndoRedo +var undos := 0 #The number of times we added undo properties var current_frame := 0 setget set_current_frame_label # warning-ignore:unused_class_variable var can_draw := false @@ -162,12 +163,15 @@ func find_node_by_name(root, node_name) -> Node: return null func undo(canvas : Canvas, layer_index : int) -> void: + undos -= 1 var action_name : String = undo_redo.get_current_action_name() if action_name == "Draw" || action_name == "Rectangle Select": canvas.update_texture(layer_index) print("Undo: ", action_name) func redo(canvas : Canvas, layer_index : int) -> 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": canvas.update_texture(layer_index)