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[=] ## 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, ## TrayControls builds the tabs, Copy menu and sheet headline, TrayWhy opens ## the Why pop-over, 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 ## Earlier / Variables on the right (hidden in the phone layout). var side_panel: Control ## Phone layout: shorter labels. var compact := false var _pause_box: VBoxContainer 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 add_theme_stylebox_override("panel", Style.panel_box([16, 14, 16, 16])) 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_BG_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 = TrayControls.copy_menu(main) 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) # Rolls waiting at a paused thread (RollPauses). _pause_box = VBoxContainer.new() now.add_child(_pause_box) # A chain that reached a sheet card leads with the rendered sheet. TrayControls.build_headline(self) # 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_BG_MUTED) now.add_child(_empty) var right := VBoxContainer.new() side_panel = right 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 = TrayControls.tab_button("EARLIER") _tab_earlier.pressed.connect(func() -> void: set_side(Side.EARLIER)) tabs.add_child(_tab_earlier) _tab_vars = TrayControls.tab_button("VARIABLES") _tab_vars.tooltip_text = "Saved variables for this spread" _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 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 render() -> void: for c in _pause_box.get_children(): c.queue_free() if main.rolling.pauses.has_waiting(): _pause_box.add_child(main.rolling.pauses.make_strip()) 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 again" if compact else "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[=]: opens the Why pop-over once after a roll (screenshots). func _open_why_from_flag() -> void: TrayWhy.open_from_flag(self) ## Opens (or, from the same button, closes) the Why pop-over for steps[index]. func open_why(index: int, from: Control = null) -> void: TrayWhy.open(self, index, from) 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)