TableTower/ui/inspector_list.gd
RobinFiredrake 1e944fa27d Inspector: one list with the card and every result, each collapsible
Selecting a card or result on the spread opens its section and closes the
rest; sections opened by hand stay open, and a click or focus inside one
makes it the section edits go to. Only open sections build their fields.
Result sections edit the weight or range and the position; the card
section picks Weights or Ranges and Auto-sort.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:35:27 -06:00

183 lines
7.5 KiB
GDScript

class_name InspectorList
extends RefCounted
## The inspector's page for a card, as one list: the card's own section, then
## one section per result, each with a header that opens and closes it.
##
## Clicking the card or a result on the spread opens that section and closes the
## rest (Inspector.select). Sections opened by hand stay open until then.
## Only open sections build their fields, so long cards stay quick.
##
## The older section code edits "the selected result" (Inspector.selection,
## fields, effects, ...). So each open section keeps its own parts, and a click
## or focus inside a section makes it the active one (activate): selection and
## those parts then point at it, and its header is outlined.
const CARD := "card"
## Builds the whole page for card `t` into the inspector's content box.
static func build(insp: Inspector, t: RollTable) -> void:
insp.section_parts.clear()
insp.section_boxes.clear()
var keep_selection := insp.selection.duplicate()
insp._content.add_child(insp._head("Card · " + Describe.kind_label(t), Inspector._title_of(t.name, "(no name)")))
var list := InspectorKit.vbox(6)
insp._content.add_child(list)
_section(insp, list, CARD, "Card", "Settings", "", func(body: VBoxContainer) -> void:
insp.selection = {"kind": "card", "tid": t.id}
InspectorCard.build(insp, body, t, false))
if t.kind != RollTable.KIND_VARIABLE:
for i in t.entries.size():
var e := t.entries[i]
var span := t.num_label(e, i) if t.is_ranged() and e.has_range else ""
_section(insp, list, e.id, _kind_word(t), Inspector._title_of(e.text, "(nothing)"), span, func(body: VBoxContainer) -> void:
insp.selection = {"kind": "result", "tid": t.id, "eid": e.id}
InspectorRange.build(insp, body, t, e)
var outer := insp._content
insp._content = body
InspectorResult.section(insp, t, e, false)
insp._content = outer)
insp.selection = keep_selection
activate(insp, key_of(insp.selection))
## "card" for a card selection, the result's id for a result.
static func key_of(sel: Dictionary) -> String:
return str(sel.get("eid", "")) if str(sel.get("kind", "")) == "result" else CARD
static func _kind_word(t: RollTable) -> String:
match t.kind:
RollTable.KIND_CHOICE:
return "Option"
RollTable.KIND_ROUTER:
return "Case"
return "Result"
## One section: a header (▸/▾, kind, title, range) and, when open, its body.
static func _section(insp: Inspector, list: VBoxContainer, key: String, kind: String, title: String, span: String, fill: Callable) -> void:
var open: bool = insp.open_sections.get(key, false)
var box := InspectorKit.vbox(0)
list.add_child(box)
insp.section_boxes[key] = box
var head := PanelContainer.new()
head.mouse_filter = Control.MOUSE_FILTER_STOP
head.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
head.tooltip_text = "Close this section" if open else "Open this section"
var row := InspectorKit.hbox(8)
head.add_child(row)
row.add_child(InspectorKit.word("▾" if open else "▸", Style.MUTED))
var kind_label := InspectorKit.flabel(kind)
kind_label.size_flags_vertical = Control.SIZE_SHRINK_CENTER
row.add_child(kind_label)
var title_label := InspectorKit.word(title, Style.INK, true)
title_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
title_label.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
title_label.clip_text = true
row.add_child(title_label)
if span != "":
row.add_child(InspectorKit.word(span, Style.TRIM))
head.gui_input.connect(func(ev: InputEvent) -> void:
if ev is InputEventMouseButton and (ev as InputEventMouseButton).pressed and (ev as InputEventMouseButton).button_index == MOUSE_BUTTON_LEFT:
toggle(insp, key))
box.add_child(head)
var parts := {"head": head, "title": title_label, "fields": {}, "pipe_parts": {}, "built": open}
insp.section_parts[key] = parts
if not open:
_style_head(head, false, false)
return
var pad := MarginContainer.new()
for side: String in ["margin_left", "margin_right"]:
pad.add_theme_constant_override(side, 8)
pad.add_theme_constant_override("margin_top", 10)
pad.add_theme_constant_override("margin_bottom", 14)
box.add_child(pad)
var body := InspectorKit.vbox(14)
pad.add_child(body)
insp.fields = parts["fields"]
insp.pipe_parts = parts["pipe_parts"]
insp.effects = null
insp.show_if_builder = null
insp.case_builder = null
fill.call(body)
parts["effects"] = insp.effects
parts["show_if_builder"] = insp.show_if_builder
parts["case_builder"] = insp.case_builder
_style_head(head, true, false)
static func _style_head(head: PanelContainer, open: bool, active: bool) -> void:
var fill := Style.HOVER_ROW if open else Color(0, 0, 0, 0.04)
var sb := Style.bordered(Style.box(fill, 7, [10, 7, 10, 7]), 2 if active else 1, Style.TRIM if active else Style.RULE)
head.add_theme_stylebox_override("panel", sb)
## Opens or closes a section by its header. Opening one makes it active.
static func toggle(insp: Inspector, key: String) -> void:
var t := insp.selected_table()
if t == null:
return
var open: bool = not insp.open_sections.get(key, false)
insp.open_sections[key] = open
if open:
insp.selection = {"kind": "card", "tid": t.id} if key == CARD else {"kind": "result", "tid": t.id, "eid": key}
insp.rebuild()
insp._mark_cards()
## Makes section `key` the one edits go to: the selection and the shared parts
## (fields, effects, ...) point at it, and its header is outlined.
static func activate(insp: Inspector, key: String) -> void:
var t := insp.selected_table()
if t == null or not insp.section_parts.has(key):
return
var parts: Dictionary = insp.section_parts[key]
if parts.get("built", false):
insp.fields = parts["fields"]
insp.pipe_parts = parts["pipe_parts"]
insp.effects = parts.get("effects")
insp.show_if_builder = parts.get("show_if_builder")
insp.case_builder = parts.get("case_builder")
var was := key_of(insp.selection)
insp.selection = {"kind": "card", "tid": t.id} if key == CARD else {"kind": "result", "tid": t.id, "eid": key}
for k: String in insp.section_parts:
var p: Dictionary = insp.section_parts[k]
_style_head(p["head"], p.get("built", false), k == key)
if was != key:
insp._mark_cards()
## The open section under a point or holding a control, or "".
static func key_at(insp: Inspector, global_point: Vector2) -> String:
for key: String in insp.section_boxes:
var box: Control = insp.section_boxes[key]
if is_instance_valid(box) and box.get_global_rect().has_point(global_point):
return key
return ""
static func key_holding(insp: Inspector, c: Control) -> String:
for key: String in insp.section_boxes:
var box: Control = insp.section_boxes[key]
if is_instance_valid(box) and box.is_ancestor_of(c):
return key
return ""
## Scrolls so the section's header shows (after the page is laid out).
static func scroll_to(insp: Inspector, key: String) -> void:
await insp.get_tree().process_frame
var head: Control = (insp.section_parts.get(key, {}) as Dictionary).get("head")
if is_instance_valid(head) and is_instance_valid(insp._scroll):
# The header near the top, so the open section's fields show below it.
var offset := head.get_global_rect().position.y - insp._scroll.get_global_rect().position.y
insp._scroll.scroll_vertical = maxi(0, insp._scroll.scroll_vertical + int(offset) - 8)
## Keeps a result's header title in step with its text as it is typed.
static func refresh_title(insp: Inspector, e: TableEntry) -> void:
var parts: Dictionary = insp.section_parts.get(e.id, {})
var label: Label = parts.get("title")
if is_instance_valid(label):
label.text = Inspector._title_of(e.text, "(nothing)")