From 26aa9bda38f0fc12ece62652b17c0ee634cc5383 Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Wed, 5 Jul 2023 04:00:36 +0500 Subject: [PATCH] Move Centralize Image to the frame menu (#884) * moved centralize to frame menu And make it effect on selected frames * Formatting (1 of 2) * Formatting (2 of 2) --- src/Autoload/DrawingAlgos.gd | 63 +++++++++------------ src/Autoload/Global.gd | 5 -- src/UI/Timeline/FrameButton.gd | 4 +- src/UI/Timeline/FrameButton.tscn | 2 +- src/UI/TopMenuContainer/TopMenuContainer.gd | 4 -- 5 files changed, 31 insertions(+), 47 deletions(-) diff --git a/src/Autoload/DrawingAlgos.gd b/src/Autoload/DrawingAlgos.gd index 79b5878d6..785e82053 100644 --- a/src/Autoload/DrawingAlgos.gd +++ b/src/Autoload/DrawingAlgos.gd @@ -462,32 +462,36 @@ func scale_image(width: int, height: int, interpolation: int) -> void: general_undo_scale() -func centralize() -> void: +func centralize(indices: Array) -> void: + var project: Project = Global.current_project Global.canvas.selection.transform_content_confirm() - # Find used rect of the current frame (across all of the layers) - var used_rect := Rect2() - for cel in Global.current_project.frames[Global.current_project.current_frame].cels: - if not cel is PixelCel: + project.undos += 1 + project.undo_redo.create_action("Centralize") + for frame in indices: + # Find used rect of the current frame (across all of the layers) + var used_rect := Rect2() + for cel in project.frames[frame].cels: + if not cel is PixelCel: + continue + var cel_rect: Rect2 = cel.image.get_used_rect() + if not cel_rect.has_no_area(): + used_rect = cel_rect if used_rect.has_no_area() else used_rect.merge(cel_rect) + if used_rect.has_no_area(): continue - var cel_rect: Rect2 = cel.image.get_used_rect() - if not cel_rect.has_no_area(): - used_rect = cel_rect if used_rect.has_no_area() else used_rect.merge(cel_rect) - if used_rect.has_no_area(): - return - var offset: Vector2 = (0.5 * (Global.current_project.size - used_rect.size)).floor() - general_do_centralize() - for cel in Global.current_project.frames[Global.current_project.current_frame].cels: - if not cel is PixelCel: - continue - var sprite := Image.new() - sprite.create( - Global.current_project.size.x, Global.current_project.size.y, false, Image.FORMAT_RGBA8 - ) - sprite.blend_rect(cel.image, used_rect, offset) - 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_centralize() + # Now apply Centralization + var offset: Vector2 = (0.5 * (project.size - used_rect.size)).floor() + for cel in project.frames[frame].cels: + if not cel is PixelCel: + continue + var sprite := Image.new() + sprite.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8) + sprite.blend_rect(cel.image, used_rect, offset) + project.undo_redo.add_do_property(cel.image, "data", sprite.data) + project.undo_redo.add_undo_property(cel.image, "data", cel.image.data) + project.undo_redo.add_undo_method(Global, "undo_or_redo", true) + project.undo_redo.add_do_method(Global, "undo_or_redo", false) + project.undo_redo.commit_action() func crop_image() -> void: @@ -591,19 +595,6 @@ func general_undo_scale() -> void: project.undo_redo.commit_action() -func general_do_centralize() -> void: - var project: Project = Global.current_project - project.undos += 1 - project.undo_redo.create_action("Centralize") - - -func general_undo_centralize() -> void: - var project: Project = Global.current_project - project.undo_redo.add_undo_method(Global, "undo_or_redo", true) - project.undo_redo.add_do_method(Global, "undo_or_redo", false) - project.undo_redo.commit_action() - - func generate_outline( image: Image, affect_selection: bool, diff --git a/src/Autoload/Global.gd b/src/Autoload/Global.gd index f41091d98..644f65236 100644 --- a/src/Autoload/Global.gd +++ b/src/Autoload/Global.gd @@ -29,7 +29,6 @@ enum ImageMenu { SCALE_IMAGE, CROP_IMAGE, FLIP, - CENTRALIZE_IMAGE, ROTATE, OUTLINE, DROP_SHADOW, @@ -293,10 +292,6 @@ func _initialize_keychain() -> void: Keychain.MenuInputAction.new("", "Edit menu", true, "EditMenu", EditMenu.PREFERENCES), "scale_image": Keychain.MenuInputAction.new("", "Image menu", true, "ImageMenu", ImageMenu.SCALE_IMAGE), - "centralize_image": - Keychain.MenuInputAction.new( - "", "Image menu", true, "ImageMenu", ImageMenu.CENTRALIZE_IMAGE - ), "crop_image": Keychain.MenuInputAction.new("", "Image menu", true, "ImageMenu", ImageMenu.CROP_IMAGE), "resize_canvas": diff --git a/src/UI/Timeline/FrameButton.gd b/src/UI/Timeline/FrameButton.gd index 3d46f86c0..3ddb5b1bc 100644 --- a/src/UI/Timeline/FrameButton.gd +++ b/src/UI/Timeline/FrameButton.gd @@ -1,6 +1,6 @@ extends Button -enum { REMOVE, CLONE, MOVE_LEFT, MOVE_RIGHT, PROPERTIES, REVERSE } +enum { REMOVE, CLONE, MOVE_LEFT, MOVE_RIGHT, PROPERTIES, REVERSE, CENTRALIZE } var frame := 0 @@ -92,6 +92,8 @@ func _on_PopupMenu_id_pressed(id: int) -> void: Global.dialog_open(true) REVERSE: Global.animation_timeline.reverse_frames(indices) + CENTRALIZE: + DrawingAlgos.centralize(indices) func change_frame_order(rate: int) -> void: diff --git a/src/UI/Timeline/FrameButton.tscn b/src/UI/Timeline/FrameButton.tscn index 6a2b6329d..3cc5c93f9 100644 --- a/src/UI/Timeline/FrameButton.tscn +++ b/src/UI/Timeline/FrameButton.tscn @@ -20,7 +20,7 @@ __meta__ = { margin_right = 20.0 margin_bottom = 20.0 mouse_default_cursor_shape = 2 -items = [ "Remove Frame", null, 0, false, true, -1, 0, null, "", false, "Clone Frame", null, 0, false, false, -1, 0, null, "", false, "Move Left", null, 0, false, true, -1, 0, null, "", false, "Move Right", null, 0, false, true, -1, 0, null, "", false, "Frame Properties", null, 0, false, false, -1, 0, null, "", false, "Reverse Frames", null, 0, false, true, 5, 0, null, "", false ] +items = [ "Remove Frame", null, 0, false, true, -1, 0, null, "", false, "Clone Frame", null, 0, false, false, -1, 0, null, "", false, "Move Left", null, 0, false, true, -1, 0, null, "", false, "Move Right", null, 0, false, true, -1, 0, null, "", false, "Frame Properties", null, 0, false, false, -1, 0, null, "", false, "Reverse Frames", null, 0, false, true, 5, 0, null, "", false, "Centralize Frames", null, 0, false, false, 6, 0, null, "", false ] __meta__ = { "_edit_use_anchors_": false } diff --git a/src/UI/TopMenuContainer/TopMenuContainer.gd b/src/UI/TopMenuContainer/TopMenuContainer.gd index 5e96ee46d..5c07ac06c 100644 --- a/src/UI/TopMenuContainer/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer/TopMenuContainer.gd @@ -277,7 +277,6 @@ func _setup_image_menu() -> void: "Scale Image", "Crop Image", "Mirror Image", - "Centralize Image", "Rotate Image", "Outline", "Drop Shadow", @@ -654,9 +653,6 @@ func image_menu_id_pressed(id: int) -> void: Global.ImageMenu.OFFSET_IMAGE: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/OffsetImage")) - Global.ImageMenu.CENTRALIZE_IMAGE: - DrawingAlgos.centralize() - Global.ImageMenu.CROP_IMAGE: DrawingAlgos.crop_image()