mirror of
https://github.com/tonytins/CozyPixelStudio.git
synced 2025-06-25 10:24:44 -04:00
Changed Export PNG settings, moved code from Main.gd to ExportSprites.gd
And made ExportSprites.tscn a scene of its own
This commit is contained in:
parent
04fe708560
commit
67631ac648
7 changed files with 162 additions and 144 deletions
104
Scripts/Dialogs/ExportSprites.gd
Normal file
104
Scripts/Dialogs/ExportSprites.gd
Normal file
|
@ -0,0 +1,104 @@
|
|||
extends FileDialog
|
||||
|
||||
var current_export_path := ""
|
||||
var export_option := 0
|
||||
|
||||
func _ready() -> void:
|
||||
var children := []
|
||||
for i in range(get_child_count()):
|
||||
if i > 7:
|
||||
children.append(get_child(i))
|
||||
|
||||
for child in children:
|
||||
remove_child(child)
|
||||
get_vbox().add_child(child)
|
||||
|
||||
func _on_ExportOption_item_selected(ID : int) -> void:
|
||||
export_option = ID
|
||||
|
||||
func _on_ExportSprites_file_selected(path : String) -> void:
|
||||
current_export_path = path
|
||||
Global.file_menu.get_popup().set_item_text(5, tr("Export") + " %s" % path.get_file())
|
||||
export_project()
|
||||
|
||||
func export_project() -> void:
|
||||
if export_option == 0: # Export current frame
|
||||
save_sprite(Global.canvas, current_export_path)
|
||||
elif export_option == 1: # Export all frames as multiple files
|
||||
var i := 1
|
||||
for canvas in Global.canvases:
|
||||
var path := "%s_%s" % [current_export_path, str(i)]
|
||||
path = path.replace(".png", "")
|
||||
path = "%s.png" % path
|
||||
save_sprite(canvas, path)
|
||||
i += 1
|
||||
else: # Export all frames as a spritesheet (single file)
|
||||
save_spritesheet(export_option == 2)
|
||||
|
||||
Global.notification_label("File exported")
|
||||
|
||||
func save_sprite(canvas : Canvas, path : String) -> void:
|
||||
var whole_image := Image.new()
|
||||
whole_image.create(canvas.size.x, canvas.size.y, false, Image.FORMAT_RGBA8)
|
||||
whole_image.lock()
|
||||
for layer in canvas.layers:
|
||||
var img : Image = layer[0]
|
||||
img.lock()
|
||||
if layer[4] < 1: # If we have layer transparency
|
||||
for xx in img.get_size().x:
|
||||
for yy in img.get_size().y:
|
||||
var pixel_color := img.get_pixel(xx, yy)
|
||||
var alpha : float = pixel_color.a * layer[4]
|
||||
img.set_pixel(xx, yy, Color(pixel_color.r, pixel_color.g, pixel_color.b, alpha))
|
||||
|
||||
canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), Vector2.ZERO)
|
||||
layer[0].lock()
|
||||
var err = whole_image.save_png(path)
|
||||
if err != OK:
|
||||
OS.alert("Can't save file")
|
||||
|
||||
func save_spritesheet(horizontal : bool) -> void:
|
||||
var width
|
||||
var height
|
||||
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()
|
||||
whole_image.create(width, height, false, Image.FORMAT_RGBA8)
|
||||
whole_image.lock()
|
||||
var dst := Vector2.ZERO
|
||||
for canvas in Global.canvases:
|
||||
for layer in canvas.layers:
|
||||
var img : Image = layer[0]
|
||||
img.lock()
|
||||
if layer[4] < 1: # If we have layer transparency
|
||||
for xx in img.get_size().x:
|
||||
for yy in img.get_size().y:
|
||||
var pixel_color := img.get_pixel(xx, yy)
|
||||
var alpha : float = pixel_color.a * layer[4]
|
||||
img.set_pixel(xx, yy, Color(pixel_color.r, pixel_color.g, pixel_color.b, alpha))
|
||||
|
||||
canvas.blend_rect(whole_image, img, Rect2(canvas.position, canvas.size), dst)
|
||||
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)
|
||||
if err != OK:
|
||||
OS.alert("Can't save file")
|
||||
|
|
@ -5,7 +5,7 @@ var import_spritesheet := false
|
|||
var spritesheet_horizontal := 1
|
||||
var spritesheet_vertical := 1
|
||||
|
||||
func _ready():
|
||||
func _ready() -> void:
|
||||
var children := []
|
||||
for i in range(get_child_count()):
|
||||
if i > 7:
|
||||
|
@ -44,7 +44,7 @@ func _on_ImportSprites_files_selected(paths : PoolStringArray) -> void:
|
|||
for path in paths:
|
||||
var image := Image.new()
|
||||
var err := image.load(path)
|
||||
if err != OK: #An error occured
|
||||
if err != OK: # An error occured
|
||||
OS.alert("Can't load file")
|
||||
continue
|
||||
|
||||
|
@ -74,7 +74,7 @@ func _on_ImportSprites_files_selected(paths : PoolStringArray) -> void:
|
|||
else:
|
||||
var image := Image.new()
|
||||
var err := image.load(first_path)
|
||||
if err != OK: #An error occured
|
||||
if err != OK: # An error occured
|
||||
OS.alert("Can't load file")
|
||||
return
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue