TableTower/ui/result_tray.gd
RobinFiredrake 33913889da Roll modifiers and resizable tray/inspector
- Ranged roll tables take a modifier (dice_mod, e.g. @luck). Ranges may run
  past the dice (-3 to 24 on a d20); totals past the ends use the end result.
- Range and chance code moved from RollTable into RollRange; chances follow
  the modifier's current value; plain wording and Why lines name it.
- ModifierEditor builds modifiers in plain words for roll and check cards;
  the inspector's roll section is now InspectorRoll.
- The Earlier tab scrolls; the tray and inspector resize with drag handles
  (PanelSplitter), sizes kept in local settings.
- Demo Room check uses 2d6 + Luck with a luck-only vault; new test_modifier
  suite; docs updated.

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

395 lines
14 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.
##
## Captions and effect lines read in plain words ("Loot · Tier 1 · rolled 3",
## "Luck 2 → 1") or closer to the rule text, following RulesView.plain(main);
## the tray re-renders on Main's rules_view_changed. A chip whose step has
## why-trace facts gets a "Why this?" button that opens a WhyPopover; hovering
## the chip shows the same reasons. Screenshot flag: --why[=<step index>]
## opens the pop-over for that step (default: the last one with reasons)
## after a roll.
##
## Pieces: TrayChips builds the chips and links, TrayText words them, and
## EarlierRolls is the Earlier tab.
##
## The tray keeps the height the player drags it to (a PanelSplitter above it,
## setting "tray_height"); the chips, the sheet, Earlier and Variables each
## scroll inside it.
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: EarlierRolls
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
## True when "+N earlier steps" has been opened for the current roll.
var expanded := false
var _scroll_to_end := false
var _why: WhyPopover
## The "Why this?" buttons of the chips shown, by step index (tests).
var why_buttons: Dictionary = {}
var _why_flag_done := 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 tray's default and smallest heights (it's resizable).
const HEIGHT := 230.0
const MIN_HEIGHT := 120.0
## Room the tray's padding, head row and gaps take from its height.
const CHROME_H := 84.0
var height := HEIGHT
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
now.size_flags_vertical = 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 inside the tray, 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)
body_row.size_flags_vertical = SIZE_EXPAND_FILL
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.size_flags_vertical = SIZE_EXPAND_FILL
_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
_flow.minimum_size_changed.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)
right.size_flags_vertical = SIZE_EXPAND_FILL
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 = EarlierRolls.new()
_earlier.setup(main)
right.add_child(_earlier)
_vars = VariablesPanel.new()
_vars.setup(main)
right.add_child(_vars)
set_side(Side.EARLIER)
_why = WhyPopover.new()
add_child(_why)
RulesView.watch(main, _on_rules_view_changed)
func _on_rules_view_changed(_v: Variant = null) -> void:
_why.close()
render()
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.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()
why_buttons.clear()
if _why != null:
_why.close()
_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(TrayChips.earlier_toggle(self, first, steps.size()))
for i in range(first, steps.size()):
var step: Dictionary = steps[i]
if i > first:
_flow.add_child(TrayChips.link(step.get("inline", false)))
var chip := TrayChips.chip(self, step, done and i == steps.size() - 1, steps, i)
_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(TrayChips.link(false))
_flow.add_child(TrayChips.asking_chip(asking))
_earlier.render()
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()
if not _why_flag_done and current.get("done", false):
_open_why_from_flag.call_deferred()
## --why[=<step index>]: opens the Why pop-over once after a roll (screenshots).
func _open_why_from_flag() -> void:
for a in OS.get_cmdline_user_args():
if a == "--why" or a.begins_with("--why="):
_why_flag_done = true
await get_tree().create_timer(0.4).timeout
if not main.current.get("done", false):
_why_flag_done = false # a new roll began; wait for it to end
return
var at := -1
if a == "--why":
for k: int in why_buttons:
at = maxi(at, k)
else:
at = a.get_slice("=", 1).to_int()
if why_buttons.has(at):
(why_buttons[at] as Button).emit_signal("pressed")
return
## Opens (or, from the same button, closes) the Why pop-over for steps[index].
func open_why(index: int, from: Control = null) -> void:
if _why.visible and from != null and _why.opener == from:
_why.close()
return
var steps: Array = main.current.get("steps", [])
if index < 0 or index >= steps.size():
return
main.details().hide_for()
var at := Rect2(global_position + Vector2(20, 20), Vector2.ZERO)
var chip: Control = null
if from != null:
# Above the whole chip, so it never hides the step it explains.
chip = from.get_parent().get_parent() as Control
at = chip.get_global_rect() if chip != null else from.get_global_rect()
_why.open(steps, index, main.data, RulesView.plain(main), at, from, chip)
func why_popover() -> WhyPopover:
return _why
## 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
## 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 := TrayText.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()
## Sets the tray's height (from its PanelSplitter); the parts inside scroll.
func set_height(px: float) -> void:
height = px
custom_minimum_size.y = px
_fit_headline()
## Scrolls the chips to the newest step after a roll.
func _fit_scroll() -> void:
if _scroll == null or _flow == null:
return
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)
## Sizes the sheet headline's scroll area to its text, up to what the
## tray's height leaves room for.
func _fit_headline() -> void:
if not is_inside_tree():
return
await get_tree().process_frame
if is_instance_valid(_headline_text):
var room := maxf(40.0, height - CHROME_H - 30.0)
_headline_scroll.custom_minimum_size.y = minf(_headline_text.get_content_height(), room)