2019-12-14 23:18:45 +00:00
|
|
|
extends GridContainer
|
|
|
|
|
2020-02-09 23:23:33 +00:00
|
|
|
const palette_button = preload("res://Prefabs/PaletteButton.tscn")
|
2019-12-14 23:18:45 +00:00
|
|
|
|
2020-04-11 02:13:35 +00:00
|
|
|
|
2020-02-09 23:23:33 +00:00
|
|
|
var palettes_path := "Palettes"
|
2019-12-15 03:11:32 +00:00
|
|
|
var current_palette = "Default"
|
2019-12-20 20:22:52 +00:00
|
|
|
var from_palette : Palette
|
2019-12-15 03:11:32 +00:00
|
|
|
|
2019-12-15 01:15:01 +00:00
|
|
|
func _ready() -> void:
|
2019-12-15 03:11:32 +00:00
|
|
|
_load_palettes()
|
2019-12-15 11:44:53 +00:00
|
|
|
|
2019-12-30 18:03:22 +00:00
|
|
|
# Select default palette "Default"
|
2019-12-15 03:11:32 +00:00
|
|
|
on_palette_select(current_palette)
|
|
|
|
|
2019-12-31 01:44:27 +00:00
|
|
|
var add_palette_menu : PopupMenu = Global.add_palette_button.get_child(0)
|
|
|
|
add_palette_menu.connect("id_pressed", self, "add_palette_menu_id_pressed")
|
|
|
|
|
2019-12-15 03:19:17 +00:00
|
|
|
func _clear_swatches() -> void:
|
2019-12-15 03:11:32 +00:00
|
|
|
for child in get_children():
|
|
|
|
if child is BaseButton:
|
|
|
|
child.disconnect("pressed", self, "on_color_select")
|
|
|
|
child.queue_free()
|
|
|
|
|
2019-12-15 05:06:04 +00:00
|
|
|
func on_palette_select(palette_name : String) -> void:
|
2019-12-15 03:11:32 +00:00
|
|
|
_clear_swatches()
|
2019-12-30 18:29:46 +00:00
|
|
|
if Global.palettes.has(palette_name): # Palette exists in memory
|
2019-12-16 15:14:16 +00:00
|
|
|
current_palette = palette_name
|
2019-12-20 20:22:52 +00:00
|
|
|
var palette : Palette = Global.palettes[palette_name]
|
2019-12-16 15:48:23 +00:00
|
|
|
_display_palette(palette)
|
2019-12-15 03:11:32 +00:00
|
|
|
|
2019-12-18 00:49:20 +00:00
|
|
|
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"
|
2019-12-20 20:22:52 +00:00
|
|
|
from_palette = null
|
2019-12-18 00:49:20 +00:00
|
|
|
Global.new_palette_dialog.popup_centered()
|
|
|
|
|
|
|
|
func on_import_palette() -> void:
|
2019-12-18 14:43:11 +00:00
|
|
|
Global.palette_import_file_dialog.popup_centered()
|
|
|
|
|
2019-12-31 12:40:44 +00:00
|
|
|
func on_palette_import_file_selected(path : String) -> void:
|
2019-12-20 20:22:52 +00:00
|
|
|
var palette : Palette = null
|
|
|
|
if path.to_lower().ends_with("json"):
|
|
|
|
palette = Palette.new().load_from_file(path)
|
|
|
|
elif path.to_lower().ends_with("gpl"):
|
|
|
|
palette = Import.import_gpl(path)
|
2020-03-02 21:37:52 +00:00
|
|
|
elif path.to_lower().ends_with("png"):
|
|
|
|
palette = Import.import_png_palette(path)
|
2019-12-22 18:50:37 +00:00
|
|
|
|
2019-12-20 20:22:52 +00:00
|
|
|
if palette:
|
|
|
|
if not Global.palettes.has(palette.name):
|
|
|
|
Global.palettes[palette.name] = palette
|
|
|
|
Global.palette_option_button.add_item(palette.name)
|
2019-12-27 22:00:24 +00:00
|
|
|
var index: int = Global.palette_option_button.get_item_count() - 1
|
2019-12-20 20:22:52 +00:00
|
|
|
Global.palette_option_button.set_item_metadata(index, palette.name)
|
|
|
|
Global.palette_option_button.select(index)
|
|
|
|
on_palette_select(palette.name)
|
|
|
|
save_palette(palette.name, palette.name + ".json")
|
|
|
|
else:
|
2020-01-08 15:31:56 +00:00
|
|
|
Global.error_dialog.set_text(tr("Error: Palette named '%s' already exists!") % palette.name)
|
2019-12-20 20:22:52 +00:00
|
|
|
Global.error_dialog.popup_centered()
|
|
|
|
else:
|
|
|
|
Global.error_dialog.set_text("Invalid Palette file!")
|
|
|
|
Global.error_dialog.popup_centered()
|
2019-12-18 00:49:20 +00:00
|
|
|
|
2019-12-31 01:44:27 +00:00
|
|
|
func _on_AddPalette_pressed() -> void:
|
2020-04-07 15:13:35 +00:00
|
|
|
Global.can_draw = false
|
2019-12-31 01:44:27 +00:00
|
|
|
Global.add_palette_button.get_child(0).popup(Rect2(Global.add_palette_button.rect_global_position, Vector2.ONE))
|
2019-12-17 01:23:18 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
if not result.empty():
|
2019-12-30 18:03:22 +00:00
|
|
|
Global.error_dialog.set_text(result)
|
2019-12-17 01:23:18 +00:00
|
|
|
Global.error_dialog.popup_centered()
|
|
|
|
|
2019-12-31 12:40:44 +00:00
|
|
|
func add_palette_menu_id_pressed(id : int) -> void:
|
2019-12-31 01:44:27 +00:00
|
|
|
match id:
|
|
|
|
0: # New Empty Palette
|
|
|
|
Global.palette_container.on_new_empty_palette()
|
|
|
|
1: # Import Palette
|
|
|
|
Global.palette_container.on_import_palette()
|
|
|
|
|
2019-12-24 02:08:16 +00:00
|
|
|
func create_new_palette(name : String, _from_palette : Palette) -> String: # Returns empty string, else error string
|
2019-12-20 20:22:52 +00:00
|
|
|
var new_palette : Palette = Palette.new()
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
# Check if new name is valid
|
|
|
|
if name.empty():
|
2020-01-08 15:31:56 +00:00
|
|
|
return tr("Error: Palette must have a valid name.")
|
2019-12-17 01:23:18 +00:00
|
|
|
if Global.palettes.has(name):
|
2020-01-08 15:31:56 +00:00
|
|
|
return tr("Error: Palette named '%s' already exists!") % name
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
new_palette.name = name
|
|
|
|
# Check if source palette has data
|
2019-12-24 02:08:16 +00:00
|
|
|
if _from_palette:
|
|
|
|
new_palette = _from_palette.duplicate()
|
2019-12-17 01:23:18 +00:00
|
|
|
new_palette.name = name
|
|
|
|
new_palette.editable = true
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
# Add palette to Global and options
|
|
|
|
Global.palettes[name] = new_palette
|
|
|
|
Global.palette_option_button.add_item(name)
|
2019-12-30 23:24:56 +00:00
|
|
|
var index : int = Global.palette_option_button.get_item_count() - 1
|
2019-12-17 01:23:18 +00:00
|
|
|
Global.palette_option_button.set_item_metadata(index, name)
|
|
|
|
Global.palette_option_button.select(index)
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
save_palette(name, name + ".json")
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
on_palette_select(name)
|
|
|
|
return ""
|
|
|
|
|
2019-12-31 01:44:27 +00:00
|
|
|
func on_edit_palette() -> void:
|
2020-01-12 00:22:37 +00:00
|
|
|
var palette : Palette = Global.palettes[current_palette]
|
2019-12-31 01:44:27 +00:00
|
|
|
|
|
|
|
var create_new_palette := true # Create new palette by default
|
|
|
|
if palette.editable:
|
|
|
|
create_new_palette = false # Edit if already a custom palette
|
|
|
|
|
|
|
|
if create_new_palette:
|
|
|
|
from_palette = Global.palettes[current_palette]
|
|
|
|
Global.new_palette_dialog.window_title = "Create a new custom palette from existing default?"
|
|
|
|
Global.new_palette_name_line_edit.text = "Custom_" + current_palette
|
|
|
|
Global.new_palette_dialog.popup_centered()
|
2020-04-07 15:13:35 +00:00
|
|
|
Global.can_draw = false
|
2019-12-31 01:44:27 +00:00
|
|
|
else:
|
|
|
|
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)
|
|
|
|
|
2019-12-20 20:22:52 +00:00
|
|
|
func _display_palette(palette : Palette) -> void:
|
2019-12-15 01:15:01 +00:00
|
|
|
var index := 0
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-16 15:48:23 +00:00
|
|
|
for color_data in palette.colors:
|
2019-12-20 20:22:52 +00:00
|
|
|
var color = color_data.color
|
2019-12-14 23:18:45 +00:00
|
|
|
var new_button = palette_button.instance()
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-14 23:18:45 +00:00
|
|
|
new_button.get_child(0).modulate = color
|
2019-12-20 20:22:52 +00:00
|
|
|
new_button.hint_tooltip = "#" + color_data.data.to_upper() + " " + color_data.name
|
2019-12-15 03:11:32 +00:00
|
|
|
new_button.connect("pressed", self, "on_color_select", [index])
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-14 23:18:45 +00:00
|
|
|
add_child(new_button)
|
|
|
|
index += 1
|
|
|
|
|
2019-12-15 03:19:17 +00:00
|
|
|
func on_color_select(index : int) -> void:
|
2019-12-20 20:22:52 +00:00
|
|
|
var color : Color = Global.palettes[current_palette].get_color(index)
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-26 00:01:04 +00:00
|
|
|
if Input.is_action_just_pressed("left_mouse"):
|
2019-12-15 03:35:31 +00:00
|
|
|
Global.left_color_picker.color = color
|
2019-12-15 01:15:01 +00:00
|
|
|
Global.update_left_custom_brush()
|
2019-12-26 00:01:04 +00:00
|
|
|
elif Input.is_action_just_pressed("right_mouse"):
|
2019-12-15 03:35:31 +00:00
|
|
|
Global.right_color_picker.color = color
|
2019-12-15 01:15:01 +00:00
|
|
|
Global.update_right_custom_brush()
|
2019-12-14 23:18:45 +00:00
|
|
|
|
2019-12-15 03:19:17 +00:00
|
|
|
func _load_palettes() -> void:
|
2020-04-11 06:58:58 +00:00
|
|
|
Global.directory_module.ensure_xdg_user_dirs_exist()
|
|
|
|
var search_locations = Global.directory_module.get_palette_search_path_in_order()
|
|
|
|
var priority_ordered_files := get_palette_priority_file_map(search_locations)
|
2020-04-11 02:13:35 +00:00
|
|
|
|
2020-04-11 06:58:58 +00:00
|
|
|
# Iterate backwards, so any palettes defined in default files
|
|
|
|
# get overwritten by those of the same name in user files
|
|
|
|
search_locations.invert()
|
|
|
|
priority_ordered_files.invert()
|
|
|
|
for i in range(len(search_locations)):
|
|
|
|
var base_directory : String = search_locations[i]
|
|
|
|
var palette_files : Array = priority_ordered_files[i]
|
|
|
|
for file_name in palette_files:
|
|
|
|
var palette : Palette = Palette.new().load_from_file(base_directory.plus_file(file_name))
|
|
|
|
if palette:
|
|
|
|
Global.palettes[palette.name] = palette
|
|
|
|
Global.palette_option_button.add_item(palette.name)
|
|
|
|
var index: int = Global.palette_option_button.get_item_count() - 1
|
|
|
|
Global.palette_option_button.set_item_metadata(index, palette.name)
|
|
|
|
if palette.name == "Default":
|
|
|
|
Global.palette_option_button.select(index)
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-30 18:03:22 +00:00
|
|
|
if not "Default" in Global.palettes && Global.palettes.size() > 0:
|
|
|
|
Global.control._on_PaletteOptionButton_item_selected(0)
|
|
|
|
|
2020-04-11 06:58:58 +00:00
|
|
|
# Get the palette files in a single directory.
|
|
|
|
# if it does not exist, return []
|
|
|
|
func get_palette_files(path : String ) -> Array:
|
2019-12-17 01:23:18 +00:00
|
|
|
var dir := Directory.new()
|
|
|
|
var results = []
|
2020-04-11 06:58:58 +00:00
|
|
|
|
|
|
|
if not dir.dir_exists(path):
|
|
|
|
return []
|
2019-12-17 02:22:39 +00:00
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
dir.open(path)
|
|
|
|
dir.list_dir_begin()
|
2019-12-15 11:44:53 +00:00
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
while true:
|
|
|
|
var file_name = dir.get_next()
|
|
|
|
if file_name == "":
|
|
|
|
break
|
2020-04-11 06:58:58 +00:00
|
|
|
elif (not file_name.begins_with(".")) && file_name.to_lower().ends_with("json") && not dir.current_is_dir():
|
2019-12-17 01:23:18 +00:00
|
|
|
results.append(file_name)
|
|
|
|
|
|
|
|
dir.list_dir_end()
|
|
|
|
return results
|
2019-12-15 03:11:32 +00:00
|
|
|
|
2020-04-11 06:58:58 +00:00
|
|
|
# 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
|
|
|
|
# such that the files in the first directory override the
|
|
|
|
# second, third, etc. ^.^
|
|
|
|
# It returns an array of arrays, where each output array
|
|
|
|
# corresponds to the given input array at the same index, and
|
|
|
|
# contains the (relative to the given directory) palette files
|
|
|
|
# to load, excluding all ones already existing in higher-priority
|
|
|
|
# directories. nya
|
|
|
|
# in particular, this also means you can run backwards on the result
|
|
|
|
# so that palettes with the given palette name in the higher priority
|
|
|
|
# directories override those set in lower priority directories :)
|
|
|
|
func get_palette_priority_file_map(looking_paths: Array) -> Array:
|
|
|
|
var final_list := []
|
|
|
|
# Holds pattern files already found
|
|
|
|
var working_file_set : Dictionary = {}
|
|
|
|
for search_directory in looking_paths:
|
|
|
|
var to_add_files := []
|
|
|
|
var files = get_palette_files(search_directory)
|
|
|
|
# files to check
|
|
|
|
for maybe_to_add in files:
|
|
|
|
if not maybe_to_add in working_file_set:
|
|
|
|
to_add_files.append(maybe_to_add)
|
|
|
|
working_file_set[maybe_to_add] = true
|
|
|
|
|
|
|
|
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
|
|
|
|
func get_best_palette_file_location(looking_paths: Array, fname: String): # -> String:
|
|
|
|
var priority_fmap : Array = get_palette_priority_file_map(looking_paths)
|
|
|
|
for i in range(len(looking_paths)):
|
|
|
|
var base_path : String = looking_paths[i]
|
|
|
|
var the_files : Array = priority_fmap[i]
|
|
|
|
if the_files.has(fname):
|
|
|
|
return base_path.plus_file(fname)
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
func save_palette(palette_name : String, filename : String) -> void:
|
2020-04-11 06:58:58 +00:00
|
|
|
Global.directory_module.ensure_xdg_user_dirs_exist()
|
2019-12-20 20:22:52 +00:00
|
|
|
var palette = Global.palettes[palette_name]
|
2020-04-11 06:58:58 +00:00
|
|
|
var palettes_write_path: String = Global.directory_module.get_palette_write_path()
|
|
|
|
palette.save_to_file(palettes_write_path.plus_file(filename))
|
2020-04-07 15:13:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _on_NewPaletteDialog_popup_hide() -> void:
|
|
|
|
Global.can_draw = true
|