1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-31 07:29:49 +00:00

Use a new Layer class to handle layer information

This replaces the old Global.layers nested array mess, and makes the code easier to read and to understand.
This commit is contained in:
OverloadedOrama 2020-06-01 16:42:53 +03:00
parent 1762383c6b
commit a9ca858303
13 changed files with 100 additions and 86 deletions

View file

@ -19,6 +19,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/UI/Rulers/Guides.gd"
}, {
"base": "Reference",
"class": "Layer",
"language": "GDScript",
"path": "res://src/Layer.gd"
}, {
"base": "Button",
"class": "LayerButton",
"language": "GDScript",
@ -37,6 +42,7 @@ _global_script_classes=[ {
_global_script_class_icons={
"Canvas": "",
"Guide": "",
"Layer": "",
"LayerButton": "",
"Palette": "",
"PaletteColor": ""

View file

@ -366,13 +366,11 @@ func _ready() -> void:
error_dialog = find_node_by_name(root, "ErrorDialog")
# Store [Layer name (0), Layer visibility boolean (1), Layer lock boolean (2), Frame container (3),
# will new cels be linked boolean (4), Array of linked cels (5)]
layers.append([tr("Layer") + " 0", true, false, HBoxContainer.new(), false, []])
layers.append(Layer.new())
# Thanks to https://godotengine.org/qa/17524/how-to-find-an-instanced-scene-by-its-name
func find_node_by_name(root, node_name) -> Node:
func find_node_by_name(root : Node, node_name : String) -> Node:
if root.get_name() == node_name:
return root
for child in root.get_children():
@ -488,7 +486,7 @@ func canvases_changed(value : Array) -> void:
frame_id.queue_free()
for i in range(layers.size() - 1, -1, -1):
frames_container.add_child(layers[i][3])
frames_container.add_child(layers[i].frame_container)
for j in range(canvases.size()):
var label := Label.new()
@ -503,7 +501,7 @@ func canvases_changed(value : Array) -> void:
cel_button.layer = i
cel_button.get_child(0).texture = Global.canvases[j].layers[i][1]
layers[i][3].add_child(cel_button)
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
@ -556,27 +554,27 @@ func layers_changed(value : Array) -> void:
for i in range(layers.size() - 1, -1, -1):
var layer_container = load("res://src/UI/Timeline/LayerButton.tscn").instance()
layer_container.i = i
if !layers[i][0]:
layers[i][0] = tr("Layer") + " %s" % i
if !layers[i].name:
layers[i].name = tr("Layer") + " %s" % i
layers_container.add_child(layer_container)
layer_container.label.text = layers[i][0]
layer_container.line_edit.text = layers[i][0]
layer_container.label.text = layers[i].name
layer_container.line_edit.text = layers[i].name
frames_container.add_child(layers[i][3])
frames_container.add_child(layers[i].frame_container)
for j in range(canvases.size()):
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 = Global.canvases[j].layers[i][1]
layers[i][3].add_child(cel_button)
layers[i].frame_container.add_child(cel_button)
var layer_button = layers_container.get_child(layers_container.get_child_count() - 1 - current_layer)
layer_button.pressed = true
self.current_frame = current_frame # Call frame_changed to update UI
if layers[current_layer][2]:
if layers[current_layer].locked:
disable_button(remove_layer_button, true)
if layers.size() == 1:
@ -584,7 +582,7 @@ func layers_changed(value : Array) -> void:
disable_button(move_up_layer_button, true)
disable_button(move_down_layer_button, true)
disable_button(merge_down_layer_button, true)
elif !layers[current_layer][2]:
elif !layers[current_layer].locked:
disable_button(remove_layer_button, false)
@ -602,20 +600,20 @@ func frame_changed(value : int) -> void:
text_color = Color.black
frame_ids.get_child(i).add_color_override("font_color", text_color)
for layer in layers:
if i < layer[3].get_child_count():
layer[3].get_child(i).pressed = false
if i < layer.frame_container.get_child_count():
layer.frame_container.get_child(i).pressed = false
i += 1
# Select the new canvas/frame
canvas = canvases[current_frame]
canvas.visible = true
frame_ids.get_child(current_frame).add_color_override("font_color", control.theme.get_color("Selected Color", "Label"))
if current_frame < layers[current_layer][3].get_child_count():
layers[current_layer][3].get_child(current_frame).pressed = true
if current_frame < layers[current_layer].frame_container.get_child_count():
layers[current_layer].frame_container.get_child(current_frame).pressed = true
if canvases.size() == 1:
disable_button(remove_frame_button, true)
elif !layers[current_layer][2]:
elif !layers[current_layer].locked:
disable_button(remove_frame_button, false)
Global.transparent_checker._ready() # To update the rect size
@ -646,7 +644,7 @@ func layer_changed(value : int) -> void:
disable_button(merge_down_layer_button, true)
if current_layer < layers.size():
if layers[current_layer][2]:
if layers[current_layer].locked:
disable_button(remove_layer_button, true)
else:
if layers.size() > 1:

View file

@ -60,9 +60,8 @@ func open_pxo_file(path : String, untitled_backup : bool = false) -> void:
var layer_new_cels_linked := file.get_8()
linked_cels.append(file.get_var())
# Store [Layer name (0), Layer visibility boolean (1), Layer lock boolean (2), Frame container (3),
# will new cels be linked boolean (4), Array of linked cels (5)]
Global.layers.append([layer_name, layer_visibility, layer_lock, HBoxContainer.new(), layer_new_cels_linked, []])
var l := Layer.new(layer_name, layer_visibility, layer_lock, HBoxContainer.new(), layer_new_cels_linked, [])
Global.layers.append(l)
global_layer_line = file.get_line()
var frame_line := file.get_line()
@ -80,9 +79,8 @@ func open_pxo_file(path : String, untitled_backup : bool = false) -> void:
if file_major_version == 0 and file_minor_version < 7:
var layer_name_old_version = file.get_line()
if frame == 0:
# Store [Layer name (0), Layer visibility boolean (1), Layer lock boolean (2), Frame container (3),
# will new frames be linked boolean (4), Array of linked frames (5)]
Global.layers.append([layer_name_old_version, true, false, HBoxContainer.new(), false, []])
var l := Layer.new(layer_name_old_version)
Global.layers.append(l)
var layer_transparency := 1.0
if file_major_version >= 0 and file_minor_version > 5:
layer_transparency = file.get_float()
@ -94,7 +92,7 @@ func open_pxo_file(path : String, untitled_backup : bool = false) -> void:
canvas.layers.append([image, tex, layer_transparency])
if file_major_version >= 0 and file_minor_version >= 7:
if frame in linked_cels[layer_i]:
Global.layers[layer_i][5].append(canvas)
Global.layers[layer_i].linked_cels.append(canvas)
layer_i += 1
layer_line = file.get_line()
@ -185,12 +183,12 @@ func save_pxo_file(path : String, autosave : bool) -> void:
# Store Global layers
for layer in Global.layers:
file.store_line(".")
file.store_line(layer[0]) # Layer name
file.store_8(layer[1]) # Layer visibility
file.store_8(layer[2]) # Layer lock
file.store_8(layer[4]) # Future cels linked
file.store_line(layer.name)
file.store_8(layer.visible)
file.store_8(layer.locked)
file.store_8(layer.new_cels_linked)
var linked_cels := []
for canvas in layer[5]:
for canvas in layer.linked_cels:
linked_cels.append(canvas.frame)
file.store_var(linked_cels) # Linked cels as cel numbers

View file

@ -53,7 +53,7 @@ func _ready() -> void:
# Store [Image, ImageTexture, Opacity]
layers.append([sprite, tex, 1])
if self in l[5]:
if self in l.linked_cels:
# If the linked button is pressed, set as the Image & ImageTexture
# to be the same as the first linked cel
layers[layer_i][0] = l[5][0].layers[layer_i][0]
@ -80,7 +80,7 @@ func _draw() -> void:
# Draw current frame layers
for i in range(layers.size()):
var modulate_color := Color(1, 1, 1, layers[i][2])
if Global.layers[i][1]: # if it's visible
if Global.layers[i].visible: # if it's visible
draw_texture(layers[i][1], location, modulate_color)
if Global.tile_mode:
@ -143,7 +143,7 @@ func _input(event : InputEvent) -> void:
can_undo = true
current_pixel = get_local_mouse_position() + location
if Global.current_frame != frame || Global.layers[Global.current_layer][2]:
if Global.current_frame != frame || Global.layers[Global.current_layer].locked:
previous_mouse_pos = current_pixel
previous_mouse_pos.x = clamp(previous_mouse_pos.x, location.x, location.x + size.x)
previous_mouse_pos.y = clamp(previous_mouse_pos.y, location.y, location.y + size.y)
@ -482,7 +482,7 @@ func update_texture(layer_index : int) -> void:
layers[layer_index][1].create_from_image(layers[layer_index][0], 0)
var frame_texture_rect : TextureRect
frame_texture_rect = Global.find_node_by_name(Global.layers[layer_index][3].get_child(frame), "CelTexture")
frame_texture_rect = Global.find_node_by_name(Global.layers[layer_index].frame_container.get_child(frame), "CelTexture")
frame_texture_rect.texture = layers[layer_index][1]
@ -498,7 +498,7 @@ func onion_skinning() -> void:
if Global.current_frame >= i:
var layer_i := 0
for layer in Global.canvases[Global.current_frame - i].layers:
if Global.layers[layer_i][1]: # If it's visible
if Global.layers[layer_i].visible:
color.a = 0.6 / i
draw_texture(layer[1], location, color)
layer_i += 1
@ -514,7 +514,7 @@ func onion_skinning() -> void:
if Global.current_frame < Global.canvases.size() - i:
var layer_i := 0
for layer in Global.canvases[Global.current_frame + i].layers:
if Global.layers[layer_i][1]: # If it's visible
if Global.layers[layer_i].visible:
color.a = 0.6 / i
draw_texture(layer[1], location, color)
layer_i += 1

18
src/Layer.gd Normal file
View file

@ -0,0 +1,18 @@
class_name Layer
extends Reference
var name := ""
var visible := true
var locked := false
var frame_container : HBoxContainer
var new_cels_linked := false
var linked_cels := []
func _init(_name := tr("Layer") + " 0", _visible := true, _locked := false, _frame_container := HBoxContainer.new(), _new_cels_linked := false, _linked_cels := []) -> void:
self.name = _name
self.visible = _visible
self.locked = _locked
self.frame_container = _frame_container
self.new_cels_linked = _new_cels_linked
self.linked_cels = _linked_cels

View file

@ -21,9 +21,9 @@ func _ready() -> void:
Global.window_title = "(" + tr("untitled") + ") - Pixelorama " + Global.current_version
Global.layers[0][0] = tr("Layer") + " 0"
Global.layers_container.get_child(0).label.text = Global.layers[0][0]
Global.layers_container.get_child(0).line_edit.text = Global.layers[0][0]
Global.layers[0].name = tr("Layer") + " 0"
Global.layers_container.get_child(0).label.text = Global.layers[0].name
Global.layers_container.get_child(0).line_edit.text = Global.layers[0].name
Import.import_brushes(Global.directory_module.get_brushes_search_path_in_order())
Import.import_patterns(Global.directory_module.get_patterns_search_path_in_order())
@ -500,7 +500,7 @@ func show_hsv_configuration_popup() -> void:
func image_menu_id_pressed(id : int) -> void:
if Global.layers[Global.current_layer][2]: # No changes if the layer is locked
if Global.layers[Global.current_layer].locked: # No changes if the layer is locked
return
match id:
0: # Scale Image

View file

@ -20,7 +20,7 @@ func _ready() -> void:
func _process(_delta : float) -> void:
if Global.layers[Global.current_layer][2]:
if Global.layers[Global.current_layer].locked:
return
var mouse_pos: Vector2 = get_local_mouse_position() - Global.canvas.location
var mouse_pos_floored := mouse_pos.floor()

View file

@ -75,9 +75,7 @@ func _on_CreateNewImage_confirmed() -> void:
var fill_color : Color = fill_color_node.color
Global.clear_canvases()
Global.layers.clear()
# Store [Layer name (0), Layer visibility boolean (1), Layer lock boolean (2), Frame container (3),
# will new frames be linked boolean (4), Array of linked frames (5)]
Global.layers.append([tr("Layer") + " 0", true, false, HBoxContainer.new(), false, []])
Global.layers.append(Layer.new())
Global.canvas = load("res://src/Canvas.tscn").instance()
Global.canvas.size = Vector2(width, height).floor()

View file

@ -365,7 +365,7 @@ func blend_layers(image: Image, canvas: Canvas, origin: Vector2 = Vector2(0, 0))
image.lock()
var layer_i := 0
for layer in canvas.layers:
if Global.layers[layer_i][1]:
if Global.layers[layer_i].visible:
var layer_image := Image.new()
layer_image.copy_from(layer[0])
layer_image.lock()

View file

@ -41,7 +41,7 @@ func _on_ImportSprites_files_selected(paths : PoolStringArray) -> void:
Global.layers.clear()
# Store [Layer name (0), Layer visibility boolean (1), Layer lock boolean (2), Frame container (3),
# will new frames be linked boolean (4), Array of linked frames (5)]
Global.layers.append([tr("Layer") + " 0", true, false, HBoxContainer.new(), false, []])
Global.layers.append(Layer.new())
Global.current_layer = 0
var first_path : String = paths[0]

View file

@ -4,7 +4,7 @@ var fps := 6.0
var animation_loop := 1 # 0 is no loop, 1 is cycle loop, 2 is ping-pong loop
var animation_forward := true
var first_frame := 0
var last_frame := Global.canvases.size() - 1
var last_frame : int = Global.canvases.size() - 1
var timeline_scroll : ScrollContainer
var tag_scroll_container : ScrollContainer
@ -28,7 +28,7 @@ func add_frame() -> void:
new_canvas.size = Global.canvas.size
new_canvas.frame = Global.canvases.size()
var new_canvases: Array = Global.canvases.duplicate()
var new_canvases : Array = Global.canvases.duplicate()
new_canvases.append(new_canvas)
Global.undos += 1
@ -45,8 +45,8 @@ func add_frame() -> void:
Global.undo_redo.add_undo_property(c, "visible", c.visible)
for l_i in range(Global.layers.size()):
if Global.layers[l_i][4]: # If the link button is pressed
Global.layers[l_i][5].append(new_canvas)
if Global.layers[l_i].new_cels_linked: # If the link button is pressed
Global.layers[l_i].linked_cels.append(new_canvas)
Global.undo_redo.add_undo_property(Global, "canvases", Global.canvases)
Global.undo_redo.add_undo_property(Global, "canvas", Global.canvas)
@ -61,7 +61,7 @@ func _on_DeleteFrame_pressed(frame := -1) -> void:
frame = Global.current_frame
var canvas : Canvas = Global.canvases[frame]
var new_canvases := Global.canvases.duplicate()
var new_canvases : Array = Global.canvases.duplicate()
new_canvases.erase(canvas)
var current_frame := Global.current_frame
if current_frame > 0 && current_frame == new_canvases.size(): # If it's the last frame
@ -82,11 +82,11 @@ func _on_DeleteFrame_pressed(frame := -1) -> void:
# 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 := Global.layers.duplicate(true)
var new_layers : Array = Global.layers.duplicate(true)
for layer in new_layers:
for linked in layer[5]:
for linked in layer.linked_cels:
if linked == Global.canvases[frame]:
layer[5].erase(linked)
layer.linked_cels.erase(linked)
Global.undos += 1
Global.undo_redo.create_action("Remove Frame")
@ -152,7 +152,7 @@ func _on_CopyFrame_pressed(frame := -1) -> void:
Global.undo_redo.add_do_property(Global, "current_frame", frame + 1)
Global.undo_redo.add_do_property(Global, "animation_tags", new_animation_tags)
for i in range(Global.layers.size()):
for child in Global.layers[i][3].get_children():
for child in Global.layers[i].frame_container.get_children():
Global.undo_redo.add_do_property(child, "pressed", false)
Global.undo_redo.add_undo_property(child, "pressed", child.pressed)
for c in Global.canvases:
@ -336,15 +336,11 @@ func _on_BlueRedMode_toggled(button_pressed : bool) -> void:
# Layer buttons
func add_layer(is_new := true) -> void:
var layer_name = null
if !is_new: # Clone layer
layer_name = Global.layers[Global.current_layer][0] + " (" + tr("copy") + ")"
var new_layers : Array = Global.layers.duplicate()
# Store [Layer name (0), Layer visibility boolean (1), Layer lock boolean (2), Frame container (3),
# will new frames be linked boolean (4), Array of linked frames (5)]
new_layers.append([layer_name, true, false, HBoxContainer.new(), false, []])
var l := Layer.new()
if !is_new: # Clone layer
l.name = Global.layers[Global.current_layer].name + " (" + tr("copy") + ")"
new_layers.append(l)
Global.undos += 1
Global.undo_redo.create_action("Add Layer")
@ -449,8 +445,8 @@ func _on_MergeDownLayer_pressed() -> void:
new_layer.lock()
DrawingAlgos.blend_rect(new_layer, selected_layer, Rect2(c.position, c.size), Vector2.ZERO)
new_layers_canvas.remove(Global.current_layer)
if !selected_layer.is_invisible() and Global.layers[Global.current_layer - 1][5].size() > 1 and (c in Global.layers[Global.current_layer - 1][5]):
new_layers[Global.current_layer - 1][5].erase(c)
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)
var tex := ImageTexture.new()
tex.create_from_image(new_layer, 0)
new_layers_canvas[Global.current_layer - 1][0] = new_layer

View file

@ -8,7 +8,7 @@ onready var popup_menu : PopupMenu = $PopupMenu
func _ready() -> void:
hint_tooltip = "Frame: %s, Layer: %s" % [frame + 1, layer]
if Global.canvases[frame] in Global.layers[layer][5]:
if Global.canvases[frame] in Global.layers[layer].linked_cels:
get_node("LinkedIndicator").visible = true
popup_menu.set_item_text(4, "Unlink Cel")
popup_menu.set_item_metadata(4, "Unlink Cel")
@ -53,13 +53,13 @@ func _on_PopupMenu_id_pressed(ID : int) -> void:
3: # Move Right
change_frame_order(1)
4: # Unlink Cel
var cel_index : int = Global.layers[layer][5].find(Global.canvases[frame])
var cel_index : int = Global.layers[layer].linked_cels.find(Global.canvases[frame])
var c = Global.canvases[frame]
var new_layers := Global.layers.duplicate(true)
var new_layers : Array = Global.layers.duplicate(true)
var new_canvas_layers : Array = c.layers.duplicate(true)
if popup_menu.get_item_metadata(4) == "Unlink Cel":
new_layers[layer][5].remove(cel_index)
new_layers[layer].linked_cels.remove(cel_index)
var sprite := Image.new()
sprite.copy_from(Global.canvases[frame].layers[layer][0])
sprite.lock()
@ -78,14 +78,14 @@ func _on_PopupMenu_id_pressed(ID : int) -> void:
Global.undo_redo.add_do_method(Global, "redo", [Global.canvases[frame]], layer)
Global.undo_redo.commit_action()
elif popup_menu.get_item_metadata(4) == "Link Cel":
new_layers[layer][5].append(Global.canvases[frame])
new_layers[layer].linked_cels.append(Global.canvases[frame])
Global.undo_redo.create_action("Link Cel")
Global.undo_redo.add_do_property(Global, "layers", new_layers)
if new_layers[layer][5].size() > 1:
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][5][0].layers[layer][0]
new_canvas_layers[layer][1] = new_layers[layer][5][0].layers[layer][1]
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]
Global.undo_redo.add_do_property(c, "layers", new_canvas_layers)
Global.undo_redo.add_undo_property(c, "layers", c.layers)
@ -97,7 +97,7 @@ func _on_PopupMenu_id_pressed(ID : int) -> void:
func change_frame_order(rate : int) -> void:
var change = frame + rate
var new_canvases := Global.canvases.duplicate()
var new_canvases : Array = Global.canvases.duplicate()
var temp = new_canvases[frame]
new_canvases[frame] = new_canvases[change]
new_canvases[change] = temp

View file

@ -16,7 +16,7 @@ func _ready() -> void:
label = Global.find_node_by_name(self, "Label")
line_edit = Global.find_node_by_name(self, "LineEdit")
if Global.layers[i][1]:
if Global.layers[i].visible:
Global.change_button_texturerect(visibility_button.get_child(0), "layer_visible.png")
visibility_button.get_child(0).rect_size = Vector2(24, 14)
visibility_button.get_child(0).rect_position = Vector2(4, 9)
@ -25,12 +25,12 @@ func _ready() -> void:
visibility_button.get_child(0).rect_size = Vector2(24, 8)
visibility_button.get_child(0).rect_position = Vector2(4, 12)
if Global.layers[i][2]:
if Global.layers[i].locked:
Global.change_button_texturerect(lock_button.get_child(0), "lock.png")
else:
Global.change_button_texturerect(lock_button.get_child(0), "unlock.png")
if Global.layers[i][4]: # If new layers will be linked
if Global.layers[i].new_cels_linked: # If new layers will be linked
Global.change_button_texturerect(linked_button.get_child(0), "linked_layer.png")
else:
Global.change_button_texturerect(linked_button.get_child(0), "unlinked_layer.png")
@ -59,20 +59,20 @@ func save_layer_name(new_name : String) -> void:
line_edit.editable = false
label.text = new_name
Global.layers_changed_skip = true
Global.layers[i][0] = new_name
Global.layers[i].name = new_name
func _on_VisibilityButton_pressed() -> void:
Global.layers[i][1] = !Global.layers[i][1]
Global.layers[i].visible = !Global.layers[i].visible
Global.canvas.update()
func _on_LockButton_pressed() -> void:
Global.layers[i][2] = !Global.layers[i][2]
Global.layers[i].locked = !Global.layers[i].locked
func _on_LinkButton_pressed() -> void:
Global.layers[i][4] = !Global.layers[i][4]
if Global.layers[i][4] && !Global.layers[i][5]:
Global.layers[i][5].append(Global.canvas)
Global.layers[i][3].get_child(Global.current_frame)._ready()
Global.layers[i].new_cels_linked = !Global.layers[i].new_cels_linked
if Global.layers[i].new_cels_linked && !Global.layers[i].linked_cels:
Global.layers[i].linked_cels.append(Global.canvas)
Global.layers[i].frame_container.get_child(Global.current_frame)._ready()