TableTower/ui/board_layout.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

107 lines
4 KiB
GDScript

class_name BoardLayout
extends RefCounted
## Pure layout helpers for whole blocks on the board (group frames, collapsed
## group cards, loose cards), given as world rects. Each returns one offset
## per rect, in the same order; Main moves the blocks.
## Closes the big gaps between blocks while keeping their order.
##
## Blocks whose rows overlap form a band (top to bottom); inside a band,
## blocks whose columns overlap form a column (left to right), which keeps its
## inner layout. Gaps wider than `gap_x` between columns shrink to `gap_x`, the
## first column of every band moves to the board's left edge, and gaps wider
## than `gap_y` between bands shrink to `gap_y`. The mapping only ever moves
## blocks left and up, in order, so nothing that didn't overlap before
## overlaps after, and reading order (left to right, top to bottom) holds.
static func pack(rects: Array, gap_x: float = 60.0, gap_y: float = 60.0) -> Array[Vector2]:
var out: Array[Vector2] = []
out.resize(rects.size())
for i in rects.size():
out[i] = Vector2.ZERO
if rects.is_empty():
return out
var left := INF
for r: Rect2 in rects:
left = minf(left, r.position.x)
var bands := _clusters(rects, range(rects.size()), false)
var y_cursor := 0.0
for b in bands.size():
var band: Dictionary = bands[b]
var new_top: float = band["lo"]
if b > 0:
var prev: Dictionary = bands[b - 1]
new_top = y_cursor + minf(float(band["lo"]) - float(prev["hi"]), gap_y)
var dy := new_top - float(band["lo"])
y_cursor = float(band["hi"]) + dy
var cols := _clusters(rects, band["members"], true)
var x_cursor := left
for c in cols.size():
var col: Dictionary = cols[c]
var new_left := left
if c > 0:
var pc: Dictionary = cols[c - 1]
new_left = x_cursor + minf(float(col["lo"]) - float(pc["hi"]), gap_x)
var dx := new_left - float(col["lo"])
x_cursor = float(col["hi"]) + dx
for i: int in col["members"]:
out[i] = Vector2(dx, dy)
return out
## Groups indices whose rects overlap along one axis (x when `horizontal`)
## into clusters sorted along that axis: [{lo, hi, members}].
static func _clusters(rects: Array, idx: Array, horizontal: bool) -> Array:
var order := idx.duplicate()
order.sort_custom(func(a: int, b: int) -> bool:
var ra: Rect2 = rects[a]
var rb: Rect2 = rects[b]
return (ra.position.x < rb.position.x) if horizontal else (ra.position.y < rb.position.y))
var out: Array = []
for i: int in order:
var r: Rect2 = rects[i]
var lo := r.position.x if horizontal else r.position.y
var hi := r.end.x if horizontal else r.end.y
if not out.is_empty() and lo < float(out[-1]["hi"]):
out[-1]["hi"] = maxf(float(out[-1]["hi"]), hi)
(out[-1]["members"] as Array).append(i)
else:
out.append({"lo": lo, "hi": hi, "members": [i]})
return out
## Makes room for a block that grew from `compact` to `grown` (a group
## expanding in place; both share their top-left corner). Blocks to its right
## whose rows it now covers shift right together, and the blocks below it
## shift down together, each by just enough to clear it with `gap`. Blocks
## that already clear it don't move. Returns one offset per rect.
static func make_room(rects: Array, compact: Rect2, grown: Rect2, gap: float = 60.0) -> Array[Vector2]:
var out: Array[Vector2] = []
out.resize(rects.size())
for i in rects.size():
out[i] = Vector2.ZERO
var right: Array[int] = []
var below: Array[int] = []
for i in rects.size():
var r: Rect2 = rects[i]
if r.position.x >= compact.end.x - 1.0 and r.position.y < grown.end.y + gap and r.end.y > grown.position.y:
right.append(i)
elif r.position.y >= compact.end.y - 1.0:
below.append(i)
var dx := 0.0
for i in right:
var r: Rect2 = rects[i]
if r.position.y < grown.end.y and r.end.y > grown.position.y:
dx = maxf(dx, grown.end.x + gap - r.position.x)
if dx > 0.0:
for i in right:
out[i].x = dx
var dy := 0.0
for i in below:
var r: Rect2 = rects[i]
if r.position.x < grown.end.x + gap and r.end.x > grown.position.x - gap:
dy = maxf(dy, grown.end.y + gap - r.position.y)
if dy > 0.0:
for i in below:
out[i].y = dy
return out