32-bit word access is indexing now, not peek32/poke32: peek32(ui_rx, i) -> ui_rx[i] (reads an int) poke32(rt_fb, i, c) -> rt_fb[i] = c (writes an int) A buffer typed `words` (a pointer whose elements are i32) indexes with `w[i]` as a full int; a plain `ptr`/`str` keeps byte indexing. emit_index_addr picks the element type from the base's type — byte-identical IR to the old intrinsics, so the migration reproduces the compiler and every golden render exactly. The ~60 word buffers (rt_fb, tt_*/gc_* font tables, png_px/spr_px pixels, ui_* layout arrays) were retyped from `ptr` to `words` scope-aware (per-function, so the s/out/p byte-vs-word name collisions across functions stay correct), then the 254 peek32/poke32 sites migrated to indexing. A scope-analysis miss left 12 buffers (gc_*, sc_d, ui_rx/ui_ry) un-retyped — caught as a menu golden diff and fixed. No true mixed byte+word access exists on any one variable, so a per-buffer element type is sound. Reseeded (22527 lines); C-free fixpoint holds; goldens byte-identical; 18/18; vocab (byte/words types in, peek32/poke32 out) + doc-fences clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
312 lines
8.2 KiB
Text
312 lines
8.2 KiB
Text
# ============================================================================
|
|
# ui.ludic — the retained UI engine, written in Ludic.
|
|
#
|
|
# A `ui Name { … }` block in a game is DATA: the compiler flattens the widget
|
|
# tree and emits a build function that calls rt_ui_set() for each property.
|
|
# Everything after that — measure/arrange layout, 9-slice skins, text, keyboard
|
|
# focus and activation — is this file.
|
|
#
|
|
# Widgets are parallel arrays rather than a struct per widget, because Ludic
|
|
# has components for per-entity data and raw memory for everything else, and a
|
|
# widget is not an entity.
|
|
# ============================================================================
|
|
|
|
const UI_MAX: int = 256
|
|
|
|
# widget types, matching the compiler's flattener
|
|
const WT_PANEL: int = 0
|
|
const WT_COL: int = 1
|
|
const WT_ROW: int = 2
|
|
const WT_LABEL: int = 3
|
|
const WT_BUTTON: int = 4
|
|
const WT_IMAGE: int = 5
|
|
const WT_SPACER: int = 6
|
|
|
|
# property keys — the private protocol between ludicc and this runtime
|
|
const K_TYPE: int = 0
|
|
const K_PARENT: int = 1
|
|
const K_W: int = 2
|
|
const K_H: int = 3
|
|
const K_X: int = 4
|
|
const K_Y: int = 5
|
|
const K_HASPOS: int = 6
|
|
const K_PAD: int = 7
|
|
const K_GAP: int = 8
|
|
const K_BG: int = 9
|
|
const K_FG: int = 10
|
|
const K_BORDER: int = 11
|
|
const K_ALIGN: int = 12
|
|
const K_GROW: int = 13
|
|
const K_FONT: int = 14
|
|
const K_SIZE: int = 15
|
|
const K_SKININSET: int = 16
|
|
const K_FOCUSABLE: int = 17
|
|
const K_VISIBLE: int = 18
|
|
const K_IMG: int = 19
|
|
const K_SKIN: int = 20
|
|
|
|
var ui_type: words = null
|
|
var ui_parent: words = null
|
|
var ui_w: words = null
|
|
var ui_h: words = null
|
|
var ui_x: words = null
|
|
var ui_y: words = null
|
|
var ui_haspos: words = null
|
|
var ui_pad: words = null
|
|
var ui_gap: words = null
|
|
var ui_bg: words = null
|
|
var ui_fg: words = null
|
|
var ui_border: words = null
|
|
var ui_align: words = null
|
|
var ui_grow: words = null
|
|
var ui_font: words = null
|
|
var ui_size: words = null
|
|
var ui_skin: words = null
|
|
var ui_inset: words = null
|
|
var ui_img: words = null
|
|
var ui_focusable: words = null
|
|
var ui_rx: words = null
|
|
var ui_ry: words = null
|
|
var ui_rw: words = null
|
|
var ui_rh: words = null
|
|
var ui_visible: words = null
|
|
var ui_fired: words = null
|
|
var ui_hasdyn: words = null
|
|
var ui_dyn: ptr = null # UI_MAX * 96 bytes
|
|
var ui_text: ptr = null # UI_MAX static string pointers
|
|
|
|
var ui_n: int = 0
|
|
var ui_active: int = -1
|
|
var ui_focus: int = -1
|
|
|
|
fn rt_ui_init() -> void {
|
|
ui_type = mem_alloc(UI_MAX * 4)
|
|
ui_parent = mem_alloc(UI_MAX * 4)
|
|
ui_w = mem_alloc(UI_MAX * 4)
|
|
ui_h = mem_alloc(UI_MAX * 4)
|
|
ui_x = mem_alloc(UI_MAX * 4)
|
|
ui_y = mem_alloc(UI_MAX * 4)
|
|
ui_haspos = mem_alloc(UI_MAX * 4)
|
|
ui_pad = mem_alloc(UI_MAX * 4)
|
|
ui_gap = mem_alloc(UI_MAX * 4)
|
|
ui_bg = mem_alloc(UI_MAX * 4)
|
|
ui_fg = mem_alloc(UI_MAX * 4)
|
|
ui_border = mem_alloc(UI_MAX * 4)
|
|
ui_align = mem_alloc(UI_MAX * 4)
|
|
ui_grow = mem_alloc(UI_MAX * 4)
|
|
ui_font = mem_alloc(UI_MAX * 4)
|
|
ui_size = mem_alloc(UI_MAX * 4)
|
|
ui_skin = mem_alloc(UI_MAX * 4)
|
|
ui_inset = mem_alloc(UI_MAX * 4)
|
|
ui_img = mem_alloc(UI_MAX * 4)
|
|
ui_focusable = mem_alloc(UI_MAX * 4)
|
|
ui_rx = mem_alloc(UI_MAX * 4)
|
|
ui_ry = mem_alloc(UI_MAX * 4)
|
|
ui_rw = mem_alloc(UI_MAX * 4)
|
|
ui_rh = mem_alloc(UI_MAX * 4)
|
|
ui_visible = mem_alloc(UI_MAX * 4)
|
|
ui_fired = mem_alloc(UI_MAX * 4)
|
|
ui_hasdyn = mem_alloc(UI_MAX * 4)
|
|
ui_dyn = mem_alloc(UI_MAX * 96)
|
|
ui_text = mem_alloc(UI_MAX * 8)
|
|
}
|
|
|
|
# ---- build-time interface (called by compiler-emitted code) ---------------
|
|
fn rt_ui_reset(n: int) -> void {
|
|
ui_n = n
|
|
for i in 0 .. n {
|
|
ui_type[i] = 0
|
|
ui_parent[i] = 0 - 1
|
|
ui_w[i] = 0
|
|
ui_h[i] = 0
|
|
ui_x[i] = 0
|
|
ui_y[i] = 0
|
|
ui_haspos[i] = 0
|
|
ui_pad[i] = 0
|
|
ui_gap[i] = 0
|
|
ui_bg[i] = 0 - 1
|
|
ui_fg[i] = 0 - 1
|
|
ui_border[i] = 0 - 1
|
|
ui_align[i] = 0 - 1
|
|
ui_grow[i] = 0
|
|
ui_font[i] = 0 - 1
|
|
ui_size[i] = 8
|
|
ui_skin[i] = 0 - 1
|
|
ui_inset[i] = 6
|
|
ui_img[i] = 0 - 1
|
|
ui_focusable[i] = 0
|
|
ui_visible[i] = 1
|
|
ui_fired[i] = 0
|
|
ui_hasdyn[i] = 0
|
|
pokep(ui_text, i, null)
|
|
}
|
|
}
|
|
|
|
fn rt_ui_set(i: int, k: int, v: int) -> void {
|
|
if i < 0 { return }
|
|
if i >= UI_MAX { return }
|
|
if k == K_TYPE { ui_type[i] = v }
|
|
if k == K_PARENT { ui_parent[i] = v }
|
|
if k == K_W { ui_w[i] = v }
|
|
if k == K_H { ui_h[i] = v }
|
|
if k == K_X { ui_x[i] = v }
|
|
if k == K_Y { ui_y[i] = v }
|
|
if k == K_HASPOS { ui_haspos[i] = v }
|
|
if k == K_PAD { ui_pad[i] = v }
|
|
if k == K_GAP { ui_gap[i] = v }
|
|
if k == K_BG { ui_bg[i] = v }
|
|
if k == K_FG { ui_fg[i] = v }
|
|
if k == K_BORDER { ui_border[i] = v }
|
|
if k == K_ALIGN { ui_align[i] = v }
|
|
if k == K_GROW { ui_grow[i] = v }
|
|
if k == K_FONT { ui_font[i] = v }
|
|
if k == K_SIZE { ui_size[i] = v }
|
|
if k == K_SKININSET { ui_inset[i] = v }
|
|
if k == K_FOCUSABLE { ui_focusable[i] = v }
|
|
if k == K_VISIBLE { ui_visible[i] = v }
|
|
if k == K_IMG { ui_img[i] = v }
|
|
if k == K_SKIN { ui_skin[i] = v }
|
|
}
|
|
|
|
fn rt_ui_static_text(i: int, s: str) -> void {
|
|
if i < 0 { return }
|
|
if i >= UI_MAX { return }
|
|
pokep(ui_text, i, s)
|
|
}
|
|
|
|
# ---- text helpers: TrueType when a font is loaded, bitmap otherwise -------
|
|
fn ui_str(i: int) -> ptr {
|
|
if ui_hasdyn[i] == 1 { return ptr_add(ui_dyn, i * 96) }
|
|
let t = peekp(ui_text, i)
|
|
if (t == null) { return ptr_add(ui_dyn, i * 96) }
|
|
return t
|
|
}
|
|
|
|
fn ui_draw_text(font: int, x: int, y: int, s: ptr, colour: int, size: int) -> void {
|
|
if font >= 0 {
|
|
if font < tt_n {
|
|
rt_text_ttf(font, x, y, s, colour, size)
|
|
return
|
|
}
|
|
}
|
|
rt_text(x, y, s, colour, max(size / 8, 1))
|
|
}
|
|
|
|
fn ui_tw(font: int, s: ptr, size: int) -> int {
|
|
if font >= 0 {
|
|
if font < tt_n { return rt_text_w(font, s, size) }
|
|
}
|
|
var n = 0
|
|
while s[n] != 0 { n = n + 1 }
|
|
return n * 6 * max(size / 8, 1)
|
|
}
|
|
|
|
fn ui_th(font: int, size: int) -> int {
|
|
if font >= 0 {
|
|
if font < tt_n { return rt_text_h(font, size) }
|
|
}
|
|
return 7 * max(size / 8, 1)
|
|
}
|
|
|
|
fn ui_is_container(t: int) -> bool {
|
|
if t == WT_PANEL { return true }
|
|
if t == WT_COL { return true }
|
|
if t == WT_ROW { return true }
|
|
return false
|
|
}
|
|
fn ui_dir(t: int) -> int {
|
|
if t == WT_ROW { return 0 }
|
|
return 1
|
|
}
|
|
fn ui_under(i: int, root: int) -> bool {
|
|
var k = i
|
|
while k >= 0 {
|
|
if k == root { return true }
|
|
k = ui_parent[k]
|
|
}
|
|
return false
|
|
}
|
|
|
|
# ---- measure: content sizes, children before parents ----------------------
|
|
fn ui_measure() -> void {
|
|
var i = ui_n - 1
|
|
while i >= 0 {
|
|
if ui_visible[i] == 0 {
|
|
ui_rw[i] = 0
|
|
ui_rh[i] = 0
|
|
} else {
|
|
let t = ui_type[i]
|
|
let pad = ui_pad[i]
|
|
var cw = 0
|
|
var ch = 0
|
|
if t == WT_LABEL { cw = 0 - 1 }
|
|
if t == WT_BUTTON { cw = 0 - 1 }
|
|
if cw == 0 - 1 {
|
|
let s = ui_str(i)
|
|
cw = ui_tw(ui_font[i], s, ui_size[i]) + 2 * pad
|
|
ch = ui_th(ui_font[i], ui_size[i]) + 2 * pad
|
|
if t == WT_BUTTON {
|
|
cw = cw + 10
|
|
ch = ch + 6
|
|
}
|
|
} else {
|
|
if t == WT_IMAGE {
|
|
let im = ui_img[i]
|
|
if im >= 0 {
|
|
cw = img_w[im]
|
|
ch = img_h[im]
|
|
}
|
|
} else {
|
|
if t == WT_SPACER {
|
|
cw = ui_w[i]
|
|
ch = ui_h[i]
|
|
} else {
|
|
let dir = ui_dir(t)
|
|
var mainsz = 0
|
|
var cross = 0
|
|
var nc = 0
|
|
for c in 0 .. ui_n {
|
|
if ui_parent[c] == i {
|
|
if ui_visible[c] == 1 {
|
|
if dir == 1 {
|
|
mainsz = mainsz + ui_rh[c]
|
|
cross = max(cross, ui_rw[c])
|
|
} else {
|
|
mainsz = mainsz + ui_rw[c]
|
|
cross = max(cross, ui_rh[c])
|
|
}
|
|
nc = nc + 1
|
|
}
|
|
}
|
|
}
|
|
if nc > 1 { mainsz = mainsz + ui_gap[i] * (nc - 1) }
|
|
mainsz = mainsz + 2 * pad
|
|
cross = cross + 2 * pad
|
|
if dir == 1 {
|
|
cw = cross
|
|
ch = mainsz
|
|
} else {
|
|
cw = mainsz
|
|
ch = cross
|
|
}
|
|
}
|
|
}
|
|
}
|
|
var rw = ui_w[i]
|
|
var rh = ui_h[i]
|
|
if rw == 0 { rw = cw }
|
|
if rh == 0 { rh = ch }
|
|
ui_rw[i] = rw
|
|
ui_rh[i] = rh
|
|
}
|
|
i = i - 1
|
|
}
|
|
}
|
|
|
|
fn ui_eff_align(child: int, parent: int) -> int {
|
|
if ui_align[child] >= 0 { return ui_align[child] }
|
|
if ui_align[parent] >= 0 { return ui_align[parent] }
|
|
return 0
|
|
}
|
|
|
|
import "ui_draw.ludic"
|