Code names and JSON keys stay "collection". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
341 lines
15 KiB
GDScript
341 lines
15 KiB
GDScript
class_name SettingsKindFields
|
|
extends RefCounted
|
|
## The kind-specific part of a TableSettingsDialog (docs/LOGIC.md): the
|
|
## fields shown for a choice, loop, variable, sheet or router card, their
|
|
## part of the settings Dictionary, and their live checks.
|
|
|
|
var d: TableSettingsDialog
|
|
var table: RollTable
|
|
## The kind's problems, shown under its fields (null for roll tables).
|
|
var problem: Label
|
|
|
|
var _choice := "pick"
|
|
var _choice_note: Label
|
|
var _prompt: LineEdit
|
|
var _save_as: LineEdit
|
|
var _min: LineEdit
|
|
var _max: LineEdit
|
|
var _minmax_row: Control
|
|
## Pick choices: "fixed" or "vars" (options from variables, plus fixed extras).
|
|
var _options_mode := "fixed"
|
|
var _options_box: Control
|
|
var _opt_mode_box: Control
|
|
var _opt_from: LineEdit
|
|
var _opt_where: LineEdit
|
|
var _opt_note: Label
|
|
var _show_if_box: Control
|
|
## entry id -> its Show only if field.
|
|
var _show_if: Dictionary = {}
|
|
var _loop_mode := "times"
|
|
var _loop_expr: LineEdit
|
|
var _loop_note: Label
|
|
var _var_name: LineEdit
|
|
var _var_type := "number"
|
|
var _initial: LineEdit
|
|
## Scale variables: the steps, one per line (shown only for the Scale type).
|
|
var _steps: TextEdit
|
|
var _steps_field: Control
|
|
var _template: TextEdit
|
|
var _template_preview: RichTextLabel
|
|
## Router cards: one condition field per case, in order.
|
|
var _cases: Array[LineEdit] = []
|
|
var _case_note: Label
|
|
|
|
|
|
func _init(dialog: TableSettingsDialog) -> void:
|
|
d = dialog
|
|
table = dialog.table
|
|
_choice = table.choice
|
|
_options_mode = "vars" if table.options_from.strip_edges() != "" else "fixed"
|
|
_loop_mode = table.loop_mode
|
|
_var_type = table.var_type
|
|
|
|
|
|
## Adds the kind's settings to `s`.
|
|
func add_settings(s: Dictionary) -> void:
|
|
match table.kind:
|
|
RollTable.KIND_CHOICE:
|
|
s["choice"] = _choice
|
|
s["prompt"] = _prompt.text
|
|
s["save_as"] = _save_as.text.strip_edges()
|
|
s["min"] = _min.text.strip_edges()
|
|
s["max"] = _max.text.strip_edges()
|
|
s["options_mode"] = _options_mode
|
|
s["options_from"] = _opt_from.text.strip_edges()
|
|
s["options_where"] = _opt_where.text.strip_edges()
|
|
var si := {}
|
|
for eid: String in _show_if:
|
|
si[eid] = (_show_if[eid] as LineEdit).text.strip_edges()
|
|
s["show_if"] = si
|
|
RollTable.KIND_LOOP:
|
|
s["loop_mode"] = _loop_mode
|
|
s["loop_expr"] = _loop_expr.text.strip_edges()
|
|
RollTable.KIND_VARIABLE:
|
|
s["var_name"] = _var_name.text.strip_edges().trim_prefix("@")
|
|
s["var_type"] = _var_type
|
|
s["initial"] = _initial.text.strip_edges()
|
|
if _var_type == RollTable.VAR_SCALE:
|
|
s["steps"] = _steps.text
|
|
RollTable.KIND_SHEET:
|
|
s["template"] = _template.text
|
|
RollTable.KIND_ROUTER:
|
|
var cases: Array = []
|
|
for le in _cases:
|
|
cases.append(le.text.strip_edges())
|
|
s["cases"] = cases
|
|
|
|
|
|
func build() -> void:
|
|
var box := VBoxContainer.new()
|
|
box.add_theme_constant_override("separation", 10)
|
|
match table.kind:
|
|
RollTable.KIND_CHOICE:
|
|
box.add_child(ModalCard.section("Question type"))
|
|
box.add_child(ModalCard.segmented(Describe.choice_labels(), RollTable.CHOICES.find(_choice), func(i: int) -> void:
|
|
_choice = RollTable.CHOICES[i]
|
|
d._refresh()
|
|
))
|
|
_choice_note = ModalCard.note("")
|
|
box.add_child(_choice_note)
|
|
_prompt = LineEdit.new()
|
|
_prompt.text = table.prompt
|
|
_prompt.placeholder_text = "Which way out? You have {@gold} gold."
|
|
ModalCard.style_line_edit(_prompt)
|
|
_prompt.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
box.add_child(ModalCard.field("Question (BBCode, {@var} works)", _prompt))
|
|
_save_as = LineEdit.new()
|
|
_save_as.text = table.save_as
|
|
_save_as.placeholder_text = "@name to keep, or a name for this roll"
|
|
ModalCard.style_line_edit(_save_as)
|
|
_save_as.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
box.add_child(ModalCard.field("Save the answer as (optional)", _save_as))
|
|
var mm := HBoxContainer.new()
|
|
mm.add_theme_constant_override("separation", 12)
|
|
_min = LineEdit.new()
|
|
_min.text = table.min_expr
|
|
ModalCard.style_line_edit(_min)
|
|
_min.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
_max = LineEdit.new()
|
|
_max.text = table.max_expr
|
|
ModalCard.style_line_edit(_max)
|
|
_max.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
mm.add_child(ModalCard.field("Lowest", _min))
|
|
mm.add_child(ModalCard.field("Highest", _max))
|
|
for c in mm.get_children():
|
|
(c as Control).size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
_minmax_row = mm
|
|
box.add_child(mm)
|
|
_build_options(box)
|
|
RollTable.KIND_LOOP:
|
|
box.add_child(ModalCard.section("Repeat"))
|
|
box.add_child(ModalCard.segmented(TableSettingsDialog.LOOP_LABELS, RollTable.LOOPS.find(_loop_mode), func(i: int) -> void:
|
|
_loop_mode = RollTable.LOOPS[i]
|
|
d._refresh()
|
|
))
|
|
_loop_expr = LineEdit.new()
|
|
_loop_expr.text = table.loop_expr
|
|
ModalCard.style_line_edit(_loop_expr)
|
|
_loop_expr.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
box.add_child(ModalCard.field("How many times / condition", _loop_expr))
|
|
_loop_note = ModalCard.note("")
|
|
box.add_child(_loop_note)
|
|
RollTable.KIND_VARIABLE:
|
|
_var_name = LineEdit.new()
|
|
_var_name.text = table.var_name
|
|
_var_name.placeholder_text = "hp"
|
|
ModalCard.style_line_edit(_var_name)
|
|
_var_name.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
box.add_child(ModalCard.field("Variable name (used as @name)", _var_name))
|
|
box.add_child(ModalCard.section("Type"))
|
|
box.add_child(ModalCard.segmented(["Number", "Text", "List", "Ledger", "Scale"], RollTable.VAR_TYPES.find(_var_type), func(i: int) -> void:
|
|
_var_type = RollTable.VAR_TYPES[i]
|
|
d._refresh()
|
|
))
|
|
_steps = TextEdit.new()
|
|
_steps.text = "\n".join(table.steps if not table.steps.is_empty() else ScaleFields.default_steps())
|
|
_steps.custom_minimum_size.y = 96
|
|
ModalCard.style_text_edit(_steps)
|
|
_steps.text_changed.connect(d._refresh)
|
|
_steps_field = ModalCard.field("Steps, one per line, lowest first", _steps)
|
|
box.add_child(_steps_field)
|
|
_initial = LineEdit.new()
|
|
_initial.text = table.initial
|
|
_initial.placeholder_text = "10, 2d6, or a word"
|
|
ModalCard.style_line_edit(_initial)
|
|
_initial.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
box.add_child(ModalCard.field("Starting value", _initial))
|
|
box.add_child(ModalCard.note("reset @name, and Reset in the Variables tab, put it back to this."))
|
|
RollTable.KIND_SHEET:
|
|
_template = TextEdit.new()
|
|
_template.text = table.entries[0].text if not table.entries.is_empty() else ""
|
|
_template.placeholder_text = "[b]{@name}[/b]\nHP {@hp} · Gold {@gold}\nSkills: {@skill_*}"
|
|
_template.custom_minimum_size.y = 120
|
|
_template.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY
|
|
ModalCard.style_text_edit(_template)
|
|
_template.text_changed.connect(d._refresh)
|
|
box.add_child(ModalCard.field("Template (BBCode with {@var}, {@prefix_*} and {= expr})", _template))
|
|
_template_preview = RichTextLabel.new()
|
|
_template_preview.bbcode_enabled = true
|
|
_template_preview.fit_content = true
|
|
_template_preview.scroll_active = false
|
|
var pbox := PanelContainer.new()
|
|
pbox.add_theme_stylebox_override("panel", Style.box(Color(0, 0, 0, 0.05), 7, [10, 7, 10, 7]))
|
|
pbox.add_child(_template_preview)
|
|
box.add_child(pbox)
|
|
RollTable.KIND_ROUTER:
|
|
box.add_child(ModalCard.section("Cases (if / else if / otherwise)"))
|
|
box.add_child(ModalCard.note("Checked top to bottom when the chain arrives; the first true case is chosen, and its effects, text and threads run like a rolled result. An empty condition is Otherwise: it always matches, so keep it last. Add, remove and reorder cases on the card."))
|
|
for i in table.entries.size():
|
|
var e := table.entries[i]
|
|
var le := LineEdit.new()
|
|
le.text = e.when
|
|
le.placeholder_text = "Otherwise (always)"
|
|
ModalCard.style_line_edit(le)
|
|
le.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
_cases.append(le)
|
|
var label := Formatter.strip_tags(e.text).strip_edges()
|
|
box.add_child(ModalCard.field("Case %d%s" % [i + 1, (" · " + label) if label != "" else ""], le))
|
|
if table.entries.is_empty():
|
|
box.add_child(ModalCard.note("This branch has no cases yet: add some on the card."))
|
|
_case_note = ModalCard.note("", Style.WARN_INK)
|
|
box.add_child(_case_note)
|
|
_:
|
|
box.free() # roll tables have no kind fields
|
|
return
|
|
problem = ModalCard.note("", Style.DANGER_INK)
|
|
box.add_child(problem)
|
|
d.body.add_child(box)
|
|
|
|
|
|
## Pick choices: where the options come from, and a Show only if field per
|
|
## option.
|
|
func _build_options(box: VBoxContainer) -> void:
|
|
var mb := VBoxContainer.new()
|
|
mb.add_theme_constant_override("separation", 6)
|
|
mb.add_child(ModalCard.section("Options"))
|
|
mb.add_child(ModalCard.segmented(TableSettingsDialog.OPTION_LABELS, 1 if _options_mode == "vars" else 0, func(i: int) -> void:
|
|
_options_mode = "vars" if i == 1 else "fixed"
|
|
d._refresh()
|
|
))
|
|
box.add_child(mb)
|
|
_opt_mode_box = mb
|
|
var ob := VBoxContainer.new()
|
|
ob.add_theme_constant_override("separation", 8)
|
|
_opt_from = LineEdit.new()
|
|
_opt_from.text = table.options_from
|
|
_opt_from.placeholder_text = "@skill_* or @contacts"
|
|
ModalCard.style_line_edit(_opt_from)
|
|
_opt_from.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
ob.add_child(ModalCard.field("Options from (@prefix_* or a @list)", _opt_from))
|
|
_opt_where = LineEdit.new()
|
|
_opt_where.text = table.options_where
|
|
_opt_where.placeholder_text = "option_value >= 1 (optional)"
|
|
ModalCard.style_line_edit(_opt_where)
|
|
_opt_where.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
ob.add_child(ModalCard.field("Only where (optional)", _opt_where))
|
|
_opt_note = ModalCard.note("")
|
|
ob.add_child(_opt_note)
|
|
box.add_child(ob)
|
|
_options_box = ob
|
|
var sb := VBoxContainer.new()
|
|
sb.add_theme_constant_override("separation", 6)
|
|
sb.add_child(ModalCard.section("Show only if"))
|
|
sb.add_child(ModalCard.note("Checked when the card is reached. An option whose condition is false isn't offered, not even to Random. Empty means always."))
|
|
for i in table.entries.size():
|
|
var e := table.entries[i]
|
|
var le := LineEdit.new()
|
|
le.text = e.show_if
|
|
le.placeholder_text = "always"
|
|
ModalCard.style_line_edit(le)
|
|
le.text_changed.connect(func(_t: String) -> void: d._refresh())
|
|
_show_if[e.id] = le
|
|
var label := Formatter.strip_tags(e.text).strip_edges()
|
|
var cap := "Chosen (every option from variables)" if (i == 0 and table.is_dynamic()) else "Option %d%s" % [i + (0 if table.is_dynamic() else 1), (" · " + label) if label != "" else ""]
|
|
sb.add_child(ModalCard.field(cap, le))
|
|
box.add_child(sb)
|
|
_show_if_box = sb
|
|
|
|
|
|
func refresh() -> void:
|
|
var s := d.settings()
|
|
var problems := TableSettingsRules.kind_problems(table, s)
|
|
if problem:
|
|
problem.text = "\n".join(problems)
|
|
problem.visible = not problems.is_empty()
|
|
match table.kind:
|
|
RollTable.KIND_CHOICE:
|
|
_minmax_row.visible = _choice == "number"
|
|
var msg := ""
|
|
if _choice != table.choice and table.choice == "pick":
|
|
msg = "Only the first option is kept, as the Answer result (its changes and threads stay)."
|
|
elif _choice != table.choice and _choice == "pick":
|
|
msg = "The Answer result becomes the first option; two more are added."
|
|
elif _choice == "pick" and _options_mode == "vars" and table.is_dynamic():
|
|
msg = "The player picks one of the options from variables or a fixed extra, or Random."
|
|
elif _choice == "pick" and _options_mode == "fixed" and table.is_dynamic():
|
|
msg = "Back to a fixed list: the Chosen result becomes an ordinary option (or goes, if it does nothing)."
|
|
elif _choice == "pick":
|
|
msg = "The player picks one of the card's options, or Random."
|
|
else:
|
|
msg = "The answer goes in {answer}; the Answer result's changes and threads run after it."
|
|
_choice_note.text = msg
|
|
_mark(_save_as, s["save_as"] == "" or TableSettingsRules._re(TableSettingsRules.SAVE_PATTERN).search(TemplateKit.probe(s["save_as"])) != null)
|
|
var pick := _choice == "pick"
|
|
_opt_mode_box.visible = pick
|
|
_options_box.visible = pick and _options_mode == "vars"
|
|
_show_if_box.visible = pick and not _show_if.is_empty()
|
|
if pick and _options_mode == "vars":
|
|
_mark(_opt_from, TableSettingsRules.options_from_error(s["options_from"]) == "")
|
|
_mark(_opt_where, TableSettingsRules.when_error(s["options_where"]) == "")
|
|
var desc := RollTable.new()
|
|
desc.options_from = s["options_from"]
|
|
desc.options_where = s["options_where"]
|
|
var how := "One option for %s, labelled with its name and value, after any fixed options. The Chosen result runs for whichever is picked; option, option_label and option_value hold it, e.g. @[option] += 1." % desc.options_description() \
|
|
if TableSettingsRules.options_from_error(s["options_from"]) == "" else "Write @skill_* for each set variable with that prefix, or @contacts for each item of a list."
|
|
if not table.is_dynamic():
|
|
how += " A Chosen result is added first; the options already on the card stay as fixed extras."
|
|
_opt_note.text = how
|
|
elif pick and table.is_dynamic():
|
|
_opt_note.text = ""
|
|
for eid: String in _show_if:
|
|
_mark(_show_if[eid] as LineEdit, TableSettingsRules.when_error((_show_if[eid] as LineEdit).text) == "")
|
|
_mark(_min, Expr.check(TemplateKit.probe(s["min"])) == "" and s["min"] != "")
|
|
_mark(_max, Expr.check(TemplateKit.probe(s["max"])) == "" and s["max"] != "")
|
|
RollTable.KIND_LOOP:
|
|
_loop_note.text = TableSettingsDialog.LOOP_NOTES.get(_loop_mode, "")
|
|
_mark(_loop_expr, s["loop_expr"] != "" and Expr.check(TemplateKit.probe(s["loop_expr"])) == "")
|
|
RollTable.KIND_VARIABLE:
|
|
_mark(_var_name, TableSettingsRules._re(TableSettingsRules.VAR_PATTERN).search(TemplateKit.probe(s["var_name"])) != null)
|
|
_mark(_initial, TableSettingsRules._initial_error(s["initial"], _var_type) == "")
|
|
_steps_field.visible = _var_type == RollTable.VAR_SCALE
|
|
_initial.placeholder_text = "one of the steps" if _var_type == RollTable.VAR_SCALE else "10, 2d6, or a word"
|
|
RollTable.KIND_SHEET:
|
|
var ctx := Roller.live_context(table_board(), _live_vars())
|
|
_template_preview.text = RichText.decorate(TextExpander.render(_template.text, ctx)) if _template.text.strip_edges() != "" \
|
|
else "[color=#69726b]Preview appears here.[/color]"
|
|
RollTable.KIND_ROUTER:
|
|
var cases: Array = s["cases"]
|
|
for i in _cases.size():
|
|
_mark(_cases[i], TableSettingsRules.when_error(str(cases[i])) == "")
|
|
var hint := TableSettingsRules.router_hint(cases)
|
|
_case_note.text = hint
|
|
_case_note.visible = hint != ""
|
|
# The ranged/card-mode sections only apply to roll tables and pick choices.
|
|
var rolls := table.kind == RollTable.KIND_ROLL or (table.kind == RollTable.KIND_CHOICE and _choice == "pick")
|
|
d._type_box.visible = rolls
|
|
d._card_box.visible = rolls
|
|
|
|
|
|
func _mark(le: LineEdit, ok: bool) -> void:
|
|
le.add_theme_color_override("font_color", Style.INK if ok else Style.DANGER_INK)
|
|
|
|
|
|
## The board and variables for the sheet preview, when the dialog sits in Main.
|
|
func table_board() -> TowerBoard:
|
|
var m := d.get_parent() as Main
|
|
return m.data if m else TowerBoard.new()
|
|
|
|
|
|
func _live_vars() -> Dictionary:
|
|
var m := d.get_parent() as Main
|
|
return m.persistent_vars if m else {}
|