From dcebf894bf14b7de371f4661ec80c5f158087a23 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Fri, 29 Jul 2022 17:41:38 +0300 Subject: [PATCH] Fix crash when importing a spritesheet as a new layer, undoing and then exporting Thanks to @Variable-ind for the fix. --- src/Autoload/OpenSave.gd | 44 +++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index 747671801..02c5db1e0 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -430,33 +430,47 @@ func open_image_as_spritesheet_tab(path: String, image: Image, horiz: int, vert: func open_image_as_spritesheet_layer( _path: String, image: Image, file_name: String, horizontal: int, vertical: int, start_frame: int ) -> void: - # data needed to slice images + # Data needed to slice images horizontal = min(horizontal, image.get_size().x) vertical = min(vertical, image.get_size().y) var frame_width := image.get_size().x / horizontal var frame_height := image.get_size().y / vertical - # resize canvas to if "frame_width" or "frame_height" is too large + # Resize canvas to if "frame_width" or "frame_height" is too large var project: Project = Global.current_project var project_width: int = max(frame_width, project.size.x) var project_height: int = max(frame_height, project.size.y) if project.size < Vector2(project_width, project_height): DrawingAlgos.resize_canvas(project_width, project_height, 0, 0) - #initialize undo mechanism + # Initialize undo mechanism project.undos += 1 project.undo_redo.create_action("Add Spritesheet Layer") var new_layers: Array = project.layers.duplicate() - var new_frames: Array = project.frames.duplicate() + var new_frames: Array = [] + # Create a duplicate of "project.frames" + for i in project.frames.size(): + var frame := Frame.new() + frame.cels = project.frames[i].cels.duplicate(true) + new_frames.append(frame) - #Create new frames (if needed) + # Create new frames (if needed) var new_frames_size = start_frame + (vertical * horizontal) if new_frames_size > project.frames.size(): var required_frames = new_frames_size - project.frames.size() for i in required_frames: - var frame: Frame = project.new_empty_frame() - new_frames.insert(project.current_frame + 1, frame) - #Create new layer for spritesheet + var new_frame := Frame.new() + for l_i in range(new_layers.size()): # Create as many cels as there are layers + var new_img := Image.new() + new_img.create(project_width, project_height, false, Image.FORMAT_RGBA8) + new_frame.cels.append(Cel.new(new_img, 1)) + if new_layers[l_i].new_cels_linked: + new_layers[l_i].linked_cels.append(new_frame) + new_frame.cels[l_i].image = new_layers[l_i].linked_cels[0].cels[l_i].image + new_frame.cels[l_i].image_texture = new_layers[l_i].linked_cels[0].cels[l_i].image_texture + new_frames.insert(project.current_frame + 1, new_frame) + + # Create new layer for spritesheet var layer := Layer.new(file_name) new_layers.append(layer) for f in new_frames: @@ -464,8 +478,9 @@ func open_image_as_spritesheet_layer( new_layer.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8) f.cels.append(Cel.new(new_layer, 1)) - # slice spritesheet + # Slice spritesheet var image_no: int = 0 + var layer_index = new_layers.size() - 1 for yy in range(vertical): for xx in range(horizontal): var cropped_image := Image.new() @@ -473,19 +488,16 @@ func open_image_as_spritesheet_layer( Rect2(frame_width * xx, frame_height * yy, frame_width, frame_height) ) cropped_image.crop(project.size.x, project.size.y) - var layer_index = new_layers.size() - 1 var frame_index = start_frame + image_no - for i in new_frames.size(): - if i == frame_index: - cropped_image.convert(Image.FORMAT_RGBA8) - new_frames[i].cels[layer_index] = (Cel.new(cropped_image, 1)) + cropped_image.convert(Image.FORMAT_RGBA8) + new_frames[frame_index].cels[layer_index] = (Cel.new(cropped_image, 1)) image_no += 1 + project.undo_redo.add_do_property(project, "frames", new_frames) + project.undo_redo.add_do_property(project, "layers", new_layers) project.undo_redo.add_do_property(project, "current_frame", new_frames.size() - 1) project.undo_redo.add_do_property(project, "current_layer", project.layers.size()) - project.undo_redo.add_do_property(project, "layers", new_layers) - project.undo_redo.add_do_property(project, "frames", new_frames) project.undo_redo.add_undo_property(project, "current_layer", project.current_layer) project.undo_redo.add_undo_property(project, "current_frame", project.current_frame) project.undo_redo.add_undo_property(project, "layers", project.layers)