GizStackGizStack
View .mdOpen in ChatGPTOpen in Claude

gizgame/hud

The HUD: what a game draws over its world, widget by widget.

gizlang
import * as Hud from "gizgame/hud";

Types

Hud

gizlang
class Hud { canvas: Canvas2D; width: f64; height: f64 = 720; margin: f64 = 32; leftEdge: f64; topEdge: f64; rightEdge: f64; bottomEdge: f64; scale: f64; touch: bool; private time: f64; private picture: Picture; sprites: Sprites; ring: Vec<f64>; camera: Camera | undefined = undefined; pinned: Vec<f64> = []; game: Game; constructor(picture: Picture, sprites: Sprites) throws string { const aspect = picture.width/Math.max(1, picture.height); const ratio = Math.max(0.1, picture.screen.ratio); const wide = picture.width/ratio; const tall = picture.height/ratio; let height = Math.min(720.0, tall*1.25); let width = height*aspect; const least = Math.min(640.0, wide*1.25); if (width <least) { width = least; height = width/aspect; } const scale = width/Math.max(1, wide); const margin = Math.min(32.0, Math.min(width, height)/18); const safe = picture.screen.safe; const inset = (side: i64): f64 => safe.length === 4 ? safe[side]*scale: 0.0; this.width = width; this.height = height; this.scale = scale; this.margin = margin; this.touch = picture.game.touching(); this.topEdge = margin+inset(0); this.rightEdge = width-margin-inset(1); this.bottomEdge = height-margin-inset(2); this.leftEdge = margin+inset(3); this.picture = picture; this.game = picture.game; this.time = picture.time; this.canvas = sprites.canvas(picture, width, height); this.sprites = sprites; this.ring = sprites.circle; } css(n: f64): f64 { return n*this.scale; } upright(): bool { return this.height> this.width; } press(code: string, x: f64, y: f64, width: f64, height: f64): void { this.game.targets.push({ code, x: (x+width*0.5)/this.width, y: (y+height*0.5)/this.height, halfWidth: width*0.5/this.width, halfHeight: height*0.5/this.height }); } button(code: string, x: f64, y: f64, radius: f64, icon: string, color: Vec<f64>, wait: f64 = 0.0): void throws string { const c = this.canvas; const s = this.sprites; const down = this.game.pushing(code); const r = down ? radius*0.92: radius; const idle = wait> 0; if (down) { c.light(x, y, r*4.4, [color[0], color[1], color[2], 0.3]); } c.sprite(s.icon("disc"), x, y, r*2, r*2, 0, down ? [color[0]*0.35, color[1]*0.35, color[2]*0.35, 0.72]: [0.02, 0.03, 0.06, idle ? 0.34: 0.46]); c.sprite(s.icon("ring"), x, y, r*2, r*2, 0, down ? [color[0], color[1], color[2], 0.95]: [1, 1, 1, idle ? 0.16: 0.34]); if (idle) { const lit = i64((1-Math.min(1, wait))*40); for (const k of range(0, lit)) { const a = f64(k)/40*Math.PI*2-Math.PI*0.5; c.dot(x+Math.cos(a)*r*0.95, y+Math.sin(a)*r*0.95, r*0.1, [color[0], color[1], color[2], 0.9]); } } c.sprite(s.icon(icon), x, y, r*1.12, r*1.12, 0, down ? [1, 1, 1, 1]: [color[0], color[1], color[2], idle ? 0.4: 0.95]); this.press(code, x-radius*1.12, y-radius*1.12, radius*2.24, radius*2.24); } text(text: string, x: f64, y: f64, size: f64, color: Vec<f64>, align: f64 = 0, bold: bool = false): void throws string { this.canvas.text(text, x, y, size*this.game.settings.text, color, align, bold); } dots(text: string, x: f64, y: f64, size: f64, color: Vec<f64>, align: f64 = 0): void throws string { const upper = text.toUpperCase(); if (!dotted(upper)) { this.text(text, x, y, size, color, align, true); return; } this.canvas.dots(upper, x, y+size*0.18, size/9, color, align); } say(text: string, values: Vec<string> = []): string { return this.game.say(text, values); } left(text: string, y: f64, size: f64, color: Vec<f64>, bold: bool = false): void throws string { this.text(text, this.leftEdge, y, size, color, 0, bold); } right(text: string, y: f64, size: f64, color: Vec<f64>, bold: bool = false): void throws string { this.text(text, this.rightEdge, y, size, color, 1, bold); } center(text: string, y: f64, size: f64, color: Vec<f64>, bold: bool = false): void throws string { this.text(text, this.width*0.5, y, size, color, 0.5, bold); } measure(text: string, size: f64, bold: bool = false): f64 { return this.canvas.measure(text, size*this.game.settings.text, bold); } fit(text: string, size: f64, wide: f64, bold: bool = false): f64 { return Math.min(size, size*wide/Math.max(1, this.measure(text, size, bold))); } rows(text: string, size: f64, width: f64, bold: bool = false): Vec<string> { let out: Vec<string> = []; let row = ""; for (const word of text.split(" ")) { if (word === "") { continue; } const joined = row === "" ? word: `${row } ${ word }`; if (this.measure(joined, size, bold) <= width) { row = joined; continue; } if (row !== "") { out.push(row); row = ""; } for (const g of Unicode.graphemes(word)) { if (row !== "" && this.measure(`${row }${ g }`, size, bold)> width) { out.push(row); row = ""; } row+ = g; } } if (row !== "") { out.push(row); } return out; } pulse(color: Vec<f64>, rate: f64 = 3): Vec<f64> { return [color[0], color[1], color[2], color[3]*(0.6+0.4*Math.sin(this.time*rate))]; } faded(color: Vec<f64>, fade: f64): Vec<f64> { return [color[0], color[1], color[2], color[3]*Math.max(0, Math.min(1, fade))]; } bar(x: f64, y: f64, width: f64, height: f64, fill: f64, color: Vec<f64>, back: Vec<f64> = [0, 0, 0, 0.5]): void { const f = Math.max(0, Math.min(1, fill)); const left = x-width*0.5; this.card(left, y-height*0.5, width, height, back, height*0.5); if (f> 0) { this.card(left, y-height*0.5, Math.max(height, width*f), height, color, height*0.5); } } card(x: f64, y: f64, width: f64, height: f64, color: Vec<f64>, radius: f64 = 14): void { const r = Math.max(0, Math.min(radius, Math.min(width, height)*0.5)); const uv = this.sprites.round; const k = ROUND_CORNER/f64(ROUND_SIDE); const xs = [x, x+r, x+width-r, x+width]; const ys = [y, y+r, y+height-r, y+height]; const ts = [0, k, 1-k, 1]; for (const i of range(0, 3)) { for (const j of range(0, 3)) { const w = xs[i+1]-xs[i]; const h = ys[j+1]-ys[j]; if (w <= 0 || h <= 0) { continue; } this.canvas.sprite([uv[0]+uv[2]*ts[i], uv[1]+uv[3]*ts[j], uv[2]*(ts[i+1]-ts[i]), uv[3]*(ts[j+1]-ts[j])], xs[i]+w*0.5, ys[j]+h*0.5, w, h, 0, color); } } } chip(text: string, x: f64, y: f64, size: f64, color: Vec<f64>, back: Vec<f64> = [1, 1, 1, 0.12], bold: bool = false): f64 throws string { const width = this.measure(text, size, bold)+size*1.1; this.card(x, y, width, size*1.75, back, size*0.5); this.text(text, x+size*0.55, y+size*0.3, size, color, 0, bold); return width; } box(x: f64, y: f64, width: f64, height: f64, color: Vec<f64>): void { this.canvas.sprite(this.sprites.solid, x+width*0.5, y+height*0.5, width, height, 0, color); } over(camera: Camera, at: Point3): Point2 | undefined throws string { const p = transformed(view(camera, this.picture.screen.width, this.picture.screen.height), at); if (!(p.z> = 0 && p.z <= 1 && Math.abs(p.x) <= 1 && Math.abs(p.y) <= 1)) { return undefined; } return { x: (p.x+1)*0.5*this.width, y: (1-p.y)*0.5*this.height }; } marker(uv: Vec<f64>, camera: Camera, at: Point3, size: f64, color: Vec<f64>): void throws string { const p = this.over(camera, at); if (p !== undefined) { this.canvas.sprite(uv, p.x, p.y, size, size, 0, color, 1); } } draw(): void throws string { this.sprites.draw(this.picture, this.canvas); } }

