TableTower/ui/search_panel.gd
RobinFiredrake 7dca1f2c3b Big-board navigation: search, focus_table, minimap, pack, lazy cards
- Search (top bar button and Ctrl+F): a card-styled pop-over with live
  results from names, result text, details, prompts, variables and group
  names (BBCode stripped, case and accents ignored, exact names first).
  Up/Down, Enter or click jump to a result, Esc closes.
- Main.focus_table(table_id, entry_id): opens a collapsed group (toast),
  expands the card for an entry, glides to it at a readable zoom, scrolls
  the long list and flashes rings around the card and the row.
- Minimap (Map toggle beside Fit, M key): tinted blocks, lit cards in
  brass, the view rect; click or drag to pan. Off by default in Play, on in
  Build, remembered per mode. Redraws only on change.
- Zoom goes down to 0.08 and Fit uses it; far out, cards draw as plain
  cards and group names show large.
- Build > Arrange > Pack collapsed groups closes the gaps collapsed blocks
  leave, in reading order, with undo. Expanding a group whose frame would
  cover its neighbours pushes them right/down to clear it, with undo.
- Performance: cards in collapsed groups aren't built until expanded; the
  pipe, port and card layers redraw only when the board's geometry
  changes; unlit pipes go in per-pipe canvas items so off-screen ones are
  culled; off-screen cards aren't rendered. 792-card board: open 5.0 s ->
  0.38 s, idle frame 36 ms -> 0.7 ms (all groups expanded: 313 ms -> 9 ms).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 18:16:32 -06:00

284 lines
9.2 KiB
GDScript

