Added GPL importer, new palette classes for clarification

This commit is contained in:
CheetoHead 2019-12-20 15:22:52 -05:00
parent 73f550f723
commit 770c633db2
10 changed files with 327 additions and 133 deletions

View file

@ -0,0 +1,116 @@
extends WindowDialog
onready var palette_grid = $VBoxContainer/HBoxContainer/Panel/EditPaletteGridContainer
onready var color_name_edit = $VBoxContainer/HBoxContainer3/EditPaletteColorNameLineEdit
onready var color_picker = $VBoxContainer/HBoxContainer/EditPaletteColorPicker
var palette_button = preload("res://Prefabs/PaletteButton.tscn");
var current_palette : String
var current_swatch := -1
var working_palette : Palette
func open(palette : String) -> void:
current_palette = palette
if Global.palettes.has(palette):
working_palette = Global.palettes[palette].duplicate()
_display_palette()
self.popup_centered()
pass
func _display_palette() -> void:
_clear_swatches()
var index := 0
for color_data in working_palette.colors:
var color = color_data.color
var new_button = palette_button.instance()
new_button.color = color
new_button.get_child(0).modulate = color
new_button.hint_tooltip = color_data.data.to_upper() + " " + color_data.name
new_button.draggable = true
new_button.index = index
new_button.connect("on_drop_data", self, "on_move_swatch")
new_button.connect("pressed", self, "on_swatch_select", [index])
palette_grid.add_child(new_button)
index += 1
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(index : int) -> void:
current_swatch = index
color_name_edit.text = working_palette.get_color_name(index)
color_picker.color = working_palette.get_color(index)
pass
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)
# Re-index swatches with new order
var index := 0
for child in palette_grid.get_children():
child.index = index
index += 1
pass
func _on_AddSwatchButton_pressed() -> void:
var color = Color.white
var new_index : int = working_palette.colors.size()
working_palette.add_color(color)
var new_button = palette_button.instance()
new_button.color = color
new_button.get_child(0).modulate = color
new_button.hint_tooltip = "#" + working_palette.get_color_data(new_index).to_upper() + " " + working_palette.get_color_name(new_index)
new_button.draggable = true
var index : int = palette_grid.get_child_count()
new_button.index = index
new_button.connect("on_drop_data", self, "on_move_swatch")
new_button.connect("pressed", self, "on_swatch_select", [index])
palette_grid.add_child(new_button)
pass # Replace with function body.
func _on_RemoveSwatchButton_pressed() -> void:
working_palette.remove_color(current_swatch)
palette_grid.remove_child(palette_grid.get_child(current_swatch))
pass # Replace with function body.
func _on_EditPaletteSaveButton_pressed() -> void:
Global.palettes[current_palette] = working_palette
Global.palette_container.on_palette_select(current_palette)
Global.palette_container.save_palette(current_palette, working_palette.name + ".json")
self.hide()
pass # Replace with function body.
func _on_EditPaletteCancelButton_pressed() -> void:
self.hide()
pass # Replace with function body.
func _on_EditPaletteColorNameLineEdit_text_changed(new_text) -> 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)
pass
func _on_EditPaletteColorPicker_color_changed(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)
pass
func _refresh_hint_tooltip(index : int):
palette_grid.get_child(current_swatch).hint_tooltip = "#" + working_palette.get_color_data(current_swatch).to_upper() + " " + working_palette.get_color_name(current_swatch)
pass

132
Scripts/Palette/Palette.gd Normal file
View file

@ -0,0 +1,132 @@
extends Reference
class_name Palette
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 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
if index < colors.size():
result = colors[index].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 := ""
if index < colors.size():
result = colors[index].data
return result
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 = ""
if index < colors.size():
result = colors[index].name
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 : Palette = get_script().new()
copy.name = name
copy.comments = comments
copy.editable = editable
for color in colors:
copy.colors.push_back(color.duplicate())
return copy
func _serialize() -> String:
var result = ""
var serialize_data : Dictionary = {
"name" : name,
"colors" : [],
"comments" : comments,
"editable" : editable
}
for color in colors:
serialize_data.colors.push_back(color.toDict())
result = JSON.print(serialize_data)
return result;
func deserialize(input_string : String) -> Palette:
var result = get_script().new()
var result_json = JSON.parse(input_string)
#
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)
result = null
else: # If parse OK
var data = result_json.result
if data.has("name"): #If data is 'valid' palette file
result = get_script().new()
result.name = data.name
result.comments = data.comments
result.editable = data.editable
for color_data in data.colors:
result.add_color(color_data.data, color_data.name)
return result
func load_from_file(path : String) -> Palette:
var result : Palette = null
var file = File.new()
if file.file_exists(path):
file.open(path, File.READ)
var text : String = file.get_as_text()
result = deserialize(text)
file.close()
return result

View file

@ -0,0 +1,26 @@
extends Button
signal on_drop_data
export var index := 0;
export var color : Color = Color.white
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):
#print(String(get_instance_id()) + ": Drag Start");
data = {source_index = index};
var drag_icon = TextureRect.new();
drag_icon.texture = drag_preview_texture;
drag_icon.modulate = color
set_drag_preview(drag_icon);
return data;
func can_drop_data(position, data):
return true;
func drop_data(position, data):
emit_signal("on_drop_data", data.source_index, index);
pass;

View file

@ -0,0 +1,42 @@
extends Reference
class_name PaletteColor
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"):
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,
"name" : name
}
return result
func fromDict(input_dict : Dictionary) -> PaletteColor:
var result = get_script().new()
result.data = input_dict.data
result.name = input_dict.name
return result
func duplicate() -> PaletteColor:
var copy : PaletteColor = get_script().new()
copy.data = data
copy.name = name
return copy

View file

@ -0,0 +1,220 @@
extends GridContainer
const palette_button = preload("res://Prefabs/PaletteButton.tscn");
var current_palette = "Default"
var from_palette : Palette
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
_load_palettes()
#Select default palette "Default"
on_palette_select(current_palette)
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
current_palette = palette_name
var palette : Palette = Global.palettes[palette_name]
Global.remove_palette_button.disabled = true # Cannot remove by default
if palette.editable:
Global.remove_palette_button.disabled = false # Can remove if custom palette
_display_palette(palette)
else: #Use default on fail
current_palette = "Default"
_display_palette(Global.palettes["Default"])
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"
from_palette = null
Global.new_palette_dialog.popup_centered()
pass
func on_import_palette() -> void:
Global.palette_import_file_dialog.popup_centered()
pass
func on_palette_import_file_selected(path) -> void:
var file := File.new()
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)
if palette:
if not Global.palettes.has(palette.name):
Global.palettes[palette.name] = palette
Global.palette_option_button.add_item(palette.name)
var index := Global.palette_option_button.get_item_count() - 1
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:
Global.error_dialog.set_text("Palette named '" + palette.name + "' already exists");
Global.error_dialog.popup_centered()
else:
Global.error_dialog.set_text("Invalid Palette file!")
Global.error_dialog.popup_centered()
pass
func on_edit_palette() -> void:
var palette : Dictionary = Global.palettes[current_palette]
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()
else:
from_palette = null
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 : Palette) -> String: # Returns empty string, else error string
var new_palette : Palette = Palette.new()
# 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:
new_palette = from_palette.duplicate()
new_palette.name = name
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 ""
func _display_palette(palette : Palette) -> void:
var index := 0
for color_data in palette.colors:
var color = color_data.color
var new_button = palette_button.instance()
new_button.get_child(0).modulate = color
new_button.hint_tooltip = "#" + color_data.data.to_upper() + " " + color_data.name
new_button.connect("pressed", self, "on_color_select", [index])
add_child(new_button)
index += 1
func on_color_select(index : int) -> void:
var color : Color = Global.palettes[current_palette].get_color(index)
if Input.is_action_just_released("left_mouse"):
Global.left_color_picker.color = color
Global.update_left_custom_brush()
elif Input.is_action_just_released("right_mouse"):
Global.right_color_picker.color = color
Global.update_right_custom_brush()
func _load_palettes() -> void:
var file := File.new()
var dir := Directory.new()
if not dir.dir_exists("user://palettes"):
dir.make_dir("user://palettes");
if not dir.dir_exists("user://palettes/custom"):
dir.make_dir("user://palettes/custom")
if not file.file_exists("user://palettes/default_palette.json"):
dir.copy("res://Assets/Graphics/Palette/default_palette.json","user://palettes/default_palette.json");
if not file.file_exists("user://palettes/bubblegum16.json"):
dir.copy("res://Assets/Graphics/Palette/bubblegum16.json","user://palettes/bubblegum16.json");
var palette_files : Array = get_palette_files("user://palettes")
for file_name in palette_files:
var palette : Palette = Palette.new().load_from_file("user://palettes/" + file_name)
if palette:
Global.palettes[palette.name] = palette
Global.palette_option_button.add_item(palette.name)
var index := 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)
dir.open("user://palettes/custom")
var custom_palette_files : Array = get_palette_files("user://palettes/custom")
for file_name in custom_palette_files:
var palette : Palette = Palette.new().load_from_file("user://palettes/custom/" + file_name)
if palette:
Global.palettes[palette.name] = palette
Global.palette_option_button.add_item(palette.name)
var index := Global.palette_option_button.get_item_count() - 1
Global.palette_option_button.set_item_metadata(index, palette.name)
func get_palette_files(path : String) -> Array:
var dir := Directory.new()
var results = []
dir.open(path)
dir.list_dir_begin()
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
func remove_current_palette() -> void:
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 = Global.palettes[palette_name]
palette.save_to_file("user://palettes/custom/" + filename)