- Making one: Scale (ordered steps) in + Card, the inspector's Type switch and the settings dialog; steps one per line and a starting step dropdown (InspectorScale); new scales get Low, Medium, High. - Showing one: the variable card shows the current step and the steps in a row with the current one marked; the Variables tab stores a typed step number or name as the step. - Plain words: Builder ops step_up, step_down, toward, at_least, at_most (BuilderScales; step_up/down need the board), DescribeScales phrases, scale effect rows (EffectsScaleRow) and step dropdowns in conditions. - Help: a Scales topic after Values and changes. - Tests: test_ui_scales, plus builder and describe cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
256 lines
8 KiB
GDScript
256 lines
8 KiB
GDScript
class_name ConditionBuilder
|
||
extends VBoxContainer
|
||
## A condition editor: clauses `[value ▾] [comparison ▾] [amount] ×`, an
|
||
## and/or switch once there are two, and "+ Add condition"
|
||
## (Builder.parse_condition / condition_text). For a scale value the amount
|
||
## is a dropdown of its steps ("Atmosphere is at least [Thin ▾]"). A condition
|
||
## the builder can't show (mixed and/or, dice, functions…) is a text field
|
||
## with its problem;
|
||
## "Edit as text" and "Use the builder" switch between the two.
|
||
## Emits `changed(text)`; clauses not filled in yet are left out.
|
||
|
||
signal changed(text: String)
|
||
|
||
const CMPS := ["eq", "ne", "ge", "le", "gt", "lt", "has", "set", "unset"]
|
||
const CMP_LABELS := ["is", "is not", "is at least", "is at most", "is more than", "is less than", "includes", "is set", "is not set"]
|
||
|
||
var main: Main
|
||
var join := "and"
|
||
var clauses: Array[Dictionary] = []
|
||
var text_mode := false
|
||
## What an empty condition means, shown when there are no clauses ("Always").
|
||
var empty_note := ""
|
||
## Extra values for the pickers ([{name, label, kind}]), e.g. option_value.
|
||
var extras: Array = []
|
||
## Names that stay unquoted in amounts (chain names, inputs).
|
||
var known: Array = []
|
||
var add_label := "+ Add condition"
|
||
var _raw := ""
|
||
var _key := "cond"
|
||
|
||
|
||
func setup(p_main: Main, expr: String, as_text: bool, key: String = "cond", opts: Dictionary = {}) -> void:
|
||
main = p_main
|
||
_key = key
|
||
empty_note = str(opts.get("empty_note", ""))
|
||
extras = opts.get("extras", [])
|
||
add_label = str(opts.get("add_label", add_label))
|
||
known = []
|
||
for x: Dictionary in extras:
|
||
known.append(str(x["name"]))
|
||
if main:
|
||
for k: Dictionary in Builder.known_values(main.data):
|
||
if not str(k["name"]).begins_with("@"):
|
||
known.append(str(k["name"]))
|
||
add_theme_constant_override("separation", 6)
|
||
size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
_raw = expr.strip_edges()
|
||
var m := Builder.parse_condition(_raw)
|
||
text_mode = as_text or not m["ok"]
|
||
if m["ok"]:
|
||
join = str(m["join"])
|
||
clauses.clear()
|
||
for c: Dictionary in m["clauses"]:
|
||
clauses.append(c.duplicate())
|
||
_render()
|
||
|
||
|
||
func text() -> String:
|
||
if text_mode:
|
||
return _raw.strip_edges()
|
||
var parts := PackedStringArray()
|
||
for c in clauses:
|
||
var t := Builder.clause_text(c)
|
||
if t != "":
|
||
parts.append(t)
|
||
return (" %s " % join).join(parts)
|
||
|
||
|
||
func _emit() -> void:
|
||
changed.emit(text())
|
||
|
||
|
||
# ---------- Editing (also used by tests) ----------
|
||
|
||
func add_clause(v: String = "", cmp: String = "ge", value: String = "") -> void:
|
||
clauses.append({"var": v, "cmp": cmp, "value": value})
|
||
_render()
|
||
_emit()
|
||
|
||
|
||
func remove_clause(i: int) -> void:
|
||
if i < 0 or i >= clauses.size():
|
||
return
|
||
clauses.remove_at(i)
|
||
_render()
|
||
_emit()
|
||
|
||
|
||
func set_join(j: String) -> void:
|
||
if j != "and" and j != "or":
|
||
return
|
||
join = j
|
||
_render()
|
||
_emit()
|
||
|
||
|
||
func set_clause_var(i: int, n: String) -> void:
|
||
clauses[i]["var"] = n
|
||
var steps := ScaleFields.steps_of(main, n)
|
||
var cur := Builder.unquote(str(clauses[i]["value"]))
|
||
if not steps.is_empty() and Scales.position_of(steps, cur) == 0 and not ["set", "unset"].has(str(clauses[i]["cmp"])):
|
||
clauses[i]["value"] = Builder.quote(steps[0])
|
||
_render()
|
||
_emit()
|
||
|
||
|
||
func set_clause_cmp(i: int, cmp: String) -> void:
|
||
clauses[i]["cmp"] = cmp
|
||
if cmp == "set" or cmp == "unset":
|
||
clauses[i]["value"] = ""
|
||
_render()
|
||
_emit()
|
||
|
||
|
||
## Sets a clause's amount from what a person typed (text gets quoted).
|
||
func set_clause_value(i: int, typed: String) -> void:
|
||
clauses[i]["value"] = InspectorKit.value_token(typed, known)
|
||
_emit()
|
||
|
||
|
||
## A scale clause's step.
|
||
func set_clause_step(i: int, step: String) -> void:
|
||
clauses[i]["value"] = Builder.quote(step)
|
||
_render()
|
||
_emit()
|
||
|
||
|
||
func set_text_mode(on: bool) -> void:
|
||
if on == text_mode:
|
||
return
|
||
if on:
|
||
_raw = text()
|
||
text_mode = true
|
||
else:
|
||
var m := Builder.parse_condition(_raw)
|
||
if not m["ok"]:
|
||
return
|
||
join = str(m["join"])
|
||
clauses.clear()
|
||
for c: Dictionary in m["clauses"]:
|
||
clauses.append(c.duplicate())
|
||
text_mode = false
|
||
_render()
|
||
|
||
|
||
func set_raw(t: String) -> void:
|
||
_raw = t
|
||
if TableSettingsDialog.when_error(t) == "":
|
||
_emit()
|
||
|
||
|
||
# ---------- UI ----------
|
||
|
||
func _render() -> void:
|
||
var focus_key := InspectorKit.focused_tag(self)
|
||
InspectorKit.clear(self)
|
||
if text_mode:
|
||
_render_text()
|
||
else:
|
||
if clauses.is_empty() and empty_note != "":
|
||
add_child(InspectorKit.note(empty_note, Style.MUTED, 14))
|
||
if clauses.size() > 1:
|
||
var jr := InspectorKit.hbox(6)
|
||
jr.add_child(InspectorKit.word("Needs", Style.MUTED))
|
||
var jd := InspectorKit.dropdown(["all of these (and)", "any of these (or)"], 0 if join == "and" else 1)
|
||
jd.tooltip_text = "And: every condition must hold. Or: one is enough."
|
||
jd.item_selected.connect(func(k: int) -> void: set_join("and" if k == 0 else "or"))
|
||
jr.add_child(jd)
|
||
add_child(jr)
|
||
for i in clauses.size():
|
||
add_child(_clause_row(i))
|
||
var foot := InspectorKit.hbox(12)
|
||
var add := InspectorKit.link(add_label, "Add a comparison")
|
||
add.pressed.connect(func() -> void: add_clause())
|
||
foot.add_child(add)
|
||
foot.add_child(InspectorKit.as_text_link("Edit this condition as rule text", set_text_mode.bind(true)))
|
||
add_child(foot)
|
||
InspectorKit.refocus(self, focus_key)
|
||
|
||
|
||
func _render_text() -> void:
|
||
var le := InspectorKit.line(_raw, "e.g. @str >= 8 (empty: always)")
|
||
InspectorKit.code(le)
|
||
InspectorKit.tag(le, _key + ".raw")
|
||
var err := InspectorKit.error_label()
|
||
var back := InspectorKit.link("Use the builder", "Show this condition as rows")
|
||
var check := func() -> void:
|
||
var e := TableSettingsDialog.when_error(_raw)
|
||
InspectorKit.show_error(err, "" if e == "" else "This condition can't be worked out: " + e)
|
||
InspectorKit.mark(le, e == "")
|
||
var m := Builder.parse_condition(_raw)
|
||
back.disabled = not m["ok"]
|
||
back.tooltip_text = "Show this condition as rows" if m["ok"] else "The builder can't show this (%s), so it stays as text" % str(m.get("error", "")).to_lower()
|
||
le.text_changed.connect(func(s: String) -> void:
|
||
set_raw(s)
|
||
check.call()
|
||
)
|
||
add_child(le)
|
||
add_child(err)
|
||
back.pressed.connect(func() -> void: set_text_mode(false))
|
||
add_child(back)
|
||
check.call()
|
||
|
||
|
||
func _clause_row(i: int) -> Control:
|
||
var c := clauses[i]
|
||
var v := InspectorKit.vbox(3)
|
||
var f := InspectorKit.flow()
|
||
v.add_child(f)
|
||
var picker := ValuePicker.new(main, str(c["var"]))
|
||
picker.extras = extras
|
||
picker.value_chosen.connect(func(n: String) -> void: set_clause_var(i, n))
|
||
f.add_child(picker)
|
||
var cmp := str(c["cmp"])
|
||
var dd := InspectorKit.dropdown(CMP_LABELS, maxi(0, CMPS.find(cmp)))
|
||
dd.item_selected.connect(func(k: int) -> void: set_clause_cmp(i, CMPS[k]))
|
||
f.add_child(dd)
|
||
var err := InspectorKit.error_label()
|
||
var le: LineEdit = null
|
||
var steps := ScaleFields.steps_of(main, str(c["var"]))
|
||
if not steps.is_empty() and cmp != "set" and cmp != "unset" and cmp != "has":
|
||
var cur := str(c["value"])
|
||
cur = Builder.unquote(cur) if Builder.is_text_literal(cur) else (Scales.label_at(steps, int(cur)) if cur.is_valid_int() else cur)
|
||
var sd := ScaleFields.step_dropdown(steps, cur, func(st: String) -> void: set_clause_step(i, st))
|
||
InspectorKit.tag(sd, "%s.%d.step" % [_key, i])
|
||
f.add_child(sd)
|
||
elif cmp != "set" and cmp != "unset":
|
||
le = InspectorKit.amount(InspectorKit.shown_token(str(c["value"])), "8")
|
||
le.tooltip_text = "A number, a word, or a value such as @soc"
|
||
InspectorKit.tag(le, "%s.%d.value" % [_key, i])
|
||
var lref := le
|
||
le.text_changed.connect(func(s: String) -> void:
|
||
set_clause_value(i, s)
|
||
_check_clause(i, lref, err)
|
||
)
|
||
f.add_child(le)
|
||
var x := InspectorKit.xbutton("Remove this condition")
|
||
x.pressed.connect(remove_clause.bind(i))
|
||
f.add_child(x)
|
||
v.add_child(err)
|
||
_check_clause(i, le, err)
|
||
return v
|
||
|
||
|
||
func _check_clause(i: int, le: LineEdit, err: Label) -> void:
|
||
var c := clauses[i]
|
||
var msg := ""
|
||
if str(c["var"]) == "":
|
||
msg = "Pick a value to compare."
|
||
elif str(c["cmp"]) != "set" and str(c["cmp"]) != "unset" and str(c["value"]) == "":
|
||
msg = "Fill in what to compare with."
|
||
elif Builder.clause_text(c) != "" and TableSettingsDialog.when_error(Builder.clause_text(c)) != "":
|
||
msg = "This can't be worked out: " + TableSettingsDialog.when_error(Builder.clause_text(c))
|
||
InspectorKit.show_error(err, msg)
|
||
if le:
|
||
InspectorKit.mark(le, msg == "" or str(c["var"]) == "")
|