From 7d7b181b71dcbb9839feb758cf6f360ddcd6a067 Mon Sep 17 00:00:00 2001 From: RobinFiredrake Date: Tue, 1 Dec 2026 15:33:49 -0600 Subject: [PATCH] Deck rows show copies left; Reroll this on tray steps - A deck row with no copies left is greyed out; one partly drawn gets an accent tint (Play and Build) and, in Play, shows / . - Each step of the current result has a Reroll this button: that card and the cards after it roll again in place. Earlier keeps no copy; the old branch's deck draws go back. - Tests in test_small_ui and test_fast_roll. Co-Authored-By: Claude Opus 5.5 --- core/deck_state.gd | 9 +++ core/state_store.gd | 14 +++++ docs/ARCHITECTURE.md | 3 +- docs/DESIGN.md | 2 + tests/test_fast_roll.gd | 33 +++++++++++ tests/test_small_ui.gd | 37 ++++++++++++ ui/main_rolling.gd | 3 + ui/main_rolling_reroll.gd | 107 ++++++++++++++++++++++++++++++++++ ui/main_rolling_reroll.gd.uid | 1 + ui/main_state.gd | 16 ++++- ui/result_tray.gd | 3 + ui/table_card.gd | 2 + ui/table_card_skin.gd | 39 +++++++++++-- ui/tray_chips.gd | 23 ++++++-- 14 files changed, 277 insertions(+), 15 deletions(-) create mode 100644 ui/main_rolling_reroll.gd create mode 100644 ui/main_rolling_reroll.gd.uid diff --git a/core/deck_state.gd b/core/deck_state.gd index 62cc777..f303f08 100644 --- a/core/deck_state.gd +++ b/core/deck_state.gd @@ -62,6 +62,15 @@ func take(e: TableEntry) -> void: drawn[e.id] = int(drawn.get(e.id, 0)) + 1 +## Puts one drawn copy of this result back (its roll was replaced). +func put_back(entry_id: String) -> void: + var count := int(drawn.get(entry_id, 0)) - 1 + if count > 0: + drawn[entry_id] = count + else: + drawn.erase(entry_id) + + ## Forgets draws for entries that no longer exist. func _prune(table: RollTable) -> void: for k: Variant in drawn.keys(): diff --git a/core/state_store.gd b/core/state_store.gd index 7be90d0..8e242ea 100644 --- a/core/state_store.gd +++ b/core/state_store.gd @@ -93,6 +93,20 @@ func add_history(record: Dictionary) -> void: _dirty = true +## Swaps the record with the same collection and time for this one (a +## rerolled step changes the current roll in place). Adds it if none matches. +func replace_history(record: Dictionary) -> void: + var list: Array = _section_list("history") + for i in list.size(): + var old: Variant = list[i] + if old is Dictionary and str(old.get("collection", "")) == str(record.get("collection", "")) \ + and float(old.get("time", -1.0)) == float(record.get("time", -2.0)): + list[i] = record.duplicate(true) + _dirty = true + return + add_history(record) + + ## All records, or only those whose "collection" is cid when cid isn't empty. func get_history(cid: String = "") -> Array[Dictionary]: var out: Array[Dictionary] = [] diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index d03ac74..6b77af4 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -227,7 +227,8 @@ Each original file keeps one-line delegators, so callers are unchanged. - **Ledger:** `core/ledger_members.gd` (entries on the card: clean, adopt older spreads, sync with saved values), `ui/ledger_edits.gd` (add/rename/start/remove with undo), `ui/ledger_card_rows.gd` (Build-mode fields on the card). -- **Rolling:** `ui/main_rolling_show.gd` (show a roll at once), `RollCosts.card_gate` (card costs), +- **Rolling:** `ui/main_rolling_show.gd` (show a roll at once), `ui/main_rolling_reroll.gd` + ("Reroll this" on a tray step: that card and its branch roll again in place; 2026-12-01), `RollCosts.card_gate` (card costs), `RollPauses` pop-up, `SpreadLinks` (`core/spread_links.gd`, [[Spread::Card]]) with `ui/spread_loader.gd` and `ui/spread_link_dialog.gd`. - **Interface:** `ui/delete_key.gd`, `ui/sound_panel.gd`, `ui/format_menu.gd` (right-click Format), diff --git a/docs/DESIGN.md b/docs/DESIGN.md index b8e7c87..887760e 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -78,6 +78,8 @@ undo 200 steps, formula depth 8) and the review list at the end of `docs/SHEETS. - Drag handles reorder a card's results in Build (ranged cards keep ranges in place). Hand-tested. - Thread nodes under a card stacked above theirs are hidden. Hand-tested. - Deck mode: a drawn result fades only after the roll animation reveals it (fast rolls: at once). + - Deck mode rows (2026-12-01): none left = greyed out; some drawn = accent tint, and in Play + mode " / " in place of the number. Tray steps have "↻ Reroll this". - Group marks (the user's pick, "option C"): a member card's top-right corner, and the group's border, take the group's colour (Colour ▸ on the group menu); uncoloured groups use the theme token GROUP (default gold, same as the highlight; watch for confusion). diff --git a/tests/test_fast_roll.gd b/tests/test_fast_roll.gd index a49effa..a4c0f11 100644 --- a/tests/test_fast_roll.gd +++ b/tests/test_fast_roll.gd @@ -42,9 +42,42 @@ func run() -> void: await _test_deck_waits() await _test_batch_history() _test_reroll_mark() + await _test_reroll_this() finish() +## Reroll this: a step rolls again with its branch, in place. The steps +## before it stay, Earlier keeps no copy, and a deck's old draw goes back. +func _test_reroll_this() -> void: + main.start_roll("wilderness", true) + var before: Array = main.current["all"] + var history := main.history.size() + var last := before.size() - 1 + _check(main.tray.reroll_buttons.has(last), "a step of the current result has Reroll this") + await main.rolling.reroll.reroll(last) + var after: Array = main.current["all"] + for i in last: + _check(is_same(after[i], before[i]), "the steps before it are kept (step %d)" % i) + _eq(str(after[last]["table_id"]), str(before[last]["table_id"]), "the same card rolls again") + _eq(main.history.size(), history, "Earlier keeps no copy of the old result") + _check(main.current.get("done", false), "the rerolled result is finished") + before = after + await main.rolling.reroll.reroll(0) + _check(not is_same(main.current["all"][0], before[0]), "the first step and the cards after it roll again") + _eq(str(main.current["root"]), "wilderness", "it is still the same roll") + + var t := main.data.get_table("wilderness") + t.card_mode = true + main.reshuffle_table(t.id) + main.start_roll(t.id, true) + var total := main.cards_left(t.id).y + _eq(main.cards_left(t.id).x, total - 1, "the roll draws one card") + await main.rolling.reroll.reroll(0) + _eq(main.cards_left(t.id).x, total - 1, "the old card goes back; only the new one is drawn") + t.card_mode = false + main.reshuffle_table(t.id) + + ## "Roll again" shows: the skipped result flickers with a floating note. func _test_reroll_mark() -> void: var card := main.card_for("names") diff --git a/tests/test_small_ui.gd b/tests/test_small_ui.gd index 8f2f08a..7ca93b6 100644 --- a/tests/test_small_ui.gd +++ b/tests/test_small_ui.gd @@ -84,11 +84,48 @@ func _test_chip_and_export() -> void: _check(subs.size() == 1 and (subs[0] as PopupMenu).item_count == IoMenu.EXPORTS.size() - 1, "diagram formats sit under Export as a diagram") +## Deck mode: a row with some copies drawn is tinted (Play and Build) and, in +## Play, shows " / "; with none left it is greyed out. +func _test_partly_drawn(t: RollTable) -> void: + var first := t.entries[0] + var weight := first.weight + first.weight = 2 + main.reshuffle_table(t.id) + var card := main.card_for(t.id) + card.rebuild() + await _frames() + var number: Label = card.rows_ui.parts[first.id]["num"] + var plain := number.text + (main.decks.get_or_add(t.id, DeckState.new()) as DeckState).take(first) + card.update_deck() + var row: Control = card.rows[first.id] + _check(row.self_modulate == TableCardSkin.partly_drawn_tint() and row.modulate == Color.WHITE, "a partly drawn row is tinted") + _eq(number.text, "1 / 2", "a partly drawn row shows its copies left") + main.set_mode(Main.Mode.BUILD) + await _frames() + card = main.card_for(t.id) + _check(card.rows[first.id].self_modulate == TableCardSkin.partly_drawn_tint(), "it is tinted in Build mode too") + main.set_mode(Main.Mode.PLAY) + await _frames() + card = main.card_for(t.id) + (main.decks[t.id] as DeckState).take(first) + card.update_deck() + row = card.rows[first.id] + number = card.rows_ui.parts[first.id]["num"] + _check(row.modulate == TableCardSkin.DRAWN_LOOK and row.self_modulate == Color.WHITE, "with no copies left the row is greyed out") + _eq(number.text, plain, "it shows its own number again") + first.weight = weight + main.reshuffle_table(t.id) + card.rebuild() + await _frames() + + ## Deck mode: drawn results fade, an empty deck that waits for Shuffle gives ## its Empty result, and Shuffle all puts everything back. func _test_deck_mode() -> void: var t := main.data.get_table("party_roles") _check(t != null and t.card_mode, "the sample's Party Roles is a deck") + await _test_partly_drawn(t) t.deck_empty = "stop" t.deck_empty_text = "No roles left" for i in t.entries.size(): diff --git a/ui/main_rolling.gd b/ui/main_rolling.gd index 5c40f97..7c0649f 100644 --- a/ui/main_rolling.gd +++ b/ui/main_rolling.gd @@ -22,6 +22,8 @@ var deck_reveal: DeckReveal var _seed: MainRollingSeed var _copy: MainRollingCopy var _show: MainRollingShow +## "Reroll this" on a step in the result tray. +var reroll: MainRollingReroll func _init(p_main: Main) -> void: @@ -31,6 +33,7 @@ func _init(p_main: Main) -> void: _seed = MainRollingSeed.new(p_main) _copy = MainRollingCopy.new(p_main) _show = MainRollingShow.new(p_main) + reroll = MainRollingReroll.new(p_main) ## Rolls a table and plays the result down its pipes. A fast roll (Shift held diff --git a/ui/main_rolling_reroll.gd b/ui/main_rolling_reroll.gd new file mode 100644 index 0000000..f42d06f --- /dev/null +++ b/ui/main_rolling_reroll.gd @@ -0,0 +1,107 @@ +class_name MainRollingReroll +extends RefCounted +## "Reroll this" in the result tray: one step's card rolls again, and so do +## the cards after it along its threads. The new steps take the old ones' +## place in the current result; Earlier keeps no copy. Deck draws the old +## steps made go back into their decks first. The reroll shows at once. + +## Steps that can't be rerolled on their own: text rolls inside a result, +## runs dropped from or added into a total, a choice's answer, and the +## "Cancelled" / "Stopped after …" ends. +const FIXED_KEYS := ["inline", "dropped", "combined", "answer", "spread", "cancelled", "limit"] +## How the old step was reached; the new step keeps it, so its thread lights. +const LINK_KEYS := ["pass", "after", "card_thread"] + +var m: Main + + +func _init(p_main: Main) -> void: + m = p_main + + +## True when step `index` of the finished current result can be rerolled. +func can_reroll(index: int) -> bool: + if m.current.is_empty() or not m.current.get("done", false): + return false + var steps: Array = m.current.get("all", []) + if index < 0 or index >= steps.size(): + return false + var step: Dictionary = steps[index] + for key: String in FIXED_KEYS: + var value: Variant = step.get(key) + if value != null and not (value is bool and value == false): + return false + var table := m.data.get_table(str(step.get("table_id", ""))) + return table != null and table.kind != RollTable.KIND_VARIABLE + + +## Index just past the last step under steps[index] (its whole branch). +static func branch_end(steps: Array, index: int) -> int: + var depth := int((steps[index] as Dictionary).get("depth", 0)) + var end := index + 1 + while end < steps.size() and int((steps[end] as Dictionary).get("depth", 0)) > depth: + end += 1 + return end + + +## Rolls step `index` of the current result again, with its branch. +func reroll(index: int) -> void: + if not can_reroll(index): + return + var current := m.current + var steps: Array = current["all"] + var step: Dictionary = steps[index] + var end := branch_end(steps, index) + _put_back_draws(steps.slice(index, end)) + if m.sound: + m.sound.play("roll") + var ctx := Roller.new_context(m.data, m.rng, m.persistent_vars, m.decks) + ctx.chain_vars = (current.get("chain_vars", {}) as Dictionary).duplicate(true) + ctx.prompt = _ask + m.sheet_links.before_roll(Formatter.strip_tags(str(current.get("root_name", "")))) + var depth := int(step.get("depth", 0)) + var tree: Dictionary = await Roller.roll_async(m.data, str(step["table_id"]), ctx, depth) + if not is_same(current, m.current): + return # another roll started while this one asked a question + var fresh := Roller.flatten(tree, [], depth) + if not fresh.is_empty(): + for key: String in LINK_KEYS: + if step.has(key): + (fresh[0] as Dictionary)[key] = step[key] + var all: Array = steps.slice(0, index) + fresh + steps.slice(end) + current["all"] = all + current["steps"] = all + current["chain_vars"] = ctx.chain_vars.duplicate() + current["fast"] = true + _show(all) + m.local_state.replace_current_roll(ctx) + + +## Deck mode: every card the old branch drew goes back into its deck. +func _put_back_draws(old_steps: Array) -> void: + for st: Dictionary in old_steps: + if st.get("stop", false) or not st.has("entry_id"): + continue + var deck: DeckState = m.decks.get(str(st.get("table_id", ""))) + if deck: + deck.put_back(str(st["entry_id"])) + + +## Questions in the rerolled branch ask as usual; a paused thread goes on, +## since you asked for this branch. +func _ask(req: Dictionary) -> Variant: + if str(req.get("kind", "")) == "pause": + return true + return await m.prompt_hook.call(req) + + +func _show(all: Array) -> void: + m.lit_entries.clear() + m.lit_pipes.clear() + m.lit_texts.clear() + m.lit_columns.clear() + m.rolling._light_steps(all) + m._refresh_lit() + m.rolling._reveal_steps(all) + m.refresh_live() + m.tray.render() diff --git a/ui/main_rolling_reroll.gd.uid b/ui/main_rolling_reroll.gd.uid new file mode 100644 index 0000000..1556b47 --- /dev/null +++ b/ui/main_rolling_reroll.gd.uid @@ -0,0 +1 @@ +uid://dgy1y1xvj0vpm diff --git a/ui/main_state.gd b/ui/main_state.gd index 8e2e1b6..ef67627 100644 --- a/ui/main_state.gd +++ b/ui/main_state.gd @@ -53,7 +53,7 @@ func load_local_state() -> void: ## After a roll: saves the decks, any changed persistent variables and the ## history record. -func _store_roll(ctx: RollContext) -> void: +func _store_roll(ctx: RollContext, replace: bool = false) -> void: # Values the roll changed go back into the sheets on the spread first. m.sheet_links.after_roll() if m.state == null: @@ -61,11 +61,21 @@ func _store_roll(ctx: RollContext) -> void: _store_decks() if ctx.persistent_changed: _store_variables() - m.state.add_history({"collection": m.data.id, "root": m.current["root"], "root_name": m.current["root_name"], - "time": m.current["time"], "steps": m.current["all"], "chain_vars": m.current.get("chain_vars", {})}) + var record := {"collection": m.data.id, "root": m.current["root"], "root_name": m.current["root_name"], + "time": m.current["time"], "steps": m.current["all"], "chain_vars": m.current.get("chain_vars", {})} + if replace: + m.state.replace_history(record) + else: + m.state.add_history(record) m.state.save() +## After "Reroll this": saves like _store_roll, but the current roll's history +## record is changed in place, not added again. +func replace_current_roll(ctx: RollContext) -> void: + _store_roll(ctx, true) + + func _store_decks() -> void: if m.state == null: return diff --git a/ui/result_tray.gd b/ui/result_tray.gd index 0070cb8..dae8add 100644 --- a/ui/result_tray.gd +++ b/ui/result_tray.gd @@ -58,6 +58,8 @@ var _scroll_to_end := false var _why: WhyPopover ## The "Why this?" buttons of the chips shown, by step index (tests). var why_buttons: Dictionary = {} +## The "Reroll this" buttons of the chips shown, by step index (tests). +var reroll_buttons: Dictionary = {} var _why_flag_done := false ## A long chain shows only its last steps; the rest sit behind a toggle. @@ -197,6 +199,7 @@ func render() -> void: _flow.remove_child(c) c.queue_free() why_buttons.clear() + reroll_buttons.clear() if _why != null: _why.close() _empty.visible = current.is_empty() diff --git a/ui/table_card.gd b/ui/table_card.gd index 130bb5d..f662991 100644 --- a/ui/table_card.gd +++ b/ui/table_card.gd @@ -162,6 +162,8 @@ func _num_width(build: bool, ranged: bool) -> float: return 100.0 if RangeWeights.uses_weights(table) else 90.0 if ranged: return 52.0 if wide else 40.0 + if table.card_mode and not build: + return 50.0 # room for a partly drawn deck row's " / " return 24.0 if wide else 22.0 diff --git a/ui/table_card_skin.gd b/ui/table_card_skin.gd index 07811c1..9332a24 100644 --- a/ui/table_card_skin.gd +++ b/ui/table_card_skin.gd @@ -2,6 +2,9 @@ class_name TableCardSkin extends RefCounted ## The card shell look: frame colour, drop/warning panels and scrollbars. +## A deck row with no copies left: greyed out. +const DRAWN_LOOK := Color(0.7, 0.7, 0.7, 0.45) + var card: TableCard @@ -81,8 +84,9 @@ func skin_scrollbar(bar: VScrollBar) -> void: bar.add_theme_stylebox_override("grabber_pressed", grab_hi) -## Deck mode: results already drawn (none left in the deck) show faded until -## the deck is shuffled. +## Deck mode: a result with no copies left is greyed out until the deck is +## shuffled; one with some copies drawn is tinted in the accent colour and, in +## Play mode, shows " / " in place of its number. func dim_drawn() -> void: if not card.table.card_mode: return @@ -91,10 +95,35 @@ func dim_drawn() -> void: var row: Control = card.rows.get(e.id) if row == null or not is_instance_valid(row): continue - var held := card.main.rolling.deck_reveal.held(card.table.id, e.id) - var gone := deck != null and deck.remaining(card.table, e) + held <= 0 - row.modulate.a = 0.38 if gone else 1.0 + var copies := DeckState.copies_of(card.table, e) + var left := copies + if deck != null: + var held := card.main.rolling.deck_reveal.held(card.table.id, e.id) + left = mini(copies, deck.remaining(card.table, e) + held) + var gone := left <= 0 + var partly := not gone and left < copies + row.modulate = DRAWN_LOOK if gone else Color.WHITE + row.self_modulate = partly_drawn_tint() if partly else Color.WHITE row.tooltip_text = "Already drawn: Shuffle the deck to put it back" if gone else "" + _show_copies_left(e, left, copies, partly) + + +## The tint on a row with some of its copies drawn. +static func partly_drawn_tint() -> Color: + return Color.WHITE.lerp(Style.ACCENT, 0.55) + + +## Play mode: a partly drawn row's number becomes " / ". +func _show_copies_left(e: TableEntry, left: int, copies: int, partly: bool) -> void: + var part: Dictionary = card.rows_ui.parts.get(e.id, {}) + var num: Label = part.get("num") + if num == null or not is_instance_valid(num): + return + if not num.has_meta("row_number"): + num.set_meta("row_number", [num.text, num.tooltip_text]) + var own: Array = num.get_meta("row_number") + num.text = "%d / %d" % [left, copies] if partly else str(own[0]) + num.tooltip_text = "%d of %d copies left in the deck" % [left, copies] if partly else str(own[1]) func build_styles() -> void: diff --git a/ui/tray_chips.gd b/ui/tray_chips.gd index 0129f8d..090c7ea 100644 --- a/ui/tray_chips.gd +++ b/ui/tray_chips.gd @@ -66,11 +66,22 @@ static func chip(tray: ResultTray, step: Dictionary, last: bool, steps: Array = var fx: Array = step.get("effects", []) if not fx.is_empty(): v.add_child(_effect_lines(TrayText.effect_lines_for(step, main.data, plain))) + var pills := HBoxContainer.new() + pills.add_theme_constant_override("separation", 6) if has_why: - var wb := _why_button() + var wb := _pill_button("Why this?", "What decided this step") wb.pressed.connect(func() -> void: tray.open_why(index, wb)) tray.why_buttons[index] = wb - v.add_child(wb) + pills.add_child(wb) + if index >= 0 and main.rolling.reroll.can_reroll(index): + var rb := _pill_button("↻ Reroll this", "Roll this card again, and the cards after it") + rb.pressed.connect(func() -> void: main.rolling.reroll.reroll(index)) + tray.reroll_buttons[index] = rb + pills.add_child(rb) + if pills.get_child_count() > 0: + v.add_child(pills) + else: + pills.free() if has_details or has_why: p.mouse_entered.connect(func() -> void: if not tray.why_popover().visible: @@ -78,11 +89,11 @@ static func chip(tray: ResultTray, step: Dictionary, last: bool, steps: Array = return p -## The small "Why this?" pill under a chip. -static func _why_button() -> Button: +## A small pill button under a chip ("Why this?", "Reroll this"). +static func _pill_button(text: String, tip: String) -> Button: var b := Button.new() - b.text = "Why this?" - b.tooltip_text = "What decided this step" + b.text = text + b.tooltip_text = tip b.size_flags_horizontal = Control.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)