mirror of
https://github.com/tonytins/CozyPixelStudio.git
synced 2025-06-26 02:54:43 -04:00
Changes to scripts to follow GDScript style guide
Please read here for more info: https://docs.godotengine.org/en/latest/getting_started/scripting/gdscript/gdscript_styleguide.html This commit does not add/change any new/existing features.
This commit is contained in:
parent
62bbad7374
commit
5a54235604
32 changed files with 295 additions and 165 deletions
|
@ -13,9 +13,11 @@ onready var palette_name_edit = $VBoxContainer/PaletteOptions/EditPaletteNameLin
|
|||
onready var left_color_button = $VBoxContainer/HBoxContainer/VBoxContainer/CenterContainer/HBoxContainer/LeftColor/NinePatchRect
|
||||
onready var right_color_button = $VBoxContainer/HBoxContainer/VBoxContainer/CenterContainer/HBoxContainer/RightColor/NinePatchRect
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
$VBoxContainer/HBoxContainer/EditPaletteColorPicker.presets_visible = false
|
||||
|
||||
|
||||
func open(palette : String) -> void:
|
||||
current_palette = palette
|
||||
palette_name_edit.text = current_palette
|
||||
|
@ -28,6 +30,7 @@ func open(palette : String) -> void:
|
|||
left_color_button.modulate = Global.left_color_picker.color
|
||||
right_color_button.modulate = Global.right_color_picker.color
|
||||
|
||||
|
||||
func _display_palette() -> void:
|
||||
_clear_swatches()
|
||||
var index := 0
|
||||
|
@ -50,17 +53,20 @@ func _display_palette() -> void:
|
|||
if index > 0: # If there are colors, select the first
|
||||
on_swatch_select(palette_grid.get_child(0))
|
||||
|
||||
|
||||
func _clear_swatches() -> void:
|
||||
for child in palette_grid.get_children():
|
||||
if child is BaseButton:
|
||||
child.disconnect("on_drop_data", self, "on_move_swatch")
|
||||
child.queue_free()
|
||||
|
||||
|
||||
func on_swatch_select(new_button) -> void:
|
||||
current_swatch = new_button.index
|
||||
color_name_edit.text = working_palette.get_color_name(current_swatch)
|
||||
color_picker.color = working_palette.get_color(current_swatch)
|
||||
|
||||
|
||||
func on_move_swatch(from : int, to : int) -> void:
|
||||
working_palette.move_color(from, to)
|
||||
palette_grid.move_child(palette_grid.get_child(from), to)
|
||||
|
@ -68,6 +74,7 @@ func on_move_swatch(from : int, to : int) -> void:
|
|||
|
||||
re_index_swatches()
|
||||
|
||||
|
||||
func _on_AddSwatchButton_pressed() -> void:
|
||||
var color : Color = color_picker.color
|
||||
var new_index : int = working_palette.colors.size()
|
||||
|
@ -87,6 +94,7 @@ func _on_AddSwatchButton_pressed() -> void:
|
|||
palette_grid.add_child(new_button)
|
||||
on_swatch_select(new_button)
|
||||
|
||||
|
||||
func _on_RemoveSwatchButton_pressed() -> void:
|
||||
if working_palette.colors.size() > 0:
|
||||
working_palette.remove_color(current_swatch)
|
||||
|
@ -99,6 +107,7 @@ func _on_RemoveSwatchButton_pressed() -> void:
|
|||
if current_swatch >= 0:
|
||||
on_swatch_select(palette_grid.get_child(current_swatch))
|
||||
|
||||
|
||||
func re_index_swatches() -> void:
|
||||
# Re-index swatches with new order
|
||||
var index := 0
|
||||
|
@ -106,6 +115,7 @@ func re_index_swatches() -> void:
|
|||
child.index = index
|
||||
index += 1
|
||||
|
||||
|
||||
# Rename a palette, copying to user directory if necessary.
|
||||
func rename_palette_file_with_priority_dirs(old_fname: String, new_fname: String) -> void:
|
||||
var user_write_directory: String = Global.directory_module.get_palette_write_path()
|
||||
|
@ -144,27 +154,33 @@ func _on_EditPaletteSaveButton_pressed() -> void:
|
|||
Global.palette_container.save_palette(current_palette, working_palette.name + ".json")
|
||||
self.hide()
|
||||
|
||||
|
||||
func _on_EditPaletteCancelButton_pressed() -> void:
|
||||
self.hide()
|
||||
|
||||
|
||||
func _on_EditPaletteColorNameLineEdit_text_changed(new_text : String) -> void:
|
||||
if current_swatch >= 0 && current_swatch < working_palette.colors.size():
|
||||
working_palette.set_color_name(current_swatch, new_text)
|
||||
_refresh_hint_tooltip(current_swatch)
|
||||
|
||||
|
||||
func _on_EditPaletteColorPicker_color_changed(color : Color) -> void:
|
||||
if current_swatch >= 0 && current_swatch < working_palette.colors.size():
|
||||
palette_grid.get_child(current_swatch).get_child(0).modulate = color
|
||||
working_palette.set_color(current_swatch, color)
|
||||
_refresh_hint_tooltip(current_swatch)
|
||||
|
||||
|
||||
func _refresh_hint_tooltip(_index : int) -> void:
|
||||
palette_grid.get_child(current_swatch).hint_tooltip = "#" + working_palette.get_color_data(current_swatch).to_upper() + " " + working_palette.get_color_name(current_swatch)
|
||||
|
||||
|
||||
func _on_LeftColor_pressed() -> void:
|
||||
color_picker.color = Global.left_color_picker.color
|
||||
_on_EditPaletteColorPicker_color_changed(color_picker.color)
|
||||
|
||||
|
||||
func _on_RightColor_pressed() -> void:
|
||||
color_picker.color = Global.right_color_picker.color
|
||||
_on_EditPaletteColorPicker_color_changed(color_picker.color)
|
||||
|
|
|
@ -1,35 +1,44 @@
|
|||
class_name Palette
|
||||
extends Reference
|
||||
|
||||
func get_class():
|
||||
return "Palette"
|
||||
func is_class(_name):
|
||||
return _name == "Palette" or .is_class(_name)
|
||||
|
||||
var name : String = "Custom_Palette"
|
||||
var colors : Array = []
|
||||
var comments : String = ""
|
||||
var editable : bool = true
|
||||
|
||||
|
||||
func get_class() -> String:
|
||||
return "Palette"
|
||||
|
||||
|
||||
func is_class(_name : String) -> bool:
|
||||
return _name == "Palette" or .is_class(_name)
|
||||
|
||||
|
||||
func insert_color(index : int, new_color : Color, _name : String = "no name") -> void:
|
||||
if index <= colors.size():
|
||||
var c := PaletteColor.new(new_color, _name)
|
||||
colors.insert(index, c)
|
||||
|
||||
|
||||
func add_color(new_color : Color, _name : String = "no name") -> void:
|
||||
var c := PaletteColor.new(new_color, _name)
|
||||
colors.push_back(c)
|
||||
|
||||
|
||||
func remove_color(index : int) -> void:
|
||||
if index < colors.size():
|
||||
colors.remove(index)
|
||||
|
||||
|
||||
func move_color(from : int, to : int) -> void:
|
||||
if from < colors.size() && to < colors.size():
|
||||
var c : PaletteColor = colors[from]
|
||||
remove_color(from)
|
||||
insert_color(to, c.color, c.name)
|
||||
|
||||
|
||||
func get_color(index : int) -> Color:
|
||||
var result := Color.black
|
||||
|
||||
|
@ -38,10 +47,12 @@ func get_color(index : int) -> Color:
|
|||
|
||||
return result
|
||||
|
||||
|
||||
func set_color(index : int, new_color : Color) -> void:
|
||||
if index < colors.size():
|
||||
colors[index].color = new_color
|
||||
|
||||
|
||||
func get_color_data(index : int) -> String:
|
||||
var result := ""
|
||||
|
||||
|
@ -50,16 +61,19 @@ func get_color_data(index : int) -> String:
|
|||
|
||||
return result
|
||||
|
||||
|
||||
func has_color(color: Color) -> bool:
|
||||
for palette_color in colors:
|
||||
if palette_color.color == color:
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
func set_color_data(index : int, new_color : String) -> void:
|
||||
if index < colors.size():
|
||||
colors[index].data = new_color
|
||||
|
||||
|
||||
func get_color_name(index : int) -> String:
|
||||
var result = ""
|
||||
|
||||
|
@ -68,16 +82,19 @@ func get_color_name(index : int) -> String:
|
|||
|
||||
return result
|
||||
|
||||
|
||||
func set_color_name(index : int, new_name : String) -> void:
|
||||
if index < colors.size():
|
||||
colors[index].name = new_name
|
||||
|
||||
|
||||
func save_to_file(path : String) -> void:
|
||||
var file = File.new()
|
||||
file.open(path, File.WRITE)
|
||||
file.store_string(_serialize())
|
||||
file.close()
|
||||
|
||||
|
||||
func duplicate(): # -> Palette
|
||||
var copy = get_script().new() # : Palette
|
||||
copy.name = name
|
||||
|
@ -87,6 +104,7 @@ func duplicate(): # -> Palette
|
|||
copy.colors.push_back(color.duplicate())
|
||||
return copy
|
||||
|
||||
|
||||
func _serialize() -> String:
|
||||
var result = ""
|
||||
var serialize_data : Dictionary = {
|
||||
|
@ -102,6 +120,7 @@ func _serialize() -> String:
|
|||
|
||||
return result
|
||||
|
||||
|
||||
func deserialize(input_string : String): # -> Palette
|
||||
var result = get_script().new()
|
||||
|
||||
|
@ -126,6 +145,7 @@ func deserialize(input_string : String): # -> Palette
|
|||
|
||||
return result
|
||||
|
||||
|
||||
func load_from_file(path : String): # -> Palette
|
||||
var result = null # : Palette
|
||||
var file = File.new()
|
||||
|
|
|
@ -7,6 +7,7 @@ export var draggable := false
|
|||
|
||||
var drag_preview_texture = preload("res://Assets/Graphics/Palette/swatch_drag_preview.png")
|
||||
|
||||
|
||||
func get_drag_data(_position):
|
||||
var data = null
|
||||
if draggable:
|
||||
|
@ -17,8 +18,10 @@ func get_drag_data(_position):
|
|||
set_drag_preview(drag_icon)
|
||||
return data
|
||||
|
||||
func can_drop_data(_position, _data):
|
||||
|
||||
func can_drop_data(_position, _data) -> bool:
|
||||
return true
|
||||
|
||||
func drop_data(_position, data):
|
||||
|
||||
func drop_data(_position, data) -> void:
|
||||
emit_signal("on_drop_data", data.source_index, index)
|
||||
|
|
|
@ -1,27 +1,35 @@
|
|||
class_name PaletteColor
|
||||
extends Reference
|
||||
|
||||
func get_class():
|
||||
return "PaletteColor"
|
||||
func is_class(_name):
|
||||
return _name == "PaletteColor" or .is_class(_name)
|
||||
|
||||
var color : Color = Color.black setget _set_color
|
||||
var data : String = "" setget _set_data
|
||||
var name : String = "no name"
|
||||
|
||||
func _init(new_color : Color = Color.black, new_name : String = "no name"):
|
||||
|
||||
func get_class() -> String:
|
||||
return "PaletteColor"
|
||||
|
||||
|
||||
func is_class(_name : String) -> bool:
|
||||
return _name == "PaletteColor" or .is_class(_name)
|
||||
|
||||
|
||||
func _init(new_color : Color = Color.black, new_name : String = "no name") -> void:
|
||||
self.color = new_color
|
||||
self.name = new_name
|
||||
|
||||
|
||||
func _set_color(new_value : Color) -> void:
|
||||
color = new_value
|
||||
data = color.to_html(true)
|
||||
|
||||
|
||||
func _set_data(new_value : String) -> void:
|
||||
data = new_value
|
||||
color = Color(data)
|
||||
|
||||
|
||||
func toDict() -> Dictionary:
|
||||
var result = {
|
||||
"data" : data,
|
||||
|
@ -29,6 +37,7 @@ func toDict() -> Dictionary:
|
|||
}
|
||||
return result
|
||||
|
||||
|
||||
func fromDict(input_dict : Dictionary): # -> PaletteColor
|
||||
var result = get_script().new()
|
||||
|
||||
|
@ -37,6 +46,7 @@ func fromDict(input_dict : Dictionary): # -> PaletteColor
|
|||
|
||||
return result
|
||||
|
||||
|
||||
func duplicate(): # -> PaletteColor
|
||||
var copy = get_script().new() # : PaletteColor
|
||||
copy.data = data
|
||||
|
|
|
@ -15,12 +15,14 @@ func _ready() -> void:
|
|||
var add_palette_menu : PopupMenu = Global.add_palette_button.get_child(0)
|
||||
add_palette_menu.connect("id_pressed", self, "add_palette_menu_id_pressed")
|
||||
|
||||
|
||||
func _clear_swatches() -> void:
|
||||
for child in get_children():
|
||||
if child is BaseButton:
|
||||
child.disconnect("pressed", self, "on_color_select")
|
||||
child.queue_free()
|
||||
|
||||
|
||||
func on_palette_select(palette_name : String) -> void:
|
||||
_clear_swatches()
|
||||
if Global.palettes.has(palette_name): # Palette exists in memory
|
||||
|
@ -28,6 +30,7 @@ func on_palette_select(palette_name : String) -> void:
|
|||
var palette : Palette = Global.palettes[palette_name]
|
||||
_display_palette(palette)
|
||||
|
||||
|
||||
func on_new_empty_palette() -> void:
|
||||
Global.new_palette_dialog.window_title = "Create a new empty palette?"
|
||||
Global.new_palette_name_line_edit.text = "Custom_Palette"
|
||||
|
@ -35,10 +38,12 @@ func on_new_empty_palette() -> void:
|
|||
Global.new_palette_dialog.popup_centered()
|
||||
Global.can_draw = false
|
||||
|
||||
|
||||
func on_import_palette() -> void:
|
||||
Global.palette_import_file_dialog.popup_centered()
|
||||
Global.can_draw = false
|
||||
|
||||
|
||||
func on_palette_import_file_selected(path : String) -> void:
|
||||
var palette : Palette = null
|
||||
if path.to_lower().ends_with("json"):
|
||||
|
@ -64,9 +69,11 @@ func on_palette_import_file_selected(path : String) -> void:
|
|||
Global.error_dialog.set_text("Invalid Palette file!")
|
||||
Global.error_dialog.popup_centered()
|
||||
|
||||
|
||||
func _on_AddPalette_pressed() -> void:
|
||||
Global.add_palette_button.get_child(0).popup(Rect2(Global.add_palette_button.rect_global_position, Vector2.ONE))
|
||||
|
||||
|
||||
func on_new_palette_confirmed() -> void:
|
||||
var new_palette_name : String = Global.new_palette_name_line_edit.text
|
||||
var result : String = create_new_palette(new_palette_name, from_palette)
|
||||
|
@ -74,6 +81,7 @@ func on_new_palette_confirmed() -> void:
|
|||
Global.error_dialog.set_text(result)
|
||||
Global.error_dialog.popup_centered()
|
||||
|
||||
|
||||
func add_palette_menu_id_pressed(id : int) -> void:
|
||||
match id:
|
||||
0: # New Empty Palette
|
||||
|
@ -81,6 +89,7 @@ func add_palette_menu_id_pressed(id : int) -> void:
|
|||
1: # Import Palette
|
||||
Global.palette_container.on_import_palette()
|
||||
|
||||
|
||||
func create_new_palette(name : String, _from_palette : Palette) -> String: # Returns empty string, else error string
|
||||
var new_palette : Palette = Palette.new()
|
||||
|
||||
|
@ -109,6 +118,7 @@ func create_new_palette(name : String, _from_palette : Palette) -> String: # Ret
|
|||
on_palette_select(name)
|
||||
return ""
|
||||
|
||||
|
||||
func on_edit_palette() -> void:
|
||||
var palette : Palette = Global.palettes[current_palette]
|
||||
|
||||
|
@ -126,10 +136,12 @@ func on_edit_palette() -> void:
|
|||
from_palette = null
|
||||
Global.edit_palette_popup.open(current_palette)
|
||||
|
||||
|
||||
func _on_PaletteOptionButton_item_selected(ID : int) -> void:
|
||||
var palette_name = Global.palette_option_button.get_item_metadata(ID)
|
||||
on_palette_select(palette_name)
|
||||
|
||||
|
||||
func _display_palette(palette : Palette) -> void:
|
||||
var index := 0
|
||||
|
||||
|
@ -144,6 +156,7 @@ func _display_palette(palette : Palette) -> void:
|
|||
add_child(new_button)
|
||||
index += 1
|
||||
|
||||
|
||||
func on_color_select(index : int) -> void:
|
||||
var color : Color = Global.palettes[current_palette].get_color(index)
|
||||
|
||||
|
@ -154,6 +167,7 @@ func on_color_select(index : int) -> void:
|
|||
Global.right_color_picker.color = color
|
||||
Global.update_right_custom_brush()
|
||||
|
||||
|
||||
func _load_palettes() -> void:
|
||||
Global.directory_module.ensure_xdg_user_dirs_exist()
|
||||
var search_locations = Global.directory_module.get_palette_search_path_in_order()
|
||||
|
@ -179,6 +193,7 @@ func _load_palettes() -> void:
|
|||
if not "Default" in Global.palettes && Global.palettes.size() > 0:
|
||||
Global.palette_container._on_PaletteOptionButton_item_selected(0)
|
||||
|
||||
|
||||
# Get the palette files in a single directory.
|
||||
# if it does not exist, return []
|
||||
func get_palette_files(path : String ) -> Array:
|
||||
|
@ -201,6 +216,7 @@ func get_palette_files(path : String ) -> Array:
|
|||
dir.list_dir_end()
|
||||
return results
|
||||
|
||||
|
||||
# This returns an array of arrays, with priorities.
|
||||
# In particular, it takes an array of paths to look for
|
||||
# arrays in, in order of file and palette override priority
|
||||
|
@ -230,6 +246,7 @@ func get_palette_priority_file_map(looking_paths: Array) -> Array:
|
|||
final_list.append(to_add_files)
|
||||
return final_list
|
||||
|
||||
|
||||
# Locate the highest priority palette by the given relative filename
|
||||
# If none is found in the directories, then do nothing and return
|
||||
# null
|
||||
|
@ -243,6 +260,7 @@ func get_best_palette_file_location(looking_paths: Array, fname: String): # ->
|
|||
|
||||
return null
|
||||
|
||||
|
||||
func save_palette(palette_name : String, filename : String) -> void:
|
||||
Global.directory_module.ensure_xdg_user_dirs_exist()
|
||||
var palette = Global.palettes[palette_name]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue