topotopo 0.1.0 → 0.3.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 +61 -12
- package/dist/cli.js +161 -23
- package/dist/graph/engine.js +18 -1
- package/dist/graph/params.d.ts +7 -1
- package/dist/graph/params.js +21 -8
- package/dist/graph/types.d.ts +28 -2
- package/dist/host/artifacts.d.ts +21 -0
- package/dist/host/artifacts.js +58 -0
- package/dist/host/export-targets.d.ts +33 -0
- package/dist/host/export-targets.js +84 -0
- package/dist/host/export_scene.py +200 -0
- package/dist/host/node.d.ts +8 -0
- package/dist/host/node.js +114 -0
- package/dist/host/types.d.ts +5 -0
- package/dist/inspect.js +5 -2
- package/dist/io/common.d.ts +6 -0
- package/dist/io/export.js +7 -4
- package/dist/io/gltf.d.ts +1 -1
- package/dist/io/gltf.js +167 -32
- package/dist/io/heightmap.d.ts +1 -1
- package/dist/io/import.d.ts +2 -2
- package/dist/io/import.js +10 -3
- package/dist/io/png-decode.d.ts +5 -2
- package/dist/io/png-decode.js +10 -3
- package/dist/io/targets.d.ts +31 -0
- package/dist/io/targets.js +75 -0
- package/dist/io/textures.d.ts +31 -2
- package/dist/io/textures.js +66 -18
- package/dist/io/unreal.js +22 -5
- package/dist/mcp/docs.d.ts +9 -2
- package/dist/mcp/docs.js +126 -2
- package/dist/mcp/inputs.d.ts +28 -0
- package/dist/mcp/inputs.js +377 -0
- package/dist/mcp/server.js +4 -1
- package/dist/mcp/tools.d.ts +1 -1
- package/dist/mcp/tools.js +118 -51
- package/dist/nodes/code.js +34 -6
- package/dist/nodes/copy.js +14 -4
- package/dist/nodes/flow.js +136 -35
- package/dist/nodes/heightfield.js +67 -11
- package/dist/nodes/texture.d.ts +1 -1
- package/dist/nodes/texture.js +61 -5
- package/dist/render/options.js +2 -1
- package/dist/render/raster.d.ts +2 -0
- package/dist/render/raster.js +3 -3
- package/dist/render/texview.d.ts +9 -1
- package/dist/render/texview.js +43 -2
- package/dist/viewer/app/api.js +64 -0
- package/dist/viewer/app/camera.js +250 -0
- package/dist/viewer/app/export-popover.js +68 -0
- package/dist/viewer/app/grid.js +217 -0
- package/dist/viewer/app/guides.js +402 -0
- package/dist/viewer/app/image-view.js +114 -0
- package/dist/viewer/app/keymap.js +199 -0
- package/dist/viewer/app/layers.js +244 -0
- package/dist/viewer/app/main.js +646 -0
- package/dist/viewer/app/markers.js +206 -0
- package/dist/viewer/app/mesh.js +172 -0
- package/dist/viewer/app/outliner.js +225 -0
- package/dist/viewer/app/outputs.css +73 -0
- package/dist/viewer/app/outputs.js +240 -0
- package/dist/viewer/app/props.js +432 -0
- package/dist/viewer/app/scene.js +529 -0
- package/dist/viewer/app/select.js +153 -0
- package/dist/viewer/app/selection.js +79 -0
- package/dist/viewer/app/status.js +20 -0
- package/dist/viewer/app/style.css +173 -0
- package/dist/viewer/app/undo.js +49 -0
- package/dist/viewer/app/units.js +51 -0
- package/dist/viewer/app/vars.js +64 -0
- package/dist/viewer/index.html +67 -184
- package/dist/viewer/outputs.d.ts +54 -0
- package/dist/viewer/outputs.js +436 -0
- package/dist/viewer/server.d.ts +47 -1
- package/dist/viewer/server.js +480 -17
- package/dist/workspace.d.ts +60 -4
- package/dist/workspace.js +131 -5
- package/package.json +9 -3
- package/shelf/brick_wall.json +1 -1
- package/shelf/wall_segment.json +2 -2
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Picking and selecting in the viewport (docs/design/viewer.md section 5): click selects what is under the cursor
|
|
2
|
+
// (a guide, else the nearest piece of the output or of a reference layer), Shift adds, Ctrl removes, a drag pulls a
|
|
3
|
+
// box that selects what lies wholly inside it, a click on nothing clears. Hover draws a dimmer highlight. All
|
|
4
|
+
// positions are viewport pixels.
|
|
5
|
+
import * as THREE from 'three';
|
|
6
|
+
import { opOf, parseId, pieceId } from './selection.js';
|
|
7
|
+
import { AXIS_VIEWS } from './camera.js';
|
|
8
|
+
|
|
9
|
+
const DRAG_PX = 4;
|
|
10
|
+
|
|
11
|
+
export function createSelect(ctx) {
|
|
12
|
+
const S = { press: null, boxEl: document.getElementById('boxsel'), hoverAt: null, hoverQueued: false };
|
|
13
|
+
const meshEntries = () => [...ctx.objects.values()].filter((e) => e.mesh && (ctx.isolated ? e.id === 'out' && !ctx.previewEmpty : e.visible) && e.mesh.root.visible);
|
|
14
|
+
|
|
15
|
+
/** What is under a viewport point in a slot: {id, point?} or null. Guides first (they draw on top). */
|
|
16
|
+
S.pick = (slot, x, y) => {
|
|
17
|
+
const g = ctx.isolated ? null : ctx.guides.pick(slot, x, y);
|
|
18
|
+
if (g) return { id: g.id };
|
|
19
|
+
const rc = ctx.views.ray(slot, x, y);
|
|
20
|
+
const linePx = ctx.views.worldPerPixel(slot) * 5;
|
|
21
|
+
let best = null;
|
|
22
|
+
for (const e of meshEntries()) {
|
|
23
|
+
if (e.pickable === false) continue;
|
|
24
|
+
const h = e.mesh.pick(rc, linePx);
|
|
25
|
+
if (h && h.dist < (best?.dist ?? Infinity)) best = { ...h, entry: e };
|
|
26
|
+
}
|
|
27
|
+
if (!best) return null;
|
|
28
|
+
const n = best.entry.mesh.pieceCount();
|
|
29
|
+
return { id: best.piece === null || n <= 1 ? best.entry.id : pieceId(best.entry.id, best.piece), point: best.point };
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** The construction plane of a slot's camera: y = 0 for persp, top and bottom; the view's own axis plane else. */
|
|
33
|
+
const planeOf = (slot) => {
|
|
34
|
+
const c = ctx.views.cam(slot), f = c.name === 'persp' ? [0, -1, 0] : AXIS_VIEWS[c.name].f;
|
|
35
|
+
return new THREE.Plane(new THREE.Vector3(Math.abs(f[0]), Math.abs(f[1]), Math.abs(f[2])), 0);
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Where a guide lands under a viewport point: the surface of the output or of a reference layer (not pictures,
|
|
39
|
+
* not the markers file), else the slot's construction plane. {point, surface} or null.
|
|
40
|
+
*/
|
|
41
|
+
S.surfacePoint = (slot, x, y) => {
|
|
42
|
+
const rc = ctx.views.ray(slot, x, y);
|
|
43
|
+
let best = null;
|
|
44
|
+
for (const e of meshEntries()) {
|
|
45
|
+
if (e.surface === false || e.mesh.bboxMode) continue;
|
|
46
|
+
const h = e.mesh.pick(rc, 0);
|
|
47
|
+
if (h && h.dist < (best?.dist ?? Infinity)) best = h;
|
|
48
|
+
}
|
|
49
|
+
if (best) return { point: best.point, surface: true };
|
|
50
|
+
const p = rc.ray.intersectPlane(planeOf(slot), new THREE.Vector3());
|
|
51
|
+
return p ? { point: p, surface: false } : null;
|
|
52
|
+
};
|
|
53
|
+
/** Navigation hits actual visible surfaces, never the infinite construction plane or selection bounds. */
|
|
54
|
+
S.navigationPoint = (slot, x, y) => {
|
|
55
|
+
const rc = ctx.views.ray(slot, x, y);
|
|
56
|
+
let best = null;
|
|
57
|
+
for (const e of meshEntries()) {
|
|
58
|
+
if (e.surface === false || e.mesh.picture || e.mesh.bboxMode) continue;
|
|
59
|
+
const h = e.mesh.pick(rc, 0);
|
|
60
|
+
if (h && h.dist < (best?.dist ?? Infinity)) best = h;
|
|
61
|
+
}
|
|
62
|
+
return best?.point?.clone() ?? null;
|
|
63
|
+
};
|
|
64
|
+
/** A viewport point on the horizontal plane at height `level`, through slot's camera. */
|
|
65
|
+
S.planePoint = (slot, x, y, level) => ctx.views.ray(slot, x, y).ray.intersectPlane(new THREE.Plane(new THREE.Vector3(0, 1, 0), -level), new THREE.Vector3());
|
|
66
|
+
|
|
67
|
+
// ------------------------------------------------------------------ click, box
|
|
68
|
+
S.down = (slot, x, y, e) => { S.press = { slot, x, y, op: opOf(e), box: false }; };
|
|
69
|
+
S.move = (x, y) => {
|
|
70
|
+
const p = S.press;
|
|
71
|
+
if (!p) return;
|
|
72
|
+
// as Maya's select tool: a left drag pulls a box wherever it starts (nothing moves by dragging in phase 1)
|
|
73
|
+
if (!p.box && Math.hypot(x - p.x, y - p.y) > DRAG_PX) p.box = true;
|
|
74
|
+
if (p.box) {
|
|
75
|
+
const r = ctx.views.slots[p.slot].rect;
|
|
76
|
+
const cx = Math.max(r.x, Math.min(r.x + r.w, x)), cy = Math.max(r.y, Math.min(r.y + r.h, y));
|
|
77
|
+
Object.assign(S.boxEl.style, { left: `${Math.min(p.x, cx)}px`, top: `${Math.min(p.y, cy)}px`, width: `${Math.abs(cx - p.x)}px`, height: `${Math.abs(cy - p.y)}px` });
|
|
78
|
+
S.boxEl.hidden = false;
|
|
79
|
+
p.end = { x: cx, y: cy };
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
S.up = (x, y) => {
|
|
83
|
+
const p = S.press;
|
|
84
|
+
S.press = null;
|
|
85
|
+
S.boxEl.hidden = true;
|
|
86
|
+
if (!p) return;
|
|
87
|
+
if (p.box) {
|
|
88
|
+
const e = p.end ?? { x, y };
|
|
89
|
+
return S.boxSelect(p.slot, { x0: Math.min(p.x, e.x), y0: Math.min(p.y, e.y), x1: Math.max(p.x, e.x), y1: Math.max(p.y, e.y) }, p.op);
|
|
90
|
+
}
|
|
91
|
+
const hit = S.pick(p.slot, x, y);
|
|
92
|
+
if (hit) ctx.selection.apply([hit.id], p.op);
|
|
93
|
+
else if (p.op === 'replace') ctx.selection.clear();
|
|
94
|
+
};
|
|
95
|
+
S.cancel = () => { S.press = null; S.boxEl.hidden = true; };
|
|
96
|
+
/** Everything wholly inside a viewport rect of a slot. */
|
|
97
|
+
S.boxSelect = (slot, rect, op = 'replace') => {
|
|
98
|
+
const cam = ctx.views.camera(slot);
|
|
99
|
+
const inside = (pts) => pts.every((w) => {
|
|
100
|
+
const q = ctx.views.project(slot, w, cam);
|
|
101
|
+
return q && q.x >= rect.x0 && q.x <= rect.x1 && q.y >= rect.y0 && q.y <= rect.y1;
|
|
102
|
+
});
|
|
103
|
+
const ids = [...ctx.guides.boxSelect(inside)];
|
|
104
|
+
for (const e of meshEntries()) {
|
|
105
|
+
if (e.pickable === false) continue;
|
|
106
|
+
const ks = e.mesh.boxPieces(inside);
|
|
107
|
+
const n = e.mesh.bboxMode ? 1 : e.mesh.pieceCount();
|
|
108
|
+
if (ks.length && (ks.includes(null) || n <= 1 || ks.length === n)) ids.push(e.id);
|
|
109
|
+
else for (const k of ks) ids.push(pieceId(e.id, k));
|
|
110
|
+
}
|
|
111
|
+
if (ids.length || op === 'replace') ctx.selection.apply(ids, op);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// ------------------------------------------------------------------ hover
|
|
115
|
+
/** Ask for the hover under (x, y) in a slot (picked at most every 16 ms), or clear it (slot -1). */
|
|
116
|
+
S.hover = (slot, x, y) => {
|
|
117
|
+
S.hoverAt = slot < 0 ? null : { slot, x, y };
|
|
118
|
+
if (S.hoverQueued) return;
|
|
119
|
+
S.hoverQueued = true;
|
|
120
|
+
setTimeout(() => {
|
|
121
|
+
S.hoverQueued = false;
|
|
122
|
+
const h = S.hoverAt && !S.press && !ctx.guides.mode ? S.pick(S.hoverAt.slot, S.hoverAt.x, S.hoverAt.y) : null;
|
|
123
|
+
ctx.setHover(h?.id ?? null);
|
|
124
|
+
}, 16);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// ------------------------------------------------------------------ highlights, bounds, pivot
|
|
128
|
+
/** Push the selection and the hover to the objects that draw them. */
|
|
129
|
+
S.highlight = () => {
|
|
130
|
+
for (const e of meshEntries()) {
|
|
131
|
+
const sel = { whole: ctx.selection.has(e.id), pieces: new Set() }, hov = { whole: ctx.hover === e.id, piece: null };
|
|
132
|
+
for (const id of ctx.selection.items) { const { obj, piece } = parseId(id); if (obj === e.id && piece !== null) sel.pieces.add(piece); }
|
|
133
|
+
if (ctx.hover && ctx.hover !== e.id) { const { obj, piece } = parseId(ctx.hover); if (obj === e.id) hov.piece = piece; }
|
|
134
|
+
e.mesh.highlight(sel, hov);
|
|
135
|
+
}
|
|
136
|
+
ctx.guides.draw();
|
|
137
|
+
};
|
|
138
|
+
/** Bounds of one id (an object or a piece), or null when it has no geometry on screen. */
|
|
139
|
+
S.boundsOf = (id) => {
|
|
140
|
+
const { obj, piece } = parseId(id), e = ctx.objects.get(obj);
|
|
141
|
+
if (!e) return null;
|
|
142
|
+
if (e.kind === 'guide') return ctx.guides.bounds(e.marker);
|
|
143
|
+
if (!e.mesh) return null;
|
|
144
|
+
const b = e.mesh.bounds(piece);
|
|
145
|
+
return b.isEmpty() ? null : b;
|
|
146
|
+
};
|
|
147
|
+
S.selectionBounds = () => {
|
|
148
|
+
const b = new THREE.Box3();
|
|
149
|
+
for (const id of ctx.selection.items) { const x = S.boundsOf(id); if (x) b.union(x); }
|
|
150
|
+
return b;
|
|
151
|
+
};
|
|
152
|
+
return S;
|
|
153
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// The selection the outliner and the viewport share (docs/design/viewer.md section 5): an ordered set of ids, the
|
|
2
|
+
// last one the lead (the properties panel shows it). Ids name objects ("out", "layer:<path>", "guide:<id>") or a
|
|
3
|
+
// connected piece of a mesh object ("out#12", "layer:<path>#3").
|
|
4
|
+
// Plain functions, tested in Node (test/viewer-logic.ts).
|
|
5
|
+
|
|
6
|
+
export const pieceId = (obj, k) => `${obj}#${k}`;
|
|
7
|
+
/** {obj, piece}: piece is the piece number, or null for the whole object. */
|
|
8
|
+
export function parseId(id) {
|
|
9
|
+
const i = id.lastIndexOf('#');
|
|
10
|
+
if (i < 0 || !/^\d+$/.test(id.slice(i + 1))) return { obj: id, piece: null };
|
|
11
|
+
return { obj: id.slice(0, i), piece: Number(id.slice(i + 1)) };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** How a click or a box combines with the selection: Shift adds, Ctrl removes (Ctrl+Shift adds), else replaces. */
|
|
15
|
+
export function opOf({ shiftKey = false, ctrlKey = false, metaKey = false } = {}) {
|
|
16
|
+
if (shiftKey) return 'add';
|
|
17
|
+
if (ctrlKey || metaKey) return 'remove';
|
|
18
|
+
return 'replace';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The ids after combining `ids` into `current` with op ('replace' | 'add' | 'remove' | 'toggle'). */
|
|
22
|
+
export function combine(current, ids, op) {
|
|
23
|
+
const seen = new Set();
|
|
24
|
+
const uniq = (list) => list.filter((x) => !seen.has(x) && seen.add(x));
|
|
25
|
+
if (op === 'replace') return uniq([...ids]);
|
|
26
|
+
if (op === 'add') {
|
|
27
|
+
// an added id becomes the lead: it moves to the end
|
|
28
|
+
const add = new Set(ids);
|
|
29
|
+
return uniq([...current.filter((x) => !add.has(x)), ...ids]);
|
|
30
|
+
}
|
|
31
|
+
if (op === 'remove') { const drop = new Set(ids); return current.filter((x) => !drop.has(x)); }
|
|
32
|
+
if (op === 'toggle') {
|
|
33
|
+
const cur = new Set(current), flip = new Set(ids);
|
|
34
|
+
return uniq([...current.filter((x) => !flip.has(x)), ...ids.filter((x) => !cur.has(x))]);
|
|
35
|
+
}
|
|
36
|
+
throw new Error(`unknown selection op "${op}"`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Normalise ids: a piece of an object that has one piece is the object; a piece whose object is selected whole adds
|
|
41
|
+
* nothing. `pieces(obj)` gives an object's piece count; without it pieces stay as they are.
|
|
42
|
+
*/
|
|
43
|
+
export function normalize(ids, pieces = () => Infinity) {
|
|
44
|
+
const whole = new Set(ids.filter((x) => parseId(x).piece === null));
|
|
45
|
+
const out = [];
|
|
46
|
+
for (const id of ids) {
|
|
47
|
+
const { obj, piece } = parseId(id);
|
|
48
|
+
if (piece === null) { out.push(id); continue; }
|
|
49
|
+
if (whole.has(obj)) continue;
|
|
50
|
+
if (pieces(obj) <= 1) { if (!out.includes(obj)) out.push(obj); whole.add(obj); continue; }
|
|
51
|
+
out.push(id);
|
|
52
|
+
}
|
|
53
|
+
return [...new Set(out)];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A selection with change listeners; `pieces(obj)` as for normalize. */
|
|
57
|
+
export function createSelection(pieces) {
|
|
58
|
+
let items = [];
|
|
59
|
+
const listeners = new Set();
|
|
60
|
+
const set = (next) => {
|
|
61
|
+
if (next.length === items.length && next.every((x, i) => x === items[i])) return false;
|
|
62
|
+
items = next;
|
|
63
|
+
for (const f of listeners) f(items);
|
|
64
|
+
return true;
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
get items() { return items; },
|
|
68
|
+
get lead() { return items[items.length - 1] ?? null; },
|
|
69
|
+
get size() { return items.length; },
|
|
70
|
+
has: (id) => items.includes(id),
|
|
71
|
+
/** true when the object or any of its pieces is selected */
|
|
72
|
+
touches: (obj) => items.some((x) => x === obj || parseId(x).obj === obj),
|
|
73
|
+
apply(ids, op = 'replace') { return set(normalize(combine(items, ids, op), pieces)); },
|
|
74
|
+
clear() { return set([]); },
|
|
75
|
+
/** drop ids whose object is gone (`exists(id)` false) */
|
|
76
|
+
prune(exists) { return set(items.filter(exists)); },
|
|
77
|
+
onChange(f) { listeners.add(f); return () => listeners.delete(f); },
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// The status bar's own lines (docs/design/viewer.md section 4): what the page says when nothing else is reported.
|
|
2
|
+
// Plain functions, tested in Node (test/viewer-logic.ts).
|
|
3
|
+
|
|
4
|
+
/** While the event stream is connected. */
|
|
5
|
+
export const LIVE_LINE = 'live — updates on every build, edit and file save';
|
|
6
|
+
/** While the workspace has no graph. */
|
|
7
|
+
export const NO_GRAPHS_LINE = 'no graphs yet — build one via MCP';
|
|
8
|
+
|
|
9
|
+
/** The line when the event stream (re)connects: the hint while there is no graph, else the live line. */
|
|
10
|
+
export const connectedLine = (graphCount) => (graphCount ? LIVE_LINE : NO_GRAPHS_LINE);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The line after the graph list was read, or null to keep the one shown: the hint while there is no graph, and the
|
|
14
|
+
* live line in its place once the first graph arrives. Any other line (an undo, a keymap note, an error) stays.
|
|
15
|
+
* Whichever of the two runs last at startup, the page ends on the line that fits the workspace.
|
|
16
|
+
*/
|
|
17
|
+
export function graphsLine(shown, graphCount) {
|
|
18
|
+
if (!graphCount) return NO_GRAPHS_LINE;
|
|
19
|
+
return shown === NO_GRAPHS_LINE ? LIVE_LINE : null;
|
|
20
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/* The viewer page (docs/design/viewer.md): a neutral editor UI around the viewport. Section 7 colours: viewport
|
|
2
|
+
#2b2d31, selection orange #f0a030, guides one desaturated cyan. */
|
|
3
|
+
:root {
|
|
4
|
+
--bg: #1f2124; --panel: #26282c; --panel2: #2c2f33; --line: #3a3d42; --text: #d6d9dd; --dim: #8c939b; --faint: #5d636b;
|
|
5
|
+
--view: #2b2d31; --sel: #f0a030; --sel-bg: rgba(240, 160, 48, 0.16); --hover-bg: #31353a; --guide: #7fb4c1;
|
|
6
|
+
--err: #e5675f; --ok: #8fbf7f; --x: #d9534f; --y: #6abf4b; --z: #4a86d9;
|
|
7
|
+
--left: 260px; --right: 320px; --split: 45%;
|
|
8
|
+
--font: 12px/1.35 system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
9
|
+
--mono: 11px/1.35 ui-monospace, Consolas, "SF Mono", monospace;
|
|
10
|
+
}
|
|
11
|
+
* { box-sizing: border-box; }
|
|
12
|
+
[hidden] { display: none !important; }
|
|
13
|
+
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: var(--font); overflow: hidden; }
|
|
14
|
+
#app {
|
|
15
|
+
display: grid; height: 100%;
|
|
16
|
+
/* the panels give way in a narrow window so the viewport keeps room */
|
|
17
|
+
grid-template-columns: min(var(--left), 22vw) minmax(200px, 1fr) min(var(--right), 28vw);
|
|
18
|
+
grid-template-rows: 30px minmax(0, 1fr) 22px;
|
|
19
|
+
grid-template-areas: "top top top" "left view right" "status status right";
|
|
20
|
+
}
|
|
21
|
+
select, button, input { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 2px; }
|
|
22
|
+
select { padding: 1px 2px; }
|
|
23
|
+
button { padding: 1px 8px; cursor: pointer; }
|
|
24
|
+
button:hover { border-color: var(--faint); }
|
|
25
|
+
button.on { border-color: var(--sel); color: var(--sel); }
|
|
26
|
+
input { padding: 2px 4px; }
|
|
27
|
+
input:focus, select:focus { outline: 1px solid var(--faint); outline-offset: -1px; }
|
|
28
|
+
|
|
29
|
+
/* top bar */
|
|
30
|
+
#top { grid-area: top; display: flex; align-items: center; gap: 10px; padding: 0 8px; background: var(--panel); border-bottom: 1px solid var(--line); white-space: nowrap; }
|
|
31
|
+
#top .tb { display: flex; align-items: center; gap: 4px; color: var(--dim); }
|
|
32
|
+
#top .tb select { color: var(--text); max-width: 180px; }
|
|
33
|
+
#top .readout { font: var(--mono); color: var(--dim); min-width: 84px; }
|
|
34
|
+
#top .mode { color: var(--sel); }
|
|
35
|
+
.grow { flex: 1; }
|
|
36
|
+
|
|
37
|
+
/* panels */
|
|
38
|
+
.panel { position: relative; display: flex; flex-direction: column; min-width: 0; background: var(--panel); }
|
|
39
|
+
#outliner { grid-area: left; border-right: 1px solid var(--line); }
|
|
40
|
+
#props { grid-area: right; border-left: 1px solid var(--line); }
|
|
41
|
+
.phead { flex: none; padding: 5px 8px; color: var(--dim); font-size: 11px; letter-spacing: .06em; text-transform: uppercase; border-bottom: 1px solid var(--line); user-select: none; display: flex; gap: 6px; }
|
|
42
|
+
.phead .count { margin-left: auto; letter-spacing: 0; }
|
|
43
|
+
.pscroll { overflow: auto; min-height: 0; }
|
|
44
|
+
#tree { flex: 1; }
|
|
45
|
+
#props-body { flex: 0 0 var(--split); }
|
|
46
|
+
#controls { flex: 1; }
|
|
47
|
+
.grip { position: absolute; z-index: 5; }
|
|
48
|
+
.grip.v { top: 0; bottom: 0; width: 6px; cursor: col-resize; }
|
|
49
|
+
#outliner .grip.v { right: -3px; }
|
|
50
|
+
#props .grip.v { left: -3px; }
|
|
51
|
+
.grip.h { position: relative; flex: none; height: 5px; margin: -2px 0; cursor: row-resize; }
|
|
52
|
+
.grip:hover, .grip.drag { background: rgba(240, 160, 48, 0.35); }
|
|
53
|
+
|
|
54
|
+
/* outliner tree */
|
|
55
|
+
.sec > .sh { display: flex; align-items: center; gap: 4px; height: 22px; padding: 0 6px; color: var(--dim); cursor: pointer; user-select: none; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
|
|
56
|
+
.sec > .sh::before { content: '▾'; width: 10px; color: var(--faint); }
|
|
57
|
+
.sec.closed > .sh::before { content: '▸'; }
|
|
58
|
+
.sec.closed > .rows { display: none; }
|
|
59
|
+
.sec > .sh .count { margin-left: auto; letter-spacing: 0; }
|
|
60
|
+
.row { display: flex; align-items: center; gap: 4px; height: 22px; padding: 0 6px 0 16px; cursor: default; user-select: none; border-left: 2px solid transparent; }
|
|
61
|
+
.row:hover { background: var(--hover-bg); }
|
|
62
|
+
.row.sel { background: var(--sel-bg); border-left-color: var(--sel); }
|
|
63
|
+
.row.part { border-left-color: rgba(240, 160, 48, 0.5); }
|
|
64
|
+
.row .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
65
|
+
.row .mt { color: var(--dim); font-size: 11px; white-space: nowrap; }
|
|
66
|
+
.row .err { color: var(--err); }
|
|
67
|
+
.row.err .nm { color: var(--err); }
|
|
68
|
+
.row .eye { flex: 0 0 16px; width: 16px; height: 16px; padding: 0; border: 0; background: none; display: grid; place-items: center; cursor: pointer; color: var(--dim); }
|
|
69
|
+
.row .eye svg { width: 14px; height: 14px; }
|
|
70
|
+
.row .eye.off { color: var(--faint); }
|
|
71
|
+
.row .eye.off .pupil { display: none; }
|
|
72
|
+
.row .eye.off .slash { display: inline; }
|
|
73
|
+
.row .eye .slash { display: none; }
|
|
74
|
+
.row select.disp { flex: none; width: 74px; padding: 0; font-size: 11px; color: var(--dim); background: transparent; border-color: transparent; }
|
|
75
|
+
.row select.disp:hover, .row select.disp:focus { border-color: var(--line); background: var(--panel2); }
|
|
76
|
+
.row .flag { flex: 0 0 10px; width: 10px; height: 10px; border: 1px solid var(--faint); border-radius: 1px; }
|
|
77
|
+
.row .flag.on { background: #4a86d9; border-color: #4a86d9; }
|
|
78
|
+
.row .kind { flex: 0 0 auto; color: var(--dim); font-size: 11px; width: 42px; }
|
|
79
|
+
.note { padding: 2px 8px 6px 18px; color: var(--dim); font-size: 11px; overflow-wrap: anywhere; }
|
|
80
|
+
.rowerr { padding: 0 8px 4px 38px; color: var(--err); font-size: 11px; white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
81
|
+
|
|
82
|
+
/* viewport */
|
|
83
|
+
#viewport { grid-area: view; position: relative; overflow: hidden; background: var(--view); }
|
|
84
|
+
#gl { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
|
|
85
|
+
#views { position: absolute; inset: 0; pointer-events: none; }
|
|
86
|
+
.vw { position: absolute; overflow: hidden; border: 1px solid transparent; }
|
|
87
|
+
.vw.quad { border-color: #1b1c1f; }
|
|
88
|
+
.vw.active.quad { border-color: #55595f; }
|
|
89
|
+
.vw .vname { position: absolute; left: 8px; top: 6px; color: #c9ccd0; font: var(--font); text-shadow: 0 1px 1px #000a; }
|
|
90
|
+
.vw .vname .dim { color: var(--dim); }
|
|
91
|
+
.vw .vinfo { position: absolute; right: 8px; top: 6px; text-align: right; color: var(--dim); font: var(--mono); text-shadow: 0 1px 1px #000a; white-space: pre; }
|
|
92
|
+
.vw .vinfo .selsize { color: var(--sel); }
|
|
93
|
+
.vw svg.gizmo { position: absolute; left: 6px; bottom: 6px; width: 64px; height: 64px; pointer-events: auto; overflow: visible; }
|
|
94
|
+
.vw svg.gizmo .end { cursor: pointer; }
|
|
95
|
+
.vw svg.gizmo text { font: 600 10px system-ui, sans-serif; fill: #1b1c1f; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
|
|
96
|
+
.vw svg.marks { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
|
|
97
|
+
.vw .lbl { position: absolute; transform: translate(8px, -50%); padding: 0 4px; font: var(--font); color: var(--text); background: rgba(31, 33, 36, 0.78); border-radius: 2px; white-space: nowrap; }
|
|
98
|
+
.vw .lbl.sel { color: var(--sel); }
|
|
99
|
+
.vw .lbl.dimen { transform: translate(-50%, -130%); font: var(--mono); color: var(--sel); }
|
|
100
|
+
#boxsel { position: absolute; border: 1px dashed var(--sel); background: rgba(240, 160, 48, 0.06); pointer-events: none; }
|
|
101
|
+
#viewport.placing #gl { cursor: crosshair; }
|
|
102
|
+
#viewport.nav #gl { cursor: move; }
|
|
103
|
+
|
|
104
|
+
/* properties */
|
|
105
|
+
.pgroup { padding: 6px 8px 8px; border-bottom: 1px solid var(--line); }
|
|
106
|
+
.pgroup h3 { margin: 0 0 4px; font: inherit; color: var(--dim); font-size: 11px; text-transform: uppercase; letter-spacing: .05em; font-weight: normal; display: flex; gap: 6px; align-items: center; }
|
|
107
|
+
.pgroup h3 .grow { flex: 1; }
|
|
108
|
+
.prow { display: grid; grid-template-columns: 78px 1fr; align-items: center; gap: 6px; min-height: 22px; }
|
|
109
|
+
.prow > .pl { color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
110
|
+
.prow > .pv { min-width: 0; overflow-wrap: anywhere; }
|
|
111
|
+
.prow input[type=text], .prow input[type=number] { width: 100%; }
|
|
112
|
+
.xyz { display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; }
|
|
113
|
+
.xyz input { width: 100%; font: var(--mono); padding: 1px 3px; }
|
|
114
|
+
.xyz input[readonly] { color: var(--dim); background: transparent; border-color: #34373c; }
|
|
115
|
+
.xyz input:nth-child(1)[readonly] { border-bottom-color: var(--x); }
|
|
116
|
+
.xyz input:nth-child(2)[readonly] { border-bottom-color: var(--y); }
|
|
117
|
+
.xyz input:nth-child(3)[readonly] { border-bottom-color: var(--z); }
|
|
118
|
+
.mono { font: var(--mono); }
|
|
119
|
+
.attrs { width: 100%; border-collapse: collapse; font: var(--mono); }
|
|
120
|
+
.attrs td { padding: 1px 4px 1px 0; vertical-align: top; overflow-wrap: anywhere; }
|
|
121
|
+
.attrs td:first-child { color: var(--dim); white-space: nowrap; }
|
|
122
|
+
.summary { margin: 0; font: var(--mono); color: var(--text); white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
123
|
+
.hint { color: var(--dim); font-size: 11px; overflow-wrap: anywhere; }
|
|
124
|
+
.bad { border-color: var(--err) !important; }
|
|
125
|
+
.danger:hover { border-color: var(--err); color: var(--err); }
|
|
126
|
+
|
|
127
|
+
/* controls (exposed vars) */
|
|
128
|
+
#controls { padding: 4px 6px 8px; }
|
|
129
|
+
.ctl { display: flex; align-items: center; gap: 4px; min-height: 26px; padding: 1px 2px; }
|
|
130
|
+
.ctl:hover { background: var(--hover-bg); }
|
|
131
|
+
.ctl > .cl { flex: 0 0 92px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
132
|
+
.ctl > .cw { flex: 1; display: flex; gap: 4px; align-items: center; min-width: 0; }
|
|
133
|
+
.cw input { padding: 1px 4px; }
|
|
134
|
+
.cw input[type=range] { flex: 1; min-width: 40px; padding: 0; border: 0; background: none; accent-color: var(--sel); }
|
|
135
|
+
.cw input[type=number] { width: 64px; }
|
|
136
|
+
.cw input.vec { flex: 1; width: 0; min-width: 0; }
|
|
137
|
+
.cw input[type=text] { flex: 1; width: 0; min-width: 0; }
|
|
138
|
+
.cw input[type=checkbox] { accent-color: var(--sel); margin: 0; }
|
|
139
|
+
.cw input[type=color] { width: 44px; height: 20px; padding: 0 2px; }
|
|
140
|
+
.cw select { flex: 1; width: 0; min-width: 0; }
|
|
141
|
+
.ib { flex: 0 0 20px; width: 20px; height: 20px; padding: 0; border: 0; background: none; color: var(--dim); cursor: pointer; display: grid; place-items: center; font-size: 13px; line-height: 1; }
|
|
142
|
+
.ib:hover:not(:disabled) { color: var(--text); background: var(--hover-bg); }
|
|
143
|
+
.ib:disabled { opacity: .25; cursor: default; }
|
|
144
|
+
.ib.gone { visibility: hidden; }
|
|
145
|
+
.sub { padding: 6px 2px 2px; color: var(--dim); font-size: 11px; }
|
|
146
|
+
.none { padding: 4px 2px; color: var(--dim); font-size: 11px; }
|
|
147
|
+
|
|
148
|
+
/* status bar */
|
|
149
|
+
#status { grid-area: status; display: flex; align-items: center; gap: 8px; padding: 0 8px; background: var(--panel); border-top: 1px solid var(--line); white-space: nowrap; overflow: hidden; color: var(--dim); }
|
|
150
|
+
#msg { overflow: hidden; text-overflow: ellipsis; min-width: 0; }
|
|
151
|
+
#msg.err { color: var(--err); }
|
|
152
|
+
#msg.ok { color: var(--ok); }
|
|
153
|
+
#msg.busy { color: var(--sel); }
|
|
154
|
+
#cook { overflow: hidden; text-overflow: ellipsis; min-width: 0; font: var(--mono); }
|
|
155
|
+
#cook .err { color: var(--err); }
|
|
156
|
+
#ask { display: flex; gap: 6px; align-items: center; color: var(--text); }
|
|
157
|
+
#ask[hidden] { display: none; }
|
|
158
|
+
#ask button { padding: 0 8px; }
|
|
159
|
+
|
|
160
|
+
/* drop zone */
|
|
161
|
+
#drop { position: fixed; inset: 8px; z-index: 20; display: none; align-items: center; justify-content: center; text-align: center; border: 2px dashed var(--sel); background: rgba(240, 160, 48, 0.06); color: var(--sel); font-size: 15px; pointer-events: none; }
|
|
162
|
+
body.dropping #drop { display: flex; }
|
|
163
|
+
|
|
164
|
+
/* keymap overlay */
|
|
165
|
+
.keymap-overlay { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; background: rgba(0, 0, 0, 0.45); }
|
|
166
|
+
.km-panel { max-height: 90vh; overflow: auto; background: var(--panel); border: 1px solid var(--line); padding: 10px 14px; min-width: 420px; }
|
|
167
|
+
.km-head { color: var(--text); margin-bottom: 6px; font-weight: 600; }
|
|
168
|
+
.km-panel table { border-collapse: collapse; }
|
|
169
|
+
.km-panel td { padding: 2px 16px 2px 0; }
|
|
170
|
+
.km-panel td:first-child { color: var(--dim); }
|
|
171
|
+
.km-panel td:last-child { font: var(--mono); color: var(--text); }
|
|
172
|
+
.km-panel tr.later td { color: var(--faint); }
|
|
173
|
+
.km-foot { margin-top: 8px; color: var(--dim); font-size: 11px; }
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// The undo stack of the person's own edits (guides and vars). An entry is {label, undo(), redo(), key?}: undo and
|
|
2
|
+
// redo apply a state the page already knows, so they never ask the server for anything first. Consecutive entries
|
|
3
|
+
// with the same key within `mergeMs` (typing a radius, nudging one control) merge into one step.
|
|
4
|
+
// Plain functions, tested in Node (test/viewer-logic.ts).
|
|
5
|
+
|
|
6
|
+
export function createUndo({ limit = 200, mergeMs = 1500, now = () => Date.now() } = {}) {
|
|
7
|
+
const done = [], undone = [];
|
|
8
|
+
const listeners = new Set();
|
|
9
|
+
const changed = () => { for (const f of listeners) f(); };
|
|
10
|
+
return {
|
|
11
|
+
/** Record an edit that has been applied already. */
|
|
12
|
+
push(entry) {
|
|
13
|
+
const top = done[done.length - 1], t = now();
|
|
14
|
+
if (entry.key !== undefined && top && top.key === entry.key && t - top.at <= mergeMs && !undone.length) {
|
|
15
|
+
top.redo = entry.redo;
|
|
16
|
+
top.label = entry.label;
|
|
17
|
+
top.at = t;
|
|
18
|
+
} else {
|
|
19
|
+
done.push({ ...entry, at: t });
|
|
20
|
+
if (done.length > limit) done.shift();
|
|
21
|
+
}
|
|
22
|
+
undone.length = 0;
|
|
23
|
+
changed();
|
|
24
|
+
},
|
|
25
|
+
/** Undo the last edit; its label, or null when there is none. */
|
|
26
|
+
undo() {
|
|
27
|
+
const e = done.pop();
|
|
28
|
+
if (!e) return null;
|
|
29
|
+
e.undo();
|
|
30
|
+
undone.push(e);
|
|
31
|
+
changed();
|
|
32
|
+
return e.label;
|
|
33
|
+
},
|
|
34
|
+
redo() {
|
|
35
|
+
const e = undone.pop();
|
|
36
|
+
if (!e) return null;
|
|
37
|
+
e.redo();
|
|
38
|
+
// a redone step never merges with the next edit
|
|
39
|
+
done.push({ ...e, at: -Infinity });
|
|
40
|
+
changed();
|
|
41
|
+
return e.label;
|
|
42
|
+
},
|
|
43
|
+
get canUndo() { return done.length > 0; },
|
|
44
|
+
get canRedo() { return undone.length > 0; },
|
|
45
|
+
get size() { return done.length; },
|
|
46
|
+
clear() { done.length = 0; undone.length = 0; changed(); },
|
|
47
|
+
onChange(f) { listeners.add(f); return () => listeners.delete(f); },
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Units on screen: the adaptive grid's step and how lengths and coordinates are printed (metres, Y up).
|
|
2
|
+
// Plain functions, tested in Node (test/viewer-logic.ts).
|
|
3
|
+
|
|
4
|
+
/** The grid steps the views choose from (docs/design/viewer.md section 4), in metres. */
|
|
5
|
+
export const GRID_STEPS = [0.01, 0.1, 1, 10, 100, 1000];
|
|
6
|
+
/** Major lines every this many steps. */
|
|
7
|
+
export const MAJOR_EVERY = 10;
|
|
8
|
+
/** A minor cell is at least this many pixels wide. */
|
|
9
|
+
export const MIN_CELL_PX = 8;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The step for a view showing `worldPerPixel` metres per pixel around its target: the smallest step whose cells are
|
|
13
|
+
* at least MIN_CELL_PX wide, clamped to GRID_STEPS.
|
|
14
|
+
*/
|
|
15
|
+
export function gridStep(worldPerPixel, minPx = MIN_CELL_PX) {
|
|
16
|
+
if (!(worldPerPixel > 0) || !Number.isFinite(worldPerPixel)) return 1;
|
|
17
|
+
for (const s of GRID_STEPS) if (s / worldPerPixel >= minPx) return s;
|
|
18
|
+
return GRID_STEPS[GRID_STEPS.length - 1];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** "1 m", "0.01 m", "1000 m". */
|
|
22
|
+
export const fmtStep = (s) => `${+s.toPrecision(6)} m`;
|
|
23
|
+
|
|
24
|
+
/** Decimals that show a coordinate to a hundredth of the grid step (at least 0, at most 4). */
|
|
25
|
+
export const decimalsFor = (step) => Math.max(0, Math.min(4, Math.ceil(-Math.log10(step)) + 2));
|
|
26
|
+
|
|
27
|
+
/** A number with fixed decimals, without "-0". */
|
|
28
|
+
export function fmtNum(x, d = 2) {
|
|
29
|
+
if (!Number.isFinite(x)) return String(x);
|
|
30
|
+
const s = x.toFixed(d);
|
|
31
|
+
return /^-0(\.0*)?$/.test(s) ? s.slice(1) : s;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A length in metres with decimals that suit its size: 0.0042 m, 0.85 m, 12.3 m, 1250 m. */
|
|
35
|
+
export function fmtLen(m) {
|
|
36
|
+
const a = Math.abs(m);
|
|
37
|
+
const d = a === 0 ? 0 : a < 0.01 ? 4 : a < 1 ? 3 : a < 10 ? 2 : a < 1000 ? 1 : 0;
|
|
38
|
+
return fmtNum(m, d);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A box's size, "1.20 × 0.80 × 1.10 m". */
|
|
42
|
+
export const fmtSize = (size) => `${size.map(fmtLen).join(' × ')} m`;
|
|
43
|
+
|
|
44
|
+
/** A point, "(1.20, 0.00, -3.45)", to a hundredth of `step`. */
|
|
45
|
+
export const fmtPoint = (p, step = 1) => `(${p.map((x) => fmtNum(x, decimalsFor(step))).join(', ')})`;
|
|
46
|
+
|
|
47
|
+
/** File sizes. */
|
|
48
|
+
export const fmtBytes = (n) => (n < 1024 ? `${n} B` : n < 1024 ** 2 ? `${(n / 1024).toFixed(1)} KB` : `${(n / 1024 ** 2).toFixed(1)} MB`);
|
|
49
|
+
|
|
50
|
+
/** Counts with thousands separators, the same on every machine. */
|
|
51
|
+
export const fmtCount = (n) => String(Math.round(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// The Controls section's rules (docs/design/viewer.md section 8): which widget a var gets, a slider's range and step,
|
|
2
|
+
// how values print. Plain functions, tested in Node (test/viewer-logic.ts); props.js builds the widgets.
|
|
3
|
+
|
|
4
|
+
export const isNum = (v) => typeof v === 'number' && Number.isFinite(v);
|
|
5
|
+
export const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
6
|
+
export const HEX = /^#[0-9a-f]{6}$/i, HEX3 = /^#[0-9a-f]{3}$/i;
|
|
7
|
+
export const decimals = (x) => { const s = String(x); return s.includes('e-') ? Number(s.split('e-')[1]) : (s.split('.')[1] ?? '').length; };
|
|
8
|
+
export const fmt = (v) => (isNum(v) ? String(+v.toPrecision(10)) : String(v));
|
|
9
|
+
export const short = (v) => { const s = typeof v === 'string' ? v : JSON.stringify(v) ?? String(v); return s.length > 24 ? s.slice(0, 23) + '…' : s; };
|
|
10
|
+
|
|
11
|
+
/** The widget a value's JSON type asks for. */
|
|
12
|
+
export function jsonKind(v) {
|
|
13
|
+
if (typeof v === 'boolean') return 'bool';
|
|
14
|
+
if (isNum(v)) return 'number';
|
|
15
|
+
if (typeof v === 'string') return HEX.test(v) ? 'color' : 'text';
|
|
16
|
+
if (Array.isArray(v) && v.length >= 2 && v.length <= 4 && v.every(isNum)) return 'vector';
|
|
17
|
+
return 'json';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The widget for a var: the declared type when the value fits it, else the value's JSON type. */
|
|
21
|
+
export function kindOf(vd, v) {
|
|
22
|
+
const t = vd?.type, k = jsonKind(v);
|
|
23
|
+
if (Array.isArray(vd?.options) && vd.options.length && (k === 'text' || k === 'number' || k === 'color')) return 'select';
|
|
24
|
+
if (t === 'color' && (k === 'color' || (k === 'text' && HEX3.test(v)) || (k === 'vector' && v.length >= 3))) return 'color';
|
|
25
|
+
if (t === 'bool' && k === 'number') return 'bool';
|
|
26
|
+
return k;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A value to show for a declared param that has neither a var nor a default. */
|
|
30
|
+
export function blank(vd) {
|
|
31
|
+
const t = vd?.type ?? '';
|
|
32
|
+
if (t === 'bool') return false;
|
|
33
|
+
if (t === 'color') return '#ffffff';
|
|
34
|
+
if (/vec(\d)$/.test(t)) return new Array(Number(/vec(\d)$/.exec(t)[1])).fill(0);
|
|
35
|
+
if (t === 'string' || t === 'enum' || vd?.options?.length) return vd?.options?.[0] ?? '';
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Slider range: the VarDef's min/max, else derived from the default (0..4×default, −1..1 for 0); derived ends widen. */
|
|
40
|
+
export function rangeOf(vd, v, int) {
|
|
41
|
+
let lo = isNum(vd.min) ? vd.min : undefined, hi = isNum(vd.max) ? vd.max : undefined;
|
|
42
|
+
const fixLo = lo !== undefined, fixHi = hi !== undefined;
|
|
43
|
+
const base = isNum(vd.default) ? vd.default : v;
|
|
44
|
+
if (lo === undefined && hi === undefined) [lo, hi] = base === 0 ? [-1, 1] : base > 0 ? [0, 4 * base] : [4 * base, -4 * base];
|
|
45
|
+
else if (lo === undefined) lo = hi > 0 && base >= 0 ? 0 : Math.min(base, hi) - Math.max(1, 4 * Math.abs(base));
|
|
46
|
+
else if (hi === undefined) hi = base > lo ? lo + 4 * (base - lo) : lo + Math.max(1, 4 * Math.abs(lo));
|
|
47
|
+
if (int) { lo = Math.floor(lo); hi = Math.ceil(hi); }
|
|
48
|
+
if (!(hi > lo)) hi = lo + 1;
|
|
49
|
+
const fixStep = isNum(vd.step) && vd.step > 0;
|
|
50
|
+
const r = { lo, hi, fixLo, fixHi, fixStep, int, step: 1 };
|
|
51
|
+
r.step = fixStep ? vd.step : autoStep(r);
|
|
52
|
+
return r;
|
|
53
|
+
}
|
|
54
|
+
export function autoStep(r) {
|
|
55
|
+
const s = 10 ** Math.floor(Math.log10((r.hi - r.lo) / 100));
|
|
56
|
+
return r.int ? Math.max(1, s) : s;
|
|
57
|
+
}
|
|
58
|
+
/** A slider value snapped to its range's steps. */
|
|
59
|
+
export const snapTo = (r, v) => +(r.lo + Math.round((v - r.lo) / r.step) * r.step).toFixed(Math.min(12, Math.max(decimals(r.step), decimals(r.lo))));
|
|
60
|
+
|
|
61
|
+
/** The controls a graph draws: declared params in order, then the undeclared vars ("other vars"). */
|
|
62
|
+
export function controlList(params = {}, vars = {}) {
|
|
63
|
+
return { declared: Object.keys(params), other: Object.keys(vars).filter((k) => !(k in params)) };
|
|
64
|
+
}
|