2019-12-14 23:18:45 +00:00
|
|
|
extends GridContainer
|
|
|
|
|
2019-12-15 01:15:01 +00:00
|
|
|
var palette_button = preload("res://Prefabs/PaletteButton.tscn");
|
2019-12-14 23:18:45 +00:00
|
|
|
|
2019-12-15 03:11:32 +00:00
|
|
|
var current_palette = "Default"
|
2019-12-17 01:23:18 +00:00
|
|
|
var from_palette : = {}
|
2019-12-15 03:11:32 +00:00
|
|
|
|
2019-12-14 23:18:45 +00:00
|
|
|
# Called when the node enters the scene tree for the first time.
|
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-16 15:14:16 +00:00
|
|
|
#Select default palette "Default"
|
2019-12-15 03:11:32 +00:00
|
|
|
on_palette_select(current_palette)
|
|
|
|
|
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-16 15:14:16 +00:00
|
|
|
if Global.palettes.has(palette_name): #Palette exists in memory
|
|
|
|
current_palette = palette_name
|
2019-12-16 15:48:23 +00:00
|
|
|
var palette : Dictionary = Global.palettes[palette_name]
|
|
|
|
|
|
|
|
Global.remove_palette_button.disabled = true # Cannot remove by default
|
2019-12-17 01:23:18 +00:00
|
|
|
if palette.has("editable"):
|
|
|
|
if palette.editable:
|
2019-12-16 15:48:23 +00:00
|
|
|
Global.remove_palette_button.disabled = false # Can remove if custom palette
|
|
|
|
|
|
|
|
_display_palette(palette)
|
2019-12-16 15:14:16 +00:00
|
|
|
else: #Use default on fail
|
|
|
|
current_palette = "Default"
|
2019-12-15 03:11:32 +00:00
|
|
|
_display_palette(Global.palettes["Default"])
|
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
func on_edit_palette() -> void:
|
|
|
|
var palette : Dictionary = Global.palettes[current_palette]
|
|
|
|
|
|
|
|
var create_new_palette := true # Create new palette by default
|
|
|
|
if palette.has("editable"):
|
|
|
|
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_name_line_edit.text = "Custom_" + current_palette
|
|
|
|
Global.new_palette_dialog.popup_centered()
|
|
|
|
else:
|
|
|
|
from_palette = {}
|
|
|
|
Global.edit_palette_popup.open(current_palette)
|
|
|
|
|
|
|
|
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():
|
|
|
|
Global.error_dialog.set_text(result);
|
|
|
|
Global.error_dialog.popup_centered()
|
|
|
|
|
|
|
|
func create_new_palette(name : String, from_palette : Dictionary = {}) -> String: # Returns empty string, else error string
|
|
|
|
var new_palette : Dictionary = {}
|
|
|
|
|
|
|
|
# Check if new name is valid
|
|
|
|
if name.empty():
|
|
|
|
return "Error: Palette must have a valid name."
|
|
|
|
if Global.palettes.has(name):
|
|
|
|
return "Error: Palette '" + name + "' already exists!"
|
|
|
|
|
|
|
|
new_palette.name = name
|
|
|
|
|
|
|
|
# Check if source palette has data
|
|
|
|
if from_palette.has("name"):
|
|
|
|
new_palette = from_palette.duplicate()
|
|
|
|
new_palette.name = name
|
|
|
|
new_palette.editable = true
|
|
|
|
else:
|
|
|
|
new_palette.colors = []
|
|
|
|
new_palette.comments = ""
|
|
|
|
new_palette.editable = true
|
|
|
|
|
|
|
|
# Add palette to Global and options
|
|
|
|
Global.palettes[name] = new_palette
|
|
|
|
Global.palette_option_button.add_item(name)
|
|
|
|
var index := Global.palette_option_button.get_item_count() - 1
|
|
|
|
Global.palette_option_button.set_item_metadata(index, name)
|
|
|
|
Global.palette_option_button.select(index)
|
|
|
|
|
|
|
|
save_palette(name, name + ".json")
|
|
|
|
|
|
|
|
on_palette_select(name)
|
|
|
|
return ""
|
|
|
|
|
2019-12-16 15:48:23 +00:00
|
|
|
func _display_palette(palette : Dictionary) -> void:
|
2019-12-15 01:15:01 +00:00
|
|
|
var index := 0
|
2019-12-16 15:14:16 +00:00
|
|
|
|
2019-12-16 15:48:23 +00:00
|
|
|
for color_data in palette.colors:
|
2019-12-15 03:11:32 +00:00
|
|
|
var color = Color(color_data.data)
|
2019-12-14 23:18:45 +00:00
|
|
|
var new_button = palette_button.instance()
|
2019-12-16 15:14:16 +00:00
|
|
|
|
2019-12-14 23:18:45 +00:00
|
|
|
new_button.get_child(0).modulate = color
|
2019-12-15 03:11:32 +00:00
|
|
|
new_button.hint_tooltip = color_data.data.to_upper() + " " + color_data.name
|
|
|
|
new_button.connect("pressed", self, "on_color_select", [index])
|
2019-12-16 15:14:16 +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-16 15:48:23 +00:00
|
|
|
var color = Color(Global.palettes[current_palette].colors[index].data)
|
2019-12-16 15:14:16 +00:00
|
|
|
|
2019-12-15 01:15:01 +00:00
|
|
|
if Input.is_action_just_released("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()
|
|
|
|
elif Input.is_action_just_released("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:
|
2019-12-15 03:11:32 +00:00
|
|
|
var dir := Directory.new()
|
2019-12-15 11:44:53 +00:00
|
|
|
|
2019-12-15 03:11:32 +00:00
|
|
|
if not dir.dir_exists("user://palettes"):
|
|
|
|
dir.make_dir("user://palettes");
|
2019-12-17 01:23:18 +00:00
|
|
|
dir.make_dir("user://custom/palettes")
|
2019-12-15 03:11:32 +00:00
|
|
|
dir.copy("res://Assets/Graphics/Palette/default_palette.json","user://palettes/default_palette.json");
|
2019-12-15 05:06:04 +00:00
|
|
|
dir.copy("res://Assets/Graphics/Palette/bubblegum16.json","user://palettes/bubblegum16.json");
|
2019-12-15 11:44:53 +00:00
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
var palette_files : Array = get_palette_files("user://palettes")
|
2019-12-15 11:44:53 +00:00
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
for file_name in palette_files:
|
2019-12-15 05:06:04 +00:00
|
|
|
var result : String = load_palette("user://palettes/" + file_name)
|
|
|
|
if result:
|
|
|
|
Global.palette_option_button.add_item(result)
|
|
|
|
var index := Global.palette_option_button.get_item_count() - 1
|
|
|
|
Global.palette_option_button.set_item_metadata(index, result)
|
|
|
|
if result == "Default":
|
|
|
|
Global.palette_option_button.select(index)
|
2019-12-17 01:23:18 +00:00
|
|
|
|
|
|
|
dir.open("user://palettes/custom")
|
|
|
|
var custom_palette_files : Array = get_palette_files("user://palettes/custom")
|
|
|
|
|
|
|
|
for file_name in custom_palette_files:
|
|
|
|
var result : String = load_palette("user://palettes/custom/" + file_name)
|
|
|
|
if result:
|
|
|
|
Global.palette_option_button.add_item(result)
|
|
|
|
var index := Global.palette_option_button.get_item_count() - 1
|
|
|
|
Global.palette_option_button.set_item_metadata(index, result)
|
|
|
|
|
|
|
|
func get_palette_files(path : String) -> Array:
|
|
|
|
var dir := Directory.new()
|
|
|
|
var results = []
|
|
|
|
|
|
|
|
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
|
|
|
|
elif not file_name.begins_with(".") && file_name.to_lower().ends_with("json"):
|
|
|
|
results.append(file_name)
|
|
|
|
|
|
|
|
dir.list_dir_end()
|
|
|
|
|
|
|
|
return results
|
2019-12-15 03:11:32 +00:00
|
|
|
|
2019-12-15 05:06:04 +00:00
|
|
|
func load_palette(path : String) -> String:
|
2019-12-16 15:14:16 +00:00
|
|
|
# Open file for reading
|
2019-12-15 03:11:32 +00:00
|
|
|
var file := File.new()
|
|
|
|
file.open(path, File.READ)
|
2019-12-15 11:44:53 +00:00
|
|
|
|
2019-12-15 03:11:32 +00:00
|
|
|
var text = file.get_as_text()
|
|
|
|
var result_json = JSON.parse(text)
|
|
|
|
var result = {}
|
|
|
|
|
2019-12-16 15:14:16 +00:00
|
|
|
var palette_name = null # Default error condition
|
2019-12-15 03:11:32 +00:00
|
|
|
|
|
|
|
if result_json.error != OK: # If parse has errors
|
|
|
|
print("Error: ", result_json.error)
|
|
|
|
print("Error Line: ", result_json.error_line)
|
|
|
|
print("Error String: ", result_json.error_string)
|
|
|
|
else: # If parse OK
|
|
|
|
var data = result_json.result
|
2019-12-16 15:14:16 +00:00
|
|
|
if data.has("name"): #If data is 'valid' palette file
|
2019-12-15 05:06:04 +00:00
|
|
|
palette_name = data.name
|
2019-12-16 15:48:23 +00:00
|
|
|
Global.palettes[data.name] = data
|
2019-12-15 11:44:53 +00:00
|
|
|
|
2019-12-15 05:06:04 +00:00
|
|
|
file.close()
|
2019-12-15 11:44:53 +00:00
|
|
|
|
2019-12-15 03:11:32 +00:00
|
|
|
return palette_name
|
|
|
|
|
2019-12-17 01:23:18 +00:00
|
|
|
func remove_current_palette() -> void:
|
|
|
|
if Global.palettes[current_palette].has("editable"):
|
|
|
|
if Global.palettes[current_palette].editable:
|
|
|
|
_delete_palette_file(current_palette + ".json")
|
|
|
|
Global.palettes.erase(current_palette)
|
|
|
|
var selected_index := Global.palette_option_button.selected
|
|
|
|
Global.palette_option_button.remove_item(selected_index)
|
|
|
|
if(selected_index - 1 >= 0):
|
|
|
|
Global.palette_option_button.select(selected_index - 1)
|
|
|
|
on_palette_select(Global.palette_option_button.get_item_metadata(selected_index - 1))
|
|
|
|
pass
|
|
|
|
|
|
|
|
func _delete_palette_file(file_name : String) -> void:
|
|
|
|
var dir = Directory.new()
|
|
|
|
dir.remove("user://palettes/custom/" + file_name)
|
|
|
|
|
|
|
|
func save_palette(palette_name : String, filename : String) -> void:
|
|
|
|
var palette_data = Global.palettes[palette_name]
|
2019-12-16 15:14:16 +00:00
|
|
|
# Open file for writing
|
2019-12-15 05:06:04 +00:00
|
|
|
var file := File.new()
|
2019-12-17 01:23:18 +00:00
|
|
|
file.open("user://palettes/custom/" + filename, File.WRITE)
|
2019-12-15 11:44:53 +00:00
|
|
|
|
2019-12-16 15:14:16 +00:00
|
|
|
# Write palette data to file
|
2019-12-17 01:23:18 +00:00
|
|
|
file.store_string(JSON.print(palette_data))
|
2019-12-15 05:06:04 +00:00
|
|
|
file.close()
|