From 5842ce00c6e7bcb6473b47dd509a2692aa2bd2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Nov=C3=A1k?= <42614907+novhack@users.noreply.github.com> Date: Tue, 4 Oct 2022 12:47:31 +0200 Subject: [PATCH] Add reactivity to palette grid, improve scrolling and add swatch "zoom" (#761) * Save changed color to palette file only when color dialog is closed * Restructure palette panel to use unique names * Make grid size reactive to palette panel size * Add mouse wheel scroll support to palette grid * Add palette swatch resizing * Reorganize palette grid rendering * Store swatch size in cache config * Cleanup formatting --- src/Palette/PaletteGrid.gd | 152 +++++++++++++++++---------------- src/Palette/PalettePanel.gd | 30 ++++--- src/Palette/PalettePanel.tscn | 120 +++++++++++++++----------- src/Palette/PaletteScroll.gd | 64 ++++++++++---- src/Palette/PaletteSwatch.gd | 19 ++--- src/Palette/PaletteSwatch.tscn | 1 + 6 files changed, 226 insertions(+), 160 deletions(-) diff --git a/src/Palette/PaletteGrid.gd b/src/Palette/PaletteGrid.gd index 67976337b..e5cca13a1 100644 --- a/src/Palette/PaletteGrid.gd +++ b/src/Palette/PaletteGrid.gd @@ -6,69 +6,63 @@ signal swatch_double_clicked(mouse_button, index, position) signal swatch_dropped(source_index, target_index) const PaletteSwatchScene := preload("res://src/Palette/PaletteSwatch.tscn") - -# Must be integer values -const MAX_GRID_SIZE = Vector2(8, 8) +const DEFAULT_SWATCH_SIZE = Vector2(26, 26) +const MIN_SWATCH_SIZE = Vector2(8, 8) +const MAX_SWATCH_SIZE = Vector2(64, 64) var swatches := [] # PaletteSwatch - -var displayed_palette = null +var current_palette = null var grid_window_origin := Vector2.ZERO -var grid_size := Vector2(8, 8) +var grid_size := Vector2.ZERO +var swatch_size := DEFAULT_SWATCH_SIZE -func _ready(): - init_swatches() +func _ready() -> void: + swatch_size = Global.config_cache.get_value("palettes", "swatch_size", DEFAULT_SWATCH_SIZE) -func init_swatches() -> void: - columns = grid_size.x - for j in range(grid_size.y): - for i in range(grid_size.x): - var index: int = i + grid_size.x * j +func set_palette(new_palette: Palette) -> void: + # Only display valid palette objects + if not new_palette: + return + + current_palette = new_palette + grid_window_origin = Vector2.ZERO + + +func setup_swatches() -> void: + # Colums cannot be 0 + columns = 1.0 if grid_size.x == 0.0 else grid_size.x + if grid_size.x * grid_size.y > swatches.size(): + for i in range(swatches.size(), grid_size.x * grid_size.y): var swatch: PaletteSwatch = PaletteSwatchScene.instance() - swatch.index = index - swatch.color = PaletteSwatch.DEFAULT_COLOR - swatch.show_left_highlight = false - swatch.show_right_highlight = false - swatch.empty = true - swatch.connect("pressed", self, "_on_PaletteSwatch_pressed", [index]) - swatch.connect("double_clicked", self, "_on_PaletteSwatch_double_clicked", [index]) + swatch.index = i + init_swatch(swatch) + swatch.connect("pressed", self, "_on_PaletteSwatch_pressed", [i]) + swatch.connect("double_clicked", self, "_on_PaletteSwatch_double_clicked", [i]) swatch.connect("dropped", self, "_on_PaletteSwatch_dropped") add_child(swatch) swatches.push_back(swatch) + else: + var diff = swatches.size() - grid_size.x * grid_size.y + for _i in range(0, diff): + var swatch = swatches.pop_back() + remove_child(swatch) + swatch.queue_free() + + for i in range(0, swatches.size()): + init_swatch(swatches[i]) -# Origin determines a position in palette which will be displayed on top left of grid -func display_palette(palette: Palette) -> void: - # Reset grid origin when palette changes - if displayed_palette != palette: - displayed_palette = palette - grid_window_origin = Vector2.ZERO +func init_swatch(swatch: PaletteSwatch) -> void: + swatch.color = PaletteSwatch.DEFAULT_COLOR + swatch.show_left_highlight = false + swatch.show_right_highlight = false + swatch.empty = true + swatch.set_swatch_size(swatch_size) - # Only display valid palette objects - if not palette: - return - if swatches.size() == 0: - init_swatches() - - if palette.width < MAX_GRID_SIZE.x or palette.height < MAX_GRID_SIZE.y: - grid_size = Vector2( - min(palette.width, MAX_GRID_SIZE.x), min(palette.height, MAX_GRID_SIZE.y) - ) - clear_swatches() - init_swatches() - elif ( - palette.width >= MAX_GRID_SIZE.x - and palette.height >= MAX_GRID_SIZE.y - and grid_size != MAX_GRID_SIZE - ): - grid_size = MAX_GRID_SIZE - clear_swatches() - init_swatches() - - # Create empty palette buttons +func draw_palette() -> void: for j in range(grid_size.y): for i in range(grid_size.x): var grid_index: int = i + grid_size.x * j @@ -76,7 +70,7 @@ func display_palette(palette: Palette) -> void: var swatch = swatches[grid_index] swatch.show_left_highlight = false swatch.show_right_highlight = false - var color = palette.get_color(index) + var color = current_palette.get_color(index) if color != null: swatch.color = color swatch.empty = false @@ -87,15 +81,7 @@ func display_palette(palette: Palette) -> void: func scroll_palette(origin: Vector2) -> void: grid_window_origin = origin - display_palette(displayed_palette) - - -# Removes all swatches -func clear_swatches() -> void: - swatches.clear() - for swatch in get_children(): - remove_child(swatch) # To remove the child immediately and not at the end of the frame - swatch.queue_free() + draw_palette() func find_and_select_color(mouse_button: int, target_color: Color) -> void: @@ -149,6 +135,42 @@ func reset_empty_swatches_color() -> void: swatch.empty = true +# Grid index adds grid window origin +func convert_grid_index_to_palette_index(index: int) -> int: + return ( + int(index / grid_size.x + grid_window_origin.y) * current_palette.width + + (index % int(grid_size.x) + grid_window_origin.x) + ) + + +func convert_palette_index_to_grid_index(palette_index: int) -> int: + var x: int = palette_index % current_palette.width + var y: int = palette_index / current_palette.width + return int((x - grid_window_origin.x) + (y - grid_window_origin.y) * grid_size.x) + + +func resize_grid(new_rect_size: Vector2) -> void: + var grid_x: int = new_rect_size.x / (swatch_size.x + get("custom_constants/hseparation")) + var grid_y: int = new_rect_size.y / (swatch_size.y + get("custom_constants/vseparation")) + grid_size.x = min(grid_x, current_palette.width) + grid_size.y = min(grid_y, current_palette.height) + setup_swatches() + draw_palette() + + +func change_swatch_size(size_diff: Vector2) -> void: + swatch_size += size_diff + if swatch_size.x < MIN_SWATCH_SIZE.x: + swatch_size = MIN_SWATCH_SIZE + elif swatch_size.x > MAX_SWATCH_SIZE.x: + swatch_size = MAX_SWATCH_SIZE + + for swatch in swatches: + swatch.set_swatch_size(swatch_size) + + Global.config_cache.set_value("palettes", "swatch_size", swatch_size) + + func _on_PaletteSwatch_pressed(mouse_button: int, index: int) -> void: var palette_index = convert_grid_index_to_palette_index(index) emit_signal("swatch_pressed", mouse_button, palette_index) @@ -163,17 +185,3 @@ func _on_PaletteSwatch_dropped(source_index: int, target_index: int) -> void: var palette_source_index = convert_grid_index_to_palette_index(source_index) var palette_target_index = convert_grid_index_to_palette_index(target_index) emit_signal("swatch_dropped", palette_source_index, palette_target_index) - - -# Grid index adds grid window origin -func convert_grid_index_to_palette_index(index: int) -> int: - return ( - int(index / grid_size.x + grid_window_origin.y) * displayed_palette.width - + (index % int(grid_size.x) + grid_window_origin.x) - ) - - -func convert_palette_index_to_grid_index(palette_index: int) -> int: - var x: int = palette_index % displayed_palette.width - var y: int = palette_index / displayed_palette.width - return int((x - grid_window_origin.x) + (y - grid_window_origin.y) * grid_size.x) diff --git a/src/Palette/PalettePanel.gd b/src/Palette/PalettePanel.gd index caeacacde..56f8c5d6c 100644 --- a/src/Palette/PalettePanel.gd +++ b/src/Palette/PalettePanel.gd @@ -4,21 +4,21 @@ extends PanelContainer var palettes_path_id := {} var palettes_id_path := {} -var edited_swatch_index = -1 +var edited_swatch_index := -1 +var edited_swatch_color := Color.transparent -onready var palette_select := $PaletteVBoxContainer/PaletteButtons/PaletteSelect -onready var add_palette_button := $PaletteVBoxContainer/PaletteButtons/AddPalette -onready var palette_grid := find_node("PaletteGrid") -onready var palette_scroll := $PaletteVBoxContainer/SwatchesContainer/PaletteScroll +onready var palette_select := $"%PaletteSelect" +onready var palette_grid := $"%PaletteGrid" +onready var palette_scroll := $"%PaletteScroll" -onready var add_color_button := $PaletteVBoxContainer/SwatchesContainer/ColorButtons/AddColor -onready var delete_color_button := $PaletteVBoxContainer/SwatchesContainer/ColorButtons/DeleteColor +onready var add_color_button := $"%AddColor" +onready var delete_color_button := $"%DeleteColor" -onready var edit_palette_dialog := $EditPaletteDialog -onready var create_palette_dialog := $CreatePaletteDialog +onready var edit_palette_dialog := $"%EditPaletteDialog" +onready var create_palette_dialog := $"%CreatePaletteDialog" # Color picker button itself is hidden but it's popup is used to edit color swatches -onready var hidden_color_picker := $HiddenColorPickerButton +onready var hidden_color_picker := $"%HiddenColorPickerButton" func _ready() -> void: @@ -54,7 +54,8 @@ func select_palette(palette_path: String) -> void: if palette_id != null: palette_select.selected = palette_id Palettes.select_palette(palette_path) - palette_grid.display_palette(Palettes.get_current_palette()) + palette_grid.set_palette(Palettes.get_current_palette()) + palette_scroll.resize_grid() palette_scroll.set_sliders(Palettes.get_current_palette(), palette_grid.grid_window_origin) var left_selected = Palettes.current_palette_get_selected_color_index(BUTTON_LEFT) @@ -188,7 +189,7 @@ func _on_PaletteGrid_swatch_pressed(mouse_button: int, index: int) -> void: func _on_ColorPicker_color_changed(color: Color) -> void: if edited_swatch_index != -1: - Palettes.current_palette_set_color(edited_swatch_index, color) + edited_swatch_color = color palette_grid.set_swatch_color(edited_swatch_index, color) if edited_swatch_index == Palettes.current_palette_get_selected_color_index(BUTTON_LEFT): @@ -197,6 +198,11 @@ func _on_ColorPicker_color_changed(color: Color) -> void: Tools.assign_color(color, BUTTON_RIGHT) +func _on_HiddenColorPickerButton_popup_closed(): + # Saves edited swatch to palette file when color selection dialog is closed + Palettes.current_palette_set_color(edited_swatch_index, edited_swatch_color) + + func _on_EditPaletteDialog_deleted() -> void: Palettes.current_palete_delete() setup_palettes_selector() diff --git a/src/Palette/PalettePanel.tscn b/src/Palette/PalettePanel.tscn index 7c98b3495..8e8bed3b5 100644 --- a/src/Palette/PalettePanel.tscn +++ b/src/Palette/PalettePanel.tscn @@ -9,26 +9,24 @@ [ext_resource path="res://src/Palette/PalettePanel.gd" type="Script" id=8] [node name="PalettePanel" type="PanelContainer"] -margin_right = 318.0 -margin_bottom = 118.0 -rect_min_size = Vector2( 318, 0 ) size_flags_horizontal = 3 -size_flags_vertical = 0 +size_flags_vertical = 3 script = ExtResource( 8 ) [node name="PaletteVBoxContainer" type="VBoxContainer" parent="."] margin_left = 7.0 margin_top = 7.0 -margin_right = 311.0 -margin_bottom = 111.0 +margin_right = 159.0 +margin_bottom = 93.0 size_flags_horizontal = 3 [node name="PaletteButtons" type="HBoxContainer" parent="PaletteVBoxContainer"] -margin_right = 304.0 +margin_right = 152.0 margin_bottom = 22.0 [node name="PaletteSelect" type="OptionButton" parent="PaletteVBoxContainer/PaletteButtons"] -margin_right = 252.0 +unique_name_in_owner = true +margin_right = 100.0 margin_bottom = 22.0 grow_horizontal = 0 rect_min_size = Vector2( 100, 0 ) @@ -38,8 +36,8 @@ size_flags_horizontal = 3 clip_text = true [node name="EditPalette" type="Button" parent="PaletteVBoxContainer/PaletteButtons" groups=["UIButtons"]] -margin_left = 256.0 -margin_right = 278.0 +margin_left = 104.0 +margin_right = 126.0 margin_bottom = 22.0 rect_min_size = Vector2( 22, 22 ) hint_tooltip = "Edit currently selected palette" @@ -60,13 +58,10 @@ margin_bottom = 11.0 size_flags_horizontal = 0 size_flags_vertical = 0 texture = ExtResource( 2 ) -__meta__ = { -"_edit_use_anchors_": false -} [node name="AddPalette" type="Button" parent="PaletteVBoxContainer/PaletteButtons" groups=["UIButtons"]] -margin_left = 282.0 -margin_right = 304.0 +margin_left = 130.0 +margin_right = 152.0 margin_bottom = 22.0 rect_min_size = Vector2( 22, 22 ) hint_tooltip = "Create a new palette" @@ -87,26 +82,25 @@ margin_bottom = 11.0 size_flags_horizontal = 0 size_flags_vertical = 0 texture = ExtResource( 3 ) -__meta__ = { -"_edit_use_anchors_": false -} [node name="HSeparator" type="HSeparator" parent="PaletteVBoxContainer"] margin_top = 26.0 -margin_right = 304.0 +margin_right = 152.0 margin_bottom = 30.0 [node name="SwatchesContainer" type="HBoxContainer" parent="PaletteVBoxContainer"] margin_top = 34.0 -margin_right = 304.0 +margin_right = 152.0 margin_bottom = 86.0 size_flags_horizontal = 3 +size_flags_vertical = 3 [node name="ColorButtons" type="VBoxContainer" parent="PaletteVBoxContainer/SwatchesContainer"] margin_right = 24.0 margin_bottom = 52.0 [node name="AddColor" type="Button" parent="PaletteVBoxContainer/SwatchesContainer/ColorButtons"] +unique_name_in_owner = true margin_right = 24.0 margin_bottom = 24.0 rect_min_size = Vector2( 24, 24 ) @@ -115,6 +109,7 @@ mouse_default_cursor_shape = 2 text = "+" [node name="DeleteColor" type="Button" parent="PaletteVBoxContainer/SwatchesContainer/ColorButtons"] +unique_name_in_owner = true margin_top = 28.0 margin_right = 24.0 margin_bottom = 52.0 @@ -124,54 +119,72 @@ mouse_default_cursor_shape = 2 text = "-" [node name="PaletteScroll" type="VBoxContainer" parent="PaletteVBoxContainer/SwatchesContainer"] +unique_name_in_owner = true margin_left = 28.0 -margin_right = 304.0 +margin_right = 152.0 margin_bottom = 52.0 size_flags_horizontal = 3 script = ExtResource( 6 ) [node name="HBoxContainer" type="HBoxContainer" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll"] -margin_right = 276.0 +margin_right = 124.0 +margin_bottom = 52.0 size_flags_horizontal = 3 +size_flags_vertical = 3 +alignment = 1 -[node name="CenterContainer" type="CenterContainer" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer"] -margin_right = 276.0 +[node name="VBoxContainer" type="VBoxContainer" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer"] +margin_right = 120.0 +margin_bottom = 52.0 size_flags_horizontal = 3 +size_flags_vertical = 3 +alignment = 1 -[node name="HBoxContainer" type="HBoxContainer" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/CenterContainer"] -margin_left = 138.0 -margin_right = 138.0 +[node name="ScrollContainer" type="ScrollContainer" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/VBoxContainer"] +unique_name_in_owner = true +margin_right = 120.0 +margin_bottom = 52.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 -[node name="PaletteGrid" type="GridContainer" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/CenterContainer/HBoxContainer"] -columns = 8 +[node name="PaletteGrid" type="GridContainer" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/VBoxContainer/ScrollContainer"] +unique_name_in_owner = true +margin_right = 120.0 +margin_bottom = 52.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 +custom_constants/vseparation = 3 +custom_constants/hseparation = 3 script = ExtResource( 5 ) -[node name="VScrollBar" type="VScrollBar" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/CenterContainer/HBoxContainer"] +[node name="HScrollBar" type="HScrollBar" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/VBoxContainer"] +unique_name_in_owner = true visible = false -margin_left = 4.0 -margin_right = 16.0 -margin_bottom = 12.0 +margin_left = -132.0 +margin_top = 4.0 +margin_right = 118.0 +margin_bottom = 16.0 step = 1.0 page = 8.0 custom_step = 1.0 -[node name="MarginContainer" type="MarginContainer" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll"] -margin_top = 4.0 -margin_right = 276.0 -margin_bottom = 4.0 -custom_constants/margin_right = 20 -custom_constants/margin_left = 6 +[node name="MarginContainer" type="MarginContainer" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer"] +margin_left = 124.0 +margin_right = 124.0 +margin_bottom = 52.0 +custom_constants/margin_bottom = 15 -[node name="HScrollBar" type="HScrollBar" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/MarginContainer"] +[node name="VScrollBar" type="VScrollBar" parent="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/MarginContainer"] +unique_name_in_owner = true visible = false -margin_left = 6.0 -margin_right = 256.0 -margin_bottom = 12.0 +margin_right = 12.0 +margin_bottom = 37.0 step = 1.0 page = 8.0 custom_step = 1.0 [node name="EditPaletteDialog" parent="." instance=ExtResource( 1 )] +unique_name_in_owner = true margin_left = 7.0 margin_top = 7.0 margin_right = 317.0 @@ -179,6 +192,7 @@ margin_bottom = 442.0 rect_min_size = Vector2( 250, 87.5 ) [node name="CreatePaletteDialog" parent="." instance=ExtResource( 4 )] +unique_name_in_owner = true margin_left = 7.0 margin_top = 7.0 margin_right = 317.0 @@ -186,24 +200,28 @@ margin_bottom = 312.0 rect_min_size = Vector2( 250, 87.5 ) [node name="HiddenColorPickerButton" type="ColorPickerButton" parent="."] +unique_name_in_owner = true visible = false margin_left = 7.0 margin_top = 7.0 -margin_right = 317.0 -margin_bottom = 312.0 +margin_right = 311.0 +margin_bottom = 111.0 [connection signal="item_selected" from="PaletteVBoxContainer/PaletteButtons/PaletteSelect" to="." method="_on_PaletteSelect_item_selected"] [connection signal="pressed" from="PaletteVBoxContainer/PaletteButtons/EditPalette" to="." method="_on_EditPalette_pressed"] [connection signal="pressed" from="PaletteVBoxContainer/PaletteButtons/AddPalette" to="." method="_on_AddPalette_pressed"] [connection signal="gui_input" from="PaletteVBoxContainer/SwatchesContainer/ColorButtons/AddColor" to="." method="_on_AddColor_gui_input"] [connection signal="gui_input" from="PaletteVBoxContainer/SwatchesContainer/ColorButtons/DeleteColor" to="." method="_on_DeleteColor_gui_input"] -[connection signal="gui_input" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/CenterContainer/HBoxContainer/PaletteGrid" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_PaletteGrid_gui_input"] -[connection signal="swatch_double_clicked" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/CenterContainer/HBoxContainer/PaletteGrid" to="." method="_on_PaletteGrid_swatch_double_clicked"] -[connection signal="swatch_dropped" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/CenterContainer/HBoxContainer/PaletteGrid" to="." method="_on_PaletteGrid_swatch_dropped"] -[connection signal="swatch_pressed" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/CenterContainer/HBoxContainer/PaletteGrid" to="." method="_on_PaletteGrid_swatch_pressed"] -[connection signal="value_changed" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/CenterContainer/HBoxContainer/VScrollBar" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_VSlider_value_changed"] -[connection signal="value_changed" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/MarginContainer/HScrollBar" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_HSlider_value_changed"] +[connection signal="gui_input" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_PaletteScroll_gui_input"] +[connection signal="resized" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_PaletteScroll_resized"] +[connection signal="gui_input" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/VBoxContainer/ScrollContainer/PaletteGrid" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_PaletteGrid_gui_input"] +[connection signal="swatch_double_clicked" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/VBoxContainer/ScrollContainer/PaletteGrid" to="." method="_on_PaletteGrid_swatch_double_clicked"] +[connection signal="swatch_dropped" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/VBoxContainer/ScrollContainer/PaletteGrid" to="." method="_on_PaletteGrid_swatch_dropped"] +[connection signal="swatch_pressed" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/VBoxContainer/ScrollContainer/PaletteGrid" to="." method="_on_PaletteGrid_swatch_pressed"] +[connection signal="value_changed" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/VBoxContainer/HScrollBar" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_HSlider_value_changed"] +[connection signal="value_changed" from="PaletteVBoxContainer/SwatchesContainer/PaletteScroll/HBoxContainer/MarginContainer/VScrollBar" to="PaletteVBoxContainer/SwatchesContainer/PaletteScroll" method="_on_VSlider_value_changed"] [connection signal="deleted" from="EditPaletteDialog" to="." method="_on_EditPaletteDialog_deleted"] [connection signal="saved" from="EditPaletteDialog" to="." method="_on_EditPaletteDialog_saved"] [connection signal="saved" from="CreatePaletteDialog" to="." method="_on_CreatePaletteDialog_saved"] [connection signal="color_changed" from="HiddenColorPickerButton" to="." method="_on_ColorPicker_color_changed"] +[connection signal="popup_closed" from="HiddenColorPickerButton" to="." method="_on_HiddenColorPickerButton_popup_closed"] diff --git a/src/Palette/PaletteScroll.gd b/src/Palette/PaletteScroll.gd index a41ceeda3..1006a6d97 100644 --- a/src/Palette/PaletteScroll.gd +++ b/src/Palette/PaletteScroll.gd @@ -4,9 +4,16 @@ var scroll := Vector2.ZERO var drag_started := false var drag_start_position := Vector2.ZERO -onready var h_slider := $MarginContainer/HScrollBar -onready var v_slider := $HBoxContainer/CenterContainer/HBoxContainer/VScrollBar -onready var palette_grid := $HBoxContainer/CenterContainer/HBoxContainer/PaletteGrid +onready var h_slider := $"%HScrollBar" +onready var v_slider := $"%VScrollBar" +onready var palette_grid := $"%PaletteGrid" +onready var scroll_container := $"%ScrollContainer" + + +func _ready() -> void: + # Hide default scollbars + scroll_container.get_h_scrollbar().rect_scale = Vector2.ZERO + scroll_container.get_v_scrollbar().rect_scale = Vector2.ZERO func _input(event) -> void: @@ -18,17 +25,22 @@ func _input(event) -> void: func set_sliders(palette: Palette, origin: Vector2) -> void: h_slider.value = origin.x - v_slider.value = origin.y h_slider.max_value = palette.width - if h_slider.max_value <= PaletteGrid.MAX_GRID_SIZE.x: - h_slider.visible = false - else: - h_slider.visible = true + h_slider.page = palette_grid.grid_size.x + h_slider.visible = false if h_slider.max_value <= palette_grid.grid_size.x else true + + v_slider.value = origin.y v_slider.max_value = palette.height - if v_slider.max_value <= PaletteGrid.MAX_GRID_SIZE.y: - v_slider.visible = false - else: - v_slider.visible = true + v_slider.page = palette_grid.grid_size.y + v_slider.visible = false if v_slider.max_value <= palette_grid.grid_size.y else true + + +func reset_sliders() -> void: + set_sliders(palette_grid.current_palette, palette_grid.grid_window_origin) + + +func resize_grid() -> void: + palette_grid.resize_grid(rect_size - Vector2(v_slider.rect_size.x, h_slider.rect_size.y)) func scroll_grid() -> void: @@ -52,9 +64,31 @@ func _on_PaletteGrid_gui_input(event) -> void: # Keeps position where the dragging started drag_start_position = ( event.position - + Vector2(h_slider.value, v_slider.value) * PaletteSwatch.SWATCH_SIZE + + Vector2(h_slider.value, v_slider.value) * palette_grid.swatch_size ) if event is InputEventMouseMotion and drag_started: - h_slider.value = (drag_start_position.x - event.position.x) / PaletteSwatch.SWATCH_SIZE.x - v_slider.value = (drag_start_position.y - event.position.y) / PaletteSwatch.SWATCH_SIZE.y + h_slider.value = (drag_start_position.x - event.position.x) / palette_grid.swatch_size.x + v_slider.value = (drag_start_position.y - event.position.y) / palette_grid.swatch_size.y + + +func _on_PaletteScroll_resized() -> void: + resize_grid() + reset_sliders() + + +func _on_PaletteScroll_gui_input(event) -> void: + if event is InputEventMouseButton and event.pressed: + var scroll_vector = Vector2.ZERO + if event.button_index == BUTTON_WHEEL_UP: + if event.control: + palette_grid.change_swatch_size(Vector2.ONE) + else: + scroll_vector = Vector2.LEFT if event.shift else Vector2.UP + if event.button_index == BUTTON_WHEEL_DOWN: + if event.control: + palette_grid.change_swatch_size(-Vector2.ONE) + else: + scroll_vector = Vector2.RIGHT if event.shift else Vector2.DOWN + resize_grid() + set_sliders(palette_grid.current_palette, palette_grid.grid_window_origin + scroll_vector) diff --git a/src/Palette/PaletteSwatch.gd b/src/Palette/PaletteSwatch.gd index 1b2969015..0742f1595 100644 --- a/src/Palette/PaletteSwatch.gd +++ b/src/Palette/PaletteSwatch.gd @@ -5,7 +5,6 @@ signal pressed(mouse_button) signal double_clicked(mouse_button, position) signal dropped(source_index, new_index) -const SWATCH_SIZE := Vector2(26, 26) # Required by grid sliders const DEFAULT_COLOR := Color(0.0, 0.0, 0.0, 0.0) var index := -1 @@ -14,27 +13,27 @@ var show_right_highlight := false var empty := true setget set_empty -func _ready() -> void: - rect_min_size = SWATCH_SIZE - rect_size = SWATCH_SIZE +func set_swatch_size(size: Vector2) -> void: + rect_min_size = size + rect_size = size func _draw() -> void: if not empty: # Black border around swatches with a color - draw_rect(Rect2(Vector2.ZERO, SWATCH_SIZE), Color.black, false, 1) + draw_rect(Rect2(Vector2.ZERO, rect_size), Color.black, false, 1) if show_left_highlight: # Display outer border highlight - draw_rect(Rect2(Vector2.ZERO, SWATCH_SIZE), Color.white, false, 1) - draw_rect(Rect2(Vector2.ONE, SWATCH_SIZE - Vector2(2, 2)), Color.black, false, 1) + draw_rect(Rect2(Vector2.ZERO, rect_size), Color.white, false, 1) + draw_rect(Rect2(Vector2.ONE, rect_size - Vector2(2, 2)), Color.black, false, 1) if show_right_highlight: # Display inner border highlight - var margin := SWATCH_SIZE / 4 - draw_rect(Rect2(margin, SWATCH_SIZE - margin * 2), Color.black, false, 1) + var margin := rect_size / 4 + draw_rect(Rect2(margin, rect_size - margin * 2), Color.black, false, 1) draw_rect( - Rect2(margin - Vector2.ONE, SWATCH_SIZE - margin * 2 + Vector2(2, 2)), + Rect2(margin - Vector2.ONE, rect_size - margin * 2 + Vector2(2, 2)), Color.white, false, 1 diff --git a/src/Palette/PaletteSwatch.tscn b/src/Palette/PaletteSwatch.tscn index 32009c0fe..f9b922b20 100644 --- a/src/Palette/PaletteSwatch.tscn +++ b/src/Palette/PaletteSwatch.tscn @@ -75,4 +75,5 @@ __meta__ = { "_edit_lock_": true, "_edit_use_anchors_": false } + [connection signal="gui_input" from="." to="." method="_on_PaletteSlot_gui_input"]