ludic/packages/ludic.ui/frame_box.ludic

44 lines
1.4 KiB
Text

# frame_box.ludic - what a host asks of the interface from outside it: the scale it is drawn at, and
# where an element was laid out the last time its screen was shown (a 3D view framed beside a panel)
export property UiBox {
x: float = 0.0
y: float = 0.0
w: float = 0.0
h: float = 0.0
}
# the screen pixels per design pixel: the renderer's scale (render3d's includes ui_render3d_scale)
export function ui_scale(ui_st: UiState) -> float { return ui_px(ui_st, 1.0) }
function fb_keep(ui_st: mut UiState, screen: string, root: UiNode) -> void {
for i in 0 .. len(ui_st.fb_roots) {
if ui_st.fb_roots[i].key == screen {
ui_st.fb_roots[i] = root
return
}
}
ui_kept(ui_st.fb_roots, root)
}
# the box of the element with that id (or key) on any screen shown, in screen pixels; null if none
@alloc_ok("a question the game asks now and then (an anchor, a screen's box), never per frame")
export function ui_box(ui_st: UiState, id: string) -> UiBox {
for i in 0 .. len(ui_st.fb_roots) {
let n = fb_find(ui_st.fb_roots[i], id)
if n != null {
let b = new UiBox
b.x = n.x
b.y = n.y
b.w = n.cw
b.h = n.ch
return b
}
}
return null
}
function fb_find(n: UiNode, id: string) -> UiNode {
if n.id == id or n.key == id { return n }
if n.children == null { return null }
for i in 0 .. len(n.children) {
let f = fb_find(n.children[i], id)
if f != null { return f }
}
return null
}