Added AnimationTag class

Replaces nested Global.animation_tags arrays. Also replaced array.duplicate(true) with looping through the array and creating a new class for each array element, because duplicate(true) does not create new classes, unfortunately, which was causing issues with undo/redo.
This commit is contained in:
OverloadedOrama 2020-06-02 05:14:05 +03:00
parent e229ad1519
commit 34bc528e97
14 changed files with 134 additions and 83 deletions

View file

@ -67,22 +67,33 @@ func _on_DeleteFrame_pressed(frame := -1) -> void:
if current_frame > 0 && current_frame == new_canvases.size(): # If it's the last frame
current_frame -= 1
var new_animation_tags := Global.animation_tags.duplicate(true)
var new_animation_tags := Global.animation_tags.duplicate()
# Loop through the tags to create new classes for them, so that they won't be the same
# as Global.animation_tags's classes. Needed for undo/redo to work properly.
for i in new_animation_tags.size():
new_animation_tags[i] = AnimationTag.new(new_animation_tags[i].name, new_animation_tags[i].color, new_animation_tags[i].from, new_animation_tags[i].to)
# Loop through the tags to see if the frame is in one
for tag in new_animation_tags:
if frame + 1 >= tag[2] && frame + 1 <= tag[3]:
if tag[3] == tag[2]: # If we're deleting the only frame in the tag
if frame + 1 >= tag.from && frame + 1 <= tag.to:
if tag.from == tag.to: # If we're deleting the only frame in the tag
new_animation_tags.erase(tag)
else:
tag[3] -= 1
elif frame + 1 < tag[2]:
tag[2] -= 1
tag[3] -= 1
tag.to -= 1
elif frame + 1 < tag.from:
tag.from -= 1
tag.to -= 1
# Check if one of the cels of the frame is linked
# if they are, unlink them too
# this prevents removed cels being kept in linked memory
var new_layers : Array = Global.layers.duplicate(true)
var new_layers : Array = Global.layers.duplicate()
# Loop through the array to create new classes for each element, so that they
# won't be the same as the original array's classes. Needed for undo/redo to work properly.
for i in new_layers.size():
var new_linked_cels = new_layers[i].linked_cels.duplicate()
new_layers[i] = Layer.new(new_layers[i].name, new_layers[i].visible, new_layers[i].locked, new_layers[i].frame_container, new_layers[i].new_cels_linked, new_linked_cels)
for layer in new_layers:
for linked in layer.linked_cels:
if linked == Global.canvases[frame]:
@ -134,11 +145,16 @@ func _on_CopyFrame_pressed(frame := -1) -> void:
sprite.lock()
new_canvas.layers.append(Cel.new(sprite, layer.opacity))
var new_animation_tags := Global.animation_tags.duplicate(true)
var new_animation_tags := Global.animation_tags.duplicate()
# Loop through the tags to create new classes for them, so that they won't be the same
# as Global.animation_tags's classes. Needed for undo/redo to work properly.
for i in new_animation_tags.size():
new_animation_tags[i] = AnimationTag.new(new_animation_tags[i].name, new_animation_tags[i].color, new_animation_tags[i].from, new_animation_tags[i].to)
# Loop through the tags to see if the frame is in one
for tag in new_animation_tags:
if frame + 1 >= tag[2] && frame + 1 <= tag[3]:
tag[3] += 1
if frame + 1 >= tag.from && frame + 1 <= tag.to:
tag.to += 1
Global.undos += 1
Global.undo_redo.create_action("Add Frame")
@ -263,9 +279,9 @@ func play_animation(play : bool, forward_dir : bool) -> void:
last_frame = Global.canvases.size() - 1
if Global.play_only_tags:
for tag in Global.animation_tags:
if Global.current_frame + 1 >= tag[2] && Global.current_frame + 1 <= tag[3]:
first_frame = tag[2] - 1
last_frame = min(Global.canvases.size() - 1, tag[3] - 1)
if Global.current_frame + 1 >= tag.from && Global.current_frame + 1 <= tag.to:
first_frame = tag.from - 1
last_frame = min(Global.canvases.size() - 1, tag.to - 1)
if first_frame == last_frame:
if forward_dir:
@ -400,11 +416,11 @@ func change_layer_order(rate : int) -> void:
new_layers[change] = temp
Global.undo_redo.create_action("Change Layer Order")
for c in Global.canvases:
var new_layers_canvas : Array = c.layers.duplicate()
var temp_canvas = new_layers_canvas[Global.current_layer]
new_layers_canvas[Global.current_layer] = new_layers_canvas[change]
new_layers_canvas[change] = temp_canvas
Global.undo_redo.add_do_property(c, "layers", new_layers_canvas)
var new_canvas_layers : Array = c.layers.duplicate()
var temp_canvas = new_canvas_layers[Global.current_layer]
new_canvas_layers[Global.current_layer] = new_canvas_layers[change]
new_canvas_layers[change] = temp_canvas
Global.undo_redo.add_do_property(c, "layers", new_canvas_layers)
Global.undo_redo.add_undo_property(c, "layers", c.layers)
Global.undo_redo.add_do_property(Global, "current_layer", change)
@ -418,14 +434,21 @@ func change_layer_order(rate : int) -> void:
func _on_MergeDownLayer_pressed() -> void:
var new_layers : Array = Global.layers.duplicate(true)
var new_layers : Array = Global.layers.duplicate()
# Loop through the array to create new classes for each element, so that they
# won't be the same as the original array's classes. Needed for undo/redo to work properly.
for i in new_layers.size():
var new_linked_cels = new_layers[i].linked_cels.duplicate()
new_layers[i] = Layer.new(new_layers[i].name, new_layers[i].visible, new_layers[i].locked, new_layers[i].frame_container, new_layers[i].new_cels_linked, new_linked_cels)
Global.undos += 1
Global.undo_redo.create_action("Merge Layer")
for c in Global.canvases:
var new_layers_canvas : Array = c.layers.duplicate(true)
var new_canvas_layers : Array = c.layers.duplicate()
for i in new_canvas_layers.size():
new_canvas_layers[i] = Cel.new(new_canvas_layers[i].image, new_canvas_layers[i].opacity)
var selected_layer := Image.new()
selected_layer.copy_from(new_layers_canvas[Global.current_layer].image)
selected_layer.copy_from(new_canvas_layers[Global.current_layer].image)
selected_layer.lock()
if c.layers[Global.current_layer].opacity < 1: # If we have layer transparency
@ -439,15 +462,15 @@ func _on_MergeDownLayer_pressed() -> void:
new_layer.copy_from(c.layers[Global.current_layer - 1].image)
new_layer.lock()
DrawingAlgos.blend_rect(new_layer, selected_layer, Rect2(c.position, c.size), Vector2.ZERO)
new_layers_canvas.remove(Global.current_layer)
new_canvas_layers.remove(Global.current_layer)
if !selected_layer.is_invisible() and Global.layers[Global.current_layer - 1].linked_cels.size() > 1 and (c in Global.layers[Global.current_layer - 1].linked_cels):
new_layers[Global.current_layer - 1].linked_cels.erase(c)
new_layers_canvas[Global.current_layer - 1].image = new_layer
new_canvas_layers[Global.current_layer - 1].image = new_layer
else:
Global.undo_redo.add_do_property(c.layers[Global.current_layer - 1].image, "data", new_layer.data)
Global.undo_redo.add_undo_property(c.layers[Global.current_layer - 1].image, "data", c.layers[Global.current_layer - 1].image.data)
Global.undo_redo.add_do_property(c, "layers", new_layers_canvas)
Global.undo_redo.add_do_property(c, "layers", new_canvas_layers)
Global.undo_redo.add_undo_property(c, "layers", c.layers)
new_layers.remove(Global.current_layer)

