TableTower/ui/result_tray.gd
RobinFiredrake 14a0b6e6a4 Keep long sheets from widening the tray; show sheets filled in in previews
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>
2026-11-08 17:11:19 -06:00

550 lines
20 KiB
GDScript

class_name ResultTray
extends PanelContainer
## The strip along the bottom: the current roll as a chain of result chips
## joined by brass pipe pieces, a Copy menu, and on the right either the
## earlier rolls (default) or the Variables tab.
enum Side { EARLIER, VARIABLES }
const COPY_TEXT := 0
const COPY_BBCODE := 1
const COPY_DETAILS := 2
var main: Main
var side: int = Side.EARLIER
var _title: Label
var _again: Button
var _copy: MenuButton
var _flow: HFlowContainer
var _empty: Label
var _earlier_box: VBoxContainer
var _earlier: VBoxContainer
var _earlier_empty: Label
var _vars: VariablesPanel
var _tab_earlier: Button
var _tab_vars: Button
var _shown_for: Dictionary = {}
var _shown_count := 0
var _scroll: ScrollContainer
var _halt: PanelContainer
var _halt_label: Label
var _headline: PanelContainer
var _headline_cap: Label
var _headline_text: RichTextLabel
var _headline_scroll: ScrollContainer
const HEADLINE_W := 420.0
const HEADLINE_MAX_H := 220.0
## True when "+N earlier steps" has been opened for the current roll.
var expanded := false
var _scroll_to_end := false
## A long chain shows only its last steps; the rest sit behind a toggle.
const TAIL := 12
## Most chips shown even when expanded (a chain may have thousands).
const MAX_EXPANDED := 300
## The chips area scrolls past this height instead of pushing the board up.
const MAX_FLOW_HEIGHT := 200.0
## Most effect lines listed under one chip.
const EFFECT_LINES := 3
func setup(p_main: Main) -> void:
main = p_main
var sb := Style.box(Style.FELT_DEEP, 0, [16, 14, 16, 16])
sb.border_width_top = 1
sb.border_color = Color(1, 1, 1, 0.08)
add_theme_stylebox_override("panel", sb)
var h := HBoxContainer.new()
h.add_theme_constant_override("separation", 28)
add_child(h)
var now := VBoxContainer.new()
now.add_theme_constant_override("separation", 10)
now.size_flags_horizontal = SIZE_EXPAND_FILL
h.add_child(now)
var head := HBoxContainer.new()
head.add_theme_constant_override("separation", 12)
now.add_child(head)
_title = Style.make_label("RESULT", Style.caps(), 12, Style.ON_FELT_MUTED)
_title.size_flags_vertical = SIZE_SHRINK_CENTER
head.add_child(_title)
_again = Style.bar_button("Roll again")
_again.tooltip_text = "Shift-click to skip the animation"
_again.pressed.connect(func() -> void:
if not main.current.is_empty():
main.start_roll(main.current["root"])
)
head.add_child(_again)
_copy = _make_copy_menu()
head.add_child(_copy)
_halt = PanelContainer.new()
_halt.add_theme_stylebox_override("panel", Style.box(Style.WARN_BG, 8, [10, 3, 10, 4]))
_halt.size_flags_vertical = SIZE_SHRINK_CENTER
_halt_label = Style.make_label("", Style.body_bold(), 14, Style.WARN_INK)
_halt.add_child(_halt_label)
_halt.hide()
head.add_child(_halt)
# A chain that reached a sheet card leads with the rendered sheet.
_headline = PanelContainer.new()
var hsb := Style.box(Style.CARD, 10, [16, 10, 16, 12])
Style.bordered(hsb, 2, Style.BRASS)
_headline.add_theme_stylebox_override("panel", hsb)
_headline.size_flags_horizontal = SIZE_SHRINK_BEGIN
var hv := VBoxContainer.new()
hv.add_theme_constant_override("separation", 2)
_headline.add_child(hv)
_headline_cap = Style.make_label("", Style.body(), 12, Style.MUTED)
hv.add_child(_headline_cap)
# A sheet wraps at a fixed width and scrolls past HEADLINE_MAX_H, so a long
# sheet can't widen the window or take over the tray.
_headline_text = RichText.label("", 17, Style.INK, false, true)
_headline_text.custom_minimum_size = Vector2(HEADLINE_W, 0)
RichText.hook_hover(_headline_text, main)
_headline_scroll = ScrollContainer.new()
_headline_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
_headline_scroll.custom_minimum_size = Vector2(HEADLINE_W + 12, 0)
_headline_scroll.add_child(_headline_text)
hv.add_child(_headline_scroll)
_headline.hide()
_headline.size_flags_vertical = SIZE_SHRINK_BEGIN
# The headline sits left of the chips, so a sheet doesn't make the tray taller.
var body_row := HBoxContainer.new()
body_row.add_theme_constant_override("separation", 14)
now.add_child(body_row)
body_row.add_child(_headline)
_scroll = ScrollContainer.new()
_scroll.size_flags_horizontal = SIZE_EXPAND_FILL
_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
_scroll.custom_minimum_size = Vector2(0, 58)
_flow = HFlowContainer.new()
_flow.add_theme_constant_override("h_separation", 0)
_flow.add_theme_constant_override("v_separation", 8)
_flow.size_flags_horizontal = SIZE_EXPAND_FILL
# Keep the tray's height steady while a new roll's chips arrive.
_flow.custom_minimum_size = Vector2(0, 58)
_flow.minimum_size_changed.connect(_fit_scroll.call_deferred)
_flow.resized.connect(_fit_scroll.call_deferred)
_scroll.add_child(_flow)
body_row.add_child(_scroll)
_empty = Style.make_label("Click a die on any table to roll.", Style.body(), 16, Style.ON_FELT_MUTED)
now.add_child(_empty)
var right := VBoxContainer.new()
right.add_theme_constant_override("separation", 8)
right.custom_minimum_size = Vector2(340, 0)
h.add_child(right)
var tabs := HBoxContainer.new()
tabs.add_theme_constant_override("separation", 14)
right.add_child(tabs)
_tab_earlier = _tab("EARLIER")
_tab_earlier.pressed.connect(func() -> void: set_side(Side.EARLIER))
tabs.add_child(_tab_earlier)
_tab_vars = _tab("VARIABLES")
_tab_vars.tooltip_text = "Saved variables for this collection"
_tab_vars.pressed.connect(func() -> void: set_side(Side.VARIABLES))
tabs.add_child(_tab_vars)
_earlier_box = VBoxContainer.new()
_earlier_box.add_theme_constant_override("separation", 6)
right.add_child(_earlier_box)
_earlier = VBoxContainer.new()
_earlier.add_theme_constant_override("separation", 6)
_earlier_box.add_child(_earlier)
_earlier_empty = Style.make_label("Older rolls show here.", Style.body(), 15, Style.ON_FELT_MUTED)
_earlier_box.add_child(_earlier_empty)
_vars = VariablesPanel.new()
_vars.setup(main)
right.add_child(_vars)
set_side(Side.EARLIER)
func _tab(text: String) -> Button:
var b := Button.new()
b.text = text
b.toggle_mode = true
b.add_theme_font_override("font", Style.caps())
b.add_theme_font_size_override("font_size", 12)
var n := Style.box(Color(0, 0, 0, 0), 0, [0, 0, 0, 3])
var on := Style.box(Color(0, 0, 0, 0), 0, [0, 0, 0, 1])
on.border_width_bottom = 2
on.border_color = Style.BRASS
Style.skin_button(b, n, n, on, Style.ON_FELT_MUTED, Style.ON_FELT, Style.ON_FELT)
b.add_theme_stylebox_override("focus", Style.box(Color(0, 0, 0, 0)))
return b
func set_side(s: int) -> void:
side = s
_tab_earlier.set_pressed_no_signal(s == Side.EARLIER)
_tab_vars.set_pressed_no_signal(s == Side.VARIABLES)
_earlier_box.visible = s == Side.EARLIER
_vars.visible = s == Side.VARIABLES
if s == Side.VARIABLES:
_vars.render()
func _make_copy_menu() -> MenuButton:
var m := MenuButton.new()
m.text = "Copy"
m.tooltip_text = "Copy this result"
m.flat = false
var normal := Style.bordered(Style.box(Color(1, 1, 1, 0.04), 8, [12, 6, 12, 6]), 1, Color(1, 1, 1, 0.16))
var hover := Style.bordered(Style.box(Color(1, 1, 1, 0.1), 8, [12, 6, 12, 6]), 1, Color(1, 1, 1, 0.16))
Style.skin_button(m, normal, hover, hover, Style.ON_FELT)
m.add_theme_font_size_override("font_size", 15)
var pm := m.get_popup()
pm.add_item("Copy as text", COPY_TEXT)
pm.add_item("Copy as BBCode", COPY_BBCODE)
pm.add_item("Copy with details", COPY_DETAILS)
pm.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.CARD, 8, [6, 6, 6, 6]), 1, Style.RULE))
pm.add_theme_stylebox_override("hover", Style.box(Style.LIT, 5, [8, 4, 8, 4]))
pm.add_theme_color_override("font_color", Style.INK)
pm.add_theme_color_override("font_hover_color", Style.INK)
pm.add_theme_font_override("font", Style.body())
pm.add_theme_font_size_override("font_size", 15)
pm.id_pressed.connect(func(id: int) -> void: main.copy_current(id))
return m
func render() -> void:
var current := main.current
var steps: Array = current.get("steps", [])
var fresh_from := 0
if is_same(current, _shown_for):
fresh_from = _shown_count
else:
expanded = false
_shown_for = current
_shown_count = steps.size()
for c in _flow.get_children():
_flow.remove_child(c)
c.queue_free()
_empty.visible = current.is_empty()
_render_status(current, steps)
if current.is_empty():
_title.text = "RESULT"
_again.hide()
_copy.hide()
else:
var root_name := Formatter.strip_tags(str(current["root_name"]))
_title.text = ("Result · %s" % root_name).to_upper()
_again.text = "Roll %s again" % root_name
_again.visible = main.data.get_table(current["root"]) != null
var done: bool = current.get("done", false)
_copy.visible = done
var first := visible_from(steps.size(), expanded)
if first > 0 or expanded and steps.size() > TAIL:
_flow.add_child(_earlier_toggle(first, steps.size()))
for i in range(first, steps.size()):
var step: Dictionary = steps[i]
if i > first:
_flow.add_child(_link(step.get("inline", false)))
var chip := _chip(step, done and i == steps.size() - 1)
_flow.add_child(chip)
if i >= fresh_from and not current.get("fast", false):
chip.modulate.a = 0.3
create_tween().tween_property(chip, "modulate:a", 1.0, 0.25)
var asking := str(current.get("asking", ""))
if asking != "" and not done:
if steps.size() > first:
_flow.add_child(_link(false))
_flow.add_child(_asking_chip(asking))
for c in _earlier.get_children():
_earlier.remove_child(c)
c.queue_free()
_earlier_empty.visible = main.history.is_empty()
for rec in main.history:
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 8)
var time := Style.make_label(clock(rec["time"]), Style.body(), 14, Style.ON_FELT_MUTED)
time.custom_minimum_size = Vector2(44, 0)
time.size_flags_vertical = SIZE_SHRINK_BEGIN
row.add_child(time)
var line := RichText.label(earlier_line(rec), 14, Style.ON_FELT, false)
line.size_flags_horizontal = SIZE_EXPAND_FILL
RichText.hook_hover(line, main)
row.add_child(line)
_earlier.add_child(row)
var n := main.variable_names().size()
_tab_vars.text = "VARIABLES · %d" % n if n > 0 else "VARIABLES"
if side == Side.VARIABLES:
_vars.render()
_scroll_to_end = not expanded
_fit_scroll.call_deferred()
## Index of the first chip shown for a chain of `count` steps: only the last
## TAIL steps of a long chain, or up to MAX_EXPANDED when expanded.
static func visible_from(count: int, is_expanded: bool) -> int:
if count <= TAIL:
return 0
if is_expanded:
return maxi(0, count - MAX_EXPANDED)
return count - TAIL
## One Earlier row: "Root: a → b → c", with only the last few steps of a long
## chain.
static func earlier_line(rec: Dictionary) -> String:
var texts := PackedStringArray()
var all: Array = rec.get("steps", [])
var shown := 0
var skipped := 0
for k in range(all.size() - 1, -1, -1):
var s: Dictionary = all[k]
if s.get("inline", false):
continue
if shown >= 6:
skipped += 1
continue
texts.insert(0, RichText.decorate(_one_line(str(s["text"])), true))
shown += 1
var lead := "[color=#%s]%s:[/color] " % [Style.ON_FELT_MUTED.to_html(false), RichText.plain(Formatter.strip_tags(str(rec.get("root_name", ""))))]
if skipped > 0:
lead += "[color=#%s]… %d earlier →[/color] " % [Style.ON_FELT_MUTED.to_html(false), skipped]
return lead + " → ".join(texts)
## The first line of a (sheet's) multi-line text, with "…" when there's more.
static func _one_line(text: String) -> String:
var nl := text.find("\n")
if nl < 0:
return text
return Formatter.close_open_tags(text.substr(0, nl)) + " …"
## The "Cancelled" / "Stopped after 10,000 actions" pill, and the sheet headline.
func _render_status(current: Dictionary, steps: Array) -> void:
var halted := str(current.get("halted", ""))
if halted == "" and current.get("done", false):
for k in range(steps.size() - 1, maxi(-1, steps.size() - 4), -1):
var s: Dictionary = steps[k]
if s.get("cancelled", false) or s.get("limit", false):
halted = Formatter.strip_tags(str(s.get("text", "")))
break
_halt.visible = halted != ""
if halted != "":
_halt_label.text = ("✕ " if halted == Roller.CANCELLED else "⚠ ") + halted
_halt.tooltip_text = "The roll was cancelled at a question; what came before is kept." if halted == Roller.CANCELLED \
else "This roll took too many steps, so it stopped; everything up to that point is kept."
var sheet := sheet_step(steps) if current.get("done", false) else {}
_headline.visible = not sheet.is_empty()
if not sheet.is_empty():
_headline_cap.text = Formatter.strip_tags(str(sheet.get("table", ""))).to_upper()
_headline_text.text = RichText.decorate(str(sheet.get("text", "")))
_fit_headline()
## The last sheet step of a chain (the headline), or {}.
static func sheet_step(steps: Array) -> Dictionary:
for k in range(steps.size() - 1, -1, -1):
var s: Dictionary = steps[k]
if str(s.get("kind", "")) == RollTable.KIND_SHEET and not s.get("stop", false):
return s
return {}
func _fit_scroll() -> void:
if _scroll == null or _flow == null:
return
var want := _flow.get_combined_minimum_size().y
_scroll.custom_minimum_size.y = clampf(want, 58.0, MAX_FLOW_HEIGHT)
if _scroll_to_end:
# The newest steps (and an open question) are at the end of the chain.
_scroll_to_end = false
await get_tree().process_frame
await get_tree().process_frame
if is_instance_valid(_scroll):
_scroll.scroll_vertical = int(_scroll.get_v_scroll_bar().max_value)
## "+N earlier steps" (or "Hide earlier steps") at the start of a long chain.
func _earlier_toggle(first: int, total: int) -> Control:
var b := Button.new()
if expanded:
b.text = "Hide earlier steps" if first == 0 else "Hide earlier steps (%s not shown)" % _thousands(first)
else:
b.text = "+%s earlier steps" % _thousands(first)
b.tooltip_text = "This chain has %s steps" % _thousands(total)
b.size_flags_vertical = SIZE_SHRINK_CENTER
var n := Style.bordered(Style.box(Color(1, 1, 1, 0.04), 8, [12, 6, 12, 6]), 1, Color(1, 1, 1, 0.16))
var h := Style.bordered(Style.box(Color(1, 1, 1, 0.1), 8, [12, 6, 12, 6]), 1, Color(Style.BRASS, 0.5))
Style.skin_button(b, n, h, h, Style.ON_FELT_MUTED, Style.ON_FELT)
b.add_theme_font_size_override("font_size", 14)
b.pressed.connect(func() -> void:
expanded = not expanded
render()
)
var wrap := HBoxContainer.new()
wrap.add_theme_constant_override("separation", 0)
wrap.add_child(b)
var gap := Control.new()
gap.custom_minimum_size = Vector2(10, 0)
wrap.add_child(gap)
return wrap
static func _thousands(n: int) -> String:
var s := str(n)
var out := ""
while s.length() > 3:
out = "," + s.substr(s.length() - 3) + out
s = s.substr(0, s.length() - 3)
return s + out
## A dashed placeholder chip for the question that is open right now.
func _asking_chip(title: String) -> Control:
var p := PanelContainer.new()
var sb := Style.box(Color(1, 1, 1, 0.05), 9, [12, 7, 12, 8])
Style.bordered(sb, 1, Color(Style.BRASS, 0.6))
p.add_theme_stylebox_override("panel", sb)
p.size_flags_vertical = SIZE_SHRINK_CENTER
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", 1)
p.add_child(v)
v.add_child(Style.make_label(title, Style.body(), 12, Style.ON_FELT_MUTED))
v.add_child(Style.make_label("Waiting for your answer…", Style.display(), 20, Style.BRASS))
return p
## The muted line above a chip's text: table name and how it got its result.
static func caption_for(step: Dictionary) -> String:
var caption := Formatter.strip_tags(str(step["table"]))
var num: String = str(step.get("num", ""))
match str(step.get("kind", "")):
RollTable.KIND_CHOICE:
if step.get("cancelled", false):
pass
elif step.has("answer"):
caption += " · answered"
elif step.get("picked", false):
caption += " · picked"
elif step.get("random", false):
caption += " · random"
if num != "":
caption += ", rolled %s on %s" % [num, step.get("dice", "d%d" % int(step.get("die", 0)))]
RollTable.KIND_LOOP:
if step.has("pass"):
caption += " · pass %s" % str(step["pass"])
elif step.get("after", false):
caption += " · afterwards"
RollTable.KIND_SHEET:
caption += " · sheet"
_:
if num != "":
caption += " · rolled %s on %s" % [num, step.get("dice", "d%d" % int(step.get("die", 0)))]
if step.get("reshuffled", false):
caption += " · deck reshuffled"
if str(step.get("save_as", "")) != "":
caption += " · saved as {%s}" % step["save_as"]
return caption
func _chip(step: Dictionary, last: bool) -> Control:
var inline: bool = step.get("inline", false)
var p := PanelContainer.new()
var sb := Style.box(Style.LIT if last else Style.CARD, 9, [10, 5, 10, 6] if inline else [12, 7, 12, 8])
sb.shadow_color = Color(0, 0, 0, 0.25)
sb.shadow_size = 1
sb.shadow_offset = Vector2(0, 3)
if last:
Style.bordered(sb, 2, Style.BRASS)
sb.set_expand_margin_all(2)
if inline:
sb.bg_color = sb.bg_color.darkened(0.04)
p.add_theme_stylebox_override("panel", sb)
p.size_flags_vertical = SIZE_SHRINK_CENTER
p.mouse_filter = MOUSE_FILTER_PASS
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", 1)
p.add_child(v)
var caption := caption_for(step)
var has_details := main.details().step_has_details(step)
if has_details:
caption += " " + RichText.INFO_MARK
var cap := Style.make_label(caption, Style.body(), 11 if inline else 12, Style.MUTED)
cap.mouse_filter = MOUSE_FILTER_IGNORE
v.add_child(cap)
var raw := str(step["text"])
if str(step.get("kind", "")) == RollTable.KIND_SHEET:
raw = _one_line(raw)
if step.has("answer") and Formatter.strip_tags(raw).strip_edges() == "":
raw = "[i]no answer[/i]"
var shown := RichText.decorate(raw)
var body := RichText.display_label(shown, 16 if inline else 20, Style.DANGER_INK if step.get("stop", false) else Style.INK)
if Formatter.strip_tags(shown).length() > 56:
body.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
body.custom_minimum_size = Vector2(420, 0)
RichText.hook_hover(body, main)
v.add_child(body)
var fx: Array = step.get("effects", [])
if not fx.is_empty():
v.add_child(_effect_lines(fx))
if has_details:
p.mouse_entered.connect(func() -> void: main.details().show_step(p, step))
return p
## The effect log under a chip: a few compact lines, errors in red.
func _effect_lines(fx: Array) -> Control:
var box := VBoxContainer.new()
box.add_theme_constant_override("separation", 0)
box.mouse_filter = MOUSE_FILTER_IGNORE
var shown := 0
var errors := 0
for l: Variant in fx:
var d: Dictionary = l if l is Dictionary else {"text": str(l)}
var bad: bool = d.get("error", false)
if bad:
errors += 1
# Errors are always listed; ordinary lines up to EFFECT_LINES.
if shown >= EFFECT_LINES and not bad:
continue
var lab := Style.make_label(("⚠ " if bad else "") + str(d.get("text", "")), Style.body_bold() if bad else Style.body(), 12,
Style.DANGER_INK if bad else Style.COPPER_DARK)
lab.mouse_filter = MOUSE_FILTER_IGNORE
lab.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
lab.custom_minimum_size.x = 0
box.add_child(lab)
shown += 1
var more := fx.size() - shown
if more > 0:
box.add_child(Style.make_label("+%d more" % more, Style.body(), 11, Style.MUTED))
var tip := Effects.log_text(fx)
box.tooltip_text = "\n".join(tip)
return box
func _link(inline: bool = false) -> Control:
var link := Panel.new()
var sb := Style.box(Style.BRASS)
sb.border_width_top = 2
sb.border_width_bottom = 2
sb.border_color = Style.BRASS_DARK
if inline:
sb = Style.box(Color(Style.BRASS, 0.55))
link.add_theme_stylebox_override("panel", sb)
link.custom_minimum_size = Vector2(16, 4) if inline else Vector2(22, 8)
link.size_flags_vertical = SIZE_SHRINK_CENTER
link.mouse_filter = MOUSE_FILTER_IGNORE
return link
static func clock(unix: float) -> String:
var bias: int = Time.get_time_zone_from_system().get("bias", 0)
var d := Time.get_datetime_dict_from_unix_time(int(unix) + bias * 60)
return "%02d:%02d" % [d["hour"], d["minute"]]
## Sizes the sheet headline's scroll area to its text, up to HEADLINE_MAX_H.
func _fit_headline() -> void:
await get_tree().process_frame
if is_instance_valid(_headline_text):
_headline_scroll.custom_minimum_size.y = minf(_headline_text.get_content_height(), HEADLINE_MAX_H)