-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimationInstanceGradientPattern.ts
87 lines (79 loc) · 2.82 KB
/
AnimationInstanceGradientPattern.ts
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
import AnimationGradientPattern from "./AnimationGradientPattern";
import AnimationInstance from "./AnimationInstance";
import { AnimConstants } from "./Constants";
/**
* @category Animation Instance
*/
export default class AnimationInstanceGradientPattern extends AnimationInstance {
private _rgb = 0;
get preset(): AnimationGradientPattern {
return this.animationPreset as AnimationGradientPattern;
}
start(startTime: number): void {
super.start(startTime);
if (this.preset.overrideWithFace) {
this._rgb = this.bits.getColor32(
AnimConstants.paletteColorFromFace,
this.die
);
}
}
/// <summary>
/// Computes the list of LEDs that need to be on, and what their intensities should be
/// based on the different tracks of this animation.
/// </summary>
updateLEDs(ms: number, retIndices: number[], retColors32: number[]): number {
const preset = this.preset;
const ledCount = this.die.ledCount;
const time = ms - this.startTime;
const trackTime = (time * 1000) / preset.duration;
// Figure out the color from the gradient
const gradient = this.bits.getRgbTrack(preset.gradientTrackOffset);
let gradientColor = 0;
if (preset.overrideWithFace) {
gradientColor = this._rgb;
} else {
gradientColor = gradient.evaluateColor(trackTime, this.bits, this.die);
}
// Each track will append its led indices and colors into the return array
// The assumption is that led indices don't overlap between tracks of a single animation,
// so there will always be enough room in the return arrays.
let totalCount = 0;
const indices: number[] = [];
const colors32: number[] = [];
for (let i = 0; i < preset.trackCount; ++i) {
const track = this.bits.getTrack(preset.tracksOffset + i);
const count = track.evaluate(
this.bits,
gradientColor,
trackTime,
ledCount,
indices,
colors32
);
for (let j = 0; j < count; ++j) {
retIndices[totalCount + j] = indices[j];
retColors32[totalCount + j] = colors32[j];
}
totalCount += count;
}
return totalCount;
}
stop(retIndices: number[]): number {
const preset = this.preset;
// Each track will append its led indices and colors into the return array
// The assumption is that led indices don't overlap between tracks of a single animation,
// so there will always be enough room in the return arrays.
let totalCount = 0;
const indices: number[] = [];
for (let i = 0; i < preset.trackCount; ++i) {
const track = this.bits.getRgbTrack(preset.tracksOffset + i);
const count = track.extractLEDIndices(indices);
for (let j = 0; j < count; ++j) {
retIndices[totalCount + j] = indices[j];
}
totalCount += count;
}
return totalCount;
}
}