rig-review 0.18.0 → 0.19.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/package.json +1 -1
- package/src/animation-ops.ts +1606 -0
- package/web/compare-picked-tools.ts +20 -3
|
@@ -0,0 +1,1606 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CHANNELS,
|
|
3
|
+
type AnimationInspection,
|
|
4
|
+
type InspectionTrack,
|
|
5
|
+
type StoredKey,
|
|
6
|
+
} from './animation-inspection.ts';
|
|
7
|
+
|
|
8
|
+
export const MOTION_SPEC_VERSION = 'rigc-motion/1';
|
|
9
|
+
|
|
10
|
+
export type EasingTuple = readonly [number, number, number, number];
|
|
11
|
+
|
|
12
|
+
export interface JsonSpan {
|
|
13
|
+
start: number;
|
|
14
|
+
end: number;
|
|
15
|
+
keyStart?: number;
|
|
16
|
+
keyEnd?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface ObjectMemberSpan {
|
|
20
|
+
key: string;
|
|
21
|
+
keyStart: number;
|
|
22
|
+
keyEnd: number;
|
|
23
|
+
colonIndex: number;
|
|
24
|
+
valueStart: number;
|
|
25
|
+
valueEnd: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ParsedObjectSpan {
|
|
29
|
+
kind: 'object';
|
|
30
|
+
start: number;
|
|
31
|
+
end: number;
|
|
32
|
+
members: ObjectMemberSpan[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface ParsedArraySpan {
|
|
36
|
+
kind: 'array';
|
|
37
|
+
start: number;
|
|
38
|
+
end: number;
|
|
39
|
+
elements: JsonSpan[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ParsedPrimitiveSpan {
|
|
43
|
+
kind: 'primitive';
|
|
44
|
+
start: number;
|
|
45
|
+
end: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type ParsedNodeSpan = ParsedObjectSpan | ParsedArraySpan | ParsedPrimitiveSpan;
|
|
49
|
+
|
|
50
|
+
function isWhitespace(ch: number): boolean {
|
|
51
|
+
return ch === 0x20 || ch === 0x09 || ch === 0x0a || ch === 0x0d;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function skipWhitespace(text: string, index: number): number {
|
|
55
|
+
let i = index;
|
|
56
|
+
while (i < text.length && isWhitespace(text.charCodeAt(i))) i++;
|
|
57
|
+
return i;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function scanString(text: string, start: number): { value: string; end: number } {
|
|
61
|
+
if (text.charCodeAt(start) !== 0x22 /* " */) {
|
|
62
|
+
throw new Error(`ANIMATION_JSON_PARSE: expected string at offset ${start}`);
|
|
63
|
+
}
|
|
64
|
+
let i = start + 1;
|
|
65
|
+
while (i < text.length) {
|
|
66
|
+
const ch = text.charCodeAt(i);
|
|
67
|
+
if (ch === 0x22 /* " */) {
|
|
68
|
+
const raw = text.slice(start, i + 1);
|
|
69
|
+
return { value: JSON.parse(raw) as string, end: i + 1 };
|
|
70
|
+
}
|
|
71
|
+
if (ch === 0x5c /* \ */) {
|
|
72
|
+
i += 2;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
i++;
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`ANIMATION_JSON_PARSE: unterminated string at offset ${start}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function scanPrimitive(text: string, start: number): ParsedPrimitiveSpan {
|
|
81
|
+
let i = start;
|
|
82
|
+
while (i < text.length) {
|
|
83
|
+
const ch = text.charCodeAt(i);
|
|
84
|
+
if (
|
|
85
|
+
isWhitespace(ch) ||
|
|
86
|
+
ch === 0x2c /* , */ ||
|
|
87
|
+
ch === 0x5d /* ] */ ||
|
|
88
|
+
ch === 0x7d /* } */ ||
|
|
89
|
+
ch === 0x3a /* : */
|
|
90
|
+
) {
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
i++;
|
|
94
|
+
}
|
|
95
|
+
if (i === start) {
|
|
96
|
+
throw new Error(`ANIMATION_JSON_PARSE: unexpected token at offset ${start}`);
|
|
97
|
+
}
|
|
98
|
+
return { kind: 'primitive', start, end: i };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function scanNode(text: string, startIndex: number): ParsedNodeSpan {
|
|
102
|
+
const start = skipWhitespace(text, startIndex);
|
|
103
|
+
if (start >= text.length) {
|
|
104
|
+
throw new Error('ANIMATION_JSON_PARSE: unexpected end of input');
|
|
105
|
+
}
|
|
106
|
+
const ch = text.charCodeAt(start);
|
|
107
|
+
if (ch === 0x22 /* " */) {
|
|
108
|
+
const str = scanString(text, start);
|
|
109
|
+
return { kind: 'primitive', start, end: str.end };
|
|
110
|
+
}
|
|
111
|
+
if (ch === 0x7b /* { */) {
|
|
112
|
+
const members: ObjectMemberSpan[] = [];
|
|
113
|
+
let i = skipWhitespace(text, start + 1);
|
|
114
|
+
if (i < text.length && text.charCodeAt(i) === 0x7d /* } */) {
|
|
115
|
+
return { kind: 'object', start, end: i + 1, members };
|
|
116
|
+
}
|
|
117
|
+
while (i < text.length) {
|
|
118
|
+
const keyStart = skipWhitespace(text, i);
|
|
119
|
+
const parsedKey = scanString(text, keyStart);
|
|
120
|
+
const keyEnd = parsedKey.end;
|
|
121
|
+
const colonIndex = skipWhitespace(text, keyEnd);
|
|
122
|
+
if (text.charCodeAt(colonIndex) !== 0x3a /* : */) {
|
|
123
|
+
throw new Error(`ANIMATION_JSON_PARSE: expected ':' at offset ${colonIndex}`);
|
|
124
|
+
}
|
|
125
|
+
const valueNode = scanNode(text, colonIndex + 1);
|
|
126
|
+
members.push({
|
|
127
|
+
key: parsedKey.value,
|
|
128
|
+
keyStart,
|
|
129
|
+
keyEnd,
|
|
130
|
+
colonIndex,
|
|
131
|
+
valueStart: valueNode.start,
|
|
132
|
+
valueEnd: valueNode.end,
|
|
133
|
+
});
|
|
134
|
+
i = skipWhitespace(text, valueNode.end);
|
|
135
|
+
const nextCh = text.charCodeAt(i);
|
|
136
|
+
if (nextCh === 0x2c /* , */) {
|
|
137
|
+
i = skipWhitespace(text, i + 1);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (nextCh === 0x7d /* } */) {
|
|
141
|
+
return { kind: 'object', start, end: i + 1, members };
|
|
142
|
+
}
|
|
143
|
+
throw new Error(`ANIMATION_JSON_PARSE: expected ',' or '}' at offset ${i}`);
|
|
144
|
+
}
|
|
145
|
+
throw new Error(`ANIMATION_JSON_PARSE: unterminated object at offset ${start}`);
|
|
146
|
+
}
|
|
147
|
+
if (ch === 0x5b /* [ */) {
|
|
148
|
+
const elements: JsonSpan[] = [];
|
|
149
|
+
let i = skipWhitespace(text, start + 1);
|
|
150
|
+
if (i < text.length && text.charCodeAt(i) === 0x5d /* ] */) {
|
|
151
|
+
return { kind: 'array', start, end: i + 1, elements };
|
|
152
|
+
}
|
|
153
|
+
while (i < text.length) {
|
|
154
|
+
const elemNode = scanNode(text, i);
|
|
155
|
+
elements.push({ start: elemNode.start, end: elemNode.end });
|
|
156
|
+
i = skipWhitespace(text, elemNode.end);
|
|
157
|
+
const nextCh = text.charCodeAt(i);
|
|
158
|
+
if (nextCh === 0x2c /* , */) {
|
|
159
|
+
i = skipWhitespace(text, i + 1);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (nextCh === 0x5d /* ] */) {
|
|
163
|
+
return { kind: 'array', start, end: i + 1, elements };
|
|
164
|
+
}
|
|
165
|
+
throw new Error(`ANIMATION_JSON_PARSE: expected ',' or ']' at offset ${i}`);
|
|
166
|
+
}
|
|
167
|
+
throw new Error(`ANIMATION_JSON_PARSE: unterminated array at offset ${start}`);
|
|
168
|
+
}
|
|
169
|
+
return scanPrimitive(text, start);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Locate the exact `[start, end)` byte/character span of a JSON value at `path`
|
|
174
|
+
* without reformatting or parsing unrelated subtrees into a new string.
|
|
175
|
+
*/
|
|
176
|
+
export function jsonValueSpan(
|
|
177
|
+
text: string,
|
|
178
|
+
path: readonly (string | number)[],
|
|
179
|
+
): JsonSpan | null {
|
|
180
|
+
let cursor = skipWhitespace(text, 0);
|
|
181
|
+
let keyStart: number | undefined;
|
|
182
|
+
let keyEnd: number | undefined;
|
|
183
|
+
|
|
184
|
+
for (let depth = 0; depth < path.length; depth++) {
|
|
185
|
+
const step = path[depth]!;
|
|
186
|
+
const node = scanNode(text, cursor);
|
|
187
|
+
if (typeof step === 'string') {
|
|
188
|
+
if (node.kind !== 'object') return null;
|
|
189
|
+
const member = node.members.find((m) => m.key === step);
|
|
190
|
+
if (!member) return null;
|
|
191
|
+
keyStart = member.keyStart;
|
|
192
|
+
keyEnd = member.keyEnd;
|
|
193
|
+
cursor = member.valueStart;
|
|
194
|
+
if (depth === path.length - 1) {
|
|
195
|
+
return {
|
|
196
|
+
start: member.valueStart,
|
|
197
|
+
end: member.valueEnd,
|
|
198
|
+
keyStart,
|
|
199
|
+
keyEnd,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
if (node.kind !== 'array' || !Number.isInteger(step) || step < 0 || step >= node.elements.length) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
const elem = node.elements[step]!;
|
|
207
|
+
keyStart = undefined;
|
|
208
|
+
keyEnd = undefined;
|
|
209
|
+
cursor = elem.start;
|
|
210
|
+
if (depth === path.length - 1) {
|
|
211
|
+
return { start: elem.start, end: elem.end };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const root = scanNode(text, cursor);
|
|
217
|
+
return { start: root.start, end: root.end };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function resolveObjectNode(text: string, path: readonly (string | number)[]): ParsedObjectSpan {
|
|
221
|
+
const span = jsonValueSpan(text, path);
|
|
222
|
+
if (!span) {
|
|
223
|
+
throw new Error(`ANIMATION_SPAN_NOT_FOUND: /${path.join('/')}`);
|
|
224
|
+
}
|
|
225
|
+
const node = scanNode(text, span.start);
|
|
226
|
+
if (node.kind !== 'object') {
|
|
227
|
+
throw new Error(`ANIMATION_SPAN_NOT_OBJECT: /${path.join('/')}`);
|
|
228
|
+
}
|
|
229
|
+
return node;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function resolveArrayNode(text: string, path: readonly (string | number)[]): ParsedArraySpan {
|
|
233
|
+
const span = jsonValueSpan(text, path);
|
|
234
|
+
if (!span) {
|
|
235
|
+
throw new Error(`ANIMATION_SPAN_NOT_FOUND: /${path.join('/')}`);
|
|
236
|
+
}
|
|
237
|
+
const node = scanNode(text, span.start);
|
|
238
|
+
if (node.kind !== 'array') {
|
|
239
|
+
throw new Error(`ANIMATION_SPAN_NOT_ARRAY: /${path.join('/')}`);
|
|
240
|
+
}
|
|
241
|
+
return node;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Strip IEEE-754 double rounding noise (e.g. `0.08750000000000001` -> `0.0875`)
|
|
246
|
+
* while preserving all significant digits of float32/authored motion numbers.
|
|
247
|
+
*/
|
|
248
|
+
export function cleanMotionNumber(n: number): number {
|
|
249
|
+
if (!Number.isFinite(n)) {
|
|
250
|
+
throw new Error(`ANIMATION_NUMBER_NONFINITE: ${String(n)}`);
|
|
251
|
+
}
|
|
252
|
+
if (n === 0) return 0;
|
|
253
|
+
const cleaned = Number(n.toPrecision(12));
|
|
254
|
+
return cleaned === 0 ? 0 : cleaned;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ---------------------------------------------------------------------------
|
|
258
|
+
// The two independent curve laws + thirds-line cubic seeding
|
|
259
|
+
// ---------------------------------------------------------------------------
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Law 1 (Time-only affine retime):
|
|
263
|
+
* When segment `[t0, t1]` becomes `[nextT0, nextT1]`, scale each channel's
|
|
264
|
+
* `[cx1, cx2]` affinely with no clamp and without touching `[cy1, cy2]`.
|
|
265
|
+
* Identity when `t0 === nextT0 && t1 === nextT1`.
|
|
266
|
+
*/
|
|
267
|
+
export function retimeCurveX(
|
|
268
|
+
curve: readonly number[],
|
|
269
|
+
channels: number,
|
|
270
|
+
t0: number,
|
|
271
|
+
t1: number,
|
|
272
|
+
nextT0: number,
|
|
273
|
+
nextT1: number,
|
|
274
|
+
): number[] {
|
|
275
|
+
if (!Number.isInteger(channels) || channels <= 0 || curve.length !== channels * 4) {
|
|
276
|
+
throw new Error(
|
|
277
|
+
`ANIMATION_CURVE_INVALID: expected ${channels * 4} numbers for ${channels} channel(s), got ${curve.length}`,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
const dt = t1 - t0;
|
|
281
|
+
const nextDt = nextT1 - nextT0;
|
|
282
|
+
if (!Number.isFinite(dt) || dt <= 0 || !Number.isFinite(nextDt) || nextDt <= 0) {
|
|
283
|
+
throw new Error(
|
|
284
|
+
`ANIMATION_KEY_ORDER: segment [${t0}, ${t1}] -> [${nextT0}, ${nextT1}] must have positive duration`,
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
if (t0 === nextT0 && t1 === nextT1) {
|
|
288
|
+
return [...curve];
|
|
289
|
+
}
|
|
290
|
+
const out = [...curve];
|
|
291
|
+
for (let c = 0; c < channels; c++) {
|
|
292
|
+
const i1 = c * 4;
|
|
293
|
+
const i2 = c * 4 + 2;
|
|
294
|
+
const u1 = (curve[i1]! - t0) / dt;
|
|
295
|
+
const u2 = (curve[i2]! - t0) / dt;
|
|
296
|
+
out[i1] = cleanMotionNumber(nextT0 + u1 * nextDt);
|
|
297
|
+
out[i2] = cleanMotionNumber(nextT0 + u2 * nextDt);
|
|
298
|
+
}
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Law 2 (Endpoint-owned value translation):
|
|
304
|
+
* Translating a key's channel value by `dv` shifts only the handle owned by that
|
|
305
|
+
* endpoint (`cy1` on the key's outgoing curve, or `cy2` on the previous key's
|
|
306
|
+
* incoming curve). Never scales `cy` and never touches `cx1` or `cx2`.
|
|
307
|
+
*/
|
|
308
|
+
export function translateCurveY(
|
|
309
|
+
curve: readonly number[],
|
|
310
|
+
channels: number,
|
|
311
|
+
endpoint: 'outgoing' | 'incoming',
|
|
312
|
+
deltas: readonly number[],
|
|
313
|
+
): number[] {
|
|
314
|
+
if (!Number.isInteger(channels) || channels <= 0 || curve.length !== channels * 4) {
|
|
315
|
+
throw new Error(
|
|
316
|
+
`ANIMATION_CURVE_INVALID: expected ${channels * 4} numbers for ${channels} channel(s), got ${curve.length}`,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
if (deltas.length !== channels) {
|
|
320
|
+
throw new Error(
|
|
321
|
+
`ANIMATION_CHANNEL_COUNT: expected ${channels} channel delta(s), got ${deltas.length}`,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
const out = [...curve];
|
|
325
|
+
const offset = endpoint === 'outgoing' ? 1 : 3;
|
|
326
|
+
for (let c = 0; c < channels; c++) {
|
|
327
|
+
const dv = deltas[c]!;
|
|
328
|
+
if (!Number.isFinite(dv)) {
|
|
329
|
+
throw new Error(`ANIMATION_NUMBER_NONFINITE: channel ${c} delta is ${String(dv)}`);
|
|
330
|
+
}
|
|
331
|
+
if (dv === 0) continue;
|
|
332
|
+
const idx = c * 4 + offset;
|
|
333
|
+
out[idx] = cleanMotionNumber(out[idx]! + dv);
|
|
334
|
+
}
|
|
335
|
+
return out;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Law 3 (Thirds-line cubic seed):
|
|
340
|
+
* Converting a linear, stepped, or named-easing segment `(t0, v0) -> (t1, v1)`
|
|
341
|
+
* to an explicit cubic `curve` places control points at 1/3 and 2/3 along the
|
|
342
|
+
* segment for every channel.
|
|
343
|
+
*/
|
|
344
|
+
export function seedCubicCurve(
|
|
345
|
+
t0: number,
|
|
346
|
+
v0: readonly number[],
|
|
347
|
+
t1: number,
|
|
348
|
+
v1: readonly number[],
|
|
349
|
+
): number[] {
|
|
350
|
+
if (!Number.isFinite(t0) || !Number.isFinite(t1) || t1 <= t0) {
|
|
351
|
+
throw new Error(`ANIMATION_KEY_ORDER: invalid segment [${t0}, ${t1}]`);
|
|
352
|
+
}
|
|
353
|
+
if (v0.length === 0 || v0.length !== v1.length) {
|
|
354
|
+
throw new Error(`ANIMATION_CHANNEL_COUNT: channel mismatch (${v0.length} vs ${v1.length})`);
|
|
355
|
+
}
|
|
356
|
+
const dt = t1 - t0;
|
|
357
|
+
const cx1 = cleanMotionNumber(t0 + dt / 3);
|
|
358
|
+
const cx2 = cleanMotionNumber(t0 + (2 * dt) / 3);
|
|
359
|
+
const out: number[] = [];
|
|
360
|
+
for (let c = 0; c < v0.length; c++) {
|
|
361
|
+
const a = v0[c]!;
|
|
362
|
+
const b = v1[c]!;
|
|
363
|
+
if (!Number.isFinite(a) || !Number.isFinite(b)) {
|
|
364
|
+
throw new Error(`ANIMATION_NUMBER_NONFINITE: channel ${c} endpoint is non-finite`);
|
|
365
|
+
}
|
|
366
|
+
const dv = b - a;
|
|
367
|
+
out.push(
|
|
368
|
+
cx1,
|
|
369
|
+
cleanMotionNumber(a + dv / 3),
|
|
370
|
+
cx2,
|
|
371
|
+
cleanMotionNumber(a + (2 * dv) / 3),
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
return out;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// ---------------------------------------------------------------------------
|
|
378
|
+
// Gate & Track classification (Rulings U1, U3, U4, Holes H4, H5)
|
|
379
|
+
// ---------------------------------------------------------------------------
|
|
380
|
+
|
|
381
|
+
export interface AnimationEditGate {
|
|
382
|
+
editable: boolean;
|
|
383
|
+
code:
|
|
384
|
+
| 'OK'
|
|
385
|
+
| 'ANIMATION_IDLE_GOVERNED_BY_TURN_PAD'
|
|
386
|
+
| 'ANIMATION_SOURCE_MISSING'
|
|
387
|
+
| 'ANIMATION_NOT_IN_SOURCE';
|
|
388
|
+
reason: string | null;
|
|
389
|
+
note: string | null;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export interface TrackClassification {
|
|
393
|
+
trackIndex: number;
|
|
394
|
+
targetKind: 'bone' | 'slot' | 'group' | 'physics' | 'path' | 'slider' | 'unknown';
|
|
395
|
+
target: string;
|
|
396
|
+
property: string;
|
|
397
|
+
channels: number;
|
|
398
|
+
editable: boolean;
|
|
399
|
+
reason: string | null;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
403
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Evaluate whether an animation in a package may be edited through `animations_from`.
|
|
408
|
+
*/
|
|
409
|
+
export function checkAnimationEditGate(
|
|
410
|
+
animationName: string,
|
|
411
|
+
animationsFrom: string | null | undefined,
|
|
412
|
+
motionDocument?: unknown,
|
|
413
|
+
): AnimationEditGate {
|
|
414
|
+
if (animationName === 'idle') {
|
|
415
|
+
return {
|
|
416
|
+
editable: false,
|
|
417
|
+
code: 'ANIMATION_IDLE_GOVERNED_BY_TURN_PAD',
|
|
418
|
+
reason:
|
|
419
|
+
'Animation "idle" is generated by rig-parts from config.json idle/blink/turn parameters; use the Turn & Idle controls.',
|
|
420
|
+
note: null,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
if (!animationsFrom || typeof animationsFrom !== 'string' || !animationsFrom.trim()) {
|
|
424
|
+
return {
|
|
425
|
+
editable: false,
|
|
426
|
+
code: 'ANIMATION_SOURCE_MISSING',
|
|
427
|
+
reason: 'config.json does not declare an "animations_from" motion spec file.',
|
|
428
|
+
note: null,
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
if (motionDocument !== undefined) {
|
|
432
|
+
if (!isRecord(motionDocument) || motionDocument.spec !== MOTION_SPEC_VERSION || !isRecord(motionDocument.animations)) {
|
|
433
|
+
return {
|
|
434
|
+
editable: false,
|
|
435
|
+
code: 'ANIMATION_NOT_IN_SOURCE',
|
|
436
|
+
reason: `Motion source "${animationsFrom}" is not a valid ${MOTION_SPEC_VERSION} document.`,
|
|
437
|
+
note: null,
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
const anim = motionDocument.animations[animationName];
|
|
441
|
+
if (!isRecord(anim)) {
|
|
442
|
+
return {
|
|
443
|
+
editable: false,
|
|
444
|
+
code: 'ANIMATION_NOT_IN_SOURCE',
|
|
445
|
+
reason: `Animation "${animationName}" is not declared in "${animationsFrom}".`,
|
|
446
|
+
note: null,
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
const topNote = typeof motionDocument.note === 'string' ? motionDocument.note : null;
|
|
450
|
+
const animNote = typeof anim.note === 'string' ? anim.note : null;
|
|
451
|
+
return {
|
|
452
|
+
editable: true,
|
|
453
|
+
code: 'OK',
|
|
454
|
+
reason: null,
|
|
455
|
+
note: animNote ?? topNote,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
return { editable: true, code: 'OK', reason: null, note: null };
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Classify every track in `animations[animationName].tracks`:
|
|
463
|
+
* only bone value tracks (`property in CHANNELS`) with finite numeric `v` arrays
|
|
464
|
+
* and no `derive`, `lag`, or `stagger` are editable in v1.
|
|
465
|
+
*/
|
|
466
|
+
export function classifyAnimationTracks(
|
|
467
|
+
motionDocument: unknown,
|
|
468
|
+
animationName: string,
|
|
469
|
+
): TrackClassification[] {
|
|
470
|
+
if (!isRecord(motionDocument) || !isRecord(motionDocument.animations)) return [];
|
|
471
|
+
const anim = motionDocument.animations[animationName];
|
|
472
|
+
if (!isRecord(anim) || !Array.isArray(anim.tracks)) return [];
|
|
473
|
+
|
|
474
|
+
return anim.tracks.map((rawTrack, trackIndex): TrackClassification => {
|
|
475
|
+
if (!isRecord(rawTrack)) {
|
|
476
|
+
return {
|
|
477
|
+
trackIndex,
|
|
478
|
+
targetKind: 'unknown',
|
|
479
|
+
target: '',
|
|
480
|
+
property: '',
|
|
481
|
+
channels: 0,
|
|
482
|
+
editable: false,
|
|
483
|
+
reason: 'track entry is not an object',
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
const property = typeof rawTrack.property === 'string' ? rawTrack.property : '';
|
|
487
|
+
const keys = Array.isArray(rawTrack.keys) ? rawTrack.keys : [];
|
|
488
|
+
|
|
489
|
+
for (const kind of ['slot', 'group', 'physics', 'path', 'slider'] as const) {
|
|
490
|
+
if (typeof rawTrack[kind] === 'string') {
|
|
491
|
+
return {
|
|
492
|
+
trackIndex,
|
|
493
|
+
targetKind: kind,
|
|
494
|
+
target: rawTrack[kind] as string,
|
|
495
|
+
property,
|
|
496
|
+
channels: 0,
|
|
497
|
+
editable: false,
|
|
498
|
+
reason: `${kind} track "${rawTrack[kind] as string}.${property}" is read-only in v1`,
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (typeof rawTrack.bone !== 'string') {
|
|
504
|
+
return {
|
|
505
|
+
trackIndex,
|
|
506
|
+
targetKind: 'unknown',
|
|
507
|
+
target: '',
|
|
508
|
+
property,
|
|
509
|
+
channels: 0,
|
|
510
|
+
editable: false,
|
|
511
|
+
reason: 'track has no bone target',
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
const bone = rawTrack.bone;
|
|
516
|
+
if (property === 'inherit') {
|
|
517
|
+
return {
|
|
518
|
+
trackIndex,
|
|
519
|
+
targetKind: 'bone',
|
|
520
|
+
target: bone,
|
|
521
|
+
property,
|
|
522
|
+
channels: 0,
|
|
523
|
+
editable: false,
|
|
524
|
+
reason: `bone "${bone}" inherit track is stepped mode data (read-only in v1)`,
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const channelSpec = CHANNELS[property];
|
|
529
|
+
if (!channelSpec) {
|
|
530
|
+
return {
|
|
531
|
+
trackIndex,
|
|
532
|
+
targetKind: 'bone',
|
|
533
|
+
target: bone,
|
|
534
|
+
property,
|
|
535
|
+
channels: 0,
|
|
536
|
+
editable: false,
|
|
537
|
+
reason: `bone "${bone}" property "${property}" is not an editable value timeline`,
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const channels = channelSpec.length;
|
|
542
|
+
if (rawTrack.lag !== undefined || rawTrack.stagger !== undefined) {
|
|
543
|
+
return {
|
|
544
|
+
trackIndex,
|
|
545
|
+
targetKind: 'bone',
|
|
546
|
+
target: bone,
|
|
547
|
+
property,
|
|
548
|
+
channels,
|
|
549
|
+
editable: false,
|
|
550
|
+
reason: `bone "${bone}.${property}" uses lag/stagger timing offset (read-only in v1)`,
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
if (keys.some((k) => isRecord(k) && k.derive !== undefined)) {
|
|
555
|
+
return {
|
|
556
|
+
trackIndex,
|
|
557
|
+
targetKind: 'bone',
|
|
558
|
+
target: bone,
|
|
559
|
+
property,
|
|
560
|
+
channels,
|
|
561
|
+
editable: false,
|
|
562
|
+
reason: `bone "${bone}.${property}" uses procedural derive keys (read-only in v1)`,
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
if (
|
|
567
|
+
keys.length === 0 ||
|
|
568
|
+
keys.some(
|
|
569
|
+
(k) =>
|
|
570
|
+
!isRecord(k) ||
|
|
571
|
+
typeof k.t !== 'number' ||
|
|
572
|
+
!Number.isFinite(k.t) ||
|
|
573
|
+
!Array.isArray(k.v) ||
|
|
574
|
+
k.v.length !== channels ||
|
|
575
|
+
!k.v.every((n) => typeof n === 'number' && Number.isFinite(n)),
|
|
576
|
+
)
|
|
577
|
+
) {
|
|
578
|
+
return {
|
|
579
|
+
trackIndex,
|
|
580
|
+
targetKind: 'bone',
|
|
581
|
+
target: bone,
|
|
582
|
+
property,
|
|
583
|
+
channels,
|
|
584
|
+
editable: false,
|
|
585
|
+
reason: `bone "${bone}.${property}" keys do not carry ${channels} finite numeric channel(s)`,
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
return {
|
|
590
|
+
trackIndex,
|
|
591
|
+
targetKind: 'bone',
|
|
592
|
+
target: bone,
|
|
593
|
+
property,
|
|
594
|
+
channels,
|
|
595
|
+
editable: true,
|
|
596
|
+
reason: null,
|
|
597
|
+
};
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// ---------------------------------------------------------------------------
|
|
602
|
+
// Pure span-preserving source operations
|
|
603
|
+
// ---------------------------------------------------------------------------
|
|
604
|
+
|
|
605
|
+
export type AnimationSourceOp =
|
|
606
|
+
| {
|
|
607
|
+
op: 'set_key_time';
|
|
608
|
+
animation: string;
|
|
609
|
+
track: number;
|
|
610
|
+
key: number;
|
|
611
|
+
time: number;
|
|
612
|
+
}
|
|
613
|
+
| {
|
|
614
|
+
op: 'set_key_value';
|
|
615
|
+
animation: string;
|
|
616
|
+
track: number;
|
|
617
|
+
key: number;
|
|
618
|
+
channel?: number;
|
|
619
|
+
value: number | readonly number[];
|
|
620
|
+
}
|
|
621
|
+
| {
|
|
622
|
+
op: 'set_key_curve';
|
|
623
|
+
animation: string;
|
|
624
|
+
track: number;
|
|
625
|
+
key: number;
|
|
626
|
+
curve: readonly number[] | 'stepped' | null;
|
|
627
|
+
}
|
|
628
|
+
| {
|
|
629
|
+
op: 'set_key_ease';
|
|
630
|
+
animation: string;
|
|
631
|
+
track: number;
|
|
632
|
+
key: number;
|
|
633
|
+
ease: string | null;
|
|
634
|
+
}
|
|
635
|
+
| {
|
|
636
|
+
op: 'insert_key';
|
|
637
|
+
animation: string;
|
|
638
|
+
track: number;
|
|
639
|
+
time: number;
|
|
640
|
+
value: readonly number[];
|
|
641
|
+
curve?: readonly number[] | 'stepped';
|
|
642
|
+
ease?: string;
|
|
643
|
+
}
|
|
644
|
+
| {
|
|
645
|
+
op: 'delete_key';
|
|
646
|
+
animation: string;
|
|
647
|
+
track: number;
|
|
648
|
+
key: number;
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
export interface SpanReplacement {
|
|
652
|
+
start: number;
|
|
653
|
+
end: number;
|
|
654
|
+
replacement: string;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
export interface AppliedAnimationOpsResult {
|
|
658
|
+
text: string;
|
|
659
|
+
changedSpans: { start: number; end: number }[];
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
interface KeyObjectStyle {
|
|
663
|
+
braceLead: string;
|
|
664
|
+
braceTrail: string;
|
|
665
|
+
colonSep: string;
|
|
666
|
+
memberSep: string;
|
|
667
|
+
arraySep: string;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function detectKeyObjectStyle(text: string, keyObj: ParsedObjectSpan): KeyObjectStyle {
|
|
671
|
+
const m0 = keyObj.members[0];
|
|
672
|
+
const m1 = keyObj.members[1];
|
|
673
|
+
const mLast = keyObj.members[keyObj.members.length - 1];
|
|
674
|
+
const braceLead = m0 ? text.slice(keyObj.start + 1, m0.keyStart) : '';
|
|
675
|
+
const braceTrail = mLast ? text.slice(mLast.valueEnd, keyObj.end - 1) : '';
|
|
676
|
+
const colonSep = m0 ? text.slice(m0.keyEnd, m0.valueStart) : ': ';
|
|
677
|
+
const memberSep = m0 && m1 ? text.slice(m0.valueEnd, m1.keyStart) : colonSep.includes(' ') ? ', ' : ',';
|
|
678
|
+
|
|
679
|
+
let arraySep = colonSep.includes(' ') ? ', ' : ',';
|
|
680
|
+
for (const m of keyObj.members) {
|
|
681
|
+
const valNode = scanNode(text, m.valueStart);
|
|
682
|
+
if (valNode.kind === 'array' && valNode.elements.length >= 2) {
|
|
683
|
+
arraySep = text.slice(valNode.elements[0]!.end, valNode.elements[1]!.start);
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return { braceLead, braceTrail, colonSep, memberSep, arraySep };
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
function formatNumberArray(values: readonly number[], sep: string): string {
|
|
691
|
+
return `[${values.map((v) => String(cleanMotionNumber(v))).join(sep)}]`;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function formatKeyObjectLiteral(
|
|
695
|
+
style: KeyObjectStyle,
|
|
696
|
+
key: {
|
|
697
|
+
t: number;
|
|
698
|
+
v: readonly number[];
|
|
699
|
+
ease?: string;
|
|
700
|
+
curve?: readonly number[] | 'stepped';
|
|
701
|
+
},
|
|
702
|
+
): string {
|
|
703
|
+
const parts: string[] = [
|
|
704
|
+
`"t"${style.colonSep}${String(cleanMotionNumber(key.t))}`,
|
|
705
|
+
`"v"${style.colonSep}${formatNumberArray(key.v, style.arraySep)}`,
|
|
706
|
+
];
|
|
707
|
+
if (key.ease !== undefined) {
|
|
708
|
+
parts.push(`"ease"${style.colonSep}${JSON.stringify(key.ease)}`);
|
|
709
|
+
}
|
|
710
|
+
if (key.curve !== undefined) {
|
|
711
|
+
const curveStr =
|
|
712
|
+
key.curve === 'stepped'
|
|
713
|
+
? '"stepped"'
|
|
714
|
+
: formatNumberArray(key.curve, style.arraySep);
|
|
715
|
+
parts.push(`"curve"${style.colonSep}${curveStr}`);
|
|
716
|
+
}
|
|
717
|
+
return `{${style.braceLead}${parts.join(style.memberSep)}${style.braceTrail}}`;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
function removeObjectMemberEdit(
|
|
721
|
+
keyObj: ParsedObjectSpan,
|
|
722
|
+
memberIndex: number,
|
|
723
|
+
): SpanReplacement {
|
|
724
|
+
const target = keyObj.members[memberIndex]!;
|
|
725
|
+
if (keyObj.members.length === 1) {
|
|
726
|
+
return { start: target.keyStart, end: target.valueEnd, replacement: '' };
|
|
727
|
+
}
|
|
728
|
+
if (memberIndex > 0) {
|
|
729
|
+
const prev = keyObj.members[memberIndex - 1]!;
|
|
730
|
+
return { start: prev.valueEnd, end: target.valueEnd, replacement: '' };
|
|
731
|
+
}
|
|
732
|
+
const next = keyObj.members[1]!;
|
|
733
|
+
return { start: target.keyStart, end: next.keyStart, replacement: '' };
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Produce the minimal span replacements on `keyObj` to set or clear `curve` / `ease`
|
|
738
|
+
* while enforcing Rule R6 (`ease` XOR `curve`).
|
|
739
|
+
*/
|
|
740
|
+
function buildKeyEasingEdits(
|
|
741
|
+
text: string,
|
|
742
|
+
keyObj: ParsedObjectSpan,
|
|
743
|
+
nextState: { curve?: readonly number[] | 'stepped' | null; ease?: string | null },
|
|
744
|
+
baseCurveRawValue?: string,
|
|
745
|
+
): SpanReplacement[] {
|
|
746
|
+
const style = detectKeyObjectStyle(text, keyObj);
|
|
747
|
+
const easeIdx = keyObj.members.findIndex((m) => m.key === 'ease');
|
|
748
|
+
const curveIdx = keyObj.members.findIndex((m) => m.key === 'curve');
|
|
749
|
+
|
|
750
|
+
const wantCurve = nextState.curve !== undefined ? nextState.curve : null;
|
|
751
|
+
const wantEase = nextState.ease !== undefined ? nextState.ease : null;
|
|
752
|
+
|
|
753
|
+
// Case 1: clear both ease and curve
|
|
754
|
+
if (wantCurve === null && wantEase === null) {
|
|
755
|
+
const edits: SpanReplacement[] = [];
|
|
756
|
+
if (easeIdx >= 0 && curveIdx >= 0) {
|
|
757
|
+
const firstIdx = Math.min(easeIdx, curveIdx);
|
|
758
|
+
const secondIdx = Math.max(easeIdx, curveIdx);
|
|
759
|
+
if (firstIdx > 0 && secondIdx === firstIdx + 1) {
|
|
760
|
+
const prev = keyObj.members[firstIdx - 1]!;
|
|
761
|
+
const second = keyObj.members[secondIdx]!;
|
|
762
|
+
edits.push({ start: prev.valueEnd, end: second.valueEnd, replacement: '' });
|
|
763
|
+
return edits;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
if (easeIdx >= 0) edits.push(removeObjectMemberEdit(keyObj, easeIdx));
|
|
767
|
+
if (curveIdx >= 0) edits.push(removeObjectMemberEdit(keyObj, curveIdx));
|
|
768
|
+
return edits;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// Case 2: set curve (and remove ease if present)
|
|
772
|
+
if (wantCurve !== null) {
|
|
773
|
+
let formattedCurve =
|
|
774
|
+
wantCurve === 'stepped'
|
|
775
|
+
? '"stepped"'
|
|
776
|
+
: formatNumberArray(wantCurve, style.arraySep);
|
|
777
|
+
let baseCurveElems: string[] | undefined;
|
|
778
|
+
if (baseCurveRawValue !== undefined && Array.isArray(wantCurve)) {
|
|
779
|
+
try {
|
|
780
|
+
const node = scanNode(baseCurveRawValue, 0);
|
|
781
|
+
if (node.kind === 'array' && node.elements.length === wantCurve.length) {
|
|
782
|
+
baseCurveElems = node.elements.map((e) => baseCurveRawValue.slice(e.start, e.end));
|
|
783
|
+
if (baseCurveElems.every((raw, i) => Number(raw) === cleanMotionNumber(wantCurve[i]!))) {
|
|
784
|
+
formattedCurve = baseCurveRawValue;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
} catch {
|
|
788
|
+
// ignore
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
if (curveIdx >= 0 && easeIdx < 0) {
|
|
793
|
+
const curveMember = keyObj.members[curveIdx]!;
|
|
794
|
+
if (Array.isArray(wantCurve)) {
|
|
795
|
+
const existingNode = scanNode(text, curveMember.valueStart);
|
|
796
|
+
if (existingNode.kind === 'array' && existingNode.elements.length === wantCurve.length) {
|
|
797
|
+
const elemEdits: SpanReplacement[] = [];
|
|
798
|
+
for (let i = 0; i < wantCurve.length; i++) {
|
|
799
|
+
const elem = existingNode.elements[i]!;
|
|
800
|
+
const rawElem = text.slice(elem.start, elem.end);
|
|
801
|
+
const nextStr = formatNumberWithRawStyle(wantCurve[i]!, rawElem, baseCurveElems?.[i]);
|
|
802
|
+
if (rawElem !== nextStr) {
|
|
803
|
+
elemEdits.push({ start: elem.start, end: elem.end, replacement: nextStr });
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
return elemEdits;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
if (text.slice(curveMember.valueStart, curveMember.valueEnd) === formattedCurve) {
|
|
810
|
+
return [];
|
|
811
|
+
}
|
|
812
|
+
return [
|
|
813
|
+
{
|
|
814
|
+
start: curveMember.valueStart,
|
|
815
|
+
end: curveMember.valueEnd,
|
|
816
|
+
replacement: formattedCurve,
|
|
817
|
+
},
|
|
818
|
+
];
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
if (easeIdx >= 0 && curveIdx < 0) {
|
|
822
|
+
const easeMember = keyObj.members[easeIdx]!;
|
|
823
|
+
return [
|
|
824
|
+
{
|
|
825
|
+
start: easeMember.keyStart,
|
|
826
|
+
end: easeMember.valueEnd,
|
|
827
|
+
replacement: `"curve"${style.colonSep}${formattedCurve}`,
|
|
828
|
+
},
|
|
829
|
+
];
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
const edits: SpanReplacement[] = [];
|
|
833
|
+
if (easeIdx >= 0) edits.push(removeObjectMemberEdit(keyObj, easeIdx));
|
|
834
|
+
if (curveIdx >= 0) {
|
|
835
|
+
const curveMember = keyObj.members[curveIdx]!;
|
|
836
|
+
edits.push({
|
|
837
|
+
start: curveMember.valueStart,
|
|
838
|
+
end: curveMember.valueEnd,
|
|
839
|
+
replacement: formattedCurve,
|
|
840
|
+
});
|
|
841
|
+
return edits;
|
|
842
|
+
}
|
|
843
|
+
const lastMember = keyObj.members[keyObj.members.length - 1]!;
|
|
844
|
+
edits.push({
|
|
845
|
+
start: lastMember.valueEnd,
|
|
846
|
+
end: lastMember.valueEnd,
|
|
847
|
+
replacement: `${style.memberSep}"curve"${style.colonSep}${formattedCurve}`,
|
|
848
|
+
});
|
|
849
|
+
return edits;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
// Case 3: set ease (and remove curve if present)
|
|
853
|
+
const formattedEase = JSON.stringify(wantEase);
|
|
854
|
+
if (easeIdx >= 0 && curveIdx < 0) {
|
|
855
|
+
const easeMember = keyObj.members[easeIdx]!;
|
|
856
|
+
if (text.slice(easeMember.valueStart, easeMember.valueEnd) === formattedEase) {
|
|
857
|
+
return [];
|
|
858
|
+
}
|
|
859
|
+
return [
|
|
860
|
+
{
|
|
861
|
+
start: easeMember.valueStart,
|
|
862
|
+
end: easeMember.valueEnd,
|
|
863
|
+
replacement: formattedEase,
|
|
864
|
+
},
|
|
865
|
+
];
|
|
866
|
+
}
|
|
867
|
+
if (curveIdx >= 0 && easeIdx < 0) {
|
|
868
|
+
const curveMember = keyObj.members[curveIdx]!;
|
|
869
|
+
return [
|
|
870
|
+
{
|
|
871
|
+
start: curveMember.keyStart,
|
|
872
|
+
end: curveMember.valueEnd,
|
|
873
|
+
replacement: `"ease"${style.colonSep}${formattedEase}`,
|
|
874
|
+
},
|
|
875
|
+
];
|
|
876
|
+
}
|
|
877
|
+
const edits: SpanReplacement[] = [];
|
|
878
|
+
if (curveIdx >= 0) edits.push(removeObjectMemberEdit(keyObj, curveIdx));
|
|
879
|
+
if (easeIdx >= 0) {
|
|
880
|
+
const easeMember = keyObj.members[easeIdx]!;
|
|
881
|
+
edits.push({
|
|
882
|
+
start: easeMember.valueStart,
|
|
883
|
+
end: easeMember.valueEnd,
|
|
884
|
+
replacement: formattedEase,
|
|
885
|
+
});
|
|
886
|
+
return edits;
|
|
887
|
+
}
|
|
888
|
+
const lastMember = keyObj.members[keyObj.members.length - 1]!;
|
|
889
|
+
edits.push({
|
|
890
|
+
start: lastMember.valueEnd,
|
|
891
|
+
end: lastMember.valueEnd,
|
|
892
|
+
replacement: `${style.memberSep}"ease"${style.colonSep}${formattedEase}`,
|
|
893
|
+
});
|
|
894
|
+
return edits;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
function formatNumberWithRawStyle(
|
|
898
|
+
nextNum: number,
|
|
899
|
+
rawElem: string,
|
|
900
|
+
baseRawElem?: string,
|
|
901
|
+
): string {
|
|
902
|
+
// An unchanged number keeps its literal whatever its precision; only a number
|
|
903
|
+
// this edit computed is cleaned.
|
|
904
|
+
if (Number(rawElem) === nextNum) return rawElem;
|
|
905
|
+
const cleaned = cleanMotionNumber(nextNum);
|
|
906
|
+
if (baseRawElem !== undefined && cleanMotionNumber(Number(baseRawElem)) === cleaned) {
|
|
907
|
+
return baseRawElem;
|
|
908
|
+
}
|
|
909
|
+
if (cleanMotionNumber(Number(rawElem)) === cleaned) return rawElem;
|
|
910
|
+
if (Number.isInteger(cleaned) && /^-?\d+\.0$/.test(rawElem)) {
|
|
911
|
+
return cleaned.toFixed(1);
|
|
912
|
+
}
|
|
913
|
+
return String(cleaned);
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
function buildCurveElementsReplacement(
|
|
917
|
+
text: string,
|
|
918
|
+
keyObj: ParsedObjectSpan,
|
|
919
|
+
nextCurve: readonly number[],
|
|
920
|
+
baseCurveRawElems?: readonly string[],
|
|
921
|
+
): SpanReplacement[] {
|
|
922
|
+
const curveMember = keyObj.members.find((m) => m.key === 'curve');
|
|
923
|
+
if (!curveMember) return [];
|
|
924
|
+
const curveNode = scanNode(text, curveMember.valueStart);
|
|
925
|
+
if (curveNode.kind !== 'array' || curveNode.elements.length !== nextCurve.length) {
|
|
926
|
+
const style = detectKeyObjectStyle(text, keyObj);
|
|
927
|
+
return [
|
|
928
|
+
{
|
|
929
|
+
start: curveMember.valueStart,
|
|
930
|
+
end: curveMember.valueEnd,
|
|
931
|
+
replacement: formatNumberArray(nextCurve, style.arraySep),
|
|
932
|
+
},
|
|
933
|
+
];
|
|
934
|
+
}
|
|
935
|
+
const edits: SpanReplacement[] = [];
|
|
936
|
+
for (let i = 0; i < nextCurve.length; i++) {
|
|
937
|
+
const elem = curveNode.elements[i]!;
|
|
938
|
+
const rawElem = text.slice(elem.start, elem.end);
|
|
939
|
+
const nextStr = formatNumberWithRawStyle(
|
|
940
|
+
nextCurve[i]!,
|
|
941
|
+
rawElem,
|
|
942
|
+
baseCurveRawElems?.[i],
|
|
943
|
+
);
|
|
944
|
+
if (rawElem !== nextStr) {
|
|
945
|
+
edits.push({ start: elem.start, end: elem.end, replacement: nextStr });
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
return edits;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
function readRawArrayElements(
|
|
952
|
+
baseText: string | undefined,
|
|
953
|
+
path: readonly (string | number)[],
|
|
954
|
+
): string[] | undefined {
|
|
955
|
+
if (!baseText) return undefined;
|
|
956
|
+
const span = jsonValueSpan(baseText, path);
|
|
957
|
+
if (!span) return undefined;
|
|
958
|
+
const node = scanNode(baseText, span.start);
|
|
959
|
+
if (node.kind !== 'array') return undefined;
|
|
960
|
+
return node.elements.map((e) => baseText.slice(e.start, e.end));
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
function applySpanReplacements(
|
|
964
|
+
text: string,
|
|
965
|
+
replacements: readonly SpanReplacement[],
|
|
966
|
+
): { text: string; changedSpans: { start: number; end: number }[] } {
|
|
967
|
+
if (replacements.length === 0) {
|
|
968
|
+
return { text, changedSpans: [] };
|
|
969
|
+
}
|
|
970
|
+
const sorted = [...replacements].sort((a, b) => a.start - b.start || a.end - b.end);
|
|
971
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
972
|
+
const prev = sorted[i - 1]!;
|
|
973
|
+
const curr = sorted[i]!;
|
|
974
|
+
if (curr.start < prev.end) {
|
|
975
|
+
throw new Error(
|
|
976
|
+
`ANIMATION_OVERLAPPING_SPANS: [${prev.start}, ${prev.end}) overlaps [${curr.start}, ${curr.end})`,
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
let out = '';
|
|
982
|
+
let cursor = 0;
|
|
983
|
+
const changedSpans: { start: number; end: number }[] = [];
|
|
984
|
+
for (const edit of sorted) {
|
|
985
|
+
out += text.slice(cursor, edit.start);
|
|
986
|
+
const newStart = out.length;
|
|
987
|
+
out += edit.replacement;
|
|
988
|
+
const newEnd = out.length;
|
|
989
|
+
changedSpans.push({ start: newStart, end: newEnd });
|
|
990
|
+
cursor = edit.end;
|
|
991
|
+
}
|
|
992
|
+
out += text.slice(cursor);
|
|
993
|
+
return { text: out, changedSpans };
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
interface ValidatedTrackContext {
|
|
997
|
+
doc: Record<string, unknown>;
|
|
998
|
+
classification: TrackClassification;
|
|
999
|
+
keys: Array<{
|
|
1000
|
+
t: number;
|
|
1001
|
+
v: number[];
|
|
1002
|
+
ease?: string;
|
|
1003
|
+
curve?: number[] | 'stepped';
|
|
1004
|
+
}>;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
function resolveEditableTrack(
|
|
1008
|
+
text: string,
|
|
1009
|
+
animation: string,
|
|
1010
|
+
trackIndex: number,
|
|
1011
|
+
): ValidatedTrackContext {
|
|
1012
|
+
const doc = JSON.parse(text) as unknown;
|
|
1013
|
+
const gate = checkAnimationEditGate(animation, 'animations.json', doc);
|
|
1014
|
+
if (!gate.editable) {
|
|
1015
|
+
throw new Error(`${gate.code}: ${gate.reason}`);
|
|
1016
|
+
}
|
|
1017
|
+
const classifications = classifyAnimationTracks(doc, animation);
|
|
1018
|
+
if (!Number.isInteger(trackIndex) || trackIndex < 0 || trackIndex >= classifications.length) {
|
|
1019
|
+
throw new Error(
|
|
1020
|
+
`ANIMATION_TRACK_OUT_OF_RANGE: animation "${animation}" has ${classifications.length} track(s), got index ${trackIndex}`,
|
|
1021
|
+
);
|
|
1022
|
+
}
|
|
1023
|
+
const classification = classifications[trackIndex]!;
|
|
1024
|
+
if (!classification.editable) {
|
|
1025
|
+
throw new Error(`ANIMATION_TRACK_READ_ONLY: ${classification.reason}`);
|
|
1026
|
+
}
|
|
1027
|
+
const animObj = (doc as { animations: Record<string, { tracks: Array<{ keys: ValidatedTrackContext['keys'] }> }> })
|
|
1028
|
+
.animations[animation]!;
|
|
1029
|
+
const keys = animObj.tracks[trackIndex]!.keys;
|
|
1030
|
+
return { doc: doc as Record<string, unknown>, classification, keys };
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* Apply a single pure source operation to a `rigc-motion/1` JSON string,
|
|
1035
|
+
* touching only the target value/key span(s) and any curve handle spans coupled
|
|
1036
|
+
* by Law 1 or Law 2.
|
|
1037
|
+
*/
|
|
1038
|
+
export function applyAnimationOp(
|
|
1039
|
+
text: string,
|
|
1040
|
+
op: AnimationSourceOp,
|
|
1041
|
+
baseText?: string,
|
|
1042
|
+
): AppliedAnimationOpsResult {
|
|
1043
|
+
const { doc, classification, keys } = resolveEditableTrack(text, op.animation, op.track);
|
|
1044
|
+
const channels = classification.channels;
|
|
1045
|
+
const keysPath = ['animations', op.animation, 'tracks', op.track, 'keys'] as const;
|
|
1046
|
+
|
|
1047
|
+
if (op.op === 'set_key_time') {
|
|
1048
|
+
if (!Number.isInteger(op.key) || op.key < 0 || op.key >= keys.length) {
|
|
1049
|
+
throw new Error(`ANIMATION_KEY_OUT_OF_RANGE: key index ${op.key} out of [0, ${keys.length - 1}]`);
|
|
1050
|
+
}
|
|
1051
|
+
if (typeof op.time !== 'number' || !Number.isFinite(op.time) || op.time < 0) {
|
|
1052
|
+
throw new Error(`ANIMATION_KEY_TIME_INVALID: key time must be a finite number >= 0, got ${String(op.time)}`);
|
|
1053
|
+
}
|
|
1054
|
+
const nextTime = cleanMotionNumber(op.time);
|
|
1055
|
+
const prevKey = op.key > 0 ? keys[op.key - 1] : undefined;
|
|
1056
|
+
const nextKey = op.key + 1 < keys.length ? keys[op.key + 1] : undefined;
|
|
1057
|
+
if (prevKey && nextTime <= prevKey.t) {
|
|
1058
|
+
throw new Error(
|
|
1059
|
+
`ANIMATION_KEY_ORDER: key ${op.key} time ${nextTime} must be > previous key ${op.key - 1} time ${prevKey.t}`,
|
|
1060
|
+
);
|
|
1061
|
+
}
|
|
1062
|
+
if (nextKey && nextTime >= nextKey.t) {
|
|
1063
|
+
throw new Error(
|
|
1064
|
+
`ANIMATION_KEY_ORDER: key ${op.key} time ${nextTime} must be < next key ${op.key + 1} time ${nextKey.t}`,
|
|
1065
|
+
);
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
const currentKey = keys[op.key]!;
|
|
1069
|
+
if (currentKey.t === nextTime) {
|
|
1070
|
+
return { text, changedSpans: [] };
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
const edits: SpanReplacement[] = [];
|
|
1074
|
+
const tSpan = jsonValueSpan(text, [...keysPath, op.key, 't']);
|
|
1075
|
+
if (!tSpan) throw new Error('ANIMATION_SPAN_NOT_FOUND: key.t');
|
|
1076
|
+
const rawT = text.slice(tSpan.start, tSpan.end);
|
|
1077
|
+
const baseTSpan = baseText ? jsonValueSpan(baseText, [...keysPath, op.key, 't']) : null;
|
|
1078
|
+
const baseRawT = baseTSpan && baseText ? baseText.slice(baseTSpan.start, baseTSpan.end) : undefined;
|
|
1079
|
+
const nextTimeStr = formatNumberWithRawStyle(nextTime, rawT, baseRawT);
|
|
1080
|
+
if (rawT !== nextTimeStr) {
|
|
1081
|
+
edits.push({ start: tSpan.start, end: tSpan.end, replacement: nextTimeStr });
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
// Law 1 on previous key's curve if segment [prevKey.t, currentKey.t] -> [prevKey.t, nextTime]
|
|
1085
|
+
if (prevKey && Array.isArray(prevKey.curve)) {
|
|
1086
|
+
const retimedPrev = retimeCurveX(
|
|
1087
|
+
prevKey.curve,
|
|
1088
|
+
channels,
|
|
1089
|
+
prevKey.t,
|
|
1090
|
+
currentKey.t,
|
|
1091
|
+
prevKey.t,
|
|
1092
|
+
nextTime,
|
|
1093
|
+
);
|
|
1094
|
+
const prevKeyObj = resolveObjectNode(text, [...keysPath, op.key - 1]);
|
|
1095
|
+
const basePrevCurve = readRawArrayElements(baseText, [...keysPath, op.key - 1, 'curve']);
|
|
1096
|
+
edits.push(...buildCurveElementsReplacement(text, prevKeyObj, retimedPrev, basePrevCurve));
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
// Law 1 on current key's curve if segment [currentKey.t, nextKey.t] -> [nextTime, nextKey.t]
|
|
1100
|
+
if (nextKey && Array.isArray(currentKey.curve)) {
|
|
1101
|
+
const retimedCurr = retimeCurveX(
|
|
1102
|
+
currentKey.curve,
|
|
1103
|
+
channels,
|
|
1104
|
+
currentKey.t,
|
|
1105
|
+
nextKey.t,
|
|
1106
|
+
nextTime,
|
|
1107
|
+
nextKey.t,
|
|
1108
|
+
);
|
|
1109
|
+
const currKeyObj = resolveObjectNode(text, [...keysPath, op.key]);
|
|
1110
|
+
const baseCurrCurve = readRawArrayElements(baseText, [...keysPath, op.key, 'curve']);
|
|
1111
|
+
edits.push(...buildCurveElementsReplacement(text, currKeyObj, retimedCurr, baseCurrCurve));
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
return applySpanReplacements(text, edits);
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
if (op.op === 'set_key_value') {
|
|
1118
|
+
if (!Number.isInteger(op.key) || op.key < 0 || op.key >= keys.length) {
|
|
1119
|
+
throw new Error(`ANIMATION_KEY_OUT_OF_RANGE: key index ${op.key} out of [0, ${keys.length - 1}]`);
|
|
1120
|
+
}
|
|
1121
|
+
const currentKey = keys[op.key]!;
|
|
1122
|
+
const nextValues = [...currentKey.v];
|
|
1123
|
+
|
|
1124
|
+
if (op.channel !== undefined) {
|
|
1125
|
+
if (!Number.isInteger(op.channel) || op.channel < 0 || op.channel >= channels) {
|
|
1126
|
+
throw new Error(`ANIMATION_CHANNEL_OUT_OF_RANGE: channel ${op.channel} out of [0, ${channels - 1}]`);
|
|
1127
|
+
}
|
|
1128
|
+
if (typeof op.value !== 'number' || !Number.isFinite(op.value)) {
|
|
1129
|
+
throw new Error(`ANIMATION_VALUE_INVALID: expected finite number for channel ${op.channel}`);
|
|
1130
|
+
}
|
|
1131
|
+
nextValues[op.channel] = cleanMotionNumber(op.value);
|
|
1132
|
+
} else {
|
|
1133
|
+
if (
|
|
1134
|
+
!Array.isArray(op.value) ||
|
|
1135
|
+
op.value.length !== channels ||
|
|
1136
|
+
!op.value.every((n) => typeof n === 'number' && Number.isFinite(n))
|
|
1137
|
+
) {
|
|
1138
|
+
throw new Error(`ANIMATION_VALUE_INVALID: expected array of ${channels} finite number(s)`);
|
|
1139
|
+
}
|
|
1140
|
+
for (let c = 0; c < channels; c++) {
|
|
1141
|
+
nextValues[c] = cleanMotionNumber(op.value[c]!);
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
const deltas = nextValues.map((nv, c) => cleanMotionNumber(nv - currentKey.v[c]!));
|
|
1146
|
+
if (deltas.every((d) => d === 0)) {
|
|
1147
|
+
return { text, changedSpans: [] };
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
const edits: SpanReplacement[] = [];
|
|
1151
|
+
const vArray = resolveArrayNode(text, [...keysPath, op.key, 'v']);
|
|
1152
|
+
const baseVElems = readRawArrayElements(baseText, [...keysPath, op.key, 'v']);
|
|
1153
|
+
for (let c = 0; c < channels; c++) {
|
|
1154
|
+
if (deltas[c] === 0) continue;
|
|
1155
|
+
const elemSpan = vArray.elements[c]!;
|
|
1156
|
+
const rawV = text.slice(elemSpan.start, elemSpan.end);
|
|
1157
|
+
const nextStr = formatNumberWithRawStyle(nextValues[c]!, rawV, baseVElems?.[c]);
|
|
1158
|
+
if (rawV !== nextStr) {
|
|
1159
|
+
edits.push({ start: elemSpan.start, end: elemSpan.end, replacement: nextStr });
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
// Law 2 on current key's outgoing handle cy1
|
|
1164
|
+
if (op.key + 1 < keys.length && Array.isArray(currentKey.curve)) {
|
|
1165
|
+
const translatedCurr = translateCurveY(currentKey.curve, channels, 'outgoing', deltas);
|
|
1166
|
+
const currKeyObj = resolveObjectNode(text, [...keysPath, op.key]);
|
|
1167
|
+
const baseCurrCurve = readRawArrayElements(baseText, [...keysPath, op.key, 'curve']);
|
|
1168
|
+
edits.push(...buildCurveElementsReplacement(text, currKeyObj, translatedCurr, baseCurrCurve));
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
// Law 2 on previous key's incoming handle cy2
|
|
1172
|
+
const prevKey = op.key > 0 ? keys[op.key - 1] : undefined;
|
|
1173
|
+
if (prevKey && Array.isArray(prevKey.curve)) {
|
|
1174
|
+
const translatedPrev = translateCurveY(prevKey.curve, channels, 'incoming', deltas);
|
|
1175
|
+
const prevKeyObj = resolveObjectNode(text, [...keysPath, op.key - 1]);
|
|
1176
|
+
const basePrevCurve = readRawArrayElements(baseText, [...keysPath, op.key - 1, 'curve']);
|
|
1177
|
+
edits.push(...buildCurveElementsReplacement(text, prevKeyObj, translatedPrev, basePrevCurve));
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
return applySpanReplacements(text, edits);
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
if (op.op === 'set_key_curve') {
|
|
1184
|
+
if (!Number.isInteger(op.key) || op.key < 0 || op.key >= keys.length) {
|
|
1185
|
+
throw new Error(`ANIMATION_KEY_OUT_OF_RANGE: key index ${op.key} out of [0, ${keys.length - 1}]`);
|
|
1186
|
+
}
|
|
1187
|
+
if (op.curve !== null && op.key === keys.length - 1) {
|
|
1188
|
+
throw new Error('ANIMATION_LAST_KEY_CURVE: the last key of a track cannot carry a curve');
|
|
1189
|
+
}
|
|
1190
|
+
if (op.curve !== null && op.curve !== 'stepped') {
|
|
1191
|
+
if (
|
|
1192
|
+
!Array.isArray(op.curve) ||
|
|
1193
|
+
op.curve.length !== channels * 4 ||
|
|
1194
|
+
!op.curve.every((n) => typeof n === 'number' && Number.isFinite(n))
|
|
1195
|
+
) {
|
|
1196
|
+
throw new Error(
|
|
1197
|
+
`ANIMATION_CURVE_INVALID: curve must be "stepped", null, or ${channels * 4} finite numbers`,
|
|
1198
|
+
);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
const keyObj = resolveObjectNode(text, [...keysPath, op.key]);
|
|
1202
|
+
const baseCurveSpan = baseText ? jsonValueSpan(baseText, [...keysPath, op.key, 'curve']) : null;
|
|
1203
|
+
const baseCurveRaw =
|
|
1204
|
+
baseCurveSpan && baseText ? baseText.slice(baseCurveSpan.start, baseCurveSpan.end) : undefined;
|
|
1205
|
+
const edits = buildKeyEasingEdits(text, keyObj, { curve: op.curve, ease: null }, baseCurveRaw);
|
|
1206
|
+
return applySpanReplacements(text, edits);
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
if (op.op === 'set_key_ease') {
|
|
1210
|
+
if (!Number.isInteger(op.key) || op.key < 0 || op.key >= keys.length) {
|
|
1211
|
+
throw new Error(`ANIMATION_KEY_OUT_OF_RANGE: key index ${op.key} out of [0, ${keys.length - 1}]`);
|
|
1212
|
+
}
|
|
1213
|
+
if (op.ease !== null && op.key === keys.length - 1) {
|
|
1214
|
+
throw new Error('ANIMATION_LAST_KEY_EASING: the last key of a track cannot carry an easing');
|
|
1215
|
+
}
|
|
1216
|
+
if (op.ease !== null) {
|
|
1217
|
+
if (op.ease === 'stepped') {
|
|
1218
|
+
throw new Error('ANIMATION_EASING_STEPPED_USE_CURVE: "stepped" is a curve mode; use curve: "stepped"');
|
|
1219
|
+
}
|
|
1220
|
+
const easings = isRecord(doc.easings) ? doc.easings : {};
|
|
1221
|
+
if (!Array.isArray(easings[op.ease])) {
|
|
1222
|
+
throw new Error(`ANIMATION_EASING_UNKNOWN: unknown easing "${op.ease}"`);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
const keyObj = resolveObjectNode(text, [...keysPath, op.key]);
|
|
1226
|
+
const edits = buildKeyEasingEdits(text, keyObj, { ease: op.ease, curve: null });
|
|
1227
|
+
return applySpanReplacements(text, edits);
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
if (op.op === 'insert_key') {
|
|
1231
|
+
if (typeof op.time !== 'number' || !Number.isFinite(op.time) || op.time < 0) {
|
|
1232
|
+
throw new Error(`ANIMATION_KEY_TIME_INVALID: key time must be a finite number >= 0, got ${String(op.time)}`);
|
|
1233
|
+
}
|
|
1234
|
+
const insertTime = cleanMotionNumber(op.time);
|
|
1235
|
+
if (keys.some((k) => k.t === insertTime)) {
|
|
1236
|
+
throw new Error(`ANIMATION_KEY_DUPLICATE_TIME: a key already exists at t=${insertTime}`);
|
|
1237
|
+
}
|
|
1238
|
+
if (
|
|
1239
|
+
!Array.isArray(op.value) ||
|
|
1240
|
+
op.value.length !== channels ||
|
|
1241
|
+
!op.value.every((n) => typeof n === 'number' && Number.isFinite(n))
|
|
1242
|
+
) {
|
|
1243
|
+
throw new Error(`ANIMATION_VALUE_INVALID: expected ${channels} finite channel value(s)`);
|
|
1244
|
+
}
|
|
1245
|
+
if (op.curve !== undefined && op.ease !== undefined) {
|
|
1246
|
+
throw new Error('ANIMATION_KEY_EASING_AND_CURVE: a key cannot carry both ease and curve');
|
|
1247
|
+
}
|
|
1248
|
+
const insertIndex = keys.findIndex((k) => k.t > insertTime);
|
|
1249
|
+
const targetIdx = insertIndex === -1 ? keys.length : insertIndex;
|
|
1250
|
+
const isLast = targetIdx === keys.length;
|
|
1251
|
+
if (isLast && op.curve !== undefined) {
|
|
1252
|
+
throw new Error('ANIMATION_LAST_KEY_CURVE: the last key of a track cannot carry a curve');
|
|
1253
|
+
}
|
|
1254
|
+
if (isLast && op.ease !== undefined) {
|
|
1255
|
+
throw new Error('ANIMATION_LAST_KEY_EASING: the last key of a track cannot carry an easing');
|
|
1256
|
+
}
|
|
1257
|
+
if (op.curve !== undefined && op.curve !== 'stepped') {
|
|
1258
|
+
if (
|
|
1259
|
+
!Array.isArray(op.curve) ||
|
|
1260
|
+
op.curve.length !== channels * 4 ||
|
|
1261
|
+
!op.curve.every((n) => typeof n === 'number' && Number.isFinite(n))
|
|
1262
|
+
) {
|
|
1263
|
+
throw new Error(`ANIMATION_CURVE_INVALID: expected ${channels * 4} finite numbers`);
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
if (op.ease !== undefined) {
|
|
1267
|
+
if (op.ease === 'stepped') {
|
|
1268
|
+
throw new Error('ANIMATION_EASING_STEPPED_USE_CURVE: "stepped" is a curve mode; use curve: "stepped"');
|
|
1269
|
+
}
|
|
1270
|
+
const easings = isRecord(doc.easings) ? doc.easings : {};
|
|
1271
|
+
if (!Array.isArray(easings[op.ease])) {
|
|
1272
|
+
throw new Error(`ANIMATION_EASING_UNKNOWN: unknown easing "${op.ease}"`);
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
const keysArray = resolveArrayNode(text, keysPath);
|
|
1277
|
+
const refKeyIndex = targetIdx > 0 ? targetIdx - 1 : 0;
|
|
1278
|
+
const refKeyObj = resolveObjectNode(text, [...keysPath, refKeyIndex]);
|
|
1279
|
+
const style = detectKeyObjectStyle(text, refKeyObj);
|
|
1280
|
+
const newKeyLiteral = formatKeyObjectLiteral(style, {
|
|
1281
|
+
t: insertTime,
|
|
1282
|
+
v: op.value.map(cleanMotionNumber),
|
|
1283
|
+
ease: op.ease,
|
|
1284
|
+
curve: op.curve,
|
|
1285
|
+
});
|
|
1286
|
+
|
|
1287
|
+
let elemSep = style.memberSep;
|
|
1288
|
+
if (keysArray.elements.length >= 2) {
|
|
1289
|
+
const sepIdx = Math.min(targetIdx > 0 ? targetIdx - 1 : 0, keysArray.elements.length - 2);
|
|
1290
|
+
elemSep = text.slice(
|
|
1291
|
+
keysArray.elements[sepIdx]!.end,
|
|
1292
|
+
keysArray.elements[sepIdx + 1]!.start,
|
|
1293
|
+
);
|
|
1294
|
+
} else if (keysArray.elements.length === 1) {
|
|
1295
|
+
const lead = text.slice(keysArray.start + 1, keysArray.elements[0]!.start);
|
|
1296
|
+
elemSep = lead.includes('\n') ? `,${lead}` : style.memberSep;
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
const edits: SpanReplacement[] = [];
|
|
1300
|
+
|
|
1301
|
+
// If inserted between [targetIdx - 1, targetIdx], retime previous key's numeric curve
|
|
1302
|
+
if (targetIdx > 0 && targetIdx < keys.length) {
|
|
1303
|
+
const prevKey = keys[targetIdx - 1]!;
|
|
1304
|
+
const nextKey = keys[targetIdx]!;
|
|
1305
|
+
if (Array.isArray(prevKey.curve)) {
|
|
1306
|
+
const retimedPrev = retimeCurveX(
|
|
1307
|
+
prevKey.curve,
|
|
1308
|
+
channels,
|
|
1309
|
+
prevKey.t,
|
|
1310
|
+
nextKey.t,
|
|
1311
|
+
prevKey.t,
|
|
1312
|
+
insertTime,
|
|
1313
|
+
);
|
|
1314
|
+
const prevKeyObj = resolveObjectNode(text, [...keysPath, targetIdx - 1]);
|
|
1315
|
+
edits.push(...buildCurveElementsReplacement(text, prevKeyObj, retimedPrev));
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
if (targetIdx > 0) {
|
|
1320
|
+
const prevElem = keysArray.elements[targetIdx - 1]!;
|
|
1321
|
+
edits.push({
|
|
1322
|
+
start: prevElem.end,
|
|
1323
|
+
end: prevElem.end,
|
|
1324
|
+
replacement: `${elemSep}${newKeyLiteral}`,
|
|
1325
|
+
});
|
|
1326
|
+
} else {
|
|
1327
|
+
const firstElem = keysArray.elements[0]!;
|
|
1328
|
+
edits.push({
|
|
1329
|
+
start: firstElem.start,
|
|
1330
|
+
end: firstElem.start,
|
|
1331
|
+
replacement: `${newKeyLiteral}${elemSep}`,
|
|
1332
|
+
});
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
return applySpanReplacements(text, edits);
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
if (op.op === 'delete_key') {
|
|
1339
|
+
if (!Number.isInteger(op.key) || op.key < 0 || op.key >= keys.length) {
|
|
1340
|
+
throw new Error(`ANIMATION_KEY_OUT_OF_RANGE: key index ${op.key} out of [0, ${keys.length - 1}]`);
|
|
1341
|
+
}
|
|
1342
|
+
if (keys.length <= 1) {
|
|
1343
|
+
throw new Error('ANIMATION_TRACK_MIN_KEYS: cannot delete the only key on a track');
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
const keysArray = resolveArrayNode(text, keysPath);
|
|
1347
|
+
const edits: SpanReplacement[] = [];
|
|
1348
|
+
|
|
1349
|
+
// If deleting the last key, strip any curve/ease on the new last key (key - 1)
|
|
1350
|
+
if (op.key === keys.length - 1) {
|
|
1351
|
+
const newLastKey = keys[op.key - 1]!;
|
|
1352
|
+
if (newLastKey.curve !== undefined || newLastKey.ease !== undefined) {
|
|
1353
|
+
const prevKeyObj = resolveObjectNode(text, [...keysPath, op.key - 1]);
|
|
1354
|
+
edits.push(...buildKeyEasingEdits(text, prevKeyObj, { curve: null, ease: null }));
|
|
1355
|
+
}
|
|
1356
|
+
} else if (op.key > 0) {
|
|
1357
|
+
// Deleting an interior key: retime previous key's numeric curve to the new next key
|
|
1358
|
+
const prevKey = keys[op.key - 1]!;
|
|
1359
|
+
const currKey = keys[op.key]!;
|
|
1360
|
+
const nextKey = keys[op.key + 1]!;
|
|
1361
|
+
if (Array.isArray(prevKey.curve)) {
|
|
1362
|
+
const retimedPrev = retimeCurveX(
|
|
1363
|
+
prevKey.curve,
|
|
1364
|
+
channels,
|
|
1365
|
+
prevKey.t,
|
|
1366
|
+
currKey.t,
|
|
1367
|
+
prevKey.t,
|
|
1368
|
+
nextKey.t,
|
|
1369
|
+
);
|
|
1370
|
+
const prevKeyObj = resolveObjectNode(text, [...keysPath, op.key - 1]);
|
|
1371
|
+
edits.push(...buildCurveElementsReplacement(text, prevKeyObj, retimedPrev));
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
if (op.key > 0) {
|
|
1376
|
+
const prevElem = keysArray.elements[op.key - 1]!;
|
|
1377
|
+
const currElem = keysArray.elements[op.key]!;
|
|
1378
|
+
edits.push({ start: prevElem.end, end: currElem.end, replacement: '' });
|
|
1379
|
+
} else {
|
|
1380
|
+
const currElem = keysArray.elements[0]!;
|
|
1381
|
+
const nextElem = keysArray.elements[1]!;
|
|
1382
|
+
edits.push({ start: currElem.start, end: nextElem.start, replacement: '' });
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
return applySpanReplacements(text, edits);
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
const exhaustive: never = op;
|
|
1389
|
+
throw new Error(`ANIMATION_OP_UNKNOWN: ${JSON.stringify(exhaustive)}`);
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
/**
|
|
1393
|
+
* Apply a sequence of pure source ops to `text`.
|
|
1394
|
+
* When `ops` is empty, returns `{ text, changedSpans: [] }` byte-identically.
|
|
1395
|
+
*/
|
|
1396
|
+
export function applyAnimationOps(
|
|
1397
|
+
text: string,
|
|
1398
|
+
ops: readonly AnimationSourceOp[],
|
|
1399
|
+
): AppliedAnimationOpsResult {
|
|
1400
|
+
let current = text;
|
|
1401
|
+
const allSpans: { start: number; end: number }[] = [];
|
|
1402
|
+
for (const op of ops) {
|
|
1403
|
+
const res = applyAnimationOp(current, op, text);
|
|
1404
|
+
current = res.text;
|
|
1405
|
+
allSpans.push(...res.changedSpans);
|
|
1406
|
+
}
|
|
1407
|
+
return { text: current, changedSpans: allSpans };
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* Stateful builder over an immutable base `animations_from` text.
|
|
1412
|
+
* Supports applying, coalescing, undoing, and reverting edits while guaranteeing
|
|
1413
|
+
* that reverting all ops yields the exact original bytes.
|
|
1414
|
+
*/
|
|
1415
|
+
export class AnimationOpBuilder {
|
|
1416
|
+
readonly baseText: string;
|
|
1417
|
+
private currentText: string;
|
|
1418
|
+
private readonly history: Array<{ op: AnimationSourceOp; textBefore: string }> = [];
|
|
1419
|
+
|
|
1420
|
+
constructor(baseText: string) {
|
|
1421
|
+
this.baseText = baseText;
|
|
1422
|
+
this.currentText = baseText;
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
get text(): string {
|
|
1426
|
+
return this.currentText;
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
get ops(): readonly AnimationSourceOp[] {
|
|
1430
|
+
return this.history.map((h) => h.op);
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
get dirty(): boolean {
|
|
1434
|
+
return this.currentText !== this.baseText;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
apply(op: AnimationSourceOp): AppliedAnimationOpsResult {
|
|
1438
|
+
const res = applyAnimationOp(this.currentText, op, this.baseText);
|
|
1439
|
+
this.history.push({ op, textBefore: this.currentText });
|
|
1440
|
+
this.currentText = res.text;
|
|
1441
|
+
return res;
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
undo(): boolean {
|
|
1445
|
+
const last = this.history.pop();
|
|
1446
|
+
if (!last) return false;
|
|
1447
|
+
this.currentText = last.textBefore;
|
|
1448
|
+
return true;
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
revert(): string {
|
|
1452
|
+
this.history.length = 0;
|
|
1453
|
+
this.currentText = this.baseText;
|
|
1454
|
+
return this.currentText;
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
// ---------------------------------------------------------------------------
|
|
1459
|
+
// Pending-preview adapter into src/animation-inspection.ts (Correction 7)
|
|
1460
|
+
// ---------------------------------------------------------------------------
|
|
1461
|
+
|
|
1462
|
+
function f32(n: number): number {
|
|
1463
|
+
return Math.fround(n);
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
function resolveKeyCurveForInspection(
|
|
1467
|
+
key: { t: number; v: readonly number[]; ease?: string; curve?: readonly number[] | 'stepped' },
|
|
1468
|
+
nextKey: { t: number; v: readonly number[] } | undefined,
|
|
1469
|
+
easings: Readonly<Record<string, EasingTuple>> | undefined,
|
|
1470
|
+
): number[] | 'stepped' | undefined {
|
|
1471
|
+
if (!nextKey) return undefined;
|
|
1472
|
+
if (key.curve !== undefined) {
|
|
1473
|
+
return key.curve === 'stepped' ? 'stepped' : [...key.curve];
|
|
1474
|
+
}
|
|
1475
|
+
if (key.ease === undefined) return undefined;
|
|
1476
|
+
if (key.ease === 'stepped') return 'stepped';
|
|
1477
|
+
const handles = easings?.[key.ease];
|
|
1478
|
+
if (!handles || handles.length !== 4) return undefined;
|
|
1479
|
+
const isHold =
|
|
1480
|
+
key.v.length === nextKey.v.length && key.v.every((v, c) => f32(v) === f32(nextKey.v[c]!));
|
|
1481
|
+
if (isHold) return 'stepped';
|
|
1482
|
+
const [hx1, hy1, hx2, hy2] = handles;
|
|
1483
|
+
const dt = nextKey.t - key.t;
|
|
1484
|
+
const out: number[] = [];
|
|
1485
|
+
for (let c = 0; c < key.v.length; c++) {
|
|
1486
|
+
const v1 = key.v[c]!;
|
|
1487
|
+
const dv = nextKey.v[c]! - v1;
|
|
1488
|
+
out.push(
|
|
1489
|
+
cleanMotionNumber(key.t + dt * hx1),
|
|
1490
|
+
cleanMotionNumber(v1 + dv * hy1),
|
|
1491
|
+
cleanMotionNumber(key.t + dt * hx2),
|
|
1492
|
+
cleanMotionNumber(v1 + dv * hy2),
|
|
1493
|
+
);
|
|
1494
|
+
}
|
|
1495
|
+
return out;
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
/**
|
|
1499
|
+
* Project an editable bone value track from a `rigc-motion/1` document into an
|
|
1500
|
+
* `InspectionTrack` so `evaluateStoredChannel` and `evaluateBoneLocals` in
|
|
1501
|
+
* `src/animation-inspection.ts` evaluate pending edits directly with no second evaluator.
|
|
1502
|
+
*/
|
|
1503
|
+
export function draftTrackToInspectionTrack(
|
|
1504
|
+
track: {
|
|
1505
|
+
bone: string;
|
|
1506
|
+
property: string;
|
|
1507
|
+
keys: ReadonlyArray<{
|
|
1508
|
+
t: number;
|
|
1509
|
+
v: readonly number[];
|
|
1510
|
+
ease?: string;
|
|
1511
|
+
curve?: readonly number[] | 'stepped';
|
|
1512
|
+
}>;
|
|
1513
|
+
},
|
|
1514
|
+
pointer = `/draft/bones/${track.bone}/${track.property}`,
|
|
1515
|
+
easings?: Readonly<Record<string, EasingTuple>>,
|
|
1516
|
+
): InspectionTrack {
|
|
1517
|
+
const channelSpec = CHANNELS[track.property];
|
|
1518
|
+
if (!channelSpec) {
|
|
1519
|
+
throw new Error(`ANIMATION_TRACK_READ_ONLY: unsupported bone property "${track.property}"`);
|
|
1520
|
+
}
|
|
1521
|
+
const storedKeys: StoredKey[] = track.keys.map((k, idx) => {
|
|
1522
|
+
const entry: StoredKey = { time: k.t };
|
|
1523
|
+
channelSpec.forEach((ch, c) => {
|
|
1524
|
+
entry[ch.field] = k.v[c]!;
|
|
1525
|
+
});
|
|
1526
|
+
const curve = resolveKeyCurveForInspection(k, track.keys[idx + 1], easings);
|
|
1527
|
+
if (curve !== undefined) {
|
|
1528
|
+
entry.curve = curve;
|
|
1529
|
+
}
|
|
1530
|
+
return entry;
|
|
1531
|
+
});
|
|
1532
|
+
return {
|
|
1533
|
+
kind: 'bone',
|
|
1534
|
+
target: track.bone,
|
|
1535
|
+
name: track.property,
|
|
1536
|
+
pointer,
|
|
1537
|
+
keys: storedKeys,
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
/**
|
|
1542
|
+
* Overlay all editable bone value tracks from a draft `rigc-motion/1` text onto
|
|
1543
|
+
* an existing `AnimationInspection`, returning a new `AnimationInspection` that
|
|
1544
|
+
* `evaluateStoredChannel` and `evaluateBoneLocals` can evaluate at any time `t`.
|
|
1545
|
+
*/
|
|
1546
|
+
export function overlayDraftOnInspection(
|
|
1547
|
+
inspection: AnimationInspection,
|
|
1548
|
+
draftMotionText: string,
|
|
1549
|
+
): AnimationInspection {
|
|
1550
|
+
const doc = JSON.parse(draftMotionText) as Record<string, unknown>;
|
|
1551
|
+
if (!isRecord(doc.animations)) return inspection;
|
|
1552
|
+
const anim = doc.animations[inspection.name];
|
|
1553
|
+
if (!isRecord(anim) || !Array.isArray(anim.tracks)) return inspection;
|
|
1554
|
+
const rawTracks = anim.tracks as unknown[];
|
|
1555
|
+
|
|
1556
|
+
const easings = isRecord(doc.easings)
|
|
1557
|
+
? (doc.easings as Readonly<Record<string, EasingTuple>>)
|
|
1558
|
+
: undefined;
|
|
1559
|
+
const classifications = classifyAnimationTracks(doc, inspection.name);
|
|
1560
|
+
const replacementByKey = new Map<string, InspectionTrack>();
|
|
1561
|
+
|
|
1562
|
+
classifications.forEach((cls, idx) => {
|
|
1563
|
+
if (!cls.editable) return;
|
|
1564
|
+
const rawTrack = rawTracks[idx] as {
|
|
1565
|
+
bone: string;
|
|
1566
|
+
property: string;
|
|
1567
|
+
keys: ReadonlyArray<{
|
|
1568
|
+
t: number;
|
|
1569
|
+
v: readonly number[];
|
|
1570
|
+
ease?: string;
|
|
1571
|
+
curve?: readonly number[] | 'stepped';
|
|
1572
|
+
}>;
|
|
1573
|
+
};
|
|
1574
|
+
const key = `${rawTrack.bone}\0${rawTrack.property}`;
|
|
1575
|
+
const existing = inspection.tracks.find(
|
|
1576
|
+
(t) => t.kind === 'bone' && t.target === rawTrack.bone && t.name === rawTrack.property,
|
|
1577
|
+
);
|
|
1578
|
+
replacementByKey.set(
|
|
1579
|
+
key,
|
|
1580
|
+
draftTrackToInspectionTrack(
|
|
1581
|
+
rawTrack,
|
|
1582
|
+
existing?.pointer ?? `/animations/0/bones/${idx}/timelines/0`,
|
|
1583
|
+
easings,
|
|
1584
|
+
),
|
|
1585
|
+
);
|
|
1586
|
+
});
|
|
1587
|
+
|
|
1588
|
+
const usedKeys = new Set<string>();
|
|
1589
|
+
const nextTracks: InspectionTrack[] = inspection.tracks.map((t) => {
|
|
1590
|
+
if (t.kind !== 'bone') return t;
|
|
1591
|
+
const key = `${t.target}\0${t.name}`;
|
|
1592
|
+
const rep = replacementByKey.get(key);
|
|
1593
|
+
if (!rep) return t;
|
|
1594
|
+
usedKeys.add(key);
|
|
1595
|
+
return rep;
|
|
1596
|
+
});
|
|
1597
|
+
|
|
1598
|
+
for (const [key, rep] of replacementByKey.entries()) {
|
|
1599
|
+
if (!usedKeys.has(key)) nextTracks.push(rep);
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
return {
|
|
1603
|
+
...inspection,
|
|
1604
|
+
tracks: nextTracks,
|
|
1605
|
+
};
|
|
1606
|
+
}
|