-
Notifications
You must be signed in to change notification settings - Fork 10
/
PFTween.js
701 lines (701 loc) · 26.5 KB
/
PFTween.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PFTween = exports.Ease = void 0;
const Time_1 = __importDefault(require("Time"));
const Animation_1 = __importDefault(require("Animation"));
const Reactive_1 = __importDefault(require("Reactive"));
const Patches_1 = __importDefault(require("Patches"));
const samplers = {
linear: Animation_1.default.samplers.linear,
easeInQuad: Animation_1.default.samplers.easeInQuad,
easeOutQuad: Animation_1.default.samplers.easeOutQuad,
easeInOutQuad: Animation_1.default.samplers.easeInOutQuad,
easeInCubic: Animation_1.default.samplers.easeInCubic,
easeOutCubic: Animation_1.default.samplers.easeOutCubic,
easeInOutCubic: Animation_1.default.samplers.easeInOutCubic,
easeInQuart: Animation_1.default.samplers.easeInQuart,
easeOutQuart: Animation_1.default.samplers.easeOutQuart,
easeInOutQuart: Animation_1.default.samplers.easeInOutQuart,
easeInQuint: Animation_1.default.samplers.easeInQuint,
easeOutQuint: Animation_1.default.samplers.easeOutQuint,
easeInOutQuint: Animation_1.default.samplers.easeInOutQuint,
easeInSine: Animation_1.default.samplers.easeInSine,
easeOutSine: Animation_1.default.samplers.easeOutSine,
easeInOutSine: Animation_1.default.samplers.easeInOutSine,
easeInExpo: Animation_1.default.samplers.easeInExpo,
easeOutExpo: Animation_1.default.samplers.easeOutExpo,
easeInOutExpo: Animation_1.default.samplers.easeInOutExpo,
easeInCirc: Animation_1.default.samplers.easeInCirc,
easeOutCirc: Animation_1.default.samplers.easeOutCirc,
easeInOutCirc: Animation_1.default.samplers.easeInOutCirc,
easeInBack: Animation_1.default.samplers.easeInBack,
easeOutBack: Animation_1.default.samplers.easeOutBack,
easeInOutBack: Animation_1.default.samplers.easeInOutBack,
easeInElastic: Animation_1.default.samplers.easeInElastic,
easeOutElastic: Animation_1.default.samplers.easeOutElastic,
easeInOutElastic: Animation_1.default.samplers.easeInOutElastic,
easeInBounce: Animation_1.default.samplers.easeInBounce,
easeOutBounce: Animation_1.default.samplers.easeOutBounce,
easeInOutBounce: Animation_1.default.samplers.easeInOutBounce,
linearPingPong: (begin, end) => Animation_1.default.samplers.polyline({ keyframes: [begin, end, begin] }),
easePingPong: (begin, end) => Animation_1.default.samplers.polybezier({ keyframes: [begin, end, begin] }),
punch: (begin, amount) => {
if (Array.isArray(begin) && Array.isArray(amount)) {
return Animation_1.default.samplers.polybezier({
keyframes: [
[begin[0] + (amount[0] / 5) * 4, begin[1] + (amount[1] / 5) * 4, begin[2] + (amount[2] / 5) * 4],
[begin[0] - (amount[0] / 5) * 3, begin[1] - (amount[1] / 5) * 3, begin[2] - (amount[2] / 5) * 3],
[begin[0] + (amount[0] / 5) * 2, begin[1] + (amount[1] / 5) * 2, begin[2] + (amount[2] / 5) * 2],
[begin[0] - (amount[0] / 5) * 1, begin[1] - (amount[1] / 5) * 1, begin[2] - (amount[2] / 5) * 1],
[begin[0], begin[1], begin[2]]
]
});
}
else {
return Animation_1.default.samplers.polybezier({
keyframes: [
begin + (amount / 5) * 4,
begin - (amount / 5) * 3,
begin + (amount / 5) * 2,
begin - (amount / 5) * 1,
begin
]
});
}
},
};
exports.Ease = samplers;
const degreeToRadian = 0.0174532925;
const PFTweenManager = new class {
constructor() {
this.table = {};
}
onKill(id, callback) {
if (this.table[id] == undefined) {
this.table[id] = [];
}
this.table[id].push(callback);
}
kill(id) {
if (this.table[id] != undefined) {
this.table[id].forEach(callback => callback());
delete this.table[id];
}
}
hasId(id) {
return this.table[id] != undefined;
}
};
class PFTweenEvent {
constructor() {
this._events = [];
}
get events() {
return this._events;
}
invoke(arg) {
if (this._events.length > 0) {
for (let i = 0; i < this._events.length; i++) {
this._events[i](arg);
}
}
;
}
invokeOnMonitor(signal) {
if (this._events.length == 0) {
return;
}
if (Array.isArray(signal)) {
let list = {};
for (let i = 0; i < signal.length; i++) {
list[i] = signal[i];
}
this._subscription = Reactive_1.default.monitorMany(list, { fireOnInitialValue: true }).subscribe(values => this.invoke(Object.values(values.newValues)));
}
else {
this._subscription = signal.monitor({ fireOnInitialValue: true }).select('newValue').subscribe(value => this.invoke(value));
}
}
invokeOnEvent(eventSource) {
if (this._events.length > 0) {
this._subscription = eventSource.subscribe(value => this.invoke(value));
}
}
add(callback) {
this._events.push(callback);
}
dispose() {
if (this._subscription != undefined) {
this._subscription.unsubscribe();
this._subscription = undefined;
}
this._events = [];
}
}
class PFTweenEvents {
constructor() {
this.onStart = new PFTweenEvent();
this.onComplete = new PFTweenEvent();
this.onLoop = new PFTweenEvent();
this.onUpdate = new PFTweenEvent();
this.onFinally = new PFTweenEvent();
}
dispose() {
this.onStart.dispose();
this.onComplete.dispose();
this.onLoop.dispose();
this.onUpdate.dispose();
this.onFinally.dispose();
}
}
class PFTweenClipCancellation {
constructor(value) {
this.value = value;
}
cancel() { }
}
function instanceOfICurveProvider(object) {
return 'evaluate' in object;
}
class PFTween {
constructor(begin, end, durationMilliseconds) {
begin = toNumber(begin);
end = toNumber(end);
this.config = {
loopCount: undefined,
isMirror: false,
durationMilliseconds: durationMilliseconds,
delayMilliseconds: 0,
events: new PFTweenEvents(),
id: undefined,
useCustomCurve: false,
curve: undefined,
begin: begin,
end: end,
sampler: samplers.linear(begin, end),
};
}
static combine(...clips) {
clips = clips.flat();
return value => Promise.all(clips.map(i => i(value))).then(endValues => Promise.resolve(value != undefined ? value : endValues));
}
static concat(...clips) {
clips = clips.flat();
return value => clips.slice(1).reduce((pre, cur) => pre.then(cur), clips[0](value));
}
static combineProgress(...progresses) {
progresses = progresses.flat();
const max = Math.max(...progresses.map(pftween => pftween.durationMilliseconds));
return {
get durationMilliseconds() { return max; },
setProgress(progress) {
for (let i = 0; i < progresses.length; i++) {
const tween = progresses[i];
tween.setProgress(Reactive_1.default.fromRange(progress, 0, tween.durationMilliseconds / max));
}
}
};
}
static concatProgerss(...progresses) {
progresses = progresses.flat();
return PFTween.concatProgress(progresses);
}
static concatProgress(...progresses) {
progresses = progresses.flat();
const total = progresses
.map(pftween => pftween.durationMilliseconds)
.reduce((pre, cur) => pre + cur, 0);
return {
get durationMilliseconds() { return total; },
setProgress(progress) {
let start = 0;
let startTimings = [0];
for (let i = 0; i < progresses.length; i++) {
startTimings.push(start + progresses[i].durationMilliseconds / total);
}
for (let i = progresses.length - 1; i >= 0; i--) {
const tween = progresses[i];
start = startTimings[i];
const end = start + tween.durationMilliseconds / total;
tween.setProgress(Reactive_1.default.fromRange(progress, start, end));
}
}
};
}
/**
* Stop and unsubscribe all events of the animation.
* @param id The id you set for to your animation.
*/
static kill(id) {
PFTweenManager.kill(id);
}
/**
* Check if the animation of this id exist.
* @param id
* @returns
*/
static hasId(id) {
return PFTweenManager.hasId(id);
}
/**
* Create a cancellation used for interrupt clips.
* @param value The args you want to pass to clip.
* @returns
*/
static newClipCancellation(value) {
return new PFTweenClipCancellation(value);
}
setEase() {
if (arguments.length == 1 && instanceOfICurveProvider(arguments[0])) {
this.config.useCustomCurve = true;
this.config.curve = arguments[0];
this.config.sampler = undefined;
}
else if (arguments.length == 1) {
this.config.useCustomCurve = false;
this.config.curve = undefined;
this.config.sampler = arguments[0](this.config.begin, this.config.end);
}
return this;
}
setLoops() {
if (arguments.length == 0) {
this.config.loopCount = Infinity;
}
else if (arguments.length == 1 && typeof arguments[0] == 'boolean') {
this.config.loopCount = Infinity;
this.config.isMirror = arguments[0];
}
else if (arguments.length == 1 && typeof arguments[0] == 'number') {
this.config.loopCount = arguments[0];
}
else if (arguments.length == 2 && typeof arguments[0] == 'number' && typeof arguments[1] == 'boolean') {
this.config.loopCount = arguments[0];
this.config.isMirror = arguments[1];
}
return this;
}
setMirror(isMirror = true) {
this.config.isMirror = isMirror;
return this;
}
/**
* Delay to start animation, this will only delay the first time.
*/
setDelay(delayMilliseconds) {
this.config.delayMilliseconds = delayMilliseconds;
return this;
}
setId(id) {
this.config.id = id;
return this;
}
setAutoKill() {
if (this.config.id == undefined) {
this.setId(Symbol());
}
this.config.events.onFinally.add(() => PFTweenManager.kill(this.config.id));
return this;
}
onStart(callback) {
this.config.events.onStart.add(callback);
return this;
}
onComplete(callback) {
this.config.events.onComplete.add(callback);
return this;
}
onUpdate(callback) {
this.config.events.onUpdate.add(callback);
return this;
}
onLoop(callback) {
this.config.events.onLoop.add(callback);
return this;
}
onStartVisible(sceneObject) {
this.onStart(() => sceneObject.hidden = Reactive_1.default.val(false));
return this;
}
onAnimatingVisibleOnly(sceneObject) {
this.onStartVisible(sceneObject);
this.onCompleteHidden(sceneObject);
return this;
}
onStartHidden(sceneObject) {
this.onStart(() => sceneObject.hidden = Reactive_1.default.val(true));
return this;
}
onCompleteVisible(sceneObject) {
this.onComplete(() => sceneObject.hidden = Reactive_1.default.val(false));
return this;
}
onCompleteHidden(sceneObject) {
this.onComplete(() => sceneObject.hidden = Reactive_1.default.val(true));
return this;
}
onCompleteResetPosition(sceneObject) {
const original = Reactive_1.default.pack3(sceneObject.transform.x.pinLastValue(), sceneObject.transform.y.pinLastValue(), sceneObject.transform.z.pinLastValue());
this.onComplete(() => sceneObject.transform.position = original);
return this;
}
onCompleteResetRotation(sceneObject) {
const original = {
x: sceneObject.transform.rotationX.pinLastValue(),
y: sceneObject.transform.rotationY.pinLastValue(),
z: sceneObject.transform.rotationZ.pinLastValue(),
};
this.onComplete(() => {
sceneObject.transform.rotationX = original.x;
sceneObject.transform.rotationY = original.y;
sceneObject.transform.rotationZ = original.z;
});
return this;
}
onCompleteResetScale(sceneObject) {
const original = Reactive_1.default.scale(sceneObject.transform.scaleX.pinLastValue(), sceneObject.transform.scaleY.pinLastValue(), sceneObject.transform.scaleZ.pinLastValue());
this.onComplete(() => sceneObject.transform.scale = original);
return this;
}
onCompleteResetOpacity(material) {
const original = material.opacity.pinLastValue();
this.onComplete(() => material.opacity = original);
return this;
}
/**
* @deprecated Please use `onStart(callback)` instead. The callback of `onStart` receive the tween value. `bind` is equivalent to `onStart` now.
*/
bind(callback) {
this.onStart(callback);
return this;
}
/**
* @deprecated Please use `build()` instead. `apply` is equivalent to `build` now.
*/
apply(autoPlay = true) {
return this.build(autoPlay);
}
build(autoPlay = true) {
const tweener = new PFTweener(this.config);
if (autoPlay) {
tweener.start();
}
return tweener;
}
/**
* Take input numbers and output them in a different order.
* Input values correspond to the swizzle value (xyzw) in the order theyre inputted. For example, an input of (1,2,3) and a swizzle value of (yxz) would output (2,1,3). You can also use 0 and 1. For example, a swizzle value of (x01) would output (1,0,1).
*/
swizzle(specifier) {
return this.setAutoKill().build().swizzle(specifier);
}
/**
* Convert
* @param name
* @returns
*/
patch(name) { return this.setAutoKill().build().patch(name); }
/**
* Get an evaluable animation controller.
* Please note that the `onCompleteEvent`, `onStartEvent`, `onLoopEvent` won't work.
*/
get progress() {
this.config.events.onComplete.dispose();
this.config.events.onStart.dispose();
this.config.events.onLoop.dispose();
return new PFTweenProgress(this.config);
}
get clip() {
let promiseResolve, resolveResult;
this.onComplete(() => promiseResolve(resolveResult && (resolveResult.value || resolveResult.cancel) ? resolveResult : { value: resolveResult }));
const tweener = this.build(false);
return result => new Promise((resolve, reject) => {
PFTweenManager.onKill(this.config.id, () => reject(`PFTween killed: ${String(this.config.id)}`));
resolveResult = result;
promiseResolve = resolve;
tweener.replay();
if (result instanceof PFTweenClipCancellation) {
result.cancel = () => {
tweener.stop();
reject('PFTween clip canceled');
};
}
});
}
get scalar() { return this.setAutoKill().build().scalar; }
get pack2() { return this.setAutoKill().build().pack2; }
get pack3() { return this.setAutoKill().build().pack3; }
get pack4() { return this.setAutoKill().build().pack4; }
/**
* @deprecated Please use `pack3` instead. `scale` is equivalent to `pack3` now.
*/
get scale() { return this.pack3; }
get quaternion() { return this.setAutoKill().build().quaternion; }
get rgba() { return this.setAutoKill().build().rgba; }
/**
* @deprecated Please use `deg2rad` instead. `rotation` is equivalent to `deg2rad` now.
*/
get rotation() { return this.setAutoKill().build().rotation; }
get deg2rad() { return this.setAutoKill().build().deg2rad; }
get durationMilliseconds() { return this.config.durationMilliseconds; }
}
exports.PFTween = PFTween;
class PFTweenValue {
constructor(animate) {
this.rawValue = animate;
}
/**
* Take input numbers and output them in a different order.
* Input values correspond to the swizzle value (xyzw) in the order theyre inputted. For example, an input of (1,2,3) and a swizzle value of (yxz) would output (2,1,3). You can also use 0 and 1. For example, a swizzle value of (x01) would output (1,0,1).
*/
swizzle(specifier) {
return swizzle(this.rawValue, specifier);
}
patch(name) {
if (!Array.isArray(this.rawValue)) {
Patches_1.default.inputs.setScalar(name, this.rawValue);
}
else {
switch (this.rawValue.length) {
case 2:
Patches_1.default.inputs.setPoint2D(name, Reactive_1.default.pack2(this.rawValue[0], this.rawValue[1]));
break;
case 3:
Patches_1.default.inputs.setPoint(name, Reactive_1.default.pack3(this.rawValue[0], this.rawValue[1], this.rawValue[2]));
break;
case 4:
Patches_1.default.inputs.setColor(name, Reactive_1.default.RGBA(this.rawValue[0], this.rawValue[1], this.rawValue[2], this.rawValue[3]));
break;
default:
throw `Unsupported value length: ${this.rawValue.length} values from script to patch with the name '${name}'`;
}
}
}
get scalar() {
if (Array.isArray(this.rawValue)) {
return this.rawValue[0];
}
else {
return this.rawValue;
}
}
get pack2() {
if (Array.isArray(this.rawValue)) {
return Reactive_1.default.pack2(this.rawValue[0] ? this.rawValue[0] : 0, this.rawValue[1] ? this.rawValue[1] : 0);
}
else {
return Reactive_1.default.pack2(this.scalar, this.scalar);
}
}
get pack3() {
if (Array.isArray(this.rawValue)) {
return Reactive_1.default.pack3(this.rawValue[0] ? this.rawValue[0] : 0, this.rawValue[1] ? this.rawValue[1] : 0, this.rawValue[2] ? this.rawValue[2] : 0);
}
else {
return Reactive_1.default.pack3(this.scalar, this.scalar, this.scalar);
}
}
/**
* @deprecated Please use `pack3` instead. `scale` is equivalent to `pack3` now.
*/
get scale() {
return this.pack3;
}
get pack4() {
if (Array.isArray(this.rawValue)) {
return Reactive_1.default.pack4(this.rawValue[0] ? this.rawValue[0] : 0, this.rawValue[1] ? this.rawValue[1] : 0, this.rawValue[2] ? this.rawValue[2] : 0, this.rawValue[3] ? this.rawValue[3] : 0);
}
else {
return Reactive_1.default.pack4(this.scalar, this.scalar, this.scalar, this.scalar);
}
}
get quaternion() {
if (Array.isArray(this.rawValue) && this.rawValue.length == 4) {
return Reactive_1.default.quaternion(this.rawValue[3], this.rawValue[0], this.rawValue[1], this.rawValue[2]);
}
else {
throw `The length of tween value' mismatched, 'quaternion' expected 4 numbers but got ${Array.isArray(this.rawValue) ? this.rawValue.length : 1}.`;
}
}
get rgba() {
return this.pack4.toRGBA();
}
/**
* @deprecated Please use `deg2rad` instead. `rotation` is equivalent to `deg2rad` now.
*/
get rotation() {
return this.deg2rad;
}
get deg2rad() {
return this.scalar.mul(degreeToRadian);
}
}
class PFTweenProgress {
constructor(config) {
this.durationMilliseconds = config.durationMilliseconds;
this.config = config;
}
setProgress(progress) {
const driver = Animation_1.default.valueDriver(progress, 0, 1);
if (this.config.useCustomCurve) {
const progress = Animation_1.default.animate(driver, Animation_1.default.samplers.linear(0, 1));
const y = this.config.curve.evaluate(progress);
this.config.events.onUpdate.invokeOnMonitor(Animation_1.default.animate(Animation_1.default.valueDriver(y, 0, 1), Animation_1.default.samplers.linear(this.config.begin, this.config.end)));
}
else {
this.config.events.onUpdate.invokeOnMonitor(Animation_1.default.animate(driver, this.config.sampler));
}
}
}
class PFTweener extends PFTweenValue {
constructor(config) {
const driver = Animation_1.default.timeDriver({
durationMilliseconds: config.durationMilliseconds,
loopCount: config.loopCount,
mirror: config.isMirror
});
const tween = super((() => {
if (config.useCustomCurve) {
const progress = Animation_1.default.animate(driver, Animation_1.default.samplers.linear(0, 1));
const y = config.curve.evaluate(progress);
return Animation_1.default.animate(Animation_1.default.valueDriver(y, 0, 1), Animation_1.default.samplers.linear(config.begin, config.end));
}
else {
return Animation_1.default.animate(driver, config.sampler);
}
})());
// prevent unnecessary subscription
if (config.loopCount != Infinity) {
config.events.onComplete.invokeOnEvent(driver.onCompleted());
}
else {
config.events.onComplete.dispose();
}
config.events.onLoop.invokeOnEvent(driver.onAfterIteration());
config.events.onUpdate.invokeOnMonitor(tween.rawValue);
this.driver = driver;
this.config = config;
this.play = () => {
config.events.onStart.invoke(tween);
driver.start();
};
if (config.id != undefined) {
PFTweenManager.onKill(config.id, () => {
config.events.dispose();
driver.stop();
});
}
}
start() {
if (this.config.delayMilliseconds != 0) {
Time_1.default.setTimeout(this.play, this.config.delayMilliseconds);
}
else {
this.play();
}
}
replay() {
this.stop(true);
this.start();
}
reset() {
this.driver.reset();
}
reverse() {
this.driver.reverse();
}
stop(reset = false) {
this.driver.stop();
if (reset) {
this.driver.reset();
}
}
get isRunning() {
return this.driver.isRunning();
}
}
/** Convert scalar signal to number, or the signal that contains 'xyzw' to array of numbers.*/
function toNumber(signal) {
if (typeof signal == 'number') {
return signal;
}
if (Array.isArray(signal)) {
return signal;
}
if (signal.pinLastValue) {
if (typeof signal.pinLastValue() == 'number') {
return signal.pinLastValue();
}
else {
let arr = [];
if (signal.x && signal.x.pinLastValue)
arr.push(signal.x.pinLastValue());
if (signal.y && signal.y.pinLastValue)
arr.push(signal.y.pinLastValue());
if (signal.z && signal.z.pinLastValue)
arr.push(signal.z.pinLastValue());
if (signal.w && signal.w.pinLastValue)
arr.push(signal.w.pinLastValue());
return arr;
}
}
return undefined;
}
/**
* Take input numbers and output them in a different order.
* Input values correspond to the swizzle value (xyzw) in the order theyre inputted. For example, an input of (1,2,3) and a swizzle value of (yxz) would output (2,1,3). You can also use 0 and 1. For example, a swizzle value of (x01) would output (1,0,1).
*/
function swizzle(value, specifier) {
const isArray = Array.isArray(value);
const signal = (element) => {
const swizzleSignal = (property) => {
if (typeof (value) == 'number') {
if (property == 'x') {
return value;
}
else {
throw `Specifier '${property}' in '${specifier}' can't be used with this signal.`;
}
}
else if (value['pinLastValue'] != undefined && typeof value.pinLastValue() == 'number') {
if (property == 'x') {
return value;
}
else {
throw `Specifier '${property}' in '${specifier}' can't be used with this signal.`;
}
}
else {
if (value[property] == undefined) {
throw `Specifier '${property}' in '${specifier}' can't be used with this signal.`;
}
else {
return value[property];
}
}
};
switch (element) {
case '0': return 0;
case '1': return 1;
case 'x': return isArray ? (value[0] ? value[0] : 0) : swizzleSignal('x');
case 'y': return isArray ? (value[1] ? value[1] : 0) : swizzleSignal('y');
case 'z': return isArray ? (value[2] ? value[2] : 0) : swizzleSignal('z');
case 'w': return isArray ? (value[3] ? value[3] : 0) : swizzleSignal('w');
case 'r': return isArray ? (value[0] ? value[0] : 0) : swizzleSignal('x');
case 'g': return isArray ? (value[1] ? value[1] : 0) : swizzleSignal('y');
case 'b': return isArray ? (value[2] ? value[2] : 0) : swizzleSignal('z');
case 'a': return isArray ? (value[3] ? value[3] : 0) : swizzleSignal('w');
default: throw `Invalid swizzle element specifier: '${element}' in '${specifier}'`;
}
};
switch (specifier.length) {
case 1: return signal(specifier[0]);
case 2: return Reactive_1.default.pack2(signal(specifier[0]), signal(specifier[1]));
case 3: return Reactive_1.default.pack3(signal(specifier[0]), signal(specifier[1]), signal(specifier[2]));
case 4: return Reactive_1.default.pack4(signal(specifier[0]), signal(specifier[1]), signal(specifier[2]), signal(specifier[3]));
default: throw `Invalid swizzle specifier: '${specifier}'`;
}
}