TableTower/ui/inspector_list.gd

246 lines
9.6 KiB
GDScript3
Raw Normal View History

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, and a
## rebuild of the same card keeps the closed sections' headers (`_keep_closed`).
##
## 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:
var kept := _keep_closed(insp, t)
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 := insp.section_list
if list == null:
list = InspectorKit.vbox(6)
insp.section_list = list
insp.section_list_tid = t.id
insp._content.add_child(list)
else:
insp._content.move_child(list, -1)
_section(insp, list, kept, 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, kept, 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
for key: String in kept:
var box: Control = kept[key]["box"]
list.remove_child(box)
box.queue_free()
var at := 0
for key: String in insp.section_boxes:
list.move_child(insp.section_boxes[key], at)
at += 1
activate(insp, key_of(insp.selection))
## Before a rebuild: the closed sections that can stay, as {key: {box, parts}}.
## Sections with fields go. The list itself goes when the page shows another
## card or isn't a list (Inspector.rebuild keeps only `section_list`).
static func _keep_closed(insp: Inspector, t: RollTable) -> Dictionary:
var kept := {}
if insp.section_list == null:
return kept
if insp.section_list_tid != t.id:
drop_list(insp)
return kept
for key: String in insp.section_boxes:
var box: Control = insp.section_boxes[key]
var parts: Dictionary = insp.section_parts[key]
if parts["built"]:
insp.section_list.remove_child(box)
box.queue_free()
else:
kept[key] = {"box": box, "parts": parts}
return kept
## Forgets the list of sections and everything in it.
static func drop_list(insp: Inspector) -> void:
if insp.section_list != null:
insp.section_list.get_parent().remove_child(insp.section_list)
insp.section_list.queue_free()
insp.section_list = null
insp.section_list_tid = ""
insp.section_parts.clear()
insp.section_boxes.clear()
## "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.
## `kept`: closed sections from before the rebuild; one that still fits is used
## again (and taken out of `kept`).
static func _section(insp: Inspector, list: VBoxContainer, kept: Dictionary, key: String, kind: String, title: String, span: String, fill: Callable) -> void:
var open: bool = insp.open_sections.get(key, false)
var look := "%s|%s" % [kind, span]
if not open and kept.has(key) and kept[key]["parts"]["look"] == look:
insp.section_boxes[key] = kept[key]["box"]
insp.section_parts[key] = kept[key]["parts"]
(kept[key]["parts"]["title"] as Label).text = title
kept.erase(key)
return
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, "look": look}
insp.section_parts[key] = parts
if not open:
_style_head(parts, 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(parts, false)
## Outlines the active section's header. A header that already looks this way
## is left alone (restyling makes it lay out again).
static func _style_head(parts: Dictionary, active: bool) -> void:
if parts.get("active") == active:
return
parts["active"] = active
var open: bool = parts["built"]
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)
(parts["head"] as PanelContainer).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:
_style_head(insp.section_parts[k], 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)")