class_name SearchPanel
extends PanelContainer
## The Search pop-over under the top bar's Search button (or Ctrl+F): a text
## field and a live list of matches (BoardSearch). Up/Down pick a result,
## Enter or a click jumps to it (Main.focus_table / focus_group), Esc or a
## click elsewhere closes it.
const WIDTH := 460.0
const LIST_HEIGHT := 400.0
const SHOWN := 40
signal closed
var main: Main
var field: LineEdit
## The matches shown, best first (BoardSearch results).
var results: Array[Dictionary] = []
var selected := -1
var _search: BoardSearch
var _search_rev := -1
var _anchor: Control
var _scroll: ScrollContainer
var _list: VBoxContainer
var _note: Label
var _rows: Array[PanelContainer] = []
var _row_normal: StyleBoxFlat
var _row_hover: StyleBoxFlat
var _row_selected: StyleBoxFlat
func setup(p_main: Main) -> void:
main = p_main
visible = false
mouse_filter = MOUSE_FILTER_STOP
top_level = true
theme = Style.card_theme()
var sb := Style.box(Style.CARD, 10, [12, 12, 12, 10])
sb.shadow_color = Color(0, 0, 0, 0.4)
sb.shadow_size = 18
sb.shadow_offset = Vector2(0, 8)
add_theme_stylebox_override("panel", sb)
custom_minimum_size = Vector2(WIDTH, 0)
_row_normal = Style.box(Color(0, 0, 0, 0), 7, [10, 6, 10, 7])
_row_hover = Style.box(Style.HOVER_ROW, 7, [10, 6, 10, 7])
_row_selected = Style.bordered(Style.box(Style.LIT, 7, [10, 6, 10, 7]), 1, Color(Style.BRASS_DARK, 0.35))
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 8)
add_child(col)
field = LineEdit.new()
field.placeholder_text = "Search names, results, prompts, variables, groups…"
field.add_theme_font_override("font", Style.body())
field.add_theme_font_size_override("font_size", 16)
field.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 7, [10, 6, 10, 6]), 1, Style.RULE))
field.add_theme_stylebox_override("focus", Style.bordered(Style.box(Color.WHITE, 7, [10, 6, 10, 6]), 2, Style.COPPER))
field.clear_button_enabled = true
field.text_changed.connect(func(_t: String) -> void: refresh())
field.gui_input.connect(_on_field_input)
col.add_child(field)
_note = Style.make_label("", Style.body_italic(), 14, Style.MUTED)
_note.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
col.add_child(_note)
_scroll = ScrollContainer.new()
_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
col.add_child(_scroll)
_list = VBoxContainer.new()
_list.add_theme_constant_override("separation", 2)
_list.size_flags_horizontal = SIZE_EXPAND_FILL
_scroll.add_child(_list)
## Opens under `anchor` (the Search button) with the field focused.
func open(anchor: Control = null) -> void:
_anchor = anchor
visible = true
_place()
field.grab_focus()
field.select_all()
refresh()
func close() -> void:
if not visible:
return
visible = false
if field.has_focus():
field.release_focus()
closed.emit()
func _place() -> void:
reset_size()
var vp := get_viewport_rect().size
var x := vp.x - size.x - 12.0
var y := 56.0
if is_instance_valid(_anchor) and _anchor.is_inside_tree():
var r := _anchor.get_global_rect()
x = r.end.x - size.x
y = r.end.y + 8.0
position = Vector2(clampf(x, 8.0, maxf(8.0, vp.x - size.x - 8.0)), y)
## The index, rebuilt when the board was edited since it was made.
func index() -> BoardSearch:
if _search == null or _search_rev != main.edit_rev:
_search = BoardSearch.new(main.data)
_search_rev = main.edit_rev
return _search
## Re-runs the query and rebuilds the list.
func refresh() -> void:
for c in _list.get_children():
_list.remove_child(c)
c.queue_free()
_rows.clear()
var q := field.text.strip_edges()
results = index().find(q, SHOWN + 1) if q != "" else ([] as Array[Dictionary])
var more := results.size() > SHOWN
if more:
results.resize(SHOWN)
for i in results.size():
_rows.append(_make_row(results[i], i))
_list.add_child(_rows[i])
if q == "":
_note.text = "Type to find a card by its name, results, details, prompt, variables or group."
elif results.is_empty():
_note.text = "Nothing matches “%s”." % q
elif more:
_note.text = "The first %d matches. Keep typing to narrow them down." % SHOWN
else:
_note.text = "%d %s · ↑↓ to pick, Enter to jump" % [results.size(), "match" if results.size() == 1 else "matches"]
_scroll.custom_minimum_size.y = 0.0 if results.is_empty() else minf(LIST_HEIGHT, results.size() * 62.0)
selected = 0 if not results.is_empty() else -1
_update_selected()
_place.call_deferred()
func _make_row(r: Dictionary, i: int) -> PanelContainer:
var row := PanelContainer.new()
row.add_theme_stylebox_override("panel", _row_normal)
row.mouse_filter = MOUSE_FILTER_STOP
row.mouse_default_cursor_shape = CURSOR_POINTING_HAND
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", 1)
v.mouse_filter = MOUSE_FILTER_IGNORE
row.add_child(v)
var top := HBoxContainer.new()
top.add_theme_constant_override("separation", 8)
top.mouse_filter = MOUSE_FILTER_IGNORE
v.add_child(top)
var name_field: bool = r["field"] == BoardSearch.FIELD_NAME or r["field"] == BoardSearch.FIELD_GROUP
var title := _rich(highlight(r["snippet"], r["hl_from"], r["hl_len"]) if name_field else _esc(str(r["name"])),
Style.body_bold(), 16)
title.size_flags_horizontal = SIZE_EXPAND_FILL
top.add_child(title)
var kind := Style.make_label(str(r["kind"]).to_upper(), Style.caps(), 10, Style.COPPER_DARK)
kind.size_flags_vertical = SIZE_SHRINK_CENTER
kind.mouse_filter = MOUSE_FILTER_IGNORE
top.add_child(kind)
var where := PackedStringArray()
if str(r["group"]) != "":
where.append("in " + str(r["group"]))
if not name_field:
where.append(_field_label(str(r["field"])))
if not where.is_empty():
var sub := Style.make_label(" · ".join(where), Style.body(), 13, Style.MUTED)
sub.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
sub.custom_minimum_size = Vector2(40, 0)
sub.mouse_filter = MOUSE_FILTER_IGNORE
v.add_child(sub)
if not name_field:
var snip := _rich(highlight(r["snippet"], r["hl_from"], r["hl_len"]), Style.body(), 14)
v.add_child(snip)
row.mouse_entered.connect(func() -> void:
if selected != i:
row.add_theme_stylebox_override("panel", _row_hover))
row.mouse_exited.connect(func() -> void: _update_selected())
row.gui_input.connect(func(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.pressed and mb.button_index == MOUSE_BUTTON_LEFT:
selected = i
activate()
row.accept_event())
return row
static func _field_label(field: String) -> String:
match field:
BoardSearch.FIELD_RESULT: return "result"
BoardSearch.FIELD_DETAILS: return "details"
BoardSearch.FIELD_PROMPT: return "prompt"
BoardSearch.FIELD_VARIABLE: return "variable"
BoardSearch.FIELD_EFFECTS: return "effects"
return field
func _rich(bb: String, font: Font, fs: int) -> RichTextLabel:
var l := RichTextLabel.new()
l.bbcode_enabled = true
l.fit_content = true
l.scroll_active = false
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
l.mouse_filter = MOUSE_FILTER_IGNORE
l.add_theme_font_override("normal_font", font)
l.add_theme_font_override("bold_font", Style.body_bold())
l.add_theme_font_size_override("normal_font_size", fs)
l.add_theme_font_size_override("bold_font_size", fs)
l.add_theme_color_override("default_color", Style.INK)
l.custom_minimum_size = Vector2(40, 0)
l.size_flags_horizontal = SIZE_EXPAND_FILL
l.text = bb
return l
static func _esc(s: String) -> String:
return s.replace("[", "[lb]")
## BBCode for plain `text` with [from, from + length) marked in brass.
static func highlight(text: String, from: int, length: int) -> String:
if from < 0 or length <= 0 or from >= text.length():
return _esc(text)
return "%s[bgcolor=#%s][b]%s[/b][/bgcolor]%s" % [_esc(text.substr(0, from)), Style.BRASS.to_html(false),
_esc(text.substr(from, length)), _esc(text.substr(from + length))]
func _update_selected() -> void:
for i in _rows.size():
_rows[i].add_theme_stylebox_override("panel", _row_selected if i == selected else _row_normal)
func move_selection(step: int) -> void:
if results.is_empty():
return
selected = wrapi(selected + step, 0, results.size())
_update_selected()
if selected < _rows.size():
_scroll.ensure_control_visible(_rows[selected])
## Jumps to the selected result and closes.
func activate() -> void:
if selected < 0 or selected >= results.size():
return
var r := results[selected]
close()
if str(r["group_id"]) != "":
main.focus_group(str(r["group_id"]))
else:
main.focus_table(str(r["table_id"]), str(r["entry_id"]))
func _on_field_input(ev: InputEvent) -> void:
var k := ev as InputEventKey
if k == null or not k.pressed:
return
match k.keycode:
KEY_DOWN:
move_selection(1)
KEY_UP:
move_selection(-1)
KEY_ENTER, KEY_KP_ENTER:
activate()
KEY_ESCAPE:
close()
_:
return
field.accept_event()
func _input(ev: InputEvent) -> void:
if not visible:
return
var mb := ev as InputEventMouseButton
if mb and mb.pressed and not get_global_rect().has_point(mb.global_position):
# A click elsewhere closes it (the Search button toggles it itself).
if not (is_instance_valid(_anchor) and _anchor.get_global_rect().has_point(mb.global_position)):
close()
var k := ev as InputEventKey
if k and k.pressed and k.keycode == KEY_ESCAPE:
close()
get_viewport().set_input_as_handled()