diff --git a/help/topics.txt b/help/topics.txt index 1ef4ed4..bc9d46b 100644 --- a/help/topics.txt +++ b/help/topics.txt @@ -66,6 +66,16 @@ A thread carries the roll from a result to another card. - [b]Otherwise[/b] runs the thread only when none of the result's Only if threads ran. - [b]Inputs[/b] pass values to a card that declares them. +## Rolling with other dice +A thread can roll a card with numbered ranges using different dice, just this once. +- Type the dice in [b]Roll it with … instead[/b], such as `3d10+70`. Leave it empty to use the card's own dice. +- Example: a World Feature card is rolled with 1d100, but "Multiple features" rolls it twice with 3d10+70, so only the higher results come up. + +## Rolling again on some results +A thread can roll its card again when certain results come up. +- Open [b]Roll again if it comes up[/b] and tick those results. +- Example: "Mixed" rolls World Feature twice, rolling again on any 96 or higher, so it never lands on itself. + [demo]threads[/demo] # Roll modifiers diff --git a/tests/test_ui_threads.gd b/tests/test_ui_threads.gd new file mode 100644 index 0000000..19e5a12 --- /dev/null +++ b/tests/test_ui_threads.gd @@ -0,0 +1,82 @@ +extends TestBase +## The thread inspector's "Roll it with … instead" field and "Roll again if +## it comes up" list: shown for a card with numbered ranges, edits land on the +## result, undo puts them back. +## Run: godot --headless --path . -s res://tests/test_ui_threads.gd -- --sample + + +func _init() -> void: + if not await start_main(): + return + await _test_ranged_target() + await _test_plain_target() + finish() + + +func _parts(to: String) -> Dictionary: + return main.inspector.pipe_parts.get(to, {}) + + +func _road() -> TableEntry: + return main.data.get_table("road").entries[3] + + +func _test_ranged_target() -> void: + print("Ranged target") + main.set_mode(Main.Mode.BUILD) + var e := _road() + e.add_pipe("wilderness") + main.mark_dirty() + main.undo_redo._commit_edits() + main.inspecting.select_pipe(Roller.pipe_key("road", e.id, "wilderness")) + await _frames(3) + var p := _parts("wilderness") + _check(p.has("dice"), "a ranged card gets the Roll it with field") + var dice := p.get("dice") as LineEdit + _eq(dice.placeholder_text, "1d100", "its placeholder is the card's own dice") + var toggle := p.get("reroll_toggle") as Button + _check(toggle != null and toggle.text.contains("never"), "the roll-again list starts closed, saying never: %s" % (toggle.text if toggle else "")) + var boxes: Dictionary = p.get("reroll", {}) + var w := main.data.get_table("wilderness") + _eq(boxes.size(), w.entries.size(), "one tick box per result") + var last := w.entries[w.entries.size() - 1] + _check((boxes[last.id] as CheckBox).text.begins_with("96–00"), "a box shows the number and the text: %s" % (boxes[last.id] as CheckBox).text) + + dice.text = "3d10+" + dice.text_changed.emit("3d10+") + _eq(e.dice_for("wilderness"), "", "dice that can't be rolled aren't kept") + dice.text = "3d10+70" + dice.text_changed.emit("3d10+70") + _eq(e.dice_for("wilderness"), "3d10+70", "the dice land on the thread") + main.undo_redo._commit_edits() + toggle.pressed.emit() + _check((boxes[last.id] as CheckBox).is_visible_in_tree(), "the list opens") + (boxes[last.id] as CheckBox).button_pressed = true + _eq(e.reroll_for("wilderness"), [last.id] as Array[String], "ticking adds the result") + _check(toggle.text.contains("96–00"), "the heading names it: %s" % toggle.text) + main.undo_redo._commit_edits() + + # Quick edits to one card merge into one undo step (EditHistory.COALESCE_SECS). + main.undo_redo.undo() + await _frames(2) + e = _road() + _check(e.reroll_for("wilderness").is_empty(), "undo takes the tick back") + _eq(e.dice_for("wilderness"), "", "and the dice") + main.undo_redo.redo() + await _frames(2) + e = _road() + _eq(e.dice_for("wilderness"), "3d10+70", "redo brings the dice back") + _eq(e.reroll_for("wilderness"), [last.id] as Array[String], "and the tick") + var boxes2: Dictionary = _parts("wilderness").get("reroll", {}) + _check(boxes2.has(last.id) and (boxes2[last.id] as CheckBox).button_pressed, "the inspector shows the tick again") + _check(DescribeCards.pipe(e, "wilderness", main.data).contains("again on 96–00"), "the thread's label says so") + + +func _test_plain_target() -> void: + print("Card without ranges") + var e := main.data.get_table("wilderness").entries[0] + main.inspecting.select_pipe(Roller.pipe_key("wilderness", e.id, "road")) + await _frames(3) + var p := _parts("road") + _check(not p.has("dice"), "no Roll it with field for a card without ranges") + _check(p.has("reroll"), "but the roll-again list is there") diff --git a/tests/test_ui_threads.gd.uid b/tests/test_ui_threads.gd.uid new file mode 100644 index 0000000..6c1c981 --- /dev/null +++ b/tests/test_ui_threads.gd.uid @@ -0,0 +1 @@ +uid://dfak6dtnel5he diff --git a/ui/inspector.gd b/ui/inspector.gd index dc6937c..91de9b8 100644 --- a/ui/inspector.gd +++ b/ui/inspector.gd @@ -250,6 +250,13 @@ func set_pipe_color(hex: String, to: String) -> void: InspectorThread.set_color(self, hex, to) +func set_pipe_dice(to: String, s: String) -> void: + InspectorThreadRolls.set_dice(self, to, s) + + +func set_pipe_reroll(to: String, rid: String, on: bool) -> void: + InspectorThreadRolls.set_reroll(self, to, rid, on) + func remove_pipe(to: String) -> void: var t := selected_table() diff --git a/ui/inspector_thread.gd b/ui/inspector_thread.gd index bfe0a9b..11098df 100644 --- a/ui/inspector_thread.gd +++ b/ui/inspector_thread.gd @@ -245,6 +245,7 @@ static func box(insp: Inspector, t: RollTable, e: TableEntry, to: String) -> Con v.add_child(cr) v.add_child(sr) v.add_child(cerr) + InspectorThreadRolls.rows(insp, e, to, target, v, parts) var rm := InspectorKit.link("Delete thread", "Delete this thread") rm.add_theme_color_override("font_color", Style.DANGER_INK) diff --git a/ui/inspector_thread_rolls.gd b/ui/inspector_thread_rolls.gd new file mode 100644 index 0000000..d6787a7 --- /dev/null +++ b/ui/inspector_thread_rolls.gd @@ -0,0 +1,132 @@ +class_name InspectorThreadRolls +extends RefCounted +## The thread inspector's "how the card is rolled" options: roll it with other +## dice instead (a card with numbered ranges only), and roll again on some of +## its results. InspectorThread.box() adds rows() to the thread box. + +## Results the "Roll again" list shows before it scrolls, and its height then. +const LIST_ROWS := 7 +const LIST_MAX_H := 196.0 +## Inspector meta: the thread targets whose "Roll again" list is open. +const OPEN_META := "reroll_open" + + +## "" when `s` is dice the thread can roll with (empty = the card's own). +static func dice_error(s: String) -> String: + var d := s.strip_edges() + if d == "" or Dice.parse(d).valid or Expr.check(d) == "": + return "" + return "That isn't something the app can roll. Try dice such as 3d10+70." + + +static func set_dice(insp: Inspector, to: String, s: String) -> void: + var e := insp.selected_entry() + if e == null or dice_error(s) != "" or s.strip_edges() == e.dice_for(to): + return + e.set_pipe_dice(to, s) + insp.wrote(insp.selected_table()) + + +## Ticks (on) or unticks the target's result `rid` in the thread's "roll again" list. +static func set_reroll(insp: Inspector, to: String, rid: String, on: bool) -> void: + var e := insp.selected_entry() + if e == null: + return + var ids := e.reroll_for(to) + if ids.has(rid) == on: + return + if on: + ids.append(rid) + else: + ids.erase(rid) + e.set_pipe_reroll(to, ids) + insp.wrote(insp.selected_table()) + + +## Adds the rows for the thread to `target` to `v`; fills `parts` with +## "dice" (LineEdit), "reroll_toggle" (Button) and "reroll" (rid -> CheckBox). +static func rows(insp: Inspector, e: TableEntry, to: String, target: RollTable, v: VBoxContainer, parts: Dictionary) -> void: + if target == null or target.kind != RollTable.KIND_ROLL or target.entries.is_empty(): + return + if target.is_ranged(): + _dice_row(insp, e, to, target, v, parts) + _reroll_rows(insp, e, to, target, v, parts) + + +static func _dice_row(insp: Inspector, e: TableEntry, to: String, target: RollTable, v: VBoxContainer, parts: Dictionary) -> void: + var row := InspectorKit.flow() + row.add_child(InspectorKit.word("Roll it with")) + var le := InspectorKit.amount(e.dice_for(to), target.dice) + le.tooltip_text = "Other dice to roll %s with this time, such as 3d10+70. Leave it empty to use the card's own dice (%s)." % [target.name, target.dice] + InspectorKit.tag(le, "pipe.%s.dice" % to) + row.add_child(le) + row.add_child(InspectorKit.word("instead")) + var err := InspectorKit.error_label() + le.text_changed.connect(func(s: String) -> void: + var msg := dice_error(s) + InspectorKit.show_error(err, msg) + InspectorKit.mark(le, msg == "") + set_dice(insp, to, s) + ) + parts["dice"] = le + v.add_child(row) + v.add_child(err) + + +static func _reroll_rows(insp: Inspector, e: TableEntry, to: String, target: RollTable, v: VBoxContainer, parts: Dictionary) -> void: + var open: Dictionary = insp.get_meta(OPEN_META, {}) + var toggle := InspectorKit.link("", "Pick results that are rolled again when they come up") + toggle.alignment = HORIZONTAL_ALIGNMENT_LEFT + toggle.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + toggle.size_flags_horizontal = Control.SIZE_EXPAND_FILL + v.add_child(toggle) + var scroll := ScrollContainer.new() + scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED + # Short lists show whole; longer ones scroll inside a capped box. + if target.entries.size() <= LIST_ROWS: + scroll.vertical_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED + else: + scroll.custom_minimum_size.y = LIST_MAX_H + var list := InspectorKit.vbox(0) + scroll.add_child(list) + var boxes := {} + for i in target.entries.size(): + var te := target.entries[i] + var text := Formatter.strip_tags(te.text).strip_edges() + var num := target.num_label(te, i) + var cb := InspectorKit.check_box(("%s %s" % [num, text]).strip_edges() if text != "" else num, e.reroll_for(to).has(te.id)) + cb.add_theme_font_override("font", Style.body()) + cb.add_theme_font_size_override("font_size", 14) + cb.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS + cb.clip_text = true + cb.size_flags_horizontal = Control.SIZE_EXPAND_FILL + cb.tooltip_text = cb.text + var rid := te.id + cb.toggled.connect(func(on: bool) -> void: + set_reroll(insp, to, rid, on) + _label_toggle(toggle, insp, to, target, scroll.visible) + ) + boxes[te.id] = cb + list.add_child(cb) + v.add_child(scroll) + var show := func(on: bool) -> void: + scroll.visible = on + var o: Dictionary = insp.get_meta(OPEN_META, {}) + if on: + o[to] = true + else: + o.erase(to) + insp.set_meta(OPEN_META, o) + _label_toggle(toggle, insp, to, target, on) + toggle.pressed.connect(func() -> void: show.call(not scroll.visible)) + show.call(open.has(to)) + parts["reroll_toggle"] = toggle + parts["reroll"] = boxes + + +## "Roll again if it comes up: 96–00 ▸" (or "never"). +static func _label_toggle(toggle: Button, insp: Inspector, to: String, target: RollTable, open: bool) -> void: + var e := insp.selected_entry() + var ids: Array = e.reroll_for(to) if e else [] + var picked := DescribeCards.reroll_names(ids, target) if not ids.is_empty() else "never" + toggle.text = "Roll again if it comes up: %s %s" % [picked, "▾" if open else "▸"] diff --git a/ui/inspector_thread_rolls.gd.uid b/ui/inspector_thread_rolls.gd.uid new file mode 100644 index 0000000..c478573 --- /dev/null +++ b/ui/inspector_thread_rolls.gd.uid @@ -0,0 +1 @@ +uid://dainfeh5iath3