1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 17:19:50 +00:00

Better spritesheet exporting

You can now choose how many columns you want your spritesheet to be, allowing you to export atlases.

ISSUE: If the number of columns is larger than the number of rows AND rows * columns are not equal to the number of frames, the result is weird. The exported image has transparent space on its bottom side.
This commit is contained in:
OverloadedOrama 2019-12-29 16:55:01 +02:00
parent 4ae5968887
commit eeaa691d36
8 changed files with 68 additions and 39 deletions

View file

@ -1541,6 +1541,7 @@ margin_bottom = 17.0
rect_min_size = Vector2( 106, 0 ) rect_min_size = Vector2( 106, 0 )
size_flags_vertical = 1 size_flags_vertical = 1
value = 100.0 value = 100.0
ticks_on_borders = false
[node name="OpacitySpinBox" type="SpinBox" parent="MenuAndUI/UI/LayerPanel/LayersAndMisc/LayerVBoxContainer/OpacityCenter/OpacityContainer"] [node name="OpacitySpinBox" type="SpinBox" parent="MenuAndUI/UI/LayerPanel/LayersAndMisc/LayerVBoxContainer/OpacityCenter/OpacityContainer"]
margin_left = 158.0 margin_left = 158.0
@ -1736,6 +1737,7 @@ current_path = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρι
[node name="ImportSprites" parent="." instance=ExtResource( 61 )] [node name="ImportSprites" parent="." instance=ExtResource( 61 )]
[node name="ExportSprites" parent="." instance=ExtResource( 62 )] [node name="ExportSprites" parent="." instance=ExtResource( 62 )]
visible = false
[node name="ScaleImage" type="ConfirmationDialog" parent="."] [node name="ScaleImage" type="ConfirmationDialog" parent="."]
editor/display_folded = true editor/display_folded = true

View file

@ -19,10 +19,35 @@ current_path = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρι
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="ExportOption" type="OptionButton" parent="."] [node name="ExportOption" type="OptionButton" parent="."]
margin_right = 41.0 margin_left = 8.0
margin_bottom = 20.0 margin_top = 8.0
margin_right = 507.0
margin_bottom = 312.0
text = "Export current frame" 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 horizontal spritesheet (single file)", null, false, 2, null, "Export all frames as a vertical spritesheet (single file)", null, false, 3, null ] 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 selected = 0
[node name="Spritesheet" type="HBoxContainer" parent="."]
visible = false
margin_left = 8.0
margin_top = 8.0
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_bottom = 159.0
text = "Columns:"
[node name="VerticalFrames" type="SpinBox" parent="Spritesheet"]
margin_left = 304.0
margin_right = 378.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="file_selected" from="." to="." method="_on_ExportSprites_file_selected"]
[connection signal="item_selected" from="ExportOption" to="." method="_on_ExportOption_item_selected"] [connection signal="item_selected" from="ExportOption" to="." method="_on_ExportOption_item_selected"]
[connection signal="value_changed" from="Spritesheet/VerticalFrames" to="." method="_on_VerticalFrames_value_changed"]

View file

@ -21,14 +21,14 @@ margin_right = 507.0
margin_bottom = 312.0 margin_bottom = 312.0
[node name="ImportAsNewFrame" type="CheckBox" parent="HBoxContainer2"] [node name="ImportAsNewFrame" type="CheckBox" parent="HBoxContainer2"]
margin_right = 130.0 margin_right = 156.0
margin_bottom = 40.0 margin_bottom = 304.0
text = "IMPORT_FILE_LABEL" text = "IMPORT_FILE_LABEL"
[node name="ImportSpritesheet" type="CheckBox" parent="HBoxContainer2"] [node name="ImportSpritesheet" type="CheckBox" parent="HBoxContainer2"]
margin_left = 134.0 margin_left = 160.0
margin_right = 268.0 margin_right = 327.0
margin_bottom = 40.0 margin_bottom = 304.0
text = "Import as spritesheet" text = "Import as spritesheet"
[node name="Spritesheet" type="HBoxContainer" parent="."] [node name="Spritesheet" type="HBoxContainer" parent="."]
@ -48,6 +48,7 @@ text = "Horizontal frames:"
margin_left = 105.0 margin_left = 105.0
margin_right = 159.0 margin_right = 159.0
margin_bottom = 17.0 margin_bottom = 17.0
mouse_default_cursor_shape = 2
min_value = 1.0 min_value = 1.0
value = 1.0 value = 1.0
@ -62,6 +63,7 @@ text = "Vertical frames:"
margin_left = 252.0 margin_left = 252.0
margin_right = 306.0 margin_right = 306.0
margin_bottom = 17.0 margin_bottom = 17.0
mouse_default_cursor_shape = 2
min_value = 1.0 min_value = 1.0
value = 1.0 value = 1.0
[connection signal="files_selected" from="." to="." method="_on_ImportSprites_files_selected"] [connection signal="files_selected" from="." to="." method="_on_ImportSprites_files_selected"]

View file

@ -2,6 +2,8 @@ extends FileDialog
var current_export_path := "" var current_export_path := ""
var export_option := 0 var export_option := 0
var spritesheet_rows = 1
var spritesheet_columns = 1
func _ready() -> void: func _ready() -> void:
var children := [] var children := []
@ -15,6 +17,19 @@ func _ready() -> void:
func _on_ExportOption_item_selected(ID : int) -> void: func _on_ExportOption_item_selected(ID : int) -> void:
export_option = ID export_option = ID
var spritesheet_container = Global.find_node_by_name(self, "Spritesheet")
if ID > 1:
spritesheet_container.visible = true
else:
spritesheet_container.visible = false
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")
vertical_frames.value = spritesheet_columns
func _on_ExportSprites_file_selected(path : String) -> void: func _on_ExportSprites_file_selected(path : String) -> void:
current_export_path = path current_export_path = path
@ -32,8 +47,8 @@ func export_project() -> void:
path = "%s.png" % path path = "%s.png" % path
save_sprite(canvas, path) save_sprite(canvas, path)
i += 1 i += 1
else: # Export all frames as a spritesheet (single file) elif export_option == 2: # Export all frames as a spritesheet (single file)
save_spritesheet(export_option == 2) save_spritesheet()
Global.notification_label("File exported") Global.notification_label("File exported")
@ -57,29 +72,26 @@ func save_sprite(canvas : Canvas, path : String) -> void:
if err != OK: if err != OK:
OS.alert("Can't save file") OS.alert("Can't save file")
func save_spritesheet(horizontal : bool) -> void: func save_spritesheet() -> void:
var width var width = Global.canvas.size.x * spritesheet_rows
var height var height = Global.canvas.size.y * spritesheet_columns
if horizontal: # Horizontal spritesheet
width = 0
height = Global.canvas.size.y
for canvas in Global.canvases:
width += canvas.size.x
if canvas.size.y > height:
height = canvas.size.y
else: # Vertical spritesheet
width = Global.canvas.size.x
height = 0
for canvas in Global.canvases:
height += canvas.size.y
if canvas.size.x > width:
width = canvas.size.x
var whole_image := Image.new() var whole_image := Image.new()
whole_image.create(width, height, false, Image.FORMAT_RGBA8) whole_image.create(width, height, false, Image.FORMAT_RGBA8)
whole_image.lock() whole_image.lock()
var dst := Vector2.ZERO var dst := Vector2.ZERO
var hh := 0
var vv := 0
for canvas in Global.canvases: for canvas in Global.canvases:
if hh < spritesheet_rows:
dst.x = canvas.size.x * hh
hh += 1
else:
vv += 1
dst.x = 0
hh = 1
dst.y = canvas.size.y * vv
for layer in canvas.layers: for layer in canvas.layers:
var img : Image = layer[0] var img : Image = layer[0]
img.lock() img.lock()
@ -93,10 +105,6 @@ func save_spritesheet(horizontal : bool) -> void:
canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), dst) canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), dst)
layer[0].lock() layer[0].lock()
if horizontal:
dst += Vector2(canvas.size.x, 0)
else:
dst += Vector2(0, canvas.size.y)
var err = whole_image.save_png(current_export_path) var err = whole_image.save_png(current_export_path)
if err != OK: if err != OK:

View file

@ -2,8 +2,6 @@
[ext_resource path="res://Assets/Fonts/Roboto-Regular.tres" type="DynamicFont" id=1] [ext_resource path="res://Assets/Fonts/Roboto-Regular.tres" type="DynamicFont" id=1]
[sub_resource type="StyleBoxFlat" id=1] [sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.517647, 0.517647, 0.517647, 1 ) bg_color = Color( 0.517647, 0.517647, 0.517647, 1 )
border_width_left = 4 border_width_left = 4

View file

@ -2,8 +2,6 @@
[ext_resource path="res://Assets/Fonts/Roboto-Regular.tres" type="DynamicFont" id=1] [ext_resource path="res://Assets/Fonts/Roboto-Regular.tres" type="DynamicFont" id=1]
[sub_resource type="StyleBoxFlat" id=1] [sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.219608, 0.211765, 0.168627, 1 ) bg_color = Color( 0.219608, 0.211765, 0.168627, 1 )

View file

@ -2,8 +2,6 @@
[ext_resource path="res://Assets/Fonts/Roboto-Regular.tres" type="DynamicFont" id=1] [ext_resource path="res://Assets/Fonts/Roboto-Regular.tres" type="DynamicFont" id=1]
[sub_resource type="StyleBoxFlat" id=1] [sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.517647, 0.517647, 0.517647, 1 ) bg_color = Color( 0.517647, 0.517647, 0.517647, 1 )
border_width_left = 4 border_width_left = 4

View file

@ -2,8 +2,6 @@
[ext_resource path="res://Assets/Fonts/Roboto-Regular.tres" type="DynamicFont" id=1] [ext_resource path="res://Assets/Fonts/Roboto-Regular.tres" type="DynamicFont" id=1]
[sub_resource type="StyleBoxFlat" id=1] [sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.803922, 0.803922, 0.803922, 1 ) bg_color = Color( 0.803922, 0.803922, 0.803922, 1 )
border_width_left = 4 border_width_left = 4