ludic/packages/ludic.ui/env.ludic
Orkuncakilkaya 49b5e80af5 ludic ui-preview: ludic.ui components previewed for a studio over stdio (R5)
A prebuilt tool, bin/ludic-ui-preview (built by ludic-dev build, shipped beside ludic-lsp, run as
`ludic ui-preview [--font DIR]`): ludic.ui with a backend that records every draw call as a line,
and mock component classes the studio describes (load / model / calls). frame t runs ui_show at
interface time t; text is measured on the CPU with the game's font.json metrics; locale goes
through ludic.i18n; tree / box / rules inspect the frame. No game code, no GPU, no network. The wire
protocol is frozen as tools/ui-preview/protocol-v1.md; smoke.txt is a transcript to run.

ludic.ui gains, all additive: UiClass.make_of, UiBackend.said / emitted, UiRule.text / at (with
comment line breaks kept so rule lines count true, and a class's styles read under its .lss path),
and ui_root, ui_find, ui_rules_of, ui_rule_value, ui_building, ui_class, ui_class_load,
ui_file_forget, ui_errors_clear; ui_nine_cuts_into exported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 19:53:26 +03:00

94 lines
3.5 KiB
Text

# env.ludic - what a name means where an expression is read: the loop's own names, then the
# component's props and state, then the view's model. A component sees its props, its state
# and the model - never the names of whoever used it.
export property UiEnv {
names: []string = null
vals: []Val = null
state: Val = null
parent: UiEnv = null
model: Val = null
slot: UiTpl = null
slot_env: UiEnv = null
on_names: []string = null # a component's on-* attributes, which its `emit` runs
on_acts: []UiAct = null
on_envs: []UiEnv = null
ctx: UiEnv = null # what a <provide> above gave, seen through components too
inst: UiInst = null # the Ludic component whose template this is
}
function env_new(ui_st: mut UiState, parent: UiEnv) -> UiEnv {
let e = env_take(ui_st)
e.parent = parent
if parent != null {
e.model = parent.model
e.ctx = parent.ctx
}
return e
}
function env_bind(e: UiEnv, name: string, v: Val) -> void {
ui_kept(e.names, name)
ui_kept(e.vals, v)
}
function env_get(ui_st: mut UiState, e: UiEnv, name: string) -> Val {
var at = e
while at != null {
var i = len(at.names) - 1
while i >= 0 {
if at.names[i] == name { return at.vals[i] }
i -= 1
}
if at.state != null and Value.has(at.state, name) != 0 { return Value.get(at.state, name) }
at = at.parent
}
var cx = e.ctx
while cx != null {
for i in 0 .. len(cx.names) {
if cx.names[i] == name { return cx.vals[i] }
}
cx = cx.parent
}
if e.model != null and Value.has(e.model, name) != 0 { return Value.get(e.model, name) }
@alloc_ok("only when a template is wrong: a broken screen, never a working one")
ui_err(ui_st, `no {name} here: not a loop's, a prop, a state or in the view`)
return Value.null()
}
# `set name = value`: the nearest component's state of that name, or <state> that holds it
function env_set(ui_st: mut UiState, e: UiEnv, name: string, v: Val) -> void {
var at = e
while at != null {
if at.inst != null {
let args = Value.list()
Value.add(args, v)
@alloc_ok("on an action that sets a component's state, not a frame")
if Value.kind(at.inst.cls.call(at.inst.ptr, `set:{name}`, args, null)) != 99 { return }
}
if at.state != null and Value.has(at.state, name) != 0 {
Value.put(at.state, name, v)
return
}
at = at.parent
}
@alloc_ok("only when a template is wrong: a broken screen, never a working one")
ui_err(ui_st, `set {name}: no state here holds it - declare it with <state {name}="..."/>`)
}
# an error is said once, however many frames make it, and names the template line being built
# said by the runtime, or by a host about something a template asked for
@alloc_ok("only when a template is wrong: a broken screen, never a working one")
export function ui_err(ui_st: mut UiState, msg0: string) -> void {
var msg = msg0
if ui_st.ui_at != "" and not ui_has(msg, ".xml:") { msg = `{ui_st.ui_at}: {msg}` }
for i in 0 .. len(ui_st.ui_errs) {
if ui_st.ui_errs[i] == msg { return }
}
push(ui_st.ui_errs, msg)
if ui_st.ui_be != null and ui_st.ui_be.said != null { ui_st.ui_be.said(msg) } else { print(`ui: {msg}`) }
}
function ui_has(s: string, part: string) -> bool {
let n = len(part)
for i in 0 .. len(s) - n + 1 {
if sat(s, i, part) { return true }
}
return false
}
# every error said so far - for a host to show them where a developer will see them
export function ui_errors(ui_st: UiState) -> []string { return ui_st.ui_errs }