This repository was archived by the owner on Sep 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStationaryAnimation.ts
86 lines (75 loc) · 2.56 KB
/
StationaryAnimation.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
import { IAnimation } from "./Interfaces/IAnimation";
import { Led } from "./Led";
export class StationaryAnimation {
private animation: IAnimation;
private start: number;
private end: number;
/**
* Creates a new Stationary Animation, wrapping the LEDs passed to the Animation in order to constrain it to a portion of the Strip
* This will also call onInit with the right LED Array portion
* @param animation Animation to play
* @param start First LED for this Animation
* @param end Last LED for this Animation
* @param leds Original LED Array
*/
constructor(animation: IAnimation, start: number, end: number) {
//TODO: Constrain
this.animation = animation;
this.start = start;
this.end = end;
}
/**
* Moves the position of the Animation.
* !If changing the length call onInit again!
* @param start First LED for this Animation
* @param end Last LED for this Animation
*/
public changePosition(start: number, end: number): void {
//TODO: Constrain
this.start = start;
this.end = end;
}
/**
* Shifts the Animations Position
* @param amount Number of LEDs to shift
*/
public translatePosition(amount: number): void {
//TODO: Constrain
this.start += amount;
this.end += amount;
}
/**
* Swapes the Animation
* !Call onInit() again!
* @param animation New Animation
*/
public changeAnimation(animation: IAnimation): void {
this.animation = animation;
}
/**
* Init the Animation.
* This will slice the LED Array to the specific portion
* @param leds LEDs to Animate
*/
public onInit(leds: Array<Led>): void {
//TODO: Constrain
this.animation.onInit(leds.slice(this.start, this.end + 1));
// onInit should not and can't change LEDs
}
/**
* Update the Animation.
* This will slice the LED Array to the specific portion
* @param leds LEDs to Animate
*/
public update(leds: Array<Led>): void {
// map leds from whole strip to local animation strip
// that way animation can access it 0 based
const portionOfLEDs: Array<Led> = leds.slice(this.start, this.end + 1); // end is exlusive but we are treating it as inclusive
this.animation.update(portionOfLEDs, this.animation);
// copy back leds into whole strip
let j = 0;
for (let i = this.start; i <= this.end; i++) {
leds[i] = portionOfLEDs[j++];
}
}
}