1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 17:19:50 +00:00

Loading palettes from json file in user folder

This commit is contained in:
CheetoHead 2019-12-14 22:11:32 -05:00
parent 2a2a187ace
commit 51e26f6efa
6 changed files with 164 additions and 28 deletions

View file

@ -0,0 +1,37 @@
{
"name": "Default",
"colors" : [
{ "data" : "#FF000000", "name" : "no name" },
{ "data" : "#FF222034", "name" : "no name" },
{ "data" : "#FF45283c", "name" : "no name" },
{ "data" : "#FF663931", "name" : "no name" },
{ "data" : "#FF8f563b", "name" : "no name" },
{ "data" : "#FFdf7126", "name" : "no name" },
{ "data" : "#FFd9a066", "name" : "no name" },
{ "data" : "#FFeec39a", "name" : "no name" },
{ "data" : "#FFfbf236", "name" : "no name" },
{ "data" : "#FF99e550", "name" : "no name" },
{ "data" : "#FF6abe30", "name" : "no name" },
{ "data" : "#FF37946e", "name" : "no name" },
{ "data" : "#FF4b692f", "name" : "no name" },
{ "data" : "#FF524b24", "name" : "no name" },
{ "data" : "#FF323c39", "name" : "no name" },
{ "data" : "#FF3f3f74", "name" : "no name" },
{ "data" : "#FF306082", "name" : "no name" },
{ "data" : "#FF5b6ee1", "name" : "no name" },
{ "data" : "#FF639bff", "name" : "no name" },
{ "data" : "#FF5fcde4", "name" : "no name" },
{ "data" : "#FFcbdbfc", "name" : "no name" },
{ "data" : "#FFffffff", "name" : "no name" },
{ "data" : "#FF9badb7", "name" : "no name" },
{ "data" : "#FF847e87", "name" : "no name" },
{ "data" : "#FF696a6a", "name" : "no name" },
{ "data" : "#FF595652", "name" : "no name" },
{ "data" : "#FF76428a", "name" : "no name" },
{ "data" : "#FFac3232", "name" : "no name" },
{ "data" : "#FFd95763", "name" : "no name" },
{ "data" : "#FFd77bba", "name" : "no name" },
{ "data" : "#FF8f974a", "name" : "no name" },
{ "data" : "#FF8a6f30", "name" : "no name" }
]
}

File diff suppressed because one or more lines are too long

View file

@ -8,9 +8,10 @@
[sub_resource type="ImageTexture" id=1]
[node name="PaletteButton" type="Button"]
margin_right = 22.0
margin_bottom = 22.0
rect_min_size = Vector2( 22, 22 )
margin_right = 26.0
margin_bottom = 26.0
rect_min_size = Vector2( 26, 26 )
hint_tooltip = "Color Name"
custom_styles/hover = ExtResource( 1 )
custom_styles/pressed = ExtResource( 2 )
custom_styles/normal = ExtResource( 3 )

View file

@ -103,6 +103,10 @@ var custom_left_brush_texture := ImageTexture.new()
# warning-ignore:unused_class_variable
var custom_right_brush_texture := ImageTexture.new()
#Palettes
# warning-ignore:unused_class_variable
var palettes := {}
#Nodes
var control : Node
var left_cursor : Sprite
@ -176,6 +180,12 @@ var move_up_layer_button : BaseButton
var move_down_layer_button : BaseButton
var merge_down_layer_button : BaseButton
var add_palette_button : TextureButton
var remove_palette_button : TextureButton
var palette_option_button : OptionButton
var edit_palette_button : BaseButton
var palette_container : GridContainer
func _ready() -> void:
undo_redo = UndoRedo.new()
image_clipboard = Image.new()
@ -258,6 +268,12 @@ func _ready() -> void:
move_down_layer_button = find_node_by_name(root, "MoveDownLayer")
merge_down_layer_button = find_node_by_name(root, "MergeDownLayer")
add_palette_button = find_node_by_name(root, "AddPalette")
remove_palette_button = find_node_by_name(root, "RemovePalette")
palette_option_button = find_node_by_name(root, "PaletteOptionButton")
edit_palette_button = find_node_by_name(root, "EditPalette")
palette_container = find_node_by_name(root, "PaletteContainer")
#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:
if root.get_name() == node_name:

View file

@ -13,7 +13,7 @@ func _ready() -> void:
changed_selection()
func _input(event : InputEvent):
if event.is_action_released("ui_accept") && line_edit.visible:
if event.is_action_released("ui_accept") && line_edit.visible && event.scancode != KEY_SPACE:
label.visible = true
line_edit.visible = false
line_edit.editable = false

View file

@ -6,6 +6,8 @@ extends GridContainer
var palette_button = load("res://Prefabs/PaletteButton.tscn");
var current_palette = "Default"
var default_palette = [
Color("#FF000000"),
Color("#FF222034"),
@ -43,19 +45,95 @@ var default_palette = [
# Called when the node enters the scene tree for the first time.
func _ready():
var index = 0
for color in default_palette:
var new_button = palette_button.instance()
new_button.get_child(0).modulate = color
new_button.connect("pressed", self, "_on_color_select", [index])
add_child(new_button)
index += 1
#Global.palettes["Default"] = default_palette
_load_palettes()
on_palette_select(current_palette)
pass # Replace with function body.
func _on_color_select(index):
func _clear_swatches():
for child in get_children():
if child is BaseButton:
child.disconnect("pressed", self, "on_color_select")
child.queue_free()
pass
func on_palette_select(palette_name):
_clear_swatches()
if Global.palettes.has(palette_name):
_display_palette(Global.palettes[palette_name])
else:
_display_palette(Global.palettes["Default"])
pass
func _display_palette(palette):
var index = 0
for color_data in palette:
var color = Color(color_data.data)
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
pass
func on_color_select(index):
Global.left_color_picker.color = default_palette[index]
pass
func _load_palettes():
var files := []
var dir := Directory.new()
if not dir.dir_exists("user://palettes"):
dir.make_dir("user://palettes");
dir.copy("res://Assets/Graphics/Palette/default_palette.json","user://palettes/default_palette.json");
dir.open("user://palettes")
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"):
files.append(file_name)
dir.list_dir_end()
for file_name in files:
var success = _load_palette("user://palettes/" + file_name)
if success:
Global.palette_option_button.add_item(success)
pass
func _load_palette(path):
var file := File.new()
file.open(path, File.READ)
var text = file.get_as_text()
var result_json = JSON.parse(text)
var result = {}
var palette_name = null
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
palette_name = data.name
Global.palettes[data.name] = data.colors
return palette_name
func _save_palette(palette, path):
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass