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 …", and how the result reads (each ## column by name, or one phrase: column_join). The result section's cell fields ## come from result_cells(), with the result's "roll twice and combine" ## option (TableEntry.repeat). 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”.", ] const JOIN_LABELS := ["Show each column by name", "Read as one phrase (spaces)", "Read as one phrase (commas)", "Read as one phrase (dashes)"] const JOIN_VALUES := ["", " ", ", ", " – "] 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)) v.add_child(_join_picker(insp, t)) 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)) ## How the result reads: "1st Part: Ash · 2nd Part: Canyons", or the columns ## as one phrase ("Ash Canyons"), which is also what a thread saves. static func _join_picker(insp: Inspector, t: RollTable) -> Control: var labels := JOIN_LABELS.duplicate() var values := JOIN_VALUES.duplicate() if not values.has(t.column_join): labels.append("Read as one phrase (joined with “%s”)" % t.column_join) values.append(t.column_join) var join := InspectorKit.dropdown(labels, values.find(t.column_join)) join.tooltip_text = "Whether the result lists each column by name, or reads the columns together as one phrase, like “Ash Canyons”" InspectorKit.tag(join, "card.column_join") insp.fields["column_join"] = join join.item_selected.connect(func(i: int) -> void: if t.column_join != values[i]: t.column_join = values[i] insp.wrote(t, false, true)) return join 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)) insp._content.add_child(InspectorKit.field("Roll twice and combine", _repeat_row(insp, t, e))) ## "When this comes up in a column, roll that column [2] times and combine", ## plus "skip this result on those rolls" (TableEntry.repeat). static func _repeat_row(insp: Inspector, t: RollTable, e: TableEntry) -> Control: var v := InspectorKit.vbox(4) var on := InspectorKit.check_box("When this comes up in a column, roll that column again", not e.repeat.is_empty()) InspectorKit.tag(on, "res.repeat") insp.fields["repeat"] = on on.toggled.connect(func(b: bool) -> void: e.repeat = {"count": 2} if b else {} insp.wrote(t, false, true)) v.add_child(on) if e.repeat.is_empty(): return v var row := InspectorKit.flow() row.add_child(InspectorKit.word("Roll it")) var n := InspectorKit.amount(str(e.repeat.get("count", 2)), "2") InspectorKit.tag(n, "res.repeat.count") insp.fields["repeat_count"] = n n.text_changed.connect(func(s: String) -> void: if s.strip_edges().is_valid_int() and int(s) >= 1 and int(s) != int(e.repeat.get("count", 2)): e.repeat["count"] = int(s) insp.wrote(t)) row.add_child(n) row.add_child(InspectorKit.word("times and combine the results")) v.add_child(row) var skip := InspectorKit.check_box("Roll again if this comes up on those rolls", (e.repeat.get("reroll", []) as Array).has(e.id)) InspectorKit.tag(skip, "res.repeat.skip") insp.fields["repeat_skip"] = skip skip.toggled.connect(func(b: bool) -> void: if b: e.repeat["reroll"] = [e.id] else: e.repeat.erase("reroll") insp.wrote(t)) v.add_child(skip) v.add_child(InspectorKit.note("Otherwise it adds another roll each time it comes up.", Style.MUTED, 12)) return v # ---------- 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 t.column_join = "" for e in t.entries: e.cells.clear() e.repeat = {} ## 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("")