@margoengine/inspector 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +622 -2
- package/dist/animation.d.ts +53 -0
- package/dist/animation.js +234 -0
- package/dist/color-D6qetPCJ.js +10 -0
- package/dist/color.d.ts +4 -0
- package/dist/debug-geometry-D61TEkP9.js +61 -0
- package/dist/debug-geometry.d.ts +15 -0
- package/dist/edit-state-Dm3G17js.js +4 -0
- package/dist/edit-state.d.ts +3 -0
- package/dist/edit.d.ts +105 -0
- package/dist/edit.js +379 -0
- package/dist/errors-D8rc8kzg.js +7 -0
- package/dist/frame-panel.d.ts +16 -0
- package/dist/frame.d.ts +264 -0
- package/dist/frame.js +881 -0
- package/dist/gizmo.d.ts +49 -0
- package/dist/gizmo.js +131 -0
- package/dist/gl.d.ts +89 -0
- package/dist/gl.js +284 -0
- package/dist/history.d.ts +65 -0
- package/dist/history.js +66 -0
- package/dist/index.d.ts +122 -0
- package/dist/index.js +490 -0
- package/dist/mesh-debug.d.ts +115 -0
- package/dist/mesh-debug.js +501 -0
- package/dist/mesh-source-BSQA_1GQ.js +31 -0
- package/dist/packages.d.ts +98 -0
- package/dist/packages.js +337 -0
- package/dist/remote-relay.d.ts +25 -0
- package/dist/remote-relay.js +64 -0
- package/dist/remote.d.ts +175 -0
- package/dist/remote.js +349 -0
- package/dist/resources.d.ts +30 -0
- package/dist/resources.js +42 -0
- package/dist/shaders.d.ts +122 -0
- package/dist/shaders.js +285 -0
- package/dist/snapshot.d.ts +47 -0
- package/dist/snapshot.js +90 -0
- package/dist/textures.d.ts +127 -0
- package/dist/textures.js +380 -0
- package/dist/tools.d.ts +101 -0
- package/dist/tools.js +220 -0
- package/dist/tweak.d.ts +86 -0
- package/dist/tweak.js +291 -0
- package/dist/view-rect-CTC0iGjf.js +23 -0
- package/dist/visibility-7_j5J9Nv.js +7 -0
- package/package.json +147 -4
package/dist/tools.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const t=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./errors-D8rc8kzg.js";
|
|
5
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
6
|
+
//#region src/tools.ts
|
|
7
|
+
function n(e) {
|
|
8
|
+
return {
|
|
9
|
+
position: [...e.position],
|
|
10
|
+
target: [...e.target],
|
|
11
|
+
up: [...e.up],
|
|
12
|
+
fov: e.fov,
|
|
13
|
+
near: e.near,
|
|
14
|
+
far: e.far,
|
|
15
|
+
project: e.project
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function r(e, t) {
|
|
19
|
+
return t.throwIfAborted(), new Promise((n, r) => {
|
|
20
|
+
let i = () => r(t.reason);
|
|
21
|
+
t.addEventListener("abort", i, { once: !0 }), e.then((e) => {
|
|
22
|
+
t.removeEventListener("abort", i), n(e);
|
|
23
|
+
}, (e) => {
|
|
24
|
+
t.removeEventListener("abort", i), r(e);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
var i = 0, a = class a {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
29
|
+
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
30
|
+
let n = new a(e, t);
|
|
31
|
+
try {
|
|
32
|
+
return e.use(n), n;
|
|
33
|
+
} catch (e) {
|
|
34
|
+
throw n.dispose(), e;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
constructor(e, r = {}) {
|
|
38
|
+
if (this.engine = e, this.options = r, this.name = `inspector:tools:${++i}`, this.cameras = /* @__PURE__ */ new Map(), this.modelIds = /* @__PURE__ */ new WeakMap(), this.nextModel = 0, this.active = "default", this.selection = 0, this.signature = "", this.disposed = !1, this.starting = !1, this.controller = new AbortController(), this.urls = /* @__PURE__ */ new Set(), this.abort = () => this.dispose(), r.signal?.throwIfAborted(), (r.cameras ?? []).some((e) => !e?.id || e.id === "default" || e.id.startsWith("model:") || !e.camera || typeof e.label != "string")) throw TypeError("Use named application cameras with unreserved ids");
|
|
39
|
+
this.runtime = t(e), this.home = n(e.camera);
|
|
40
|
+
let a = e.canvas.ownerDocument;
|
|
41
|
+
this.element = a.createElement("fieldset");
|
|
42
|
+
let o = a.createElement("legend");
|
|
43
|
+
o.textContent = "Cameras and capture", this.element.append(o);
|
|
44
|
+
let s = (e) => {
|
|
45
|
+
let t = a.createElement("label"), n = a.createElement("select");
|
|
46
|
+
return t.textContent = e, n.setAttribute("aria-label", e), t.append(n), this.element.append(t), n;
|
|
47
|
+
};
|
|
48
|
+
this.picker = s("Camera"), this.picker.addEventListener("change", () => this.action(() => this.select(this.picker.value))), this.scope = s("Export scope"), this.format = s("Export format");
|
|
49
|
+
for (let [e, t] of [["glb", "Binary glTF (.glb)"], ["gltf", "glTF JSON (.gltf)"]]) {
|
|
50
|
+
let n = a.createElement("option");
|
|
51
|
+
n.value = e, n.textContent = t, this.format.append(n);
|
|
52
|
+
}
|
|
53
|
+
let c = (e, t) => {
|
|
54
|
+
let n = a.createElement("button");
|
|
55
|
+
return n.type = "button", n.textContent = e, n.addEventListener("click", () => this.action(t)), this.element.append(n), n;
|
|
56
|
+
};
|
|
57
|
+
c("Screenshot", async () => this.download(await this.screenshot(), "screenshot.png")), this.recordButton = c("Record canvas", () => this.startRecording()), this.stopButton = c("Stop recording", async () => this.download(await this.stopRecording(), "recording.webm")), c("Export glTF", async () => {
|
|
58
|
+
let e = this.format.value === "glb", t = this.models().find((e) => this.modelKey(e) === this.scope.value);
|
|
59
|
+
this.download(await this.exportGLTF({
|
|
60
|
+
binary: e,
|
|
61
|
+
model: t
|
|
62
|
+
}), e ? "scene.glb" : "scene.gltf");
|
|
63
|
+
}), this.output = a.createElement("output"), this.output.setAttribute("aria-live", "polite"), this.element.append(this.output), (r.parent === void 0 ? a.body : r.parent)?.append(this.element);
|
|
64
|
+
for (let e of r.cameras ?? []) this.addCamera(e);
|
|
65
|
+
r.signal?.addEventListener("abort", this.abort, { once: !0 }), this.refresh();
|
|
66
|
+
}
|
|
67
|
+
setup() {}
|
|
68
|
+
signal(e) {
|
|
69
|
+
let t = e ? AbortSignal.any([e, this.controller.signal]) : this.controller.signal;
|
|
70
|
+
return t.throwIfAborted(), t;
|
|
71
|
+
}
|
|
72
|
+
async action(e) {
|
|
73
|
+
try {
|
|
74
|
+
this.output.textContent = "", await e();
|
|
75
|
+
} catch (e) {
|
|
76
|
+
this.disposed || (this.output.textContent = e instanceof Error ? e.message : String(e));
|
|
77
|
+
} finally {
|
|
78
|
+
this.refresh();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
models() {
|
|
82
|
+
let e = new Set(this.options.models ?? []);
|
|
83
|
+
for (let t of this.runtime.scenes) for (let n of t.plugins.values()) "asset" in n && "nodes" in n && "cameras" in n && e.add(n);
|
|
84
|
+
return [...e].filter((e) => !e.disposed);
|
|
85
|
+
}
|
|
86
|
+
modelKey(e) {
|
|
87
|
+
return this.modelIds.has(e) || this.modelIds.set(e, ++this.nextModel), `model:${this.modelIds.get(e)}`;
|
|
88
|
+
}
|
|
89
|
+
list() {
|
|
90
|
+
return [
|
|
91
|
+
{
|
|
92
|
+
id: "default",
|
|
93
|
+
label: "Default camera"
|
|
94
|
+
},
|
|
95
|
+
...[...this.cameras.values()].map(({ id: e, label: t }) => ({
|
|
96
|
+
id: e,
|
|
97
|
+
label: t
|
|
98
|
+
})),
|
|
99
|
+
...this.models().flatMap((e) => e.cameras.map((t, n) => ({
|
|
100
|
+
id: `${this.modelKey(e)}:${n}`,
|
|
101
|
+
label: `${e.name}: ${t.name || `Camera ${n}`}`
|
|
102
|
+
})))
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
addCamera(e) {
|
|
106
|
+
if (this.disposed || !e?.id || e.id === "default" || e.id.startsWith("model:") || !e.camera || typeof e.label != "string") throw TypeError("Use a live panel and a named application camera with a unique unreserved id");
|
|
107
|
+
return this.cameras.set(e.id, e), this.active === e.id && (this.updateCamera = () => Object.assign(this.engine.camera, n(e.camera))), this.signature = "", this.refresh(), this;
|
|
108
|
+
}
|
|
109
|
+
removeCamera(e) {
|
|
110
|
+
this.cameras.delete(e), this.active === e && this.restoreCamera(), this.signature = "", this.refresh();
|
|
111
|
+
}
|
|
112
|
+
restoreCamera() {
|
|
113
|
+
this.selection++, this.updateCamera = void 0, this.active = "default", Object.assign(this.engine.camera, n(this.home));
|
|
114
|
+
}
|
|
115
|
+
async select(t) {
|
|
116
|
+
this.signal();
|
|
117
|
+
let i = ++this.selection;
|
|
118
|
+
if (t === "default") {
|
|
119
|
+
this.restoreCamera(), this.refresh();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
let a = this.cameras.get(t), o;
|
|
123
|
+
if (a) o = () => Object.assign(this.engine.camera, n(a.camera));
|
|
124
|
+
else {
|
|
125
|
+
let n = this.models().find((e) => t.startsWith(this.modelKey(e) + ":")), i = Number(t.slice(t.lastIndexOf(":") + 1));
|
|
126
|
+
if (!n || !Number.isInteger(i) || !n.cameras[i]) throw RangeError("Use an id from CameraTools.list()");
|
|
127
|
+
let { viewGLTFCamera: a } = await r(import("@margoengine/gltf/pbr"), this.controller.signal);
|
|
128
|
+
if (this.signal(), !this.models().includes(n)) throw e("disposed", "The selected camera model is no longer available; select a live camera");
|
|
129
|
+
o = () => {
|
|
130
|
+
this.engine.camera.project = void 0, a(n, i);
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
i === this.selection && (o(), this.active = t, this.updateCamera = o, this.refresh());
|
|
134
|
+
}
|
|
135
|
+
onPreUpdate() {
|
|
136
|
+
this.list().some((e) => e.id === this.active) || this.restoreCamera(), this.updateCamera?.(), this.refresh();
|
|
137
|
+
}
|
|
138
|
+
screenshot(t = {}) {
|
|
139
|
+
let n = this.signal(t.signal), r = t.timeout ?? 1e4;
|
|
140
|
+
if (!Number.isFinite(r) || r <= 0 || t.quality !== void 0 && (!Number.isFinite(t.quality) || t.quality < 0 || t.quality > 1)) throw RangeError("Use a positive screenshot timeout and quality from 0 to 1");
|
|
141
|
+
return new Promise((i, a) => {
|
|
142
|
+
let o = !1, s = (t, r) => {
|
|
143
|
+
o || (o = !0, clearTimeout(l), n.removeEventListener("abort", c), r ? a(r) : t ? i(t) : a(e("failed", "Screenshot encoder returned no image; check canvas origin access")));
|
|
144
|
+
}, c = () => s(void 0, n.reason), l = setTimeout(() => s(void 0, e("timeout", "Screenshot timed out; check the encoder or increase timeout")), r);
|
|
145
|
+
n.addEventListener("abort", c, { once: !0 });
|
|
146
|
+
try {
|
|
147
|
+
this.engine.render(), this.engine.canvas.toBlob((e) => s(e), t.type ?? "image/png", t.quality);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
s(void 0, e);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
async startRecording(t = {}) {
|
|
154
|
+
let n = this.signal(t.signal);
|
|
155
|
+
if (this.recording || this.starting) throw e("state", "Stop the current canvas recording before starting another");
|
|
156
|
+
this.starting = !0, this.refresh();
|
|
157
|
+
try {
|
|
158
|
+
let { recordCanvas: e } = await r(import("@margoengine/video/record"), n);
|
|
159
|
+
n.throwIfAborted();
|
|
160
|
+
let i = this.recording = e(this.engine.canvas, {
|
|
161
|
+
...t,
|
|
162
|
+
signal: n
|
|
163
|
+
});
|
|
164
|
+
return i.result.then(() => {
|
|
165
|
+
this.recording === i && (this.recording = void 0, this.refresh());
|
|
166
|
+
}, (e) => {
|
|
167
|
+
this.recording === i && (this.recording = void 0, this.disposed || (this.output.textContent = String(e)), this.refresh());
|
|
168
|
+
}), i;
|
|
169
|
+
} finally {
|
|
170
|
+
this.starting = !1, this.refresh();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async stopRecording() {
|
|
174
|
+
if (this.signal(), !this.recording) throw e("state", "Start a canvas recording before stopping it");
|
|
175
|
+
return this.recording.stop();
|
|
176
|
+
}
|
|
177
|
+
async exportGLTF(e = {}) {
|
|
178
|
+
let t = this.signal(e.signal), n = e.binary ?? !0, i = await r(import("@margoengine/exporters"), t);
|
|
179
|
+
t.throwIfAborted();
|
|
180
|
+
let a = e.model;
|
|
181
|
+
if (a && (a.disposed || a.root.engine !== this.runtime)) throw TypeError("Use a live model from this tools engine for export");
|
|
182
|
+
let o = a ? [...this.runtime.scenes].flatMap((e) => [...e.plugins.values()]).filter((e) => "model" in e && e.model === a && "clips" in e) : [], s = a ? await r(i.exportModel(a, {
|
|
183
|
+
binary: n,
|
|
184
|
+
animations: o
|
|
185
|
+
}), t) : n ? i.exportGLB(this.engine) : i.exportGLTF(this.engine);
|
|
186
|
+
return t.throwIfAborted(), s instanceof Uint8Array ? new Blob([s], { type: "model/gltf-binary" }) : new Blob([JSON.stringify(s.json)], { type: "model/gltf+json" });
|
|
187
|
+
}
|
|
188
|
+
download(e, t) {
|
|
189
|
+
this.signal();
|
|
190
|
+
let n = URL.createObjectURL(e), r = this.element.ownerDocument.createElement("a");
|
|
191
|
+
this.urls.add(n), r.href = n, r.download = t, r.textContent = t, this.element.append(r), r.click(), this.output.textContent = `Saved ${t}`;
|
|
192
|
+
}
|
|
193
|
+
refresh() {
|
|
194
|
+
if (this.disposed || !this.picker) return;
|
|
195
|
+
let e = this.list(), t = this.models(), n = JSON.stringify([e, t.map((e) => [this.modelKey(e), e.name])]);
|
|
196
|
+
if (n !== this.signature) {
|
|
197
|
+
this.signature = n;
|
|
198
|
+
let r = (e, t, n) => {
|
|
199
|
+
let r = e.ownerDocument.createElement("option");
|
|
200
|
+
r.value = t, r.textContent = n, e.append(r);
|
|
201
|
+
};
|
|
202
|
+
this.picker.replaceChildren(), e.forEach((e) => r(this.picker, e.id, e.label));
|
|
203
|
+
let i = this.scope.value;
|
|
204
|
+
this.scope.replaceChildren(), r(this.scope, "scene", "Engine scene"), t.forEach((e) => r(this.scope, this.modelKey(e), e.name)), this.scope.value = [...this.scope.options].some((e) => e.value === i) ? i : "scene";
|
|
205
|
+
}
|
|
206
|
+
this.picker.value = this.active, this.recordButton.disabled = this.starting || !!this.recording, this.stopButton.disabled = !this.recording;
|
|
207
|
+
}
|
|
208
|
+
dispose() {
|
|
209
|
+
if (!this.disposed) {
|
|
210
|
+
this.disposed = !0, this.restoreCamera(), this.controller.abort(), this.recording?.dispose(), this.recording = void 0, this.options.signal?.removeEventListener("abort", this.abort);
|
|
211
|
+
for (let e of this.urls) URL.revokeObjectURL(e);
|
|
212
|
+
this.urls.clear(), this.element.remove(), this.runtime.sceneOf(this)?.removePlugin(this.name);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
[Symbol.dispose]() {
|
|
216
|
+
this.dispose();
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
//#endregion
|
|
220
|
+
export { a as CameraTools };
|
package/dist/tweak.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import { EditHistory } from './history.js';
|
|
3
|
+
/** Numeric bounds. Unset bounds are unlimited; unset step accepts any finite number. */
|
|
4
|
+
export interface TweakRange {
|
|
5
|
+
min?: number;
|
|
6
|
+
max?: number;
|
|
7
|
+
step?: number;
|
|
8
|
+
}
|
|
9
|
+
/** Binding options. Colours use normalized RGB(A) unless colorScale is 255. */
|
|
10
|
+
export interface TweakBindingOptions extends TweakRange {
|
|
11
|
+
label?: string;
|
|
12
|
+
kind?: 'color' | 'vector' | 'text';
|
|
13
|
+
options?: readonly unknown[] | Record<string, unknown>;
|
|
14
|
+
colorScale?: 1 | 255;
|
|
15
|
+
axes?: Record<string, TweakRange>;
|
|
16
|
+
readonly?: boolean;
|
|
17
|
+
/** Reverses a function button. Functions without this are actions outside the edit history. */
|
|
18
|
+
undo?: () => void;
|
|
19
|
+
}
|
|
20
|
+
/** Panel placement, shared history and cancellation. */
|
|
21
|
+
export interface TweakPanelOptions {
|
|
22
|
+
/** Default document.body. Pass a parent to use a different document. */
|
|
23
|
+
parent?: HTMLElement;
|
|
24
|
+
title?: string;
|
|
25
|
+
history?: EditHistory;
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
}
|
|
28
|
+
/** One live binding; setValue is one undo step, a DOM input gesture is one step until change/blur. */
|
|
29
|
+
export declare class TweakBinding<T extends object = object, K extends keyof T = keyof T> {
|
|
30
|
+
readonly panel: TweakPanel;
|
|
31
|
+
readonly object: T;
|
|
32
|
+
readonly key: K;
|
|
33
|
+
readonly options: TweakBindingOptions;
|
|
34
|
+
readonly element: HTMLDivElement;
|
|
35
|
+
private readonly inputs;
|
|
36
|
+
private readonly axes;
|
|
37
|
+
private readonly initial;
|
|
38
|
+
private pending?;
|
|
39
|
+
private dead;
|
|
40
|
+
private readonly choices?;
|
|
41
|
+
private readonly kind;
|
|
42
|
+
private readonly listeners;
|
|
43
|
+
constructor(panel: TweakPanel, object: T, key: K, options: TweakBindingOptions);
|
|
44
|
+
private alive;
|
|
45
|
+
private normalize;
|
|
46
|
+
private write;
|
|
47
|
+
private record;
|
|
48
|
+
/** Checked programmatic edit; vectors and colours keep the bound object's identity. */
|
|
49
|
+
setValue(value: T[K]): this;
|
|
50
|
+
getValue(): T[K];
|
|
51
|
+
/** Called on changes, including history replay. Returns an unsubscribe function. */
|
|
52
|
+
onChange(listener: (value: T[K]) => void): () => void;
|
|
53
|
+
reset(): this;
|
|
54
|
+
fire(): void;
|
|
55
|
+
private input;
|
|
56
|
+
finish(): void;
|
|
57
|
+
cancel(): void;
|
|
58
|
+
refresh(): this;
|
|
59
|
+
dispose(): void;
|
|
60
|
+
[Symbol.dispose](): void;
|
|
61
|
+
}
|
|
62
|
+
/** Standalone, engine-free parameter panel. Folders share the root's history and lifecycle. */
|
|
63
|
+
export declare class TweakPanel {
|
|
64
|
+
readonly element: HTMLElement;
|
|
65
|
+
readonly content: HTMLElement;
|
|
66
|
+
readonly history: EditHistory;
|
|
67
|
+
private readonly bindings;
|
|
68
|
+
private readonly folders;
|
|
69
|
+
private dead;
|
|
70
|
+
private readonly unsubscribe;
|
|
71
|
+
private readonly abort;
|
|
72
|
+
private readonly signal?;
|
|
73
|
+
private readonly titleElement;
|
|
74
|
+
constructor(options?: TweakPanelOptions);
|
|
75
|
+
private alive;
|
|
76
|
+
add<T extends object, K extends keyof T>(object: T, key: K, options?: TweakBindingOptions): TweakBinding<T, K>;
|
|
77
|
+
addColor<T extends object, K extends keyof T>(object: T, key: K, options?: TweakBindingOptions): TweakBinding<T, K>;
|
|
78
|
+
addFolder(title: string): TweakPanel;
|
|
79
|
+
/** Explicit refresh follows external application writes without timers or globals. */
|
|
80
|
+
refresh(): void;
|
|
81
|
+
private finish;
|
|
82
|
+
undo(): boolean;
|
|
83
|
+
redo(): boolean;
|
|
84
|
+
dispose(): void;
|
|
85
|
+
[Symbol.dispose](): void;
|
|
86
|
+
}
|
package/dist/tweak.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { i as e, r as t, t as n } from "./errors-D8rc8kzg.js";
|
|
2
|
+
import { EditHistory as r } from "./history.js";
|
|
3
|
+
import { hexColor as i } from "@margoengine/math/color";
|
|
4
|
+
//#region src/tweak.ts
|
|
5
|
+
var a = (e) => ArrayBuffer.isView(e) ? Array.from(e) : Array.isArray(e) ? [...e] : e && typeof e == "object" ? { ...e } : e, o = (e, t) => JSON.stringify(e) === JSON.stringify(t);
|
|
6
|
+
function s(t, n) {
|
|
7
|
+
if (typeof t != "number" || !Number.isFinite(t)) throw e("Binding needs a finite number", "transform");
|
|
8
|
+
let r = t;
|
|
9
|
+
return n.step !== void 0 && (r = (n.min ?? 0) + Math.round((t - (n.min ?? 0)) / n.step) * n.step), r = Math.max(n.min ?? -Infinity, Math.min(n.max ?? Infinity, r)), r;
|
|
10
|
+
}
|
|
11
|
+
function c(e) {
|
|
12
|
+
if ([
|
|
13
|
+
e.min,
|
|
14
|
+
e.max,
|
|
15
|
+
e.step
|
|
16
|
+
].some((e) => e !== void 0 && !Number.isFinite(e)) || e.step !== void 0 && e.step <= 0 || (e.min ?? -Infinity) > (e.max ?? Infinity)) throw t("Binding bounds must be finite, min <= max and step > 0", "transform");
|
|
17
|
+
}
|
|
18
|
+
var l = class {
|
|
19
|
+
constructor(n, r, i, o) {
|
|
20
|
+
this.panel = n, this.object = r, this.key = i, this.options = o, this.inputs = [], this.dead = !1, this.listeners = /* @__PURE__ */ new Set(), c(o);
|
|
21
|
+
for (let e of Object.values(o.axes ?? {})) c(e);
|
|
22
|
+
if (o.colorScale !== void 0 && o.colorScale !== 1 && o.colorScale !== 255) throw t("colorScale must be 1 or 255", "transform");
|
|
23
|
+
let s = r[i];
|
|
24
|
+
if (this.initial = o.options ? s : a(s), this.axes = !o.options && s && typeof s == "object" ? Array.isArray(s) || ArrayBuffer.isView(s) ? Array.from({ length: s.length }, (e, t) => String(t)) : Object.keys(s) : [], this.choices = o.options ? Array.isArray(o.options) ? o.options.map((e) => [String(e), e]) : Object.entries(o.options) : void 0, this.kind = this.choices ? "enum" : typeof s == "function" ? "button" : o.kind ?? (this.axes.length ? this.axes.includes("r") ? "color" : "vector" : typeof s), ![
|
|
25
|
+
"enum",
|
|
26
|
+
"button",
|
|
27
|
+
"color",
|
|
28
|
+
"vector",
|
|
29
|
+
"text",
|
|
30
|
+
"string",
|
|
31
|
+
"number",
|
|
32
|
+
"boolean"
|
|
33
|
+
].includes(this.kind)) throw e("Binding supports numbers, strings, booleans, vectors, colours and functions", "transform");
|
|
34
|
+
this.normalize(s);
|
|
35
|
+
let l = n.element.ownerDocument;
|
|
36
|
+
this.element = l.createElement("div"), this.element.className = "binding", Object.assign(this.element.style, {
|
|
37
|
+
display: "flex",
|
|
38
|
+
alignItems: "center",
|
|
39
|
+
flexWrap: "wrap",
|
|
40
|
+
gap: "6px",
|
|
41
|
+
margin: "6px 0"
|
|
42
|
+
});
|
|
43
|
+
let u = l.createElement("span");
|
|
44
|
+
u.textContent = o.label ?? String(i), this.element.append(u);
|
|
45
|
+
let d = (e, t) => {
|
|
46
|
+
let n = l.createElement("input");
|
|
47
|
+
if (n.type = e, n.setAttribute("aria-label", `${u.textContent}${t === void 0 ? "" : `.${t}`}`), e === "number" || e === "range") {
|
|
48
|
+
let e = {
|
|
49
|
+
...o,
|
|
50
|
+
...o.axes?.[t ?? ""]
|
|
51
|
+
};
|
|
52
|
+
e.min !== void 0 && (n.min = String(e.min)), e.max !== void 0 && (n.max = String(e.max)), n.step = e.step === void 0 ? "any" : String(e.step);
|
|
53
|
+
}
|
|
54
|
+
n.addEventListener("input", () => this.input(n, t)), n.addEventListener("change", () => {
|
|
55
|
+
this.input(n, t), this.finish();
|
|
56
|
+
}), n.addEventListener("blur", () => this.finish()), n.addEventListener("keydown", (e) => {
|
|
57
|
+
e.key === "Escape" && (this.cancel(), e.stopPropagation());
|
|
58
|
+
}), this.inputs.push(n), this.element.append(n);
|
|
59
|
+
};
|
|
60
|
+
if (this.kind === "enum") {
|
|
61
|
+
let e = l.createElement("select");
|
|
62
|
+
e.setAttribute("aria-label", u.textContent);
|
|
63
|
+
for (let [t, [n]] of this.choices.entries()) {
|
|
64
|
+
let r = l.createElement("option");
|
|
65
|
+
r.value = String(t), r.textContent = n, e.append(r);
|
|
66
|
+
}
|
|
67
|
+
e.addEventListener("change", () => this.setValue(this.choices[Number(e.value)][1])), this.inputs.push(e), this.element.append(e);
|
|
68
|
+
} else if (this.kind === "button") {
|
|
69
|
+
let e = l.createElement("button");
|
|
70
|
+
e.textContent = u.textContent, e.addEventListener("click", () => this.fire()), this.inputs.push(e), this.element.append(e), u.hidden = !0;
|
|
71
|
+
} else if (this.kind === "vector" || this.kind === "color" && this.axes.length) for (let e of this.axes) d("number", e);
|
|
72
|
+
else this.kind === "color" ? d("color") : this.kind === "number" ? (d("number"), o.min !== void 0 && o.max !== void 0 && d("range")) : d(this.kind === "boolean" ? "checkbox" : "text");
|
|
73
|
+
this.kind === "color" && this.axes.length && d("color"), n.content.append(this.element), this.refresh();
|
|
74
|
+
}
|
|
75
|
+
alive() {
|
|
76
|
+
if (this.dead) throw n("disposed", "Binding is disposed; add a new binding");
|
|
77
|
+
}
|
|
78
|
+
normalize(n) {
|
|
79
|
+
if (this.kind === "enum") {
|
|
80
|
+
if (!this.choices.some(([, e]) => Object.is(e, n))) throw t("Value is absent from binding options", "transform");
|
|
81
|
+
return n;
|
|
82
|
+
}
|
|
83
|
+
if (this.kind === "number") return s(n, this.options);
|
|
84
|
+
if (this.kind === "color" && !this.axes.length) {
|
|
85
|
+
if (typeof this.initial == "number") {
|
|
86
|
+
if (!Number.isInteger(n) || n < 0 || n > 16777215) throw t("Colour must be a 24-bit integer", "transform");
|
|
87
|
+
} else if (typeof n != "string" || n.length !== 7 || !i(n)) throw e("Colour string must be #rrggbb", "transform");
|
|
88
|
+
return n;
|
|
89
|
+
}
|
|
90
|
+
if (this.axes.length) {
|
|
91
|
+
if (!n || typeof n != "object") throw e("Vector or colour must retain its component shape", "transform");
|
|
92
|
+
let t = a(n);
|
|
93
|
+
for (let e of this.axes) {
|
|
94
|
+
let r = this.kind === "color" ? {
|
|
95
|
+
min: 0,
|
|
96
|
+
max: e === "a" || e === "3" ? 1 : this.options.colorScale ?? 1
|
|
97
|
+
} : {};
|
|
98
|
+
t[e] = s(n[e], {
|
|
99
|
+
...this.options,
|
|
100
|
+
...r,
|
|
101
|
+
...this.options.axes?.[e]
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return t;
|
|
105
|
+
}
|
|
106
|
+
if (typeof n != typeof this.initial) throw e("Binding value must keep its type", "transform");
|
|
107
|
+
return n;
|
|
108
|
+
}
|
|
109
|
+
write(e) {
|
|
110
|
+
if (this.axes.length) {
|
|
111
|
+
let t = this.object[this.key];
|
|
112
|
+
for (let n of this.axes) t[n] = e[n];
|
|
113
|
+
} else this.object[this.key] = e;
|
|
114
|
+
this.refresh();
|
|
115
|
+
for (let e of this.listeners) e(this.object[this.key]);
|
|
116
|
+
}
|
|
117
|
+
record(e, t) {
|
|
118
|
+
(this.kind === "enum" ? Object.is(e, t) : o(e, t)) || this.panel.history.record({
|
|
119
|
+
label: `Set ${this.options.label ?? String(this.key)}`,
|
|
120
|
+
undo: () => this.write(e),
|
|
121
|
+
redo: () => this.write(t)
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
setValue(e) {
|
|
125
|
+
if (this.alive(), this.options.readonly) throw n("state", "Binding is read only; edit the source object");
|
|
126
|
+
if (this.kind === "button") throw n("state", "Call fire() to invoke a button");
|
|
127
|
+
this.finish();
|
|
128
|
+
let t = this.normalize(e), r = this.kind === "enum" ? this.object[this.key] : a(this.object[this.key]);
|
|
129
|
+
return (this.kind === "enum" ? Object.is(r, t) : o(r, t)) || (this.write(t), this.record(r, this.kind === "enum" ? this.object[this.key] : a(this.object[this.key]))), this;
|
|
130
|
+
}
|
|
131
|
+
getValue() {
|
|
132
|
+
return this.alive(), this.object[this.key];
|
|
133
|
+
}
|
|
134
|
+
onChange(e) {
|
|
135
|
+
return this.listeners.add(e), () => void this.listeners.delete(e);
|
|
136
|
+
}
|
|
137
|
+
reset() {
|
|
138
|
+
return this.setValue(this.kind === "enum" ? this.initial : a(this.initial));
|
|
139
|
+
}
|
|
140
|
+
fire() {
|
|
141
|
+
if (this.alive(), this.options.readonly || this.kind !== "button") throw n("state", "Binding is not an enabled button");
|
|
142
|
+
let e = () => this.object[this.key].call(this.object);
|
|
143
|
+
e(), this.options.undo && this.panel.history.record({
|
|
144
|
+
label: this.options.label ?? String(this.key),
|
|
145
|
+
undo: this.options.undo,
|
|
146
|
+
redo: e
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
input(e, t) {
|
|
150
|
+
if (this.dead || this.options.readonly) return;
|
|
151
|
+
let n = this.object[this.key];
|
|
152
|
+
if (e.type === "color") {
|
|
153
|
+
let t = parseInt(e.value.slice(1), 16);
|
|
154
|
+
this.axes.length ? (n = a(n), (this.axes.includes("r") ? [
|
|
155
|
+
"r",
|
|
156
|
+
"g",
|
|
157
|
+
"b"
|
|
158
|
+
] : [
|
|
159
|
+
"0",
|
|
160
|
+
"1",
|
|
161
|
+
"2"
|
|
162
|
+
]).forEach((e, r) => {
|
|
163
|
+
n[e] = (t >> (2 - r) * 8 & 255) / 255 * (this.options.colorScale ?? 1);
|
|
164
|
+
})) : n = typeof this.initial == "number" ? t : e.value;
|
|
165
|
+
} else t === void 0 ? n = e.type === "checkbox" ? e.checked : this.kind === "number" ? e.valueAsNumber : e.value : (n = a(n), n[t] = e.valueAsNumber);
|
|
166
|
+
if ((e.type === "number" || e.type === "range") && !Number.isFinite(e.valueAsNumber)) {
|
|
167
|
+
e.setCustomValidity("Enter a finite number");
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
e.setCustomValidity("");
|
|
171
|
+
let r = this.normalize(n);
|
|
172
|
+
this.pending ??= { before: a(this.object[this.key]) }, this.write(r);
|
|
173
|
+
}
|
|
174
|
+
finish() {
|
|
175
|
+
if (this.pending) {
|
|
176
|
+
let { before: e } = this.pending;
|
|
177
|
+
this.pending = void 0, this.record(e, a(this.object[this.key]));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
cancel() {
|
|
181
|
+
if (this.pending) {
|
|
182
|
+
let { before: e } = this.pending;
|
|
183
|
+
this.pending = void 0, this.write(e);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
refresh() {
|
|
187
|
+
if (this.dead) return this;
|
|
188
|
+
let e = this.object[this.key], t = 0;
|
|
189
|
+
for (let n of this.inputs) {
|
|
190
|
+
if (n.disabled = !!this.options.readonly, n.tagName === "BUTTON") continue;
|
|
191
|
+
if (this.kind === "enum") {
|
|
192
|
+
n.value = String(this.choices.findIndex(([, t]) => Object.is(t, e)));
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
let r = n;
|
|
196
|
+
if (r.type === "checkbox") r.checked = !!e;
|
|
197
|
+
else if (r.type === "color") {
|
|
198
|
+
let t;
|
|
199
|
+
t = this.axes.length ? (this.axes.includes("r") ? [
|
|
200
|
+
"r",
|
|
201
|
+
"g",
|
|
202
|
+
"b"
|
|
203
|
+
] : [
|
|
204
|
+
"0",
|
|
205
|
+
"1",
|
|
206
|
+
"2"
|
|
207
|
+
]).reduce((t, n) => t << 8 | Math.round(Number(e[n]) / (this.options.colorScale ?? 1) * 255), 0) : typeof e == "number" ? e : parseInt(String(e).slice(1), 16), r.value = "#" + t.toString(16).padStart(6, "0");
|
|
208
|
+
} else r.value = String(this.axes.length ? e[this.axes[t++]] : e);
|
|
209
|
+
}
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
212
|
+
dispose() {
|
|
213
|
+
this.dead || (this.finish(), this.dead = !0, this.listeners.clear(), this.element.remove());
|
|
214
|
+
}
|
|
215
|
+
[Symbol.dispose]() {
|
|
216
|
+
this.dispose();
|
|
217
|
+
}
|
|
218
|
+
}, u = class e {
|
|
219
|
+
constructor(e = {}) {
|
|
220
|
+
this.bindings = /* @__PURE__ */ new Set(), this.folders = /* @__PURE__ */ new Set(), this.dead = !1, e.signal?.throwIfAborted();
|
|
221
|
+
let t = e.parent ?? document.body, n = t.ownerDocument;
|
|
222
|
+
this.history = e.history ?? new r(), this.element = n.createElement("details"), this.element.open = !0, this.titleElement = n.createElement("summary"), this.titleElement.textContent = e.title ?? "Parameters", this.content = n.createElement("div"), Object.assign(this.element.style, {
|
|
223
|
+
font: "12px system-ui",
|
|
224
|
+
background: "#fafbfc",
|
|
225
|
+
color: "#202226",
|
|
226
|
+
padding: "10px",
|
|
227
|
+
maxWidth: "360px"
|
|
228
|
+
}), this.element.append(this.titleElement, this.content), t.append(this.element);
|
|
229
|
+
let i = n.createElement("div");
|
|
230
|
+
for (let e of ["undo", "redo"]) {
|
|
231
|
+
let t = n.createElement("button");
|
|
232
|
+
t.textContent = e === "undo" ? "Undo" : "Redo", t.addEventListener("click", () => this[e]()), i.append(t);
|
|
233
|
+
}
|
|
234
|
+
this.content.append(i), this.unsubscribe = this.history.onChange(() => {
|
|
235
|
+
this.refresh(), i.children[0].disabled = !this.history.undoCount, i.children[1].disabled = !this.history.redoCount;
|
|
236
|
+
}), this.element.addEventListener("keydown", (e) => {
|
|
237
|
+
let t = e;
|
|
238
|
+
!t.ctrlKey && !t.metaKey || /INPUT|TEXTAREA|SELECT/.test(t.target.tagName) || (t.key.toLowerCase() === "z" ? (t.preventDefault(), t.stopPropagation(), t.shiftKey ? this.redo() : this.undo()) : t.key.toLowerCase() === "y" && (t.preventDefault(), t.stopPropagation(), this.redo()));
|
|
239
|
+
}), this.signal = e.signal, this.abort = () => this.dispose(), this.signal?.addEventListener("abort", this.abort, { once: !0 });
|
|
240
|
+
}
|
|
241
|
+
alive() {
|
|
242
|
+
if (this.dead) throw n("disposed", "TweakPanel is disposed; create a new panel");
|
|
243
|
+
}
|
|
244
|
+
add(e, t, n = {}) {
|
|
245
|
+
this.alive();
|
|
246
|
+
let r = new l(this, e, t, n);
|
|
247
|
+
return this.bindings.add(r), r;
|
|
248
|
+
}
|
|
249
|
+
addColor(e, t, n = {}) {
|
|
250
|
+
return this.add(e, t, {
|
|
251
|
+
...n,
|
|
252
|
+
kind: "color"
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
addFolder(t) {
|
|
256
|
+
this.alive();
|
|
257
|
+
let n = new e({
|
|
258
|
+
parent: this.content,
|
|
259
|
+
title: t,
|
|
260
|
+
history: this.history
|
|
261
|
+
});
|
|
262
|
+
return this.folders.add(n), n;
|
|
263
|
+
}
|
|
264
|
+
refresh() {
|
|
265
|
+
for (let e of this.bindings) e.refresh();
|
|
266
|
+
for (let e of this.folders) e.refresh();
|
|
267
|
+
}
|
|
268
|
+
finish() {
|
|
269
|
+
for (let e of this.bindings) e.finish();
|
|
270
|
+
for (let e of this.folders) e.finish();
|
|
271
|
+
}
|
|
272
|
+
undo() {
|
|
273
|
+
return this.alive(), this.finish(), this.history.undo();
|
|
274
|
+
}
|
|
275
|
+
redo() {
|
|
276
|
+
return this.alive(), this.finish(), this.history.redo();
|
|
277
|
+
}
|
|
278
|
+
dispose() {
|
|
279
|
+
if (!this.dead) {
|
|
280
|
+
this.dead = !0, this.signal?.removeEventListener("abort", this.abort), this.unsubscribe();
|
|
281
|
+
for (let e of this.bindings) e.dispose();
|
|
282
|
+
for (let e of this.folders) e.dispose();
|
|
283
|
+
this.bindings.clear(), this.folders.clear(), this.element.remove();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
[Symbol.dispose]() {
|
|
287
|
+
this.dispose();
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
//#endregion
|
|
291
|
+
export { l as TweakBinding, u as TweakPanel };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region ../shared/view-rect.js
|
|
2
|
+
function e(e) {
|
|
3
|
+
return e?.view?.viewport;
|
|
4
|
+
}
|
|
5
|
+
function t(t, n) {
|
|
6
|
+
let r = e(n);
|
|
7
|
+
if (!r) return;
|
|
8
|
+
t.setViewport(r[0], r[1], r[2], r[3], 0, 1);
|
|
9
|
+
let i = n.view.scissor ?? r;
|
|
10
|
+
t.setScissorRect(i[0], i[1], i[2], i[3]);
|
|
11
|
+
}
|
|
12
|
+
function n() {
|
|
13
|
+
return globalThis[Symbol.for("margoengine.layers")];
|
|
14
|
+
}
|
|
15
|
+
function r(e) {
|
|
16
|
+
return n()?.maskOf(e) ?? 1;
|
|
17
|
+
}
|
|
18
|
+
function i(e, t) {
|
|
19
|
+
let n = e?.view?.layers;
|
|
20
|
+
return n === void 0 || (r(t) & n) !== 0;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { i as n, e as r, t };
|