From 76b7014d84313e86fcd7aca4720e838015e0bb25 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 21 Jun 2020 21:39:16 +0300 Subject: [PATCH] Added UndoRedo support for importing images as frames and layers --- src/Autoload/OpenSave.gd | 40 ++++++++++++++++++++++++++++++++-------- src/UI/CanvasPreview.gd | 2 +- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index 9f04713f6..34729ffbc 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -357,6 +357,7 @@ func open_image_as_spritesheet(path : String, image : Image, horizontal : int, v func open_image_as_new_frame(image : Image, layer_index := 0) -> void: var project = Global.current_project image.crop(project.size.x, project.size.y) + var new_frames : Array = project.frames.duplicate() var frame := Frame.new() for i in project.layers.size(): @@ -370,15 +371,27 @@ func open_image_as_new_frame(image : Image, layer_index := 0) -> void: empty_image.lock() frame.cels.append(Cel.new(empty_image, 1)) - project.frames.append(frame) - project.frames = project.frames # Just to call frames_changed() - project.current_frame = project.frames.size() - 1 - project.current_layer = layer_index + new_frames.append(frame) + + Global.current_project.undos += 1 + Global.current_project.undo_redo.create_action("Add Frame") + Global.current_project.undo_redo.add_do_method(Global, "redo") + Global.current_project.undo_redo.add_undo_method(Global, "undo") + + Global.current_project.undo_redo.add_do_property(project, "frames", new_frames) + Global.current_project.undo_redo.add_do_property(project, "current_frame", new_frames.size() - 1) + Global.current_project.undo_redo.add_do_property(project, "current_layer", layer_index) + + Global.current_project.undo_redo.add_undo_property(project, "frames", project.frames) + Global.current_project.undo_redo.add_undo_property(project, "current_frame", project.current_frame) + Global.current_project.undo_redo.add_undo_property(project, "current_layer", project.current_layer) + Global.current_project.undo_redo.commit_action() func open_image_as_new_layer(image : Image, file_name : String, frame_index := 0) -> void: var project = Global.current_project image.crop(project.size.x, project.size.y) + var new_layers : Array = Global.current_project.layers.duplicate() var layer := Layer.new(file_name) for i in project.frames.size(): @@ -392,10 +405,21 @@ func open_image_as_new_layer(image : Image, file_name : String, frame_index := 0 empty_image.lock() project.frames[i].cels.append(Cel.new(empty_image, 1)) - project.layers.append(layer) - project.layers = project.layers # Just to call layers_changed() - project.current_layer = project.layers.size() - 1 - project.current_frame = frame_index + new_layers.append(layer) + + Global.current_project.undos += 1 + Global.current_project.undo_redo.create_action("Add Layer") + Global.current_project.undo_redo.add_do_property(project, "current_layer", new_layers.size() - 1) + Global.current_project.undo_redo.add_do_property(project, "layers", new_layers) + Global.current_project.undo_redo.add_do_property(project, "current_frame", frame_index) + + Global.current_project.undo_redo.add_undo_property(project, "current_layer", project.current_layer) + Global.current_project.undo_redo.add_undo_property(project, "layers", project.layers) + Global.current_project.undo_redo.add_undo_property(project, "current_frame", project.current_frame) + + Global.current_project.undo_redo.add_undo_method(Global, "undo") + Global.current_project.undo_redo.add_do_method(Global, "redo") + Global.current_project.undo_redo.commit_action() func set_new_tab(project : Project, path : String) -> void: diff --git a/src/UI/CanvasPreview.gd b/src/UI/CanvasPreview.gd index e4a4c97af..222180e5b 100644 --- a/src/UI/CanvasPreview.gd +++ b/src/UI/CanvasPreview.gd @@ -9,5 +9,5 @@ func _draw() -> void: # Draw current frame layers for i in range(current_cels.size()): var modulate_color := Color(1, 1, 1, current_cels[i].opacity) - if current_project.layers[i].visible: # if it's visible + if i < current_project.layers.size() and current_project.layers[i].visible: # if it's visible draw_texture(current_cels[i].image_texture, Vector2.ZERO, modulate_color)