From 613dd5d7f630735ed9b658cc44665024c3d0f20d Mon Sep 17 00:00:00 2001 From: Vaibhav Kubre Date: Thu, 29 Aug 2024 21:37:48 +0530 Subject: [PATCH] feat: Added option to trim sprites empty area while exporting (#1088) * feat: Added trim sprite option works like charm * fix: format issue * ran working gdformat --- src/Autoload/Export.gd | 3 +++ src/UI/Dialogs/ExportDialog.gd | 6 ++++++ src/UI/Dialogs/ExportDialog.tscn | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/src/Autoload/Export.gd b/src/Autoload/Export.gd index 10a6bfadc..4521ac5c9 100644 --- a/src/Autoload/Export.gd +++ b/src/Autoload/Export.gd @@ -48,6 +48,7 @@ var current_tab := ExportTab.IMAGE var processed_images: Array[ProcessedImage] = [] var export_json := false var split_layers := false +var trim_sprite := false # Spritesheet options var orientation := Orientation.COLUMNS @@ -270,6 +271,8 @@ func process_animation(project := Global.current_project) -> void: else: var image := Image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8) _blend_layers(image, frame) + if trim_sprite: + image = image.get_region(image.get_used_rect()) var duration := frame.duration * (1.0 / project.fps) processed_images.append(ProcessedImage.new(image, project.frames.find(frame), duration)) diff --git a/src/UI/Dialogs/ExportDialog.gd b/src/UI/Dialogs/ExportDialog.gd index 0e0ad5047..07f45b42f 100644 --- a/src/UI/Dialogs/ExportDialog.gd +++ b/src/UI/Dialogs/ExportDialog.gd @@ -448,6 +448,12 @@ func _on_MultipleAnimationsDirectories_toggled(button_pressed: bool) -> void: Export.new_dir_for_each_frame_tag = button_pressed +func _on_TrimSprite_toggled(toggled_on: bool) -> void: + Export.trim_sprite = toggled_on + Export.process_data() + set_preview() + + func _on_Frames_item_selected(id: int) -> void: Export.frame_current_tag = id Export.process_data() diff --git a/src/UI/Dialogs/ExportDialog.tscn b/src/UI/Dialogs/ExportDialog.tscn index 16458fcfe..909b02812 100644 --- a/src/UI/Dialogs/ExportDialog.tscn +++ b/src/UI/Dialogs/ExportDialog.tscn @@ -301,6 +301,12 @@ tooltip_text = "Creates multiple files but every file is stored in different fol mouse_default_cursor_shape = 2 text = "Create new folder for each frame tag" +[node name="TrimSprite" type="CheckBox" parent="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer" groups=["ExportImageOptions", "ExportMultipleFilesOptions"]] +layout_mode = 2 +tooltip_text = "Trims sprite to visible portion of the spirte, considering each pixel with a non-zero alpha channel as visible." +mouse_default_cursor_shape = 2 +text = "Trim Sprite" + [node name="PathDialog" type="FileDialog" parent="." groups=["FileDialogs"]] mode = 2 title = "Open a Directory" @@ -354,6 +360,7 @@ size_flags_horizontal = 3 [connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/SplitLayers" to="." method="_on_split_layers_toggled"] [connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/IncludeTagsInFilename" to="." method="_on_IncludeTagsInFilename_toggled"] [connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/MultipleAnimationsDirectories" to="." method="_on_MultipleAnimationsDirectories_toggled"] +[connection signal="toggled" from="VBoxContainer/VSplitContainer/VBoxContainer/AdvancedOptions/GridContainer/TrimSprite" to="." method="_on_TrimSprite_toggled"] [connection signal="canceled" from="PathDialog" to="." method="_on_path_dialog_canceled"] [connection signal="dir_selected" from="PathDialog" to="." method="_on_path_dialog_dir_selected"] [connection signal="confirmed" from="FileExistsAlert" to="." method="_on_FileExistsAlert_confirmed"]