# 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). # a texture, loaded once # 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; 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) }