diff --git a/src/Autoload/Global.gd b/src/Autoload/Global.gd index b931cfc15..870504ef4 100644 --- a/src/Autoload/Global.gd +++ b/src/Autoload/Global.gd @@ -453,6 +453,7 @@ func title_changed(value : String) -> void: func project_changed(value : int) -> void: current_project_index = value current_project = projects[value] + current_project.change_project() func clear_frames() -> void: diff --git a/src/Classes/Project.gd b/src/Classes/Project.gd index e3275d9c2..105193de4 100644 --- a/src/Classes/Project.gd +++ b/src/Classes/Project.gd @@ -21,7 +21,8 @@ var brush_textures := [ImageTexture.new(), ImageTexture.new()] var selected_pixels := [] -func _init(_name := tr("untitled"), _size := Vector2(64, 64)) -> void: +func _init(_frames := [], _name := tr("untitled"), _size := Vector2(64, 64)) -> void: + frames = _frames name = _name size = _size layers.append(Layer.new()) @@ -30,13 +31,64 @@ func _init(_name := tr("untitled"), _size := Vector2(64, 64)) -> void: Global.tabs.add_tab(name) +func change_project() -> void: + # Remove old nodes + for container in Global.layers_container.get_children(): + container.queue_free() + + remove_cel_buttons() + + for frame_id in Global.frame_ids.get_children(): + Global.frame_ids.remove_child(frame_id) + frame_id.queue_free() + + # Create new ones + for i in range(layers.size() - 1, -1, -1): + # Create layer buttons + var layer_container = load("res://src/UI/Timeline/LayerButton.tscn").instance() + layer_container.i = i + if layers[i].name == tr("Layer") + " 0": + layers[i].name = tr("Layer") + " %s" % i + + Global.layers_container.add_child(layer_container) + layer_container.label.text = layers[i].name + layer_container.line_edit.text = layers[i].name + + Global.frames_container.add_child(layers[i].frame_container) + for j in range(frames.size()): # Create Cel buttons + var cel_button = load("res://src/UI/Timeline/CelButton.tscn").instance() + cel_button.frame = j + cel_button.layer = i + cel_button.get_child(0).texture = frames[j].cels[i].image_texture + if j == current_frame and i == current_layer: + cel_button.pressed = true + + layers[i].frame_container.add_child(cel_button) + + for j in range(frames.size()): # Create frame ID labels + var label := Label.new() + label.rect_min_size.x = 36 + label.align = Label.ALIGN_CENTER + label.text = str(j + 1) + if j == current_frame: + label.add_color_override("font_color", Global.control.theme.get_color("Selected Color", "Label")) + Global.frame_ids.add_child(label) + + var layer_button = Global.layers_container.get_child(Global.layers_container.get_child_count() - 1 - current_layer) + layer_button.pressed = true + + Global.current_frame_mark_label.text = "%s/%s" % [str(current_frame + 1), frames.size()] + + Global.disable_button(Global.remove_frame_button, frames.size() == 1) + toggle_layer_buttons_layers() + toggle_layer_buttons_current_layer() + + self.animation_tags = animation_tags + + func frames_changed(value : Array) -> void: frames = value - for container in Global.frames_container.get_children(): - for button in container.get_children(): - container.remove_child(button) - button.queue_free() - Global.frames_container.remove_child(container) + remove_cel_buttons() for frame_id in Global.frame_ids.get_children(): Global.frame_ids.remove_child(frame_id) @@ -60,16 +112,7 @@ func frames_changed(value : Array) -> void: layers[i].frame_container.add_child(cel_button) - # This is useful in case tagged frames get deleted DURING the animation is playing - # otherwise, this code is useless in this context, since these values are being set - # when the play buttons get pressed, anyway - Global.animation_timeline.first_frame = 0 - Global.animation_timeline.last_frame = frames.size() - 1 - if Global.play_only_tags: - for tag in animation_tags: - if current_frame + 1 >= tag.from && current_frame + 1 <= tag.to: - Global.animation_timeline.first_frame = tag.from - 1 - Global.animation_timeline.last_frame = min(frames.size() - 1, tag.to - 1) + set_first_and_last_frames() func layers_changed(value : Array) -> void: @@ -81,11 +124,7 @@ func layers_changed(value : Array) -> void: for container in Global.layers_container.get_children(): container.queue_free() - for container in Global.frames_container.get_children(): - for button in container.get_children(): - container.remove_child(button) - button.queue_free() - Global.frames_container.remove_child(container) + remove_cel_buttons() for i in range(layers.size() - 1, -1, -1): var layer_container = load("res://src/UI/Timeline/LayerButton.tscn").instance() @@ -109,29 +148,27 @@ func layers_changed(value : Array) -> void: var layer_button = Global.layers_container.get_child(Global.layers_container.get_child_count() - 1 - current_layer) layer_button.pressed = true self.current_frame = current_frame # Call frame_changed to update UI + toggle_layer_buttons_layers() - if layers[current_layer].locked: - Global.disable_button(Global.remove_layer_button, true) - if layers.size() == 1: - Global.disable_button(Global.remove_layer_button, true) - Global.disable_button(Global.move_up_layer_button, true) - Global.disable_button(Global.move_down_layer_button, true) - Global.disable_button(Global.merge_down_layer_button, true) - elif !layers[current_layer].locked: - Global.disable_button(Global.remove_layer_button, false) +func remove_cel_buttons() -> void: + for container in Global.frames_container.get_children(): + for button in container.get_children(): + container.remove_child(button) + button.queue_free() + Global.frames_container.remove_child(container) func frame_changed(value : int) -> void: current_frame = value Global.current_frame_mark_label.text = "%s/%s" % [str(current_frame + 1), frames.size()] - for i in frames.size(): # De-select all the other frames + for i in frames.size(): var text_color := Color.white if Global.theme_type == Global.Theme_Types.CARAMEL || Global.theme_type == Global.Theme_Types.LIGHT: text_color = Color.black Global.frame_ids.get_child(i).add_color_override("font_color", text_color) - for layer in layers: + for layer in layers: # De-select all the other frames if i < layer.frame_container.get_child_count(): layer.frame_container.get_child(i).pressed = false @@ -140,10 +177,7 @@ func frame_changed(value : int) -> void: if current_frame < layers[current_layer].frame_container.get_child_count(): layers[current_layer].frame_container.get_child(current_frame).pressed = true - if frames.size() == 1: - Global.disable_button(Global.remove_frame_button, true) - elif !layers[current_layer].locked: - Global.disable_button(Global.remove_frame_button, false) + Global.disable_button(Global.remove_frame_button, frames.size() == 1) Global.canvas.update() Global.transparent_checker._ready() # To update the rect size @@ -162,6 +196,26 @@ func layer_changed(value : int) -> void: var layer_button = Global.layers_container.get_child(Global.layers_container.get_child_count() - 1 - current_layer) layer_button.pressed = true + toggle_layer_buttons_current_layer() + + yield(Global.get_tree().create_timer(0.01), "timeout") + self.current_frame = current_frame # Call frame_changed to update UI + + +func toggle_layer_buttons_layers() -> void: + if layers[current_layer].locked: + Global.disable_button(Global.remove_layer_button, true) + + if layers.size() == 1: + Global.disable_button(Global.remove_layer_button, true) + Global.disable_button(Global.move_up_layer_button, true) + Global.disable_button(Global.move_down_layer_button, true) + Global.disable_button(Global.merge_down_layer_button, true) + elif !layers[current_layer].locked: + Global.disable_button(Global.remove_layer_button, false) + + +func toggle_layer_buttons_current_layer() -> void: if current_layer < layers.size() - 1: Global.disable_button(Global.move_up_layer_button, false) else: @@ -181,9 +235,6 @@ func layer_changed(value : int) -> void: if layers.size() > 1: Global.disable_button(Global.remove_layer_button, false) - yield(Global.get_tree().create_timer(0.01), "timeout") - self.current_frame = current_frame # Call frame_changed to update UI - func animation_tags_changed(value : Array) -> void: animation_tags = value @@ -206,9 +257,13 @@ func animation_tags_changed(value : Array) -> void: tag_c.get_node("Line2D").points[2] = Vector2(tag_c.rect_min_size.x, 0) tag_c.get_node("Line2D").points[3] = Vector2(tag_c.rect_min_size.x, 32) + set_first_and_last_frames() + + +func set_first_and_last_frames() -> void: # This is useful in case tags get modified DURING the animation is playing # otherwise, this code is useless in this context, since these values are being set - # when the play buttons get pressed, anyway + # when the play buttons get pressed anyway Global.animation_timeline.first_frame = 0 Global.animation_timeline.last_frame = frames.size() - 1 if Global.play_only_tags: diff --git a/src/UI/Tabs.gd b/src/UI/Tabs.gd index 946b3b227..4977b1128 100644 --- a/src/UI/Tabs.gd +++ b/src/UI/Tabs.gd @@ -3,6 +3,7 @@ extends Tabs func _on_Tabs_tab_changed(tab : int): Global.current_project_index = tab + Global.canvas.update() func _on_Tabs_tab_close(tab : int): diff --git a/src/UI/Timeline/AnimationTimeline.gd b/src/UI/Timeline/AnimationTimeline.gd index b6807f1bb..844b10f29 100644 --- a/src/UI/Timeline/AnimationTimeline.gd +++ b/src/UI/Timeline/AnimationTimeline.gd @@ -363,6 +363,8 @@ func add_layer(is_new := true) -> void: func _on_RemoveLayer_pressed() -> void: + if Global.current_project.layers.size() == 1: + return var new_layers : Array = Global.current_project.layers.duplicate() new_layers.remove(Global.current_project.current_layer) Global.current_project.undos += 1