View file

@ -22,7 +22,7 @@
[ext_resource path="res://assets/graphics/dark_themes/timeline/onion_skinning_off.png" type="Texture" id=29]
[ext_resource path="res://assets/graphics/dark_themes/timeline/expandable.png" type="Texture" id=30]
[ext_resource path="res://assets/graphics/dark_themes/timeline/loop.png" type="Texture" id=31]
[ext_resource path="res://src/UI/Dialogs/FrameTagDialog.tscn" type="PackedScene" id=42]
[ext_resource path="res://src/UI/Timeline/FrameTagDialog.tscn" type="PackedScene" id=42]
[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.0627451, 0.0627451, 0.0627451, 1 )

View file

@ -55,18 +55,22 @@ func _on_PopupMenu_id_pressed(ID : int) -> void:
4: # Unlink Cel
var cel_index : int = Global.layers[layer].linked_cels.find(Global.canvases[frame])
var c = Global.canvases[frame]
var new_layers : Array = Global.layers.duplicate(true)
var new_canvas_layers : Array = c.layers.duplicate(true)
var new_layers : Array = Global.layers.duplicate()
# Loop through the array to create new classes for each element, so that they
# won't be the same as the original array's classes. Needed for undo/redo to work properly.
for i in new_layers.size():
var new_linked_cels = new_layers[i].linked_cels.duplicate()
new_layers[i] = Layer.new(new_layers[i].name, new_layers[i].visible, new_layers[i].locked, new_layers[i].frame_container, new_layers[i].new_cels_linked, new_linked_cels)
var new_canvas_layers : Array = c.layers.duplicate()
for i in new_canvas_layers.size():
new_canvas_layers[i] = Cel.new(new_canvas_layers[i].image, new_canvas_layers[i].opacity)
if popup_menu.get_item_metadata(4) == "Unlink Cel":
new_layers[layer].linked_cels.remove(cel_index)
var sprite := Image.new()
sprite.copy_from(Global.canvases[frame].layers[layer][0])
sprite.copy_from(Global.canvases[frame].layers[layer].image)
sprite.lock()
var tex := ImageTexture.new()
tex.create_from_image(sprite, 0)
new_canvas_layers[layer][0] = sprite
new_canvas_layers[layer][1] = tex
new_canvas_layers[layer].image = sprite
Global.undo_redo.create_action("Unlink Cel")
Global.undo_redo.add_do_property(Global, "layers", new_layers)
@ -84,8 +88,8 @@ func _on_PopupMenu_id_pressed(ID : int) -> void:
if new_layers[layer].linked_cels.size() > 1:
# If there are already linked cels, set the current cel's image
# to the first linked cel's image
new_canvas_layers[layer][0] = new_layers[layer].linked_cels[0].layers[layer][0]
new_canvas_layers[layer][1] = new_layers[layer].linked_cels[0].layers[layer][1]
new_canvas_layers[layer].image = new_layers[layer].linked_cels[0].layers[layer].image
new_canvas_layers[layer].image_texture = new_layers[layer].linked_cels[0].layers[layer].image_texture
Global.undo_redo.add_do_property(c, "layers", new_canvas_layers)
Global.undo_redo.add_undo_property(c, "layers", c.layers)

