Skip to content

Commit

Permalink
Spritesheet layer improvements (#623)
Browse files Browse the repository at this point in the history
* Drastically changed an old func()

"open_image_as_spritesheet_layer" (Hope i didn't break anything...)

* removed whitespace

* some minor changes

spritesheet "Start frame" no longer restricted to existing frames number
and a minor UI change

* solved some format errors
  • Loading branch information
Variable-ind authored Dec 28, 2021
1 parent 311f4dd commit bbc56e2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
64 changes: 47 additions & 17 deletions src/Autoload/OpenSave.gd
Original file line number Diff line number Diff line change
Expand Up @@ -414,33 +414,63 @@ func open_image_as_spritesheet_layer(
var frame_height := image.get_size().y / vertical

# resize canvas to if "frame_width" or "frame_height" is too large
var project_width: int = max(frame_width, Global.current_project.size.x)
var project_height: int = max(frame_height, Global.current_project.size.y)
DrawingAlgos.resize_canvas(project_width, project_height, 0, 0)
var project: Project = Global.current_project
var project_width: int = max(frame_width, project.size.x)
var project_height: int = max(frame_height, project.size.y)
if project.size < Vector2(project_width, project_height):
DrawingAlgos.resize_canvas(project_width, project_height, 0, 0)

# slice images
#initialize undo mechanism
project.undos += 1
project.undo_redo.create_action("Add Spritesheet Layer")
var new_layers: Array = project.layers.duplicate()
var new_frames: Array = project.frames.duplicate()

#Create new frames (if needed)
var new_frames_size = start_frame + (vertical * horizontal)
if new_frames_size > project.frames.size():
var required_frames = new_frames_size - project.frames.size()
for i in required_frames:
var frame: Frame = project.new_empty_frame()
new_frames.insert(project.current_frame + 1, frame)
#Create new layer for spritesheet
var layer := Layer.new(file_name)
new_layers.append(layer)
for f in new_frames:
var new_layer := Image.new()
new_layer.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
f.cels.append(Cel.new(new_layer, 1))

# slice spritesheet
var image_no: int = 0
for yy in range(vertical):
for xx in range(horizontal):
var cropped_image := Image.new()
cropped_image = image.get_rect(
Rect2(frame_width * xx, frame_height * yy, frame_width, frame_height)
)
if (start_frame + (image_no)) < Global.current_project.frames.size():
# if frames are already present then fill those first
if image_no == 0:
open_image_as_new_layer(cropped_image, file_name, start_frame + image_no)
else:
open_image_at_frame(
cropped_image,
Global.current_project.layers.size() - 1,
start_frame + image_no
)
else:
# if no more frames are present then start making new frames
open_image_as_new_frame(cropped_image, Global.current_project.layers.size() - 1)
cropped_image.crop(project.size.x, project.size.y)
var layer_index = new_layers.size() - 1
var frame_index = start_frame + image_no

for i in new_frames.size():
if i == frame_index:
cropped_image.convert(Image.FORMAT_RGBA8)
new_frames[i].cels[layer_index] = (Cel.new(cropped_image, 1))
image_no += 1

project.undo_redo.add_do_property(project, "current_frame", new_frames.size() - 1)
project.undo_redo.add_do_property(project, "current_layer", project.layers.size())
project.undo_redo.add_do_property(project, "layers", new_layers)
project.undo_redo.add_do_property(project, "frames", new_frames)
project.undo_redo.add_undo_property(project, "current_layer", project.current_layer)
project.undo_redo.add_undo_property(project, "current_frame", project.current_frame)
project.undo_redo.add_undo_property(project, "layers", project.layers)
project.undo_redo.add_undo_property(project, "frames", project.frames)
project.undo_redo.add_do_method(Global, "undo_or_redo", false)
project.undo_redo.add_undo_method(Global, "undo_or_redo", true)
project.undo_redo.commit_action()


func open_image_at_frame(image: Image, layer_index := 0, frame_index := 0) -> void:
var project = Global.current_project
Expand Down
2 changes: 0 additions & 2 deletions src/UI/Dialogs/PreviewDialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ func _on_ImportOption_item_selected(id: int) -> void:
frame_size_label.visible = true
spritesheet_tab_options.visible = true
spritesheet_layer_options.visible = true
var n_of_frames: int = Global.current_project.frames.size()
spritesheet_layer_options.get_node("AtFrameSpinbox").max_value = n_of_frames
texture_rect.get_child(0).visible = true
texture_rect.get_child(1).visible = true
rect_size.x = spritesheet_layer_options.rect_size.x
Expand Down
5 changes: 5 additions & 0 deletions src/UI/Dialogs/PreviewDialog.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ __meta__ = {
[node name="CenterContainer" type="CenterContainer" parent="VBoxContainer"]
margin_right = 534.0
margin_bottom = 300.0
size_flags_vertical = 3
__meta__ = {
"_edit_use_anchors_": false
}
Expand All @@ -44,6 +45,9 @@ __meta__ = {
}

[node name="HorizLines" type="Control" parent="VBoxContainer/CenterContainer/TextureRect"]
__meta__ = {
"_edit_use_anchors_": false
}

[node name="VerticalLines" type="Control" parent="VBoxContainer/CenterContainer/TextureRect"]

Expand Down Expand Up @@ -249,6 +253,7 @@ text = "Brush name:"
margin_left = 85.0
margin_right = 143.0
margin_bottom = 24.0

[connection signal="about_to_show" from="." to="." method="_on_PreviewDialog_about_to_show"]
[connection signal="confirmed" from="." to="." method="_on_PreviewDialog_confirmed"]
[connection signal="popup_hide" from="." to="." method="_on_PreviewDialog_popup_hide"]
Expand Down

0 comments on commit bbc56e2

Please sign in to comment.