248 lines
7.5 KiB
GDScript3
248 lines
7.5 KiB
GDScript3
|
|
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). 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
|
|||
|
|
_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()
|
|||
|
|
|
|||
|
|
|
|||
|
|
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 := ""
|
|||
|
|
var f := get_viewport().gui_get_focus_owner() if is_inside_tree() else null
|
|||
|
|
if f and is_ancestor_of(f) and f.has_meta(InspectorKit.KEY):
|
|||
|
|
focus_key = str(f.get_meta(InspectorKit.KEY))
|
|||
|
|
for c in get_children():
|
|||
|
|
remove_child(c)
|
|||
|
|
c.queue_free()
|
|||
|
|
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)
|
|||
|
|
var as_text := InspectorKit.link("Edit as text", "Edit this condition as rule text")
|
|||
|
|
as_text.add_theme_color_override("font_color", Style.MUTED)
|
|||
|
|
as_text.pressed.connect(func() -> void: set_text_mode(true))
|
|||
|
|
foot.add_child(as_text)
|
|||
|
|
add_child(foot)
|
|||
|
|
if focus_key != "":
|
|||
|
|
var c := InspectorKit.find_tagged(self, focus_key)
|
|||
|
|
if c:
|
|||
|
|
c.grab_focus.call_deferred()
|
|||
|
|
|
|||
|
|
|
|||
|
|
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
|
|||
|
|
if 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"]) == "")
|