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

164 lines
5.1 KiB
GDScript

class_name Minimap
extends Control
## A small overview of the whole board in the bottom-right corner: group
## frames and collapsed group cards as tinted blocks, cards as card-coloured
## rectangles (brass when they hold a lit result) and the part of the board
## in view as a rectangle. Click or drag on it to pan there.
##
## It only redraws when BoardView asks (the view moved, or the board's
## geometry changed), never every frame. The map's scale comes from the
## board's content, so it holds still while panning.
const MAX_SIZE := Vector2(232, 160)
const MARGIN := 14.0
const PAD := 8.0
var board: BoardView
var _bg: StyleBoxFlat
var _dragging := false
## World rect the map shows, and how it maps onto the map's inner rect.
var _world := Rect2()
var _scale := 1.0
var _offset := Vector2.ZERO
var _rev := -1
func setup(p_board: BoardView) -> void:
board = p_board
mouse_filter = MOUSE_FILTER_STOP
mouse_default_cursor_shape = CURSOR_POINTING_HAND
tooltip_text = "Overview: click or drag to move around (M)"
_bg = Style.bordered(Style.box(Color(Style.FELT_DEEP, 0.92), 10), 1, Color(1, 1, 1, 0.16))
_bg.shadow_color = Color(0, 0, 0, 0.35)
_bg.shadow_size = 12
_bg.shadow_offset = Vector2(0, 4)
visible = false
size = MAX_SIZE
board.resized.connect(place)
## Re-places and redraws the map (BoardView calls this when the view or the
## board's geometry changed). Does nothing while hidden.
func refresh() -> void:
if not visible:
return
place()
queue_redraw()
## Keeps the map in the board's bottom-right corner, sized to the content's shape.
func place() -> void:
if board == null:
return
_refresh_frame()
position = board.size - size - Vector2(MARGIN, MARGIN)
func _notification(what: int) -> void:
if what == NOTIFICATION_VISIBILITY_CHANGED and visible:
_rev = -1
refresh()
## Recomputes the world rect and scale from the board's content (only when
## the board's geometry changed).
func _refresh_frame() -> void:
if _rev == board.geometry_rev and _world.size != Vector2.ZERO:
return
_rev = board.geometry_rev
var b := board.content_bounds()
if b.size == Vector2.ZERO:
b = Rect2(Vector2.ZERO, Vector2(1000, 700))
b = b.grow(maxf(b.size.x, b.size.y) * 0.03 + 40.0)
set_world(b)
## Sets the world rect the map covers and resizes the map to its shape.
func set_world(b: Rect2) -> void:
_world = b
var inner := MAX_SIZE - Vector2(PAD, PAD) * 2.0
_scale = minf(inner.x / b.size.x, inner.y / b.size.y)
var used := b.size * _scale
size = Vector2(maxf(used.x, 90.0), maxf(used.y, 60.0)) + Vector2(PAD, PAD) * 2.0
_offset = (size - used) / 2.0
func world_to_map(p: Vector2) -> Vector2:
return _offset + (p - _world.position) * _scale
func map_to_world(p: Vector2) -> Vector2:
return _world.position + (p - _offset) / _scale
## The board's view as a rect on the map (map coordinates).
func view_rect_on_map() -> Rect2:
var v := board.view_rect()
return Rect2(world_to_map(v.position), v.size * _scale)
func _draw() -> void:
if board == null or board.main == null or board.main.data == null:
return
var main := board.main
draw_style_box(_bg, Rect2(Vector2.ZERO, size))
var inner := Rect2(Vector2(3, 3), size - Vector2(6, 6))
# Expanded group frames first, then cards and collapsed groups on top.
for g in main.data.groups:
if not g.collapsed:
_block(g.rect(), Color(0, 0, 0, 0.28), Color(1, 1, 1, 0.18), inner)
var hidden := main.hidden_groups()
var lit_tables := {}
for k: String in main.lit_entries:
lit_tables[k.substr(0, k.rfind(":"))] = true
for t in main.data.tables:
if hidden.has(t.id):
continue
var c := main.card_for(t.id)
var r := Rect2(c.position, c.size) if c else Rect2(t.position, Vector2(TableCard.WIDTH, 120))
var lit := lit_tables.has(t.id)
_block(r, Style.BRASS if lit else Color(Style.CARD, 0.85), Color(0, 0, 0, 0), inner)
for g in main.data.groups:
if g.collapsed:
var n := main.group_node(g.id)
var r := Rect2(n.position, n.size) if n else Rect2(g.position, Vector2(TableCard.WIDTH, 120))
var lit := false
for tid in g.members:
if lit_tables.has(tid):
lit = true
break
_block(r, Style.BRASS if lit else Color("#cfc6ae"), Style.COPPER_DARK, inner)
var v := view_rect_on_map().intersection(inner)
if v.size.x > 0.0 and v.size.y > 0.0:
draw_rect(v, Color(Style.ON_FELT, 0.08))
draw_rect(v, Style.ON_FELT, false, 1.5)
## One block on the map, at least 2 px so tiny cards still show.
func _block(r: Rect2, fill: Color, edge: Color, clip: Rect2) -> void:
var m := Rect2(world_to_map(r.position), (r.size * _scale).max(Vector2(2, 2))).intersection(clip)
if m.size.x <= 0.0 or m.size.y <= 0.0:
return
draw_rect(m, fill)
if edge.a > 0.0 and m.size.x > 4.0 and m.size.y > 4.0:
draw_rect(m, edge, false, 1.0)
func _gui_input(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb:
if mb.button_index == MOUSE_BUTTON_LEFT:
_dragging = mb.pressed
if mb.pressed:
pan_to_map_point(mb.position)
accept_event()
return
var mm := ev as InputEventMouseMotion
if mm and _dragging:
pan_to_map_point(mm.position)
accept_event()
## Centres the board's view on the world point under a map point.
func pan_to_map_point(p: Vector2) -> void:
board.center_on(map_to_world(p))