Allocation reads as intent, not malloc: mem_alloc(64) -> bytes(64) (64 bytes -> a byte buffer) mem_alloc(w * h * 4) -> words(w * h) (w*h 32-bit words) bytes(n) mallocs n bytes and returns a plain pointer (byte-indexed); words(n) mallocs n*4 bytes and returns a `words` pointer (int-indexed). Since words(X) and mem_alloc(X*4) allocate the identical number of bytes, the migration cannot change any allocation size — the `* 4` factor just moves from the argument into the allocator name, pairing naturally with the Phase-7j `words` retyping (`var fb: words = words(w * h)`). Migrated 102 sites (mem_alloc(E*4) -> words(E), else bytes(E)); deleted the mem_alloc intrinsic. mem_realloc/free/copy/set stay as the low-level reallocation/free family. Reseeded (22565 lines); C-free fixpoint holds; goldens byte-identical; 18/18; vocab + doc-fences clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
283 lines
6.6 KiB
Text
283 lines
6.6 KiB
Text
# ui_draw.ludic — layout arrange, drawing, focus/activation and the game-facing
|
|
# accessors. Split out of ui.ludic (the storage/build/measure half).
|
|
|
|
# ---- arrange: rects, parents before children ------------------------------
|
|
fn ui_arrange(i: int, x: int, y: int) -> void {
|
|
ui_rx[i] = x
|
|
ui_ry[i] = y
|
|
let t = ui_type[i]
|
|
if ui_is_container(t) == false { return }
|
|
let dir = ui_dir(t)
|
|
let pad = ui_pad[i]
|
|
let gap = ui_gap[i]
|
|
let innerw = ui_rw[i] - 2 * pad
|
|
let innerh = ui_rh[i] - 2 * pad
|
|
var total = 0
|
|
var ng = 0
|
|
var grows = 0
|
|
for c in 0 .. ui_n {
|
|
if ui_parent[c] == i {
|
|
if ui_visible[c] == 1 {
|
|
if dir == 1 { total = total + ui_rh[c] } else { total = total + ui_rw[c] }
|
|
ng = ng + 1
|
|
grows = grows + ui_grow[c]
|
|
}
|
|
}
|
|
}
|
|
if ng > 1 { total = total + gap * (ng - 1) }
|
|
var avail = innerw
|
|
if dir == 1 { avail = innerh }
|
|
let extra = max(avail - total, 0)
|
|
var cx = x + pad
|
|
var cy = y + pad
|
|
for c in 0 .. ui_n {
|
|
if ui_parent[c] == i {
|
|
if ui_visible[c] == 1 {
|
|
var kw = ui_rw[c]
|
|
var kh = ui_rh[c]
|
|
if ui_grow[c] != 0 {
|
|
if grows > 0 {
|
|
if dir == 1 { kh = kh + extra / grows } else { kw = kw + extra / grows }
|
|
}
|
|
}
|
|
let al = ui_eff_align(c, i)
|
|
ui_rw[c] = kw
|
|
ui_rh[c] = kh
|
|
if dir == 1 {
|
|
var ax = x + pad
|
|
if al == 1 { ax = x + pad + (innerw - kw) / 2 }
|
|
if al == 2 { ax = x + pad + (innerw - kw) }
|
|
ui_arrange(c, ax, cy)
|
|
cy = cy + kh + gap
|
|
} else {
|
|
var ay = y + pad
|
|
if al == 1 { ay = y + pad + (innerh - kh) / 2 }
|
|
if al == 2 { ay = y + pad + (innerh - kh) }
|
|
ui_arrange(c, cx, ay)
|
|
cx = cx + kw + gap
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn ui_layout() -> void {
|
|
if ui_active < 0 { return }
|
|
ui_measure()
|
|
var px = (rt_fbw - ui_rw[ui_active]) / 2
|
|
var py = (rt_fbh - ui_rh[ui_active]) / 2
|
|
if ui_haspos[ui_active] == 1 {
|
|
px = ui_x[ui_active]
|
|
py = ui_y[ui_active]
|
|
}
|
|
ui_arrange(ui_active, px, py)
|
|
}
|
|
|
|
# ---- drawing --------------------------------------------------------------
|
|
fn ui_lighten(c: int) -> int {
|
|
var r = ((c >> 16) & 255)
|
|
var g = ((c >> 8) & 255)
|
|
var b = (c & 255)
|
|
r = r + (255 - r) * 4 / 10
|
|
g = g + (255 - g) * 4 / 10
|
|
b = b + (255 - b) * 4 / 10
|
|
return (((r << 16) | (g << 8)) | b)
|
|
}
|
|
|
|
fn ui_draw_node(i: int) -> void {
|
|
if ui_visible[i] == 0 { return }
|
|
let t = ui_type[i]
|
|
let rx = ui_rx[i]
|
|
let ry = ui_ry[i]
|
|
let rw = ui_rw[i]
|
|
let rh = ui_rh[i]
|
|
let skin = ui_skin[i]
|
|
var focused = 0
|
|
if i == ui_focus { focused = 1 }
|
|
|
|
if t == WT_PANEL {
|
|
if skin >= 0 {
|
|
rt_draw_9slice(skin, rx, ry, rw, rh, ui_inset[i])
|
|
} else {
|
|
if ui_bg[i] >= 0 { rt_fill_rect(rx, ry, rw, rh, ui_bg[i]) }
|
|
if ui_border[i] >= 0 { rt_frame_rect(rx, ry, rw, rh, ui_border[i]) }
|
|
}
|
|
}
|
|
if t == WT_BUTTON {
|
|
var bg = ui_bg[i]
|
|
if bg < 0 { bg = 0x2a2a3a }
|
|
if focused == 1 { bg = ui_lighten(bg) }
|
|
if skin >= 0 {
|
|
rt_draw_9slice(skin, rx, ry, rw, rh, ui_inset[i])
|
|
} else {
|
|
rt_fill_rect(rx, ry, rw, rh, bg)
|
|
}
|
|
var bc = ui_border[i]
|
|
if bc < 0 { bc = 0x555577 }
|
|
if focused == 1 { bc = 0xffff00 }
|
|
rt_frame_rect(rx, ry, rw, rh, bc)
|
|
let s = ui_str(i)
|
|
var fg = ui_fg[i]
|
|
if fg < 0 { fg = 0xffffff }
|
|
let font = ui_font[i]
|
|
let size = ui_size[i]
|
|
ui_draw_text(font, rx + (rw - ui_tw(font, s, size)) / 2, ry + (rh - ui_th(font, size)) / 2, s, fg, size)
|
|
}
|
|
if t == WT_LABEL {
|
|
let s = ui_str(i)
|
|
var fg = ui_fg[i]
|
|
if fg < 0 { fg = 0xffffff }
|
|
let font = ui_font[i]
|
|
let size = ui_size[i]
|
|
let pad = ui_pad[i]
|
|
let tw = ui_tw(font, s, size)
|
|
var tx = rx + pad
|
|
let al = ui_align[i]
|
|
if al == 1 { tx = rx + (rw - tw) / 2 }
|
|
if al == 2 { tx = rx + rw - pad - tw }
|
|
ui_draw_text(font, tx, ry + pad, s, fg, size)
|
|
}
|
|
if t == WT_IMAGE {
|
|
if ui_img[i] >= 0 { rt_draw_image_scaled(ui_img[i], rx, ry, rw, rh) }
|
|
}
|
|
for c in 0 .. ui_n {
|
|
if ui_parent[c] == i { ui_draw_node(c) }
|
|
}
|
|
}
|
|
|
|
# ---- focus and activation -------------------------------------------------
|
|
var ui_fl: words = null
|
|
|
|
fn ui_focusables() -> int {
|
|
if (ui_fl == null) { ui_fl = words(UI_MAX) }
|
|
var n = 0
|
|
for i in 0 .. ui_n {
|
|
if ui_focusable[i] == 1 {
|
|
if ui_visible[i] == 1 {
|
|
if ui_under(i, ui_active) {
|
|
ui_fl[n] = i
|
|
n = n + 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
fn rt_ui_open(root: int) -> void {
|
|
ui_active = root
|
|
let n = ui_focusables()
|
|
ui_focus = 0 - 1
|
|
if n > 0 { ui_focus = ui_fl[0] }
|
|
}
|
|
|
|
fn rt_ui_tick(k: int) -> void {
|
|
if ui_active < 0 { return }
|
|
for i in 0 .. ui_n { ui_fired[i] = 0 }
|
|
ui_layout()
|
|
let n = ui_focusables()
|
|
if n == 0 {
|
|
ui_focus = 0 - 1
|
|
return
|
|
}
|
|
var cur = 0 - 1
|
|
for i in 0 .. n {
|
|
if ui_fl[i] == ui_focus { cur = i }
|
|
}
|
|
if cur < 0 {
|
|
cur = 0
|
|
ui_focus = ui_fl[0]
|
|
}
|
|
if k == 119 { # 'w'
|
|
cur = (cur - 1 + n) % n
|
|
ui_focus = ui_fl[cur]
|
|
}
|
|
if k == 115 { # 's'
|
|
cur = (cur + 1) % n
|
|
ui_focus = ui_fl[cur]
|
|
}
|
|
if k == 32 { ui_fired[ui_focus] = 1 }
|
|
if k == 10 { ui_fired[ui_focus] = 1 }
|
|
if k == 13 { ui_fired[ui_focus] = 1 }
|
|
}
|
|
|
|
fn rt_ui_render() -> void {
|
|
if ui_active < 0 { return }
|
|
ui_layout()
|
|
ui_draw_node(ui_active)
|
|
}
|
|
|
|
# ---- game-facing accessors ------------------------------------------------
|
|
fn rt_ui_clicked(id: int) -> bool {
|
|
if id < 0 { return false }
|
|
if id >= ui_n { return false }
|
|
return ui_fired[id] == 1
|
|
}
|
|
|
|
fn rt_ui_set_text(id: int, s: str) -> void {
|
|
if id < 0 { return }
|
|
if id >= ui_n { return }
|
|
let base = id * 96
|
|
var i = 0
|
|
var ch = s[0]
|
|
while ch != 0 {
|
|
if i >= 95 { ch = 0 }
|
|
if ch != 0 {
|
|
ui_dyn[base + i] = ch
|
|
i = i + 1
|
|
ch = s[i]
|
|
}
|
|
}
|
|
ui_dyn[base + i] = 0
|
|
ui_hasdyn[id] = 1
|
|
}
|
|
|
|
fn rt_ui_set_int(id: int, n: int) -> void {
|
|
if id < 0 { return }
|
|
if id >= ui_n { return }
|
|
let base = id * 96
|
|
var at = 0
|
|
var v = n
|
|
if v < 0 {
|
|
ui_dyn[base] = 45
|
|
at = 1
|
|
v = 0 - v
|
|
}
|
|
if v == 0 {
|
|
ui_dyn[base + at] = 48
|
|
at = at + 1
|
|
} else {
|
|
var digits = 0
|
|
var t = v
|
|
while t > 0 {
|
|
digits = digits + 1
|
|
t = t / 10
|
|
}
|
|
var p = digits
|
|
while p > 0 {
|
|
var div = 1
|
|
for k in 1 .. p { div = div * 10 }
|
|
ui_dyn[base + at] = 48 + (v / div) % 10
|
|
at = at + 1
|
|
p = p - 1
|
|
}
|
|
}
|
|
ui_dyn[base + at] = 0
|
|
ui_hasdyn[id] = 1
|
|
}
|
|
|
|
fn rt_ui_focus(id: int) -> void {
|
|
if id < 0 { return }
|
|
if id >= ui_n { return }
|
|
ui_focus = id
|
|
}
|
|
|
|
fn rt_ui_focused() -> int {
|
|
return ui_focus
|
|
}
|
|
|
|
fn rt_ui_visible(id: int, v: int) -> void {
|
|
if id < 0 { return }
|
|
if id >= ui_n { return }
|
|
ui_visible[id] = v
|
|
}
|