From 6e69b64385abe586be9bb08110962350b81b264a Mon Sep 17 00:00:00 2001 From: RobinFiredrake Date: Sun, 8 Nov 2026 16:39:56 -0600 Subject: [PATCH] Choice pop-up: minimize pill, option preview panel, reusable TablePreview Co-Authored-By: Claude Sonnet 5.5 --- tests/test_choice_dialog.gd | 282 +++++++++++++++++++++++ tests/test_choice_dialog.gd.uid | 1 + ui/choice_dialog.gd | 394 +++++++++++++++++++++++++++++++- ui/main.gd | 16 ++ ui/table_preview.gd | 174 ++++++++++++++ ui/table_preview.gd.uid | 1 + 6 files changed, 863 insertions(+), 5 deletions(-) create mode 100644 tests/test_choice_dialog.gd create mode 100644 tests/test_choice_dialog.gd.uid create mode 100644 ui/table_preview.gd create mode 100644 ui/table_preview.gd.uid diff --git a/tests/test_choice_dialog.gd b/tests/test_choice_dialog.gd new file mode 100644 index 0000000..e098775 --- /dev/null +++ b/tests/test_choice_dialog.gd @@ -0,0 +1,282 @@ +extends SceneTree +## The choice pop-up's minimize pill and hover preview, and TablePreview. +## Run: godot --headless --path . -s res://tests/test_choice_dialog.gd -- --sample + +var _failures := 0 +var main: Main + + +func _init() -> void: + if not OS.get_cmdline_user_args().has("--sample"): + print("Run with -- --sample so the test can't touch saved data.") + quit(2) + return + + main = load("res://ui/main.tscn").instantiate() + root.add_child(main) + for i in 5: + await process_frame + if main._saving_enabled: + print("Saving is enabled; refusing to roll.") + quit(2) + return + # Headless windows are tiny; give the app a real size. + main.set_anchors_preset(Control.PRESET_TOP_LEFT) + main.size = Vector2(1280, 800) + main.rng.seed = 5 + main.prompt_hook = main._ask_player + _add_tables() + await process_frame + + _test_table_preview() + await _test_preview_content() + await _test_keyboard_focus() + await _test_minimize() + await _test_waiting_question() + await _test_cancel_from_pill() + await _test_state_kept() + + print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) + quit(1 if _failures else 0) + + +func _add_tables() -> void: + var ask := RollTable.create(RollTable.KIND_CHOICE, "t_ask", "Where to?", "pick") + ask.prompt = "Where do you go?" + ask.entries.clear() + var a := TableEntry.new("a", "Visit the [[Bazaar]]") + a.details = "A **busy** place" + a.effects = "@gold -= 5" + a.add_pipe("t_loot", 2) + var b := TableEntry.new("b", "Take the fork") + b.add_pipe("t_fork", "@terms") + var c := TableEntry.new("c", "Stay put") + ask.entries.append_array([a, b, c] as Array[TableEntry]) + + var loot := RollTable.create(RollTable.KIND_ROLL, "t_loot", "Loot", "") + loot.dice = "d6" + loot.details = "What the stalls sell" + loot.entries.clear() + for spec in [[1, 2, "Trinket"], [3, 5, "Coin"], [6, 6, "Relic"]]: + var e := TableEntry.new("l%d" % spec[0], spec[2]) + e.has_range = true + e.range_lo = spec[0] + e.range_hi = spec[1] + loot.entries.append(e) + loot.entries[2].add_pipe("t_big") + + var fork := RollTable.create(RollTable.KIND_CHOICE, "t_fork", "Which way?", "pick") + fork.prompt = "Left or right?" + var bazaar := RollTable.create(RollTable.KIND_ROLL, "t_bazaar", "Bazaar", "") + var big := RollTable.create(RollTable.KIND_ROLL, "t_big", "Big table", "") + big.entries.clear() + for n in 15: + big.entries.append(TableEntry.new("g%d" % n, "Thing %d" % n)) + for t in [ask, loot, fork, bazaar, big]: + main.data.tables.append(t) + main._rebuild_cards() + + +## All the text in a control tree. +func _texts(n: Node, out: PackedStringArray = PackedStringArray()) -> PackedStringArray: + if n is Label: + out.append((n as Label).text) + elif n is RichTextLabel: + out.append((n as RichTextLabel).text) + for c in n.get_children(): + _texts(c, out) + return out + + +func _has(n: Node, needle: String) -> bool: + for s in _texts(n): + if s.contains(needle): + return true + return false + + +func _ask() -> ChoiceDialog: + main.start_roll("t_ask") + await process_frame + await process_frame + return main.pending_choice() + + +func _key(code: Key, ctrl: bool = false) -> InputEventKey: + var ev := InputEventKey.new() + ev.keycode = code + ev.pressed = true + ev.ctrl_pressed = ctrl + return ev + + +# ---------------------------------------------------------------- TablePreview + +func _test_table_preview() -> void: + var p := TablePreview.build(main.data, "t_big") + _check(_has(p, "Big table") and _has(p, "Roll table · d15"), "the preview names the table, its kind and dice") + _check(_has(p, "Thing 11") and not _has(p, "Thing 12"), "the preview shows 12 rows") + _check(_has(p, "+3 more"), "the row cap says how many more there are") + _check(_has(p, "7%"), "plain tables show chances") + p.free() + var m := TablePreview.build(main.data, "nope") + _check(_has(m, "Missing"), "a missing table says so") + m.free() + + +# ---------------------------------------------------------------- Preview content + +func _test_preview_content() -> void: + var d := await _ask() + _check(d != null and d.kind == "pick", "the pick question opens") + if d == null: + return + _check(d.board == main.data, "the dialog finds the board") + # Option 1 pipes to a ranged table, twice. + var p := d.preview_for_option(0) + _check(p != null, "an option that pipes on has a preview") + _check(_has(p, "A **busy** place") or _has(p, "busy"), "the preview shows the option's details") + _check(_has(p, "@gold -= 5"), "the preview lists the option's effects") + _check(_has(p, "Loot") and _has(p, "d6"), "the piped table's name and dice show") + _check(_has(p, "×2"), "the pipe count shows") + _check(_has(p, "What the stalls sell"), "the piped table's details show") + _check(_has(p, "1–2") and _has(p, "3–5") and _has(p, "33%"), "ranged entries show their range and chance") + _check(_has(p, TablePreview.NEXT_HINT), "an entry that leads further gets a next-table hint") + # The text mentions [[Bazaar]]. + _check(_has(p, "MENTIONED IN THE TEXT") and _has(p, "Bazaar"), "a table named inline in the text is previewed") + p.free() + # Option 2 pipes to a choice table with an expression count. + var q := d.preview_for_option(1) + _check(_has(q, "Which way?") and _has(q, "Choice card"), "a choice target shows its kind") + _check(_has(q, "Asks: Left or right?"), "a choice target shows its question") + _check(_has(q, "×@terms"), "an expression pipe count shows") + q.free() + _check(d.preview_for_option(2) == null, "an option with nothing to show has no preview") + main.cancel_choice() + await process_frame + + +func _test_keyboard_focus() -> void: + var d := await _ask() + if d == null: + _check(false, "the question opens for the focus test") + return + d._options[0].grab_focus() + _check(d.preview_panel.visible and _has(d.preview_panel, "Loot"), "focusing an option shows its preview") + var vr := d.get_global_rect() + var cr := d.card.get_global_rect() + var pr := d.preview_panel.get_global_rect() + _check(not pr.intersects(cr) and vr.encloses(pr), "the preview sits beside the card, on screen") + d._options[1].grab_focus() + _check(_has(d.preview_panel, "Which way?") and not _has(d.preview_panel, "Loot"), "moving focus changes the preview") + d._options[2].grab_focus() + await process_frame + _check(not d.preview_panel.visible, "an option with nothing to show hides the panel") + main.cancel_choice() + await process_frame + + +# ---------------------------------------------------------------- Minimize + +func _test_minimize() -> void: + var d := await _ask() + if d == null: + _check(false, "the question opens for the minimize test") + return + d._options[1].grab_focus() + d._input(_key(KEY_M)) + _check(d.minimized and not d.card.is_visible_in_tree(), "M hides the dialog") + _check(d.pill.visible and _has(d.pill, "Question waiting: Where to?"), "the pill names the question") + _check(d.mouse_filter == Control.MOUSE_FILTER_IGNORE, "the board gets the mouse while minimized") + _check(not d._dim.visible, "the dim goes away") + _check(not d.preview_panel.visible, "the preview hides") + _check(not d.reply.finished and main.pending_choice() == d, "the question is still waiting") + d._input(_key(KEY_M)) + _check(not d.minimized and d.card.is_visible_in_tree() and not d.pill.visible, "M again restores it") + _check(d._options[1].has_focus(), "the focused option is focused again") + d._input(_key(KEY_TAB)) + _check(d.minimized, "Tab minimizes too") + d._input(_key(KEY_1)) + _check(not d.reply.finished, "number keys do nothing while minimized") + d._input(_key(KEY_ESCAPE)) + _check(not d.reply.finished, "Esc does nothing while minimized") + d._input(_key(KEY_TAB)) + _check(not d.minimized, "Tab restores") + main.cancel_choice() + await process_frame + + +func _test_waiting_question() -> void: + var d := await _ask() + if d == null: + _check(false, "the question opens for the waiting test") + return + var token: int = main._token + d.minimize() + main.start_roll("t_loot") + await process_frame + _check(main.pending_choice() == d and not d.reply.finished and main._token == token, "a die click doesn't cancel the waiting question") + _check(d.toast_panel.visible and _has(d.toast_panel, "Answer the waiting question first"), "a toast asks for the answer first") + var has_show := false + for c in (d.toast_panel.get_child(0) as Node).get_children(): + has_show = has_show or (c is Button and (c as Button).text == "Show") + _check(has_show, "the toast has a Show button") + main.set_mode(Main.Mode.BUILD) + _check(main.mode == Main.Mode.PLAY, "Build is refused while a question waits") + d.restore() + _check(not d.toast_panel.visible, "restoring hides the toast") + main.set_mode(Main.Mode.BUILD) + _check(main.mode == Main.Mode.BUILD, "Build is a normal mode switch again") + main.set_mode(Main.Mode.PLAY) + main.cancel_choice() + await process_frame + + +func _test_cancel_from_pill() -> void: + var d := await _ask() + if d == null: + _check(false, "the question opens for the cancel test") + return + d.minimize() + var x: Button + for c in (d.pill.get_child(0) as Node).get_children(): + if c is Button and (c as Button).text == "Cancel": + x = c + _check(x != null, "the pill has a cancel button") + var got: Array = ["unset"] + d.reply.done.connect(func(v: Variant) -> void: got[0] = v) + x.pressed.emit() + _check(got[0] == null and d.reply.finished, "the pill's Cancel answers Cancel") + await process_frame + await process_frame + _check(main.pending_choice() == null, "the dialog is gone") + # A roll can start again. + main.cancel_choice() + main.start_roll("t_ask") + await process_frame + await process_frame + _check(main.pending_choice() != null, "a new roll can start after cancelling") + main.cancel_choice() + await process_frame + + +func _test_state_kept() -> void: + var d := ChoiceDialog.new({"table_id": "x", "kind": "number", "title": "How many?", "prompt": "", "min": 0, "max": 20, "default": 3}) + main.add_child(d) + await process_frame + d._spin.value = 9 + d._spin.get_line_edit().grab_focus() + d._input(_key(KEY_M)) + _check(not d.minimized, "plain M in a number field types instead of minimizing") + d._input(_key(KEY_M, true)) + _check(d.minimized, "Ctrl+M minimizes from a field") + d._input(_key(KEY_M, true)) + _check(not d.minimized and int(d._spin.value) == 9, "the typed value survives minimize and restore") + d.answer(null) + await process_frame + + +func _check(ok: bool, what: String) -> void: + if not ok: + _failures += 1 + print(" ✗ ", what) diff --git a/tests/test_choice_dialog.gd.uid b/tests/test_choice_dialog.gd.uid new file mode 100644 index 0000000..c4b9ac7 --- /dev/null +++ b/tests/test_choice_dialog.gd.uid @@ -0,0 +1 @@ +uid://dru2fl6w14c7y diff --git a/ui/choice_dialog.gd b/ui/choice_dialog.gd index 46d07f8..873ecf6 100644 --- a/ui/choice_dialog.gd +++ b/ui/choice_dialog.gd @@ -4,7 +4,18 @@ extends ModalCard ## question and either its options (pick), a number field (number) or a text ## field (text), with Cancel. ## -## Keys: 1–9 pick an option, R picks Random, Enter confirms, Esc cancels. +## Keys: 1–9 pick an option, R picks Random, Enter confirms, Esc cancels, +## M or Tab (Ctrl+M inside a text field) minimizes or restores. Arrow keys +## move between options. +## +## Minimize hides the dialog and its dim so the board can be looked at and +## used; a pill at the bottom centre ("Question waiting") brings it back +## exactly as it was. While it waits, Main refuses to start another roll or +## switch to Build (see nudge()). +## +## Hovering or focusing an option shows a preview panel beside the card: the +## option's details and effects, and a TablePreview for every table it pipes +## to or mentions. Number and text questions preview where the Answer leads. ## ## The answer arrives through `reply.done`, a signal on a separate object so ## the waiting roll never hangs: closing or freeing the dialog in any way @@ -28,6 +39,23 @@ var request: Dictionary var reply := Reply.new() var kind := "pick" +const PREVIEW_WIDTH := 350.0 + +## The board the question belongs to, for previews. Found from the parent Main +## when not set; without one the dialog simply shows no preview. +var board: TowerBoard +var minimized := false +var preview_panel: PanelContainer +var pill: PanelContainer +var toast_panel: PanelContainer + +var _center: Control +var _dim: ColorRect +var _preview_box: VBoxContainer +var _preview_for := -2 +var _hover := -1 +var _prev_focus: Control +var _toast_timer: Timer var _spin: SpinBox var _text: LineEdit var _options: Array[Control] = [] @@ -43,10 +71,12 @@ func _init(p_request: Dictionary = {}) -> void: # A stray click on the board behind shouldn't throw the answer away. close_on_backdrop = false # A lighter dim, so the steps that led here stay readable behind the card. - var dim := get_child(0) as ColorRect - if dim: - dim.color = Color(0, 0, 0, 0.32) + _dim = get_child(0) as ColorRect + _center = get_child(1) as Control + if _dim: + _dim.color = Color(0, 0, 0, 0.32) _build() + _build_chrome() func _build() -> void: @@ -74,7 +104,7 @@ func _build_pick() -> void: var row := _option_row(k, o) list.add_child(row) _options.append(row) - var hint := note("Press 1–%d to choose, R for Random, Esc to cancel." % mini(9, maxi(1, opts.size()))) + var hint := note("Press 1–%d to choose, R for Random, M to look at the board, Esc to cancel." % mini(9, maxi(1, opts.size()))) hint.add_theme_font_size_override("font_size", 13) body.add_child(hint) add_button("Cancel", "plain", func() -> void: answer(null)) @@ -130,6 +160,13 @@ func _option_row(k: int, o: Dictionary) -> Control: fit.call_deferred() var index := int(o.get("index", k)) b.pressed.connect(func() -> void: answer(index)) + b.mouse_entered.connect(func() -> void: _set_hover(k)) + b.mouse_exited.connect(func() -> void: + if _hover == k: + _set_hover(-1) + ) + b.focus_entered.connect(_refresh_preview) + b.focus_exited.connect(_refresh_preview.call_deferred) return b @@ -181,6 +218,11 @@ func _build_text() -> void: func _ready() -> void: super._ready() + if board == null: + var m := get_parent() as Main + if m: + board = m.data + _refresh_preview() if _spin: _spin.get_line_edit().grab_focus() _spin.get_line_edit().select_all() @@ -188,6 +230,14 @@ func _ready() -> void: _text.grab_focus() elif not _options.is_empty(): _options[0].grab_focus() + # Screenshot flags: --choice-focus= focuses option n, --choice-minimized. + for a in OS.get_cmdline_user_args(): + if a.begins_with("--choice-focus="): + var n := a.get_slice("=", 1).to_int() + if n >= 0 and n < _options.size(): + _options[n].grab_focus() + elif a == "--choice-minimized": + minimize.call_deferred() ## Enter on the number or text field, or OK. @@ -216,6 +266,12 @@ func _input(event: InputEvent) -> void: if not is_inside_tree(): return var k := event as InputEventKey + if k and k.pressed and not k.echo and _is_minimize_key(k): + get_viewport().set_input_as_handled() + toggle_minimized() + return + if minimized: + return if k and k.pressed and not k.echo and kind == "pick": var code := k.keycode if code >= KEY_1 and code <= KEY_9: @@ -241,3 +297,331 @@ func _notification(what: int) -> void: # Freed or taken off the screen without an answer: the roll gets Cancel. if what == NOTIFICATION_EXIT_TREE or what == NOTIFICATION_PREDELETE: reply.finish(null) + + +# ---------- Minimize ---------- + +func _is_minimize_key(k: InputEventKey) -> bool: + var code := k.keycode + if code != KEY_M and code != KEY_TAB: + return false + if k.alt_pressed or (code == KEY_TAB and (k.ctrl_pressed or k.meta_pressed)): + return false + if k.ctrl_pressed or k.meta_pressed: + return code == KEY_M + if k.shift_pressed and code == KEY_M: + return false + # Plain keys type into text fields, so they only count outside of them. + var f := get_viewport().gui_get_focus_owner() + return not (f is LineEdit or f is TextEdit) + + +func toggle_minimized() -> void: + if minimized: + restore() + else: + minimize() + + +## Hides the dialog (answers stay as typed) and shows the pill. +func minimize() -> void: + if minimized or not is_inside_tree(): + return + minimized = true + var f := get_viewport().gui_get_focus_owner() + _prev_focus = f if f != null and card.is_ancestor_of(f) else null + if f != null: + f.release_focus() + _center.hide() + if _dim: + _dim.hide() + _hide_preview() + mouse_filter = Control.MOUSE_FILTER_IGNORE + pill.show() + _place_pill() + + +## Brings the dialog back exactly as it was. +func restore() -> void: + if not minimized: + return + minimized = false + pill.hide() + toast_panel.hide() + mouse_filter = Control.MOUSE_FILTER_STOP + _center.show() + if _dim: + _dim.show() + _fit.call_deferred() + if is_instance_valid(_prev_focus) and _prev_focus.is_visible_in_tree(): + _prev_focus.grab_focus() + elif _spin: + _spin.get_line_edit().grab_focus() + elif _text: + _text.grab_focus() + elif not _options.is_empty(): + _options[0].grab_focus() + _refresh_preview() + + +## Tells the player a question is waiting (they tried to start a roll or enter +## Build): a toast above the pill with a Show button. Does nothing when the +## dialog is already on screen. +func nudge(message: String = "Answer the waiting question first") -> void: + if not minimized: + return + (toast_panel.get_meta("label") as Label).text = message + toast_panel.show() + toast_panel.reset_size() + _place_pill() + _toast_timer.start() + + +func _build_chrome() -> void: + preview_panel = PanelContainer.new() + preview_panel.mouse_filter = Control.MOUSE_FILTER_IGNORE + preview_panel.clip_contents = true + var sb := Style.box(Style.CARD, 10, [14, 11, 14, 12]) + sb.border_width_top = 3 + sb.border_color = Style.COPPER + sb.shadow_color = Color(0, 0, 0, 0.4) + sb.shadow_size = 16 + sb.shadow_offset = Vector2(0, 6) + preview_panel.add_theme_stylebox_override("panel", sb) + _preview_box = VBoxContainer.new() + _preview_box.add_theme_constant_override("separation", 8) + _preview_box.mouse_filter = Control.MOUSE_FILTER_IGNORE + preview_panel.add_child(_preview_box) + preview_panel.hide() + add_child(preview_panel) + + var min_btn := add_button("Minimize", "plain", minimize) + min_btn.tooltip_text = "Hide this question to look at the board (M or Tab)" + footer.move_child(min_btn, 0) + var spacer := Control.new() + spacer.size_flags_horizontal = Control.SIZE_EXPAND_FILL + footer.add_child(spacer) + footer.move_child(spacer, 1) + + pill = _bar_panel() + var row := pill.get_child(0) as HBoxContainer + var lbl := Style.make_label("Question waiting: " + title_label.text + " ·", Style.body_bold(), 15, Style.INK) + lbl.size_flags_vertical = Control.SIZE_SHRINK_CENTER + row.add_child(lbl) + var show_btn := make_button("Show", "primary") + show_btn.add_theme_font_size_override("font_size", 14) + show_btn.pressed.connect(restore) + show_btn.tooltip_text = "Bring the question back (M)" + row.add_child(show_btn) + var x := make_button("Cancel", "plain") + x.add_theme_font_size_override("font_size", 14) + x.tooltip_text = "Cancel the question and stop this roll" + x.pressed.connect(func() -> void: answer(null)) + row.add_child(x) + pill.hide() + add_child(pill) + + toast_panel = _bar_panel() + var trow := toast_panel.get_child(0) as HBoxContainer + var tl := Style.make_label("", Style.body(), 15, Style.INK) + trow.add_child(tl) + toast_panel.set_meta("label", tl) + var ts := make_button("Show", "primary") + ts.add_theme_font_size_override("font_size", 14) + ts.pressed.connect(restore) + trow.add_child(ts) + toast_panel.hide() + add_child(toast_panel) + _toast_timer = Timer.new() + _toast_timer.one_shot = true + _toast_timer.wait_time = 4.0 + _toast_timer.timeout.connect(func() -> void: toast_panel.hide()) + add_child(_toast_timer) + + +func _bar_panel() -> PanelContainer: + var p := PanelContainer.new() + var sb := Style.bordered(Style.box(Style.CARD, 12, [14, 7, 8, 7]), 1, Style.DASH) + sb.border_width_top = 3 + sb.border_color = Style.COPPER + sb.shadow_color = Color(0, 0, 0, 0.4) + sb.shadow_size = 14 + sb.shadow_offset = Vector2(0, 5) + p.add_theme_stylebox_override("panel", sb) + p.mouse_filter = Control.MOUSE_FILTER_STOP + var row := HBoxContainer.new() + row.add_theme_constant_override("separation", 10) + p.add_child(row) + return p + + +## Bottom centre, above the result tray; the toast sits just above it. +func _place_pill() -> void: + if not minimized: + return + pill.reset_size() + var bottom := size.y - 14.0 + var m := get_parent() as Main + if m and m.tray and m.tray.is_inside_tree(): + bottom = m.tray.get_global_rect().position.y - get_global_rect().position.y - 12.0 + pill.position = Vector2(maxf(8.0, (size.x - pill.size.x) / 2.0), bottom - pill.size.y) + if toast_panel.visible: + toast_panel.reset_size() + toast_panel.position = Vector2(maxf(8.0, (size.x - toast_panel.size.x) / 2.0), pill.position.y - toast_panel.size.y - 8.0) + + +func _process(_delta: float) -> void: + if minimized: + _place_pill() + elif preview_panel.visible: + _place_preview() + + +# ---------- Preview ---------- + +func _set_hover(k: int) -> void: + _hover = k + _refresh_preview() + + +## Shows the preview for the hovered option, else the focused one, else (for +## number and text questions) where the Answer leads. +func _refresh_preview() -> void: + if minimized or preview_panel == null or not is_inside_tree(): + return + var idx := -1 + if kind == "pick": + idx = _hover + if idx < 0: + for k in _options.size(): + if _options[k].has_focus(): + idx = k + break + else: + idx = 0 + if idx == _preview_for: + return + _preview_for = idx + for c in _preview_box.get_children(): + _preview_box.remove_child(c) + c.queue_free() + var content := preview_for_option(idx) if idx >= 0 else null + if content == null: + preview_panel.hide() + return + _preview_box.add_child(content) + preview_panel.show() + _place_preview() + + +func _hide_preview() -> void: + _preview_for = -2 + if preview_panel: + preview_panel.hide() + + +## The entry behind option `k` (a position in the request's options; for +## number and text questions, the single Answer entry), or null. +func _entry_for(k: int) -> TableEntry: + if board == null: + return null + var t := board.get_table(str(request.get("table_id", ""))) + if t == null: + return null + var index := k + if kind == "pick": + var opts: Array = request.get("options", []) + if k < 0 or k >= opts.size(): + return null + index = int((opts[k] as Dictionary).get("index", k)) + return t.entries[index] if index >= 0 and index < t.entries.size() else null + + +## The preview content for option `k` as a Control, or null when there is +## nothing to say. It holds the option's details and effects, then a +## TablePreview for each table it pipes to (with its count) and each table +## its text mentions with [[Table]]. +func preview_for_option(k: int) -> Control: + var e := _entry_for(k) + if e == null: + return null + var box := VBoxContainer.new() + box.add_theme_constant_override("separation", 8) + box.mouse_filter = Control.MOUSE_FILTER_IGNORE + box.custom_minimum_size.x = PREVIEW_WIDTH - 28.0 + var heading := section(("Option %d" % (k + 1)) if kind == "pick" else "After you answer") + heading.mouse_filter = Control.MOUSE_FILTER_IGNORE + box.add_child(heading) + var any := false + var det := e.details.strip_edges() + if det != "": + any = true + var d := RichText.label(RichText.decorate(det), 14, Style.INK, false) + d.mouse_filter = Control.MOUSE_FILTER_IGNORE + box.add_child(d) + var fx := Effects.split(e.effects) + if not fx.is_empty(): + any = true + box.add_child(_rule()) + box.add_child(section("Effects")) + for st in fx: + var l := Style.make_label(st, Style.body_bold(), 13, RichText.VAR_INK) + l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + l.mouse_filter = Control.MOUSE_FILTER_IGNORE + box.add_child(l) + var shown: Array[String] = [] + for tid in e.pipes: + shown.append(tid) + any = true + box.add_child(_rule()) + box.add_child(TablePreview.build(board, tid, {"count": e.count_text(tid)})) + var mentioned: Array[String] = [] + for name in TextExpander.references(e.text, func(n: String) -> String: return Roller.resolve(board, n)): + var rid := Roller.resolve(board, name) + if rid != "" and not shown.has(rid) and not mentioned.has(rid): + mentioned.append(rid) + if not mentioned.is_empty(): + any = true + box.add_child(_rule()) + box.add_child(section("Mentioned in the text")) + for rid in mentioned: + box.add_child(TablePreview.build(board, rid)) + if not any: + box.free() + return null + return box + + +func _rule() -> Control: + var r := ColorRect.new() + r.color = Style.RULE + r.custom_minimum_size.y = 1 + r.mouse_filter = Control.MOUSE_FILTER_IGNORE + return r + + +## Beside the card (right if there is room, else left), top-aligned with it +## and kept on screen. It never overlaps the card, so it never covers options. +func _place_preview() -> void: + if not preview_panel.visible: + return + var view := size + var cr := card.get_global_rect() + var gap := 18.0 + var right := view.x - cr.end.x - gap - 10.0 + var left := cr.position.x - gap - 10.0 + var w := PREVIEW_WIDTH + var x: float + if right >= w or right >= left: + w = clampf(right, 180.0, PREVIEW_WIDTH) + x = cr.end.x + gap + else: + w = clampf(left, 180.0, PREVIEW_WIDTH) + x = cr.position.x - gap - w + preview_panel.custom_minimum_size.x = w + preview_panel.size = Vector2(w, 0) + var h := preview_panel.get_combined_minimum_size().y + var top := clampf(cr.position.y, 10.0, maxf(10.0, view.y - 10.0 - h)) + h = minf(h, view.y - top - 10.0) + preview_panel.position = Vector2(x, top) + preview_panel.size = Vector2(w, h) diff --git a/ui/main.gd b/ui/main.gd index 69b6ca5..2ae3d3a 100644 --- a/ui/main.gd +++ b/ui/main.gd @@ -350,6 +350,10 @@ func _refresh_lit() -> void: func set_mode(m: Mode) -> void: + if m == Mode.BUILD and _choice_waiting("Answer the waiting question before building"): + _build_button.set_pressed_no_signal(mode == Mode.BUILD) + _play_button.set_pressed_no_signal(mode == Mode.PLAY) + return mode = m _play_button.set_pressed_no_signal(m == Mode.PLAY) _build_button.set_pressed_no_signal(m == Mode.BUILD) @@ -625,6 +629,9 @@ func start_roll(tid: String, fast: bool = false) -> void: var t := data.get_table(tid) if t == null or t.kind == RollTable.KIND_VARIABLE: return + # A minimized question keeps waiting; a new roll must not cancel it. + if _choice_waiting("Answer the waiting question first"): + return fast = fast or Input.is_key_pressed(KEY_SHIFT) _archive_current() _token += 1 @@ -723,6 +730,15 @@ func _ask_player(req: Dictionary) -> Variant: return v +## True (after a toast with a Show button) while a question is minimized. +func _choice_waiting(message: String) -> bool: + var d := pending_choice() + if d == null or not d.minimized: + return false + d.nudge(message) + return true + + ## The open choice pop-up, or null. func pending_choice() -> ChoiceDialog: return _choice if is_instance_valid(_choice) else null diff --git a/ui/table_preview.gd b/ui/table_preview.gd new file mode 100644 index 0000000..e1183cd --- /dev/null +++ b/ui/table_preview.gd @@ -0,0 +1,174 @@ +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_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 diff --git a/ui/table_preview.gd.uid b/ui/table_preview.gd.uid new file mode 100644 index 0000000..14d3531 --- /dev/null +++ b/ui/table_preview.gd.uid @@ -0,0 +1 @@ +uid://ys3d7l44syvn