ludic/packages/ludic.ui/controls_number.ludic
Orkuncakilkaya 19fcf60599 wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic
  1804 vars into 126 states, 64 into lets; 23498 edits in 460 files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:31:34 +03:00

71 lines
3.5 KiB
Text

# controls_number.ludic - <input type="number" min max step>: digits typed into it (a minus when min
# allows one, a point when step has a fraction) replace its value with a draft, which Enter or leaving
# the field commits, held between min and max; Backspace takes a digit back (of the value, when it is
# the first key), and the left and right arrows step it. Each commit is one on-change with the
# number: an int when step and the value are whole.
function ct_num_bound(n: UiNode, key: string) -> bool { return Value.kind(ui_attr(n, key)) != 0 }
function ct_num_step_of(n: UiNode) -> float {
let s = Value.as_float(ui_attr(n, "step"))
if s <= 0.0 { return 1.0 }
return s
}
function ct_num_clamp(n: UiNode, v0: float) -> float {
var v = v0
if ct_num_bound(n, "max") { v = Math.min(v, Value.as_float(ui_attr(n, "max"))) }
if ct_num_bound(n, "min") { v = Math.max(v, Value.as_float(ui_attr(n, "min"))) }
return v
}
function ct_num_set(ui_st: mut UiState, n: UiNode, v0: float) -> void {
let v = ct_num_clamp(n, v0)
let step = ct_num_step_of(n)
let was = ui_attr(n, "value")
if Value.kind(was) != 0 and Value.as_float(was) == v { return }
if step == float(int(step)) and v == float(int(v)) { ct_change(ui_st, n, Value.int(int(v))) } else { ct_change(ui_st, n, Value.float(v)) }
}
# Enter, or the focus leaving it: the draft becomes the value (a draft of nothing changes nothing)
function ct_num_commit(ui_st: mut UiState, n: UiNode) -> void {
if ui_st.ct_num_key != n.key { return }
let s = ui_st.ct_num_text
ui_st.ct_num_key = ""
if s == "" or s == "-" or s == "." or s == "-." { return }
ct_num_set(ui_st, n, el_num(s))
}
function ct_num_step(ui_st: mut UiState, n: UiNode, d: int) -> void {
var base = Value.as_float(ui_attr(n, "value"))
if ui_st.ct_num_key == n.key and ui_st.ct_num_text != "" and ui_st.ct_num_text != "-" { base = el_num(ui_st.ct_num_text) }
ui_st.ct_num_key = ""
ct_num_set(ui_st, n, base + ct_num_step_of(n) * float(d))
}
# what was typed this frame, the characters a number can hold; Backspace takes one back
function ct_num_typed(ui_st: mut UiState, n: UiNode) -> void {
if ui_st.fc_key != n.key or (ui_st.in_now.typed == "" and not ui_st.in_now.backspace) { return }
if ui_st.ct_num_key != n.key {
ui_st.ct_num_key = n.key
ui_st.ct_num_text = ""
if ui_st.in_now.typed == "" { ui_st.ct_num_text = ui_attr_text(n, "value") }
}
if ui_st.in_now.backspace and len(ui_st.ct_num_text) > 0 { ui_st.ct_num_text = ui_st.ct_num_text[0..len(ui_st.ct_num_text) - 1] }
let step = ct_num_step_of(n)
let lo = Value.as_float(ui_attr(n, "min"))
for i in 0 .. len(ui_st.in_now.typed) {
let c = ui_st.in_now.typed[i]
let digit = c >= 48 and c <= 57
let minus = c == 45 and ui_st.ct_num_text == "" and (not ct_num_bound(n, "min") or lo < 0.0)
let point = c == 46 and step != float(int(step)) and not ui_has(ui_st.ct_num_text, ".")
if (digit or minus or point) and len(ui_st.ct_num_text) < 24 { ui_st.ct_num_text = ui_st.ct_num_text + ui_st.in_now.typed[i..i + 1] }
}
}
# the focus has gone from the field being typed in: what was typed is kept
function ct_num_blur(ui_st: mut UiState, root: UiNode) -> void {
if ui_st.ct_num_key == "" or ui_st.ct_num_key == ui_st.fc_key { return }
let n = fr_node(root, ui_st.ct_num_key)
if n == null {
ui_st.ct_num_key = ""
return
}
ct_num_commit(ui_st, n)
}
function ct_num_shown(ui_st: UiState, n: UiNode) -> string {
if ui_st.ct_num_key == n.key { return ui_st.ct_num_text }
return ct_value_text(n)
}