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:
Orkun ÇAKILKAYA 2026-09-24 17:26:09 +03:00
parent 20d011f8e9
commit 1dad96102a
45 changed files with 1386 additions and 218 deletions

View file

@ -0,0 +1,27 @@
# ui_anim.ludic — CSS animation and transition on ludic.ui's frame clock (sixty frames a second
# when the renderer gives no clock of its own): a fade-in from @keyframes, a pulse that loops back
# and forth, and a transition easing an opacity that changed.
import "ludic.ui"
program UiAnim {
numbers float
var dim: bool = false
view Anim {
dim = dim
}
const PAGE: string = "<ui><style>@keyframes fade { from { opacity: 0 } to { opacity: 1 } }\n@keyframes pulse { from { opacity: 1 } to { opacity: 0.5 } }\n.in { animation: fade 1s }\n.beat { animation: pulse 0.5s infinite alternate }\n.soft { transition: opacity 1s }\n.dim { opacity: 0.2 }</style><screen name='a'><p class='in'>a</p><p class='beat'>b</p><p class=\"soft {dim ? 'dim' : ''}\">c</p></screen></ui>"
function at() -> string {
ui_show("a", view_anim(), 0.0, 0.0, 200.0, 200.0)
let r: UiNode = ui_nodes("a", view_anim())
return `{int(r.children[0].alpha * 100.0)} {int(r.children[1].alpha * 100.0)} {int(r.children[2].alpha * 100.0)}`
}
entry {
ui_load_text(PAGE, "a.xml")
var out = at()
for i in 0 .. 29 { at() }
out = out + " | " + at()
dim = true
for i in 0 .. 29 { at() }
out = out + " | " + at()
print(out)
}
}

View file

@ -0,0 +1,71 @@
# ui_controls.ludic — ludic.ui's own controls, driven as a player would drive them: Tab and the
# arrows move the focus, Enter presses and flips, the arrows move a range and step a select, typing
# fills a text field and Backspace takes a letter back, a key field takes the next key, the pointer
# drags a range, and the wheel scrolls a box. Every change reaches the component as an event.
import "ludic.ui"
import "ui_controls_parts/Settings.ludic"
program UiControls {
numbers float
function frame(i: UiInput) -> UiNode {
ui_input(i)
ui_show("Settings", null, 0.0, 0.0, 400.0, 400.0)
let root: UiNode = ui_nodes("Settings", null)
ui_place(root, 0.0, 0.0, 400.0, 400.0)
return root
}
function press(tab: bool, down: bool, right: bool, enter: bool) -> UiInput {
let i = new UiInput
i.tab = tab
i.down_key = down
i.right = right
i.enter = enter
return i
}
function state(root: UiNode) -> string {
let page = root.children[0]
let last = page.children[len(page.children) - 1]
return last.text
}
entry {
ui_backend(new UiBackend)
var root = frame(new UiInput)
root = frame(press(true, false, false, false))
root = frame(press(false, false, false, true))
root = frame(press(false, true, false, false))
root = frame(press(false, false, false, true))
root = frame(press(false, true, false, false))
root = frame(press(false, false, true, false))
root = frame(press(false, false, true, false))
root = frame(press(false, true, false, false))
root = frame(press(false, false, true, false))
root = frame(press(false, true, false, false))
let typed = new UiInput
typed.typed = "lovelace"
root = frame(typed)
let back = new UiInput
back.backspace = true
root = frame(back)
root = frame(press(false, true, false, false))
root = frame(press(false, false, false, true))
let k = new UiInput
k.key = 70
root = frame(k)
print(state(root))
let range = root.children[0].children[2]
let track = range.children[1]
let at = new UiInput
at.x = track.x + track.cw * 0.8
at.y = track.y + 2.0
at.down = true
root = frame(at)
root = frame(new UiInput)
let list = root.children[0].children[6]
let w = new UiInput
w.x = list.x + 5.0
w.y = list.y + 5.0
w.wheel = -1.0
root = frame(w)
root = frame(new UiInput)
print(`{state(root)}; scrolled {int(root.children[0].children[6].scroll_y)}`)
}
}

View file

@ -0,0 +1,4 @@
.page { width: 400px; gap: 4px }
input, select { width: fill; height: 30px }
.list { overflow: auto; height: 60px }
p { height: 20px }

View file

@ -0,0 +1,10 @@
# Settings.ludic - a screen of controls: every change comes back as an event with its value
component Settings {
state music: bool = true
state volume: int = 5
state quality: int = 1
state name: string = "Ada"
state jump: int = 32
state pressed: int = 0
on go() { pressed += 1 }
}

View file

@ -0,0 +1,12 @@
<div class="page">
<button id="go" on-click="go()">Go</button>
<input type="checkbox" label="Music" checked="{music}" on-change="set music = event.value"/>
<input type="range" label="Volume" min="{0}" max="{10}" value="{volume}" on-change="set volume = event.value"/>
<select label="Quality" value="{quality}" on-change="set quality = event.value">
<option>Low</option><option>Medium</option><option>High</option>
</select>
<input type="text" label="Name" value="{name}" maxlength="{8}" on-change="set name = event.value"/>
<input type="key" label="Jump" value="{jump}" on-change="set jump = event.value"/>
<div class="list"><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p></div>
<p class="state">{pressed} {music} {volume} {quality} {name} {jump}</p>
</div>

View file

@ -11,7 +11,9 @@ program UiReload {
const V2: string = "<ui><screen name='note'><state n='{0}'/><h1>{title} again {n}</h1></screen></ui>"
function shown() -> string {
let root: UiNode = ui_nodes("note", view_note())
return root.children[0].text
let first = root.children[0]
if len(first.children) > 0 { return first.children[0].text }
return first.text
}
entry {
Fs.write_text(PATH, V1)