TableTower/ui/inspector_card.gd

350 lines
14 KiB
GDScript3
Raw Normal View History

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; choice,
## router and ledger cards are InspectorChoice, InspectorRouter and
## InspectorCollection.
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:
InspectorChoice.build(insp, box, t, plain)
RollTable.KIND_LOOP:
_loop(insp, box, t, plain)
RollTable.KIND_ROUTER:
InspectorRouter.build(insp, box, t)
RollTable.KIND_VARIABLE:
if t.is_collection():
InspectorCollection.build(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 (InspectorChoice) ----------
## Options from `from` where `where` (a pick card). Returns "" or the problem.
static func set_options(insp: Inspector, from: String, where: String) -> String:
return InspectorChoice.set_options(insp, from, where)
# ---------- 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 (InspectorRouter) ----------
## 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:
return InspectorRouter.case_block(insp, t, e, framed)
## Adds a case before a final Otherwise case (or at the end).
static func add_case(insp: Inspector) -> void:
InspectorRouter.add_case(insp)
# ---------- 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", RollTable.VAR_SCALE]
var seg := ModalCard.segmented(["Number", "Text", "List", "Scale"], maxi(0, types.find(t.var_type)), func(i: int) -> void:
InspectorKit.show_error(err, insp.apply_settings({"var_type": types[i], "initial": "" if t.is_scale() else t.initial}, true))
)
box.add_child(InspectorKit.field("Type", seg))
if t.is_scale():
InspectorScale.build(insp, box, t, err)
else:
box.add_child(InspectorKit.field("Starting value", setting_line(insp, "initial", t.initial, "0" if t.var_type == "number" else "nothing", err)))
box.add_child(err)
# ---------- Collection (InspectorCollection) ----------
static func set_member(insp: Inspector, t: RollTable, item: String, value: String) -> void:
InspectorCollection.set_member(insp, t, item, value)
static func rename_member(insp: Inspector, t: RollTable, old_item: String, new_item: String) -> void:
InspectorCollection.rename_member(insp, t, old_item, new_item)
static func remove_member(insp: Inspector, t: RollTable, item: String) -> void:
InspectorCollection.remove_member(insp, t, item)
static func add_member(insp: Inspector, t: RollTable) -> String:
return InspectorCollection.add_member(insp, t)
# ---------- 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))