@johnoscott/isometric-engine 0.14.1 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/iso-editor.js +249 -203
- package/iso-render.js +200 -154
- package/iso-systems.js +210 -162
- package/isometric-diagram.js +389 -0
- package/js/iso-core/index.js +5 -0
- package/js/iso-core/mono.js +242 -0
- package/js/iso-editor/ui/canvas.js +23 -1
- package/js/iso-objects/ants.js +36 -0
- package/js/iso-objects/index.js +55 -1
- package/js/iso-objects/looks.js +25 -0
- package/js/iso-objects/mono-paint.js +96 -0
- package/js/iso-objects/motion-builder.js +130 -0
- package/js/iso-objects/motion-doc.js +421 -0
- package/js/iso-objects/motion-entry.js +34 -0
- package/js/iso-objects/motion-schema.js +83 -0
- package/js/iso-objects/motion.js +640 -0
- package/js/iso-objects/resolve.js +238 -0
- package/js/iso-objects/scene.js +15 -13
- package/js/iso-objects/timeline.js +593 -0
- package/js/iso-systems/compile.js +83 -1
- package/js/iso-systems/index.js +4 -1
- package/js/iso-systems/render.js +20 -0
- package/js/iso-systems/schema.json +40 -0
- package/js/iso-viewer/index.js +6 -0
- package/js/iso-viewer/page.js +40 -16
- package/js/iso-viewer/play.js +340 -0
- package/js/iso-viewer/player.js +149 -175
- package/js/iso-viewer/transport.js +289 -0
- package/package.json +84 -2
- package/src/iso-core/index.ts +1 -0
- package/src/iso-core/mono.ts +306 -0
- package/src/iso-core/theme.ts +2 -1
- package/src/iso-editor/ui/canvas.ts +14 -1
- package/src/iso-objects/ants.ts +53 -0
- package/src/iso-objects/compile.ts +4 -1
- package/src/iso-objects/index.ts +16 -1
- package/src/iso-objects/looks.ts +21 -0
- package/src/iso-objects/mono-paint.ts +97 -0
- package/src/iso-objects/motion-builder.ts +79 -0
- package/src/iso-objects/motion-doc.ts +309 -0
- package/src/iso-objects/motion-entry.ts +13 -0
- package/src/iso-objects/motion-schema.ts +55 -0
- package/src/iso-objects/motion.ts +617 -0
- package/src/iso-objects/resolve.ts +196 -0
- package/src/iso-objects/scene.ts +32 -13
- package/src/iso-objects/timeline.ts +493 -0
- package/src/iso-objects/types.ts +22 -2
- package/src/iso-systems/compile.ts +86 -4
- package/src/iso-systems/index.ts +4 -1
- package/src/iso-systems/render.ts +45 -0
- package/src/iso-systems/schema.json +40 -0
- package/src/iso-viewer/index.ts +4 -0
- package/src/iso-viewer/page.ts +28 -11
- package/src/iso-viewer/play.ts +280 -0
- package/src/iso-viewer/player.ts +132 -150
- package/src/iso-viewer/transport.ts +272 -0
- package/types/iso-core/index.d.ts +1 -0
- package/types/iso-core/mono.d.ts +61 -0
- package/types/iso-core/theme.d.ts +2 -0
- package/types/iso-objects/ants.d.ts +34 -0
- package/types/iso-objects/compile.d.ts +3 -0
- package/types/iso-objects/index.d.ts +16 -1
- package/types/iso-objects/looks.d.ts +4 -0
- package/types/iso-objects/mono-paint.d.ts +20 -0
- package/types/iso-objects/motion-builder.d.ts +105 -0
- package/types/iso-objects/motion-doc.d.ts +193 -0
- package/types/iso-objects/motion-entry.d.ts +11 -0
- package/types/iso-objects/motion-schema.d.ts +594 -0
- package/types/iso-objects/motion.d.ts +202 -0
- package/types/iso-objects/resolve.d.ts +20 -0
- package/types/iso-objects/scene.d.ts +14 -0
- package/types/iso-objects/timeline.d.ts +66 -0
- package/types/iso-objects/types.d.ts +34 -0
- package/types/iso-systems/compile.d.ts +25 -3
- package/types/iso-systems/index.d.ts +4 -1
- package/types/iso-systems/render.d.ts +25 -0
- package/types/iso-viewer/index.d.ts +4 -0
- package/types/iso-viewer/play.d.ts +37 -0
- package/types/iso-viewer/transport.d.ts +73 -0
- package/types/player/isometric-diagram.d.ts +24 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// A self-contained drawing: the renderer's SVG with its stylesheets worked out and written on the elements, as plain
|
|
2
|
+
// presentation attributes (no <style>, no custom properties, no hsl() or color-mix()), so it looks the same with no
|
|
3
|
+
// page around it and in a rasteriser that knows no CSS. Pure: the stylesheets are the engine's own (the theme's, the
|
|
4
|
+
// drawing's, a host's marks'), read by a small reader that knows what they use and throws at what it doesn't.
|
|
5
|
+
|
|
6
|
+
import type { Theme } from './types.ts';
|
|
7
|
+
import { themeParts } from '../iso-core/theme.ts';
|
|
8
|
+
|
|
9
|
+
export type ThemeMode = 'light' | 'dark';
|
|
10
|
+
|
|
11
|
+
interface Part { tag?: string; classes: string[]; not: string[]; attrs: [string, string | undefined][]; child: boolean }
|
|
12
|
+
interface Rule { parts: Part[]; spec: number; order: number; decls: [string, string, boolean][] }
|
|
13
|
+
interface El { name: string; attrs: [string, string][]; children: (El | string)[]; parent?: El }
|
|
14
|
+
|
|
15
|
+
/** what a declaration becomes: the attribute of its name */
|
|
16
|
+
const WRITTEN = new Set(['fill', 'stroke', 'stroke-width', 'stroke-dasharray', 'stroke-linecap', 'stroke-linejoin', 'opacity', 'fill-opacity', 'stroke-opacity', 'font-size', 'font-weight', 'font-family',
|
|
17
|
+
'letter-spacing', 'text-anchor', 'dominant-baseline', 'paint-order', 'display', 'stop-color', 'stop-opacity']);
|
|
18
|
+
/** what a still has no use for */
|
|
19
|
+
const LEFT = new Set('cursor pointer-events transition animation transform-box transform-origin user-select'.split(' '));
|
|
20
|
+
|
|
21
|
+
function readPart(s: string, child: boolean): Part {
|
|
22
|
+
const p: Part = { classes: [], not: [], attrs: [], child };
|
|
23
|
+
let rest = s;
|
|
24
|
+
const eat = (re: RegExp) => { const m = re.exec(rest); if (m) rest = rest.slice(m[0].length); return m; };
|
|
25
|
+
const t = eat(/^[a-zA-Z][\w-]*/);
|
|
26
|
+
if (t) p.tag = t[0]; else eat(/^\*/);
|
|
27
|
+
while (rest) {
|
|
28
|
+
let m: RegExpExecArray | null;
|
|
29
|
+
if ((m = eat(/^\.([\w-]+)/))) p.classes.push(m[1]);
|
|
30
|
+
else if ((m = eat(/^:not\(\.([\w-]+)\)/))) p.not.push(m[1]);
|
|
31
|
+
else if ((m = eat(/^\[([\w-]+)(?:="([^"]*)")?\]/))) p.attrs.push([m[1], m[2]]);
|
|
32
|
+
else throw new Error(`resolve: a selector it doesn't know: ${s}`);
|
|
33
|
+
}
|
|
34
|
+
return p;
|
|
35
|
+
}
|
|
36
|
+
function readCss(css: string): Rule[] {
|
|
37
|
+
const text = css.replace(/\/\*[\s\S]*?\*\//g, '').replace(/@keyframes[^{]*\{(?:[^{}]*\{[^}]*\})*[^{}]*\}/g, '').replace(/@media[^{]*\{(?:[^{}]*\{[^}]*\})*[^{}]*\}/g, '');
|
|
38
|
+
if (/@/.test(text.replace(/"[^"]*"/g, ''))) throw new Error('resolve: an at-rule it doesn\'t know');
|
|
39
|
+
const rules: Rule[] = [];
|
|
40
|
+
for (const m of text.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
|
|
41
|
+
const decls = m[2].split(';').map(d => d.trim()).filter(Boolean).map(d => {
|
|
42
|
+
const i = d.indexOf(':'), k = d.slice(0, i).trim(), v = d.slice(i + 1).trim();
|
|
43
|
+
return [k, v.replace(/\s*!important$/, ''), /!important$/.test(v)] as [string, string, boolean];
|
|
44
|
+
});
|
|
45
|
+
for (const d of decls) if (!d[0].startsWith('--') && !WRITTEN.has(d[0]) && !LEFT.has(d[0])) throw new Error(`resolve: a property it doesn't know: ${d[0]}`);
|
|
46
|
+
for (const sel of m[1].split(',').map(x => x.trim()).filter(Boolean)) {
|
|
47
|
+
if (sel.includes(':root') || sel.includes('::') || /^(html|body)\b/.test(sel)) continue;
|
|
48
|
+
const toks = sel.replace(/\s*>\s*/g, ' >').split(/\s+/);
|
|
49
|
+
const parts = toks.map(x => readPart(x.replace(/^>/, ''), x.startsWith('>')));
|
|
50
|
+
const spec = parts.reduce((n, p) => n + 10 * (p.classes.length + p.not.length + p.attrs.length) + (p.tag ? 1 : 0), 0);
|
|
51
|
+
rules.push({ parts, spec, order: rules.length, decls });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return rules;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function read(svg: string): El {
|
|
58
|
+
const root: El = { name: '#root', attrs: [], children: [] };
|
|
59
|
+
let top = root;
|
|
60
|
+
for (const m of svg.matchAll(/<\/[a-zA-Z][\w:-]*>|<([a-zA-Z][\w:-]*)((?:\s+[\w:-]+="[^"]*")*)\s*(\/?)>|([^<]+)/g)) {
|
|
61
|
+
if (m[4] !== undefined) { top.children.push(m[4]); continue; }
|
|
62
|
+
if (m[1] === undefined) { top = top.parent!; continue; }
|
|
63
|
+
const el: El = { name: m[1], attrs: [...m[2].matchAll(/([\w:-]+)="([^"]*)"/g)].map(a => [a[1], a[2]]), children: [], parent: top };
|
|
64
|
+
top.children.push(el);
|
|
65
|
+
if (!m[3]) top = el;
|
|
66
|
+
}
|
|
67
|
+
return root.children[0] as El;
|
|
68
|
+
}
|
|
69
|
+
const write = (e: El | string): string => (typeof e === 'string' ? e : `<${e.name}${e.attrs.map(([k, v]) => ` ${k}="${v}"`).join('')}${e.children.length || e.name === 'g' ? `>${e.children.map(write).join('')}</${e.name}>` : '/>'}`);
|
|
70
|
+
const get = (e: El, k: string) => e.attrs.find(a => a[0] === k)?.[1];
|
|
71
|
+
const put = (e: El, k: string, v: string) => { const a = e.attrs.find(x => x[0] === k); if (a) a[1] = v; else e.attrs.push([k, v]); };
|
|
72
|
+
const classesOf = (e: El) => (get(e, 'class') ?? '').split(/\s+/).filter(Boolean);
|
|
73
|
+
|
|
74
|
+
function fits(e: El, p: Part): boolean {
|
|
75
|
+
if (p.tag && p.tag !== e.name) return false;
|
|
76
|
+
const c = classesOf(e);
|
|
77
|
+
return p.classes.every(x => c.includes(x)) && !p.not.some(x => c.includes(x)) && p.attrs.every(([k, v]) => { const g = get(e, k); return g !== undefined && (v === undefined || g === v); });
|
|
78
|
+
}
|
|
79
|
+
function matches(e: El, parts: Part[], i = parts.length - 1): boolean {
|
|
80
|
+
if (!fits(e, parts[i])) return false;
|
|
81
|
+
if (i === 0) return true;
|
|
82
|
+
let up = e.parent;
|
|
83
|
+
if (parts[i].child) return !!up && up.name !== '#root' && matches(up, parts, i - 1);
|
|
84
|
+
for (; up && up.name !== '#root'; up = up.parent) if (matches(up, parts, i - 1)) return true;
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ---------- colours ----------
|
|
89
|
+
|
|
90
|
+
type Rgba = [number, number, number, number];
|
|
91
|
+
const num = (s: string) => (s.endsWith('%') ? parseFloat(s) / 100 : parseFloat(s));
|
|
92
|
+
function hsl(h: number, s: number, l: number, a: number): Rgba {
|
|
93
|
+
const c = (1 - Math.abs(2 * l - 1)) * s, hp = ((h % 360) + 360) % 360 / 60, x = c * (1 - Math.abs(hp % 2 - 1)), m = l - c / 2;
|
|
94
|
+
const [r, g, b] = hp < 1 ? [c, x, 0] : hp < 2 ? [x, c, 0] : hp < 3 ? [0, c, x] : hp < 4 ? [0, x, c] : hp < 5 ? [x, 0, c] : [c, 0, x];
|
|
95
|
+
return [(r + m) * 255, (g + m) * 255, (b + m) * 255, a];
|
|
96
|
+
}
|
|
97
|
+
/** the parts of a list at its top level */
|
|
98
|
+
function split(s: string, sep: string): string[] {
|
|
99
|
+
const out: string[] = []; let depth = 0, cur = '';
|
|
100
|
+
for (const ch of s) { if (ch === '(') depth++; if (ch === ')') depth--; if (ch === sep && depth === 0) { out.push(cur.trim()); cur = ''; } else cur += ch; }
|
|
101
|
+
return [...out, cur.trim()].filter(Boolean);
|
|
102
|
+
}
|
|
103
|
+
const NAMED: Record<string, Rgba> = { transparent: [0, 0, 0, 0], black: [0, 0, 0, 1], white: [255, 255, 255, 1] };
|
|
104
|
+
function colour(v: string): Rgba | undefined {
|
|
105
|
+
const s = v.trim();
|
|
106
|
+
if (Object.hasOwn(NAMED, s)) return NAMED[s];
|
|
107
|
+
let m = /^#([0-9a-f]{3,8})$/i.exec(s);
|
|
108
|
+
if (m) {
|
|
109
|
+
const x = m[1].length < 6 ? [...m[1]].map(c => c + c).join('') : m[1];
|
|
110
|
+
return [parseInt(x.slice(0, 2), 16), parseInt(x.slice(2, 4), 16), parseInt(x.slice(4, 6), 16), x.length === 8 ? parseInt(x.slice(6), 16) / 255 : 1];
|
|
111
|
+
}
|
|
112
|
+
m = /^hsla?\((.*)\)$/.exec(s);
|
|
113
|
+
if (m) {
|
|
114
|
+
const [c, a] = m[1].split('/').map(x => x.trim()), [h, sat, l, a2] = c.split(/[\s,]+/);
|
|
115
|
+
return hsl(parseFloat(h), num(sat), num(l), a !== undefined ? num(a) : a2 !== undefined ? num(a2) : 1);
|
|
116
|
+
}
|
|
117
|
+
m = /^rgba?\((.*)\)$/.exec(s);
|
|
118
|
+
if (m) { const [r, g, b, a] = m[1].split(/[\s,/]+/).filter(Boolean); return [num(r), num(g), num(b), a === undefined ? 1 : num(a)]; }
|
|
119
|
+
m = /^color-mix\(in srgb\s*,(.*)\)$/.exec(s);
|
|
120
|
+
if (m) {
|
|
121
|
+
const [x, y] = split(m[1], ',').map(p => { const q = /^(.*?)\s+([\d.]+)%$/.exec(p); return q ? { c: colour(q[1]), p: Number(q[2]) / 100 } : { c: colour(p), p: undefined as number | undefined }; });
|
|
122
|
+
if (!x?.c || !y?.c) return undefined;
|
|
123
|
+
const px = x.p ?? (y.p === undefined ? 0.5 : 1 - y.p), py = y.p ?? 1 - px, sum = px + py, a = (x.c[3] * px + y.c[3] * py) / sum;
|
|
124
|
+
const mix = (i: 0 | 1 | 2) => (a === 0 ? 0 : (x.c![i] * x.c![3] * px + y.c![i] * y.c![3] * py) / sum / a);
|
|
125
|
+
return [mix(0), mix(1), mix(2), a * Math.min(1, sum)];
|
|
126
|
+
}
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
const hex = (c: Rgba) => '#' + [c[0], c[1], c[2]].map(v => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, '0')).join('');
|
|
130
|
+
const short = (v: number) => String(Math.round(v * 10000) / 10000);
|
|
131
|
+
|
|
132
|
+
/** custom properties put in, the nearest first */
|
|
133
|
+
function vars(v: string, env: Map<string, string>, seen = 0): string {
|
|
134
|
+
if (seen > 20) throw new Error(`resolve: a custom property in a loop: ${v}`);
|
|
135
|
+
const i = v.indexOf('var(');
|
|
136
|
+
if (i < 0) return v;
|
|
137
|
+
let depth = 0, j = i + 3;
|
|
138
|
+
do { if (v[j] === '(') depth++; if (v[j] === ')') depth--; j++; } while (depth > 0 && j < v.length);
|
|
139
|
+
const [name, ...fall] = split(v.slice(i + 4, j - 1), ',');
|
|
140
|
+
const val = env.get(name) ?? (fall.length ? fall.join(',') : undefined);
|
|
141
|
+
if (val === undefined) throw new Error(`resolve: no value for ${name}`);
|
|
142
|
+
return vars(v.slice(0, i) + val + v.slice(j), env, seen + 1);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const tokens = (decls: string) => new Map(decls.split(';').filter(Boolean).map(d => { const i = d.indexOf(':'); return [d.slice(0, i).trim(), d.slice(i + 1).trim()] as [string, string]; }));
|
|
146
|
+
|
|
147
|
+
export interface ResolveOptions {
|
|
148
|
+
mode: ThemeMode;
|
|
149
|
+
theme: Theme;
|
|
150
|
+
/** the drawing's stylesheets, in order, after the theme's */
|
|
151
|
+
css: string;
|
|
152
|
+
/** the fonts the texts ask for (a stack each) */
|
|
153
|
+
fonts?: { sans: string; mono: string };
|
|
154
|
+
}
|
|
155
|
+
export const FONT_STACKS = { sans: 'system-ui, -apple-system, Segoe UI, Roboto, sans-serif', mono: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace' };
|
|
156
|
+
|
|
157
|
+
/** The drawing, self-contained, in one of its theme's modes. What its stylesheets hide is left out of the paint (display
|
|
158
|
+
* none); ids, classes and data- attributes stay. */
|
|
159
|
+
export function resolveSvg(svg: string, o: ResolveOptions): string {
|
|
160
|
+
const t = themeParts(o.theme), fonts = o.fonts ?? FONT_STACKS;
|
|
161
|
+
const rules = readCss(`.mono{font-family:${fonts.mono.replace(/"/g, '')}}\n${t.rest}\n${o.css}`);
|
|
162
|
+
const root = read(svg);
|
|
163
|
+
put(root, 'font-family', fonts.sans.replace(/"/g, ''));
|
|
164
|
+
const walk = (e: El, env0: Map<string, string>) => {
|
|
165
|
+
let env = env0;
|
|
166
|
+
// the winning declaration of each property: !important, then specificity, then order; a style attribute over all but !important
|
|
167
|
+
const won = new Map<string, { v: string; rank: [number, number, number] }>();
|
|
168
|
+
const offer = (k: string, v: string, rank: [number, number, number]) => { const w = won.get(k); if (!w || rank[0] > w.rank[0] || (rank[0] === w.rank[0] && (rank[1] > w.rank[1] || (rank[1] === w.rank[1] && rank[2] >= w.rank[2])))) won.set(k, { v, rank }); };
|
|
169
|
+
for (const r of rules) if (matches(e, r.parts)) for (const [k, v, imp] of r.decls) offer(k, v, [imp ? 1 : 0, r.spec, r.order]);
|
|
170
|
+
const style = get(e, 'style');
|
|
171
|
+
if (style) {
|
|
172
|
+
for (const d of style.split(';').map(x => x.trim()).filter(Boolean)) { const i = d.indexOf(':'); offer(d.slice(0, i).trim(), d.slice(i + 1).trim(), [0, 1e6, 0]); }
|
|
173
|
+
e.attrs = e.attrs.filter(a => a[0] !== 'style');
|
|
174
|
+
}
|
|
175
|
+
for (const [k, w] of won) if (k.startsWith('--')) { if (env === env0) env = new Map(env0); env.set(k, w.v); }
|
|
176
|
+
const alpha: Record<string, number> = {};
|
|
177
|
+
for (const [k, w] of won) {
|
|
178
|
+
if (!WRITTEN.has(k)) continue;
|
|
179
|
+
const v = vars(w.v, env).trim();
|
|
180
|
+
if (k === 'fill' || k === 'stroke' || k === 'stop-color') {
|
|
181
|
+
if (v === 'none' || v === 'currentColor' || v === 'inherit' || v.startsWith('url(')) { put(e, k, v); continue; }
|
|
182
|
+
const c = colour(v);
|
|
183
|
+
if (!c) throw new Error(`resolve: a colour it doesn't know: ${v}`);
|
|
184
|
+
put(e, k, hex(c));
|
|
185
|
+
if (c[3] !== 1) alpha[k === 'stop-color' ? 'stop' : k] = c[3];
|
|
186
|
+
} else put(e, k, k === 'letter-spacing' || k === 'font-size' || k === 'stroke-width' ? v : v.replace(/"/g, ''));
|
|
187
|
+
}
|
|
188
|
+
for (const [k, a] of Object.entries(alpha)) put(e, `${k}-opacity`, short(a * Number(get(e, `${k}-opacity`) ?? 1)));
|
|
189
|
+
if (get(e, 'opacity') === '0') { put(e, 'display', 'none'); e.attrs = e.attrs.filter(a => a[0] !== 'opacity'); }
|
|
190
|
+
// a presentation attribute as the renderer wrote it (a marker's fill) may name a token too
|
|
191
|
+
for (const a of e.attrs) if (/var\(|hsl\(|color-mix/.test(a[1])) { const v = vars(a[1], env), c = colour(v); a[1] = c ? hex(c) : v; }
|
|
192
|
+
for (const c of e.children) if (typeof c !== 'string') walk(c, env);
|
|
193
|
+
};
|
|
194
|
+
walk(root, tokens(o.mode === 'dark' ? `${t.light};${t.dark}` : t.light));
|
|
195
|
+
return write(root);
|
|
196
|
+
}
|
package/src/iso-objects/scene.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// One scene → one SVG string. Pure: no DOM, so it runs the same in Node and in the browser.
|
|
2
2
|
|
|
3
|
+
import type { MonoLook } from '../iso-core/mono.ts';
|
|
3
4
|
import { type Attrs, type Box, type P2, Kernel, FONTS, S, tag, iso, pt, n1, esc, textWidth } from '../iso-core/kernel.ts';
|
|
4
5
|
import { drawCloud } from './assets.ts';
|
|
5
6
|
import { type KindCatalog, regionKind, nodeKindOr } from './kinds.ts';
|
|
@@ -25,6 +26,15 @@ export interface RenderedScene {
|
|
|
25
26
|
paths: Record<string, string>;
|
|
26
27
|
/** things drawn worse than asked, e.g. a route label shortened or dropped */
|
|
27
28
|
warnings: RenderWarning[];
|
|
29
|
+
/** (0.15.0) a self-contained drawing's own colours, for what a state adds to it (a token's rim and fill) */
|
|
30
|
+
inks?: { surface: string; control: string; kinds?: Record<string, string> };
|
|
31
|
+
/** (0.16.0) its black and white table, when it is in that treatment */
|
|
32
|
+
mono?: MonoLook;
|
|
33
|
+
/** the drawing with every thing that has a look of this name in it, as SVG text (what a switch takes the thing's parts from);
|
|
34
|
+
* none when nothing declares one */
|
|
35
|
+
variants?: Record<string, string>;
|
|
36
|
+
/** the looks each thing declares */
|
|
37
|
+
looks?: Record<string, string[]>;
|
|
28
38
|
/** the assemble intro's timeline */
|
|
29
39
|
assembly: Assembly;
|
|
30
40
|
/** callout pills in screen px, per step, for layout tests */
|
|
@@ -34,6 +44,14 @@ export interface RenderWarning { path: string; message: string }
|
|
|
34
44
|
|
|
35
45
|
interface Item { box: Box; svg: string }
|
|
36
46
|
|
|
47
|
+
/** (0.15.0) A thing's parts in a layer, in a wrapper of their own: `<g data-item=ID data-part=…>` (block, wall-1…, gate,
|
|
48
|
+
* name, label, note), a route's `<g data-route=ID data-part=…>` (line, with its arrowhead's marker; label). A wrapper has
|
|
49
|
+
* no transform and no class: a host (and motion.ts) moves and fades wrappers, never what is inside them, whose texts
|
|
50
|
+
* have transforms, and whose classes have opacities, of their own. One wrapper a part and a layer, not one a thing: its
|
|
51
|
+
* block, walls and label are painted in different layers, among other things' (its data-id stays on every part of it;
|
|
52
|
+
* data-item and data-part together are one wrapper's). */
|
|
53
|
+
const wrap = (id: string, part: string, svg: string, what: 'item' | 'route' = 'item') => (svg ? tag('g', { [`data-${what}`]: id, 'data-part': part }, svg) : '');
|
|
54
|
+
|
|
37
55
|
export interface SceneInput { scene: RawScene; layout: Layout; routes: Routed[]; steps: Steps; theme: Theme; id: string; gridStep?: number }
|
|
38
56
|
|
|
39
57
|
export function renderScene({ scene, layout, routes, steps, theme, id, gridStep = 1 }: SceneInput): RenderedScene {
|
|
@@ -53,20 +71,20 @@ export function renderScene({ scene, layout, routes, steps, theme, id, gridStep
|
|
|
53
71
|
for (const r of ordered) L.base += drawFlatRegion(k, cat, r, visAttr(r.id), gridStep);
|
|
54
72
|
for (const r of regions.filter(r => shapeOf(cat, r) === 'walled')) {
|
|
55
73
|
for (const w of wallsOf(r)) obs.solids.push(silhouette(w));
|
|
56
|
-
for (const w of wallsOf(r)) L.blocks.push({ box: w, svg: tag('g', { class: `blk k-${regionRole(cat, r.region!)} wall${r.region!.ghost ? ' ghost' : ''}`, 'data-id': r.id, 'data-dim': '1', ...visAttr(r.id) }, k.blockFaces(w)) });
|
|
74
|
+
for (const [i, w] of wallsOf(r).entries()) L.blocks.push({ box: w, svg: wrap(r.id, `wall-${i + 1}`, tag('g', { class: `blk k-${regionRole(cat, r.region!)} wall${r.region!.ghost ? ' ghost' : ''}`, 'data-id': r.id, 'data-dim': '1', ...visAttr(r.id) }, k.blockFaces(w))) });
|
|
57
75
|
const g = r.region!.gate;
|
|
58
76
|
if (g && !g.node) {
|
|
59
77
|
const b = gateBox(r, g.height ?? (r.region!.height ?? 0.8) + 0.3, 0.3, 0.3);
|
|
60
78
|
obs.solids.push(silhouette(b));
|
|
61
|
-
L.blocks.push({ box: b, svg: tag('g', { class: 'blk k-gate', 'data-id': r.id, ...visAttr(r.id) }, k.blockFaces(b)) });
|
|
79
|
+
L.blocks.push({ box: b, svg: wrap(r.id, 'gate', tag('g', { class: 'blk k-gate', 'data-id': r.id, ...visAttr(r.id) }, k.blockFaces(b))) });
|
|
62
80
|
}
|
|
63
81
|
if (r.region!.label && (r.region!['label-style'] ?? 'face') !== 'none') {
|
|
64
|
-
L.labels += k.faceText(r.box.x0 + 0.3, r.box.y1, r.box.z0 + 0.13, r.region!.label, 'slabname sm fz', FONTS.sm, 'left', 'start', { 'data-id': r.id, ...visAttr(r.id) });
|
|
82
|
+
L.labels += wrap(r.id, 'name', k.faceText(r.box.x0 + 0.3, r.box.y1, r.box.z0 + 0.13, r.region!.label, 'slabname sm fz', FONTS.sm, 'left', 'start', { 'data-id': r.id, ...visAttr(r.id) }));
|
|
65
83
|
}
|
|
66
84
|
}
|
|
67
85
|
|
|
68
86
|
// ---- tunnels: translucent tubes in the air layer ----
|
|
69
|
-
for (const r of regions.filter(r => shapeOf(cat, r) === 'tunnel')) L.air += drawTunnel(k, r, visAttr(r.id));
|
|
87
|
+
for (const r of regions.filter(r => shapeOf(cat, r) === 'tunnel')) L.air += wrap(r.id, 'block', drawTunnel(k, r, visAttr(r.id)));
|
|
70
88
|
|
|
71
89
|
// ---- nodes ----
|
|
72
90
|
const nodes = layout.items.filter(p => p.type === 'node');
|
|
@@ -74,7 +92,7 @@ export function renderScene({ scene, layout, routes, steps, theme, id, gridStep
|
|
|
74
92
|
const n = p.node!, a = nodeKindOr(cat, n.asset);
|
|
75
93
|
const ghost = n.ghost || (n.deco ?? []).includes('ghost');
|
|
76
94
|
const cls = ['blk', `k-${n.role ?? a.role}`, a.cls, ghost ? 'ghost' : ''].filter(Boolean).join(' ');
|
|
77
|
-
L.blocks.push({ box: p.box, svg: tag('g', { class: cls, 'data-id': p.id, 'data-dim': '1', ...visAttr(p.id) }, a.draw(k, p.box, n)) });
|
|
95
|
+
L.blocks.push({ box: p.box, svg: wrap(p.id, 'block', tag('g', { class: cls, 'data-id': p.id, 'data-dim': '1', ...visAttr(p.id) }, a.draw(k, p.box, n))) });
|
|
78
96
|
obs.polys.push({ poly: silhouette(p.box), owner: p.id });
|
|
79
97
|
obs.solids.push(silhouette(p.box));
|
|
80
98
|
}
|
|
@@ -91,8 +109,8 @@ export function renderScene({ scene, layout, routes, steps, theme, id, gridStep
|
|
|
91
109
|
paths[r.id] = d;
|
|
92
110
|
const arrow = r.raw.arrow !== false;
|
|
93
111
|
if (arrow) markers.add(r.kind);
|
|
94
|
-
L.flows += tag('g', { 'data-hl': r.id, 'data-id': r.id, ...visAttr(r.id) },
|
|
95
|
-
tag('path', { d, class: `flow f-${r.kind}`, 'marker-end': arrow ? `url(#ah-${id}-${r.kind})` : undefined }));
|
|
112
|
+
L.flows += wrap(r.id, 'line', tag('g', { 'data-hl': r.id, 'data-id': r.id, ...visAttr(r.id) },
|
|
113
|
+
tag('path', { d, class: `flow f-${r.kind}`, 'marker-end': arrow ? `url(#ah-${id}-${r.kind})` : undefined })), 'route');
|
|
96
114
|
obs.lines.push({ pts: scr, owner: r.id });
|
|
97
115
|
}
|
|
98
116
|
for (const kd of markers) {
|
|
@@ -105,13 +123,13 @@ export function renderScene({ scene, layout, routes, steps, theme, id, gridStep
|
|
|
105
123
|
// ---- node labels: biggest first, each clear of nodes, routes and the labels before it ----
|
|
106
124
|
const labelled = nodes.filter(p => p.node!.label || p.node!.sub)
|
|
107
125
|
.sort((a, b) => (b.box.x1 - b.box.x0) * (b.box.y1 - b.box.y0) - (a.box.x1 - a.box.x0) * (a.box.y1 - a.box.y0));
|
|
108
|
-
for (const p of labelled) L.labels += nodeLabel(k, cat, p, obs, visAttr(p.id));
|
|
126
|
+
for (const p of labelled) L.labels += wrap(p.id, 'label', nodeLabel(k, cat, p, obs, visAttr(p.id)));
|
|
109
127
|
|
|
110
128
|
// ---- route labels and notes ----
|
|
111
129
|
const warnings: RenderWarning[] = [];
|
|
112
130
|
for (const r of routes) if (r.raw.label && r.points.length >= 2) {
|
|
113
131
|
const out = routeLabel(k, r, obs, visAttr(r.id), layout);
|
|
114
|
-
L.floor += out.svg;
|
|
132
|
+
L.floor += wrap(r.id, 'label', out.svg, 'route');
|
|
115
133
|
if (out.warning) warnings.push({ path: `${r.path}/label`, message: out.warning });
|
|
116
134
|
if (obs.budget.over && !obs.budget.told) {
|
|
117
135
|
obs.budget.told = true;
|
|
@@ -119,7 +137,7 @@ export function renderScene({ scene, layout, routes, steps, theme, id, gridStep
|
|
|
119
137
|
}
|
|
120
138
|
}
|
|
121
139
|
(scene.notes ?? []).forEach((note, i) => {
|
|
122
|
-
const s = drawNote(k, note, layout, routes, `${id}-note-${i}`);
|
|
140
|
+
const s = wrap(`${id}-note-${i}`, 'note', drawNote(k, note, layout, routes, `${id}-note-${i}`));
|
|
123
141
|
if (note.floor) L.floor += s; else L.labels += s;
|
|
124
142
|
});
|
|
125
143
|
|
|
@@ -180,6 +198,7 @@ function drawFlatRegion(k: Kernel, cat: KindCatalog, r: Placed, vis: Attrs, grid
|
|
|
180
198
|
if (shape === 'cloud') s = tag('g', { class: `blk k-${role} cloud`, ...attrs }, drawCloud(k, b));
|
|
181
199
|
else if (shape === 'zone') s = tag('g', { class: `zone k-${role}`, ...attrs }, k.poly([[b.x0, b.y0, b.z1], [b.x1, b.y0, b.z1], [b.x1, b.y1, b.z1], [b.x0, b.y1, b.z1]]));
|
|
182
200
|
else s = tag('g', { class: `blk k-${role}${R.ghost ? ' ghost' : ''}`, ...attrs }, k.blockFaces(b) + gridLines(b, gridStep));
|
|
201
|
+
s = wrap(r.id, 'block', s);
|
|
183
202
|
const style = R['label-style'] ?? rk?.labelStyle;
|
|
184
203
|
if (R.label && style !== 'none') {
|
|
185
204
|
if (style === 'face') {
|
|
@@ -187,13 +206,13 @@ function drawFlatRegion(k: Kernel, cat: KindCatalog, r: Placed, vis: Attrs, grid
|
|
|
187
206
|
const h = b.z1 - b.z0, cap = (f: { size: number }) => (f.size * 0.73) / S, thin = shape === 'slab' && 0.28 + cap(FONTS.slabname) + 0.08 > h;
|
|
188
207
|
const big = shape === 'slab' && !thin;
|
|
189
208
|
const z = big ? b.z0 + 0.28 : thin ? b.z0 + Math.max(0.02, (h - cap(FONTS.sm)) / 2) : b.z0 + Math.min(0.1, h * 0.25);
|
|
190
|
-
s += tag('g', attrs, k.faceText(b.x0 + (big ? 0.6 : 0.3), b.y1, z, R.label, big ? 'slabname' : 'slabname sm', big ? FONTS.slabname : FONTS.sm, 'left', 'start'));
|
|
209
|
+
s += wrap(r.id, 'name', tag('g', attrs, k.faceText(b.x0 + (big ? 0.6 : 0.3), b.y1, z, R.label, big ? 'slabname' : 'slabname sm', big ? FONTS.slabname : FONTS.sm, 'left', 'start')));
|
|
191
210
|
} else if (style === 'face-right') {
|
|
192
|
-
s += tag('g', attrs, k.faceText(b.x1, b.y0 + 0.6, b.z0 + Math.max(0.14, (b.z1 - b.z0) * 0.2), R.label, 'slabname', FONTS.slabname, 'right', 'end'));
|
|
211
|
+
s += wrap(r.id, 'name', tag('g', attrs, k.faceText(b.x1, b.y0 + 0.6, b.z0 + Math.max(0.14, (b.z1 - b.z0) * 0.2), R.label, 'slabname', FONTS.slabname, 'right', 'end')));
|
|
193
212
|
} else {
|
|
194
213
|
const m = shape === 'cloud' ? 'middle' : 'start';
|
|
195
214
|
const x = shape === 'cloud' ? (b.x0 + b.x1) / 2 : b.x0 + 0.35, y = shape === 'cloud' ? (b.y0 + b.y1) / 2 : b.y1 - 0.35;
|
|
196
|
-
s += tag('g', attrs, k.floorText(x, y, b.z1 + 0.01, R.label, 'slabname sm', 'x', m));
|
|
215
|
+
s += wrap(r.id, 'name', tag('g', attrs, k.floorText(x, y, b.z1 + 0.01, R.label, 'slabname sm', 'x', m)));
|
|
197
216
|
}
|
|
198
217
|
}
|
|
199
218
|
return s;
|