From 45c22ab9fdaf88c9d0418a436eaaf4dd02dbf521 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 18 Nov 2022 22:08:53 +0100 Subject: [PATCH] Reimplemented undo and redo for PaintCanvas. --- game/PaintCanvas.gd | 66 ++++++++++++++++++++++++++++++++++++++++++--- game/Project.tscn | 4 +-- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/game/PaintCanvas.gd b/game/PaintCanvas.gd index 59f6bc9..a117141 100644 --- a/game/PaintCanvas.gd +++ b/game/PaintCanvas.gd @@ -205,7 +205,6 @@ func draw_brush_preview() -> void: # paint_canvas->update(); #} - func do_action(arr : Array) -> void: if !_current_action: return @@ -237,10 +236,42 @@ func commit_action() -> void: current_tool = TOOL_PASTECUT - return - _current_action = null +func redo_action() -> void: + if (_redo_history.empty()): + print("PaintCanvas: nothing to redo"); + return; + + var action : PaintAction = _redo_history[_redo_history.size() - 1]; + _redo_history.remove(_redo_history.size() - 1); + + if (!action): + return; + + _actions_history.push_back(action); + action.redo_action(); + update_textures() + + print("PaintCanvas: redo action"); + +func undo_action() -> void: + if (_actions_history.empty()): + print("PaintCanvas: nothing to undo"); + return; + + var action : PaintAction = _actions_history[_actions_history.size() - 1]; + _actions_history.remove(_actions_history.size() - 1); + + if (!action): + return; + + _redo_history.push_back(action); + action.undo_action(); + update_textures() + + print("PaintCanvas: undo action") + #void PaintWindow::redo_action_old() { # if (_redo_history.empty()) { # //print("nothing to redo"); @@ -260,6 +291,7 @@ func commit_action() -> void: # # //print("redo action"); #} + #void PaintWindow::undo_action_old() { # if (_actions_history.empty()) { # return; @@ -453,6 +485,34 @@ func _forward_canvas_gui_input(event: InputEvent) -> bool: last_mouse_position = mouse_position last_cell_mouse_position = local_position + + if event is InputEventKey: + if event.echo || !event.pressed: + return false + + var scancode : int = event.get_physical_scancode_with_modifiers() + + var undo : bool = false + if (scancode == (KEY_Z | KEY_MASK_CTRL)): + undo = true + + var redo : bool = false + if (scancode == (KEY_Z | KEY_MASK_CTRL | KEY_MASK_SHIFT)): + redo = true + + if !undo && !redo: + return false + + var local_position : Vector2 = get_local_mouse_position() + + if has_point(local_position): + if redo: + redo_action() + return true + + if undo: + undo_action() + return true return false diff --git a/game/Project.tscn b/game/Project.tscn index ba180a0..6aa1363 100644 --- a/game/Project.tscn +++ b/game/Project.tscn @@ -28,7 +28,7 @@ size = Vector2( 32, 32 ) [node name="PaintProject" type="PaintProject"] size = Vector2i( 128, 128 ) -current_color = Color( 0.964706, 0.992157, 0.121569, 1 ) +current_color = Color( 0.129412, 0.113725, 0.384314, 1 ) color_presets = PoolColorArray( 0.67914, 0.117493, 0.0852439, 1, 0.129412, 0.113725, 0.384314, 1, 0.0666667, 0.796078, 0.196078, 1, 0.0666667, 0.796078, 0.196078, 1, 0.964706, 0.992157, 0.121569, 1, 0.00784314, 0.835294, 0.564706, 1, 0.0666667, 0.796078, 0.196078, 1, 0.658824, 0.313726, 0.207843, 1 ) script = ExtResource( 2 ) @@ -48,6 +48,6 @@ script = ExtResource( 3 ) [node name="PaintCanvas" type="PaintCanvas" parent="."] size = Vector2i( 128, 128 ) -brush_prefab = 1 brush_size = 3 +current_tool = 6 script = ExtResource( 1 )