- TableCardColumns: the column-name header, each row's cells after its text (Play: wrapping text; Build: a field per cell, edited inline), cards widen per column, and in "each" mode the cell each column landed on is lit (Main.lit_columns, from the rolled node's columns). - InspectorColumns: Add columns, add/rename/move/remove (cells kept in step), one roll vs each column, Save as (value picker) and Only if (condition builder); the result inspector gets a field per extra column. - Help: a Columns topic after Ranged tables and dice. - tests/test_ui_columns.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
220 lines
8.2 KiB
GDScript
220 lines
8.2 KiB
GDScript
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("")
|