@margoengine/cutscene 0.0.0-stage → 0.1.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.
@@ -0,0 +1,79 @@
1
+ import type { EaseSpec } from './document.js';
2
+ import type { ItemRef, TimelineEditor, TimelineModel } from './editor.js';
3
+ /** One scalar channel of a numeric key track. */
4
+ export interface CurveChannel {
5
+ /** Timeline track id. */
6
+ track: string;
7
+ /** Key field, such as value, position, fov or scale. */
8
+ field: string;
9
+ /** Component of an array; omitted for a scalar. */
10
+ component?: number;
11
+ }
12
+ /** A graph point, retaining its place in the original track, including sparse transform keys. */
13
+ export interface CurvePoint extends ItemRef {
14
+ time: number;
15
+ value: number;
16
+ ease?: EaseSpec;
17
+ }
18
+ /** Numeric graph operations on a shared timeline model. No DOM or rendering required. */
19
+ export declare class CurveModel {
20
+ readonly timeline: TimelineModel;
21
+ constructor(timeline: TimelineModel);
22
+ /** All editable numeric channels; string and boolean keys have no value graph. */
23
+ get channels(): CurveChannel[];
24
+ private keys;
25
+ /** Points of a channel, omitting keys that do not set that channel. */
26
+ points(channel: CurveChannel): CurvePoint[];
27
+ /** Moves a key and edits one component, keeping all other components and returning its new index. */
28
+ moveKey(channel: CurveChannel, index: number, time: number, value: number): ItemRef;
29
+ /** Sets a segment's normalized CSS control point on its destination key. X is clamped to [0,1], Y may overshoot. */
30
+ setHandle(channel: CurveChannel, endIndex: number, handle: 0 | 1, x: number, y: number): boolean;
31
+ /** Trims a clip edge while holding its opposite edge. Invalid overlaps or empty spans leave it unchanged. */
32
+ resizeClip(ref: ItemRef, edge: 'start' | 'end', time: number): boolean;
33
+ }
34
+ /** Options of the graph panel. */
35
+ export interface CurveEditorOptions {
36
+ /** Container; default the timeline panel. */
37
+ parent?: HTMLElement;
38
+ /** Initial channel; default the first numeric channel. */
39
+ channel?: CurveChannel;
40
+ /** View box width, default 800. The SVG scales to the available width. */
41
+ width?: number;
42
+ /** Height in pixels, default 240. */
43
+ height?: number;
44
+ }
45
+ /** A graph panel sharing a TimelineEditor's model, playhead, zoom and undo. Clip edges in the timeline become draggable. */
46
+ export declare class CurveEditor {
47
+ readonly editor: TimelineEditor;
48
+ readonly element: HTMLDivElement;
49
+ readonly model: CurveModel;
50
+ private readonly svg;
51
+ private readonly select;
52
+ private readonly width;
53
+ private readonly height;
54
+ private current?;
55
+ private low;
56
+ private high;
57
+ private drag?;
58
+ private readonly unsubscribe;
59
+ private readonly abort;
60
+ constructor(editor: TimelineEditor, { parent, channel, width, height }?: CurveEditorOptions);
61
+ /** Current channel, or undefined when the document has no numeric keys. */
62
+ get channel(): CurveChannel | undefined;
63
+ set channel(value: CurveChannel | undefined);
64
+ /** Frames the current key values with a 20 percent margin. */
65
+ fit(): void;
66
+ private x;
67
+ private y;
68
+ private position;
69
+ private node;
70
+ private reconcileChannels;
71
+ private draw;
72
+ private down;
73
+ private move;
74
+ private up;
75
+ private key;
76
+ /** Removes the graph, edge handlers and subscriptions. Finishes an active gesture as one undo step. */
77
+ dispose(): void;
78
+ [Symbol.dispose](): void;
79
+ }
@@ -0,0 +1,293 @@
1
+ //#region src/curve-editor.ts
2
+ var e = (e) => {
3
+ if (!Number.isFinite(e)) throw RangeError("Curve coordinates must be finite; pass a number");
4
+ }, t = (e, t, n) => Math.max(t, Math.min(n, e)), n = class {
5
+ constructor(e) {
6
+ this.timeline = e;
7
+ }
8
+ get channels() {
9
+ let e = /* @__PURE__ */ new Map();
10
+ for (let t of this.timeline.document.tracks) if ("keys" in t && Array.isArray(t.keys)) for (let n of t.keys) for (let [r, i] of Object.entries(n)) {
11
+ if (r === "time" || r === "ease") continue;
12
+ let n = (n) => {
13
+ let i = {
14
+ track: t.id,
15
+ field: r,
16
+ component: n
17
+ };
18
+ e.set(JSON.stringify(i), i);
19
+ };
20
+ typeof i == "number" ? n() : Array.isArray(i) && i.forEach((e, t) => {
21
+ typeof e == "number" && n(t);
22
+ });
23
+ }
24
+ return [...e.values()];
25
+ }
26
+ keys(e) {
27
+ let t = this.timeline.document.tracks.find((t) => t.id === e.track);
28
+ if (!t || !("keys" in t) || !Array.isArray(t.keys)) throw RangeError("Curve track must have keys; choose a numeric channel");
29
+ return t.keys;
30
+ }
31
+ points(e) {
32
+ return this.keys(e).flatMap((t, n) => {
33
+ let r = t[e.field], i = e.component === void 0 ? r : Array.isArray(r) ? r[e.component] : void 0;
34
+ return typeof i == "number" ? [{
35
+ track: e.track,
36
+ index: n,
37
+ time: t.time,
38
+ value: i,
39
+ ease: t.ease
40
+ }] : [];
41
+ });
42
+ }
43
+ moveKey(n, r, i, a) {
44
+ e(i), e(a);
45
+ let o = this.keys(n)[r];
46
+ if (!o || !this.points(n).some((e) => e.index === r)) throw RangeError("Curve point does not exist; choose a point of this channel");
47
+ let s = n.component === void 0 ? a : [...o[n.field]];
48
+ Array.isArray(s) && (s[n.component] = a);
49
+ let c = {
50
+ track: n.track,
51
+ index: r
52
+ };
53
+ return this.timeline.updateItem(c, {
54
+ time: t(i, 0, this.timeline.duration),
55
+ [n.field]: s
56
+ }) ?? c;
57
+ }
58
+ setHandle(n, r, i, a, o) {
59
+ e(a), e(o);
60
+ let s = this.points(n), c = s.findIndex((e) => e.index === r);
61
+ if (c < 1) throw RangeError("A curve handle needs a preceding point; choose a segment destination");
62
+ let l = s[c].ease, u = Array.isArray(l) ? [...l] : [
63
+ 1 / 3,
64
+ 1 / 3,
65
+ 2 / 3,
66
+ 2 / 3
67
+ ];
68
+ return u[i * 2] = t(a, 0, 1), u[i * 2 + 1] = o, this.timeline.setItem({
69
+ track: n.track,
70
+ index: r
71
+ }, { ease: u });
72
+ }
73
+ resizeClip(n, r, i) {
74
+ e(i);
75
+ let a = this.timeline.rows.find((e) => e.track === n.track)?.items[n.index];
76
+ if (!a || a.kind !== "clip") throw RangeError("Only clip edges resize; choose a clip item");
77
+ let o = a.time + a.duration, s = t(i, 0, this.timeline.duration);
78
+ return r === "start" && s >= o || r === "end" && s <= a.time ? !1 : this.timeline.setItem(n, r === "start" ? {
79
+ start: s,
80
+ duration: o - s
81
+ } : { duration: s - a.time });
82
+ }
83
+ }, r = "http://www.w3.org/2000/svg", i = class {
84
+ constructor(e, { parent: t = e.element, channel: i, width: a = 800, height: o = 240 } = {}) {
85
+ if (this.editor = e, this.low = -1, this.high = 1, this.abort = new AbortController(), !(a > 80 && o > 80 && Number.isFinite(a) && Number.isFinite(o))) throw RangeError("Curve view must exceed 80 pixels; pass finite width and height");
86
+ this.width = a, this.height = o, this.model = new n(e.model), this.current = i ?? this.model.channels[0], this.element = document.createElement("div"), this.element.className = "margo-curve-editor", this.element.style.cssText = "background:#16181d;color:#e6e8ec;padding:8px;touch-action:none", this.select = document.createElement("select"), this.select.setAttribute("aria-label", "Curve channel"), this.element.append(this.select);
87
+ let s = document.createElement("button");
88
+ s.textContent = "Fit values", s.type = "button", this.element.append(s), this.svg = document.createElementNS(r, "svg"), this.svg.setAttribute("viewBox", `0 0 ${a} ${o}`), this.svg.style.cssText = `display:block;width:100%;height:${o}px;touch-action:none;user-select:none`, this.svg.setAttribute("aria-label", "Key value graph"), this.element.append(this.svg), t.append(this.element);
89
+ let c = { signal: this.abort.signal };
90
+ this.select.addEventListener("change", () => {
91
+ this.current = this.model.channels[this.select.selectedIndex], this.fit();
92
+ }, c), s.addEventListener("click", () => this.fit(), c), this.element.addEventListener("keydown", (e) => this.key(e), c), e.element.addEventListener("pointerdown", (e) => this.down(e), {
93
+ ...c,
94
+ capture: !0
95
+ }), e.element.contains(this.element) || this.element.addEventListener("pointerdown", (e) => this.down(e), c), window.addEventListener("pointermove", (e) => this.move(e), c), window.addEventListener("pointerup", (e) => this.up(e), c), window.addEventListener("pointercancel", (e) => this.up(e), c), this.unsubscribe = e.model.on("change", () => this.draw()), this.fit();
96
+ }
97
+ get channel() {
98
+ return this.current;
99
+ }
100
+ set channel(e) {
101
+ this.current = e, this.fit();
102
+ }
103
+ fit() {
104
+ this.reconcileChannels();
105
+ let e = this.current ? this.model.points(this.current).map((e) => e.value) : [0];
106
+ this.low = Math.min(...e), this.high = Math.max(...e);
107
+ let t = Math.max(.001, (this.high - this.low || Math.abs(this.high) || 1) * .2);
108
+ this.low -= t, this.high += t, this.draw();
109
+ }
110
+ x(e) {
111
+ return 32 + e / (this.editor.model.duration || 1) * (this.width - 48);
112
+ }
113
+ y(e) {
114
+ return this.height - 24 - (e - this.low) / (this.high - this.low) * (this.height - 40);
115
+ }
116
+ position(e) {
117
+ let t = this.svg.getBoundingClientRect(), n = (e.clientX - t.left) * this.width / t.width, r = (e.clientY - t.top) * this.height / t.height;
118
+ return [(n - 32) / (this.width - 48) * this.editor.model.duration, this.low + (this.height - 24 - r) / (this.height - 40) * (this.high - this.low)];
119
+ }
120
+ node(e, t, n) {
121
+ let i = document.createElementNS(r, e);
122
+ for (let [e, n] of Object.entries(t)) i.setAttribute(e, String(n));
123
+ return n && i.setAttribute("aria-label", n), this.svg.append(i), i;
124
+ }
125
+ reconcileChannels() {
126
+ let e = this.model.channels;
127
+ return this.current = e.find((e) => e.track === this.current?.track && e.field === this.current.field && e.component === this.current.component) ?? e[0], e;
128
+ }
129
+ draw() {
130
+ let e = this.current, t = this.reconcileChannels();
131
+ if (e?.track !== this.current?.track || e?.field !== this.current?.field || e?.component !== this.current?.component) {
132
+ this.fit();
133
+ return;
134
+ }
135
+ this.select.replaceChildren(...t.map((e) => {
136
+ let t = document.createElement("option");
137
+ return t.textContent = `${e.track}.${e.field}${e.component === void 0 ? "" : "[" + e.component + "]"}`, t;
138
+ })), this.select.selectedIndex = t.findIndex((e) => JSON.stringify(e) === JSON.stringify(this.current)), this.svg.replaceChildren();
139
+ for (let e = 0; e <= 4; e++) {
140
+ let t = this.low + (this.high - this.low) * e / 4;
141
+ this.node("line", {
142
+ x1: 32,
143
+ x2: this.width - 16,
144
+ y1: this.y(t),
145
+ y2: this.y(t),
146
+ stroke: "#3a3f4a"
147
+ }), this.node("text", {
148
+ x: 0,
149
+ y: this.y(t),
150
+ fill: "#a9afbb",
151
+ "font-size": 10
152
+ }).textContent = t.toPrecision(3);
153
+ let n = this.editor.model.duration * e / 4;
154
+ this.node("text", {
155
+ x: this.x(n),
156
+ y: this.height - 4,
157
+ fill: "#a9afbb",
158
+ "font-size": 10
159
+ }).textContent = `${n.toFixed(1)}s`;
160
+ }
161
+ if (!this.current) return;
162
+ let n = this.model.points(this.current);
163
+ for (let e = 1; e < n.length; e++) {
164
+ let t = n[e - 1], r = n[e], i = Array.isArray(r.ease) ? r.ease : [
165
+ 1 / 3,
166
+ 1 / 3,
167
+ 2 / 3,
168
+ 2 / 3
169
+ ], a = (e) => [this.x(t.time + (r.time - t.time) * i[e * 2]), this.y(t.value + (r.value - t.value) * i[e * 2 + 1])], o = a(0), s = a(1), c = this.editor.model.document.tracks.find((e) => e.id === this.current.track), l = this.editor.model.cutscene(), u = [];
170
+ for (let e = 0; e <= 48; e++) {
171
+ let n = t.time + (r.time - t.time) * e / 48, i = l.evaluate(n), a;
172
+ c.type === "property" ? a = i.properties[c.target]?.[c.property] : c.type === "transform" ? a = i.transforms[c.target]?.[this.current.field] : c.type === "camera" && (a = i.camera?.[this.current.field]), this.current.component !== void 0 && Array.isArray(a) && (a = a[this.current.component]), typeof a == "number" && u.push(`${e ? "L" : "M"}${this.x(n)},${this.y(a)}`);
173
+ }
174
+ this.node("path", {
175
+ d: u.join(" "),
176
+ stroke: "#f2b134",
177
+ fill: "none",
178
+ "stroke-width": 2
179
+ });
180
+ for (let [e, n] of [o, s].entries()) {
181
+ let i = e ? r : t;
182
+ this.node("line", {
183
+ x1: this.x(i.time),
184
+ y1: this.y(i.value),
185
+ x2: n[0],
186
+ y2: n[1],
187
+ stroke: "#86c5ff"
188
+ }), this.node("circle", {
189
+ cx: n[0],
190
+ cy: n[1],
191
+ r: 5,
192
+ fill: "#86c5ff",
193
+ tabindex: 0,
194
+ role: "button",
195
+ "data-kind": "handle",
196
+ "data-index": r.index,
197
+ "data-handle": e
198
+ }, `Bezier handle ${e + 1} into key ${r.index}`);
199
+ }
200
+ }
201
+ for (let e of n) this.node("circle", {
202
+ cx: this.x(e.time),
203
+ cy: this.y(e.value),
204
+ r: 6,
205
+ fill: "#f2b134",
206
+ tabindex: 0,
207
+ role: "button",
208
+ "data-kind": "key",
209
+ "data-index": e.index
210
+ }, `Key ${e.index}: ${e.value} at ${e.time} seconds`);
211
+ }
212
+ down(e) {
213
+ if (e.button !== 0 || this.drag) return;
214
+ let t = e.target, n = t.closest("[data-kind][data-index]");
215
+ if (t.closest(".mtl") && t.closest(".mtl") !== this.editor.element) return;
216
+ let r;
217
+ if (n && this.svg.contains(n) && this.current) r = {
218
+ pointer: e.pointerId,
219
+ kind: n.getAttribute("data-kind"),
220
+ index: Number(n.getAttribute("data-index")),
221
+ handle: Number(n.getAttribute("data-handle") ?? 0),
222
+ track: this.current.track
223
+ };
224
+ else {
225
+ let n = t.closest(".mtl-item[data-kind=\"clip\"]");
226
+ if (n) {
227
+ let t = n.getBoundingClientRect(), i = e.clientX - t.left < 8 ? "start" : t.right - e.clientX < 8 ? "end" : void 0;
228
+ i && (r = {
229
+ pointer: e.pointerId,
230
+ kind: i,
231
+ index: Number(n.dataset.index),
232
+ track: n.dataset.track,
233
+ handle: 0
234
+ });
235
+ }
236
+ }
237
+ r && (e.preventDefault(), e.stopImmediatePropagation(), this.drag = r, this.element.setPointerCapture(e.pointerId), this.editor.model.begin("Curve drag"));
238
+ }
239
+ move(e) {
240
+ let t = this.drag;
241
+ if (!t || t.pointer !== e.pointerId) return;
242
+ if (t.kind === "start" || t.kind === "end") {
243
+ let n = this.editor.element.querySelector(".mtl-ruler").getBoundingClientRect();
244
+ this.model.resizeClip(t, t.kind, this.editor.model.snap((e.clientX - n.left) / this.editor.zoom, { ignore: [t] }));
245
+ return;
246
+ }
247
+ if (!this.current) return;
248
+ let [n, r] = this.position(e);
249
+ if (t.kind === "key") t.index = this.model.moveKey(this.current, t.index, this.editor.model.snap(n, { ignore: [t] }), r).index;
250
+ else {
251
+ let e = this.model.points(this.current), i = e.findIndex((e) => e.index === t.index), a = e[i - 1], o = e[i];
252
+ o.time !== a.time && o.value !== a.value && this.model.setHandle(this.current, t.index, t.handle, (n - a.time) / (o.time - a.time), (r - a.value) / (o.value - a.value));
253
+ }
254
+ }
255
+ up(e) {
256
+ this.drag && this.drag.pointer === e.pointerId && (this.drag = void 0, this.editor.model.commit(), this.element.hasPointerCapture(e.pointerId) && this.element.releasePointerCapture(e.pointerId), this.draw());
257
+ }
258
+ key(e) {
259
+ if (e.target.matches("select,button")) {
260
+ e.stopPropagation();
261
+ return;
262
+ }
263
+ if (e.ctrlKey || e.metaKey) return;
264
+ let t = e.target, n = t.getAttribute("data-kind"), r = Number(t.getAttribute("data-index"));
265
+ if (!this.current || !n || ![
266
+ "ArrowUp",
267
+ "ArrowDown",
268
+ "ArrowLeft",
269
+ "ArrowRight"
270
+ ].includes(e.key)) return;
271
+ e.preventDefault(), e.stopPropagation();
272
+ let i = e.key === "ArrowLeft" ? -.01 : e.key === "ArrowRight" ? .01 : 0, a = e.key === "ArrowDown" ? -.01 : e.key === "ArrowUp" ? .01 : 0, o = this.model.points(this.current).find((e) => e.index === r);
273
+ if (n === "key") this.editor.model.begin("Nudge curve"), this.model.moveKey(this.current, r, o.time + i, o.value + a * (this.high - this.low)), this.editor.model.commit();
274
+ else {
275
+ let e = Number(t.getAttribute("data-handle")), n = Array.isArray(o.ease) ? o.ease : [
276
+ 1 / 3,
277
+ 1 / 3,
278
+ 2 / 3,
279
+ 2 / 3
280
+ ];
281
+ this.model.setHandle(this.current, r, e, n[e * 2] + i, n[e * 2 + 1] + a);
282
+ }
283
+ this.svg.querySelector(`[data-kind="${n}"][data-index="${r}"]`)?.focus();
284
+ }
285
+ dispose() {
286
+ this.drag && (this.drag = void 0, this.editor.model.commit()), this.abort.abort(), this.unsubscribe(), this.element.remove();
287
+ }
288
+ [Symbol.dispose]() {
289
+ this.dispose();
290
+ }
291
+ };
292
+ //#endregion
293
+ export { i as CurveEditor, n as CurveModel };