TableTower/ui/inspector_card.gd
2026-11-08 21:29:59 -06:00

694 lines
27 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.
const KIND_NAMES := {
RollTable.KIND_ROLL: "Roll table", RollTable.KIND_CHOICE: "Choice", RollTable.KIND_LOOP: "Loop",
RollTable.KIND_VARIABLE: "Variable", RollTable.KIND_SHEET: "Sheet", RollTable.KIND_ROUTER: "Router",
RollTable.KIND_CHECK: "Check",
}
const DM_PATTERN := "^dm\\(\\s*(@[a-z_][a-z0-9_]*)\\s*\\)$"
const NONE := "__none"
const AS_TEXT := "__text"
static func build(insp: Inspector, box: VBoxContainer, t: RollTable) -> void:
var kind := "Collection" if t.is_collection() else str(KIND_NAMES.get(t.kind, "Card"))
box.add_child(insp._head("Selected card · " + kind, 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:
_roll(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))
if t.kind != RollTable.KIND_VARIABLE:
box.add_child(_inputs(insp, t))
var more := InspectorKit.link("More settings…", "Card mode, colours 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 pipe 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
# ---------- Roll ----------
static func _roll(insp: Inspector, box: VBoxContainer, t: RollTable) -> void:
var v := InspectorKit.vbox(6)
var ranged := t.is_ranged()
var dice := InspectorKit.line(t.dice, "2d6", 90.0)
InspectorKit.tag(dice, "card.dice")
insp.fields["dice"] = dice
var err := InspectorKit.error_label()
var seg := ModalCard.segmented(["Plain", "Ranged"], 1 if ranged else 0, func(i: int) -> void:
var msg := insp.apply_settings({"ranged": i == 1, "dice": dice.text.strip_edges() if dice.text.strip_edges() != "" else "1d6"}, true)
InspectorKit.show_error(err, msg)
)
v.add_child(seg)
if ranged:
var row := InspectorKit.hbox(6)
row.add_child(InspectorKit.word("Roll"))
row.add_child(dice)
v.add_child(row)
v.add_child(InspectorKit.note(Describe.dice(t.dice, insp.main.data), Style.MUTED, 13))
dice.text_changed.connect(func(s: String) -> void:
if not TableSettingsDialog.dice_ok_text(s):
InspectorKit.show_error(err, "“%s” isn't dice such as 2d6 or 1d100." % s.strip_edges())
InspectorKit.mark(dice, false)
return
InspectorKit.mark(dice, true)
InspectorKit.show_error(err, insp.apply_settings({"ranged": true, "dice": s.strip_edges()}))
)
var probs := t.range_problems()
if probs != "":
v.add_child(InspectorKit.note(probs, Style.WARN_INK, 13))
else:
v.add_child(InspectorKit.note("Each result is equally likely, unless you give it a weight on the card.", Style.MUTED, 13))
v.add_child(err)
box.add_child(InspectorKit.field("Rolls", v))
# ---------- Check ----------
## The modifier picker's value for check_mod text: "" -> none, "dm(@str)" ->
## "@str", anything else -> AS_TEXT.
static func modifier_value(mod: String) -> String:
var s := mod.strip_edges()
if s == "":
return NONE
var m := RegEx.create_from_string(DM_PATTERN).search(s)
return m.get_string(1) if m else AS_TEXT
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(InspectorKit.TAKEN_BG, 8, [10, 7, 10, 8]), 1, Style.BRASS))
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))
# Modifier: [Strength ▾] modifier, none, or text.
var mv := modifier_value(t.check_mod)
var mod_box := InspectorKit.vbox(5)
var once := insp.has_meta("mod_text_once")
if once:
insp.remove_meta("mod_text_once")
if mv == AS_TEXT or not plain or once:
var le := InspectorKit.line(t.check_mod, "e.g. dm(@str) or @skill_melee")
InspectorKit.code(le)
InspectorKit.tag(le, "card.check_mod")
insp.fields["check_mod"] = le
var merr := InspectorKit.error_label()
le.text_changed.connect(func(s: String) -> void:
var msg := InspectorKit.expr_error(s, "The modifier")
InspectorKit.show_error(merr, msg)
InspectorKit.mark(le, msg == "")
if msg == "":
set_check(insp, "check_mod", s)
)
mod_box.add_child(le)
mod_box.add_child(merr)
var back := InspectorKit.link("Use the picker", "Pick a value's modifier instead")
back.disabled = modifier_value(t.check_mod) == AS_TEXT
back.tooltip_text = "Pick a value's modifier instead" if not back.disabled else "The picker shows \"none\" or one value's modifier, dm(@value)"
back.pressed.connect(func() -> void: insp.rebuild())
if plain:
mod_box.add_child(back)
else:
var row := InspectorKit.flow()
var picker := ValuePicker.new(main, mv if mv != NONE else NONE)
picker.kinds = ["number"]
picker.extras = [{"name": NONE, "label": "none", "kind": ""}, {"name": AS_TEXT, "label": "Edit as text…", "kind": ""}]
picker.set_value(mv)
insp.fields["check_mod_picker"] = picker
picker.value_chosen.connect(func(n: String) -> void: choose_modifier(insp, n))
row.add_child(picker)
row.add_child(InspectorKit.word("modifier" if mv != NONE else "", Style.INK))
mod_box.add_child(row)
mod_box.add_child(InspectorKit.note("A value's modifier is dm(value): −1 at 3–5, 0 at 6–8, +1 at 9–11 and so on.", Style.MUTED, 12))
box.add_child(InspectorKit.field("Modifier", mod_box))
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 pipes.", 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)
## The modifier picker: none, a value (dm(@value)) or Edit as text.
static func choose_modifier(insp: Inspector, n: String) -> void:
match n:
NONE:
set_check(insp, "check_mod", "")
insp.rebuild()
AS_TEXT:
_rebuild_as_text(insp)
_:
set_check(insp, "check_mod", "dm(%s)" % n)
insp.rebuild()
static func _rebuild_as_text(insp: Inspector) -> void:
# A one-shot flag: the next build shows the modifier as text.
insp.set_meta("mod_text_once", true)
insp.rebuild()
# ---------- 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(TableSettingsDialog.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(InspectorKit.BOX_BG, 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 ----------
static func _variable(insp: Inspector, box: VBoxContainer, t: RollTable) -> void:
var err := InspectorKit.error_label()
var vn := InspectorKit.line(t.var_name, "luck")
InspectorKit.code(vn)
InspectorKit.tag(vn, "card.var_name")
insp.fields["var_name"] = vn
vn.text_changed.connect(func(s: String) -> void:
var msg := insp.apply_settings({"var_name": s})
InspectorKit.mark(vn, msg == "")
InspectorKit.show_error(err, msg)
)
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 := InspectorKit.line(t.initial, "0" if t.var_type == "number" else "nothing")
InspectorKit.tag(init, "card.initial")
insp.fields["initial"] = init
init.text_changed.connect(func(s: String) -> void:
var msg := insp.apply_settings({"initial": s})
InspectorKit.mark(init, msg == "")
InspectorKit.show_error(err, msg)
)
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 := InspectorKit.line(t.var_name, "skill")
InspectorKit.code(vn)
InspectorKit.tag(vn, "card.var_name")
insp.fields["var_name"] = vn
vn.text_changed.connect(func(s: String) -> void:
var msg := insp.apply_settings({"var_name": s})
InspectorKit.mark(vn, msg == "")
InspectorKit.show_error(err, msg)
)
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 board, 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))