From d36f89365c3da0e3edc93938f4483d888c1a002d Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Sun, 24 Dec 2023 01:11:12 +0200 Subject: [PATCH] Change "Crop Image" to "Trim Image" and add a new "Crop Image" that actually crops the image based on the active selection --- Translations/Translations.pot | 3 +++ project.godot | 4 ++++ src/Autoload/DrawingAlgos.gd | 24 ++++++++++++++++++--- src/Autoload/Global.gd | 2 ++ src/Classes/Project.gd | 6 ++++-- src/UI/TopMenuContainer/TopMenuContainer.gd | 19 ++++------------ src/UI/UI.tscn | 4 +++- 7 files changed, 41 insertions(+), 21 deletions(-) diff --git a/Translations/Translations.pot b/Translations/Translations.pot index 7d3c1befc..f95afab03 100644 --- a/Translations/Translations.pot +++ b/Translations/Translations.pot @@ -159,6 +159,9 @@ msgstr "" msgid "Crop Image" msgstr "" +msgid "Trim Image" +msgstr "" + msgid "Resize Canvas" msgstr "" diff --git a/project.godot b/project.godot index 9fbaf1b27..c6af7c8c1 100644 --- a/project.godot +++ b/project.godot @@ -643,6 +643,10 @@ crop_image={ "deadzone": 0.5, "events": [] } +trim_image={ +"deadzone": 0.5, +"events": [] +} mirror_image={ "deadzone": 0.5, "events": [] diff --git a/src/Autoload/DrawingAlgos.gd b/src/Autoload/DrawingAlgos.gd index 73276fb12..6e7660456 100644 --- a/src/Autoload/DrawingAlgos.gd +++ b/src/Autoload/DrawingAlgos.gd @@ -456,8 +456,6 @@ func color_distance(c1: Color, c2: Color) -> float: # Image effects - - func scale_image(width: int, height: int, interpolation: int) -> void: general_do_scale(width, height) @@ -519,7 +517,27 @@ func center(indices: Array) -> void: project.undo_redo.commit_action() +## Sets the size of the project to be the same as the size of the active selection. func crop_image() -> void: + if not Global.current_project.has_selection: + return + Global.canvas.selection.transform_content_confirm() + var rect: Rect2i = Global.canvas.selection.big_bounding_rectangle + general_do_scale(rect.size.x, rect.size.y) + # Loop through all the cels to crop them + for f in Global.current_project.frames: + for cel in f.cels: + if not cel is PixelCel: + continue + var sprite := cel.get_image().get_region(rect) + Global.undo_redo_compress_images({cel.image: sprite.data}, {cel.image: cel.image.data}) + + general_undo_scale() + + +## Automatically makes the project smaller by looping through all of the cels and +## trimming out the pixels that are transparent in all cels. +func trim_image() -> void: Global.canvas.selection.transform_content_confirm() var used_rect := Rect2i() for f in Global.current_project.frames: @@ -542,7 +560,7 @@ func crop_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 + # Loop through all the cels to trim them for f in Global.current_project.frames: for cel in f.cels: if not cel is PixelCel: diff --git a/src/Autoload/Global.gd b/src/Autoload/Global.gd index b0d0d0922..eaac6d2f0 100644 --- a/src/Autoload/Global.gd +++ b/src/Autoload/Global.gd @@ -45,6 +45,7 @@ enum ImageMenu { OFFSET_IMAGE, SCALE_IMAGE, CROP_IMAGE, + TRIM_IMAGE, FLIP, ROTATE, OUTLINE, @@ -564,6 +565,7 @@ func _initialize_keychain() -> void: "preferences": Keychain.InputAction.new("", "Edit menu", true), "scale_image": Keychain.InputAction.new("", "Image menu", true), "crop_image": Keychain.InputAction.new("", "Image menu", true), + "trim_image": Keychain.InputAction.new("", "Image menu", true), "resize_canvas": Keychain.InputAction.new("", "Image menu", true), "offset_image": Keychain.InputAction.new("", "Image menu", true), "mirror_image": Keychain.InputAction.new("", "Image menu", true), diff --git a/src/Classes/Project.gd b/src/Classes/Project.gd index 848d8998d..f04642aa3 100644 --- a/src/Classes/Project.gd +++ b/src/Classes/Project.gd @@ -175,8 +175,10 @@ func selection_map_changed() -> void: if has_selection: image_texture = ImageTexture.create_from_image(selection_map) Global.canvas.selection.marching_ants_outline.texture = image_texture - var edit_menu_popup: PopupMenu = Global.top_menu_container.edit_menu - edit_menu_popup.set_item_disabled(Global.EditMenu.NEW_BRUSH, !has_selection) + Global.top_menu_container.edit_menu.set_item_disabled(Global.EditMenu.NEW_BRUSH, !has_selection) + Global.top_menu_container.image_menu.set_item_disabled( + Global.ImageMenu.CROP_IMAGE, !has_selection + ) func change_project() -> void: diff --git a/src/UI/TopMenuContainer/TopMenuContainer.gd b/src/UI/TopMenuContainer/TopMenuContainer.gd index eae923bc1..efb020d34 100644 --- a/src/UI/TopMenuContainer/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer/TopMenuContainer.gd @@ -279,6 +279,7 @@ func _setup_image_menu() -> void: "Offset Image": "offset_image", "Scale Image": "scale_image", "Crop Image": "crop_image", + "Trim Image": "trim_image", "Mirror Image": "mirror_image", "Rotate Image": "rotate_image", "Outline": "outline", @@ -295,7 +296,7 @@ func _setup_image_menu() -> void: for item in image_menu_items: _set_menu_shortcut(image_menu_items[item], image_menu, i, item) i += 1 - + image_menu.set_item_disabled(Global.ImageMenu.CROP_IMAGE, true) image_menu.id_pressed.connect(image_menu_id_pressed) @@ -679,46 +680,34 @@ func image_menu_id_pressed(id: int) -> void: match id: Global.ImageMenu.SCALE_IMAGE: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/ScaleImage")) - Global.ImageMenu.OFFSET_IMAGE: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/OffsetImage")) - Global.ImageMenu.CROP_IMAGE: DrawingAlgos.crop_image() - + Global.ImageMenu.TRIM_IMAGE: + DrawingAlgos.trim_image() Global.ImageMenu.RESIZE_CANVAS: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/ResizeCanvas")) - Global.ImageMenu.FLIP: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/FlipImageDialog")) - Global.ImageMenu.ROTATE: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/RotateImage")) - Global.ImageMenu.INVERT_COLORS: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/InvertColorsDialog")) - Global.ImageMenu.DESATURATION: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/DesaturateDialog")) - Global.ImageMenu.OUTLINE: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/OutlineDialog")) - Global.ImageMenu.DROP_SHADOW: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/DropShadowDialog")) - Global.ImageMenu.HSV: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/HSVDialog")) - Global.ImageMenu.GRADIENT: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/GradientDialog")) - Global.ImageMenu.GRADIENT_MAP: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/GradientMapDialog")) - Global.ImageMenu.POSTERIZE: _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/Posterize")) - # Global.ImageMenu.SHADER: # _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/ShaderEffect")) diff --git a/src/UI/UI.tscn b/src/UI/UI.tscn index 2f83c65a0..1801f7eae 100644 --- a/src/UI/UI.tscn +++ b/src/UI/UI.tscn @@ -310,11 +310,12 @@ camera_path = NodePath("SubViewport/Camera2D") [node name="SubViewport" type="SubViewport" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/SubViewportContainer"] handle_input_locally = false canvas_item_default_texture_filter = 0 -size = Vector2i(867, 513) +size = Vector2i(2, 2) render_target_update_mode = 4 [node name="TransparentChecker" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/SubViewportContainer/SubViewport" instance=ExtResource("5")] material = SubResource("2") +anchors_preset = 0 [node name="Canvas" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/SubViewportContainer/SubViewport" instance=ExtResource("19")] @@ -350,6 +351,7 @@ render_target_update_mode = 0 [node name="TransparentChecker" parent="DockableContainer/Second Canvas/SubViewport" instance=ExtResource("5")] material = SubResource("3") +anchors_preset = 0 [node name="CanvasPreview" parent="DockableContainer/Second Canvas/SubViewport" instance=ExtResource("2")]