diff --git a/src/Autoload/ExtensionsApi.gd b/src/Autoload/ExtensionsApi.gd index 774a7bf09..864bbbad8 100644 --- a/src/Autoload/ExtensionsApi.gd +++ b/src/Autoload/ExtensionsApi.gd @@ -27,7 +27,8 @@ var tools := ToolAPI.new() ## Gives ability to add/remove tools. var selection := SelectionAPI.new() ## Gives access to pixelorama's selection system. var project := ProjectAPI.new() ## Gives access to project manipulation. var export := ExportAPI.new() ## Gives access to adding custom exporters. -var import := ImportAPI.new() ## Gives access to adding custom exporters. +var import := ImportAPI.new() ## Gives access to adding custom import options. +var palette := PaletteAPI.new() ## Gives access to palettes. var signals := SignalsAPI.new() ## Gives access to the basic commonly used signals. ## This fail-safe below is designed to work ONLY if Pixelorama is launched in Godot Editor @@ -712,6 +713,19 @@ class ImportAPI: ExtensionsApi.remove_action("ImportAPI", "add_import_option") +## Gives access to palettes. +class PaletteAPI: + ## Creates and adds a new [Palette] with name [param palette_name] with [param data] + ## in the form of a [Dictionary]. + func create_palette_from_data(palette_name: String, data: Dictionary) -> void: + var palette := Palette.new(palette_name) + palette.deserialize_from_dictionary(data) + Palettes.save_palette(palette) + Palettes.palettes[palette_name] = palette + Palettes.select_palette(palette_name) + Palettes.new_palette_created.emit() + + ## Gives access to the basic commonly used signals. ## ## Gives access to the basic commonly used signals. diff --git a/src/Autoload/Palettes.gd b/src/Autoload/Palettes.gd index 606bf874b..579f4b9e1 100644 --- a/src/Autoload/Palettes.gd +++ b/src/Autoload/Palettes.gd @@ -1,15 +1,14 @@ extends Node signal palette_selected(palette_name: String) +signal new_palette_created signal new_palette_imported enum SortOptions { NEW_PALETTE, REVERSE, HUE, SATURATION, VALUE, RED, GREEN, BLUE, ALPHA } ## Presets for creating a new palette -enum NewPalettePresetType { - EMPTY = 0, FROM_CURRENT_PALETTE = 1, FROM_CURRENT_SPRITE = 2, FROM_CURRENT_SELECTION = 3 -} +enum NewPalettePresetType {EMPTY, FROM_CURRENT_PALETTE, FROM_CURRENT_SPRITE, FROM_CURRENT_SELECTION} ## Color options when user creates a new palette from current sprite or selection -enum GetColorsFrom { CURRENT_FRAME = 0, CURRENT_CEL = 1, ALL_FRAMES = 2 } +enum GetColorsFrom { CURRENT_FRAME, CURRENT_CEL, ALL_FRAMES } const DEFAULT_PALETTE_NAME := "Default" var palettes_write_path := Global.home_data_directory.path_join("Palettes") ## All available palettes @@ -53,7 +52,7 @@ func _ensure_palette_directory_exists() -> void: dir.make_dir(palettes_write_path) -func _save_palette(palette: Palette = current_palette) -> void: +func save_palette(palette: Palette = current_palette) -> void: _ensure_palette_directory_exists() if not is_instance_valid(palette): return @@ -102,13 +101,14 @@ func create_new_palette( _create_new_palette_from_current_selection( palette_name, comment, width, height, add_alpha_colors, get_colors_from ) + new_palette_created.emit() func _create_new_empty_palette( palette_name: String, comment: String, width: int, height: int ) -> void: var new_palette := Palette.new(palette_name, width, height, comment) - _save_palette(new_palette) + save_palette(new_palette) palettes[palette_name] = new_palette select_palette(palette_name) @@ -120,7 +120,7 @@ func _create_new_palette_from_current_palette(palette_name: String, comment: Str new_palette.name = palette_name new_palette.comment = comment new_palette.path = palettes_write_path.path_join(new_palette.name) + ".json" - _save_palette(new_palette) + save_palette(new_palette) palettes[palette_name] = new_palette select_palette(palette_name) @@ -194,7 +194,7 @@ func _fill_new_palette_with_colors( if not new_palette.has_theme_color(color): new_palette.add_color(color) - _save_palette(new_palette) + save_palette(new_palette) palettes[new_palette.name] = new_palette select_palette(new_palette.name) @@ -202,7 +202,7 @@ func _fill_new_palette_with_colors( func current_palette_edit(palette_name: String, comment: String, width: int, height: int) -> void: _check_palette_settings_values(palette_name, width, height) current_palette.edit(palette_name, width, height, comment) - _save_palette() + save_palette() palettes[palette_name] = current_palette @@ -232,7 +232,7 @@ func current_palette_add_color(mouse_button: int, start_index := 0) -> void: # Get color on left or right tool var color := Tools.get_assigned_color(mouse_button) current_palette.add_color(color, start_index) - _save_palette() + save_palette() func current_palette_get_color(index: int) -> Color: @@ -241,12 +241,12 @@ func current_palette_get_color(index: int) -> Color: func current_palette_set_color(index: int, color: Color) -> void: current_palette.set_color(index, color) - _save_palette() + save_palette() func current_palette_delete_color(index: int) -> void: current_palette.remove_color(index) - _save_palette() + save_palette() func current_palette_sort_colors(id: SortOptions) -> void: @@ -256,25 +256,25 @@ func current_palette_sort_colors(id: SortOptions) -> void: current_palette.reverse_colors() else: current_palette.sort(id) - _save_palette() + save_palette() func current_palette_swap_colors(source_index: int, target_index: int) -> void: current_palette.swap_colors(source_index, target_index) _select_color(MOUSE_BUTTON_LEFT, target_index) - _save_palette() + save_palette() func current_palette_copy_colors(from: int, to: int) -> void: current_palette.copy_colors(from, to) - _save_palette() + save_palette() func current_palette_insert_color(from: int, to: int) -> void: var from_color: Palette.PaletteColor = current_palette.colors[from] current_palette.remove_color(from) current_palette.insert_color(to, from_color.color) - _save_palette() + save_palette() func current_palette_get_selected_color_index(mouse_button: int) -> int: @@ -314,14 +314,6 @@ func _clear_selected_colors() -> void: right_selected_color = -1 -func current_palette_is_empty() -> bool: - return current_palette.is_empty() - - -func current_palette_is_full() -> bool: - return current_palette.is_full() - - func _check_palette_settings_values(palette_name: String, width: int, height: int) -> bool: # Just in case. These values should be not allowed in gui. if palette_name.length() <= 0 or width <= 0 or height <= 0: @@ -438,7 +430,7 @@ func import_palette_from_path(path: String, make_copy := false, is_initialising if is_instance_valid(palette): if make_copy: - _save_palette(palette) # Makes a copy of the palette + save_palette(palette) # Makes a copy of the palette palettes[palette.name] = palette var default_palette_name: String = Global.config_cache.get_value( "data", "last_palette", DEFAULT_PALETTE_NAME diff --git a/src/Palette/Palette.gd b/src/Palette/Palette.gd index 6f455e6a6..7062dfade 100644 --- a/src/Palette/Palette.gd +++ b/src/Palette/Palette.gd @@ -103,11 +103,19 @@ func deserialize(json_string: String) -> void: var data = test_json_conv.get_data() if not typeof(data) == TYPE_DICTIONARY: return + deserialize_from_dictionary(data) + + +func deserialize_from_dictionary(data: Dictionary) -> void: if data.has("comment"): comment = data.comment if data.has("colors"): for color_data in data.colors: - var color := str_to_var("Color" + color_data["color"]) as Color + var color: Color + if typeof(color_data["color"]) == TYPE_STRING: + color = str_to_var("Color" + color_data["color"]) + elif typeof(color_data["color"]) == TYPE_COLOR: + color = color_data["color"] var index := color_data["index"] as int var palette_color := PaletteColor.new(color, index) colors[index] = palette_color diff --git a/src/Palette/PalettePanel.gd b/src/Palette/PalettePanel.gd index d497fc3b5..c1459bac0 100644 --- a/src/Palette/PalettePanel.gd +++ b/src/Palette/PalettePanel.gd @@ -54,6 +54,7 @@ func _ready() -> void: sort_button_popup.add_item("Sort by blue", Palettes.SortOptions.BLUE) sort_button_popup.add_item("Sort by alpha", Palettes.SortOptions.ALPHA) Palettes.palette_selected.connect(select_palette) + Palettes.new_palette_created.connect(_new_palette_created) Palettes.new_palette_imported.connect(setup_palettes_selector) Tools.color_changed.connect(_color_changed) sort_button_popup.id_pressed.connect(sort_pressed) @@ -113,13 +114,13 @@ func redraw_current_palette() -> void: func toggle_add_delete_buttons() -> void: - add_color_button.disabled = Palettes.current_palette_is_full() + add_color_button.disabled = Palettes.current_palette.is_full() if add_color_button.disabled: add_color_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN else: add_color_button.mouse_default_cursor_shape = CURSOR_POINTING_HAND - delete_color_button.disabled = Palettes.current_palette_is_empty() - sort_button.disabled = Palettes.current_palette_is_empty() + delete_color_button.disabled = Palettes.current_palette.is_empty() + sort_button.disabled = Palettes.current_palette.is_empty() if delete_color_button.disabled: delete_color_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN sort_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN @@ -195,8 +196,6 @@ func _on_create_palette_dialog_saved( Palettes.create_new_palette( preset, palette_name, comment, width, height, add_alpha_colors, colors_from ) - setup_palettes_selector() - redraw_current_palette() func _on_edit_palette_dialog_saved( @@ -265,6 +264,11 @@ func _on_edit_palette_dialog_deleted(permanent: bool) -> void: redraw_current_palette() +func _new_palette_created() -> void: + setup_palettes_selector() + redraw_current_palette() + + func _color_changed(_color: Color, button: int) -> void: if not hidden_color_picker.get_popup().visible and is_instance_valid(Palettes.current_palette): # Unselect swatches when tools color is changed