diff --git a/tests/test_small_ui.gd b/tests/test_small_ui.gd index 175982d..61f9930 100644 --- a/tests/test_small_ui.gd +++ b/tests/test_small_ui.gd @@ -21,6 +21,7 @@ func _init() -> void: _test_template_replace() await _test_chip_and_export() await _test_deck_mode() + await _test_batch() print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) quit(1 if _failures else 0) @@ -116,6 +117,14 @@ func _test_deck_mode() -> void: _check((card.rows[t.entries[0].id] as Control).modulate.a == 1.0, "Shuffle all brings them back") +func _test_batch() -> void: + var texts: Array[String] = await BatchRoll.run(main, "road", 4) + _check(texts.size() == 4 and texts[0].contains("Road Encounter"), "a batch gives every result: %d" % texts.size()) + var dialogs := main.find_children("*", "ModalCard", true, false) + _check(not dialogs.is_empty() and (dialogs[dialogs.size() - 1] as ModalCard).title_label.text == "Road Encounter × 4", "and shows them together") + (dialogs[dialogs.size() - 1] as ModalCard).close() + + func _frames(n: int = 2) -> void: for i in n: await process_frame diff --git a/ui/actions.gd b/ui/actions.gd index 40226e3..dc93e86 100644 --- a/ui/actions.gd +++ b/ui/actions.gd @@ -82,6 +82,8 @@ static func card(m: Main, t: RollTable) -> Array: var list: Array = [] if t.kind != RollTable.KIND_VARIABLE: list.append({"text": "Roll", "do": m.rolling.start_roll.bind(t.id)}) + list.append({"text": "Roll several times…", "do": BatchRoll.ask.bind(m, t.id), + "tip": "Roll the whole chain a number of times and see every result together"}) if t.card_mode: list.append({"text": "Shuffle the deck", "do": m.reshuffle_table.bind(t.id)}) if c and c.can_collapse(): diff --git a/ui/batch_roll.gd b/ui/batch_roll.gd new file mode 100644 index 0000000..9ef8e2c --- /dev/null +++ b/ui/batch_roll.gd @@ -0,0 +1,67 @@ +class_name BatchRoll +extends RefCounted +## Batches: roll a card's whole chain several times ("5 NPCs") and get every +## result at the end, in one window with Copy all. Questions on the way are +## asked each time. Values and decks change as they would for single rolls. + +const MAX := 100 + + +## Asks how many, then runs the batch. +static func ask(m: Main, tid: String) -> void: + var t := m.data.get_table(tid) + if t == null: + return + m._add_modal(ModalCard.ask_text("Roll several times", "How many times should %s roll?" % Formatter.strip_tags(t.name), "5", "Roll", + func(s: String) -> void: + if not s.strip_edges().is_valid_int() or int(s) < 1: + m.toast("Type a number from 1 to %d" % MAX) + return + run(m, tid, mini(int(s), MAX)))) + + +## Rolls `n` times and shows the results. Returns the result texts. +static func run(m: Main, tid: String, n: int) -> Array[String]: + var t := m.data.get_table(tid) + var texts: Array[String] = [] + for i in n: + var ctx := Roller.new_context(m.data, m.rng, m.persistent_vars, m.decks) + ctx.prompt = m.prompt_hook + var tree: Dictionary = await Roller.roll_async(m.data, tid, ctx) + if ctx.halted == Roller.CANCELLED: + break + var steps := Roller.flatten(tree, []) + texts.append(m.rolling.result_text(steps, ResultTray.COPY_TEXT)) + if m.state: + m.local_state._store_decks() + if ctx.persistent_changed: + m.local_state._store_variables() + m.state.add_history({"collection": m.data.id, "root": tid, "root_name": t.name, + "time": Time.get_unix_time_from_system(), "steps": steps, "chain_vars": ctx.chain_vars.duplicate()}) + if m.state: + m.state.save() + m.refresh_live() + if not texts.is_empty(): + m._add_modal(dialog(t, texts)) + return texts + + +static func dialog(t: RollTable, texts: Array[String]) -> ModalCard: + var d := ModalCard.new("%s × %d" % [Formatter.strip_tags(t.name), texts.size()], 620.0) + for i in texts.size(): + var box := PanelContainer.new() + box.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.CARD_SOFT, 8, [12, 8, 12, 8]), 1, Style.RULE)) + var v := VBoxContainer.new() + box.add_child(v) + v.add_child(Style.make_label("#%d" % (i + 1), Style.body_bold(), 13, Style.TRIM_DARK)) + var l := Style.make_label(texts[i], Style.body(), 15, Style.INK) + l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + v.add_child(l) + d.body.add_child(box) + var parts := PackedStringArray() + for i in texts.size(): + parts.append("#%d\n%s" % [i + 1, texts[i]]) + var all := "\n\n".join(parts) + d.add_button("Copy all", "plain", func() -> void: DisplayServer.clipboard_set(all)) + d.add_button("Close", "primary", d.close) + return d diff --git a/ui/batch_roll.gd.uid b/ui/batch_roll.gd.uid new file mode 100644 index 0000000..165308c --- /dev/null +++ b/ui/batch_roll.gd.uid @@ -0,0 +1 @@ +uid://ca2kspegcfk5i