ludic/packages/ludic.ui/controls_build.ludic
Orkuncakilkaya 9f59000f5b feat(ui): number and key fields, notes, anchored popovers, text-shadow, and pictures as painted
- <input type="number" min max step>: typed digits replace its value (a minus when min allows one,
  a point when step has a fraction), Enter or leaving the field commits them held between min and
  max, the left and right arrows step it.
- <input type="key">: Enter or a click starts it listening and the next key is its value, Tab, the
  arrows and Enter included; Esc stops it listening, Backspace clears it, `shown` names the value,
  and ui_capturing() tells the host to leave its keys alone (through the frame that ended it).
- note="..." under any control's label (.ui-labels > .ui-label + .ui-note); a range's (or number's)
  value with decimals, format="percent" and a unit.
- A popover with `anchor="id"` (the nearest element of that id) or a bare `anchor` (the element
  before it) sits beside it by `placement` (right, left, bottom, top), its margin the gap, opens to
  the other side where it would leave the screen, and is kept on it.
- A tooltip's title splits into lines at a newline or a written \n.
- text-shadow, inherited, drawn sharp under the text.
- ui_opacity(): the group opacity a native's draw is at.
- border-image is drawn as painted with no background colour, tinted by one, and not at all when
  that colour is transparent (it drew a white nine-slice).
- A picture file is drawn untinted, as a browser draws one; an atlas cell (prefix:name) still takes
  the color around it, and an <img> with its own color is tinted by it.
- The render3d backend loads a picture again when its file changes or appears (a failed load was
  kept for ever), and at once after ui_image_reload(path); takes a path with a drive letter (C:/...)
  as a path rather than an atlas; and slices a nine-slice by its texture's own width and height.
- ui_inputs.ludic and ui_look.ludic; LANGUAGE.md and the changeset say all of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 19:55:20 +03:00

100 lines
3.4 KiB
Text

# controls_build.ludic - a control's parts, made as plain child boxes a stylesheet can style:
# input[type=checkbox|radio] .ui-label .ui-track > .ui-knob
# input[type=range] .ui-label .ui-track > .ui-fill, .ui-thumb .ui-value
# input[type=text|number|key] .ui-label .ui-field > .ui-value (+ .ui-caret while focused)
# select .ui-label .ui-prev .ui-value .ui-next (its <option>s hidden)
# with a note="...", the label is .ui-labels > .ui-label + .ui-note
function ct_part(n: UiNode, cls: string, text: string) -> UiNode {
let p = new UiNode
p.key = `{n.key}/{cls}`
p.tag = "span"
p.env = n.env
p.sheet = n.sheet
p.children = new []UiNode
p.classes = new []string
push(p.classes, cls)
p.akeys = new []string
p.avals = new []string
p.vals = new []Val
if text != "" {
p.kind = UI_TEXT
p.text = text
}
return p
}
# <progress value max> and <meter value min max>: a track with a fill as far as the value
function ct_build_bar(n: UiNode) -> void {
n.dir = UI_ROW
let lo = Value.as_float(ui_attr(n, "min"))
var hi = Value.as_float(ui_attr(n, "max"))
if hi <= lo { hi = lo + 1.0 }
let fill = ct_part(n, "ui-fill", "")
fill.w_pct = Math.clamp((Value.as_float(ui_attr(n, "value")) - lo) / (hi - lo), 0.0, 1.0) * 100.0
push(n.children, fill)
}
function ct_build(n: UiNode) -> void {
if n.tag == "progress" or n.tag == "meter" {
ct_build_bar(n)
return
}
n.dir = UI_ROW
n.focusable = true
let t = ui_attr_text(n, "type")
if n.tag == "select" {
ct_build_select(n)
return
}
ct_label(n)
if t == "checkbox" or t == "radio" {
let track = ct_part(n, "ui-track", "")
push(track.children, ct_part(n, "ui-knob", ""))
push(n.children, track)
} else if t == "range" {
ct_build_range(n)
} else {
let field = ct_part(n, "ui-field", "")
push(field.children, ct_part(n, "ui-value", ct_field_text(n)))
if fc_key == n.key and t != "key" { push(field.children, ct_part(n, "ui-caret", "")) }
push(n.children, field)
}
}
function ct_build_range(n: UiNode) -> void {
let lo = Value.as_float(ui_attr(n, "min"))
var hi = Value.as_float(ui_attr(n, "max"))
if hi <= lo { hi = lo + 1.0 }
let v = Value.as_float(ui_attr(n, "value"))
let track = ct_part(n, "ui-track", "")
let fill = ct_part(n, "ui-fill", "")
fill.w_pct = Math.clamp((v - lo) / (hi - lo), 0.0, 1.0) * 100.0
push(track.children, fill)
push(track.children, ct_part(n, "ui-thumb", ""))
push(n.children, track)
push(n.children, ct_part(n, "ui-value", ct_value_text(n)))
}
function ct_build_select(n: UiNode) -> void {
let opts = n.children
n.children = new []UiNode
ct_label(n)
var shown = ""
let at = ct_option_at(n, opts)
if at >= 0 { shown = opts[at].text }
push(n.children, ct_part(n, "ui-prev", "<"))
push(n.children, ct_part(n, "ui-value", shown))
push(n.children, ct_part(n, "ui-next", ">"))
n.opts = opts
}
# which option the select's value names: an option's value="...", else its place
function ct_option_at(n: UiNode, opts: []UiNode) -> int {
let v = ui_attr_text(n, "value")
for i in 0 .. len(opts) {
let ov = ui_attr_text(opts[i], "value")
if ov == v or (ov == "" and string(i) == v) { return i }
}
return -1
}
function ct_field_text(n: UiNode) -> string {
let t = ui_attr_text(n, "type")
if t == "key" { return ct_key_text(n) }
if t == "number" { return ct_num_shown(n) }
return ui_attr_text(n, "value")
}