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

Allow extensions to create new palettes

This commit is contained in:
Emmanouil Papadeas 2024-04-19 18:50:27 +03:00
parent 9fa9c01a93
commit 85d73bdb5d
4 changed files with 50 additions and 32 deletions

View file

@ -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 selection := SelectionAPI.new() ## Gives access to pixelorama's selection system.
var project := ProjectAPI.new() ## Gives access to project manipulation. var project := ProjectAPI.new() ## Gives access to project manipulation.
var export := ExportAPI.new() ## Gives access to adding custom exporters. 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. 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 ## 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") 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.
## ##
## Gives access to the basic commonly used signals. ## Gives access to the basic commonly used signals.

View file

@ -1,15 +1,14 @@
extends Node extends Node
signal palette_selected(palette_name: String) signal palette_selected(palette_name: String)
signal new_palette_created
signal new_palette_imported signal new_palette_imported
enum SortOptions { NEW_PALETTE, REVERSE, HUE, SATURATION, VALUE, RED, GREEN, BLUE, ALPHA } enum SortOptions { NEW_PALETTE, REVERSE, HUE, SATURATION, VALUE, RED, GREEN, BLUE, ALPHA }
## Presets for creating a new palette ## Presets for creating a new palette
enum NewPalettePresetType { enum NewPalettePresetType {EMPTY, FROM_CURRENT_PALETTE, FROM_CURRENT_SPRITE, FROM_CURRENT_SELECTION}
EMPTY = 0, FROM_CURRENT_PALETTE = 1, FROM_CURRENT_SPRITE = 2, FROM_CURRENT_SELECTION = 3
}
## Color options when user creates a new palette from current sprite or 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" const DEFAULT_PALETTE_NAME := "Default"
var palettes_write_path := Global.home_data_directory.path_join("Palettes") var palettes_write_path := Global.home_data_directory.path_join("Palettes")
## All available palettes ## All available palettes
@ -53,7 +52,7 @@ func _ensure_palette_directory_exists() -> void:
dir.make_dir(palettes_write_path) 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() _ensure_palette_directory_exists()
if not is_instance_valid(palette): if not is_instance_valid(palette):
return return
@ -102,13 +101,14 @@ func create_new_palette(
_create_new_palette_from_current_selection( _create_new_palette_from_current_selection(
palette_name, comment, width, height, add_alpha_colors, get_colors_from palette_name, comment, width, height, add_alpha_colors, get_colors_from
) )
new_palette_created.emit()
func _create_new_empty_palette( func _create_new_empty_palette(
palette_name: String, comment: String, width: int, height: int palette_name: String, comment: String, width: int, height: int
) -> void: ) -> void:
var new_palette := Palette.new(palette_name, width, height, comment) var new_palette := Palette.new(palette_name, width, height, comment)
_save_palette(new_palette) save_palette(new_palette)
palettes[palette_name] = new_palette palettes[palette_name] = new_palette
select_palette(palette_name) 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.name = palette_name
new_palette.comment = comment new_palette.comment = comment
new_palette.path = palettes_write_path.path_join(new_palette.name) + ".json" 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 palettes[palette_name] = new_palette
select_palette(palette_name) select_palette(palette_name)
@ -194,7 +194,7 @@ func _fill_new_palette_with_colors(
if not new_palette.has_theme_color(color): if not new_palette.has_theme_color(color):
new_palette.add_color(color) new_palette.add_color(color)
_save_palette(new_palette) save_palette(new_palette)
palettes[new_palette.name] = new_palette palettes[new_palette.name] = new_palette
select_palette(new_palette.name) 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: func current_palette_edit(palette_name: String, comment: String, width: int, height: int) -> void:
_check_palette_settings_values(palette_name, width, height) _check_palette_settings_values(palette_name, width, height)
current_palette.edit(palette_name, width, height, comment) current_palette.edit(palette_name, width, height, comment)
_save_palette() save_palette()
palettes[palette_name] = current_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 # Get color on left or right tool
var color := Tools.get_assigned_color(mouse_button) var color := Tools.get_assigned_color(mouse_button)
current_palette.add_color(color, start_index) current_palette.add_color(color, start_index)
_save_palette() save_palette()
func current_palette_get_color(index: int) -> Color: 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: func current_palette_set_color(index: int, color: Color) -> void:
current_palette.set_color(index, color) current_palette.set_color(index, color)
_save_palette() save_palette()
func current_palette_delete_color(index: int) -> void: func current_palette_delete_color(index: int) -> void:
current_palette.remove_color(index) current_palette.remove_color(index)
_save_palette() save_palette()
func current_palette_sort_colors(id: SortOptions) -> void: func current_palette_sort_colors(id: SortOptions) -> void:
@ -256,25 +256,25 @@ func current_palette_sort_colors(id: SortOptions) -> void:
current_palette.reverse_colors() current_palette.reverse_colors()
else: else:
current_palette.sort(id) current_palette.sort(id)
_save_palette() save_palette()
func current_palette_swap_colors(source_index: int, target_index: int) -> void: func current_palette_swap_colors(source_index: int, target_index: int) -> void:
current_palette.swap_colors(source_index, target_index) current_palette.swap_colors(source_index, target_index)
_select_color(MOUSE_BUTTON_LEFT, target_index) _select_color(MOUSE_BUTTON_LEFT, target_index)
_save_palette() save_palette()
func current_palette_copy_colors(from: int, to: int) -> void: func current_palette_copy_colors(from: int, to: int) -> void:
current_palette.copy_colors(from, to) current_palette.copy_colors(from, to)
_save_palette() save_palette()
func current_palette_insert_color(from: int, to: int) -> void: func current_palette_insert_color(from: int, to: int) -> void:
var from_color: Palette.PaletteColor = current_palette.colors[from] var from_color: Palette.PaletteColor = current_palette.colors[from]
current_palette.remove_color(from) current_palette.remove_color(from)
current_palette.insert_color(to, from_color.color) current_palette.insert_color(to, from_color.color)
_save_palette() save_palette()
func current_palette_get_selected_color_index(mouse_button: int) -> int: func current_palette_get_selected_color_index(mouse_button: int) -> int:
@ -314,14 +314,6 @@ func _clear_selected_colors() -> void:
right_selected_color = -1 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: func _check_palette_settings_values(palette_name: String, width: int, height: int) -> bool:
# Just in case. These values should be not allowed in gui. # Just in case. These values should be not allowed in gui.
if palette_name.length() <= 0 or width <= 0 or height <= 0: 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 is_instance_valid(palette):
if make_copy: 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 palettes[palette.name] = palette
var default_palette_name: String = Global.config_cache.get_value( var default_palette_name: String = Global.config_cache.get_value(
"data", "last_palette", DEFAULT_PALETTE_NAME "data", "last_palette", DEFAULT_PALETTE_NAME

View file

@ -103,11 +103,19 @@ func deserialize(json_string: String) -> void:
var data = test_json_conv.get_data() var data = test_json_conv.get_data()
if not typeof(data) == TYPE_DICTIONARY: if not typeof(data) == TYPE_DICTIONARY:
return return
deserialize_from_dictionary(data)
func deserialize_from_dictionary(data: Dictionary) -> void:
if data.has("comment"): if data.has("comment"):
comment = data.comment comment = data.comment
if data.has("colors"): if data.has("colors"):
for color_data in data.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 index := color_data["index"] as int
var palette_color := PaletteColor.new(color, index) var palette_color := PaletteColor.new(color, index)
colors[index] = palette_color colors[index] = palette_color

View file

@ -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 blue", Palettes.SortOptions.BLUE)
sort_button_popup.add_item("Sort by alpha", Palettes.SortOptions.ALPHA) sort_button_popup.add_item("Sort by alpha", Palettes.SortOptions.ALPHA)
Palettes.palette_selected.connect(select_palette) Palettes.palette_selected.connect(select_palette)
Palettes.new_palette_created.connect(_new_palette_created)
Palettes.new_palette_imported.connect(setup_palettes_selector) Palettes.new_palette_imported.connect(setup_palettes_selector)
Tools.color_changed.connect(_color_changed) Tools.color_changed.connect(_color_changed)
sort_button_popup.id_pressed.connect(sort_pressed) sort_button_popup.id_pressed.connect(sort_pressed)
@ -113,13 +114,13 @@ func redraw_current_palette() -> void:
func toggle_add_delete_buttons() -> 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: if add_color_button.disabled:
add_color_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN add_color_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN
else: else:
add_color_button.mouse_default_cursor_shape = CURSOR_POINTING_HAND add_color_button.mouse_default_cursor_shape = CURSOR_POINTING_HAND
delete_color_button.disabled = Palettes.current_palette_is_empty() delete_color_button.disabled = Palettes.current_palette.is_empty()
sort_button.disabled = Palettes.current_palette_is_empty() sort_button.disabled = Palettes.current_palette.is_empty()
if delete_color_button.disabled: if delete_color_button.disabled:
delete_color_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN delete_color_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN
sort_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( Palettes.create_new_palette(
preset, palette_name, comment, width, height, add_alpha_colors, colors_from preset, palette_name, comment, width, height, add_alpha_colors, colors_from
) )
setup_palettes_selector()
redraw_current_palette()
func _on_edit_palette_dialog_saved( func _on_edit_palette_dialog_saved(
@ -265,6 +264,11 @@ func _on_edit_palette_dialog_deleted(permanent: bool) -> void:
redraw_current_palette() redraw_current_palette()
func _new_palette_created() -> void:
setup_palettes_selector()
redraw_current_palette()
func _color_changed(_color: Color, button: int) -> void: func _color_changed(_color: Color, button: int) -> void:
if not hidden_color_picker.get_popup().visible and is_instance_valid(Palettes.current_palette): if not hidden_color_picker.get_popup().visible and is_instance_valid(Palettes.current_palette):
# Unselect swatches when tools color is changed # Unselect swatches when tools color is changed