ludic/packages/ludic.ui/render3d.ludic
Orkuncakilkaya 40a91f39ed fix(ui): the pointer in the renderer's pixels - macOS reports it in window points, so on Retina every hit landed at half the cursor's position
The old kit read Input.mouse_x() * gl_scale; ludic.ui read it raw against a layout sized in
drawable pixels. A backend says how many screen pixels one pointer unit is (pointer_scale);
render3d's is gl_pixel_scale(), 1 on Windows and headless.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 23:31:47 +03:00

82 lines
4.9 KiB
Text

# render3d.ludic - ludic.ui drawn with ludic.render3d's overlay. A program that draws with render3d
# imports this, and its templates draw in the overlay pass (ui_render3d() installs it again), sized in
# design pixels of a 1080-line screen (ui_render3d_scale sets the player's own interface size).
# <img src="path/to/picture.png"> a texture, loaded once
# <img src="icon:tent"> a cell of an atlas the program named with ui_atlas
# border-image: url(button.png) 14 a nine-slice background
module ludic_ui
import "render3d_image.ludic"
import "render3d_nine.ludic"
# importing this file is enough: it is the renderer from the program's first frame
def UiRenderers render3d { make: fn r3u_make }
export function ui_render3d(ui_st: mut UiState) -> void { ui_backend(ui_st, r3u_make()) }
function r3u_make() -> UiBackend {
let b = new UiBackend
b.rect = fn r3u_rect
b.round = fn r3u_round
b.ring = fn r3u_ring
b.text = fn r3u_text
b.measure = fn r3u_measure
b.image = fn r3u_image
b.image_w = fn r3u_image_w
b.image_h = fn r3u_image_h
b.nine = fn r3u_nine
b.clip = fn r3u_clip
b.unclip = fn r3u_unclip
b.scale = fn r3u_scale
b.pointer_scale = fn r3u_pointer_scale
b.now = fn r3u_now
return b
}
# the player's interface size, 1.0 as designed
export function ui_render3d_scale(ui_st: mut UiState, s: float) -> void { ui_st.r3u_user = s }
# a grid atlas: `names` are its cells, row by row, `cols` across and `rows` down; <img
# src="prefix:name"> draws one (by its name, or by its number: "item:12")
export function ui_atlas(ui_st: mut UiState, prefix: string, tex: int, cols: int, rows: int, names: []string) -> void {
push(ui_st.r3u_atlas, prefix)
push(ui_st.r3u_atlas_tex, tex)
push(ui_st.r3u_atlas_cols, cols)
push(ui_st.r3u_atlas_rows, Math.max(rows, 1))
push(ui_st.r3u_atlas_names, names)
}
function r3u_now() -> float { return float(Time.now_us() / 1000) / 1000.0 }
function r3u_scale(ui_st: UiState) -> float { return float(gl_height()) / 1080.0 * ui_st.r3u_user }
function r3u_pointer_scale() -> float { return float(gl_pixel_scale()) }
function r3u_r(c: int) -> float { return float((c / 65536) % 256) / 255.0 }
function r3u_g(c: int) -> float { return float((c / 256) % 256) / 255.0 }
function r3u_b(c: int) -> float { return float(c % 256) / 255.0 }
function r3u_rect(render3d_st: mut Render3dState, x: float, y: float, w: float, h: float, c: int, a: float) -> void { ov_rect(render3d_st, int(x), int(y), int(w), int(h), r3u_r(c), r3u_g(c), r3u_b(c), a) }
# a rounded rectangle: a cross of two rects and a disc in each corner
function r3u_round(render3d_st: mut Render3dState, x: float, y: float, w: float, h: float, rad0: float, c: int, a: float) -> void {
let rad = Math.min(rad0, Math.min(w, h) / 2.0)
let r = r3u_r(c)
let g = r3u_g(c)
let b = r3u_b(c)
ov_rect(render3d_st, int(x + rad), int(y), int(w - rad * 2.0), int(h), r, g, b, a)
ov_rect(render3d_st, int(x), int(y + rad), int(rad), int(h - rad * 2.0), r, g, b, a)
ov_rect(render3d_st, int(x + w - rad), int(y + rad), int(rad), int(h - rad * 2.0), r, g, b, a)
ov_disc(render3d_st, x + rad, y + rad, rad, 12, r, g, b, a)
ov_disc(render3d_st, x + w - rad, y + rad, rad, 12, r, g, b, a)
ov_disc(render3d_st, x + rad, y + h - rad, rad, 12, r, g, b, a)
ov_disc(render3d_st, x + w - rad, y + h - rad, rad, 12, r, g, b, a)
}
# a rounded frame: four straight edges and a quarter arc at each corner
function r3u_ring(render3d_st: mut Render3dState, x: float, y: float, w: float, h: float, rad0: float, t: float, c: int, a: float) -> void {
let rad = Math.min(rad0, Math.min(w, h) / 2.0)
let r = r3u_r(c)
let g = r3u_g(c)
let b = r3u_b(c)
ov_rect(render3d_st, int(x + rad), int(y), int(w - rad * 2.0), int(t), r, g, b, a)
ov_rect(render3d_st, int(x + rad), int(y + h - t), int(w - rad * 2.0), int(t), r, g, b, a)
ov_rect(render3d_st, int(x), int(y + rad), int(t), int(h - rad * 2.0), r, g, b, a)
ov_rect(render3d_st, int(x + w - t), int(y + rad), int(t), int(h - rad * 2.0), r, g, b, a)
let q = 1.5707964
ov_arc(render3d_st, x + rad, y + rad, rad - t / 2.0, t, q * 2.0, q, 8, r, g, b, a)
ov_arc(render3d_st, x + w - rad, y + rad, rad - t / 2.0, t, q * 3.0, q, 8, r, g, b, a)
ov_arc(render3d_st, x + w - rad, y + h - rad, rad - t / 2.0, t, 0.0, q, 8, r, g, b, a)
ov_arc(render3d_st, x + rad, y + h - rad, rad - t / 2.0, t, q, q, 8, r, g, b, a)
}
function r3u_text(render3d_st: mut Render3dState, x: float, y: float, s: string, size: float, c: int, a: float) -> void { ov_text(render3d_st, int(x), int(y), int(size), s, r3u_r(c), r3u_g(c), r3u_b(c), a) }
function r3u_measure(render3d_st: mut Render3dState, s: string, size: float) -> float { return float(ov_text_w(render3d_st, int(size), s)) }
function r3u_clip(render3d_st: mut Render3dState, x: float, y: float, w: float, h: float) -> void { ov_clip(render3d_st, int(x), int(y), int(w), int(h)) }
function r3u_unclip(render3d_st: mut Render3dState) -> void { ov_unclip(render3d_st) }