feat(ui): ludic.ui is a UI framework - input, controls, render3d backend, the CSS a game needs
A game no longer writes a host:
- The runtime reads Input itself: focus and keyboard navigation (Tab, arrows, Enter/Space,
autofocus), the pointer (hover, :active, click on release, drag), and scroll boxes with the
wheel, a draggable scrollbar, clipping and scroll-into-view.
- HTML's controls are built in (button, checkbox, radio, range, select, text, key capture), made
of plain parts a stylesheet styles, each reporting with on-change and event.value.
- ludic.ui/render3d.ludic draws with render3d's overlay: textures, named atlases (icon:NAME),
nine-slice border-image, rounded rects and rings, clipping, and a scale.
- Hooks for the program's language, sounds and clock (ui_translator, ui_sounds, ui_clock).
- ui_dev: hot reload, errors on screen, LUDIC_UI_DUMP.
- CSS:
- colours as #rgb / #rrggbbaa / rgb() / rgba() / names;
- border-radius and outline (following the radius), box-shadow, background-image and a tinted
border-image;
- group opacity, @keyframes / animation, transition;
- :focus, :focus-visible and :focus-within.
- HTML mixed content, and boolean attributes.
- render3d gains tex_width / tex_height, and the XML reader keeps text runs in order.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
20d011f8e9
commit
1dad96102a
45 changed files with 1386 additions and 218 deletions
86
packages/ludic.ui/controls_build.ludic
Normal file
86
packages/ludic.ui/controls_build.ludic
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
# 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|key] .ui-label .ui-field > .ui-value (+ .ui-caret while focused)
|
||||
# select .ui-label .ui-prev .ui-value .ui-next (its <option>s hidden)
|
||||
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
|
||||
}
|
||||
function ct_build(n: UiNode) -> void {
|
||||
n.dir = UI_ROW
|
||||
n.focusable = true
|
||||
let t = ui_attr_text(n, "type")
|
||||
let label = ui_attr_text(n, "label")
|
||||
if label != "" { push(n.children, ct_part(n, "ui-label", ui_tr(label))) }
|
||||
if n.tag == "select" {
|
||||
ct_build_select(n)
|
||||
return
|
||||
}
|
||||
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", ui_attr_text(n, "value")))
|
||||
}
|
||||
function ct_build_select(n: UiNode) -> void {
|
||||
let opts = n.children
|
||||
n.children = new []UiNode
|
||||
let label = ui_attr_text(n, "label")
|
||||
if label != "" { push(n.children, ct_part(n, "ui-label", ui_tr(label))) }
|
||||
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 {
|
||||
if ui_attr_text(n, "type") != "key" { return ui_attr_text(n, "value") }
|
||||
if ct_capture == n.key { return ui_tr("press a key...") }
|
||||
return Input.key_label(Value.as_int(ui_attr(n, "value")))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue