ludic/packages/ludic.ui/state.ludic

35 lines
1.3 KiB
Text

# state.ludic - a component's or a screen's own values, <state name="{first}"/>, kept from frame
# to frame by where the instance is in the tree, and set by `set name = value`
function st_for(ui_st: mut UiState, key: string, t: UiTpl, e: UiEnv) -> Val {
var decl: UiTpl = null
for i in 0 .. len(t.kids) {
if t.kids[i].tag == "state" { decl = t.kids[i] }
}
if decl == null { return null }
for i in 0 .. len(ui_st.st_keys) {
if ui_st.st_keys[i] == key { return ui_st.st_vals[i] }
}
let o = Value.object()
let was = ui_st.uv_on # a state's start is kept for good: its values are never pooled
ui_st.uv_on = false
for i in 0 .. len(decl.keys) { Value.put(o, decl.keys[i], ui_eval(ui_st, decl.vals[i], e)) }
ui_st.uv_on = was
ui_kept(ui_st.st_keys, key)
ui_kept(ui_st.st_vals, o)
return o
}
# a screen's state starts again the next time it opens
export function ui_forget(ui_st: mut UiState, screen: string) -> void {
let keys = new []string
let vals = new []Val
for i in 0 .. len(ui_st.st_keys) {
let k = ui_st.st_keys[i]
let mine = len(k) >= len(screen) and sat(k, 0, screen) and (len(k) == len(screen) or k[len(screen)] == 47)
if not mine {
push(keys, k)
push(vals, ui_st.st_vals[i])
}
}
ui_st.st_keys = keys
ui_st.st_vals = vals
}