The heads-up display over a picture. On a desktop its unit keeps the height at 720 and the width at least 640, at the surface's aspect; on a small screen (a phone) a unit is 0.8 CSS pixels, so text stays readable and a finger's target stays large. A text's size is its em in those units. CANVAS is open for other drawing.

  • canvas: gizlang_draw.Canvas2D
  • width: f64
  • height: f64
  • margin: f64 — The margin of text at the edges.
  • leftEdge: f64 — The edges the display stays inside: the margin in from the surface's sides and from what a notch or a round corner covers (Screen safe).
  • topEdge: f64
  • rightEdge: f64
  • bottomEdge: f64
  • scale: f64 — The units in a CSS pixel: what a finger presses is sized in CSS pixels (css).
  • touch: bool — Whether a finger plays (Game.touching): the widgets then lay out for thumbs, and name no key.
  • time: f64
  • picture: Ref<M213.Picture>
  • sprites: Ref<M228.Sprites> — The sprites it draws with, a ring of them (a marker, a life), and the camera of the world under it, if any.
  • ring: Vec<f64>
  • camera: _gizlang_sdk_core.Option<gizlang_meshdraw.Camera>
  • pinned: Vec<f64> — The card that stays at the top over the world (the quest's: its left, top, width and height), or none: what floats over a being keeps out of it.
  • game: Ref<M201.Game> — The game drawn.

Widget

gizlang
type Widget = (hud: Hud) => void throws string

One thing of the HUD: it reads the game (and the world under it: Hud.camera) and draws itself.

Functions

constructor

gizlang
constructor(picture: Picture, sprites: Sprites) throws string

A display over PICTURE that draws with the brush of SPRITES (draw).

Effects: fail, read, write, alloc.

Methods

Hud.css

gizlang
css(n: f64): f64

N CSS pixels in the display's units: a finger's target is the same size on every screen.

Effects: fail, read.

Hud.upright

gizlang
upright(): bool

Whether the display is taller than wide: a phone held upright.

Effects: fail, read.

Hud.press

gizlang
press(code: string, x: f64, y: f64, width: f64, height: f64): void

A pointer on the box WIDTH by HEIGHT with its top left at X, Y presses the key CODE (Game.targets).

Effects: fail, read, write, alloc.

Hud.button

gizlang
button(code: string, x: f64, y: f64, radius: f64, icon: string, color: Vec<f64>, wait: f64 = 0.0): void throws string

A round button of RADIUS at X, Y that a pointer presses as the key CODE: ICON (Sprites.icon) in COLOR on dark glass in a thin ring, lit while it is pressed. WAIT (1 to 0) is the share of a cooldown left: it dims the button, and the ring winds back as it ends.

Effects: fail, read, write, console, io, alloc.

Hud.text

gizlang
text(text: string, x: f64, y: f64, size: f64, color: Vec<f64>, align: f64 = 0, bold: bool = false): void throws string

TEXT with its line's top at X, Y, SIZE units high (its em) times the person's text size (Settings text), in COLOR, its left (ALIGN 0), middle (0.5) or right (1) at X; BOLD in the bold face.

Effects: fail, read, write, console, io, alloc.

Hud.dots

gizlang
dots(text: string, x: f64, y: f64, size: f64, color: Vec<f64>, align: f64 = 0): void throws string

TEXT as text places it, in the glowing dot-matrix font of an arcade (Canvas2D.dots) when it has every character of it, upper case; in the text's own faces when it has not (a translation into Hangul).

Effects: fail, read, write, console, io, alloc.

Hud.say

gizlang
say(text: string, values: Vec<string> = []): string

TEXT in the game's language (Game.say).

Effects: fail, read, alloc.

Hud.left

gizlang
left(text: string, y: f64, size: f64, color: Vec<f64>, bold: bool = false): void throws string

TEXT at the left margin.

Effects: fail, read, write, console, io, alloc.

Hud.right

gizlang
right(text: string, y: f64, size: f64, color: Vec<f64>, bold: bool = false): void throws string

TEXT at the right margin.

Effects: fail, read, write, console, io, alloc.

Hud.center

gizlang
center(text: string, y: f64, size: f64, color: Vec<f64>, bold: bool = false): void throws string

TEXT in the middle.

Effects: fail, read, write, console, io, alloc.

Hud.measure

gizlang
measure(text: string, size: f64, bold: bool = false): f64

The width of TEXT at SIZE.

Effects: fail, read, write, console, io, alloc.

Hud.fit

gizlang
fit(text: string, size: f64, wide: f64, bold: bool = false): f64

The size, SIZE at most, at which TEXT is no wider than WIDE.

Effects: fail, read, write, console, io, alloc.

Hud.rows

gizlang
rows(text: string, size: f64, width: f64, bold: bool = false): Vec<string>

TEXT broken into rows no wider than WIDTH at SIZE: between words, and inside a word that is wider alone.

Effects: fail, read, write, console, io, alloc.

Hud.pulse

gizlang
pulse(color: Vec<f64>, rate: f64 = 3): Vec<f64>

COLOR with its alpha pulsing at RATE, as a prompt to press a key blinks.

Effects: fail, read, alloc.

Hud.faded

gizlang
faded(color: Vec<f64>, fade: f64): Vec<f64>

COLOR at FADE of its alpha.

Effects: fail, alloc.

Hud.bar

gizlang
bar(x: f64, y: f64, width: f64, height: f64, fill: f64, color: Vec<f64>, back: Vec<f64> = [0, 0, 0, 0.5]): void

A bar WIDTH by HEIGHT with round ends centered at X, Y, filled to FILL (0 to 1) from the left in COLOR over BACK.

Effects: fail, read, write, alloc.

Hud.card

gizlang
card(x: f64, y: f64, width: f64, height: f64, color: Vec<f64>, radius: f64 = 14): void

A box in COLOR with round corners of RADIUS, its top left at X, Y, WIDTH by HEIGHT: a card, a pill or a chip.

Effects: fail, read, write, alloc.

Hud.chip

gizlang
chip(text: string, x: f64, y: f64, size: f64, color: Vec<f64>, back: Vec<f64> = [1, 1, 1, 0.12], bold: bool = false): f64 throws string

TEXT of SIZE on a rounded chip in BACK, its left at X and its top at Y: its width.

Effects: fail, read, write, console, io, alloc.

Hud.box

gizlang
box(x: f64, y: f64, width: f64, height: f64, color: Vec<f64>): void

A solid box in COLOR with its top left at X, Y, WIDTH by HEIGHT: the back of a panel.

Effects: fail, read, write, alloc.

Hud.over

gizlang
over(camera: Camera, at: Point3): Point2 | undefined throws string

Where the world point AT shows under CAMERA, in the HUD's units; undefined behind the camera or off screen.

Effects: fail, read, alloc.

Hud.marker

gizlang
marker(uv: Vec<f64>, camera: Camera, at: Point3, size: f64, color: Vec<f64>): void throws string

Sprite UV of SIZE in COLOR over the world point AT under CAMERA, when it is in view: a marker on a goal.

Effects: fail, read, write, alloc.

Hud.draw

gizlang
draw(): void throws string

The HUD drawn with SPRITES over the picture.

Effects: fail, read, write, console, io, alloc.