mirror of
https://github.com/tonytins/CozyPixelStudio.git
synced 2025-06-25 10:24:44 -04:00
Choose how many rows or columns when exporting spritesheet
Also made CreateNewImage.tscn, ScaleImage.tscn and OutlineDialogn.tscn scenes of their own, with their respective scripts.
This commit is contained in:
parent
1a6f6c1cc9
commit
3ae679fe00
10 changed files with 396 additions and 340 deletions
18
Scripts/Dialogs/CreateNewImage.gd
Normal file
18
Scripts/Dialogs/CreateNewImage.gd
Normal file
|
@ -0,0 +1,18 @@
|
|||
extends ConfirmationDialog
|
||||
|
||||
func _on_CreateNewImage_confirmed() -> void:
|
||||
var width : int = $VBoxContainer/OptionsContainer/WidthValue.value
|
||||
var height : int = $VBoxContainer/OptionsContainer/HeightValue.value
|
||||
var fill_color : Color = $VBoxContainer/OptionsContainer/FillColor.color
|
||||
Global.control.clear_canvases()
|
||||
Global.canvas = load("res://Prefabs/Canvas.tscn").instance()
|
||||
Global.canvas.size = Vector2(width, height).floor()
|
||||
|
||||
Global.canvases.append(Global.canvas)
|
||||
Global.canvas_parent.add_child(Global.canvas)
|
||||
Global.current_frame = 0
|
||||
if fill_color.a > 0:
|
||||
Global.canvas.layers[0][0].fill(fill_color)
|
||||
Global.canvas.layers[0][0].lock()
|
||||
Global.canvas.update_texture(0)
|
||||
Global.undo_redo.clear_history(false)
|
|
@ -4,6 +4,7 @@ var current_export_path := ""
|
|||
var export_option := 0
|
||||
var resize := 100
|
||||
var interpolation = Image.INTERPOLATE_NEAREST
|
||||
var per_rows := false
|
||||
var spritesheet_rows = 1
|
||||
var spritesheet_columns = 1
|
||||
|
||||
|
@ -31,12 +32,19 @@ func _on_ResizeValue_value_changed(value) -> void:
|
|||
func _on_Interpolation_item_selected(ID : int) -> void:
|
||||
interpolation = ID
|
||||
|
||||
func _on_VerticalFrames_value_changed(value) -> void:
|
||||
value = min(value, Global.canvases.size())
|
||||
spritesheet_columns = value
|
||||
func _on_ColumnsOrRows_item_selected(ID) -> void:
|
||||
per_rows = bool(ID)
|
||||
|
||||
var vertical_frames : SpinBox = Global.find_node_by_name(self, "VerticalFrames")
|
||||
vertical_frames.value = spritesheet_columns
|
||||
func _on_Frames_value_changed(value):
|
||||
value = min(value, Global.canvases.size())
|
||||
var frames_spinbox : SpinBox = Global.find_node_by_name(self, "Frames")
|
||||
|
||||
if per_rows:
|
||||
spritesheet_rows = value
|
||||
frames_spinbox.value = spritesheet_rows
|
||||
else:
|
||||
spritesheet_columns = value
|
||||
frames_spinbox.value = spritesheet_columns
|
||||
|
||||
func _on_ExportSprites_file_selected(path : String) -> void:
|
||||
current_export_path = path
|
||||
|
@ -83,7 +91,10 @@ func save_sprite(canvas : Canvas, path : String) -> void:
|
|||
OS.alert("Can't save file")
|
||||
|
||||
func save_spritesheet() -> void:
|
||||
spritesheet_rows = ceil(Global.canvases.size() / spritesheet_columns)
|
||||
if per_rows:
|
||||
spritesheet_columns = ceil(Global.canvases.size() / spritesheet_rows)
|
||||
else:
|
||||
spritesheet_rows = ceil(Global.canvases.size() / spritesheet_columns)
|
||||
var width = Global.canvas.size.x * spritesheet_rows
|
||||
var height = Global.canvas.size.y * spritesheet_columns
|
||||
|
||||
|
@ -94,14 +105,25 @@ func save_spritesheet() -> void:
|
|||
var hh := 0
|
||||
var vv := 0
|
||||
for canvas in Global.canvases:
|
||||
if hh < spritesheet_rows:
|
||||
dst.x = canvas.size.x * hh
|
||||
hh += 1
|
||||
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
|
||||
|
||||
else:
|
||||
vv += 1
|
||||
dst.x = 0
|
||||
hh = 1
|
||||
dst.y = canvas.size.y * vv
|
||||
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:
|
||||
var img : Image = layer[0]
|
||||
|
@ -121,3 +143,4 @@ func save_spritesheet() -> void:
|
|||
var err = whole_image.save_png(current_export_path)
|
||||
if err != OK:
|
||||
OS.alert("Can't save file")
|
||||
|
||||
|
|
74
Scripts/Dialogs/OutlineDialog.gd
Normal file
74
Scripts/Dialogs/OutlineDialog.gd
Normal file
|
@ -0,0 +1,74 @@
|
|||
extends ConfirmationDialog
|
||||
|
||||
func _on_OutlineDialog_confirmed() -> void:
|
||||
var outline_color : Color = $OptionsContainer/OutlineColor.color
|
||||
var thickness : int = $OptionsContainer/ThickValue.value
|
||||
var diagonal : bool = $OptionsContainer/DiagonalCheckBox.pressed
|
||||
|
||||
var image : Image = Global.canvas.layers[Global.canvas.current_layer_index][0]
|
||||
if image.is_invisible():
|
||||
return
|
||||
var new_image := Image.new()
|
||||
new_image.copy_from(image)
|
||||
new_image.lock()
|
||||
|
||||
Global.canvas.handle_undo("Draw")
|
||||
for xx in image.get_size().x:
|
||||
for yy in image.get_size().y:
|
||||
var pos = Vector2(xx, yy)
|
||||
var current_pixel := image.get_pixelv(pos)
|
||||
if current_pixel.a == 0:
|
||||
continue
|
||||
|
||||
for i in range(1, thickness + 1):
|
||||
var new_pos : Vector2 = pos + Vector2.LEFT * i # Left
|
||||
if new_pos.x >= 0:
|
||||
var new_pixel = image.get_pixelv(new_pos)
|
||||
if new_pixel.a == 0:
|
||||
new_image.set_pixelv(new_pos, outline_color)
|
||||
|
||||
new_pos = pos + Vector2.RIGHT * i # Right
|
||||
if new_pos.x < Global.canvas.size.x:
|
||||
var new_pixel = image.get_pixelv(new_pos)
|
||||
if new_pixel.a == 0:
|
||||
new_image.set_pixelv(new_pos, outline_color)
|
||||
|
||||
new_pos = pos + Vector2.UP * i # Up
|
||||
if new_pos.y >= 0:
|
||||
var new_pixel = image.get_pixelv(new_pos)
|
||||
if new_pixel.a == 0:
|
||||
new_image.set_pixelv(new_pos, outline_color)
|
||||
|
||||
new_pos = pos + Vector2.DOWN * i # Down
|
||||
if new_pos.y < Global.canvas.size.y:
|
||||
var new_pixel = image.get_pixelv(new_pos)
|
||||
if new_pixel.a == 0:
|
||||
new_image.set_pixelv(new_pos, outline_color)
|
||||
|
||||
if diagonal:
|
||||
new_pos = pos + (Vector2.LEFT + Vector2.UP) * i # Top left
|
||||
if new_pos.x >= 0 && new_pos.y >= 0:
|
||||
var new_pixel = image.get_pixelv(new_pos)
|
||||
if new_pixel.a == 0:
|
||||
new_image.set_pixelv(new_pos, outline_color)
|
||||
|
||||
new_pos = pos + (Vector2.LEFT + Vector2.DOWN) * i # Bottom left
|
||||
if new_pos.x >= 0 && new_pos.y < Global.canvas.size.y:
|
||||
var new_pixel = image.get_pixelv(new_pos)
|
||||
if new_pixel.a == 0:
|
||||
new_image.set_pixelv(new_pos, outline_color)
|
||||
|
||||
new_pos = pos + (Vector2.RIGHT + Vector2.UP) * i # Top right
|
||||
if new_pos.x < Global.canvas.size.x && new_pos.y >= 0:
|
||||
var new_pixel = image.get_pixelv(new_pos)
|
||||
if new_pixel.a == 0:
|
||||
new_image.set_pixelv(new_pos, outline_color)
|
||||
|
||||
new_pos = pos + (Vector2.RIGHT + Vector2.DOWN) * i # Bottom right
|
||||
if new_pos.x < Global.canvas.size.x && new_pos.y < Global.canvas.size.y:
|
||||
var new_pixel = image.get_pixelv(new_pos)
|
||||
if new_pixel.a == 0:
|
||||
new_image.set_pixelv(new_pos, outline_color)
|
||||
|
||||
image.copy_from(new_image)
|
||||
Global.canvas.handle_redo("Draw")
|
20
Scripts/Dialogs/ScaleImage.gd
Normal file
20
Scripts/Dialogs/ScaleImage.gd
Normal file
|
@ -0,0 +1,20 @@
|
|||
extends ConfirmationDialog
|
||||
|
||||
func _on_ScaleImage_confirmed() -> void:
|
||||
var width : int = $VBoxContainer/OptionsContainer/WidthValue.value
|
||||
var height : int = $VBoxContainer/OptionsContainer/HeightValue.value
|
||||
var interpolation : int = $VBoxContainer/OptionsContainer/InterpolationType.selected
|
||||
Global.undos += 1
|
||||
Global.undo_redo.create_action("Scale")
|
||||
Global.undo_redo.add_do_property(Global.canvas, "size", Vector2(width, height).floor())
|
||||
|
||||
for i in range(Global.canvas.layers.size() - 1, -1, -1):
|
||||
var sprite : Image = Global.canvas.layers[i][1].get_data()
|
||||
sprite.resize(width, height, interpolation)
|
||||
Global.undo_redo.add_do_property(Global.canvas.layers[i][0], "data", sprite.data)
|
||||
Global.undo_redo.add_undo_property(Global.canvas.layers[i][0], "data", Global.canvas.layers[i][0].data)
|
||||
|
||||
Global.undo_redo.add_undo_property(Global.canvas, "size", Global.canvas.size)
|
||||
Global.undo_redo.add_undo_method(Global, "undo", [Global.canvas])
|
||||
Global.undo_redo.add_do_method(Global, "redo", [Global.canvas])
|
||||
Global.undo_redo.commit_action()
|
Loading…
Add table
Add a link
Reference in a new issue