2019-12-28 18:30:33 +00:00
|
|
|
extends FileDialog
|
|
|
|
|
|
|
|
var current_export_path := ""
|
|
|
|
var export_option := 0
|
2019-12-29 20:04:44 +00:00
|
|
|
var resize := 100
|
|
|
|
var interpolation = Image.INTERPOLATE_NEAREST
|
2019-12-29 22:50:58 +00:00
|
|
|
var frames_spinbox : SpinBox
|
2019-12-29 22:24:37 +00:00
|
|
|
var per_rows := false
|
2019-12-29 14:55:01 +00:00
|
|
|
var spritesheet_rows = 1
|
|
|
|
var spritesheet_columns = 1
|
2019-12-28 18:30:33 +00:00
|
|
|
|
|
|
|
func _ready() -> void:
|
2019-12-29 22:50:58 +00:00
|
|
|
frames_spinbox = Global.find_node_by_name(self, "Frames")
|
2019-12-28 18:30:33 +00:00
|
|
|
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
|
2019-12-29 14:55:01 +00:00
|
|
|
var spritesheet_container = Global.find_node_by_name(self, "Spritesheet")
|
|
|
|
if ID > 1:
|
|
|
|
spritesheet_container.visible = true
|
|
|
|
else:
|
|
|
|
spritesheet_container.visible = false
|
|
|
|
|
2019-12-29 20:04:44 +00:00
|
|
|
func _on_ResizeValue_value_changed(value) -> void:
|
|
|
|
resize = value
|
|
|
|
|
|
|
|
func _on_Interpolation_item_selected(ID : int) -> void:
|
|
|
|
interpolation = ID
|
|
|
|
|
2019-12-29 22:24:37 +00:00
|
|
|
func _on_ColumnsOrRows_item_selected(ID) -> void:
|
|
|
|
per_rows = bool(ID)
|
2019-12-29 22:50:58 +00:00
|
|
|
# Update spritesheet_rows/columns variable
|
|
|
|
_on_Frames_value_changed(frames_spinbox.value)
|
2019-12-29 22:24:37 +00:00
|
|
|
|
|
|
|
func _on_Frames_value_changed(value):
|
2019-12-29 14:55:01 +00:00
|
|
|
value = min(value, Global.canvases.size())
|
|
|
|
|
2019-12-29 22:24:37 +00:00
|
|
|
if per_rows:
|
|
|
|
spritesheet_rows = value
|
|
|
|
frames_spinbox.value = spritesheet_rows
|
|
|
|
else:
|
|
|
|
spritesheet_columns = value
|
|
|
|
frames_spinbox.value = spritesheet_columns
|
2019-12-28 18:30:33 +00:00
|
|
|
|
|
|
|
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
|
2019-12-29 14:55:01 +00:00
|
|
|
elif export_option == 2: # Export all frames as a spritesheet (single file)
|
|
|
|
save_spritesheet()
|
2019-12-28 18:30:33 +00:00
|
|
|
|
|
|
|
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()
|
2020-03-06 18:56:25 +00:00
|
|
|
if layer[2] < 1: # If we have layer transparency
|
2019-12-28 18:30:33 +00:00
|
|
|
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()
|
2019-12-29 20:04:44 +00:00
|
|
|
|
|
|
|
if resize != 100:
|
2020-02-17 12:47:15 +00:00
|
|
|
whole_image.unlock()
|
2019-12-29 20:04:44 +00:00
|
|
|
whole_image.resize(whole_image.get_size().x * resize / 100, whole_image.get_size().y * resize / 100, interpolation)
|
2019-12-28 18:30:33 +00:00
|
|
|
var err = whole_image.save_png(path)
|
|
|
|
if err != OK:
|
|
|
|
OS.alert("Can't save file")
|
|
|
|
|
2019-12-29 14:55:01 +00:00
|
|
|
func save_spritesheet() -> void:
|
2019-12-29 22:24:37 +00:00
|
|
|
if per_rows:
|
|
|
|
spritesheet_columns = ceil(Global.canvases.size() / spritesheet_rows)
|
|
|
|
else:
|
|
|
|
spritesheet_rows = ceil(Global.canvases.size() / spritesheet_columns)
|
2019-12-29 14:55:01 +00:00
|
|
|
var width = Global.canvas.size.x * spritesheet_rows
|
|
|
|
var height = Global.canvas.size.y * spritesheet_columns
|
2019-12-28 18:30:33 +00:00
|
|
|
|
|
|
|
var whole_image := Image.new()
|
|
|
|
whole_image.create(width, height, false, Image.FORMAT_RGBA8)
|
|
|
|
whole_image.lock()
|
|
|
|
var dst := Vector2.ZERO
|
2019-12-29 14:55:01 +00:00
|
|
|
var hh := 0
|
|
|
|
var vv := 0
|
2019-12-28 18:30:33 +00:00
|
|
|
for canvas in Global.canvases:
|
2019-12-29 22:24:37 +00:00
|
|
|
if per_rows:
|
|
|
|
if vv < spritesheet_columns:
|
|
|
|
dst.y = canvas.size.y * vv
|
|
|
|
vv += 1
|
|
|
|
else:
|
|
|
|
hh += 1
|
|
|
|
dst.y = 0
|
|
|
|
vv = 1
|
|
|
|
dst.x = canvas.size.x * hh
|
|
|
|
|
2019-12-29 14:55:01 +00:00
|
|
|
else:
|
2019-12-29 22:24:37 +00:00
|
|
|
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
|
2019-12-29 14:55:01 +00:00
|
|
|
|
2019-12-28 18:30:33 +00:00
|
|
|
for layer in canvas.layers:
|
|
|
|
var img : Image = layer[0]
|
|
|
|
img.lock()
|
2020-03-06 18:56:25 +00:00
|
|
|
if layer[2] < 1: # If we have layer transparency
|
2019-12-28 18:30:33 +00:00
|
|
|
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()
|
|
|
|
|
2019-12-29 20:04:44 +00:00
|
|
|
if resize != 100:
|
2020-02-17 12:50:46 +00:00
|
|
|
whole_image.unlock()
|
2019-12-29 20:04:44 +00:00
|
|
|
whole_image.resize(width * resize / 100, height * resize / 100, interpolation)
|
2019-12-28 18:30:33 +00:00
|
|
|
var err = whole_image.save_png(current_export_path)
|
|
|
|
if err != OK:
|
|
|
|
OS.alert("Can't save file")
|
2019-12-29 22:24:37 +00:00
|
|
|
|