import {clamp01, pad} from './util.js'; /** * Instrument-style HUD: header with stage/command/clock, annotations over the * viewport, a terminal log and a side panel (minimap + stats) at the bottom. * Everything is derived from `t` and the config's stages, so it's deterministic. * * cfg.stages: [{at, name, cmd, logs: [string]}]. A log line starting with "!" * is highlighted in the accent. {key} tokens are replaced from `facts`. */ export function createHud(root, cfg) { const h = cfg.hud; root.insertAdjacentHTML( 'beforeend', `
`, ); const $ = (s) => root.querySelector(s); const el = { hud: $('.hud'), ann: $('.ann'), stage: $('.row2 .stage'), cmd: $('.row2 .cmd'), time: $('.row2 .time'), prog: $('.prog i'), prompt: $('.prompt'), logs: $('.logs'), tfoot: $('.tfoot'), status: $('.thead .status span'), stats: $('.stats'), sfoot: $('.sfoot'), base: $('.baseline i'), mini: $('.mini'), }; $('.title .t').textContent = h.title; $('.sub').textContent = h.subtitle; $('.right').textContent = h.right; $('.wm').textContent = cfg.watermark; $('.thead .host').textContent = h.host; $('.thead .path').textContent = h.path; $('.shead').textContent = h.sideTitle; const stages = cfg.stages; const D = cfg.duration; const TYPE_CMD = 0.028; const TYPE_LOG = 0.011; // Log line schedule: each stage types its command, then its lines spread // over the stage. const lines = []; stages.forEach((s, i) => { const end = i + 1 < stages.length ? stages[i + 1].at : D; const t0 = s.at + 0.25 + s.cmd.length * TYPE_CMD + 0.2; const n = s.logs.length; const gap = Math.min(0.8, Math.max(0.22, (end - t0 - 0.4) / Math.max(1, n))); s.logs.forEach((text, j) => lines.push({at: t0 + j * gap, text, stage: i})); }); const stageAt = (t) => { let i = 0; while (i + 1 < stages.length && stages[i + 1].at <= t) i++; return i; }; const fill = (text, facts) => text.replace(/\{(\w+)\}/g, (m, k) => (k in facts ? facts[k] : m)); const esc = (s) => s.replace(/&/g, '&').replace(/> ${esc(cmd.slice(0, typed))}${ caretOn ? '' : '' }`; const shown = lines.filter((l) => l.at <= t); const rows = shown.slice(-h.logRows); const offset = shown.length - rows.length; const html = rows .map((l, j) => { const full = fill(l.text, facts); const hl = full.startsWith('!'); const text = hl ? full.slice(1) : full; const n = Math.floor((t - l.at) / TYPE_LOG); const isLast = j === rows.length - 1 && n < text.length; return `
${pad(offset + j, 3)}${esc( text.slice(0, n), )}${isLast ? '' : ''}
`; }) .join(''); if (html !== lastLogs) { el.logs.innerHTML = html; lastLogs = html; } el.tfoot.textContent = `frame ${pad(frame, 4)} delta ${pad((dt * 7.3) % 100, 2)} / ${s.name.toLowerCase()}`; el.status.textContent = info.status ?? (t >= D - 0.05 ? 'DONE' : 'RUNNING'); el.sfoot.textContent = `view ${pad(si + 1, 2)} / ${pad(h.views, 2)} seed ${cfg.seed} ยท t+${dt.toFixed(2)}s`; const stats = (info.stats || []).map((x) => `
${esc(x.label)}${esc(String(x.value))}
`).join(''); if (stats !== lastStats) { el.stats.innerHTML = stats; lastStats = stats; } const anns = info.annotations || []; while (annPool.length < anns.length) { const d = document.createElement('div'); el.ann.appendChild(d); annPool.push(d); } annPool.forEach((d, i) => { const a = anns[i]; if (!a) { d.style.display = 'none'; return; } d.style.display = ''; d.className = a.cls || ''; d.style.left = `${a.x}px`; d.style.top = `${a.y}px`; d.style.opacity = a.opacity ?? 1; if (a.html !== undefined) d.innerHTML = a.html; else d.textContent = a.text; // keep labels inside the viewport (between header and bottom panel) const vp = this.viewport(); const w = d.offsetWidth; const hh = d.offsetHeight; const x = Math.min(Math.max(a.x, 8), innerWidth - w - 40); const y = Math.min(Math.max(a.y, vp.top + hh / 2 + 8), innerHeight - vp.bottom - hh / 2 - 8); d.style.left = `${x}px`; d.style.top = `${y}px`; }); }, }; }