From 28b8e000f9841d43b613764e59006993c8dddd1e Mon Sep 17 00:00:00 2001 From: Manolis Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Tue, 20 Jul 2021 21:30:27 +0300 Subject: [PATCH] Rewrite the crop image method Should be a bit faster, but I still get weird bugs at random --- src/Autoload/DrawingAlgos.gd | 31 ++++++++++++++----------------- src/UI/TopMenuContainer.gd | 3 +-- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/Autoload/DrawingAlgos.gd b/src/Autoload/DrawingAlgos.gd index d56f6b526..262ccecb1 100644 --- a/src/Autoload/DrawingAlgos.gd +++ b/src/Autoload/DrawingAlgos.gd @@ -256,24 +256,21 @@ func centralize() -> void: general_undo_centralize() -func crop_image(image : Image) -> void: +func crop_image() -> void: Global.canvas.selection.transform_content_confirm() # Use first cel as a starting rectangle - var used_rect : Rect2 = image.get_used_rect() + var used_rect : Rect2 = Global.current_project.frames[0].cels[0].image.get_used_rect() for f in Global.current_project.frames: - # However, if first cel is empty, loop through all cels until we find one that isn't for cel in f.cels: - if used_rect != Rect2(0, 0, 0, 0): - break - else: - if cel.image.get_used_rect() != Rect2(0, 0, 0, 0): - used_rect = cel.image.get_used_rect() + var cel_used_rect : Rect2 = cel.image.get_used_rect() + if cel_used_rect == Rect2(0, 0, 0, 0): # If the cel has no content + continue - # Merge all layers with content - for cel in f.cels: - if cel.image.get_used_rect() != Rect2(0, 0, 0, 0): - used_rect = used_rect.merge(cel.image.get_used_rect()) + if used_rect == Rect2(0, 0, 0, 0): # If we still haven't found the first cel with content + used_rect = cel_used_rect + else: + used_rect = used_rect.merge(cel_used_rect) # If no layer has any content, just return if used_rect == Rect2(0, 0, 0, 0): @@ -282,12 +279,12 @@ func crop_image(image : Image) -> void: var width := used_rect.size.x var height := used_rect.size.y general_do_scale(width, height) + # Loop through all the cels to crop them for f in Global.current_project.frames: - # Loop through all the layers to crop them - for j in range(Global.current_project.layers.size() - 1, -1, -1): - var sprite : Image = f.cels[j].image.get_rect(used_rect) - Global.current_project.undo_redo.add_do_property(f.cels[j].image, "data", sprite.data) - Global.current_project.undo_redo.add_undo_property(f.cels[j].image, "data", f.cels[j].image.data) + for cel in f.cels: + var sprite : Image = cel.image.get_rect(used_rect) + Global.current_project.undo_redo.add_do_property(cel.image, "data", sprite.data) + Global.current_project.undo_redo.add_undo_property(cel.image, "data", cel.image.data) general_undo_scale() diff --git a/src/UI/TopMenuContainer.gd b/src/UI/TopMenuContainer.gd index 931d20ea2..4ed9ea554 100644 --- a/src/UI/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer.gd @@ -445,7 +445,6 @@ func toggle_fullscreen() -> void: func image_menu_id_pressed(id : int) -> void: - var image : Image = Global.current_project.frames[Global.current_project.current_frame].cels[Global.current_project.current_layer].image match id: ImageMenuId.SCALE_IMAGE: show_scale_image_popup() @@ -454,7 +453,7 @@ func image_menu_id_pressed(id : int) -> void: DrawingAlgos.centralize() ImageMenuId.CROP_IMAGE: - DrawingAlgos.crop_image(image) + DrawingAlgos.crop_image() ImageMenuId.RESIZE_CANVAS: show_resize_canvas_popup()