From 8b1367494d8d082c2813b38e73118c469a2f8274 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sat, 23 Nov 2024 17:54:28 +0200 Subject: [PATCH] Ensure that the swatches get deleted when the user removes all palettes --- src/Autoload/Palettes.gd | 6 ++++-- src/Palette/PaletteGrid.gd | 15 +++++++-------- src/Palette/PalettePanel.gd | 18 ++++++++++-------- src/Palette/PaletteScroll.gd | 31 ++++++++++++++++++------------- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/Autoload/Palettes.gd b/src/Autoload/Palettes.gd index 4aa1c8588..eef4abab6 100644 --- a/src/Autoload/Palettes.gd +++ b/src/Autoload/Palettes.gd @@ -36,9 +36,10 @@ func does_palette_exist(palette_name: String) -> bool: func select_palette(palette_name: String) -> void: - current_palette = palettes.get(palette_name) + current_palette = palettes.get(palette_name, null) _clear_selected_colors() - Global.config_cache.set_value("data", "last_palette", current_palette.name) + if is_instance_valid(current_palette): + Global.config_cache.set_value("data", "last_palette", current_palette.name) palette_selected.emit(palette_name) @@ -224,6 +225,7 @@ func current_palete_delete(permanent := true) -> void: select_palette(palettes.keys()[0]) else: current_palette = null + select_palette("") func current_palette_add_color(mouse_button: int, start_index := 0) -> void: diff --git a/src/Palette/PaletteGrid.gd b/src/Palette/PaletteGrid.gd index ffc80527e..66bb92c46 100644 --- a/src/Palette/PaletteGrid.gd +++ b/src/Palette/PaletteGrid.gd @@ -23,10 +23,6 @@ func _ready() -> void: 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 @@ -117,6 +113,8 @@ func find_and_select_color(target_color: Color, mouse_button: int) -> void: ## Displays a left/right highlight over a swatch func select_swatch(mouse_button: int, palette_index: int, old_palette_index: int) -> void: + if not is_instance_valid(current_palette): + return var index := convert_palette_index_to_grid_index(palette_index) var old_index := convert_palette_index_to_grid_index(old_palette_index) if index >= 0 and index < swatches.size(): @@ -161,16 +159,17 @@ func convert_palette_index_to_grid_index(palette_index: int) -> int: func resize_grid(new_rect_size: Vector2) -> void: - if not is_instance_valid(current_palette): - return var grid_x: int = ( new_rect_size.x / (swatch_size.x + get("theme_override_constants/h_separation")) ) var grid_y: int = ( new_rect_size.y / (swatch_size.y + get("theme_override_constants/v_separation")) ) - grid_size.x = mini(grid_x, current_palette.width) - grid_size.y = mini(grid_y, current_palette.height) + if is_instance_valid(current_palette): + grid_size.x = mini(grid_x, current_palette.width) + grid_size.y = mini(grid_y, current_palette.height) + else: + grid_size = Vector2i.ZERO setup_swatches() draw_palette() diff --git a/src/Palette/PalettePanel.gd b/src/Palette/PalettePanel.gd index 1c059f116..647cb235e 100644 --- a/src/Palette/PalettePanel.gd +++ b/src/Palette/PalettePanel.gd @@ -89,16 +89,16 @@ func select_palette(palette_name: String) -> void: var palette_id = palettes_path_id.get(palette_name) if palette_id != null: palette_select.selected = palette_id - palette_grid.set_palette(Palettes.current_palette) - palette_scroll.resize_grid() - palette_scroll.set_sliders(Palettes.current_palette, palette_grid.grid_window_origin) + palette_grid.set_palette(Palettes.current_palette) + palette_scroll.resize_grid() + palette_scroll.set_sliders(Palettes.current_palette, palette_grid.grid_window_origin) - var left_selected := Palettes.current_palette_get_selected_color_index(MOUSE_BUTTON_LEFT) - var right_selected := Palettes.current_palette_get_selected_color_index(MOUSE_BUTTON_RIGHT) - palette_grid.select_swatch(MOUSE_BUTTON_LEFT, left_selected, left_selected) - palette_grid.select_swatch(MOUSE_BUTTON_RIGHT, right_selected, right_selected) + var left_selected := Palettes.current_palette_get_selected_color_index(MOUSE_BUTTON_LEFT) + var right_selected := Palettes.current_palette_get_selected_color_index(MOUSE_BUTTON_RIGHT) + palette_grid.select_swatch(MOUSE_BUTTON_LEFT, left_selected, left_selected) + palette_grid.select_swatch(MOUSE_BUTTON_RIGHT, right_selected, right_selected) - toggle_add_delete_buttons() + toggle_add_delete_buttons() ## Select and display current palette @@ -115,6 +115,8 @@ func redraw_current_palette() -> void: func toggle_add_delete_buttons() -> void: + if not is_instance_valid(Palettes.current_palette): + return add_color_button.disabled = Palettes.current_palette.is_full() if add_color_button.disabled: add_color_button.mouse_default_cursor_shape = CURSOR_FORBIDDEN diff --git a/src/Palette/PaletteScroll.gd b/src/Palette/PaletteScroll.gd index c3c2156a0..c06232e7d 100644 --- a/src/Palette/PaletteScroll.gd +++ b/src/Palette/PaletteScroll.gd @@ -4,9 +4,9 @@ var scroll := Vector2i.ZERO var drag_started := false var drag_start_position := Vector2i.ZERO -@onready var h_slider := %HScrollBar -@onready var v_slider := %VScrollBar -@onready var palette_grid := %PaletteGrid +@onready var h_slider := %HScrollBar as HScrollBar +@onready var v_slider := %VScrollBar as VScrollBar +@onready var palette_grid := %PaletteGrid as PaletteGrid func _input(event: InputEvent) -> void: @@ -17,16 +17,21 @@ func _input(event: InputEvent) -> void: func set_sliders(palette: Palette, origin: Vector2i) -> void: - if not is_instance_valid(palette): - return - h_slider.value = origin.x - h_slider.max_value = palette.width - h_slider.page = palette_grid.grid_size.x + if is_instance_valid(palette): + h_slider.value = origin.x + h_slider.max_value = palette.width + h_slider.page = palette_grid.grid_size.x + v_slider.value = origin.y + v_slider.max_value = palette.height + v_slider.page = palette_grid.grid_size.y + else: + h_slider.value = 0 + h_slider.max_value = 0 + h_slider.page = 0 + v_slider.value = 0 + v_slider.max_value = 0 + v_slider.page = 0 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 - v_slider.page = palette_grid.grid_size.y v_slider.visible = false if v_slider.max_value <= palette_grid.grid_size.y else true @@ -58,7 +63,7 @@ func _on_PaletteGrid_gui_input(event: InputEvent) -> void: drag_started = true # Keeps position where the dragging started drag_start_position = ( - event.position + Vector2i(h_slider.value, v_slider.value) * palette_grid.swatch_size + event.position + Vector2(h_slider.value, v_slider.value) * palette_grid.swatch_size ) if event is InputEventMouseMotion and drag_started: