TableTower/ui/main_navigation.gd
RobinFiredrake 0202312688 Split ui/main.gd into area helpers (no behaviour change)
Main (3679 lines) becomes a shell: shared state, start-up, cards, input,
rules view and one-line delegators for the methods other files call. Each
area moves with its state into a RefCounted helper holding a back-reference
(m: Main):

  main_cli.gd          command-line and screenshot flags
  main_top_bar.gd      top bar, toast, endless-loop chip
  main_editing.gd      add/delete cards, results and pipes, selection bar
  main_groups.gd       groups and card drags
  main_templates.gd    templates
  main_rolling.gd      rolling, choice questions, playback, session seed, copy
  main_collections.gd  collections, import/export, table settings, test board
  main_state.gd        decks, variables, roll history (local state)
  main_navigation.gd   search, focus, minimap, arrange/pack
  main_history.gd      undo/redo and mark_dirty
  main_inspector.gd    inspector selection and refresh

Tests that reached moved private members now go through the helper
(main.undo_redo.edits, main.rolling.pending_choice(), ...).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:14:19 -06:00

338 lines
11 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
const ARRANGE_PACK := 0
const ARRANGE_COLLAPSE := 1
var m: Main
var _search_button: Button
var _search_panel: SearchPanel
var _map_toggle: Button
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
var g := m.data.group_of(table_id)
if g and g.collapsed:
var info := _expand_group(g)
notes.append("Opened group %s" % Formatter.strip_tags(g.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 -----
func _make_map_toggle() -> Button:
var b := Style.bar_button("Map")
b.toggle_mode = true
b.tooltip_text = "Show an overview of the board in the corner (M)"
var on := Style.bordered(Style.box(Color(Style.BRASS, 0.16), 8, [12, 6, 12, 6]), 1, Style.BRASS)
b.add_theme_stylebox_override("pressed", on)
b.add_theme_stylebox_override("hover_pressed", on)
m.top_bar._bar_icon(b, func(c: Control) -> void:
c.draw_rect(Rect2(1, 2, 14, 12), Style.ON_FELT, false, 1.5)
c.draw_rect(Rect2(4, 5, 4, 3), Style.ON_FELT)
c.draw_rect(Rect2(9, 8, 3, 3), Style.BRASS))
b.toggled.connect(func(pressed: bool) -> void: set_minimap_visible(pressed, true))
_nav_mode_changed.call_deferred()
return b
## 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 _map_toggle:
_map_toggle.set_pressed_no_signal(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 -----
func _make_arrange_menu() -> MenuButton:
var mb := MenuButton.new()
mb.text = "Arrange ▾"
mb.tooltip_text = "Tidy the layout of the board"
mb.flat = false
var ref := Style.bar_button("")
for n in ["normal", "hover", "pressed", "hover_pressed", "disabled", "focus"]:
mb.add_theme_stylebox_override(n, ref.get_theme_stylebox(n))
for c in ["font_color", "font_hover_color", "font_focus_color", "font_pressed_color", "font_hover_pressed_color"]:
mb.add_theme_color_override(c, ref.get_theme_color(c))
mb.add_theme_font_size_override("font_size", 15)
mb.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
ref.free()
var pm := mb.get_popup()
ModalCard.style_popup(pm)
pm.add_item("Pack collapsed groups", ARRANGE_PACK)
pm.set_item_tooltip(0, "Move collapsed groups and the cards between them together, closing the gaps, in reading order")
# Build mode's bar has no room for the Collapse all button, so it lives here.
pm.add_item("Collapse all", ARRANGE_COLLAPSE)
pm.about_to_popup.connect(func() -> void:
pm.set_item_text(pm.get_item_index(ARRANGE_COLLAPSE), "Collapse all" if m._any_expanded() else "Expand all"))
pm.id_pressed.connect(func(id: int) -> void:
if id == ARRANGE_PACK:
pack_collapsed_groups()
elif id == ARRANGE_COLLAPSE:
m.set_all_collapsed(m._any_expanded()))
return mb
## 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
if g.id == skip:
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 board: 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}