diff --git a/help/topics.txt b/help/topics.txt index bc9d46b..967a9f8 100644 --- a/help/topics.txt +++ b/help/topics.txt @@ -53,6 +53,16 @@ A [b]ranged table[/b] gives each result a range of numbers instead of a weight, Dice can be written `2d6`, `d20`, `3D` or `D3`. A missing count means 1 and missing sides mean 6, so `2D` is 2d6. To add a value to the dice, use a [topic]Roll modifiers[/topic] modifier. +# Columns +A card can give each result more than one part, like a row of a printed table with several columns. A [b]World[/b] card might have the columns Type | Resources | Water | Atmosphere | Life, and each row fills them all in: Garden · Rich · Some · Thin · Yes. +- In Build mode, select the card and press [b]+ Add columns[/b] in the inspector. The first column is each result's own text; the card gets a heading row with the column names. +- Type each column's text right on the card, or in the inspector when a result is selected. +- [b]One roll for the whole row[/b] picks a row and gives you every column of it. +- [b]Roll each column separately[/b] rolls again for every column, for rules like "roll 1d10 for each of Resources, Water, Atmosphere and Life". After the roll, the part each column landed on is lit. +- [b]Save as[/b] keeps a column's text as a value, so later text and rules can use it. +- [b]Only roll this column if[/b] skips a column unless a condition is true, such as "only roll Life if the Atmosphere is at least Thin". Columns are rolled left to right, so a condition can use what an earlier column saved. +- Remove a column with its ×: its text goes from every result. + # Threads A thread carries the roll from a result to another card. - In Build mode, drag from the ● at the right of a result onto another card to lay a thread. diff --git a/tests/test_ui_columns.gd b/tests/test_ui_columns.gd new file mode 100644 index 0000000..ee47b8a --- /dev/null +++ b/tests/test_ui_columns.gd @@ -0,0 +1,222 @@ +extends TestBase +## Cards with columns on the spread and in the inspector: the header and the +## cells, the lit cells after a roll, inline cell editing, and the inspector's +## Columns section (add, rename, move, remove, roll mode, save as, only if) +## with the result's cell fields, all undoable. +## Run: godot --headless --path . -s res://tests/test_ui_columns.gd -- --sample + +var insp: Inspector + + +func _init() -> void: + if not await start_main(): + return + main.set_anchors_and_offsets_preset(Control.PRESET_TOP_LEFT) + main.size = Vector2(1440, 900) + insp = main.inspector + await _load() + await _test_card() + await _test_lit_cells() + await _test_real_roll() + await _test_build_cells() + await _test_inspector_columns() + await _test_result_cells() + finish() + + +func _board_dict() -> Dictionary: + return {"format_version": 2, "id": "cols-test", "name": "Columns test", "tables": [ + {"id": "world", "name": "World", "x": 40, "y": 40, "column_rolls": "each", + "columns": [{"name": "Type"}, {"name": "Resources", "save_as": "@res"}, {"name": "Water"}, {"name": "Life", "when": "@res == \"Rich\""}], + "entries": [ + {"id": "e1", "text": "Rocky", "cells": ["Poor", "None", "No"]}, + {"id": "e2", "text": "Ocean", "cells": ["Fair", "Plentiful", "Yes"]}, + {"id": "e3", "text": "Garden", "cells": ["Rich", "Some", "Yes"]}]}, + {"id": "rowcard", "name": "Row card", "x": 700, "y": 40, + "columns": [{"name": "Name"}, {"name": "Trait"}], + "entries": [{"id": "e1", "text": "Ana", "cells": ["Bold"]}]}, + {"id": "plain", "name": "Plain", "x": 40, "y": 400, + "entries": [{"id": "e1", "text": "One"}, {"id": "e2", "text": "Two"}]}, + ]} + + +func _load() -> void: + main.data.load_dict(_board_dict()) + main.lit_entries.clear() + main.lit_pipes.clear() + main.lit_texts.clear() + main.lit_columns.clear() + main.persistent_vars.clear() + main.set_mode(Main.Mode.PLAY) + main._rebuild_cards() + main.clear_inspector() + await _frames(4) + + +func _t(id: String) -> RollTable: + return main.data.get_table(id) + + +func _cells(tid: String, eid: String) -> Array: + return main.card_for(tid).rows_ui.columns_ui.cells.get(eid, []) + + +func _header_names(card: TableCard) -> Array: + var out: Array = [] + for c in card.find_children("*", "MarginContainer", true, false): + if c.has_meta("columns_header"): + for l in c.find_children("*", "Label", true, false): + out.append((l as Label).text) + return out + + +# ---------- The card ---------- + +func _test_card() -> void: + print("Card") + var card := main.card_for("world") + _eq(_header_names(card), ["Type", "Resources", "Water", "Life"], "the header names every column") + _eq(_header_names(main.card_for("plain")), [], "a plain card has no header") + var cs := _cells("world", "e2") + _eq(cs.size(), 3, "a row shows a cell per extra column") + _eq((cs[1]["label"] as RichTextLabel).text, "Plentiful", "a cell shows its text") + _check(card.card_width() > main.card_for("plain").card_width() + 3 * 100.0, "a card with columns is wider") + var text: RichTextLabel = card.rows_ui.parts["e2"]["text"] + _eq(text.text, "Ocean", "the row's own text is column 0") + + +func _test_lit_cells() -> void: + print("Lit cells") + var card := main.card_for("world") + main.lit_entries["world:e1"] = true + main.lit_texts["world:e1"] = "Type: Rocky · Resources: Rich · Water: Plentiful" + main.lit_columns["world:e1"] = [{"name": "Type", "text": "Rocky", "index": 0}, + {"name": "Resources", "text": "Rich", "index": 2}, {"name": "Water", "text": "Plentiful", "index": 1}, + {"name": "Life", "text": "", "index": 0, "skipped": true}] + card.refresh_lit() + var text: RichTextLabel = card.rows_ui.parts["e1"]["text"] + _eq(text.text, "Rocky", "the lit row shows column 0, not the whole summary") + var rich: Dictionary = _cells("world", "e3")[0] + _check(bool((rich["box"] as Control).get_meta("lit", false)), "each column lights the cell it landed on") + _eq((rich["label"] as RichTextLabel).text, "Rich", "with the rolled text") + _check(bool((_cells("world", "e2")[1]["box"] as Control).get_meta("lit", false)), "a second column lights another row") + _check(not bool((_cells("world", "e1")[0]["box"] as Control).get_meta("lit", false)), "the lit row's own cell for that column isn't lit") + _check(not bool((_cells("world", "e1")[2]["box"] as Control).get_meta("lit", false)), "a skipped column lights nothing") + main.lit_entries.clear() + main.lit_texts.clear() + main.lit_columns.clear() + card.refresh_lit() + _check(not bool((rich["box"] as Control).get_meta("lit", false)), "clearing the roll unlights the cells") + + +func _test_real_roll() -> void: + print("A real roll") + main.rolling.start_roll("rowcard") + for i in 200: + if main.current.get("done", false): + break + await _frames() + _check(main.is_entry_lit("rowcard", "e1"), "the row lights") + var card := main.card_for("rowcard") + _eq((card.rows_ui.parts["e1"]["text"] as RichTextLabel).text, "Ana", "the row shows its own text") + _eq((_cells("rowcard", "e1")[0]["label"] as RichTextLabel).text, "Bold", "and its cell") + _eq(main.lit_text("rowcard", "e1"), "Name: Ana · Trait: Bold", "(the summary names the columns)") + + +func _test_build_cells() -> void: + print("Build cells") + main.set_mode(Main.Mode.BUILD) + await _frames(2) + var card := main.card_for("world") + _check(card.card_width() > TableCard.BUILD_WIDTH + 3 * 100.0, "Build keeps the room for the columns") + var cs := _cells("world", "e1") + var le: LineEdit = cs[1]["edit"] + _eq(le.text, "None", "Build shows a field per cell") + main.undo_redo._commit_edits() + le.text = "A little" + le.text_changed.emit("A little") + main.undo_redo._commit_edits() + _eq(_t("world").find_entry("e1").cells[1], "A little", "typing in a cell edits it") + main.undo_redo.undo() + await _frames(2) + _eq(_t("world").find_entry("e1").cells[1], "None", "undo puts the cell back") + + +func _test_inspector_columns() -> void: + print("Inspector columns") + var t := _t("plain") + main.select_card("plain") + await _frames(2) + main.undo_redo._commit_edits() + _press("+ Add columns") + _eq(t.column_names(), ["Result", "Column 2"] as Array[String], "Add columns makes Result and one more") + _eq(t.entries[0].cells, ["" ] as Array[String], "every result gets a cell") + _press("+ Add column") + _eq(t.columns.size(), 3, "+ Add column") + _eq(t.entries[1].cells.size(), 2, "the cells keep in step") + var nm: LineEdit = insp.fields["column_name_1"] + nm.text = "Water" + nm.text_changed.emit("Water") + _eq(t.column_names()[1], "Water", "renaming a column") + _eq(_header_names(main.card_for("plain"))[1], "Water", "the card's header follows") + t.entries[0].cells.assign(["wet", "big"]) + (insp.fields["column_name_2"] as LineEdit).text_changed.emit("Size") + InspectorColumns.move_column(t, 2, -1) + insp.wrote(t, false, true) + _eq(t.column_names(), ["Result", "Size", "Water"] as Array[String], "moving a column") + _eq(t.entries[0].cells, ["big", "wet"] as Array[String], "moves its cells too") + InspectorColumns.remove_column(t, 1) + insp.wrote(t, false, true) + _eq(t.column_names(), ["Result", "Water"] as Array[String], "removing a column") + _eq(t.entries[0].cells, ["wet"] as Array[String], "removes its cell from every result") + var mode: OptionButton = insp.fields["column_rolls"] + mode.item_selected.emit(1) + _eq(t.column_rolls, RollTable.COLUMNS_EACH, "Roll each column separately") + (insp.fields["column_save_1"] as ValuePicker).choose("@water") + _eq(str(t.columns[1].get("save_as", "")), "@water", "Save as picks a value") + main.undo_redo._commit_edits() + var cond: ConditionBuilder = insp.fields["column_when_1"] + cond.changed.emit("@luck >= 2") + _eq(str(t.columns[1].get("when", "")), "@luck >= 2", "Only if sets the column's condition") + main.undo_redo._commit_edits() + main.undo_redo.undo() + await _frames(2) + # Quick edits to one card merge into one step (MainHistory), so undo may go further back. + _check(not JSON.stringify(_t("plain").to_dict()).contains("@luck"), "undo takes the condition back") + for i in 12: + main.undo_redo.undo() + await _frames() + if _t("plain").columns.is_empty(): + break + _check(_t("plain").columns.is_empty() and _t("plain").entries[0].cells.is_empty(), "undo goes back to a plain card") + main.select_card("plain") + await _frames(2) + _press("+ Add columns") + InspectorColumns.remove_column(_t("plain"), 1) + _check(_t("plain").columns.is_empty() and _t("plain").entries[0].cells.is_empty(), "removing the last extra column turns columns off") + + +func _test_result_cells() -> void: + print("Result cells") + main.select_result("world", "e2") + await _frames(2) + _check(insp.fields.has("cell_1") and insp.fields.has("cell_3"), "the result shows a field per extra column") + var le: LineEdit = insp.fields["cell_2"] + _eq(le.text, "Plentiful", "with the cell's text") + main.undo_redo._commit_edits() + le.text_changed.emit("Seas") + main.undo_redo._commit_edits() + _eq(_t("world").find_entry("e2").cells[1], "Seas", "editing it changes the cell") + var card_edit: LineEdit = _cells("world", "e2")[1]["edit"] + _eq(card_edit.text, "Seas", "and the card") + main.undo_redo.undo() + await _frames(2) + _eq(_t("world").find_entry("e2").cells[1], "Plentiful", "undo") + + +func _press(label: String) -> void: + for b in insp.find_children("*", "Button", true, false): + if (b as Button).text == label: + (b as Button).pressed.emit() + return + _check(false, "no button %s" % label) diff --git a/tests/test_ui_columns.gd.uid b/tests/test_ui_columns.gd.uid new file mode 100644 index 0000000..81703a2 --- /dev/null +++ b/tests/test_ui_columns.gd.uid @@ -0,0 +1 @@ +uid://d121mi1dm3l0y diff --git a/ui/inspector_card.gd b/ui/inspector_card.gd index 97e64b5..bb86cf4 100644 --- a/ui/inspector_card.gd +++ b/ui/inspector_card.gd @@ -24,6 +24,7 @@ static func build(insp: Inspector, box: VBoxContainer, t: RollTable) -> void: RollTable.KIND_ROLL: InspectorRoll.build(insp, box, t, plain) InspectorRoll.deck(insp, box, t) + InspectorColumns.build(insp, box, t) RollTable.KIND_CHECK: _check(insp, box, t, plain) RollTable.KIND_CHOICE: diff --git a/ui/inspector_columns.gd b/ui/inspector_columns.gd new file mode 100644 index 0000000..9cdd4fb --- /dev/null +++ b/ui/inspector_columns.gd @@ -0,0 +1,220 @@ +class_name InspectorColumns +extends RefCounted +## The inspector's "Columns" section for a roll card (RollTable.columns, a +## multi-column table): "Add columns" turns them on (Result plus one more); +## then one roll for the whole row or each column rolled separately, and a box +## per column with its name, move and remove, "Save as [value ▾]" and (after +## the first) "Only roll this column if …". The result section's cell fields +## come from result_cells(). Writes go through Inspector.wrote, so undo works +## as for any card edit. The editing helpers keep every result's cells in step +## with the columns. + +const ROLL_LABELS := ["One roll for the whole row", "Roll each column separately"] +const ROLL_MODES := [RollTable.COLUMNS_ROW, RollTable.COLUMNS_EACH] +const ROLL_NOTES := [ + "One roll picks a row, and you get every column of that row.", + "The first column is the card's own roll. Each other column is rolled on its own, like “roll 1d10 for each”.", +] + + +static func build(insp: Inspector, box: VBoxContainer, t: RollTable) -> void: + var v := InspectorKit.vbox(8) + if t.columns.is_empty(): + v.add_child(InspectorKit.note("Give each result more than one part, like a row of a printed table: Resources, Water, Atmosphere…", Style.MUTED, 13)) + var add := InspectorKit.link("+ Add columns", "Split the results into columns") + add.pressed.connect(func() -> void: + add_columns(t) + insp.wrote(t, false, true)) + v.add_child(add) + box.add_child(InspectorKit.field("Columns", v)) + return + var mode := InspectorKit.dropdown(ROLL_LABELS, maxi(0, ROLL_MODES.find(t.column_rolls))) + mode.tooltip_text = "Whether one roll gives the whole row, or each column gets its own roll" + InspectorKit.tag(mode, "card.column_rolls") + insp.fields["column_rolls"] = mode + mode.item_selected.connect(func(i: int) -> void: + if t.column_rolls != ROLL_MODES[i]: + t.column_rolls = ROLL_MODES[i] + insp.wrote(t, false, true)) + v.add_child(mode) + v.add_child(InspectorKit.note(ROLL_NOTES[maxi(0, ROLL_MODES.find(t.column_rolls))], Style.MUTED, 13)) + for k in t.columns.size(): + v.add_child(_column_box(insp, t, k)) + var row := InspectorKit.hbox(12) + var more := InspectorKit.link("+ Add column", "Add a column at the end") + more.pressed.connect(func() -> void: + add_column(t) + insp.wrote(t, false, true)) + row.add_child(more) + var off := InspectorKit.link("Remove columns", "Back to one text per result (the first column stays)") + off.add_theme_color_override("font_color", Style.DANGER_INK) + off.pressed.connect(func() -> void: + remove_columns(t) + insp.wrote(t, false, true)) + row.add_child(off) + v.add_child(row) + box.add_child(InspectorKit.field("Columns", v)) + + +static func _column_box(insp: Inspector, t: RollTable, k: int) -> Control: + var col: Dictionary = t.columns[k] + var panel := PanelContainer.new() + panel.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.CARD_SOFT, 8, [10, 8, 8, 8]), 1, Style.RULE)) + var v := InspectorKit.vbox(6) + panel.add_child(v) + + var head := InspectorKit.hbox(4) + var name := InspectorKit.line(str(col.get("name", "")), "Column name") + name.size_flags_horizontal = Control.SIZE_EXPAND_FILL + InspectorKit.tag(name, "col.%d.name" % k) + insp.fields["column_name_%d" % k] = name + name.text_changed.connect(func(s: String) -> void: + t.columns[k]["name"] = s + insp.wrote(t)) + head.add_child(name) + if k > 0: + head.add_child(TableCardEditing.mini_button("↑", "Move this column left", k > 1, func() -> void: + move_column(t, k, -1) + insp.wrote(t, false, true))) + head.add_child(TableCardEditing.mini_button("↓", "Move this column right", k < t.columns.size() - 1, func() -> void: + move_column(t, k, 1) + insp.wrote(t, false, true))) + var rm := InspectorKit.xbutton("Remove this column (and its text on every result)") + rm.pressed.connect(func() -> void: + remove_column(t, k) + insp.wrote(t, false, true)) + head.add_child(rm) + v.add_child(head) + if k == 0: + v.add_child(InspectorKit.note("The result's own text: its effects and threads go with this column.", Style.MUTED, 12)) + + var save := InspectorKit.flow() + save.add_child(InspectorKit.word("Save as")) + var pick := ValuePicker.new(insp.main) + pick.placeholder = "nothing" + pick.set_value(str(col.get("save_as", ""))) + pick.tooltip_text = "Keep this column's text as a value, so later rules and text can use it" + pick.value_chosen.connect(func(n: String) -> void: + t.columns[k]["save_as"] = n + insp.wrote(t, false, true)) + insp.fields["column_save_%d" % k] = pick + save.add_child(pick) + if str(col.get("save_as", "")) != "": + var clear := InspectorKit.xbutton("Don't save this column") + clear.pressed.connect(func() -> void: + t.columns[k].erase("save_as") + insp.wrote(t, false, true)) + save.add_child(clear) + v.add_child(save) + + if k > 0: + var w := str(col.get("when", "")) + var cond := ConditionBuilder.new() + cond.setup(insp.main, w, not insp.main.rules_plain(), "col.%d.when" % k, + {"empty_note": "Always rolled.", "extras": _earlier_saves(insp, t, k), + "add_label": "+ Only if…" if w == "" else "+ Add condition"}) + cond.changed.connect(func(s: String) -> void: + if TableSettingsDialog.when_error(s) == "" and s.strip_edges() != str(t.columns[k].get("when", "")): + set_when(t, k, s) + insp.wrote(t)) + insp.fields["column_when_%d" % k] = cond + v.add_child(InspectorKit.word("Only roll this column if", Style.MUTED)) + v.add_child(cond) + return panel + + +## Values the columns before `k` save, for column k's condition pickers. +static func _earlier_saves(insp: Inspector, t: RollTable, k: int) -> Array: + var known := {} + for kv: Dictionary in Builder.known_values(insp.main.data): + known[str(kv["name"])] = true + var out: Array = [] + for j in k: + var s := str(t.columns[j].get("save_as", "")).strip_edges() + if s != "" and not known.has(s): + out.append({"name": s, "label": str(t.columns[j].get("name", s)), "kind": "text"}) + return out + + +## The result section: one text field per column after the first. +static func result_cells(insp: Inspector, t: RollTable, e: TableEntry) -> void: + if not t.has_columns(): + return + for k in range(1, t.columns.size()): + var nm := str(t.columns[k].get("name", "")) + var le := InspectorKit.line(RollTable.cell_text(e, k), nm) + InspectorKit.tag(le, "res.cell.%d" % k) + insp.fields["cell_%d" % k] = le + le.text_changed.connect(func(s: String) -> void: + if RollTable.cell_text(e, k) != s: + TableCardColumns.set_cell(e, k, s) + insp.wrote(t)) + insp._content.add_child(InspectorKit.field(nm if nm.strip_edges() != "" else "Column %d" % (k + 1), le)) + + +# ---------- Editing (keeps every result's cells in step) ---------- + +## Turns columns on: "Result" (the results' own text) plus one more. +static func add_columns(t: RollTable) -> void: + if not t.columns.is_empty(): + return + t.columns = [{"name": "Result"}] + t.column_rolls = RollTable.COLUMNS_ROW + add_column(t) + + +static func add_column(t: RollTable) -> void: + if t.columns.is_empty(): + add_columns(t) + return + t.columns.append({"name": "Column %d" % (t.columns.size() + 1)}) + for e in t.entries: + _fit(e, t.columns.size() - 1) + + +## Removes column `k` (not the first) and its cell on every result. With only +## the first column left, the card goes back to plain results. +static func remove_column(t: RollTable, k: int) -> void: + if k <= 0 or k >= t.columns.size(): + return + t.columns.remove_at(k) + for e in t.entries: + if k - 1 < e.cells.size(): + e.cells.remove_at(k - 1) + if t.columns.size() <= 1: + remove_columns(t) + + +## Back to a plain card: the results keep their own text, the cells go. +static func remove_columns(t: RollTable) -> void: + t.columns = [] + t.column_rolls = RollTable.COLUMNS_ROW + for e in t.entries: + e.cells.clear() + + +## Moves column `k` left (-1) or right (+1); the first column stays put. +static func move_column(t: RollTable, k: int, by: int) -> void: + var to := k + by + if k <= 0 or to <= 0 or to >= t.columns.size(): + return + var c: Dictionary = t.columns[k] + t.columns[k] = t.columns[to] + t.columns[to] = c + for e in t.entries: + _fit(e, t.columns.size() - 1) + var s := e.cells[k - 1] + e.cells[k - 1] = e.cells[to - 1] + e.cells[to - 1] = s + + +static func set_when(t: RollTable, k: int, s: String) -> void: + if s.strip_edges() == "": + t.columns[k].erase("when") + else: + t.columns[k]["when"] = s.strip_edges() + + +static func _fit(e: TableEntry, n: int) -> void: + while e.cells.size() < n: + e.cells.append("") diff --git a/ui/inspector_columns.gd.uid b/ui/inspector_columns.gd.uid new file mode 100644 index 0000000..db66382 --- /dev/null +++ b/ui/inspector_columns.gd.uid @@ -0,0 +1 @@ +uid://dubl3q1mxs068 diff --git a/ui/inspector_result.gd b/ui/inspector_result.gd index 992a34e..a5878a9 100644 --- a/ui/inspector_result.gd +++ b/ui/inspector_result.gd @@ -69,7 +69,8 @@ static func section(insp: Inspector, t: RollTable, e: TableEntry) -> void: InspectorKit.tag(le, "res.text") le.text_changed.connect(insp.set_entry_text) insp.fields["text"] = le - insp._content.add_child(InspectorKit.field("Text", le)) + insp._content.add_child(InspectorKit.field(Inspector._title_of(str(t.columns[0].get("name", "")), "Text") if t.has_columns() else "Text", le)) + InspectorColumns.result_cells(insp, t, e) var det := InspectorKit.text_box(e.details, "Shown on hover (optional)", 36.0) InspectorKit.tag(det, "res.details") diff --git a/ui/main.gd b/ui/main.gd index caf96da..f0e1d7d 100644 --- a/ui/main.gd +++ b/ui/main.gd @@ -60,6 +60,7 @@ var _saving_enabled := true var decks: Dictionary = {} # table id -> DeckState var persistent_vars: Dictionary = {} # name without "@" -> {"value", "source"} var lit_texts: Dictionary = {} # "tid:eid" -> expanded BBCode from the last roll +var lit_columns: Dictionary = {} # "tid:eid" -> the rolled node's "columns" (TableCardColumns) var _details_popup: DetailsPopup ## Choice cards: func(request: Dictionary) -> Variant, awaited by the roller ## (docs/LOGIC.md §8). It opens the choice pop-up (_ask_player); tests swap in diff --git a/ui/main_rolling.gd b/ui/main_rolling.gd index 2dce18c..3c4e608 100644 --- a/ui/main_rolling.gd +++ b/ui/main_rolling.gd @@ -73,6 +73,7 @@ func start_roll(tid: String, fast: bool = false) -> void: m.lit_entries.clear() m.lit_pipes.clear() m.lit_texts.clear() + m.lit_columns.clear() m.current = {"collection": m.data.id, "root": tid, "root_name": t.name, "time": m.current.get("time", Time.get_unix_time_from_system()), "steps": [], "all": Roller.flatten(tree, []), "done": false, "chain_vars": ctx.chain_vars.duplicate()} if ctx.halted != "": @@ -120,6 +121,7 @@ func show_progress() -> void: m.lit_entries.clear() m.lit_pipes.clear() m.lit_texts.clear() + m.lit_columns.clear() _light_steps(steps) m._refresh_lit() _reveal_steps(steps) @@ -233,6 +235,7 @@ func _play_one(n: Dictionary, my: int, depth: int) -> void: var ek := Roller.entry_key(t.id, e.id) m.lit_entries[ek] = true m.lit_texts[ek] = str(n.get("summary", e.text)) + TableCardColumns.note_lit(m, ek, n) m._refresh_lit() if card: card.reveal(e.id) @@ -371,6 +374,7 @@ func _light_steps(steps: Array) -> void: var ek := Roller.entry_key(tid, str(s["entry_id"])) m.lit_entries[ek] = true m.lit_texts[ek] = str(s.get("text", "")) + TableCardColumns.note_lit(m, ek, s) if d > 0 and not (s.has("pass") or s.has("after")) and stack[d - 1] is Dictionary and (stack[d - 1] as Dictionary).has("entry_id"): var p: Dictionary = stack[d - 1] var ptid := str(p.get("table_id", "")) diff --git a/ui/main_state.gd b/ui/main_state.gd index 9012c62..a5c6361 100644 --- a/ui/main_state.gd +++ b/ui/main_state.gd @@ -25,6 +25,7 @@ func load_local_state() -> void: m.lit_entries.clear() m.lit_pipes.clear() m.lit_texts.clear() + m.lit_columns.clear() _ref_cache.clear() if m.state == null or m.data == null: return diff --git a/ui/table_card.gd b/ui/table_card.gd index d4a8d21..c8e57f5 100644 --- a/ui/table_card.gd +++ b/ui/table_card.gd @@ -301,9 +301,11 @@ func card_width() -> float: return (table.width if table.width > 0.0 else WIDTH) + extra_width() -## Build mode has more controls per row, so cards are a little wider there. +## Build mode has more controls per row, so cards are a little wider there; +## a card with columns is wider by its extra columns. func extra_width() -> float: - return BUILD_WIDTH - WIDTH if main.mode == Main.Mode.BUILD else 0.0 + var cols := rows_ui.columns_ui.extra_width() if rows_ui else 0.0 + return (BUILD_WIDTH - WIDTH if main.mode == Main.Mode.BUILD else 0.0) + cols ## The height the results take now (the scrolling list's, if there is one). diff --git a/ui/table_card_columns.gd b/ui/table_card_columns.gd new file mode 100644 index 0000000..fa9809a --- /dev/null +++ b/ui/table_card_columns.gd @@ -0,0 +1,206 @@ +class_name TableCardColumns +extends RefCounted +## A TableCard's columns (a roll card with RollTable.columns): the small +## header row of column names over the results, each row's cells after its +## text (Play: the text, wrapping; Build: a field per cell, edited inline like +## the result text) and, after a roll, the cell each column landed on lit +## (from the rolled node's "columns", kept in Main.lit_columns). Column 0 is +## the row's own text, so TableCardRows draws it as usual. + +## Width of one extra column's cell. +const CELL_W := 112.0 +## Room the Build row keeps after the cells: the weight box and the ×. +const BUILD_TAIL := 34.0 + 22.0 + 8.0 +const BUILD_TAIL_RANGED := 22.0 + 4.0 +## Room a ranged Play row keeps for its chance ("17%"). +const ODDS_TAIL := 36.0 + +var card: TableCard +## entry id -> Array of {"box", "label" (Play) or "edit" (Build), "k"}. +var cells: Dictionary = {} + + +func _init(p_card: TableCard) -> void: + card = p_card + + +func clear() -> void: + cells.clear() + + +func active() -> bool: + return card.table != null and card.table.has_columns() + + +## The extra card width the columns after the first need. +func extra_width() -> float: + if not active(): + return 0.0 + return (card.table.columns.size() - 1) * (CELL_W + _sep()) + + +func _sep() -> float: + return 4.0 if card.main.mode == Main.Mode.BUILD else 8.0 + + +## The row of column names over the results, lined up with the cells. +func make_header(build: bool, ranged: bool, numw: float) -> Control: + var t := card.table + var pad := MarginContainer.new() + pad.mouse_filter = Control.MOUSE_FILTER_PASS + pad.add_theme_constant_override("margin_left", 12) + pad.add_theme_constant_override("margin_right", 22) + pad.add_theme_constant_override("margin_bottom", 2) + var h := TableCard.hbox(int(_sep())) + pad.add_child(h) + var lead := Control.new() + lead.custom_minimum_size = Vector2(numw, 0) + lead.mouse_filter = Control.MOUSE_FILTER_IGNORE + h.add_child(lead) + for k in t.columns.size(): + var l := Style.make_label(str(t.columns[k].get("name", "")), Style.body_bold(true), 12, Style.MUTED) + l.clip_text = true + l.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS + l.mouse_filter = Control.MOUSE_FILTER_PASS + l.tooltip_text = _column_tip(t.columns[k], k) + if k == 0: + l.size_flags_horizontal = Control.SIZE_EXPAND_FILL + else: + l.custom_minimum_size = Vector2(CELL_W, 0) + h.add_child(l) + var tail := 0.0 + if build: + tail = BUILD_TAIL_RANGED if ranged else BUILD_TAIL + elif ranged and t.has_chances(): + tail = ODDS_TAIL + if tail > 0.0: + var sp := Control.new() + sp.custom_minimum_size = Vector2(tail, 0) + sp.mouse_filter = Control.MOUSE_FILTER_IGNORE + h.add_child(sp) + pad.set_meta("columns_header", true) + return pad + + +func _column_tip(col: Dictionary, k: int) -> String: + var bits := PackedStringArray() + if k > 0 and str(col.get("when", "")).strip_edges() != "": + bits.append("Only rolled if " + str(col["when"]).strip_edges()) + if str(col.get("save_as", "")).strip_edges() != "": + bits.append("Saved as " + str(col["save_as"]).strip_edges()) + return "\n".join(bits) + + +## Adds row `e`'s cells (columns 1..) to its row box `h`, right after the text. +func add_cells(h: HBoxContainer, e: TableEntry, index: int, build: bool) -> void: + if not active(): + return + var list: Array = [] + for k in range(1, card.table.columns.size()): + var c := _build_cell(e, k) if build else _play_cell(e, index, k) + h.add_child(c["box"]) + list.append(c) + cells[e.id] = list + + +func _build_cell(e: TableEntry, k: int) -> Dictionary: + var edit := LineEdit.new() + edit.text = RollTable.cell_text(e, k) + edit.placeholder_text = str(card.table.columns[k].get("name", "")) + edit.custom_minimum_size = Vector2(CELL_W, 0) + edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER + edit.add_theme_font_size_override("font_size", 15) + edit.tooltip_text = edit.placeholder_text + edit.text_changed.connect(func(s: String) -> void: + set_cell(e, k, s) + card.main.mark_dirty() + ) + edit.focus_entered.connect(func() -> void: card.select_entry(e.id)) + edit.set_meta("cell", k) + return {"box": edit, "edit": edit, "k": k} + + +func _play_cell(e: TableEntry, index: int, k: int) -> Dictionary: + var box := PanelContainer.new() + box.mouse_filter = Control.MOUSE_FILTER_PASS + box.custom_minimum_size = Vector2(CELL_W, 0) + box.size_flags_vertical = Control.SIZE_SHRINK_CENTER + var l := RichText.label("", 15, Style.INK, true) + l.custom_minimum_size = Vector2(CELL_W - 8.0, 0) + RichText.hook_hover(l, card.main) + box.add_child(l) + var c := {"box": box, "label": l, "k": k, "index": index, "entry": e} + _show_cell(c) + return c + + +## Re-renders a Play row's cells from the last roll (TableCardRows calls it +## with the row's text). +func refresh(eid: String) -> void: + for c: Dictionary in cells.get(eid, []): + if c.has("label"): + _show_cell(c) + + +func _show_cell(c: Dictionary) -> void: + var e: TableEntry = c["entry"] + var k: int = c["k"] + var rolled := rolled_cell(int(c["index"]), k) + var text := RichText.decorate(str(rolled["text"])) if not rolled.is_empty() \ + else RichText.preview(RollTable.cell_text(e, k), card.main.data) + var l: RichTextLabel = c["label"] + if l.text != text: + l.text = text + l.tooltip_text = Formatter.strip_tags(text).strip_edges() + var lit := not rolled.is_empty() + # "Roll each column separately": a lit row's other cells weren't rolled. + var passed := not lit and card.table.column_rolls == RollTable.COLUMNS_EACH \ + and card.main.is_entry_lit(card.table.id, e.id) and not _lit_columns().is_empty() + RichText.set_fonts(l, 15, true, lit) + l.add_theme_color_override("default_color", Style.MUTED if passed else Style.INK) + var box: PanelContainer = c["box"] + box.add_theme_stylebox_override("panel", Style.box(Style.ACCENT if lit and card.table.column_rolls == RollTable.COLUMNS_EACH else Color(0, 0, 0, 0), 4, [4, 2, 4, 2])) + box.set_meta("lit", lit) + + +# ---------- The last roll ---------- + +## The "columns" of every lit roll of this card in the last roll. +func _lit_columns() -> Array: + var out: Array = [] + var pre := card.table.id + ":" + for key: String in card.main.lit_columns: + if key.begins_with(pre) and card.main.lit_entries.has(key): + out.append(card.main.lit_columns[key]) + return out + + +## What column `k` rolled on result number `index` in the last roll +## ({name, text, index, value?}), or {} when it didn't land there. +func rolled_cell(index: int, k: int) -> Dictionary: + for cols: Array in _lit_columns(): + if k < cols.size(): + var c: Dictionary = cols[k] + if not c.get("skipped", false) and int(c.get("index", -1)) == index: + return c + return {} + + +## Main.lit_columns from a rolled node or step: its "columns", if it has them. +static func note_lit(m: Main, key: String, d: Dictionary) -> void: + if d.get("columns") is Array: + m.lit_columns[key] = d["columns"] + else: + m.lit_columns.erase(key) + + +# ---------- Editing ---------- + +## Sets column `k`'s text on result `e` (k 0 is its own text). +static func set_cell(e: TableEntry, k: int, s: String) -> void: + if k == 0: + e.text = s + return + while e.cells.size() < k: + e.cells.append("") + e.cells[k - 1] = s diff --git a/ui/table_card_columns.gd.uid b/ui/table_card_columns.gd.uid new file mode 100644 index 0000000..e3f9d85 --- /dev/null +++ b/ui/table_card_columns.gd.uid @@ -0,0 +1 @@ +uid://bg7px8r88mta4 diff --git a/ui/table_card_list.gd b/ui/table_card_list.gd index 24593fc..4324e4e 100644 --- a/ui/table_card_list.gd +++ b/ui/table_card_list.gd @@ -29,6 +29,8 @@ func fill(col: VBoxContainer, build: bool, ranged: bool, collapsed: bool) -> voi col.add_child(pad) var numw := card._num_width(build, ranged) + if card.rows_ui.columns_ui.active(): + card._list_box.add_child(card.rows_ui.columns_ui.make_header(build, ranged, numw)) for i in card.table.entries.size(): var e := card.table.entries[i] var lit := card.main.is_entry_lit(card.table.id, e.id) diff --git a/ui/table_card_rows.gd b/ui/table_card_rows.gd index c2a8fbb..aa7340d 100644 --- a/ui/table_card_rows.gd +++ b/ui/table_card_rows.gd @@ -19,6 +19,7 @@ var parts: Dictionary = {} var more_label: Label var sheet_label: RichTextLabel var kind_rows: TableCardKindRows +var columns_ui: TableCardColumns ## Row styles by state, then +3 for the inspector's selected result, then +6 ## for a striped (every second) row. @@ -32,6 +33,7 @@ var _odds: Dictionary = {} func _init(p_card: TableCard) -> void: card = p_card kind_rows = TableCardKindRows.new(self) + columns_ui = TableCardColumns.new(p_card) # Normal, flicker and lit; then the same with the copper bar of the # inspector's selected result. Striped rows tint only the normal state, so # the flicker and the lit result always read over the stripe. @@ -49,6 +51,7 @@ func _init(p_card: TableCard) -> void: ## Forgets the previous build's rows. func clear() -> void: parts.clear() + columns_ui.clear() _odds.clear() more_label = null _flick_id = "" @@ -130,6 +133,7 @@ func make_row(e: TableEntry, index: int, build: bool, ranged: bool, numw: float) col.add_child(cl) col.add_child(edit.text_edit(e, index, chosen)) add_rule_chips(col, e) + columns_ui.add_cells(h, e, index, true) if not ranged and not fixed: h.add_child(edit.weight_edit(e)) if not fixed and not chosen: @@ -146,6 +150,7 @@ func make_row(e: TableEntry, index: int, build: bool, ranged: bool, numw: float) col.add_child(text) add_rule_chips(col, e) h.add_child(col) + columns_ui.add_cells(h, e, index, false) row.mouse_entered.connect(func() -> void: main.details().show_entry(row, table.id, e.id)) if ranged and table.has_chances(): var pct := Style.make_label("", Style.body(true), 12, Style.MUTED) @@ -207,6 +212,9 @@ func row_text(e: TableEntry) -> String: var table := card.table var main := card.main var rolled := main.lit_text(table.id, e.id) + if columns_ui.active(): + # The lit text is "Resources: Rich · Water: …"; this row shows column 0. + rolled = str(columns_ui.rolled_cell(table.entries.find(e), 0).get("text", "")) var s := RichText.decorate(rolled) if rolled != "" else RichText.preview(e.text, main.data) var muted := Style.MUTED.to_html(false) if table.kind == RollTable.KIND_LOOP: @@ -355,6 +363,7 @@ func update_row_text(eid: String) -> void: var want := row_text(p["entry"]) if text.text != want: text.text = want + columns_ui.refresh(eid) func flick_random() -> void: