TableTower/ui/inspector_effects_scale.gd

135 lines
5 KiB
GDScript3
Raw Normal View History

class_name EffectsScaleRow
extends RefCounted
## The effects builder's row for a scale value (BuilderScales): `[Move up ▾]
## [Climate ▾] by [1] step`, `[Set ▾] [Climate ▾] to [Hot ▾]`, `[Move toward ▾]
## [Climate ▾] one step toward [Temperate ▾]`, `[Raise ▾] [Atmosphere ▾] to at
## least [Medium ▾]`, `[Lower ▾] … to at most [Thin ▾]`, `[Reset ▾] [Climate ▾]`.
## Step pickers list the value's steps. Edits go through EffectsBuilder.
const OPS := ["step_up", "step_down", "set", "toward", "at_least", "at_most", "reset"]
const OP_LABELS := ["Move up", "Move down", "Set", "Move toward", "Raise", "Lower", "Reset"]
const _WORDS := {"set": "to", "toward": "one step toward", "at_least": "to at least", "at_most": "to at most"}
## True when row model `m` shows as a scale row: a scale op, or Set/Reset of a
## scale value to one of its steps (Set to an expression stays a plain row).
static func shows(m: Dictionary, steps: Array) -> bool:
var op := str(m["op"])
if BuilderScales.OPS.has(op):
return true
if steps.is_empty():
return false
if op == "reset":
return true
var a := str(m["amount"])
return op == "set" and (a == "" or (Builder.is_text_literal(a) and Scales.position_of(steps, Builder.unquote(a)) > 0))
## Switches row model `m` to `op`, keeping its step where it can: a count of
## steps for Move up/down, a step's name for the others (quoted for Set).
static func set_op(m: Dictionary, op: String, steps: Array) -> void:
var was := str(m["op"])
var step := current_step(m, steps)
if step == "" and not steps.is_empty():
step = str(steps[0])
m["op"] = op
match op:
"step_up", "step_down":
if was != "step_up" and was != "step_down":
m["amount"] = "1"
"set":
m["amount"] = Builder.quote(step) if step != "" else ""
"toward", "at_least", "at_most":
m["amount"] = step
"reset":
m["amount"] = ""
## After the row's value changed: a scale value turns Add/Subtract into Move
## up/down (and anything that can't apply into Move up 1); a value that isn't
## a scale turns the scale ops back into ordinary ones.
static func on_var(m: Dictionary, steps: Array) -> void:
var op := str(m["op"])
if not steps.is_empty():
match op:
"add":
m["op"] = "step_up"
"subtract":
m["op"] = "step_down"
"multiply", "divide", "append", "remove":
m["op"] = "step_up"
m["amount"] = "1"
"set":
if not shows(m, steps):
m["amount"] = Builder.quote(str(steps[0]))
return
match op:
"step_up":
m["op"] = "add"
"step_down":
m["op"] = "subtract"
"toward", "at_least", "at_most":
m["op"] = "set"
m["amount"] = Builder.quote(str(m["amount"])) if str(m["amount"]) != "" else ""
## The step a row names ("" for none or a count of steps).
static func current_step(m: Dictionary, steps: Array) -> String:
var a := str(m.get("amount", ""))
match str(m["op"]):
"set":
return Builder.unquote(a) if Builder.is_text_literal(a) else ""
"toward", "at_least", "at_most":
return a
return ""
## Fills flow `f` with row `i` of `eb` (the op dropdown, the value picker and
## the step or count), then `x`, and shows the row's problem in `err`.
static func fill(eb: EffectsBuilder, i: int, f: Container, picker: ValuePicker, x: Button, err: Label) -> void:
var m := eb.rows[i]
var op := str(m["op"])
var steps := ScaleFields.steps_of(eb.main, str(m["var"]))
var dd := InspectorKit.dropdown(OP_LABELS, OPS.find(op))
dd.tooltip_text = "What to do"
dd.item_selected.connect(func(k: int) -> void: eb.set_row_op(i, OPS[k]))
f.add_child(dd)
picker.kinds = ["scale"]
f.add_child(picker)
var msg := "" if str(m["var"]) != "" else "Pick the value to change."
match op:
"step_up", "step_down":
f.add_child(InspectorKit.word("by"))
var le := InspectorKit.amount(str(m["amount"]), "1")
le.tooltip_text = "How many steps: a number, or dice such as 1d3"
InspectorKit.tag(le, "%s.%d.amount" % [eb._key, i])
var unit := InspectorKit.word("step" if str(m["amount"]) == "1" else "steps")
le.text_changed.connect(func(s: String) -> void:
eb.set_row_amount(i, s)
unit.text = "step" if s.strip_edges() == "1" else "steps"
_check_count(m, le, err)
)
f.add_child(le)
f.add_child(unit)
f.add_child(x)
_check_count(m, le, err)
return
"set", "toward", "at_least", "at_most":
f.add_child(InspectorKit.word(_WORDS[op]))
var sd := ScaleFields.step_dropdown(steps, current_step(m, steps), func(s: String) -> void: eb.set_row_step(i, s))
InspectorKit.tag(sd, "%s.%d.step" % [eb._key, i])
f.add_child(sd)
if msg == "" and current_step(m, steps) == "":
msg = "Pick a step."
f.add_child(x)
InspectorKit.show_error(err, msg)
static func _check_count(m: Dictionary, le: LineEdit, err: Label) -> void:
var a := str(m["amount"]).strip_edges()
var msg := "Pick the value to change." if str(m["var"]) == "" else ""
if msg == "":
msg = "Fill in how many steps." if a == "" else InspectorKit.expr_error(a, "The number of steps")
InspectorKit.show_error(err, msg)
InspectorKit.mark(le, msg == "" or str(m["var"]) == "")