From the fence's kept frames in 22 minutes of play: - A component field that is a record or a list of records was `value_put(o, k, view_val_T(x))`, a whole new tree every frame (HudPrompt's notifications). view_fill.ludic generates view_set_T / view_set_list_T / view_fill_T that fill the object and list under the key in place. - value_list_fit dropped the items it cut off and value_item made new ones as the list grew back, a Value per item per regrowth (value_item / value_set_strs); the cut-off items are now the list's spares (Val.spare), and an item of another kind is turned rather than replaced. - ludic.ui: a scroll box's "scroll" and a slider's "change" fired a fresh Value.float a frame (sc_walk, scroll.ludic:36); ui_fire_float takes one from a ring kept with the state (fired.ludic). ui_object_fit_into is exported, for a draw that keeps its list. - Json.read_file(path): read, parsed, and the file's text given back - Json.parse(Fs.read_text()) kept the whole file on every read (Maroon Lake's settings peeks). Golden value_list_regrow: a list alternating 6 and 2 items every frame keeps nothing (the toolchain before this fails it: +128 B new Val from value_item). Game compiles; ludic.i18n/settings/hints/ base tests pass (ludic.ui has none); arena and fence goldens unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
4.2 KiB
Text
89 lines
4.2 KiB
Text
# controls.ludic - what a control does: a button (or anything with on-click) is pressed, a checkbox
|
|
# flips, a radio chooses its value, a select steps through its options, a range moves by its step
|
|
# (or to where the pointer is on its track), a text field takes what is typed, a number field its
|
|
# digits, a key field the next key. Each says so with on-change and event.value, after the frame is drawn.
|
|
function ct_type(n: UiNode) -> string {
|
|
if n.tag == "select" { return "select" }
|
|
if n.tag != "input" { return "" }
|
|
let t = ui_attr_text(n, "type")
|
|
if t == "" { return "text" }
|
|
return t
|
|
}
|
|
function ct_takes_arrows(n: UiNode) -> bool { return ct_type(n) == "range" or ct_type(n) == "select" or ct_type(n) == "number" }
|
|
function ct_takes_space(n: UiNode) -> bool { return ct_type(n) == "text" }
|
|
function ct_change(ui_st: mut UiState, n: UiNode, v: Val) -> void {
|
|
ui_sound(ui_st, ct_sound(n))
|
|
ui_fire(ui_st, n, "change", v)
|
|
}
|
|
function ct_sound(n: UiNode) -> string {
|
|
let s = ui_attr_text(n, "sound")
|
|
if s == "" { return "click" }
|
|
return s
|
|
}
|
|
function ct_activate(ui_st: mut UiState, n: UiNode) -> void {
|
|
if not n.enabled { return }
|
|
let t = ct_type(n)
|
|
if t == "checkbox" { ct_change(ui_st, n, vl_bool(ui_st, not ui_attr_on(n, "checked"))) } else if t == "radio" { ct_change(ui_st, n, ui_attr(n, "value")) } else if t == "select" { ct_step(ui_st, n, 1) } else if t == "key" { ct_key_start(ui_st, n) } else if t == "number" { ct_num_commit(ui_st, n) } else if t == "text" { ct_submit(ui_st, n) } else if t == "" {
|
|
ui_sound(ui_st, ct_sound(n))
|
|
ui_fire(ui_st, n, "press", ui_st.fc_none)
|
|
}
|
|
}
|
|
function ct_step(ui_st: mut UiState, n: UiNode, d: int) -> void {
|
|
let t = ct_type(n)
|
|
if t == "select" and n.opts != null and len(n.opts) > 0 {
|
|
let at = (ct_option_at(n, n.opts) + d + len(n.opts)) % len(n.opts)
|
|
let ov = ui_attr(n.opts[at], "value")
|
|
if Value.kind(ov) == 0 { ct_change(ui_st, n, Value.int(at)) } else { ct_change(ui_st, n, ov) }
|
|
}
|
|
if t == "number" { ct_num_step(ui_st, n, d) }
|
|
if t == "range" {
|
|
var step = Value.as_float(ui_attr(n, "step"))
|
|
if step <= 0.0 { step = 1.0 }
|
|
ct_range_set(ui_st, n, Value.as_float(ui_attr(n, "value")) + step * float(d))
|
|
}
|
|
}
|
|
function ct_range_set(ui_st: mut UiState, n: UiNode, v0: float) -> void {
|
|
let lo = Value.as_float(ui_attr(n, "min"))
|
|
let hi = Value.as_float(ui_attr(n, "max"))
|
|
let v = Math.clamp(v0, lo, Math.max(hi, lo))
|
|
if v != Value.as_float(ui_attr(n, "value")) { ui_fire_float(ui_st, n, "change", v) }
|
|
}
|
|
# a range's track pressed or dragged: the value where the pointer is
|
|
function ct_press(ui_st: mut UiState, n: UiNode, x: float, y: float) -> void {
|
|
if ct_type(n) != "range" { return }
|
|
for i in 0 .. len(n.children) {
|
|
let t = n.children[i]
|
|
if sel_has(t.classes, "ui-track") and t.cw > 0.0 {
|
|
let lo = Value.as_float(ui_attr(n, "min"))
|
|
let hi = Value.as_float(ui_attr(n, "max"))
|
|
var step = Value.as_float(ui_attr(n, "step"))
|
|
if step <= 0.0 { step = 1.0 }
|
|
let raw = lo + Math.clamp((x - t.x) / t.cw, 0.0, 1.0) * (hi - lo)
|
|
ct_range_set(ui_st, n, lo + float(int((raw - lo) / step + 0.5)) * step)
|
|
}
|
|
}
|
|
}
|
|
function ct_drag(ui_st: mut UiState, root: UiNode, key: string, x: float, y: float) -> void {
|
|
let n = fr_node(root, key)
|
|
if n != null { ct_press(ui_st, n, x, y) }
|
|
}
|
|
# typing into a focused text field, or the key a key field is waiting for
|
|
@alloc_ok("on a key typed into a field, not a frame")
|
|
function ct_typed(ui_st: mut UiState, n: UiNode) -> void {
|
|
let t = ct_type(n)
|
|
if t == "key" { ct_key_typed(ui_st, n) }
|
|
if t == "number" { ct_num_typed(ui_st, n) }
|
|
if t != "text" or (ui_st.in_now.typed == "" and not ui_st.in_now.backspace) { return }
|
|
var s = ui_attr_text(n, "value")
|
|
if ui_st.in_now.backspace and len(s) > 0 { s = s[0..ct_char_start(s)] }
|
|
var max = Value.as_int(ui_attr(n, "maxlength"))
|
|
if max <= 0 { max = 1000 }
|
|
if len(ui_st.in_now.typed) > 0 and len(s) < max { s = s + ui_st.in_now.typed[0..Math.min(len(ui_st.in_now.typed), max - len(s))] }
|
|
ui_fire(ui_st, n, "change", Value.str(s))
|
|
}
|
|
# where the last UTF-8 character starts, so Backspace takes a whole letter
|
|
function ct_char_start(s: string) -> int {
|
|
var i = len(s) - 1
|
|
while i > 0 and s[i] >= 128 and s[i] < 192 { i -= 1 }
|
|
return i
|
|
}
|