From 1a6f6c1cc9cdd8110958aeb423239ac21dabc9d6 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 29 Dec 2019 22:04:44 +0200 Subject: [PATCH] Resize image on exporting PNG Also fixed issue when exporting a spritesheet, spritesheet_rows wasn't being updated if the user didn't change the value of the spinbox --- Prefabs/Dialogs/ExportSprites.tscn | 43 +++++++++++++++++++++++++++--- Scripts/Dialogs/ExportSprites.gd | 17 +++++++++--- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/Prefabs/Dialogs/ExportSprites.tscn b/Prefabs/Dialogs/ExportSprites.tscn index 7968be807..e621ad6dd 100644 --- a/Prefabs/Dialogs/ExportSprites.tscn +++ b/Prefabs/Dialogs/ExportSprites.tscn @@ -27,6 +27,40 @@ text = "Export current frame" items = [ "Export current frame", null, false, 0, null, "Export all frames as multiple files", null, false, 1, null, "Export all frames as a spritesheet (single file)", null, false, 2, null ] selected = 0 +[node name="Resize" type="HBoxContainer" parent="."] +margin_right = 40.0 +margin_bottom = 40.0 + +[node name="Label" type="Label" parent="Resize"] +margin_top = 13.0 +margin_right = 46.0 +margin_bottom = 27.0 +text = "Resize:" + +[node name="ResizeValue" type="SpinBox" parent="Resize"] +margin_left = 50.0 +margin_right = 124.0 +margin_bottom = 40.0 +min_value = 10.0 +max_value = 1000.0 +step = 10.0 +value = 100.0 +suffix = "%" + +[node name="Label2" type="Label" parent="Resize"] +margin_left = 128.0 +margin_top = 13.0 +margin_right = 215.0 +margin_bottom = 27.0 +text = "Interpolation:" + +[node name="Interpolation" type="OptionButton" parent="Resize"] +margin_right = 41.0 +margin_bottom = 20.0 +text = "Item 0" +items = [ "Nearest", null, false, 0, null, "Bilinear", null, false, 1, null, "Cubic", null, false, 2, null, "Trilinear", null, false, 3, null, "Lanczos", null, true, 4, null ] +selected = 0 + [node name="Spritesheet" type="HBoxContainer" parent="."] visible = false margin_left = 8.0 @@ -35,19 +69,20 @@ margin_right = 507.0 margin_bottom = 312.0 [node name="Label" type="Label" parent="Spritesheet"] -margin_left = 200.0 margin_top = 145.0 -margin_right = 300.0 +margin_right = 60.0 margin_bottom = 159.0 text = "Columns:" [node name="VerticalFrames" type="SpinBox" parent="Spritesheet"] -margin_left = 304.0 -margin_right = 378.0 +margin_left = 64.0 +margin_right = 138.0 margin_bottom = 304.0 mouse_default_cursor_shape = 2 min_value = 1.0 value = 1.0 [connection signal="file_selected" from="." to="." method="_on_ExportSprites_file_selected"] [connection signal="item_selected" from="ExportOption" to="." method="_on_ExportOption_item_selected"] +[connection signal="value_changed" from="Resize/ResizeValue" to="." method="_on_ResizeValue_value_changed"] +[connection signal="item_selected" from="Resize/Interpolation" to="." method="_on_Interpolation_item_selected"] [connection signal="value_changed" from="Spritesheet/VerticalFrames" to="." method="_on_VerticalFrames_value_changed"] diff --git a/Scripts/Dialogs/ExportSprites.gd b/Scripts/Dialogs/ExportSprites.gd index 819efc906..136bceca8 100644 --- a/Scripts/Dialogs/ExportSprites.gd +++ b/Scripts/Dialogs/ExportSprites.gd @@ -2,6 +2,8 @@ extends FileDialog var current_export_path := "" var export_option := 0 +var resize := 100 +var interpolation = Image.INTERPOLATE_NEAREST var spritesheet_rows = 1 var spritesheet_columns = 1 @@ -23,9 +25,14 @@ func _on_ExportOption_item_selected(ID : int) -> void: else: spritesheet_container.visible = false +func _on_ResizeValue_value_changed(value) -> void: + resize = value + +func _on_Interpolation_item_selected(ID : int) -> void: + interpolation = ID + func _on_VerticalFrames_value_changed(value) -> void: value = min(value, Global.canvases.size()) - spritesheet_rows = ceil(Global.canvases.size() / value) spritesheet_columns = value var vertical_frames : SpinBox = Global.find_node_by_name(self, "VerticalFrames") @@ -68,11 +75,15 @@ func save_sprite(canvas : Canvas, path : String) -> void: canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), Vector2.ZERO) layer[0].lock() + + if resize != 100: + whole_image.resize(whole_image.get_size().x * resize / 100, whole_image.get_size().y * resize / 100, interpolation) var err = whole_image.save_png(path) if err != OK: OS.alert("Can't save file") func save_spritesheet() -> void: + spritesheet_rows = ceil(Global.canvases.size() / spritesheet_columns) var width = Global.canvas.size.x * spritesheet_rows var height = Global.canvas.size.y * spritesheet_columns @@ -105,8 +116,8 @@ func save_spritesheet() -> void: canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), dst) layer[0].lock() - + if resize != 100: + whole_image.resize(width * resize / 100, height * resize / 100, interpolation) var err = whole_image.save_png(current_export_path) if err != OK: OS.alert("Can't save file") -