TableTower/ui/table_preview.gd
RobinFiredrake ebe81d9f1e Theme system: runtime colour tokens, theme sets and a Theme dialog
Style's colours become static vars named by role (BG, PANEL, CARD, THREAD,
ACCENT, TRIM, GLOW…) instead of materials (felt, copper, brass), so a theme
can replace them. Stray hard-coded colours moved into tokens.

ThemeStore applies built-in sets (Tarot, the new default: jewel-purple
cloth with a shine; Felt, the old look) and the user's own sets from
user://themes/. Fills for the background and panels can be gradients; the
background pattern (dots, stars, none) and logo are per set. View ▾ →
Theme… edits a set; changes to a built-in save as a new set. Using a theme
reloads the UI and keeps the open spread.

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

262 lines
10 KiB
GDScript
Raw 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.
##
## Rules (conditions, effects, option sources, loop rules, a check's rule)
## read in plain words unless opts "plain" is false (docs/PLAIN.md §6).
##
## 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), plain (bool,
## default RulesView.plain(null): plain words unless the text view is on).
static func build(board: TowerBoard, table_id: String, opts: Dictionary = {}) -> Control:
var plain := bool(opts.get("plain", RulesView.plain(null)))
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("×" + (RulesView.cut(Describe.dice(count, board), 18) if plain else count), 15, Style.TRIM, 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, board, plain)
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, board, plain))
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 · " + Describe.choice_label(t.choice).to_lower()
RollTable.KIND_LOOP:
return "Loop card"
RollTable.KIND_VARIABLE:
return "Collection card" if t.is_collection() else "Variable card"
RollTable.KIND_SHEET:
return "Sheet card"
RollTable.KIND_ROUTER:
return "Router · if / else if / otherwise"
RollTable.KIND_CHECK:
return "Check card · " + t.check_dice.strip_edges()
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, ... With
## `plain`, rules read in plain words (labelled from `board`).
static func summary_line(t: RollTable, board: TowerBoard = null, plain: bool = false) -> String:
if plain:
var s := _plain_summary(t, board)
if s != "":
return s
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 _plain_summary(t: RollTable, board: TowerBoard) -> 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, Describe.dice(t.min_expr, board), Describe.dice(t.max_expr, board)]
"text":
return ask + " (free text)"
if t.is_dynamic():
return "%s\n%s" % [ask, RulesView.options_line(t, board, true)]
return ask
RollTable.KIND_LOOP:
return _cap(RulesView.loop_rule(t, board, true))
RollTable.KIND_VARIABLE:
if t.is_collection():
return "Holds your %s" % Formatter.strip_tags(t.name).strip_edges()
return "Holds %s (%s), kept between rolls" % [Describe.label("@" + t.var_name, board), t.var_type]
RollTable.KIND_CHECK:
return Describe.check(t, board)
RollTable.KIND_ROLL:
if t.clamps():
return Describe.roll(t, board)
return ""
static func _cap(s: String) -> String:
if s.begins_with("×"):
return "Repeats " + s
return s.substr(0, 1).to_upper() + s.substr(1)
static func _row(t: RollTable, e: TableEntry, index: int, board: TowerBoard = null, plain: bool = false) -> 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.TRIM, 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(RulesView.case_label(e, board, plain), 12, Style.TRIM_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(RulesView.show_if_line(e, board, plain), 12, Style.TRIM_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"
if t.kind == RollTable.KIND_CHECK and (tx == "" or tx == TableCard.check_outlet_name(index)):
tx = TableCard.check_outlet_name(index)
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 e.effects.strip_edges() != "":
var fx := _label(RulesView.effects_line(e.effects, board, t, plain), 11, Style.TRIM_DARK, true, true)
fx.max_lines_visible = 1
fx.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
col.add_child(fx)
if not e.pipes.is_empty():
col.add_child(_label(NEXT_HINT, 11, Style.TRIM, false))
row.add_child(col)
if t.kind == RollTable.KIND_ROLL:
# The preview doesn't know the modifier's value, so it shows no chances.
var ch := "" if t.has_mod() else 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)
RollTable.KIND_CHECK:
var outs := RulesView.check_outlets(t)
return outs[index] if index < 2 else str(index + 1)
return t.num_label(e, index)
## "25%", "<1%" or "" when the chances can't be known. `mod` is the roll
## modifier's value (RollRange.live_mod), for a table that has one.
static func chance_text(t: RollTable, e: TableEntry, mod: int = 0) -> String:
if not t.has_chances():
return ""
var p := t.chance(e, mod) * 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