The tray's sheet headline now wraps at a fixed width and scrolls past 220 px, so a long character sheet can't stretch the window (which also threw off Fit on big boards). Option previews in the choice pop-up show a sheet card rendered with current values instead of its raw template and a meaningless 100%. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
181 lines
6.6 KiB
GDScript
181 lines
6.6 KiB
GDScript
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"
|
||
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)"
|
||
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]
|
||
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
|
||
var tx := Formatter.strip_tags(e.text).strip_edges()
|
||
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:
|
||
return str(index + 1) if t.choice == "pick" else "Answer"
|
||
RollTable.KIND_LOOP:
|
||
return "Each" if index == 0 else "After"
|
||
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
|