From fb948c33b440821627682a6d224acadf2dc67da3 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Mon, 5 Feb 2024 00:16:22 +0200 Subject: [PATCH] Add a "Create a new palette" option on the sort palette button Enabled by default, every time you sort a palette it creates a copy of the original palette and it sorts that, instead of sorting the original palette directly. This reduces the risk of destructive change, as the palette system does not have undo/redo support. If disabled, sorting affects the original palette directly, without creating a new one. --- src/Autoload/Palettes.gd | 12 +++++++++++- src/Palette/PalettePanel.gd | 32 +++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/Autoload/Palettes.gd b/src/Autoload/Palettes.gd index c0280c282..941d55efd 100644 --- a/src/Autoload/Palettes.gd +++ b/src/Autoload/Palettes.gd @@ -3,7 +3,7 @@ extends Node signal palette_selected(palette_name: String) signal new_palette_imported -enum SortOptions { 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 enum NewPalettePresetType { EMPTY = 0, FROM_CURRENT_PALETTE = 1, FROM_CURRENT_SPRITE = 2, FROM_CURRENT_SELECTION = 3 @@ -71,6 +71,14 @@ func _save_palette(palette: Palette = current_palette) -> void: Global.notification_label("Failed to save palette") +func copy_palette(palette := current_palette) -> void: + var new_palette_name := current_palette.name + while does_palette_exist(new_palette_name): + new_palette_name += " copy" + var comment := current_palette.comment + _create_new_palette_from_current_palette(new_palette_name, comment) + + func create_new_palette( preset: int, palette_name: String, @@ -242,6 +250,8 @@ func current_palette_delete_color(index: int) -> void: func current_palette_sort_colors(id: SortOptions) -> void: + if id == SortOptions.NEW_PALETTE: + return if id == SortOptions.REVERSE: current_palette.reverse_colors() else: diff --git a/src/Palette/PalettePanel.gd b/src/Palette/PalettePanel.gd index 82a2387d5..453fcbcb8 100644 --- a/src/Palette/PalettePanel.gd +++ b/src/Palette/PalettePanel.gd @@ -14,6 +14,7 @@ var edited_swatch_color := Color.TRANSPARENT @onready var add_color_button := $"%AddColor" @onready var delete_color_button := $"%DeleteColor" @onready var sort_button := %Sort as MenuButton +@onready var sort_button_popup := sort_button.get_popup() @onready var edit_palette_dialog := $"%EditPaletteDialog" @onready var create_palette_dialog := $"%CreatePaletteDialog" @@ -23,20 +24,22 @@ var edited_swatch_color := Color.TRANSPARENT func _ready() -> void: - sort_button.get_popup().add_item("Reverse colors", Palettes.SortOptions.REVERSE) - sort_button.get_popup().add_separator() - sort_button.get_popup().add_item("Sort by hue", Palettes.SortOptions.HUE) - sort_button.get_popup().add_item("Sort by saturation", Palettes.SortOptions.SATURATION) - sort_button.get_popup().add_item("Sort by value", Palettes.SortOptions.VALUE) - sort_button.get_popup().add_separator() - sort_button.get_popup().add_item("Sort by red", Palettes.SortOptions.RED) - sort_button.get_popup().add_item("Sort by green", Palettes.SortOptions.GREEN) - sort_button.get_popup().add_item("Sort by blue", Palettes.SortOptions.BLUE) - sort_button.get_popup().add_item("Sort by alpha", Palettes.SortOptions.ALPHA) + sort_button_popup.add_check_item("Create a new palette", Palettes.SortOptions.NEW_PALETTE) + sort_button_popup.set_item_checked(Palettes.SortOptions.NEW_PALETTE, true) + sort_button_popup.add_item("Reverse colors", Palettes.SortOptions.REVERSE) + sort_button_popup.add_separator() + sort_button_popup.add_item("Sort by hue", Palettes.SortOptions.HUE) + sort_button_popup.add_item("Sort by saturation", Palettes.SortOptions.SATURATION) + sort_button_popup.add_item("Sort by value", Palettes.SortOptions.VALUE) + sort_button_popup.add_separator() + sort_button_popup.add_item("Sort by red", Palettes.SortOptions.RED) + sort_button_popup.add_item("Sort by green", Palettes.SortOptions.GREEN) + 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_imported.connect(setup_palettes_selector) Tools.color_changed.connect(_color_changed) - sort_button.get_popup().id_pressed.connect(sort_pressed) + sort_button_popup.id_pressed.connect(sort_pressed) setup_palettes_selector() redraw_current_palette() @@ -152,6 +155,13 @@ func _on_DeleteColor_gui_input(event: InputEvent) -> void: func sort_pressed(id: Palettes.SortOptions) -> void: + var new_palette := sort_button_popup.is_item_checked(Palettes.SortOptions.NEW_PALETTE) + if id == Palettes.SortOptions.NEW_PALETTE: + sort_button_popup.set_item_checked(Palettes.SortOptions.NEW_PALETTE, not new_palette) + return + if new_palette: + Palettes.copy_palette() + setup_palettes_selector() Palettes.current_palette_sort_colors(id) redraw_current_palette()