Skip to content

Commit e393258

Browse files
authored
Fix some issues in the previews area (#1770)
1 parent 516510d commit e393258

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

src/ui_parts/previews.gd

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const MAX_ICON_PREVIEW_SIZE = 128
2222
@onready var presentation_config_button: Button = $ActionContainer/PresentationConfigButton
2323
@onready var more_button: Button = $ActionContainer/MoreButton
2424

25+
# Computes all layout data for one preview tile.
2526
class IconPreviewTileData extends RefCounted:
2627
var index := -1
2728
var position: Vector2
@@ -55,6 +56,7 @@ class IconPreviewTileData extends RefCounted:
5556
# The position needs to be set when all sizes are known, so only size is set here.
5657
size = Vector2(maxf(preview_size.x, bottom_row_width) + TILE_PADDING * 2, preview_size.y + 18 + TILE_PADDING * 2)
5758

59+
# Wide previews keep the text centered beneath them, while narrow previews are centered over the label row.
5860
if preview_size.x >= bottom_row_width:
5961
preview_rect = Rect2(Vector2(TILE_PADDING, TILE_PADDING), preview_size)
6062
dimensions_label_pos = Vector2(TILE_PADDING + roundf((preview_size.x - full_text_width) / 2.0) + 2,
@@ -74,6 +76,8 @@ var selected_tile_index := -1
7476
var edited_tile_index := -1
7577
var edit_field: NumberEdit
7678

79+
var presentation_config_button_ci := RenderingServer.canvas_item_create()
80+
7781
func _ready() -> void:
7882
icon_preview_tiles.draw.connect(_on_preview_tiles_draw)
7983
icon_preview_tiles.gui_input.connect(_on_tiles_gui_input)
@@ -87,7 +91,6 @@ func _ready() -> void:
8791

8892
add_button.pressed.connect(_add_new_tile)
8993

90-
var presentation_config_button_ci := RenderingServer.canvas_item_create()
9194
RenderingServer.canvas_item_set_parent(presentation_config_button_ci, presentation_config_button.get_canvas_item())
9295
presentation_config_button.pressed.connect(_on_presentation_config_button_pressed)
9396
presentation_config_button.draw.connect(
@@ -113,7 +116,6 @@ func _ready() -> void:
113116
_sync_preview_background()
114117
HandlerGUI.register_focus_sequence(self, [add_button, presentation_config_button, more_button])
115118

116-
117119
func sync_theming() -> void:
118120
preview_top_panel.add_theme_stylebox_override("panel", get_theme_stylebox("tabbar_background", "TabContainer"))
119121
sync_preview_top_panel_expand_margins()
@@ -124,6 +126,10 @@ func sync_theming() -> void:
124126
func sync_localization() -> void:
125127
add_button.text = Translator.translate("Add preview")
126128

129+
func _exit_tree() -> void:
130+
RenderingServer.free_rid(presentation_config_button_ci)
131+
132+
127133
func sync_preview_top_panel_expand_margins() -> void:
128134
var stylebox := preview_top_panel.get_theme_stylebox("panel").duplicate()
129135
if split_container.vertical:
@@ -144,6 +150,7 @@ func sync_tiles() -> void:
144150
sync_tile_positions()
145151
_sync_texture()
146152

153+
# Reflow tiles into centered rows whenever the panel resizes or the tile set changes.
147154
func sync_tile_positions() -> void:
148155
var current_x := TILE_MARGIN
149156
var current_y := TILE_MARGIN
@@ -183,6 +190,7 @@ func sync_tile_positions() -> void:
183190

184191
icon_preview_tiles.custom_minimum_size.y = current_y + row_height + TILE_MARGIN
185192
_sync_buttons()
193+
set_hovered_to_pos(icon_preview_tiles.get_local_mouse_position())
186194
icon_preview_tiles.queue_redraw()
187195

188196

@@ -210,17 +218,7 @@ func _on_preview_tiles_draw() -> void:
210218

211219
func _on_tiles_gui_input(event: InputEvent) -> void:
212220
if event is InputEventMouseMotion:
213-
var old_hovered_index := hovered_tile_index
214-
hovered_tile_index = -1
215-
216-
for tile in tiles:
217-
if Rect2(tile.position, tile.size).has_point(event.position):
218-
hovered_tile_index = tile.index
219-
break
220-
221-
if old_hovered_index != hovered_tile_index:
222-
icon_preview_tiles.queue_redraw()
223-
221+
set_hovered_to_pos(event.position)
224222
elif event is InputEventMouseButton:
225223
if event.is_pressed():
226224
if event.button_index == MOUSE_BUTTON_LEFT:
@@ -238,11 +236,20 @@ func _on_tiles_gui_input(event: InputEvent) -> void:
238236
var vp := get_viewport()
239237
HandlerGUI.popup_under_pos(ContextPopup.create(btn_array), vp.get_mouse_position(), vp)
240238

241-
242239
func _on_tiles_mouse_exited() -> void:
243240
hovered_tile_index = -1
244241
icon_preview_tiles.queue_redraw()
245242

243+
func set_hovered_to_pos(pos: Vector2) -> void:
244+
var old_hovered_index := hovered_tile_index
245+
hovered_tile_index = -1
246+
for tile in tiles:
247+
if Rect2(tile.position, tile.size).has_point(pos):
248+
hovered_tile_index = tile.index
249+
break
250+
if old_hovered_index != hovered_tile_index:
251+
icon_preview_tiles.queue_redraw()
252+
246253
func _select_tile(tile_index: int) -> void:
247254
if tile_index == selected_tile_index:
248255
return

0 commit comments

Comments
 (0)