TableTower/ui/main_navigation.gd
RobinFiredrake 5d427d3856 Nested groups: groups can hold groups
TableGroup.parent (JSON "parent") puts a group inside another. Grouping a
selection that includes groups nests them (the new group goes in the group
they all share). A collapsed group hides every card and group inside it and
stands in for them on threads; moving, deleting, search, focus (opens every
collapsed group around a card), packing, undo and templates all work
through the nesting. Ungroup hands the contents to the outer group.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:47:05 -06:00

295 lines
9.2 KiB
GDScript

class_name MainNavigation
extends RefCounted
## Main's navigation: search, focus (jump to a card), the minimap and
## Arrange (pack collapsed groups, make room).
## focus_table zooms in to at least this, so the card can be read.
const READABLE_ZOOM := 0.85
## ...but zooms out as far as this to fit a tall card.
const MIN_FOCUS_ZOOM := 0.7
const PACK_GAP := 60.0
var m: Main
var _search_button: Button
var _search_panel: SearchPanel
func _init(p_main: Main) -> void:
m = p_main
# ----- Search (top bar, Ctrl+F) -----
func toggle_search() -> void:
if _search_panel and _search_panel.visible:
_search_panel.close()
else:
open_search()
## Opens the search pop-over (Ctrl+F), with its field focused.
func open_search() -> SearchPanel:
if _search_panel == null:
_search_panel = SearchPanel.new()
m.add_child(_search_panel)
_search_panel.setup(m)
_search_panel.open(_search_button)
return _search_panel
# ----- Focus: jump to a card -----
## Shows a card: expands its group if that's collapsed (saved, with a toast),
## expands the card if it's collapsed and an entry was asked for, glides the
## view so the card sits in the middle at a readable zoom and flashes a ring
## around it. With `entry_id`, a long list scrolls to that result
## (TableCard.reveal) and its row flashes too. Unknown ids do nothing.
func focus_table(table_id: String, entry_id: String = "") -> void:
var t := m.data.get_table(table_id)
if t == null:
return
if entry_id != "" and t.find_entry(entry_id) == null:
entry_id = ""
var notes := PackedStringArray()
var undoable := false
var settle := false
# Open every collapsed group around the card, outermost first.
var around: Array[TableGroup] = []
var g := m.data.group_of(table_id)
while g != null:
if g.collapsed:
around.push_front(g)
g = m.data.get_group(g.parent)
for outer in around:
var info := _expand_group(outer)
notes.append("Opened group %s" % Formatter.strip_tags(outer.name))
if int(info["moved"]) > 0:
notes.append("moved %d %s to make room" % [info["moved"], "block" if int(info["moved"]) == 1 else "blocks"])
undoable = true
settle = true
if entry_id != "" and t.collapsed and t.kind != RollTable.KIND_VARIABLE and t.kind != RollTable.KIND_SHEET:
t.collapsed = false
var c0 := m.card_for(table_id)
if c0:
c0.rebuild()
m.mark_dirty("", false)
settle = true
var card := m.card_for(table_id)
if card == null:
return
if settle or card.size.y <= 0.0:
# Let a card that was just built or expanded lay out before measuring it.
for i in 3:
await m.get_tree().process_frame
card = m.card_for(table_id)
if card == null:
return
# On top of any card it overlaps.
card.get_parent().move_child(card, -1)
if entry_id != "":
card.reveal(entry_id)
var r := Rect2(card.position, card.size)
m.board.animate_to(_focus_point(r), _focus_zoom(r))
m.board.cards.flash(card)
if not notes.is_empty():
m.toast(", ".join(notes), undoable)
if entry_id != "":
# The list scrolls on the next layout pass; ring the row after it.
await m.get_tree().process_frame
if is_instance_valid(card):
m.board.cards.flash(card, entry_rect(card, entry_id))
## Centres a group: its frame, or its compact card when collapsed (which
## isn't expanded), and flashes it.
func focus_group(gid: String) -> void:
var g := m.data.get_group(gid)
var n := m.group_node(gid)
if g == null or n == null:
return
if g.collapsed:
n.get_parent().move_child(n, -1)
var r := Rect2(n.position, n.size)
var z := _focus_zoom(r) if g.collapsed else clampf(_fit_zoom(r), BoardView.MIN_ZOOM, READABLE_ZOOM)
m.board.animate_to(r.get_center() if not g.collapsed or r.size.y * z < m.board.size.y else _focus_point(r), z)
m.board.cards.flash(n)
## The world rect of an entry's row on a card (anchor_for geometry), or Rect2().
func entry_rect(card: TableCard, eid: String) -> Rect2:
var a := card.anchor_for(eid)
var row: Control = card.rows.get(eid)
if a.is_empty() or row == null:
return Rect2()
var p: Vector2 = a["pos"]
var h := row.size.y
return Rect2(card.position.x + 3.0, p.y - h / 2.0, card.size.x - 6.0, h)
func _fit_zoom(r: Rect2) -> float:
return minf((m.board.size.x - 80.0) / maxf(r.size.x, 1.0), (m.board.size.y - 80.0) / maxf(r.size.y, 1.0))
func _focus_zoom(r: Rect2) -> float:
var z := maxf(m.board.zoom, READABLE_ZOOM)
return clampf(minf(z, maxf(MIN_FOCUS_ZOOM, _fit_zoom(r))), BoardView.MIN_ZOOM, BoardView.MAX_ZOOM)
## The middle of a card, or a point that keeps its top in view when it's
## taller than the board at the zoom focus_table picks.
func _focus_point(r: Rect2) -> Vector2:
var z := _focus_zoom(r)
var c := r.get_center()
if r.size.y > (m.board.size.y - 80.0) / z:
c.y = r.position.y - 40.0 / z + m.board.size.y / 2.0 / z
return c
## Expands a collapsed group (saved) and makes room for it. Returns
## _make_room_for's {moved}.
func _expand_group(g: TableGroup) -> Dictionary:
var n := m.group_node(g.id)
var compact := Rect2(n.position, n.size) if n else Rect2()
var blocks := _layout_blocks(g.id)
g.collapsed = false
m.details().hide_for()
m.grouping._groups_changed()
m.mark_dirty("", false)
return _make_room_for(g, compact, blocks)
# ----- Minimap -----
## Shows or hides the minimap; `remember` saves the choice for this mode.
func set_minimap_visible(on: bool, remember: bool = false) -> void:
if m.board == null:
return
m.board.minimap.visible = on
if remember and m.state:
m.state.set_setting(_minimap_key(), on)
m.state.save()
func _minimap_key() -> String:
return "minimap_build" if m.mode == Main.Mode.BUILD else "minimap_play"
## Off by default in Play (which stays simple), on by default in Build;
## either is remembered once toggled.
func _minimap_default() -> bool:
var dflt := m.mode == Main.Mode.BUILD
return bool(m.state.get_setting(_minimap_key(), dflt)) if m.state else dflt
## set_mode calls this after switching.
func _nav_mode_changed() -> void:
set_minimap_visible(_minimap_default())
# ----- Arrange: pack collapsed groups -----
## The blocks Pack and make-room move: every group (its compact card when
## collapsed, its frame when expanded) and every card in no group, as
## [{rect, group, table}] with one of group / table set. `skip` is a group id
## to leave out.
func _layout_blocks(skip: String = "") -> Array:
var out: Array = []
var grouped := {}
for g in m.data.groups:
for tid in g.members:
grouped[tid] = true
# Inner groups move with the outermost one.
if g.id == skip or g.parent != "":
continue
var r := g.rect()
if g.collapsed:
var n := m.group_node(g.id)
r = Rect2(n.position, n.size) if n else Rect2(g.position, Vector2(TableCard.WIDTH, 120))
out.append({"rect": r, "group": g.id, "table": ""})
for t in m.data.tables:
if grouped.has(t.id):
continue
var c := m.card_for(t.id)
var r := Rect2(c.position, c.size) if c else Rect2(t.position, Vector2(TableCard.WIDTH, 120))
out.append({"rect": r, "group": "", "table": t.id})
return out
func _move_block(b: Dictionary, delta: Vector2) -> void:
if delta == Vector2.ZERO:
return
if b["group"] != "":
m.move_group(str(b["group"]), delta)
else:
var t := m.data.get_table(str(b["table"]))
t.position += delta
var c := m.card_for(t.id)
if c:
c.position = t.position
## Build ▸ Arrange ▸ Pack collapsed groups: closes the gaps that collapsed
## blocks leave (BoardLayout.pack), keeping reading order, then fits the view.
## Saved, with undo. Returns how many blocks moved.
func pack_collapsed_groups() -> int:
var any := false
for g in m.data.groups:
if g.collapsed:
any = true
break
if not any:
m.toast("Collapse some groups first: Pack closes the gaps they leave")
return 0
var blocks := _layout_blocks()
var rects: Array = []
for b: Dictionary in blocks:
rects.append(b["rect"])
var offsets := BoardLayout.pack(rects, PACK_GAP, PACK_GAP)
m.undo_redo._begin_edit()
var moved := 0
for i in blocks.size():
var d := offsets[i].round()
if d != Vector2.ZERO:
_move_block(blocks[i], d)
moved += 1
if moved == 0:
m.toast("Already packed")
return 0
m.undo_redo._end_edit("Pack collapsed groups")
m.toast("Packed the spread: moved %d %s" % [moved, "block" if moved == 1 else "blocks"], true)
await m.get_tree().process_frame
await m.get_tree().process_frame
m.board.fit()
return moved
## After a group expands in place: if its frame now covers other blocks, they
## shift right or down to clear it (BoardLayout.make_room). `blocks` are the
## other blocks (_layout_blocks) measured before the group expanded, while
## every group card was laid out. Returns {moved}; the move is an undo step
## of its own (expanding the group isn't one).
func _make_room_for(g: TableGroup, compact: Rect2, blocks: Array) -> Dictionary:
if compact.size == Vector2.ZERO or blocks.is_empty():
return {"moved": 0}
var grown := g.rect()
var rects: Array = []
var hits := false
for b: Dictionary in blocks:
var r: Rect2 = b["rect"]
rects.append(r)
if r.intersects(grown):
hits = true
if not hits:
return {"moved": 0}
m.undo_redo._begin_edit()
var offsets := BoardLayout.make_room(rects, compact, grown, PACK_GAP)
var moved := 0
for i in blocks.size():
var d := offsets[i].round()
if d != Vector2.ZERO:
_move_block(blocks[i], d)
moved += 1
if moved > 0:
m.undo_redo._end_edit("Make room for %s" % Formatter.strip_tags(g.name))
return {"moved": moved}