diff --git a/Translations/Translations.pot b/Translations/Translations.pot index 7ed68084c..7d3c1befc 100644 --- a/Translations/Translations.pot +++ b/Translations/Translations.pot @@ -2397,6 +2397,42 @@ msgstr "" msgid "Remove a selected color" msgstr "" +#. Tooltip of the Sort button found in the palette panel. +msgid "Sort palette" +msgstr "" + +#. An option of the Sort palette button found in the palette panel. When selected, the colors of the palette get reversed. +msgid "Reverse colors" +msgstr "" + +#. An option of the Sort palette button found in the palette panel. When selected, the colors of the palette are being sorted based on their hue. +msgid "Sort by hue" +msgstr "" + +#. An option of the Sort palette button found in the palette panel. When selected, the colors of the palette are being sorted based on their saturation. +msgid "Sort by saturation" +msgstr "" + +#. An option of the Sort palette button found in the palette panel. When selected, the colors of the palette are being sorted based on their value. +msgid "Sort by value" +msgstr "" + +#. An option of the Sort palette button found in the palette panel. When selected, the colors of the palette are being sorted based on their red channel value. +msgid "Sort by red" +msgstr "" + +#. An option of the Sort palette button found in the palette panel. When selected, the colors of the palette are being sorted based on their green channel value. +msgid "Sort by green" +msgstr "" + +#. An option of the Sort palette button found in the palette panel. When selected, the colors of the palette are being sorted based on their blue channel value. +msgid "Sort by blue" +msgstr "" + +#. An option of the Sort palette button found in the palette panel. When selected, the colors of the palette are being sorted based on their alpha channel value. +msgid "Sort by alpha" +msgstr "" + msgid "Palette with the same name and path already exists!" msgstr "" diff --git a/assets/graphics/palette/sort.png b/assets/graphics/palette/sort.png new file mode 100644 index 000000000..f88802a03 Binary files /dev/null and b/assets/graphics/palette/sort.png differ diff --git a/assets/graphics/palette/sort.png.import b/assets/graphics/palette/sort.png.import new file mode 100644 index 000000000..06c8f66d2 --- /dev/null +++ b/assets/graphics/palette/sort.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://db4i3w3yencxg" +path="res://.godot/imported/sort.png-d80e96b77a5aad6d1848c4243782da48.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/graphics/palette/sort.png" +dest_files=["res://.godot/imported/sort.png-d80e96b77a5aad6d1848c4243782da48.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/src/Autoload/Palettes.gd b/src/Autoload/Palettes.gd index 44e3ab384..eb16353a2 100644 --- a/src/Autoload/Palettes.gd +++ b/src/Autoload/Palettes.gd @@ -3,6 +3,7 @@ extends Node signal palette_selected(palette_name: String) signal new_palette_imported +enum SortOptions { 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 @@ -240,6 +241,14 @@ func current_palette_delete_color(index: int) -> void: _save_palette() +func current_palette_sort_colors(id: SortOptions) -> void: + if id == SortOptions.REVERSE: + current_palette.reverse_colors() + else: + current_palette.sort(id) + _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) diff --git a/src/Palette/Palette.gd b/src/Palette/Palette.gd index 984b1007b..c4bf97df0 100644 --- a/src/Palette/Palette.gd +++ b/src/Palette/Palette.gd @@ -247,6 +247,40 @@ func copy_colors(from_index: int, to_index: int) -> void: colors[to_index].index = to_index +func reverse_colors() -> void: + var reversed_colors := colors.values() + reversed_colors.reverse() + colors.clear() + for i in reversed_colors.size(): + reversed_colors[i].index = i + colors[i] = reversed_colors[i] + + +func sort(option: Palettes.SortOptions) -> void: + var sorted_colors := colors.values() + var sort_method: Callable + match option: + Palettes.SortOptions.HUE: + sort_method = func(a: PaletteColor, b: PaletteColor): return a.color.h < b.color.h + Palettes.SortOptions.SATURATION: + sort_method = func(a: PaletteColor, b: PaletteColor): return a.color.s < b.color.s + Palettes.SortOptions.VALUE: + sort_method = func(a: PaletteColor, b: PaletteColor): return a.color.v < b.color.v + Palettes.SortOptions.RED: + sort_method = func(a: PaletteColor, b: PaletteColor): return a.color.r < b.color.r + Palettes.SortOptions.GREEN: + sort_method = func(a: PaletteColor, b: PaletteColor): return a.color.g < b.color.g + Palettes.SortOptions.BLUE: + sort_method = func(a: PaletteColor, b: PaletteColor): return a.color.b < b.color.b + Palettes.SortOptions.ALPHA: + sort_method = func(a: PaletteColor, b: PaletteColor): return a.color.a < b.color.a + sorted_colors.sort_custom(sort_method) + colors.clear() + for i in sorted_colors.size(): + sorted_colors[i].index = i + colors[i] = sorted_colors[i] + + ## True if all swatches are occupied func is_full() -> bool: return colors.size() >= colors_max diff --git a/src/Palette/PalettePanel.gd b/src/Palette/PalettePanel.gd index c919329c8..e539ec21a 100644 --- a/src/Palette/PalettePanel.gd +++ b/src/Palette/PalettePanel.gd @@ -13,6 +13,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 edit_palette_dialog := $"%EditPaletteDialog" @onready var create_palette_dialog := $"%CreatePaletteDialog" @@ -22,9 +23,20 @@ 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) 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) setup_palettes_selector() redraw_current_palette() @@ -73,9 +85,11 @@ func redraw_current_palette() -> void: Palettes.select_palette(Palettes.current_palette.name) add_color_button.show() delete_color_button.show() + sort_button.show() else: add_color_button.hide() delete_color_button.hide() + sort_button.hide() func toggle_add_delete_buttons() -> void: @@ -85,10 +99,13 @@ func toggle_add_delete_buttons() -> void: 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() if delete_color_button.disabled: delete_color_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN + sort_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN else: delete_color_button.mouse_default_cursor_shape = CURSOR_POINTING_HAND + sort_button.mouse_default_cursor_shape = CURSOR_POINTING_HAND func _on_AddPalette_pressed() -> void: @@ -134,6 +151,11 @@ func _on_DeleteColor_gui_input(event: InputEvent) -> void: toggle_add_delete_buttons() +func sort_pressed(id: Palettes.SortOptions) -> void: + Palettes.current_palette_sort_colors(id) + redraw_current_palette() + + func _on_CreatePaletteDialog_saved( preset: int, palette_name: String, diff --git a/src/Palette/PalettePanel.tscn b/src/Palette/PalettePanel.tscn index b3d932d31..dbe0058f4 100644 --- a/src/Palette/PalettePanel.tscn +++ b/src/Palette/PalettePanel.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=14 format=3 uid="uid://wplk62pbgih4"] +[gd_scene load_steps=15 format=3 uid="uid://wplk62pbgih4"] [ext_resource type="PackedScene" uid="uid://c0p84w7umxwt1" path="res://src/Palette/EditPaletteDialog.tscn" id="1"] [ext_resource type="Texture2D" uid="uid://biyn51upnlxle" path="res://assets/graphics/palette/edit.png" id="2"] @@ -6,6 +6,7 @@ [ext_resource type="Texture2D" uid="uid://b7ydn1tt37rcl" path="res://assets/graphics/palette/add.png" id="3"] [ext_resource type="Texture2D" uid="uid://bt72662c3gp2f" path="res://assets/graphics/timeline/remove_frame.png" id="3_0e27e"] [ext_resource type="PackedScene" uid="uid://68aakj2l6ee1" path="res://src/Palette/CreatePaletteDialog.tscn" id="4"] +[ext_resource type="Texture2D" uid="uid://db4i3w3yencxg" path="res://assets/graphics/palette/sort.png" id="4_hcsuy"] [ext_resource type="Script" path="res://src/Palette/PaletteGrid.gd" id="5"] [ext_resource type="Script" path="res://src/Palette/PaletteScroll.gd" id="6"] [ext_resource type="Script" path="res://src/Palette/PalettePanel.gd" id="8"] @@ -81,6 +82,31 @@ size_flags_horizontal = 0 size_flags_vertical = 0 texture = ExtResource("3_0e27e") +[node name="Sort" type="MenuButton" parent="PaletteVBoxContainer/PaletteButtons"] +unique_name_in_owner = true +custom_minimum_size = Vector2(24, 0) +layout_mode = 2 +tooltip_text = "Sort palette" +mouse_default_cursor_shape = 2 +flat = false + +[node name="TextureRect" type="TextureRect" parent="PaletteVBoxContainer/PaletteButtons/Sort"] +layout_mode = 1 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -6.0 +offset_top = -6.0 +offset_right = 6.0 +offset_bottom = 6.0 +grow_horizontal = 2 +grow_vertical = 2 +size_flags_horizontal = 0 +size_flags_vertical = 0 +texture = ExtResource("4_hcsuy") + [node name="PaletteSelect" type="OptionButton" parent="PaletteVBoxContainer/PaletteButtons"] unique_name_in_owner = true custom_minimum_size = Vector2(100, 0)