TableTower/ui/table_preview.gd
RobinFiredrake 86d1b214c3 Dynamic pick options, show_if and indirect @[expr] names
Pick choice cards can take their options from variables (options_from:
@prefix_* or a @list, filtered by options_where with option, option_label
and option_value bound). A Chosen entry runs for whichever dynamic option
is picked; fixed entries stay as extras, listed first. Random weighs each
dynamic option as the Chosen entry and fixed ones by weight. Any pick
option can have show_if, which hides it from the pop-up and from Random;
with nothing offered the node stops with "No options available".

@[expr] reads or writes the variable named by expr in expressions,
effects (including reset) and text ({@[expr]}); bad computed names are
effect errors. JSON round trip, lint_options in lint_pipes, choice
pop-up value badges and previews, card and settings UI, docs, demo board
(Spend a find) and tests/test_options.gd.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 18:55:38 -06:00

205 lines
7.6 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name TablePreview
extends RefCounted
## A compact, read-only summary of a table, as a Control that can be dropped
## into any panel: name, kind and dice, details, then the entries with their
## number or range labels and chances (capped), and a "→ Next table" hint on
## entries that lead further. Used by the choice pop-up's preview panel; the
## board's hover pop-ups can use it too.
##
## Usage: TablePreview.build(board, table_id, {"count": "2"}) -> Control.
## Nothing in it takes the mouse, so it never gets in the way of what is
## underneath.
## Rows shown before "+N more".
const MAX_ROWS := 12
const NEXT_HINT := "→ Next table"
## `opts`: count (String, e.g. "2" or "@terms": shown as "×2" when not "1"),
## max_rows (int), width (float, minimum width of the block),
## heading (String shown in place of the kind line, optional).
static func build(board: TowerBoard, table_id: String, opts: Dictionary = {}) -> Control:
var box := VBoxContainer.new()
box.add_theme_constant_override("separation", 4)
box.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.custom_minimum_size.x = float(opts.get("width", 0.0))
var t: RollTable = board.get_table(table_id) if board != null else null
if t == null:
box.add_child(_label("Missing table", 14, Style.DANGER_INK, true))
return box
# Head: name, then "Roll table · d100" and the pipe count.
var head := HBoxContainer.new()
head.add_theme_constant_override("separation", 8)
head.mouse_filter = Control.MOUSE_FILTER_IGNORE
var name := Style.make_label(Formatter.strip_tags(t.name), Style.display(), 17, Style.INK)
name.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
name.size_flags_horizontal = Control.SIZE_EXPAND_FILL
name.mouse_filter = Control.MOUSE_FILTER_IGNORE
head.add_child(name)
var count := str(opts.get("count", "1")).strip_edges()
if count != "" and count != "1":
head.add_child(_label("×" + count, 15, Style.COPPER, true))
box.add_child(head)
box.add_child(_label(str(opts["heading"]) if opts.has("heading") else kind_line(t), 12, Style.MUTED, false))
var det := t.details.strip_edges()
if det != "":
var d := RichText.label(RichText.decorate(det), 13, Style.INK, false)
d.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.add_child(d)
var extra := summary_line(t)
if extra != "":
box.add_child(_label(extra, 13, Style.INK, false, true))
var shown := 0
var cap := int(opts.get("max_rows", MAX_ROWS))
var list := VBoxContainer.new()
list.add_theme_constant_override("separation", 2)
list.mouse_filter = Control.MOUSE_FILTER_IGNORE
if t.kind == RollTable.KIND_SHEET:
# A sheet's "entry" is its template: show it filled in, not as a result row.
var vars: Dictionary = opts.get("vars", {})
var sheet := RichText.label(RichText.decorate(Roller.render_sheet(board, vars, t)), 13, Style.INK, false)
sheet.mouse_filter = Control.MOUSE_FILTER_IGNORE
box.add_child(sheet)
return box
if t.kind != RollTable.KIND_VARIABLE:
for i in t.entries.size():
if shown >= cap:
break
list.add_child(_row(t, t.entries[i], i))
shown += 1
if list.get_child_count() > 0:
box.add_child(list)
if t.kind != RollTable.KIND_VARIABLE and t.entries.size() > shown:
box.add_child(_label("+%d more" % (t.entries.size() - shown), 12, Style.MUTED, false))
return box
## "Roll table · d100", "Choice card · pick", ...
static func kind_line(t: RollTable) -> String:
match t.kind:
RollTable.KIND_CHOICE:
return "Choice card · " + {"pick": "pick an option", "number": "enter a number", "text": "enter text"}.get(t.choice, t.choice)
RollTable.KIND_LOOP:
return "Loop card"
RollTable.KIND_VARIABLE:
return "Variable card"
RollTable.KIND_SHEET:
return "Sheet card"
RollTable.KIND_ROUTER:
return "Router · if / else if / otherwise"
var s := "Roll table · " + t.dice_label()
return s + " · deck" if t.card_mode else s
## The kind-specific line: a choice card's question, a loop's rule, ...
static func summary_line(t: RollTable) -> String:
match t.kind:
RollTable.KIND_CHOICE:
var q := Formatter.strip_tags(t.prompt).strip_edges()
var ask := "Asks: " + q if q != "" else "Asks the player"
match t.choice:
"number":
return "%s (a whole number from %s to %s)" % [ask, t.min_expr, t.max_expr]
"text":
return ask + " (free text)"
if t.is_dynamic():
return "%s\nOptions: %s" % [ask, t.options_description()]
return ask
RollTable.KIND_LOOP:
var e := t.loop_expr.strip_edges()
match t.loop_mode:
"while":
return "Repeats while %s" % e
"until":
return "Repeats until %s" % e
return "Repeats %s times" % (e if e != "" else "?")
RollTable.KIND_VARIABLE:
return "Holds @%s (%s)" % [t.var_name, t.var_type]
RollTable.KIND_ROUTER:
return "Goes on with the first case that's true"
return ""
static func _row(t: RollTable, e: TableEntry, index: int) -> Control:
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 8)
row.mouse_filter = Control.MOUSE_FILTER_IGNORE
var num := _label(row_label(t, e, index), 12, Style.COPPER, false)
num.custom_minimum_size.x = 40
num.size_flags_vertical = Control.SIZE_SHRINK_BEGIN
row.add_child(num)
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 0)
col.size_flags_horizontal = Control.SIZE_EXPAND_FILL
col.mouse_filter = Control.MOUSE_FILTER_IGNORE
if t.kind == RollTable.KIND_ROUTER:
var cond := _label(TableCard.case_label(e), 12, Style.COPPER_DARK, true, true)
cond.max_lines_visible = 2
cond.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
col.add_child(cond)
if t.kind == RollTable.KIND_CHOICE and t.choice == "pick" and e.show_if.strip_edges() != "":
var si := _label("only if " + e.show_if.strip_edges(), 12, Style.COPPER_DARK, true, true)
si.max_lines_visible = 2
si.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
col.add_child(si)
var tx := Formatter.strip_tags(e.text).strip_edges()
if t.is_dynamic() and index == 0 and (tx == "" or tx == Roller.CHOSEN):
tx = "The picked option"
var text := _label(tx if tx != "" else "(empty)", 13, Style.INK, false, true)
text.max_lines_visible = 2
text.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
col.add_child(text)
if not e.pipes.is_empty():
col.add_child(_label(NEXT_HINT, 11, Style.COPPER, false))
row.add_child(col)
if t.kind == RollTable.KIND_ROLL:
var ch := chance_text(t, e)
if ch != "":
var c := _label(ch, 12, Style.MUTED, false)
c.size_flags_vertical = Control.SIZE_SHRINK_BEGIN
row.add_child(c)
return row
## The number or range an entry covers; choice options and loop steps are
## numbered or named instead.
static func row_label(t: RollTable, e: TableEntry, index: int) -> String:
match t.kind:
RollTable.KIND_CHOICE:
if t.choice != "pick":
return "Answer"
if t.is_dynamic():
return "Chosen" if index == 0 else str(index)
return str(index + 1)
RollTable.KIND_LOOP:
return "Each" if index == 0 else "After"
RollTable.KIND_ROUTER:
return str(index + 1)
return t.num_label(e, index)
## "25%", "<1%" or "" when the chances can't be known.
static func chance_text(t: RollTable, e: TableEntry) -> String:
if not t.has_chances():
return ""
var p := t.chance(e) * 100.0
if p <= 0.0:
return "0%"
if p < 1.0:
return "<1%"
if p < 10.0 and absf(p - roundf(p)) > 0.05:
return "%.1f%%" % p
return "%d%%" % roundi(p)
static func _label(text: String, size: int, color: Color, bold: bool, wrap: bool = false) -> Label:
var l := Style.make_label(text, Style.body_bold() if bold else Style.body(), size, color)
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
if wrap:
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
l.size_flags_horizontal = Control.SIZE_EXPAND_FILL
return l