- RulesView (ui/rules_view.gd): plain-or-text helpers over Describe, reading
Main.rules_plain() when present (else StateStore, else plain; --rules=text
for screenshots) and refreshing on Main.rules_view_changed.
- Cards: effect chips, router cases, option sources, show_if, loop rules,
expression dice and variable kinds read in plain words. Build cards drop the
inline effects / show_if / condition / details editors for plain chips;
result text and names stay inline. Row clicks call main.select_result();
the selected row gets a copper bar (set_selected_entry / inspector_selection).
- Check cards: 2D badge, the rule as a sentence, Success / Failure outlets.
Collection cards: live members and "+ Add" (main.select_card). Inputs:
a dashed "Input Tier · starts at 1" box.
- Pipe labels via Describe.pipe, nudged apart and cut to the gap.
- Tray: plain captions (inputs, checks, cases), effect lines from why facts,
and a "Why this?" button opening a WhyPopover; hovers show the same reasons.
- Previews and the choice pop-up read conditions, effects and option sources
in plain words. Style.rules_switch() for the Plain words / Text switch.
- Describe: empty text reads "empty" ("Loot is not empty").
- tests/test_plain_display.gd; option and choice tests pin the Text view
where they check stored text.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
712 lines
26 KiB
GDScript
712 lines
26 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.
|
|
|
|
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
|
|
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 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)
|
|
_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_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()
|
|
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(_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, 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(_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()
|
|
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
|
|
|
|
|
|
## 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.
|
|
## With `plain`, rules read in plain words ("Loot · Tier 1 · rolled 4 on 1D +
|
|
## Tier", "Force the gate · rolled 7, +1 → 8"), labelled from `board`.
|
|
static func caption_for(step: Dictionary, board: TowerBoard = null, plain: bool = false) -> String:
|
|
var caption := Formatter.strip_tags(str(step["table"]))
|
|
var num: String = str(step.get("num", ""))
|
|
var ins: Variant = step.get("inputs")
|
|
if ins is Dictionary:
|
|
for nm: Variant in ins:
|
|
if plain:
|
|
caption += " · %s %s" % [Describe.label(str(nm), board), Expr.format_value(ins[nm])]
|
|
else:
|
|
caption += " · %s=%s" % [nm, Expr.format_value(ins[nm])]
|
|
match str(step.get("kind", "")):
|
|
RollTable.KIND_CHECK:
|
|
var c: Variant = step.get("check")
|
|
if c is Dictionary:
|
|
var mod := int(c.get("mod", 0))
|
|
var ms := ""
|
|
if mod != 0:
|
|
ms = ("+%d" % mod) if mod > 0 else (Describe.MINUS + str(-mod))
|
|
if plain:
|
|
caption += " · rolled %s%s → %s" % [Expr.format_value(c.get("nat")), (", " + ms) if ms != "" else "", Expr.format_value(c.get("total"))]
|
|
else:
|
|
caption += " · %s%s = %s vs %s" % [Expr.format_value(c.get("nat")), ms.replace(Describe.MINUS, "-"), Expr.format_value(c.get("total")), Expr.format_value(c.get("target"))]
|
|
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"
|
|
RollTable.KIND_ROUTER:
|
|
if step.has("case"):
|
|
var said := false
|
|
if plain and board != null:
|
|
var rt := board.get_table(str(step.get("table_id", "")))
|
|
var k := int(step["case"])
|
|
if rt != null and k >= 0 and k < rt.entries.size():
|
|
caption += " · " + RulesView.cut(RulesView.case_label(rt.entries[k], board, true), 44)
|
|
said = true
|
|
if not said:
|
|
caption += " · case %d" % (int(step["case"]) + 1)
|
|
_:
|
|
if num != "":
|
|
var dl := str(step.get("dice", "d%d" % int(step.get("die", 0))))
|
|
if plain and dl.contains(" "):
|
|
dl = RulesView.cut(Describe.dice(dl, board), 30)
|
|
caption += " · rolled %s on %s" % [num, dl]
|
|
if step.get("reshuffled", false):
|
|
caption += " · deck reshuffled"
|
|
if str(step.get("save_as", "")) != "":
|
|
if plain:
|
|
caption += " · saved as %s" % Describe.label(str(step["save_as"]), board)
|
|
else:
|
|
caption += " · saved as {%s}" % step["save_as"]
|
|
return caption
|
|
|
|
|
|
func _chip(step: Dictionary, last: bool, steps: Array = [], index: int = -1) -> 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 plain := RulesView.plain(main)
|
|
var caption := caption_for(step, main.data, plain)
|
|
var has_why := index >= 0 and WhyPopover.has_why(steps, index)
|
|
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(effect_lines_for(step, main.data, plain)))
|
|
if has_why:
|
|
var wb := _why_button()
|
|
wb.pressed.connect(func() -> void: open_why(index, wb))
|
|
why_buttons[index] = wb
|
|
v.add_child(wb)
|
|
if has_details or has_why:
|
|
p.mouse_entered.connect(func() -> void:
|
|
if not _why.visible:
|
|
main.details().show_step(p, step, steps, index))
|
|
return p
|
|
|
|
|
|
## The small "Why this?" pill under a chip.
|
|
func _why_button() -> Button:
|
|
var b := Button.new()
|
|
b.text = "Why this?"
|
|
b.tooltip_text = "What decided this step"
|
|
b.size_flags_horizontal = SIZE_SHRINK_BEGIN
|
|
b.focus_mode = Control.FOCUS_NONE
|
|
var n := Style.bordered(Style.box(Color.WHITE, 10, [9, 1, 9, 2]), 1, Style.RULE)
|
|
var h := Style.bordered(Style.box(Style.LIT, 10, [9, 1, 9, 2]), 1, Style.BRASS_DARK)
|
|
Style.skin_button(b, n, h, h, Style.COPPER_DARK, Style.INK)
|
|
b.add_theme_font_override("font", Style.body_bold())
|
|
b.add_theme_font_size_override("font_size", 12)
|
|
return b
|
|
|
|
|
|
## A step's effect lines for its chip: in plain words, one per value that
|
|
## changed ("Luck 2 → 1", "Town = Millbrook", "Cleared all Skills (3)") from
|
|
## its why facts, then any errors from its log; else the log as it is.
|
|
static func effect_lines_for(step: Dictionary, board: TowerBoard, plain: bool) -> Array:
|
|
var fx: Array = step.get("effects", [])
|
|
if not plain or not (step.get("why") is Array):
|
|
return fx
|
|
var out: Array = []
|
|
for f: Variant in step["why"]:
|
|
if f is Dictionary and str(f.get("type", "")) == "effect":
|
|
out.append({"text": effect_fact_line(f, board)})
|
|
if out.is_empty():
|
|
return fx
|
|
for l: Variant in fx:
|
|
if l is Dictionary and (l as Dictionary).get("error", false):
|
|
out.append(l)
|
|
return out
|
|
|
|
|
|
## "Luck 2 → 1", "Town = Millbrook", "Loot cleared", "Cleared all Skills (3)".
|
|
static func effect_fact_line(f: Dictionary, board: TowerBoard) -> String:
|
|
var lab := Describe.label(str(f.get("var", "")), board)
|
|
if f.has("count"):
|
|
return "Cleared all %s (%s)" % [lab, Expr.format_value(f["count"])]
|
|
var before: Variant = f.get("before")
|
|
var after: Variant = f.get("after")
|
|
if after == null:
|
|
return "%s cleared" % lab
|
|
var a := Expr.format_value(after)
|
|
if before == null or Expr.format_value(before) == "":
|
|
return "%s = %s" % [lab, a if a != "" else "(none)"]
|
|
return "%s %s → %s" % [lab, Expr.format_value(before), a if a != "" else "(none)"]
|
|
|
|
|
|
## 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)
|