TableTower/ui/inspector_card.gd
RobinFiredrake 1377368428 UI for cards with columns: header and cells, inspector Columns section, help
- 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>
2026-11-10 21:25:03 -06:00

592 lines
24 KiB
GDScript

class_name InspectorCard
extends RefCounted
## The card section of the inspector: name, details, tags and inputs for
## every card, then the kind's own fields (roll dice, check, choice, loop,
## router cases, variable, collection members, sheet template). Writes go
## through Inspector (apply_settings / wrote), so the settings dialog's rules
## and the undo history apply. The roll section is InspectorRoll.
static func build(insp: Inspector, box: VBoxContainer, t: RollTable) -> void:
box.add_child(insp._head("Selected card · " + Describe.kind_label(t), Inspector._title_of(t.name, "(no name)")))
var plain := insp.main.rules_plain()
var name := InspectorKit.line(t.name, "Name")
InspectorKit.tag(name, "card.name")
name.text_changed.connect(func(s: String) -> void:
if s.strip_edges() != "":
insp.apply_settings({"name": s})
)
insp.fields["name"] = name
box.add_child(InspectorKit.field("Label" if t.kind == RollTable.KIND_VARIABLE else "Name", name))
match t.kind:
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:
_choice(insp, box, t, plain)
RollTable.KIND_LOOP:
_loop(insp, box, t, plain)
RollTable.KIND_ROUTER:
_router(insp, box, t, plain)
RollTable.KIND_VARIABLE:
if t.is_collection():
_collection(insp, box, t)
else:
_variable(insp, box, t)
RollTable.KIND_SHEET:
_sheet(insp, box, t)
var det := InspectorKit.text_box(t.details, "Notes about this card (optional)", 36.0)
InspectorKit.tag(det, "card.details")
det.text_changed.connect(func() -> void: insp.apply_settings({"details": det.text}))
insp.fields["details"] = det
box.add_child(InspectorKit.field("Details", det))
var tags := InspectorKit.line(", ".join(t.tags), "e.g. loot, town")
InspectorKit.tag(tags, "card.tags")
tags.text_changed.connect(func(s: String) -> void: insp.apply_settings({"tags": s}))
insp.fields["tags"] = tags
box.add_child(InspectorKit.field("Tags", tags))
box.add_child(InspectorKit.field("Colour", ColorChoice.new(t.color, func(hex: String) -> void:
t.color = hex
insp.wrote(t))))
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 and the other settings")
more.pressed.connect(func() -> void: insp.main.open_table_settings(t.id))
box.add_child(more)
# ---------- Inputs (every rollable card) ----------
static func _inputs(insp: Inspector, t: RollTable) -> Control:
var v := InspectorKit.vbox(5)
v.add_child(InspectorKit.flabel("Inputs"))
v.add_child(InspectorKit.note("Values a thread into this card can pass, like a Tier. Each one has a starting value.", Style.MUTED, 13))
var rows: Array = []
for inp: Dictionary in t.inputs:
rows.append(inp.duplicate())
insp.fields["inputs_rows"] = rows
var grid := GridContainer.new()
grid.columns = 4
grid.add_theme_constant_override("h_separation", 5)
grid.add_theme_constant_override("v_separation", 5)
v.add_child(grid)
var err := InspectorKit.error_label()
var write := func() -> void:
t.inputs = RollTable.clean_inputs(rows)
insp.wrote(t)
var lint := func() -> void:
var msgs := PackedStringArray()
for it: Dictionary in Roller.lint_inputs(insp.main.data, t):
if str(it["entry_id"]) == "":
msgs.append(str(it["message"]) + ".")
InspectorKit.show_error(err, "\n".join(msgs))
if not rows.is_empty():
for h in ["Name", "Label", "Default", ""]:
grid.add_child(InspectorKit.flabel(h))
for i in rows.size():
var r: Dictionary = rows[i]
for k in ["name", "label", "default"]:
var le := InspectorKit.line(str(r.get(k, "")), {"name": "tier", "label": "Tier", "default": "1"}[k], 0.0)
le.custom_minimum_size = Vector2(70, 0)
InspectorKit.tag(le, "card.in.%d.%s" % [i, k])
var key: String = k
le.text_changed.connect(func(s: String) -> void:
r[key] = s.strip_edges().trim_prefix("@").to_lower() if key == "name" else s
write.call()
)
grid.add_child(le)
var x := InspectorKit.xbutton("Remove this input")
x.pressed.connect(func() -> void:
rows.remove_at(i)
t.inputs = RollTable.clean_inputs(rows)
insp.wrote(t, false, true)
)
grid.add_child(x)
var add := InspectorKit.link("+ Add input")
add.pressed.connect(func() -> void:
var used := t.input_names()
var nm := "tier"
var k := 2
while used.has(nm):
nm = "input_%d" % k
k += 1
t.inputs.append({"name": nm, "label": RollContext.pretty_name(nm), "default": "1"})
insp.wrote(t, false, true)
)
v.add_child(add)
v.add_child(err)
lint.call()
insp._live.append(lint)
return v
# ---------- Check ----------
static func _check(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool) -> void:
var main := insp.main
var sentence := InspectorKit.note("", Style.INK, 15)
sentence.add_theme_font_override("font", Style.body_bold())
var sp := PanelContainer.new()
sp.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.VALUE_BG, 8, [10, 7, 10, 8]), 1, Style.ACCENT))
sp.add_child(sentence)
box.add_child(sp)
insp.fields["sentence"] = sentence
var errs := InspectorKit.error_label()
var refresh := func() -> void:
sentence.text = Describe.check(t, main.data) if plain else "Roll %s%s, need %s %s" % [t.check_dice, (" + " + t.check_mod) if t.check_mod != "" else "", t.check_target, "or more" if t.check_cmp == "ge" else "or less"]
var msgs := PackedStringArray()
for it: Dictionary in Roller.lint_check(t):
msgs.append(str(it["message"]) + ".")
InspectorKit.show_error(errs, "\n".join(msgs))
insp._live.append(refresh)
var dice := InspectorKit.line(t.check_dice, "2d6", 80.0)
InspectorKit.tag(dice, "card.check_dice")
insp.fields["check_dice"] = dice
dice.text_changed.connect(func(s: String) -> void: set_check(insp, "check_dice", s))
var r1 := InspectorKit.flow()
r1.add_child(InspectorKit.word("Roll"))
r1.add_child(dice)
box.add_child(InspectorKit.field("Dice", r1))
var mod := ModifierEditor.new(main, t.check_mod, plain, "card.check_mod")
insp.fields["check_mod"] = mod
mod.changed.connect(func(s: String) -> void: set_check(insp, "check_mod", s))
box.add_child(InspectorKit.field("Modifier", mod))
var r3 := InspectorKit.flow()
r3.add_child(InspectorKit.word("Need"))
var target := InspectorKit.amount(t.check_target, "8")
InspectorKit.tag(target, "card.check_target")
insp.fields["check_target"] = target
target.text_changed.connect(func(s: String) -> void: set_check(insp, "check_target", s))
r3.add_child(target)
var cmp := InspectorKit.dropdown(["or more", "or less"], 0 if t.check_cmp == "ge" else 1)
cmp.item_selected.connect(func(k: int) -> void: set_check(insp, "check_cmp", "ge" if k == 0 else "le"))
insp.fields["check_cmp"] = cmp
r3.add_child(cmp)
box.add_child(InspectorKit.field("Target", r3))
box.add_child(errs)
box.add_child(InspectorKit.note("Success and Failure are the card's two results; select one to give it changes and threads.", Style.MUTED, 13))
refresh.call()
## Sets a check field (check_dice, check_mod, check_target, check_cmp).
static func set_check(insp: Inspector, key: String, value: String) -> void:
var t := insp.selected_table()
if t == null or t.kind != RollTable.KIND_CHECK:
return
var s := value.strip_edges()
if key == "check_cmp":
if not RollTable.CHECK_CMPS.has(s):
return
elif key != "check_mod" and s == "":
return
if str(t.get(key)) == s:
return
t.set(key, s)
insp.wrote(t)
# ---------- Choice ----------
static func _choice(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool) -> void:
var main := insp.main
var err := InspectorKit.error_label()
var seg := ModalCard.segmented(Describe.choice_labels(), maxi(0, RollTable.CHOICES.find(t.choice)), func(i: int) -> void:
InspectorKit.show_error(err, insp.apply_settings({"choice": RollTable.CHOICES[i]}, true))
)
box.add_child(InspectorKit.field("Type", seg))
var q := InspectorKit.line(t.prompt, "Which one?")
InspectorKit.tag(q, "card.prompt")
q.text_changed.connect(func(s: String) -> void: insp.apply_settings({"prompt": s}))
insp.fields["prompt"] = q
box.add_child(InspectorKit.field("Question", q))
var save := InspectorKit.line(t.save_as, "nothing (e.g. career, or @career to keep)")
InspectorKit.tag(save, "card.save_as")
insp.fields["save_as"] = save
save.text_changed.connect(func(s: String) -> void:
var msg := Inspector._save_error(s)
InspectorKit.mark(save, msg == "")
InspectorKit.show_error(err, msg if msg != "" else insp.apply_settings({"save_as": s}))
)
box.add_child(InspectorKit.field("Save the answer as", save))
if t.choice == "number":
var r := InspectorKit.flow()
r.add_child(InspectorKit.word("From"))
var lo := InspectorKit.amount(t.min_expr, "0")
var hi := InspectorKit.amount(t.max_expr, "99")
InspectorKit.tag(lo, "card.min")
InspectorKit.tag(hi, "card.max")
lo.text_changed.connect(func(s: String) -> void: InspectorKit.show_error(err, insp.apply_settings({"min": s, "max": hi.text})))
hi.text_changed.connect(func(s: String) -> void: InspectorKit.show_error(err, insp.apply_settings({"min": lo.text, "max": s})))
r.add_child(lo)
r.add_child(InspectorKit.word("to"))
r.add_child(hi)
box.add_child(InspectorKit.field("Allowed answers", r))
if t.choice == "pick":
box.add_child(_options(insp, t, plain, err))
box.add_child(err)
## Pick cards: Fixed list, or From [collection or list ▾] where [condition].
static func _options(insp: Inspector, t: RollTable, plain: bool, err: Label) -> Control:
var main := insp.main
var v := InspectorKit.vbox(6)
var dynamic := t.is_dynamic()
var from_picker := ValuePicker.new(main, t.options_from)
from_picker.kinds = ["collection", "list"]
from_picker.allow_patterns = true
from_picker.placeholder = "a collection or list"
insp.fields["options_from"] = from_picker
var seg := ModalCard.segmented(["Fixed list", "From a value"], 1 if dynamic else 0, func(i: int) -> void:
if i == 0:
InspectorKit.show_error(err, insp.apply_settings({"options_mode": "fixed"}, true))
else:
var first := _first_source(main.data)
if first == "":
InspectorKit.show_error(err, "Add a collection (+ Card ▾ → Collection) or a list value first, then pick it here.")
return
InspectorKit.show_error(err, set_options(insp, first, ""))
)
v.add_child(seg)
if dynamic:
var row := InspectorKit.flow()
row.add_child(InspectorKit.word("From"))
from_picker.value_chosen.connect(func(n: String) -> void:
InspectorKit.show_error(err, set_options(insp, n, t.options_where)))
row.add_child(from_picker)
v.add_child(row)
var where := ConditionBuilder.new()
where.setup(main, t.options_where, not plain, "card.where", {"empty_note": "Every one is offered.",
"extras": [{"name": "option_value", "label": "Its value", "kind": "number"}, {"name": "option_label", "label": "Its name", "kind": "text"}],
"add_label": "+ Only where…"})
where.changed.connect(func(s: String) -> void:
if TableSettingsDialog.when_error(s) == "":
InspectorKit.show_error(err, set_options(insp, t.options_from, s)))
insp.fields["options_where"] = where
v.add_child(InspectorKit.word("where", Style.MUTED))
v.add_child(where)
var desc := Describe.options(t, main.data)
if desc != "" and plain:
v.add_child(InspectorKit.note("Offers %s, plus the fixed options. The Chosen result runs for the one picked." % desc, Style.MUTED, 13))
else:
v.add_child(InspectorKit.note("The player picks one of the card's results, or Random.", Style.MUTED, 13))
return InspectorKit.field("Options", v)
## The first collection (@stem_*) or list value on the board, or "".
static func _first_source(board: TowerBoard) -> String:
for k: Dictionary in Builder.known_values(board):
if str(k["kind"]) == "collection" or str(k["kind"]) == "list":
return str(k["name"])
return ""
## Options from `from` where `where` (a pick card). Returns "" or the problem.
static func set_options(insp: Inspector, from: String, where: String) -> String:
var t := insp.selected_table()
var was := t.is_dynamic()
return insp.apply_settings({"options_mode": "vars", "options_from": from, "options_where": where}, not was or from != t.options_from)
# ---------- Loop ----------
static func _loop(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool) -> void:
var labels := ["Times", "While", "Until"]
var err := InspectorKit.error_label()
var seg := ModalCard.segmented(labels, maxi(0, RollTable.LOOPS.find(t.loop_mode)), func(i: int) -> void:
set_loop_mode(insp, RollTable.LOOPS[i])
)
box.add_child(InspectorKit.field("Repeat", seg))
if t.loop_mode == "times":
var r := InspectorKit.flow()
r.add_child(InspectorKit.word("Run Each time"))
var n := InspectorKit.amount(t.loop_expr, "3")
InspectorKit.tag(n, "card.loop_expr")
insp.fields["loop_expr"] = n
n.text_changed.connect(func(s: String) -> void:
var msg := "Fill in how many times." if s.strip_edges() == "" else InspectorKit.expr_error(s, "The count")
InspectorKit.show_error(err, msg)
InspectorKit.mark(n, msg == "")
if msg == "" and s.strip_edges() != t.loop_expr:
t.loop_expr = s.strip_edges()
insp.wrote(t)
)
r.add_child(n)
r.add_child(InspectorKit.word("times, then Afterwards."))
box.add_child(r)
else:
var cond := ConditionBuilder.new()
cond.setup(insp.main, t.loop_expr, not plain, "card.loop_cond", {})
cond.changed.connect(func(s: String) -> void:
if s.strip_edges() != "" and TableSettingsDialog.when_error(s) == "" and s.strip_edges() != t.loop_expr:
t.loop_expr = s.strip_edges()
insp.wrote(t)
)
insp.fields["loop_cond"] = cond
var cap := "Keep going while" if t.loop_mode == "while" else "Stop once"
box.add_child(InspectorKit.field(cap, cond))
box.add_child(InspectorKit.note(TableSettingsDialog.LOOP_NOTES.get(t.loop_mode, ""), Style.MUTED, 13))
box.add_child(err)
static func set_loop_mode(insp: Inspector, m: String) -> void:
var t := insp.selected_table()
if t == null or m == t.loop_mode or not RollTable.LOOPS.has(m):
return
var cond_like := Builder.parse_condition(t.loop_expr)
var is_cond: bool = cond_like["ok"] and not (cond_like["clauses"] as Array).is_empty() \
and str((cond_like["clauses"] as Array)[0]["cmp"]) != "set"
if m == "times" and is_cond:
t.loop_expr = "3"
elif m != "times" and not is_cond:
t.loop_expr = "i < 3"
t.loop_mode = m
insp.wrote(t, false, true)
# ---------- Router ----------
static func _router(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool) -> void:
var v := InspectorKit.vbox(8)
for e in t.entries:
v.add_child(case_block(insp, t, e, true))
var add := InspectorKit.link("+ Add case", "Add a case before the Otherwise case")
add.pressed.connect(func() -> void: add_case(insp))
v.add_child(add)
var lint := InspectorKit.error_label()
var fill := func() -> void:
var msgs := PackedStringArray()
for it: Dictionary in Roller.lint_router(t):
msgs.append(str(it["message"]) + ".")
InspectorKit.show_error(lint, "\n".join(msgs))
fill.call()
insp._live.append(fill)
v.add_child(lint)
box.add_child(InspectorKit.field("Cases (the first that holds is taken)", v))
## One router case: its condition, or Otherwise for the last case.
static func case_block(insp: Inspector, t: RollTable, e: TableEntry, framed: bool = false) -> Control:
var i := t.entries.find(e)
var last := i == t.entries.size() - 1
var v := InspectorKit.vbox(6)
if framed:
var head := InspectorKit.hbox(6)
var cap := InspectorKit.word("%d. %s" % [i + 1, Inspector._title_of(e.text, "(no text)")], Style.INK, true)
cap.size_flags_horizontal = Control.SIZE_EXPAND_FILL
cap.clip_text = true
head.add_child(cap)
var open := InspectorKit.link("Edit", "Edit this case's result")
open.pressed.connect(func() -> void: insp.select({"kind": "result", "tid": t.id, "eid": e.id}))
head.add_child(open)
if t.entries.size() > 1:
var x := InspectorKit.xbutton("Delete this case")
x.pressed.connect(func() -> void: insp.main.delete_entry(t.id, e.id))
head.add_child(x)
v.add_child(head)
var other := e.when.strip_edges() == ""
if last:
var cb := InspectorKit.check_box("Otherwise (when no case above holds)", other)
cb.toggled.connect(func(on: bool) -> void:
if on:
insp.set_case_when(e.id, "")
insp.rebuild()
else:
insp.set_meta("open_case", e.id)
insp.rebuild()
)
v.add_child(cb)
var opening := insp.has_meta("open_case") and str(insp.get_meta("open_case")) == e.id
if not (last and other) or opening:
var cond := ConditionBuilder.new()
cond.setup(insp.main, e.when, not insp.main.rules_plain(), "case.%s" % e.id, {})
if other and cond.clauses.is_empty() and not cond.text_mode:
cond.add_clause()
cond.changed.connect(func(s: String) -> void:
if s.strip_edges() != "":
insp.set_case_when(e.id, s))
if not framed:
insp.case_builder = cond
var cap := "Taken when" if framed else "This case is taken when"
var fv := InspectorKit.vbox(4)
fv.add_child(InspectorKit.flabel(cap))
fv.add_child(cond)
if other and not last:
fv.add_child(InspectorKit.note("Empty, so this case is an Otherwise and the cases after it never run.", Style.WARN_INK, 13))
v.add_child(fv)
if opening:
insp.remove_meta("open_case")
if framed:
var p := PanelContainer.new()
p.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.CARD_SOFT, 8, [10, 8, 10, 9]), 1, Style.RULE))
p.add_child(v)
return p
return v
## Adds a case before a final Otherwise case (or at the end).
static func add_case(insp: Inspector) -> void:
var t := insp.selected_table()
if t == null:
return
var e := TableEntry.new(TowerBoard.new_id("e"), "Case %d" % (t.entries.size() + 1))
var at := t.entries.size()
if not t.entries.is_empty() and t.entries.back().when.strip_edges() == "":
at -= 1
t.entries.insert(at, e)
insp.set_meta("open_case", e.id)
insp.wrote(t, false, true)
# ---------- Variable ----------
## A field for one card setting: applied as you type, marked and explained in `err` when refused.
static func _setting_line(insp: Inspector, key: String, text: String, placeholder: String, err: Label) -> LineEdit:
var le := InspectorKit.line(text, placeholder)
InspectorKit.tag(le, "card." + key)
insp.fields[key] = le
le.text_changed.connect(func(s: String) -> void:
var msg := insp.apply_settings({key: s})
InspectorKit.mark(le, msg == "")
InspectorKit.show_error(err, msg)
)
return le
static func _variable(insp: Inspector, box: VBoxContainer, t: RollTable) -> void:
var err := InspectorKit.error_label()
var vn := _setting_line(insp, "var_name", t.var_name, "luck", err)
InspectorKit.code(vn)
box.add_child(InspectorKit.field("Name in rules (@name)", vn))
var types := ["number", "text", "list"]
var seg := ModalCard.segmented(["Number", "Text", "List"], maxi(0, types.find(t.var_type)), func(i: int) -> void:
InspectorKit.show_error(err, insp.apply_settings({"var_type": types[i]}, true))
)
box.add_child(InspectorKit.field("Type", seg))
var init := _setting_line(insp, "initial", t.initial, "0" if t.var_type == "number" else "nothing", err)
box.add_child(InspectorKit.field("Starting value", init))
box.add_child(err)
# ---------- Collection ----------
static func _collection(insp: Inspector, box: VBoxContainer, t: RollTable) -> void:
var err := InspectorKit.error_label()
var vn := _setting_line(insp, "var_name", t.var_name, "skill", err)
InspectorKit.code(vn)
box.add_child(InspectorKit.field("Members are named", vn))
box.add_child(InspectorKit.note("Each member is the value @%s_<name>, e.g. @%s_pilot. Choices can offer “one of your %s”." % [t.var_name, t.var_name, Inspector._title_of(t.name, "members")], Style.MUTED, 13))
var v := InspectorKit.vbox(5)
var list := insp.members(t)
insp.fields["members"] = list
if list.is_empty():
v.add_child(InspectorKit.note("No members yet.", Style.MUTED, 14))
var grid := GridContainer.new()
grid.columns = 3
grid.add_theme_constant_override("h_separation", 6)
grid.add_theme_constant_override("v_separation", 5)
for pair: Array in list:
var item := str(pair[0])
var nm := InspectorKit.line(RollContext.pretty_name(item), item, 0.0)
nm.size_flags_horizontal = Control.SIZE_EXPAND_FILL
nm.tooltip_text = "@%s_%s" % [t.var_name, item]
InspectorKit.tag(nm, "member.%s.name" % item)
var commit_name := func() -> void:
var slug := JsonIO.slug(nm.text, "")
if slug == "" or slug == item:
return
rename_member(insp, t, item, slug)
nm.text_submitted.connect(func(_s: String) -> void: commit_name.call())
nm.focus_exited.connect(commit_name)
grid.add_child(nm)
var val := InspectorKit.amount(str(pair[1]), "0")
InspectorKit.tag(val, "member.%s.value" % item)
var commit_val := func() -> void:
if val.text.strip_edges() != str(pair[1]):
set_member(insp, t, item, val.text.strip_edges())
val.text_submitted.connect(func(_s: String) -> void: commit_val.call())
val.focus_exited.connect(commit_val)
grid.add_child(val)
var x := InspectorKit.xbutton("Remove this member")
x.pressed.connect(func() -> void: remove_member(insp, t, item))
grid.add_child(x)
v.add_child(grid)
var add := InspectorKit.link("+ Add member")
add.pressed.connect(func() -> void: add_member(insp, t))
v.add_child(add)
v.add_child(InspectorKit.note("Members are saved values of this spread, like the Variables panel shows; they aren't part of undo.", Style.MUTED, 12))
box.add_child(InspectorKit.field("Members", v))
box.add_child(err)
static func set_member(insp: Inspector, t: RollTable, item: String, value: String) -> void:
insp.main.set_collection_member(t, item, value)
insp.rebuild()
static func rename_member(insp: Inspector, t: RollTable, old_item: String, new_item: String) -> void:
var cur: Variant = insp.main.persistent_vars.get("%s_%s" % [t.var_name, old_item])
var value := Expr.format_value(cur.get("value") if cur is Dictionary else cur)
insp.main.remove_collection_member(t, old_item)
insp.main.set_collection_member(t, new_item, value)
insp.rebuild()
static func remove_member(insp: Inspector, t: RollTable, item: String) -> void:
insp.main.remove_collection_member(t, item)
insp.rebuild()
static func add_member(insp: Inspector, t: RollTable) -> String:
var used := {}
for pair: Array in insp.members(t):
used[str(pair[0])] = true
var item := "new"
var k := 2
while used.has(item):
item = "new_%d" % k
k += 1
insp.main.set_collection_member(t, item, "0")
insp.rebuild()
var nm := InspectorKit.find_tagged(insp, "member.%s.name" % item) as LineEdit
if nm and nm.is_inside_tree():
nm.grab_focus()
nm.select_all()
return item
# ---------- Sheet ----------
static func _sheet(insp: Inspector, box: VBoxContainer, t: RollTable) -> void:
var tpl := InspectorKit.text_box(t.entries[0].text if not t.entries.is_empty() else "", "[b]{@name}[/b]\nStrength {@str}", 120.0)
InspectorKit.code(tpl)
InspectorKit.tag(tpl, "card.template")
insp.fields["template"] = tpl
tpl.text_changed.connect(func() -> void: insp.apply_settings({"template": tpl.text}))
box.add_child(InspectorKit.field("Template", tpl))
box.add_child(InspectorKit.note("Write {@value} where a value goes. The sheet shows the values as they are now.", Style.MUTED, 13))
# The easy way: pick a value and get a "Label: value" line.
var row := InspectorKit.flow()
row.add_child(InspectorKit.word("+ Add a line for"))
var pick := ValuePicker.new(insp.main, "")
pick.value_chosen.connect(func(n: String) -> void:
var name := n.trim_prefix("@")
var line := "[b]%s:[/b] {@%s}" % [Describe.label(name, insp.main.data), name]
tpl.text = (tpl.text.strip_edges() + "\n" + line).strip_edges()
insp.apply_settings({"template": tpl.text}))
row.add_child(pick)
box.add_child(row)
box.add_child(InspectorKit.note("Save a finished sheet from the tray: Copy ▾ → Save as a file (text, Markdown, or a web page to print as PDF).", Style.MUTED, 12))