View file

@ -0,0 +1,143 @@
extends AcceptDialog
var current_tag_id := 0
var tag_vboxes := []
var delete_tag_button : Button
onready var main_vbox_cont : VBoxContainer = $VBoxContainer/ScrollContainer/VBoxTagContainer
onready var add_tag_button : TextureButton = $VBoxContainer/ScrollContainer/VBoxTagContainer/AddTag
onready var options_dialog = $TagOptions
func _ready() -> void:
$"TagOptions/GridContainer/ColorPickerButton".get_picker().presets_visible = false
func _on_FrameTagDialog_about_to_show() -> void:
Global.dialog_open(true)
for vbox in tag_vboxes:
vbox.queue_free()
tag_vboxes.clear()
var i := 0
for tag in Global.animation_tags:
var vbox_cont := VBoxContainer.new()
var hbox_cont := HBoxContainer.new()
var tag_label := Label.new()
if tag.from == tag.to:
tag_label.text = "Tag %s (Frame %s)" % [i + 1, tag.from]
else:
tag_label.text = "Tag %s (Frames %s-%s)" % [i + 1, tag.from, tag.to]
hbox_cont.add_child(tag_label)
var edit_button := Button.new()
edit_button.text = "Edit"
edit_button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
edit_button.connect("pressed", self, "_on_EditButton_pressed", [i])
hbox_cont.add_child(edit_button)
vbox_cont.add_child(hbox_cont)
var name_label := Label.new()
name_label.text = tag.name
name_label.modulate = tag.color
vbox_cont.add_child(name_label)
var hsep := HSeparator.new()
hsep.size_flags_horizontal = SIZE_EXPAND_FILL
vbox_cont.add_child(hsep)
main_vbox_cont.add_child(vbox_cont)
tag_vboxes.append(vbox_cont)
i += 1
add_tag_button.visible = true
main_vbox_cont.move_child(add_tag_button, main_vbox_cont.get_child_count() - 1)
func _on_FrameTagDialog_popup_hide() -> void:
Global.dialog_open(false)
func _on_AddTag_pressed() -> void:
options_dialog.popup_centered()
current_tag_id = Global.animation_tags.size()
options_dialog.get_node("GridContainer/FromSpinBox").value = Global.current_frame + 1
options_dialog.get_node("GridContainer/ToSpinBox").value = Global.current_frame + 1
func _on_EditButton_pressed(_tag_id : int) -> void:
options_dialog.popup_centered()
current_tag_id = _tag_id
options_dialog.get_node("GridContainer/NameLineEdit").text = Global.animation_tags[_tag_id].name
options_dialog.get_node("GridContainer/ColorPickerButton").color = Global.animation_tags[_tag_id].color
options_dialog.get_node("GridContainer/FromSpinBox").value = Global.animation_tags[_tag_id].from
options_dialog.get_node("GridContainer/ToSpinBox").value = Global.animation_tags[_tag_id].to
if !delete_tag_button:
delete_tag_button = options_dialog.add_button("Delete Tag", true, "delete_tag")
else:
delete_tag_button.visible = true
func _on_TagOptions_confirmed() -> void:
var tag_name : String = options_dialog.get_node("GridContainer/NameLineEdit").text
var tag_color : Color = options_dialog.get_node("GridContainer/ColorPickerButton").color
var tag_from : int = options_dialog.get_node("GridContainer/FromSpinBox").value
var tag_to : int = options_dialog.get_node("GridContainer/ToSpinBox").value
if tag_to > Global.canvases.size():
tag_to = Global.canvases.size()
if tag_from > tag_to:
tag_from = tag_to
var new_animation_tags := Global.animation_tags.duplicate()
# Loop through the tags to create new classes for them, so that they won't be the same
# as Global.animation_tags's classes. Needed for undo/redo to work properly.
for i in new_animation_tags.size():
new_animation_tags[i] = AnimationTag.new(new_animation_tags[i].name, new_animation_tags[i].color, new_animation_tags[i].from, new_animation_tags[i].to)
if current_tag_id == Global.animation_tags.size():
new_animation_tags.append(AnimationTag.new(tag_name, tag_color, tag_from, tag_to))
else:
new_animation_tags[current_tag_id].name = tag_name
new_animation_tags[current_tag_id].color = tag_color
new_animation_tags[current_tag_id].from = tag_from
new_animation_tags[current_tag_id].to = tag_to
# Handle Undo/Redo
Global.undos += 1
Global.undo_redo.create_action("Modify Frame Tag")
Global.undo_redo.add_do_method(Global, "general_redo")
Global.undo_redo.add_undo_method(Global, "general_undo")
Global.undo_redo.add_do_property(Global, "animation_tags", new_animation_tags)
Global.undo_redo.add_undo_property(Global, "animation_tags", Global.animation_tags)
Global.undo_redo.commit_action()
_on_FrameTagDialog_about_to_show()
func _on_TagOptions_custom_action(action : String) -> void:
if action == "delete_tag":
var new_animation_tags := Global.animation_tags.duplicate()
new_animation_tags.remove(current_tag_id)
# Handle Undo/Redo
Global.undos += 1
Global.undo_redo.create_action("Delete Frame Tag")
Global.undo_redo.add_do_method(Global, "general_redo")
Global.undo_redo.add_undo_method(Global, "general_undo")
Global.undo_redo.add_do_property(Global, "animation_tags", new_animation_tags)
Global.undo_redo.add_undo_property(Global, "animation_tags", Global.animation_tags)
Global.undo_redo.commit_action()
options_dialog.hide()
_on_FrameTagDialog_about_to_show()
func _on_TagOptions_popup_hide() -> void:
if delete_tag_button:
delete_tag_button.visible = false
func _on_PlayOnlyTags_toggled(button_pressed : bool) -> void:
Global.play_only_tags = button_pressed

View file

@ -0,0 +1,168 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://src/UI/Timeline/FrameTagDialog.gd" type="Script" id=1]
[ext_resource path="res://assets/graphics/dark_themes/timeline/new_frame.png" type="Texture" id=2]
[node name="FrameTagDialog" type="AcceptDialog"]
margin_right = 83.0
margin_bottom = 58.0
rect_min_size = Vector2( 400, 200 )
window_title = "Frame Tag Properties"
resizable = true
script = ExtResource( 1 )
[node name="VBoxContainer" type="VBoxContainer" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 8.0
margin_top = 8.0
margin_right = -8.0
margin_bottom = -36.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="ScrollContainer" type="ScrollContainer" parent="VBoxContainer"]
margin_right = 384.0
margin_bottom = 128.0
size_flags_horizontal = 3
size_flags_vertical = 3
__meta__ = {
"_edit_use_anchors_": false
}
[node name="VBoxTagContainer" type="VBoxContainer" parent="VBoxContainer/ScrollContainer"]
margin_right = 384.0
margin_bottom = 28.0
size_flags_horizontal = 3
__meta__ = {
"_edit_use_anchors_": false
}
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/ScrollContainer/VBoxTagContainer"]
margin_right = 384.0
margin_bottom = 4.0
size_flags_horizontal = 3
__meta__ = {
"_edit_use_anchors_": false
}
[node name="AddTag" type="Button" parent="VBoxContainer/ScrollContainer/VBoxTagContainer" groups=[
"UIButtons",
]]
margin_top = 8.0
margin_right = 20.0
margin_bottom = 28.0
rect_min_size = Vector2( 20, 0 )
hint_tooltip = "Add a new frame tag"
mouse_default_cursor_shape = 2
size_flags_horizontal = 0
size_flags_vertical = 0
[node name="TextureRect" type="TextureRect" parent="VBoxContainer/ScrollContainer/VBoxTagContainer/AddTag"]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -6.0
margin_top = -6.0
margin_right = 6.0
margin_bottom = 6.0
texture = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="PlayOnlyTags" type="CheckBox" parent="VBoxContainer"]
margin_top = 132.0
margin_right = 333.0
margin_bottom = 156.0
hint_tooltip = "If it's selected, the animation plays only on the frames that have the same tag.
If it's not, the animation will play for all frames, ignoring tags."
mouse_default_cursor_shape = 2
size_flags_horizontal = 0
pressed = true
text = "Animation plays only on frames of the same tag"
[node name="TagOptions" type="ConfirmationDialog" parent="."]
margin_left = 8.0
margin_top = 8.0
margin_right = 392.0
margin_bottom = 164.0
[node name="GridContainer" type="GridContainer" parent="TagOptions"]
margin_left = 8.0
margin_top = 8.0
margin_right = 376.0
margin_bottom = 120.0
custom_constants/vseparation = 8
custom_constants/hseparation = 8
columns = 4
__meta__ = {
"_edit_use_anchors_": false
}
[node name="NameLabel" type="Label" parent="TagOptions/GridContainer"]
margin_top = 5.0
margin_right = 42.0
margin_bottom = 19.0
text = "Name:"
[node name="NameLineEdit" type="LineEdit" parent="TagOptions/GridContainer"]
margin_left = 50.0
margin_right = 124.0
margin_bottom = 24.0
caret_blink = true
caret_blink_speed = 0.5
[node name="ColorLabel" type="Label" parent="TagOptions/GridContainer"]
margin_left = 132.0
margin_top = 5.0
margin_right = 169.0
margin_bottom = 19.0
text = "Color:"
[node name="ColorPickerButton" type="ColorPickerButton" parent="TagOptions/GridContainer"]
margin_left = 177.0
margin_right = 251.0
margin_bottom = 24.0
mouse_default_cursor_shape = 2
color = Color( 1, 0, 0, 1 )
[node name="FromLabel" type="Label" parent="TagOptions/GridContainer"]
margin_top = 37.0
margin_right = 42.0
margin_bottom = 51.0
text = "From:"
[node name="FromSpinBox" type="SpinBox" parent="TagOptions/GridContainer"]
margin_left = 50.0
margin_top = 32.0
margin_right = 124.0
margin_bottom = 56.0
mouse_default_cursor_shape = 2
min_value = 1.0
value = 1.0
[node name="ToLabel" type="Label" parent="TagOptions/GridContainer"]
margin_left = 132.0
margin_top = 37.0
margin_right = 169.0
margin_bottom = 51.0
text = "To:"
[node name="ToSpinBox" type="SpinBox" parent="TagOptions/GridContainer"]
margin_left = 177.0
margin_top = 32.0
margin_right = 251.0
margin_bottom = 56.0
mouse_default_cursor_shape = 2
min_value = 1.0
value = 1.0
[connection signal="about_to_show" from="." to="." method="_on_FrameTagDialog_about_to_show"]
[connection signal="popup_hide" from="." to="." method="_on_FrameTagDialog_popup_hide"]
[connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxTagContainer/AddTag" to="." method="_on_AddTag_pressed"]
[connection signal="toggled" from="VBoxContainer/PlayOnlyTags" to="." method="_on_PlayOnlyTags_toggled"]
[connection signal="confirmed" from="TagOptions" to="." method="_on_TagOptions_confirmed"]
[connection signal="custom_action" from="TagOptions" to="." method="_on_TagOptions_custom_action"]
[connection signal="popup_hide" from="TagOptions" to="." method="_on_TagOptions_popup_hide"]