@johnoscott/isometric-engine 0.0.0-stage → 0.9.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/EDITOR-GUIDE.md +317 -0
- package/LICENSE +21 -0
- package/LICENSES/ajv.txt +22 -0
- package/LICENSES/lucide-static.txt +43 -0
- package/LICENSES/yaml.txt +13 -0
- package/README.md +62 -2
- package/assets/app.css +265 -0
- package/assets/default-theme.yaml +99 -0
- package/assets/icons.svg +1 -0
- package/assets/marks.css +35 -0
- package/assets/skin.css +181 -0
- package/iso-editor.d.ts +4 -0
- package/iso-editor.js +550 -0
- package/iso-render.d.ts +3 -0
- package/iso-render.js +388 -0
- package/iso-systems.d.ts +4 -0
- package/iso-systems.js +389 -0
- package/js/iso-core/compile-schema.js +22 -0
- package/js/iso-core/depth.js +55 -0
- package/js/iso-core/errors.js +65 -0
- package/js/iso-core/glyphs.js +100 -0
- package/js/iso-core/index.js +55 -0
- package/js/iso-core/kernel.js +158 -0
- package/js/iso-core/metrics.json +1 -0
- package/js/iso-core/package.json +6 -0
- package/js/iso-core/parse.js +79 -0
- package/js/iso-core/schema-errors.js +170 -0
- package/js/iso-core/shims/crypto.js +125 -0
- package/js/iso-core/shims/path.js +49 -0
- package/js/iso-core/svg-dom.js +144 -0
- package/js/iso-core/theme.js +48 -0
- package/js/iso-core/vdom.js +48 -0
- package/js/iso-editor/adapter.js +0 -0
- package/js/iso-editor/clipboard.js +152 -0
- package/js/iso-editor/commands.js +173 -0
- package/js/iso-editor/complete.js +55 -0
- package/js/iso-editor/doc.js +182 -0
- package/js/iso-editor/index.js +105 -0
- package/js/iso-editor/model.js +210 -0
- package/js/iso-editor/outline.js +111 -0
- package/js/iso-editor/package.json +6 -0
- package/js/iso-editor/path.js +19 -0
- package/js/iso-editor/providers.js +96 -0
- package/js/iso-editor/schema-nav.js +117 -0
- package/js/iso-editor/splice.js +441 -0
- package/js/iso-editor/storage-keys.js +6 -0
- package/js/iso-editor/ui/actions.js +116 -0
- package/js/iso-editor/ui/canvas-layer.js +0 -0
- package/js/iso-editor/ui/canvas.js +1205 -0
- package/js/iso-editor/ui/clip.js +133 -0
- package/js/iso-editor/ui/dialog.js +26 -0
- package/js/iso-editor/ui/flat-canvas.js +54 -0
- package/js/iso-editor/ui/icons.js +15 -0
- package/js/iso-editor/ui/keys.js +26 -0
- package/js/iso-editor/ui/later.js +28 -0
- package/js/iso-editor/ui/layer.js +11 -0
- package/js/iso-editor/ui/menu.js +105 -0
- package/js/iso-editor/ui/monaco-entry.js +2 -0
- package/js/iso-editor/ui/mount-kit.js +0 -0
- package/js/iso-editor/ui/mount.js +767 -0
- package/js/iso-editor/ui/pane.js +0 -0
- package/js/iso-editor/ui/part.js +0 -0
- package/js/iso-editor/ui/plain-icons.js +34 -0
- package/js/iso-editor/ui/props.js +314 -0
- package/js/iso-editor/ui/scope-css.js +81 -0
- package/js/iso-editor/ui/setup.js +145 -0
- package/js/iso-editor/ui/text.js +323 -0
- package/js/iso-editor/ui/tree.js +728 -0
- package/js/iso-editor/validate.js +33 -0
- package/js/iso-editor/window/protocol.js +8 -0
- package/js/iso-objects/assemble.js +43 -0
- package/js/iso-objects/assets.js +88 -0
- package/js/iso-objects/board.js +162 -0
- package/js/iso-objects/compile.js +60 -0
- package/js/iso-objects/css.js +154 -0
- package/js/iso-objects/flat.js +696 -0
- package/js/iso-objects/focus.js +16 -0
- package/js/iso-objects/geometry.js +76 -0
- package/js/iso-objects/index.js +93 -0
- package/js/iso-objects/kinds.js +12 -0
- package/js/iso-objects/labels.js +325 -0
- package/js/iso-objects/package.json +6 -0
- package/js/iso-objects/place.js +347 -0
- package/js/iso-objects/route.js +461 -0
- package/js/iso-objects/scene.js +350 -0
- package/js/iso-objects/steps.js +246 -0
- package/js/iso-objects/types.js +0 -0
- package/js/iso-systems/catalog.js +42 -0
- package/js/iso-systems/compile.js +34 -0
- package/js/iso-systems/css.js +8 -0
- package/js/iso-systems/index.js +16 -0
- package/js/iso-systems/nodes.js +124 -0
- package/js/iso-systems/package.json +6 -0
- package/js/iso-systems/schema.json +311 -0
- package/js/iso-systems/validate.js +35 -0
- package/js/iso-viewer/bundle.js +20 -0
- package/js/iso-viewer/card-motion.js +228 -0
- package/js/iso-viewer/dom.js +14 -0
- package/js/iso-viewer/dual-view.js +212 -0
- package/js/iso-viewer/flat-view.js +124 -0
- package/js/iso-viewer/history.js +990 -0
- package/js/iso-viewer/index.js +98 -0
- package/js/iso-viewer/morph.js +192 -0
- package/js/iso-viewer/overlay.js +199 -0
- package/js/iso-viewer/package.json +6 -0
- package/js/iso-viewer/page.js +138 -0
- package/js/iso-viewer/player.js +409 -0
- package/js/iso-viewer/still.js +81 -0
- package/js/iso-viewer/timeline-contract.js +14 -0
- package/js/iso-viewer/timeline-engine.js +475 -0
- package/js/iso-viewer/timeline-export.js +36 -0
- package/js/iso-viewer/timeline-scale.js +116 -0
- package/js/iso-viewer/timeline-scope.js +58 -0
- package/js/iso-viewer/timeline-search.js +216 -0
- package/js/iso-viewer/timeline-time.js +55 -0
- package/js/iso-viewer/view-css.js +68 -0
- package/js/iso-viewer/viewport.js +177 -0
- package/package.json +502 -4
- package/src/iso-core/compile-schema.ts +23 -0
- package/src/iso-core/depth.ts +56 -0
- package/src/iso-core/errors.ts +73 -0
- package/src/iso-core/glyphs.ts +108 -0
- package/src/iso-core/index.ts +27 -0
- package/src/iso-core/kernel.ts +163 -0
- package/src/iso-core/metrics.json +1 -0
- package/src/iso-core/package.json +6 -0
- package/src/iso-core/parse.ts +91 -0
- package/src/iso-core/schema-errors.ts +163 -0
- package/src/iso-core/shims/crypto.ts +37 -0
- package/src/iso-core/shims/path.ts +24 -0
- package/src/iso-core/svg-dom.ts +103 -0
- package/src/iso-core/theme.ts +64 -0
- package/src/iso-core/vdom.ts +41 -0
- package/src/iso-editor/adapter.ts +128 -0
- package/src/iso-editor/clipboard.ts +163 -0
- package/src/iso-editor/commands.ts +191 -0
- package/src/iso-editor/complete.ts +73 -0
- package/src/iso-editor/doc.ts +207 -0
- package/src/iso-editor/index.ts +67 -0
- package/src/iso-editor/model.ts +234 -0
- package/src/iso-editor/outline.ts +126 -0
- package/src/iso-editor/package.json +6 -0
- package/src/iso-editor/path.ts +18 -0
- package/src/iso-editor/providers.ts +113 -0
- package/src/iso-editor/schema-nav.ts +129 -0
- package/src/iso-editor/splice.ts +472 -0
- package/src/iso-editor/storage-keys.ts +6 -0
- package/src/iso-editor/ui/actions.ts +119 -0
- package/src/iso-editor/ui/app.css +230 -0
- package/src/iso-editor/ui/canvas-layer.ts +56 -0
- package/src/iso-editor/ui/canvas.ts +1071 -0
- package/src/iso-editor/ui/clip.ts +97 -0
- package/src/iso-editor/ui/dialog.ts +25 -0
- package/src/iso-editor/ui/flat-canvas.ts +51 -0
- package/src/iso-editor/ui/icons.ts +21 -0
- package/src/iso-editor/ui/keys.ts +30 -0
- package/src/iso-editor/ui/later.ts +22 -0
- package/src/iso-editor/ui/layer.ts +7 -0
- package/src/iso-editor/ui/marks.css +35 -0
- package/src/iso-editor/ui/menu.ts +67 -0
- package/src/iso-editor/ui/monaco-entry.ts +6 -0
- package/src/iso-editor/ui/mount-kit.ts +35 -0
- package/src/iso-editor/ui/mount.ts +684 -0
- package/src/iso-editor/ui/pane.ts +45 -0
- package/src/iso-editor/ui/part.ts +45 -0
- package/src/iso-editor/ui/plain-icons.ts +83 -0
- package/src/iso-editor/ui/props.ts +347 -0
- package/src/iso-editor/ui/scope-css.ts +65 -0
- package/src/iso-editor/ui/setup.ts +144 -0
- package/src/iso-editor/ui/skin.css +181 -0
- package/src/iso-editor/ui/text.ts +262 -0
- package/src/iso-editor/ui/tree.ts +657 -0
- package/src/iso-editor/validate.ts +62 -0
- package/src/iso-editor/window/protocol.ts +33 -0
- package/src/iso-objects/assemble.ts +57 -0
- package/src/iso-objects/assets.ts +122 -0
- package/src/iso-objects/board.ts +169 -0
- package/src/iso-objects/compile.ts +76 -0
- package/src/iso-objects/css.ts +163 -0
- package/src/iso-objects/flat.ts +653 -0
- package/src/iso-objects/focus.ts +14 -0
- package/src/iso-objects/geometry.ts +86 -0
- package/src/iso-objects/index.ts +33 -0
- package/src/iso-objects/kinds.ts +56 -0
- package/src/iso-objects/labels.ts +310 -0
- package/src/iso-objects/package.json +6 -0
- package/src/iso-objects/place.ts +318 -0
- package/src/iso-objects/route.ts +444 -0
- package/src/iso-objects/scene.ts +376 -0
- package/src/iso-objects/steps.ts +234 -0
- package/src/iso-objects/types.ts +68 -0
- package/src/iso-systems/catalog.ts +31 -0
- package/src/iso-systems/compile.ts +39 -0
- package/src/iso-systems/css.ts +9 -0
- package/src/iso-systems/index.ts +12 -0
- package/src/iso-systems/nodes.ts +126 -0
- package/src/iso-systems/package.json +6 -0
- package/src/iso-systems/schema.json +311 -0
- package/src/iso-systems/validate.ts +34 -0
- package/src/iso-viewer/bundle.ts +15 -0
- package/src/iso-viewer/card-motion.ts +237 -0
- package/src/iso-viewer/dom.ts +13 -0
- package/src/iso-viewer/dual-view.ts +212 -0
- package/src/iso-viewer/flat-view.ts +127 -0
- package/src/iso-viewer/history.ts +781 -0
- package/src/iso-viewer/index.ts +40 -0
- package/src/iso-viewer/morph.ts +191 -0
- package/src/iso-viewer/overlay.ts +212 -0
- package/src/iso-viewer/package.json +6 -0
- package/src/iso-viewer/page.ts +150 -0
- package/src/iso-viewer/player.ts +321 -0
- package/src/iso-viewer/still.ts +74 -0
- package/src/iso-viewer/timeline-contract.ts +342 -0
- package/src/iso-viewer/timeline-engine.ts +420 -0
- package/src/iso-viewer/timeline-export.ts +31 -0
- package/src/iso-viewer/timeline-scale.ts +129 -0
- package/src/iso-viewer/timeline-scope.ts +56 -0
- package/src/iso-viewer/timeline-search.ts +198 -0
- package/src/iso-viewer/timeline-time.ts +54 -0
- package/src/iso-viewer/view-css.ts +59 -0
- package/src/iso-viewer/viewport.ts +142 -0
- package/types/iso-core/compile-schema.d.ts +11 -0
- package/types/iso-core/depth.d.ts +6 -0
- package/types/iso-core/errors.d.ts +24 -0
- package/types/iso-core/glyphs.d.ts +19 -0
- package/types/iso-core/index.d.ts +17 -0
- package/types/iso-core/kernel.d.ts +130 -0
- package/types/iso-core/metrics.json +1 -0
- package/types/iso-core/parse.d.ts +18 -0
- package/types/iso-core/schema-errors.d.ts +14 -0
- package/types/iso-core/shims/crypto.d.ts +9 -0
- package/types/iso-core/svg-dom.d.ts +15 -0
- package/types/iso-core/theme.d.ts +38 -0
- package/types/iso-core/vdom.d.ts +26 -0
- package/types/iso-editor/adapter.d.ts +145 -0
- package/types/iso-editor/clipboard.d.ts +23 -0
- package/types/iso-editor/commands.d.ts +32 -0
- package/types/iso-editor/complete.d.ts +25 -0
- package/types/iso-editor/doc.d.ts +50 -0
- package/types/iso-editor/index.d.ts +57 -0
- package/types/iso-editor/model.d.ts +96 -0
- package/types/iso-editor/outline.d.ts +28 -0
- package/types/iso-editor/path.d.ts +7 -0
- package/types/iso-editor/providers.d.ts +43 -0
- package/types/iso-editor/schema-nav.d.ts +34 -0
- package/types/iso-editor/splice.d.ts +53 -0
- package/types/iso-editor/storage-keys.d.ts +5 -0
- package/types/iso-editor/ui/actions.d.ts +23 -0
- package/types/iso-editor/ui/canvas-layer.d.ts +65 -0
- package/types/iso-editor/ui/canvas.d.ts +279 -0
- package/types/iso-editor/ui/clip.d.ts +14 -0
- package/types/iso-editor/ui/dialog.d.ts +7 -0
- package/types/iso-editor/ui/flat-canvas.d.ts +2 -0
- package/types/iso-editor/ui/icons.d.ts +5 -0
- package/types/iso-editor/ui/keys.d.ts +7 -0
- package/types/iso-editor/ui/later.d.ts +12 -0
- package/types/iso-editor/ui/layer.d.ts +2 -0
- package/types/iso-editor/ui/menu.d.ts +21 -0
- package/types/iso-editor/ui/mount-kit.d.ts +31 -0
- package/types/iso-editor/ui/mount.d.ts +146 -0
- package/types/iso-editor/ui/pane.d.ts +60 -0
- package/types/iso-editor/ui/part.d.ts +40 -0
- package/types/iso-editor/ui/plain-icons.d.ts +10 -0
- package/types/iso-editor/ui/props.d.ts +57 -0
- package/types/iso-editor/ui/scope-css.d.ts +5 -0
- package/types/iso-editor/ui/setup.d.ts +40 -0
- package/types/iso-editor/ui/text.d.ts +19 -0
- package/types/iso-editor/ui/tree.d.ts +100 -0
- package/types/iso-editor/validate.d.ts +36 -0
- package/types/iso-editor/window/protocol.d.ts +60 -0
- package/types/iso-objects/assemble.d.ts +35 -0
- package/types/iso-objects/assets.d.ts +49 -0
- package/types/iso-objects/board.d.ts +36 -0
- package/types/iso-objects/compile.d.ts +35 -0
- package/types/iso-objects/css.d.ts +16 -0
- package/types/iso-objects/flat.d.ts +130 -0
- package/types/iso-objects/focus.d.ts +1 -0
- package/types/iso-objects/geometry.d.ts +59 -0
- package/types/iso-objects/index.d.ts +26 -0
- package/types/iso-objects/kinds.d.ts +53 -0
- package/types/iso-objects/labels.d.ts +92 -0
- package/types/iso-objects/place.d.ts +25 -0
- package/types/iso-objects/route.d.ts +67 -0
- package/types/iso-objects/scene.d.ts +63 -0
- package/types/iso-objects/steps.d.ts +90 -0
- package/types/iso-objects/types.d.ts +140 -0
- package/types/iso-systems/catalog.d.ts +4 -0
- package/types/iso-systems/compile.d.ts +6 -0
- package/types/iso-systems/css.d.ts +3 -0
- package/types/iso-systems/index.d.ts +5 -0
- package/types/iso-systems/nodes.d.ts +3 -0
- package/types/iso-viewer/card-motion.d.ts +56 -0
- package/types/iso-viewer/dom.d.ts +1 -0
- package/types/iso-viewer/dual-view.d.ts +44 -0
- package/types/iso-viewer/flat-view.d.ts +25 -0
- package/types/iso-viewer/history.d.ts +91 -0
- package/types/iso-viewer/index.d.ts +31 -0
- package/types/iso-viewer/morph.d.ts +28 -0
- package/types/iso-viewer/overlay.d.ts +135 -0
- package/types/iso-viewer/page.d.ts +13 -0
- package/types/iso-viewer/still.d.ts +26 -0
- package/types/iso-viewer/timeline-contract.d.ts +345 -0
- package/types/iso-viewer/timeline-engine.d.ts +20 -0
- package/types/iso-viewer/timeline-export.d.ts +5 -0
- package/types/iso-viewer/timeline-scale.d.ts +44 -0
- package/types/iso-viewer/timeline-scope.d.ts +33 -0
- package/types/iso-viewer/timeline-search.d.ts +52 -0
- package/types/iso-viewer/timeline-time.d.ts +8 -0
- package/types/iso-viewer/view-css.d.ts +7 -0
- package/types/iso-viewer/viewport.d.ts +50 -0
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
// One scene → one SVG string. Pure: no DOM, so it runs the same in Node and in the browser.
|
|
2
|
+
|
|
3
|
+
import { type Attrs, type Box, type P2, Kernel, FONTS, S, tag, iso, pt, n1, esc, textWidth } from '../iso-core/kernel.ts';
|
|
4
|
+
import { drawCloud } from './assets.ts';
|
|
5
|
+
import { type KindCatalog, regionKind, nodeKindOr } from './kinds.ts';
|
|
6
|
+
import { depthSort } from '../iso-core/depth.ts';
|
|
7
|
+
import { labelRect, Obstacles, placeCallout, placeNodeLabel, polyBounds, silhouette, type Rect } from './labels.ts';
|
|
8
|
+
import { WALL_T, gateBox, type Layout } from './place.ts';
|
|
9
|
+
import { floorAt, type Routed } from './route.ts';
|
|
10
|
+
import { assembly, type Assembly } from './assemble.ts';
|
|
11
|
+
import type { Steps } from './steps.ts';
|
|
12
|
+
import type { Placed, RawNote, RawScene, Theme } from './types.ts';
|
|
13
|
+
import { colorRef } from '../iso-core/theme.ts';
|
|
14
|
+
|
|
15
|
+
/** the colour role a region is drawn in: its own, else its kind's */
|
|
16
|
+
export const regionRole = (cat: KindCatalog, r: { role?: string; kind: string }): string => r.role ?? regionKind(cat, r.kind)?.role!;
|
|
17
|
+
const shapeOf = (cat: KindCatalog, r: Placed) => regionKind(cat, r.region!.kind)?.shape;
|
|
18
|
+
|
|
19
|
+
export interface RenderedScene {
|
|
20
|
+
svg: string;
|
|
21
|
+
viewBox: [number, number, number, number];
|
|
22
|
+
/** text rectangles in screen px, for layout tests */
|
|
23
|
+
texts: { x: number; y: number; w: number; h: number; text: string; owner?: string }[];
|
|
24
|
+
/** each route's path, for the player's tokens; in screen px */
|
|
25
|
+
paths: Record<string, string>;
|
|
26
|
+
/** things drawn worse than asked, e.g. a route label shortened or dropped */
|
|
27
|
+
warnings: RenderWarning[];
|
|
28
|
+
/** the assemble intro's timeline */
|
|
29
|
+
assembly: Assembly;
|
|
30
|
+
/** callout pills in screen px, per step, for layout tests */
|
|
31
|
+
callouts: { step: number; x: number; y: number; w: number; h: number; text: string }[];
|
|
32
|
+
}
|
|
33
|
+
export interface RenderWarning { path: string; message: string }
|
|
34
|
+
|
|
35
|
+
interface Item { box: Box; svg: string }
|
|
36
|
+
|
|
37
|
+
export interface SceneInput { scene: RawScene; layout: Layout; routes: Routed[]; steps: Steps; theme: Theme; id: string; gridStep?: number }
|
|
38
|
+
|
|
39
|
+
export function renderScene({ scene, layout, routes, steps, theme, id, gridStep = 1 }: SceneInput): RenderedScene {
|
|
40
|
+
const k = new Kernel();
|
|
41
|
+
const L = { base: '', floor: '', flows: '', blocks: [] as Item[], air: '', labels: '', marks: '', badges: '' };
|
|
42
|
+
const obs = new Obstacles();
|
|
43
|
+
obs.budget.spots = LABEL_BUDGET;
|
|
44
|
+
const visAttr = (oid: string) => (steps.vis[oid] ? { 'data-vis': '1' } : {});
|
|
45
|
+
|
|
46
|
+
// ---- regions ----
|
|
47
|
+
const regions = layout.items.filter(p => p.type === 'region');
|
|
48
|
+
const depthOf = (p: Placed) => { let d = 0; for (let a = p.parent; a; a = a.parent) d++; return d; };
|
|
49
|
+
const cat = layout.catalog;
|
|
50
|
+
const flat = regions.filter(r => shapeOf(cat, r) !== 'walled' && shapeOf(cat, r) !== 'tunnel');
|
|
51
|
+
// parents before children; siblings back to front
|
|
52
|
+
const ordered = depthSort(flat.map(r => ({ box: r.box, r }))).map(x => x.r).sort((a, b) => depthOf(a) - depthOf(b));
|
|
53
|
+
for (const r of ordered) L.base += drawFlatRegion(k, cat, r, visAttr(r.id), gridStep);
|
|
54
|
+
for (const r of regions.filter(r => shapeOf(cat, r) === 'walled')) {
|
|
55
|
+
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)) });
|
|
57
|
+
const g = r.region!.gate;
|
|
58
|
+
if (g && !g.node) {
|
|
59
|
+
const b = gateBox(r, g.height ?? (r.region!.height ?? 0.8) + 0.3, 0.3, 0.3);
|
|
60
|
+
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)) });
|
|
62
|
+
}
|
|
63
|
+
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) });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ---- 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));
|
|
70
|
+
|
|
71
|
+
// ---- nodes ----
|
|
72
|
+
const nodes = layout.items.filter(p => p.type === 'node');
|
|
73
|
+
for (const p of nodes) {
|
|
74
|
+
const n = p.node!, a = nodeKindOr(cat, n.asset);
|
|
75
|
+
const ghost = n.ghost || (n.deco ?? []).includes('ghost');
|
|
76
|
+
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)) });
|
|
78
|
+
obs.polys.push({ poly: silhouette(p.box), owner: p.id });
|
|
79
|
+
obs.solids.push(silhouette(p.box));
|
|
80
|
+
}
|
|
81
|
+
const blocks = depthSort(L.blocks).map(i => i.svg).join('');
|
|
82
|
+
|
|
83
|
+
// ---- routes ----
|
|
84
|
+
const markers = new Set<string>();
|
|
85
|
+
const paths: Record<string, string> = {};
|
|
86
|
+
for (const r of routes) {
|
|
87
|
+
if (r.points.length < 2) continue;
|
|
88
|
+
const scr = r.points.map(q => iso(q[0], q[1], q[2]));
|
|
89
|
+
scr.forEach(q => k.bounds.add(q));
|
|
90
|
+
const d = 'M' + scr.map(pt).join(' L');
|
|
91
|
+
paths[r.id] = d;
|
|
92
|
+
const arrow = r.raw.arrow !== false;
|
|
93
|
+
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 }));
|
|
96
|
+
obs.lines.push({ pts: scr, owner: r.id });
|
|
97
|
+
}
|
|
98
|
+
for (const kd of markers) {
|
|
99
|
+
k.def(`ah-${id}-${kd}`, tag('marker', { id: `ah-${id}-${kd}`, viewBox: '0 0 10 10', refX: '7', refY: '5', markerWidth: '4.2', markerHeight: '4.2', orient: 'auto-start-reverse' },
|
|
100
|
+
tag('path', { d: 'M0,0 L10,5 L0,10 z', style: `fill:${colorRef(theme, theme.kinds[kd]?.color ?? 'control')}` })));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// region names on faces and floors are obstacles for labels too
|
|
104
|
+
for (const q of k.planePolys) obs.slants.push(q);
|
|
105
|
+
// ---- node labels: biggest first, each clear of nodes, routes and the labels before it ----
|
|
106
|
+
const labelled = nodes.filter(p => p.node!.label || p.node!.sub)
|
|
107
|
+
.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));
|
|
109
|
+
|
|
110
|
+
// ---- route labels and notes ----
|
|
111
|
+
const warnings: RenderWarning[] = [];
|
|
112
|
+
for (const r of routes) if (r.raw.label && r.points.length >= 2) {
|
|
113
|
+
const out = routeLabel(k, r, obs, visAttr(r.id), layout);
|
|
114
|
+
L.floor += out.svg;
|
|
115
|
+
if (out.warning) warnings.push({ path: `${r.path}/label`, message: out.warning });
|
|
116
|
+
if (obs.budget.over && !obs.budget.told) {
|
|
117
|
+
obs.budget.told = true;
|
|
118
|
+
warnings.push({ path: r.path, message: `from route "${r.id}" on, route labels were placed without weighing what other labels and lines cover: the scene is too large to search every spot (${LABEL_BUDGET.toLocaleString('en')} spots); split it into scenes, or give the labels "along", "side" or "at"` });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
(scene.notes ?? []).forEach((note, i) => {
|
|
122
|
+
const s = drawNote(k, note, layout, routes, `${id}-note-${i}`);
|
|
123
|
+
if (note.floor) L.floor += s; else L.labels += s;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// ---- step marks: callouts, crosses, badges ----
|
|
127
|
+
const callouts: RenderedScene['callouts'] = [];
|
|
128
|
+
const byStep = new Map<number, Obstacles>();
|
|
129
|
+
for (const sy of steps.says) {
|
|
130
|
+
const o = byStep.get(sy.step) ?? obs.fork();
|
|
131
|
+
byStep.set(sy.step, o);
|
|
132
|
+
const w = textWidth(sy.text, FONTS.say) + 18;
|
|
133
|
+
const spot = placeCallout(w, sy.ax, sy.ay, { x: sy.x, y: sy.y, anchor: sy.anchor }, o);
|
|
134
|
+
o.rects.push({ r: spot.rect, owner: '' });
|
|
135
|
+
callouts.push({ step: sy.step, ...spot.rect, text: sy.text });
|
|
136
|
+
const left = spot.rect.x + 2;
|
|
137
|
+
k.bounds.addRect(spot.rect.x, spot.rect.y, spot.rect.w, spot.rect.h);
|
|
138
|
+
const c = colorRef(theme, theme.kinds[sy.kind]?.color ?? 'control');
|
|
139
|
+
L.marks += tag('g', { class: 'say', 'data-step': sy.step },
|
|
140
|
+
tag('rect', { x: left, y: spot.y - 12, width: w, height: 24, rx: 12, style: `stroke:${c}` }) +
|
|
141
|
+
tag('text', { x: left + w / 2, y: spot.y + 4, 'text-anchor': 'middle', style: `fill:${c}` }, esc(sy.text)));
|
|
142
|
+
}
|
|
143
|
+
for (const m of steps.marks) {
|
|
144
|
+
const [X, Y] = iso(...m.p);
|
|
145
|
+
k.bounds.addRect(X - 12, Y - 12, 24, 24);
|
|
146
|
+
L.marks += tag('g', { class: 'xm', 'data-step': m.step, 'data-hl': m.on },
|
|
147
|
+
tag('circle', { cx: X, cy: Y, r: 10 }) + tag('path', { d: `M${n1(X - 4.5)},${n1(Y - 4.5)} L${n1(X + 4.5)},${n1(Y + 4.5)} M${n1(X + 4.5)},${n1(Y - 4.5)} L${n1(X - 4.5)},${n1(Y + 4.5)}` }));
|
|
148
|
+
}
|
|
149
|
+
for (const b of steps.badges) {
|
|
150
|
+
const [X, Y] = iso(...b.p);
|
|
151
|
+
k.bounds.addRect(X - 13, Y - 13, 26, 26);
|
|
152
|
+
L.badges += tag('g', { class: `badge c-${b.kind}`, 'data-go': b.step, 'data-badge': b.step, tabindex: '0', role: 'button', 'aria-label': `Step ${b.num}` },
|
|
153
|
+
tag('circle', { cx: X, cy: Y, r: 11.5 }) + tag('text', { x: X, y: Y + 0.5 }, String(b.num)));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const bb = k.bounds, pad = 22;
|
|
157
|
+
const vb: [number, number, number, number] = bb.empty ? [0, 0, 100, 100] : [Math.floor(bb.x0 - pad), Math.floor(bb.y0 - pad), Math.ceil(bb.x1 - bb.x0 + pad * 2), Math.ceil(bb.y1 - bb.y0 + pad * 2)];
|
|
158
|
+
const defs = [...k.defs.values()].join('');
|
|
159
|
+
const g = (name: string, inner: string) => tag('g', { class: 'g-' + name }, inner);
|
|
160
|
+
const svg = tag('svg', {
|
|
161
|
+
xmlns: 'http://www.w3.org/2000/svg', viewBox: vb.join(' '), role: 'img', 'aria-label': scene.aria ?? scene.title,
|
|
162
|
+
'data-scene': id,
|
|
163
|
+
}, tag('defs', {}, defs) + g('base', L.base) + g('floor', L.floor) + g('flows', L.flows) + g('blocks', blocks) + g('air', L.air) + g('labels', L.labels) + g('marks', L.marks) + g('badges', L.badges) + g('anim', ''));
|
|
164
|
+
return { svg, viewBox: vb, texts: k.texts, paths, warnings, callouts, assembly: assembly(layout, routes, vb) };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Floor grid lines on a region's top, hidden until the Grid button shows them. */
|
|
168
|
+
function gridLines(b: Box, step: number): string {
|
|
169
|
+
const z = b.z1 + 0.002, e = 1e-6, d: string[] = [];
|
|
170
|
+
const seg = (a: [number, number], c: [number, number]) => { const p = iso(a[0], a[1], z), q = iso(c[0], c[1], z); d.push(`M${pt(p)}L${pt(q)}`); };
|
|
171
|
+
for (let x = Math.ceil((b.x0 + e) / step) * step; x < b.x1 - e; x += step) seg([x, b.y0], [x, b.y1]);
|
|
172
|
+
for (let y = Math.ceil((b.y0 + e) / step) * step; y < b.y1 - e; y += step) seg([b.x0, y], [b.x1, y]);
|
|
173
|
+
return d.length ? tag('path', { class: 'grid', d: d.join('') }) : '';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function drawFlatRegion(k: Kernel, cat: KindCatalog, r: Placed, vis: Attrs, gridStep = 1): string {
|
|
177
|
+
const R = r.region!, b = r.box, role = regionRole(cat, R), rk = regionKind(cat, R.kind), shape = rk?.shape;
|
|
178
|
+
const attrs = { 'data-id': r.id, 'data-dim': '1', ...vis };
|
|
179
|
+
let s = '';
|
|
180
|
+
if (shape === 'cloud') s = tag('g', { class: `blk k-${role} cloud`, ...attrs }, drawCloud(k, b));
|
|
181
|
+
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
|
+
else s = tag('g', { class: `blk k-${role}${R.ghost ? ' ghost' : ''}`, ...attrs }, k.blockFaces(b) + gridLines(b, gridStep));
|
|
183
|
+
const style = R['label-style'] ?? rk?.labelStyle;
|
|
184
|
+
if (R.label && style !== 'none') {
|
|
185
|
+
if (style === 'face') {
|
|
186
|
+
const big = shape === 'slab';
|
|
187
|
+
const z = big ? b.z0 + 0.28 : b.z0 + Math.min(0.1, (b.z1 - b.z0) * 0.25);
|
|
188
|
+
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'));
|
|
189
|
+
} else if (style === 'face-right') {
|
|
190
|
+
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'));
|
|
191
|
+
} else {
|
|
192
|
+
const m = shape === 'cloud' ? 'middle' : 'start';
|
|
193
|
+
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;
|
|
194
|
+
s += tag('g', attrs, k.floorText(x, y, b.z1 + 0.01, R.label, 'slabname sm', 'x', m));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return s;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The walls of a walled region's cage, with a gap where its gate is. */
|
|
201
|
+
export function wallsOf(r: Placed): Box[] {
|
|
202
|
+
const { x0, y0, x1, y1, z0, z1 } = r.box, t = WALL_T, g = r.region!.gate;
|
|
203
|
+
const walls: Box[] = [];
|
|
204
|
+
const W = (b: Omit<Box, 'z0' | 'z1'>) => { if (b.x1 - b.x0 > 1e-6 && b.y1 - b.y0 > 1e-6) walls.push({ ...b, z0, z1 }); };
|
|
205
|
+
const span = (lo: number, hi: number, side: 'n' | 'e' | 's' | 'w') => {
|
|
206
|
+
if (!g || g.side !== side) return [[lo, hi]];
|
|
207
|
+
const at = g.at ?? 0.5, w = g.width ?? 2;
|
|
208
|
+
const c = side === 'e' || side === 'w' ? y0 + (y1 - y0) * at : x0 + (x1 - x0) * at;
|
|
209
|
+
return [[lo, c - w / 2], [c + w / 2, hi]];
|
|
210
|
+
};
|
|
211
|
+
for (const [a, b] of span(x0, x1, 'n')) W({ x0: a, y0, x1: b, y1: y0 + t });
|
|
212
|
+
for (const [a, b] of span(y0 + t, y1 - t, 'w')) W({ x0, y0: a, x1: x0 + t, y1: b });
|
|
213
|
+
for (const [a, b] of span(y0 + t, y1 - t, 'e')) W({ x0: x1 - t, y0: a, x1, y1: b });
|
|
214
|
+
for (const [a, b] of span(x0, x1, 's')) W({ x0: a, y0: y1 - t, x1: b, y1 });
|
|
215
|
+
return walls;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function nodeLabel(k: Kernel, cat: KindCatalog, p: Placed, obs: Obstacles, vis: Attrs): string {
|
|
219
|
+
const n = p.node!, a = nodeKindOr(cat, n.asset), b = p.box;
|
|
220
|
+
const t = { name: n.label, sub: n.sub };
|
|
221
|
+
const la = n['label-at'];
|
|
222
|
+
let spot: { x: number; y: number; anchor: 'start' | 'middle' | 'end' };
|
|
223
|
+
if (la) {
|
|
224
|
+
// the author decides: no nudging
|
|
225
|
+
const sb = polyBounds(silhouette(b));
|
|
226
|
+
const side = la.side ?? 'above', midY = sb.y + Math.min(sb.h, 60) * 0.45;
|
|
227
|
+
if (side === 'right') spot = { x: sb.x + sb.w + 8, y: midY, anchor: 'start' };
|
|
228
|
+
else if (side === 'left') spot = { x: sb.x - 8, y: midY, anchor: 'end' };
|
|
229
|
+
else if (side === 'below') spot = { x: sb.x + sb.w / 2, y: sb.y + sb.h + 14, anchor: 'middle' };
|
|
230
|
+
else spot = placeNodeLabel(t, b, silhouette(b), new Obstacles(), p.id);
|
|
231
|
+
spot = { ...spot, x: spot.x + (la.dx ?? 0), y: spot.y + (la.dy ?? 0) };
|
|
232
|
+
} else {
|
|
233
|
+
const pref = a.label ? a.label(b, n, !!n.sub) : undefined;
|
|
234
|
+
spot = placeNodeLabel(t, b, silhouette(b), obs, p.id, pref);
|
|
235
|
+
}
|
|
236
|
+
obs.rects.push({ r: labelRect(t, spot.x, spot.y, spot.anchor), owner: '' });
|
|
237
|
+
let s = '';
|
|
238
|
+
if (n.label) s += k.text(spot.x, spot.y, n.label, 'lbl', FONTS.lbl, spot.anchor, {}, p.id);
|
|
239
|
+
if (n.sub) s += k.text(spot.x, n.label ? spot.y + 14 : spot.y, n.sub, 'sub mono', FONTS.sub, spot.anchor, {}, p.id);
|
|
240
|
+
return tag('g', { 'data-id': p.id, 'data-dim': '1', ...vis }, s);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** A tunnel: a translucent box with ribs across it and its name along one side, as in the reference page. */
|
|
244
|
+
function drawTunnel(k: Kernel, r: Placed, vis: Attrs): string {
|
|
245
|
+
const { x0, y0, x1, y1, z0, z1 } = r.box;
|
|
246
|
+
let s = k.blockFaces(r.box);
|
|
247
|
+
const alongX = x1 - x0 >= y1 - y0, n = Math.max(1, Math.round((alongX ? x1 - x0 : y1 - y0) / 1.6));
|
|
248
|
+
for (let i = 1; i < n; i++) {
|
|
249
|
+
const t = alongX ? x0 + (x1 - x0) * i / n : y0 + (y1 - y0) * i / n;
|
|
250
|
+
s += k.poly(alongX ? [[t, y0, z1], [t, y1, z1], [t, y1, z0]] : [[x0, t, z1], [x1, t, z1], [x1, t, z0]], 'rib');
|
|
251
|
+
}
|
|
252
|
+
const label = r.region!.label;
|
|
253
|
+
if (label && r.region!['label-style'] !== 'none') {
|
|
254
|
+
s += alongX ? k.faceText(x0 + 0.35, y1, z0 + 0.14, label, 'tunname', FONTS.tunname, 'left', 'start')
|
|
255
|
+
: k.faceText(x1, y1 - 0.35, z0 + 0.18, label, 'tunname', FONTS.tunname, 'right', 'start');
|
|
256
|
+
}
|
|
257
|
+
return tag('g', { class: 'tunnel', 'data-id': r.id, 'data-dim': '1', ...vis }, s);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* A route's floor label: beside one of its level segments (the `along` one, else the longest first), on either side,
|
|
262
|
+
* slid along the segment, wherever nothing drawn over the floor would hide it. If nowhere fits, the text is shortened
|
|
263
|
+
* with an ellipsis; if even that fails, the label is dropped. Either way the caller gets a warning.
|
|
264
|
+
*/
|
|
265
|
+
/** label spots weighed in one scene: the busiest diagram here weighs under 200; a drawing with 80 more nodes, 79,000 */
|
|
266
|
+
export const LABEL_BUDGET = 20_000;
|
|
267
|
+
|
|
268
|
+
function routeLabel(k: Kernel, r: Routed, obs: Obstacles, vis: Attrs, layout: Layout): { svg: string; warning?: string } {
|
|
269
|
+
const L = typeof r.raw.label === 'string' ? { text: r.raw.label } : r.raw.label!;
|
|
270
|
+
const cls = `floor mono t-${r.kind}`, attrs = { 'data-hl': r.id, ...vis };
|
|
271
|
+
const font = FONTS.floor;
|
|
272
|
+
if (L.at) {
|
|
273
|
+
const z = L.at.length > 2 ? L.at[2] : r.points[0][2];
|
|
274
|
+
return { svg: k.floorText(L.at[0], L.at[1], z, L.text, cls, L.dir ?? 'x', L.anchor ?? 'start', attrs) };
|
|
275
|
+
}
|
|
276
|
+
const P = r.points;
|
|
277
|
+
const segs: { i: number; len: number }[] = [];
|
|
278
|
+
for (let i = 0; i + 1 < P.length; i++) {
|
|
279
|
+
const a = P[i], b = P[i + 1];
|
|
280
|
+
if (Math.abs(a[2] - b[2]) > 1e-6) continue; // a step up or down
|
|
281
|
+
segs.push({ i, len: Math.hypot(b[0] - a[0], b[1] - a[1]) });
|
|
282
|
+
}
|
|
283
|
+
if (!segs.length) return { svg: '', warning: `label "${L.text}" of route "${r.id}" was dropped: the route has no level segment` };
|
|
284
|
+
const order = L.along ? segs.filter(sg => sg.i === Math.min(L.along! - 1, P.length - 2)) : [...segs].sort((a, b) => b.len - a.len);
|
|
285
|
+
const GAP = 0.22, hgt = 11 / S;
|
|
286
|
+
|
|
287
|
+
const attempt = (text: string) => {
|
|
288
|
+
const w = textWidth(text, font) / S;
|
|
289
|
+
let best: { x: number; y: number; z: number; dir: 'x' | 'y'; rect: Rect; poly: P2[]; hidden: number } | undefined, bestCost = Infinity;
|
|
290
|
+
let rank = 0;
|
|
291
|
+
// Every spot costs at least rank × 0.5, and rank only grows: once the best is no more than that, no later spot can
|
|
292
|
+
// beat it, so the search stops (the same spot as searching on; on a large drawing it was most of the drawing time).
|
|
293
|
+
search:
|
|
294
|
+
for (const sg of order.length ? order : segs) {
|
|
295
|
+
const a = P[sg.i], b = P[sg.i + 1], z = a[2];
|
|
296
|
+
const alongX = Math.abs(a[1] - b[1]) < 1e-6;
|
|
297
|
+
const lo = alongX ? Math.min(a[0], b[0]) : Math.min(a[1], b[1]), hi = alongX ? Math.max(a[0], b[0]) : Math.max(a[1], b[1]);
|
|
298
|
+
const line = alongX ? a[1] : a[0];
|
|
299
|
+
const sides = L.side === 'left' ? [-1] : L.side === 'right' ? [1] : [-1, 1];
|
|
300
|
+
// slide the text along the segment; it may run past the segment's end if the floor there is clear
|
|
301
|
+
const starts: number[] = [];
|
|
302
|
+
for (let t = lo + 0.3; t <= Math.max(lo + 0.3, hi - w - 0.2) + 1e-9; t += 0.4) starts.push(t);
|
|
303
|
+
for (let t = hi - w - 0.2 + 0.4; t < hi + 0.5 && hi - lo < w + 0.5; t += 0.6) starts.push(t);
|
|
304
|
+
for (const sd of sides) for (const st of starts) {
|
|
305
|
+
if (bestCost <= rank * 0.5) break search;
|
|
306
|
+
// glyphs rise toward −y (dir x) or −x (dir y) from the baseline
|
|
307
|
+
const base = sd < 0 ? line - GAP : line + GAP + hgt;
|
|
308
|
+
let x: number, y: number, poly: P2[];
|
|
309
|
+
if (alongX) { x = st; y = base; poly = [iso(x, y - hgt, z), iso(x + w, y - hgt, z), iso(x + w, y, z), iso(x, y, z)]; }
|
|
310
|
+
else { x = base; y = st + w; poly = [iso(x - hgt, y, z), iso(x - hgt, y - w, z), iso(x, y - w, z), iso(x, y, z)]; }
|
|
311
|
+
// the text must lie on this segment's floor: every corner over the same surface
|
|
312
|
+
const corners = alongX ? [[x, y - hgt], [x + w, y - hgt], [x + w, y], [x, y]] : [[x - hgt, y], [x - hgt, y - w], [x, y - w], [x, y]];
|
|
313
|
+
if (corners.some(([u, v]) => Math.abs(floorAt(layout, u, v) + 0.01 - z) > 1e-3)) { rank++; continue; }
|
|
314
|
+
const hidden = obs.hiddenPoly(poly);
|
|
315
|
+
// what is known already can't beat the best: skip the rest of the cost (it only adds). Past the scene's budget
|
|
316
|
+
// of spots, what other labels and lines cover isn't weighed: the first spot nothing hides wins.
|
|
317
|
+
const known = hidden * 50 + rank * 0.5;
|
|
318
|
+
const weigh = obs.budget.spots-- > 0;
|
|
319
|
+
if (!weigh) obs.budget.over = true;
|
|
320
|
+
const cost = known >= bestCost ? Infinity : weigh ? known + obs.costPoly(poly, bestCost - known) : known;
|
|
321
|
+
rank++;
|
|
322
|
+
if (cost < bestCost) { bestCost = cost; best = { x, y, z, dir: alongX ? 'x' : 'y', rect: polyBounds(poly), poly, hidden }; }
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return best && best.hidden < 6 ? best : undefined;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
let text = L.text, spot = attempt(text), warning: string | undefined;
|
|
329
|
+
if (!spot) {
|
|
330
|
+
const words = L.text.split(/(\s+)/);
|
|
331
|
+
for (let n = words.length - 1; n > 0 && !spot; n--) {
|
|
332
|
+
const t = words.slice(0, n).join('').trimEnd().replace(/[\s,;:·–-]+$/, '') + '…';
|
|
333
|
+
if (t.length < 6) break;
|
|
334
|
+
const sp = attempt(t);
|
|
335
|
+
if (sp) { spot = sp; text = t; }
|
|
336
|
+
}
|
|
337
|
+
warning = spot ? `label of route "${r.id}" shortened to "${text}": no clear floor for the whole text`
|
|
338
|
+
: `label "${L.text}" of route "${r.id}" was dropped: blocks cover every spot beside it`;
|
|
339
|
+
if (spot) warning += '; give it "along", "side" or "at", or move the nodes apart';
|
|
340
|
+
else warning += '; give it "at: [x, y]", or move the nodes apart';
|
|
341
|
+
}
|
|
342
|
+
if (!spot) return { svg: '', warning };
|
|
343
|
+
obs.slants.push(spot.poly);
|
|
344
|
+
return { svg: k.floorText(spot.x, spot.y, spot.z + 0.01, text, cls, spot.dir, 'start', attrs), warning };
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const shrink = (r: Rect, d: number): Rect => ({ x: r.x + d, y: r.y + d, w: Math.max(0, r.w - 2 * d), h: Math.max(0, r.h - 2 * d) });
|
|
348
|
+
|
|
349
|
+
function drawNote(k: Kernel, note: RawNote, layout: Layout, routes: Routed[], id: string): string {
|
|
350
|
+
const route = note.on ? routes.find(r => r.id === note.on) : undefined;
|
|
351
|
+
const on = note.on && !route ? layout.byId.get(note.on) : undefined;
|
|
352
|
+
const at = note.at;
|
|
353
|
+
const attrs = { 'data-id': id, 'data-hl': route?.id, 'data-dim': route ? undefined : '1' };
|
|
354
|
+
const kind = note.kind ?? route?.kind ?? 'control';
|
|
355
|
+
if (note.floor) {
|
|
356
|
+
const z = at && at.length > 2 ? at[2] : route ? route.points[0][2] : on ? on.floor : 0.36;
|
|
357
|
+
const [x, y] = at ? [at[0], at[1]] : route ? [route.points[0][0] + 0.35, route.points[0][1] - 0.25] : on ? [on.box.x0 + 0.3, on.box.y1 - 0.3] : [0, 0];
|
|
358
|
+
return k.floorText(x, y, z + 0.01, note.floor, `floor mono t-${kind}`, note.dir ?? 'x', note.anchor ?? 'start', attrs);
|
|
359
|
+
}
|
|
360
|
+
if (note.face) {
|
|
361
|
+
const b = on?.box;
|
|
362
|
+
const [x, y, z] = at ? [at[0], at[1], at[2] ?? 0.45] : b ? [b.x0 + 0.3, b.y1, b.z0 + 0.13] : [0, 0, 0];
|
|
363
|
+
return k.faceText(x, y, z, note.face, 'slabname sm', FONTS.sm, 'left', note.anchor ?? 'start', attrs);
|
|
364
|
+
}
|
|
365
|
+
if (note.label || note.sub) {
|
|
366
|
+
const b = on?.box;
|
|
367
|
+
const p3 = at ? [at[0], at[1], at[2] ?? 0.36] : b ? [(b.x0 + b.x1) / 2, (b.y0 + b.y1) / 2, b.z1 + 0.75] : [0, 0, 0];
|
|
368
|
+
const [X, Y] = iso(p3[0], p3[1], p3[2]);
|
|
369
|
+
const x = X + (note.dx ?? 0), y = Y - 18 + (note.dy ?? 0), anchor = note.anchor ?? 'middle';
|
|
370
|
+
let s = '';
|
|
371
|
+
if (note.label) s += k.text(x, y, note.label, 'lbl', FONTS.lbl, anchor);
|
|
372
|
+
if (note.sub) s += k.text(x, note.label ? y + 14 : y, note.sub, 'sub mono', FONTS.sub, anchor);
|
|
373
|
+
return tag('g', attrs, s);
|
|
374
|
+
}
|
|
375
|
+
return '';
|
|
376
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// Steps: resolve the YAML's step references (routes, nodes, step ids) into the data the player runs on.
|
|
2
|
+
|
|
3
|
+
import { iso, type P3 } from '../iso-core/kernel.ts';
|
|
4
|
+
import { nodeKind } from './kinds.ts';
|
|
5
|
+
import { didYouMean, type Diag } from '../iso-core/errors.ts';
|
|
6
|
+
import type { Layout, Located } from './place.ts';
|
|
7
|
+
import type { Routed } from './route.ts';
|
|
8
|
+
import type { RawScene, StepRef, Theme } from './types.ts';
|
|
9
|
+
import { colorRef } from '../iso-core/theme.ts';
|
|
10
|
+
|
|
11
|
+
export interface RawStep {
|
|
12
|
+
id?: string; number?: number; kind?: string; heading: string; caption?: string; command?: string;
|
|
13
|
+
animate?: (string | Record<string, string> | (string | Record<string, string>)[])[];
|
|
14
|
+
pulse?: string[]; highlight?: string[]; show?: string[]; hide?: string[]; focus?: string[];
|
|
15
|
+
say?: { at: string | number[]; text: string; kind?: string; dx?: number; dy?: number; anchor?: 'start' | 'middle' | 'end' }[];
|
|
16
|
+
mark?: { on?: string; at?: number[] }[];
|
|
17
|
+
badge?: boolean | string | number[]; duration?: 'auto' | number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface Token { route: string; kind: string; color: string }
|
|
21
|
+
export interface StepData {
|
|
22
|
+
n: number; // 1-based position
|
|
23
|
+
num: number; // the number shown
|
|
24
|
+
kind: string; color: string;
|
|
25
|
+
heading: string; caption: string; command: string;
|
|
26
|
+
phases: Token[][];
|
|
27
|
+
pulses: [number, number, number, number][]; // screen x, y, rx, ry
|
|
28
|
+
lit: string[];
|
|
29
|
+
duration: number;
|
|
30
|
+
/** screen box [x0, y0, x1, y1] to zoom to, from `focus:` */
|
|
31
|
+
focus?: [number, number, number, number];
|
|
32
|
+
}
|
|
33
|
+
/** A callout: (x, y) is where the author asked for it; (ax, ay) is the point it belongs to, for placement. */
|
|
34
|
+
export interface Say { step: number; x: number; y: number; ax: number; ay: number; text: string; kind: string; anchor: 'start' | 'middle' | 'end' }
|
|
35
|
+
export interface Mark { step: number; p: P3; on?: string }
|
|
36
|
+
export interface Badge { step: number; num: number; kind: string; p: P3 }
|
|
37
|
+
|
|
38
|
+
export interface Steps {
|
|
39
|
+
steps: StepData[];
|
|
40
|
+
says: Say[]; marks: Mark[]; badges: Badge[];
|
|
41
|
+
/** id → visibility at each step (index 0 = step 1); only for ids whose visibility ever changes */
|
|
42
|
+
vis: Record<string, boolean[]>;
|
|
43
|
+
/** ids of route-bound notes: note index → route id */
|
|
44
|
+
diags: Diag[];
|
|
45
|
+
kindsUsed: string[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function durationFor(caption: string): number {
|
|
49
|
+
return Math.max(5600, Math.min(12500, 2600 + caption.length * 40));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function resolveSteps(scene: RawScene, scenePath: string, layout: Layout, routes: Routed[], src: Located, theme: Theme): Steps {
|
|
53
|
+
const diags: Diag[] = [];
|
|
54
|
+
const err = (path: string, message: string, hint?: string, severity: 'error' | 'warning' = 'error') => {
|
|
55
|
+
const p = src.locate(path); diags.push({ severity, message, line: p.line, col: p.col, path, hint });
|
|
56
|
+
};
|
|
57
|
+
const raws = (scene.steps ?? []) as RawStep[];
|
|
58
|
+
const kinds = new Set(Object.keys(theme.kinds));
|
|
59
|
+
const routeById = new Map(routes.map(r => [r.id, r]));
|
|
60
|
+
const allIds = () => [...layout.byId.keys(), ...routeById.keys()];
|
|
61
|
+
const stepIds = new Map<string, number>();
|
|
62
|
+
raws.forEach((s, i) => {
|
|
63
|
+
if (s.id) {
|
|
64
|
+
if (stepIds.has(s.id)) err(`${scenePath}/steps/${i}/id`, `duplicate step id "${s.id}"`);
|
|
65
|
+
stepIds.set(s.id, i + 1);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
const N = raws.length;
|
|
69
|
+
const stepOf = (ref: StepRef, path: string): number | undefined => {
|
|
70
|
+
if (typeof ref === 'number') {
|
|
71
|
+
if (ref < 1 || ref > N) { err(path, `there is no step ${ref}; this scene has ${N} step(s)`); return undefined; }
|
|
72
|
+
return ref;
|
|
73
|
+
}
|
|
74
|
+
const n = stepIds.get(ref);
|
|
75
|
+
if (n === undefined) err(path, `unknown step "${ref}"` + dym(ref, stepIds.keys()));
|
|
76
|
+
return n;
|
|
77
|
+
};
|
|
78
|
+
const color = (k: string) => colorRef(theme, theme.kinds[k]?.color ?? 'control');
|
|
79
|
+
const kindsUsed: string[] = [];
|
|
80
|
+
const use = (k: string) => { if (!kindsUsed.includes(k)) kindsUsed.push(k); };
|
|
81
|
+
for (const r of routes) use(r.kind);
|
|
82
|
+
|
|
83
|
+
// visibility from appears/disappears on items and routes, then show/hide in steps
|
|
84
|
+
const vis: Record<string, boolean[]> = {};
|
|
85
|
+
const initial = (id: string, appears: StepRef | undefined, disappears: StepRef | undefined, path: string) => {
|
|
86
|
+
if (appears === undefined && disappears === undefined) return;
|
|
87
|
+
const a = appears !== undefined ? stepOf(appears, path + '/appears') : 1;
|
|
88
|
+
const d = disappears !== undefined ? stepOf(disappears, path + '/disappears') : N + 1;
|
|
89
|
+
vis[id] = Array.from({ length: N }, (_, i) => i + 1 >= (a ?? 1) && i + 1 < (d ?? N + 1));
|
|
90
|
+
};
|
|
91
|
+
for (const p of layout.items) initial(p.id, p.appears, p.disappears, p.path);
|
|
92
|
+
for (const r of routes) initial(r.id, r.raw.appears, r.raw.disappears, r.path);
|
|
93
|
+
|
|
94
|
+
const steps: StepData[] = [];
|
|
95
|
+
const says: Say[] = [], marks: Mark[] = [], badges: Badge[] = [];
|
|
96
|
+
const routeEnds = () => {
|
|
97
|
+
// a route is visible only while both its ends are (unless it says otherwise itself)
|
|
98
|
+
for (const r of routes) {
|
|
99
|
+
if (r.raw.appears !== undefined || r.raw.disappears !== undefined) continue;
|
|
100
|
+
const ends = [r.from, r.to].map(p => p && (vis[p.id] ?? ancestorVis(p)));
|
|
101
|
+
if (!ends.some(Boolean)) continue;
|
|
102
|
+
vis[r.id] = Array.from({ length: N }, (_, i) => ends.every(v => !v || v[i]));
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const ancestorVis = (p: { parent?: { id: string; parent?: unknown } }): boolean[] | undefined => {
|
|
106
|
+
for (let a = p.parent as { id: string; parent?: unknown } | undefined; a; a = a.parent as typeof a) if (vis[a.id]) return vis[a.id];
|
|
107
|
+
return undefined;
|
|
108
|
+
};
|
|
109
|
+
raws.forEach((s, i) => {
|
|
110
|
+
const n = i + 1, path = `${scenePath}/steps/${i}`;
|
|
111
|
+
const kind = s.kind ?? 'control';
|
|
112
|
+
if (!kinds.has(kind)) err(path + '/kind', `unknown flow kind "${kind}"` + dym(kind, kinds));
|
|
113
|
+
use(kind);
|
|
114
|
+
const lit = new Set<string>();
|
|
115
|
+
const phases: Token[][] = [];
|
|
116
|
+
(s.animate ?? []).forEach((ph, pi) => {
|
|
117
|
+
const list = Array.isArray(ph) ? ph : [ph];
|
|
118
|
+
const toks: Token[] = [];
|
|
119
|
+
list.forEach((ref, ri) => {
|
|
120
|
+
const rp = Array.isArray(ph) ? `${path}/animate/${pi}/${ri}` : `${path}/animate/${pi}`;
|
|
121
|
+
let id: string, k: string | undefined;
|
|
122
|
+
if (typeof ref === 'string') { const m = /^([^:\s]+)\s*(?::\s*(\S+))?$/.exec(ref.trim()); id = m ? m[1] : ref; k = m?.[2]; }
|
|
123
|
+
else { const e = Object.entries(ref)[0]; id = e[0]; k = e[1]; }
|
|
124
|
+
const r = routeById.get(id);
|
|
125
|
+
if (!r) { err(rp, `unknown route "${id}"` + dym(id, routeById.keys())); return; }
|
|
126
|
+
if (k && !kinds.has(k)) { err(rp, `unknown flow kind "${k}"` + dym(k, kinds)); return; }
|
|
127
|
+
const tk = k ?? r.kind;
|
|
128
|
+
use(tk);
|
|
129
|
+
toks.push({ route: id, kind: tk, color: color(tk) });
|
|
130
|
+
lit.add(id);
|
|
131
|
+
if (r.from) lit.add(r.from.id);
|
|
132
|
+
if (r.to) lit.add(r.to.id);
|
|
133
|
+
});
|
|
134
|
+
if (toks.length) phases.push(toks);
|
|
135
|
+
});
|
|
136
|
+
const pulses: StepData['pulses'] = [];
|
|
137
|
+
(s.pulse ?? []).forEach((id, pi) => {
|
|
138
|
+
const p = layout.byId.get(id);
|
|
139
|
+
if (!p) { err(`${path}/pulse/${pi}`, `unknown node "${id}"` + dym(id, layout.byId.keys())); return; }
|
|
140
|
+
let x: number, y: number, z: number, r: number;
|
|
141
|
+
const a = p.node ? nodeKind(layout.catalog, p.node.asset) : undefined;
|
|
142
|
+
if (a?.pulse) [x, y, z, r] = a.pulse(p.box);
|
|
143
|
+
else { const b = p.box; x = (b.x0 + b.x1) / 2; y = (b.y0 + b.y1) / 2; z = b.z1; r = Math.max(22, Math.min(60, (b.x1 - b.x0 + b.y1 - b.y0) * 8.5)); }
|
|
144
|
+
const [X, Y] = iso(x, y, z);
|
|
145
|
+
pulses.push([X, Y, r * 1.7, r * 0.98]);
|
|
146
|
+
lit.add(id);
|
|
147
|
+
});
|
|
148
|
+
(s.highlight ?? []).forEach((id, hi) => {
|
|
149
|
+
if (!layout.byId.has(id) && !routeById.has(id)) err(`${path}/highlight/${hi}`, `unknown id "${id}"` + dym(id, allIds()));
|
|
150
|
+
lit.add(id);
|
|
151
|
+
});
|
|
152
|
+
for (const [key, val] of [['show', true], ['hide', false]] as const) {
|
|
153
|
+
(s[key] ?? []).forEach((id, hi) => {
|
|
154
|
+
if (!layout.byId.has(id) && !routeById.has(id)) { err(`${path}/${key}/${hi}`, `unknown id "${id}"` + dym(id, allIds())); return; }
|
|
155
|
+
const v = vis[id] ?? (vis[id] = Array(N).fill(!val));
|
|
156
|
+
for (let j = i; j < N; j++) v[j] = val;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
(s.say ?? []).forEach((c, ci) => {
|
|
160
|
+
let X: number, Y: number;
|
|
161
|
+
if (Array.isArray(c.at)) [X, Y] = iso(c.at[0], c.at[1], c.at[2] ?? 0.36);
|
|
162
|
+
else {
|
|
163
|
+
const p = layout.byId.get(c.at);
|
|
164
|
+
if (!p) { err(`${path}/say/${ci}/at`, `unknown node "${c.at}"` + dym(c.at, layout.byId.keys())); return; }
|
|
165
|
+
const b = p.box;
|
|
166
|
+
[X, Y] = iso((b.x0 + b.x1) / 2, (b.y0 + b.y1) / 2, b.z1 + 0.75);
|
|
167
|
+
}
|
|
168
|
+
const k = c.kind ?? kind;
|
|
169
|
+
if (!kinds.has(k)) err(`${path}/say/${ci}/kind`, `unknown flow kind "${k}"` + dym(k, kinds));
|
|
170
|
+
says.push({ step: n, x: X + (c.dx ?? 0), y: Y + (c.dy ?? (Array.isArray(c.at) ? 0 : -46)), ax: X, ay: Y, text: c.text, kind: k, anchor: c.anchor ?? 'middle' });
|
|
171
|
+
});
|
|
172
|
+
(s.mark ?? []).forEach((m, mi) => {
|
|
173
|
+
if (m.at) { marks.push({ step: n, p: [m.at[0], m.at[1], m.at[2] ?? 0.36] }); return; }
|
|
174
|
+
const r = m.on ? routeById.get(m.on) : undefined;
|
|
175
|
+
if (!r) { err(`${path}/mark/${mi}`, m.on ? `unknown route "${m.on}"` + dym(m.on, routeById.keys()) : 'a mark needs "on: ROUTE" or "at: [x, y]"'); return; }
|
|
176
|
+
const end = r.points[r.points.length - 1];
|
|
177
|
+
marks.push({ step: n, p: [end[0], end[1], end[2] + 0.6], on: r.id });
|
|
178
|
+
lit.add(r.id);
|
|
179
|
+
});
|
|
180
|
+
if (scene.badges && s.badge !== false) {
|
|
181
|
+
let p: P3 | undefined = Array.isArray(s.badge) ? [s.badge[0], s.badge[1], s.badge[2] ?? 0.36] : undefined;
|
|
182
|
+
const target = typeof s.badge === 'string' ? s.badge : phases[0]?.[0]?.route;
|
|
183
|
+
const r = target ? routeById.get(target) : undefined;
|
|
184
|
+
if (p) { /* explicit point */ }
|
|
185
|
+
else if (r) p = midpoint(r.points);
|
|
186
|
+
else if (target && layout.byId.get(target)) { const b = layout.byId.get(target)!.box; p = [(b.x0 + b.x1) / 2, (b.y0 + b.y1) / 2, b.z1 + 0.3]; }
|
|
187
|
+
else if (typeof s.badge === 'string') err(`${path}/badge`, `unknown id "${s.badge}"` + dym(s.badge, allIds()));
|
|
188
|
+
if (p) badges.push({ step: n, num: s.number ?? n, kind, p });
|
|
189
|
+
}
|
|
190
|
+
// a lit thing lights the regions it stands in; a tunnel lights with the routes it wraps
|
|
191
|
+
for (const id of [...lit]) for (let a = layout.byId.get(id)?.parent; a; a = a.parent) lit.add(a.id);
|
|
192
|
+
for (const t of layout.items) if (t.region?.kind === 'tunnel' && (t.region.around ?? []).some(r => lit.has(r))) lit.add(t.id);
|
|
193
|
+
// focus: the screen box around the named parts (their outlines, plus a margin for labels above)
|
|
194
|
+
let focus: StepData['focus'];
|
|
195
|
+
if (s.focus?.length) {
|
|
196
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
197
|
+
s.focus.forEach((id, fi) => {
|
|
198
|
+
const p = layout.byId.get(id);
|
|
199
|
+
if (!p) { err(`${path}/focus/${fi}`, `unknown node or region "${id}"` + dym(id, layout.byId.keys())); return; }
|
|
200
|
+
const b = p.box;
|
|
201
|
+
for (const [x, y, z] of [[b.x0, b.y0, b.z1], [b.x1, b.y0, b.z0], [b.x0, b.y1, b.z0], [b.x1, b.y1, b.z0], [b.x1, b.y0, b.z1], [b.x0, b.y1, b.z1]] as P3[]) {
|
|
202
|
+
const [X, Y] = iso(x, y, z);
|
|
203
|
+
x0 = Math.min(x0, X); y0 = Math.min(y0, Y); x1 = Math.max(x1, X); y1 = Math.max(y1, Y);
|
|
204
|
+
}
|
|
205
|
+
lit.add(id);
|
|
206
|
+
});
|
|
207
|
+
if (x0 < Infinity) focus = [Math.round(x0 - 60), Math.round(y0 - 70), Math.round(x1 + 60), Math.round(y1 + 40)];
|
|
208
|
+
}
|
|
209
|
+
const caption = s.caption ?? '';
|
|
210
|
+
steps.push({
|
|
211
|
+
n, num: s.number ?? n, kind, color: color(kind), heading: s.heading, caption, command: s.command ?? '',
|
|
212
|
+
phases, pulses, lit: [...lit], duration: s.duration === undefined || s.duration === 'auto' ? durationFor(caption) : s.duration,
|
|
213
|
+
...(focus ? { focus } : {}),
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
routeEnds();
|
|
217
|
+
return { steps, says, marks, badges, vis, diags, kindsUsed };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** The point halfway along a polyline. */
|
|
221
|
+
export function midpoint(pts: P3[]): P3 {
|
|
222
|
+
const seg = pts.slice(1).map((q, i) => Math.hypot(q[0] - pts[i][0], q[1] - pts[i][1], q[2] - pts[i][2]));
|
|
223
|
+
let half = seg.reduce((a, b) => a + b, 0) / 2;
|
|
224
|
+
for (let i = 0; i < seg.length; i++) {
|
|
225
|
+
if (half <= seg[i] && seg[i] > 0) { const t = half / seg[i], a = pts[i], b = pts[i + 1]; return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t]; }
|
|
226
|
+
half -= seg[i];
|
|
227
|
+
}
|
|
228
|
+
return pts[0];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function dym(w: string, c: Iterable<string>): string {
|
|
232
|
+
const d = didYouMean(w, c);
|
|
233
|
+
return d ? ` (did you mean "${d}"?)` : '';
|
|
234
|
+
}
|