TableTower/core/describe_scales.gd
RobinFiredrake 03bc168756 UI for scales: make, show and edit ordered-step values
- 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>
2026-11-10 21:25:55 -06:00

42 lines
1.6 KiB
GDScript

class_name DescribeScales
extends RefCounted
## Scale values in plain words (Describe, Scales): "Climate 1 step up",
## "Climate 2 steps down", "Climate = Hot", "Climate 1 step toward
## Temperate", "Atmosphere at least Medium", "Atmosphere at most Thin". A
## step's number in a rule reads as its name ("@climate = 2" -> "Climate =
## Cold").
## The phrase for a scale model (BuilderScales.refine'd), or "" when the op
## isn't a scale one. `steps` are the value's steps; `expr` turns an amount
## expression into words (Describe._expr).
static func effect(m: Dictionary, subj: String, steps: Array, expr: Callable) -> String:
var amount := str(m["amount"])
match str(m["op"]):
"step_up", "step_down":
var up: bool = m["op"] == "step_up"
if amount.is_valid_int():
var n := int(amount)
if n < 0:
up = not up
n = -n
return "%s %d %s %s" % [subj, n, "step" if n == 1 else "steps", "up" if up else "down"]
return "%s %s steps %s" % [subj, expr.call(amount), "up" if up else "down"]
"set":
return "%s = %s" % [subj, step_word(amount, steps, expr)]
"toward":
return "%s 1 step toward %s" % [subj, amount]
"at_least":
return "%s at least %s" % [subj, amount]
"at_most":
return "%s at most %s" % [subj, amount]
return ""
## A value token beside a scale in words: a whole number reads as the step at
## that position ("2" -> "Cold"), anything else through `expr`.
static func step_word(tok: String, steps: Array, expr: Callable) -> String:
var s := tok.strip_edges()
if s.is_valid_int() and not steps.is_empty():
return Scales.label_at(steps, int(s))
return str(expr.call(s))