diff --git a/core/roll_range.gd b/core/roll_range.gd index ec567c0..92035c6 100644 --- a/core/roll_range.gd +++ b/core/roll_range.gd @@ -234,9 +234,23 @@ static func index_for(t: RollTable, value: int) -> int: ## Gaps, overlaps and (on a table that doesn't clamp) results the dice can't ## reach, or "" when clean. +## The first range whose start is above its end ("9–4"), or "". Such a card +## can't be rolled until it's fixed (the editor no longer swaps the ends). +static func backwards(t: RollTable) -> String: + if not t.is_ranged(): + return "" + for e in t.entries: + if e.has_range and e.range_lo > e.range_hi: + return "%s–%s" % [fmt(t, e.range_lo), fmt(t, e.range_hi)] + return "" + + static func problems(t: RollTable) -> String: if not t.is_ranged(): return "" + var back := backwards(t) + if back != "": + return "The range %s runs backwards: make its start no bigger than its end. The card can't be rolled until then." % back var lo := min_value(t) var hi := max_value(t) if clamps(t): diff --git a/core/template_library.gd b/core/template_library.gd index 3c0762d..bcea5cf 100644 --- a/core/template_library.gd +++ b/core/template_library.gd @@ -117,6 +117,16 @@ func _write(id: String, tpl: Dictionary) -> bool: return true +## Writes over a user template, keeping its id ("Replace" when saving, and +## Edit… in the template list). Built-ins can't be replaced. +func replace(id: String, tpl: Dictionary) -> bool: + if is_builtin(id) or not exists(id): + return false + var out := tpl.duplicate(true) + out.erase("replace_id") + return _write(id, out) + + ## Renames a user template (the file keeps its id). Built-ins can't be renamed. func rename(id: String, new_name: String) -> bool: if is_builtin(id) or new_name.strip_edges() == "": diff --git a/tests/test_small_ui.gd b/tests/test_small_ui.gd new file mode 100644 index 0000000..8f3065f --- /dev/null +++ b/tests/test_small_ui.gd @@ -0,0 +1,100 @@ +extends SceneTree +## Small UI items: backwards ranges warn and block rolling (no auto-swap), the +## inspector's thread lists, replacing a template, the tray chip's bold source +## name, and the export menu's Other formats. +## Run: godot --headless --path . -s res://tests/test_small_ui.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) + await _frames(5) + await _test_backwards_range() + await _test_thread_lists() + _test_template_replace() + await _test_chip_and_export() + print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) + quit(1 if _failures else 0) + + +func _test_backwards_range() -> void: + var t := main.data.get_table("wilderness") + var e := t.entries[0] + e.range_lo = 40 + e.range_hi = 10 + var card := main.card_for("wilderness") + card.update_warning() + _check(e.range_lo == 40 and e.range_hi == 10, "the ends aren't swapped") + _check(RollRange.backwards(t) != "" and t.range_problems().contains("runs backwards"), "a warning says the range runs backwards") + _check(card.header_ui.roll_button.disabled, "the die button is greyed out") + main.rolling.start_roll("wilderness") + await _frames() + _check(not str(main.current).contains("wilderness") or main.current.is_empty() or main.current.get("table_id", "") != "wilderness", "it can't be rolled") + e.range_lo = 1 + e.range_hi = 30 + card.update_warning() + _check(not card.header_ui.roll_button.disabled, "fixing it enables the die again") + + +func _test_thread_lists() -> void: + main.set_mode(Main.Mode.BUILD) + await _frames() + main.select_card("road") + await _frames(3) + var texts: Array[String] = [] + for b in main.inspector.find_children("*", "Button", true, false): + texts.append((b as Button).text) + var heads: Array[String] = [] + for l in main.inspector.find_children("*", "Label", true, false): + heads.append((l as Label).text) + _check(heads.any(func(s: String) -> bool: return s.begins_with("THREADS IN (")), "the card inspector lists threads in") + _check(texts.any(func(s: String) -> bool: return s.begins_with("Wilderness Travel · ")), "a thread in names its card and result: %s" % str(texts.slice(0, 6))) + _check(texts.any(func(s: String) -> bool: return s.contains(" → ")), "threads out name where they go") + + +func _test_template_replace() -> void: + var lib := main.templating.templates() + var tpl := TemplateKit.make(main.data, ["road"], "card", "Road") + var id := lib.add(tpl) + var changed := tpl.duplicate(true) + changed["name"] = "Road, revised" + changed["replace_id"] = id + main.templating.store_template(changed) + _check(lib.get_template(id).get("name") == "Road, revised", "Replace saves over the same template") + _check(not lib.get_template(id).has("replace_id"), "without the replace marker") + var count := lib.list().size() + main.templating.store_template(tpl.duplicate(true)) + _check(lib.list().size() == count + 1, "a plain save still adds a new one") + + +func _test_chip_and_export() -> void: + main.set_mode(Main.Mode.PLAY) + main.rolling.start_roll("road", true) + await _frames(4) + var bold := false + for l in main.tray.find_children("*", "Label", true, false): + var lab := l as Label + if lab.text == "Road Encounter" and lab.get_theme_font("font") == Style.body_bold(): + bold = true + _check(bold, "the chip names its card in bold") + var pm := main.top_bar._board_menu.get_popup() + var subs := pm.find_children("*", "PopupMenu", false, false) + _check(subs.size() == 1 and (subs[0] as PopupMenu).item_count == IoMenu.EXPORTS.size() - 1, "rare formats sit under Other formats") + + +func _frames(n: int = 2) -> void: + for i in n: + await process_frame + + +func _check(ok: bool, what: String) -> void: + if not ok: + _failures += 1 + print("FAIL ", what) diff --git a/tests/test_small_ui.gd.uid b/tests/test_small_ui.gd.uid new file mode 100644 index 0000000..2210942 --- /dev/null +++ b/tests/test_small_ui.gd.uid @@ -0,0 +1 @@ +uid://bom7e8k2tk2mn diff --git a/ui/inspector_card.gd b/ui/inspector_card.gd index afaeef7..7d3b0be 100644 --- a/ui/inspector_card.gd +++ b/ui/inspector_card.gd @@ -56,6 +56,7 @@ static func build(insp: Inspector, box: VBoxContainer, t: RollTable) -> void: if t.kind != RollTable.KIND_VARIABLE: box.add_child(_inputs(insp, t)) + InspectorThreads.build(insp, box, t) var more := InspectorKit.link("More settings…", "Deck mode, colours and the other settings") more.pressed.connect(func() -> void: insp.main.open_table_settings(t.id)) diff --git a/ui/inspector_threads.gd b/ui/inspector_threads.gd new file mode 100644 index 0000000..4d8ad95 --- /dev/null +++ b/ui/inspector_threads.gd @@ -0,0 +1,50 @@ +class_name InspectorThreads +extends RefCounted +## The card inspector's "Threads in" and "Threads out" lists: every thread +## leading into the card (from which card and result) and out of it (from +## which result, to which card). Clicking one selects that thread. + + +static func build(insp: Inspector, box: VBoxContainer, t: RollTable) -> void: + var board := insp.main.data + var ins := VBoxContainer.new() + for src in board.tables: + for e in src.entries: + if e.pipes.has(t.id): + ins.add_child(_link(insp, src, e, t.id, "%s · %s" % [_name(src.name), _result(e)])) + var outs := VBoxContainer.new() + for e in t.entries: + for to in e.pipes: + var target := board.get_table(to) + outs.add_child(_link(insp, t, e, to, "%s → %s" % [_result(e), _name(target.name) if target else to + " (missing)"])) + if ins.get_child_count() == 0: + ins.add_child(InspectorKit.note("Nothing leads here yet.")) + if outs.get_child_count() == 0: + outs.add_child(InspectorKit.note("No threads leave this card.")) + box.add_child(InspectorKit.field("Threads in (%d)" % _count(ins), ins)) + box.add_child(InspectorKit.field("Threads out (%d)" % _count(outs), outs)) + + +static func _link(insp: Inspector, from: RollTable, e: TableEntry, to: String, text: String) -> Button: + var key := Roller.pipe_key(from.id, e.id, to) + var b := InspectorKit.link(text, "Select this thread") + b.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS + b.clip_text = true + b.size_flags_horizontal = Control.SIZE_EXPAND_FILL + var own := str(e.pipe_color.get(to, "")) + if own != "": + b.add_theme_color_override("font_color", Color(own)) + b.pressed.connect(func() -> void: insp.main.inspecting.select_pipe(key)) + return b + + +static func _count(v: VBoxContainer) -> int: + return v.get_children().filter(func(c: Node) -> bool: return c is Button).size() + + +static func _name(s: String) -> String: + return Inspector._title_of(s, "(no name)") + + +static func _result(e: TableEntry) -> String: + return Inspector._title_of(e.text, "(no text)") diff --git a/ui/inspector_threads.gd.uid b/ui/inspector_threads.gd.uid new file mode 100644 index 0000000..abd8ae2 --- /dev/null +++ b/ui/inspector_threads.gd.uid @@ -0,0 +1 @@ +uid://r476bps2k4rr diff --git a/ui/io_menu.gd b/ui/io_menu.gd index 8c6c4db..fe9a043 100644 --- a/ui/io_menu.gd +++ b/ui/io_menu.gd @@ -47,9 +47,15 @@ func setup(can_reset: bool, can_import: bool) -> void: pm.add_item("Import as new spread…", ID_IMPORT) pm.set_item_disabled(pm.get_item_index(ID_IMPORT), not can_import) pm.add_separator("Export this spread") - for i in EXPORTS.size(): - pm.add_item("Export as " + str(EXPORTS[i]["label"]) + "…", ID_EXPORT + i) - pm.id_pressed.connect(func(id: int) -> void: + pm.add_item("Export as " + str(EXPORTS[0]["label"]) + "…", ID_EXPORT) + # Formats for other tools (diagram and graph programs) sit one level down. + var other := PopupMenu.new() + ModalCard.style_popup(other) + for i in range(1, EXPORTS.size()): + other.add_item(str(EXPORTS[i]["label"]) + "…", ID_EXPORT + i) + pm.add_child(other) + pm.add_submenu_node_item("Other formats", other) + var on_id := func(id: int) -> void: if id == ID_RESET: reset_requested.emit() elif id == ID_TEST: @@ -60,7 +66,8 @@ func setup(can_reset: bool, can_import: bool) -> void: import_requested.emit() elif id >= ID_EXPORT and id < ID_EXPORT + EXPORTS.size(): export_requested.emit(EXPORTS[id - ID_EXPORT]) - ) + pm.id_pressed.connect(on_id) + other.id_pressed.connect(on_id) # ---------- Export ---------- diff --git a/ui/main_rolling.gd b/ui/main_rolling.gd index 1c63784..cbeb537 100644 --- a/ui/main_rolling.gd +++ b/ui/main_rolling.gd @@ -29,6 +29,10 @@ func start_roll(tid: String, fast: bool = false) -> void: var t := m.data.get_table(tid) if t == null or t.kind == RollTable.KIND_VARIABLE: return + var back := RollRange.backwards(t) + if back != "": + m.toast("%s can't roll: its range %s runs backwards" % [Formatter.strip_tags(t.name), back]) + return # A minimized question keeps waiting; a new roll must not cancel it. if _choice_waiting("Answer the waiting question first"): return diff --git a/ui/main_templates.gd b/ui/main_templates.gd index 0559d1e..a8841f3 100644 --- a/ui/main_templates.gd +++ b/ui/main_templates.gd @@ -34,13 +34,27 @@ func save_template_from(table_ids: Array, kind: String, group_ids: Array = []) - return if kind == "selection": tpl["name"] = "%d cards" % (tpl["tables"] as Array).size() - m._add_modal(TemplateForms.save_dialog(tpl, func(out: Dictionary) -> void: - var id := templates().add(out) - if id == "": - m.toast("Couldn't save the template") + var mine: Array = [] + for r: Dictionary in templates().list(): + if not TemplateLibrary.is_builtin(str(r["id"])): + mine.append([str(r["id"]), str(r["name"])]) + m._add_modal(TemplateForms.save_dialog(tpl, store_template, mine)) + + +## Saves a template from the save dialog: new, or over the one it names. +func store_template(out: Dictionary) -> void: + var rid := str(out.get("replace_id", "")) + if rid != "": + if templates().replace(rid, out): + m.toast("Updated template “%s”" % out["name"]) else: - m.toast("Saved template “%s”. Insert it from + Card ▾ → From template…" % out["name"]) - )) + m.toast("Couldn't update the template") + return + var id := templates().add(out) + if id == "": + m.toast("Couldn't save the template") + else: + m.toast("Saved template “%s”. Insert it from + Card ▾ → From template…" % out["name"]) ## The selection as a template; groups whose cards are all selected come along. diff --git a/ui/table_card.gd b/ui/table_card.gd index 332b57a..be0157a 100644 --- a/ui/table_card.gd +++ b/ui/table_card.gd @@ -324,6 +324,10 @@ func update_warning() -> void: problems = ("%s\n" % problems if problems != "" else "") + "\n".join(lines) _warn_label.text = problems _warn_panel.visible = problems != "" + if is_instance_valid(header_ui.roll_button): + var back := RollRange.backwards(table) != "" + header_ui.roll_button.disabled = back + header_ui.roll_button.modulate.a = 0.4 if back else 1.0 func _skin_scrollbar(bar: VScrollBar) -> void: diff --git a/ui/table_card_editing.gd b/ui/table_card_editing.gd index 0531478..9904084 100644 --- a/ui/table_card_editing.gd +++ b/ui/table_card_editing.gd @@ -191,10 +191,6 @@ func range_edit(e: TableEntry, is_lo: bool) -> LineEdit: e.range_lo = v else: e.range_hi = v - if e.range_lo > e.range_hi: - var tmp := e.range_lo - e.range_lo = e.range_hi - e.range_hi = tmp edit.text = str(e.range_lo if is_lo else e.range_hi) card.update_warning() card.main.mark_dirty() diff --git a/ui/table_card_header.gd b/ui/table_card_header.gd index 026ed7c..a3aef58 100644 --- a/ui/table_card_header.gd +++ b/ui/table_card_header.gd @@ -7,6 +7,8 @@ extends RefCounted var card: TableCard var die_icon: DieIcon +## The die button (disabled while a range runs backwards). +var roll_button: Button var deck_label: Label ## Play mode: "3 available now" on a dynamic pick card. var options_label: Label @@ -138,6 +140,7 @@ func _subtitle() -> String: func _make_go_button() -> Button: var table := card.table die_icon = null + roll_button = null if table.kind == RollTable.KIND_VARIABLE: return null var b := Button.new() @@ -203,6 +206,7 @@ func _make_go_button() -> Button: b.add_child(die) b.resized.connect(func() -> void: die.position = Vector2(8, (b.size.y - 20.0) / 2.0 - 1.0)) b.pressed.connect(func() -> void: card.main.start_roll(table.id)) + roll_button = b return b diff --git a/ui/template_forms.gd b/ui/template_forms.gd index 795c703..7d20d16 100644 --- a/ui/template_forms.gd +++ b/ui/template_forms.gd @@ -7,8 +7,12 @@ extends RefCounted ## The "Save as template" dialog. `on_save` gets the template with the name, ## description and parameter labels and defaults the user typed. -static func save_dialog(tpl: Dictionary, on_save: Callable) -> ModalCard: - var d := ModalCard.new("Save as template", 580.0) +## `replaceable` lists [id, name] of the user's templates, offered as "Replace +## …" targets; `replace_id` preselects one (Edit… in the template list). The +## saved Dictionary carries "replace_id" when one was chosen. +static func save_dialog(tpl: Dictionary, on_save: Callable, replaceable: Array = [], replace_id: String = "") -> ModalCard: + var d := ModalCard.new("Edit template" if replace_id != "" and replaceable.is_empty() else "Save as template", 580.0) + var target := [replace_id] var top := HBoxContainer.new() top.add_theme_constant_override("separation", 16) var pv := preview(tpl, Vector2(150, 92)) @@ -22,6 +26,18 @@ static func save_dialog(tpl: Dictionary, on_save: Callable) -> ModalCard: name_edit.text = str(tpl.get("name", "")) ModalCard.style_line_edit(name_edit) fields.add_child(ModalCard.field("Name", name_edit)) + if not replaceable.is_empty(): + var ob := OptionButton.new() + ModalCard.style_popup(ob.get_popup()) + ob.add_item("A new template") + for r: Array in replaceable: + ob.add_item("Replace “%s”" % r[1]) + ob.tooltip_text = "Save over one of your templates to update it, instead of making another" + ob.item_selected.connect(func(i: int) -> void: + target[0] = "" if i == 0 else str(replaceable[i - 1][0]) + if i > 0: + name_edit.text = str(replaceable[i - 1][1])) + fields.add_child(ModalCard.field("Save as", ob)) fields.add_child(ModalCard.note(TemplateKit.summary(tpl))) d.body.add_child(top) @@ -72,7 +88,7 @@ static func save_dialog(tpl: Dictionary, on_save: Callable) -> ModalCard: d.body.add_child(row) rows.append([p, label, def]) d.add_button("Cancel", "plain", d.close) - d.add_button("Save template", "primary", func() -> void: + d.add_button("Save template" if replace_id == "" else "Save changes", "primary", func() -> void: var out := tpl.duplicate(true) out["name"] = name_edit.text.strip_edges() if name_edit.text.strip_edges() != "" else "Untitled template" out["description"] = desc.text.strip_edges() @@ -81,6 +97,8 @@ static func save_dialog(tpl: Dictionary, on_save: Callable) -> ModalCard: var p: Dictionary = r[0] plist.append({"name": p["name"], "label": (r[1] as LineEdit).text.strip_edges(), "default": (r[2] as LineEdit).text}) out["params"] = plist + if target[0] != "": + out["replace_id"] = target[0] d.close() on_save.call(out) ) diff --git a/ui/template_picker.gd b/ui/template_picker.gd index 2442067..03bc391 100644 --- a/ui/template_picker.gd +++ b/ui/template_picker.gd @@ -145,7 +145,7 @@ func _row(r: Dictionary) -> Control: var tools := VBoxContainer.new() tools.add_theme_constant_override("separation", 2) tools.size_flags_vertical = SIZE_SHRINK_BEGIN - for spec in [["Rename", func() -> void: _rename(id, str(r["name"]))], + for spec in [["Edit…", func() -> void: _edit(id)], ["Export", func() -> void: main.export_template(id)], ["Delete", func() -> void: _delete(id, str(r["name"]))]]: var b := Button.new() @@ -180,11 +180,16 @@ func _choose(id: String) -> void: main.begin_template_insert(tpl) -func _rename(id: String, old: String) -> void: - main._add_modal(ModalCard.ask_text("Rename template", "Name", old, "Rename", func(n: String) -> void: - if lib.rename(id, n): - refresh() - )) +## Edits a template's name, description and fill-in labels in place. +func _edit(id: String) -> void: + var tpl := lib.get_template(id) + if tpl.is_empty(): + main.toast("Couldn't read that template") + return + main._add_modal(TemplateForms.save_dialog(tpl, func(out: Dictionary) -> void: + out["replace_id"] = id + main.templating.store_template(out) + refresh(), [], id)) func _delete(id: String, tname: String) -> void: diff --git a/ui/test_board_dialog.gd b/ui/test_board_dialog.gd index 12e1ce6..b9340d9 100644 --- a/ui/test_board_dialog.gd +++ b/ui/test_board_dialog.gd @@ -244,7 +244,7 @@ func _show_report(report: Dictionary, problems: Array) -> void: items.append({"table_id": p["table_id"], "entry_id": p["entry_id"], "text": "%s%s: %s" % [_table_label(p["table_id"]) if board.get_table(p["table_id"]) != null else "Spread", "" if str(p["entry_id"]) == "" else ", result " + str(p["entry_id"]), p["text"]]}) - _add_section("Problems", "Static checks", items, true) + _add_section("Problems", "Mistakes found by reading the cards, before any rolling: a thread to a missing card, a rule that can't work…", items, true) # Errors: effect / expression errors, unset markers and action-limit halts items = [] for e: Dictionary in report["errors"]: @@ -258,15 +258,15 @@ func _show_report(report: Dictionary, problems: Array) -> void: items.append({"table_id": str(top[0]["table"]) if not top.is_empty() else "", "entry_id": "", "text": "%d runs stopped: %s%s" % [report["halted"][why], why, " (busiest table: %s)" % _table_label(str(top[0]["table"])) if not top.is_empty() else ""]}) - _add_section("Errors", "Runtime errors", items, true) + _add_section("Errors", "Things that went wrong while the test rolls ran", items, true) items = [] for e: Dictionary in report["dead_ends"]: items.append({"table_id": e["table_id"], "entry_id": "", "text": "%dx %s: %s" % [e["count"], e["table"], e["text"]]}) - _add_section("Dead ends", "Rolls that stopped", items, true) + _add_section("Dead ends", "Rolls that stopped before they finished", items, true) items = [] for id: String in report["unreached"]: items.append({"table_id": id, "entry_id": "", "text": _table_label(id)}) - _add_section("Never reached", "Tables no run got to", items, false) + _add_section("Never reached", "Cards none of the test rolls got to", items, false) items = [] var uc: Dictionary = report["unchosen"] for id: String in uc: @@ -274,7 +274,7 @@ func _show_report(report: Dictionary, problems: Array) -> void: var ids: Array = report["unchosen_ids"][id] for k in texts.size(): items.append({"table_id": id, "entry_id": ids[k], "text": "%s: \"%s\"" % [_table_label(id), texts[k]]}) - _add_section("Never chosen", "Results no run picked", items, false) + _add_section("Never chosen", "Results none of the test rolls picked", items, false) if str(report["sample_sheet"]) != "": var sheet := Style.make_label("Sample sheet", Style.caps(), 12, Style.MUTED) diff --git a/ui/tray_chips.gd b/ui/tray_chips.gd index 40c23f6..e2bbc63 100644 --- a/ui/tray_chips.gd +++ b/ui/tray_chips.gd @@ -34,8 +34,19 @@ static func chip(tray: ResultTray, step: Dictionary, last: bool, steps: Array = var has_details := main.details().step_has_details(step) if has_details: caption += " " + RichText.INFO_MARK - var cap := Style.make_label(caption, Style.body(), 11 if inline else 12, Style.MUTED) + # The card it came from reads first, bold and dark; how it was rolled follows, muted. + var source := Formatter.strip_tags(str(step["table"])) + var cap := HBoxContainer.new() + cap.add_theme_constant_override("separation", 0) cap.mouse_filter = Control.MOUSE_FILTER_IGNORE + var fs := 12 if inline else 13 + if source != "" and caption.begins_with(source): + cap.add_child(Style.make_label(source, Style.body_bold(), fs, Style.INK)) + caption = caption.substr(source.length()) + if caption != "": + cap.add_child(Style.make_label(caption, Style.body(), fs - 1, Style.MUTED)) + for l in cap.get_children(): + (l as Control).mouse_filter = Control.MOUSE_FILTER_IGNORE v.add_child(cap) var raw := str(step["text"]) if str(step.get("kind", "")) == RollTable.KIND_SHEET: