forked from jwunderl/pxt-color
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fade.ts
264 lines (217 loc) · 7.25 KB
/
fade.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
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
namespace color {
export class Fade {
protected startTime: number;
protected duration: number;
protected _startPalette: Palette;
protected _endPalette: Palette;
constructor() { }
public startPalette(): Palette {
if (!this._startPalette) {
this._startPalette = currentPalette();
}
return this._startPalette;
}
public endPalette(): Palette {
if (!this._endPalette) {
this._endPalette = currentPalette();
}
return this._endPalette;
}
public isActive(): boolean {
return this.startTime !== undefined
&& this.duration !== undefined
&& this.startTime + this.duration > game.runtime();
}
public start(duration = 1000): Fade {
init();
this.duration = duration;
color.setPalette(this.startPalette());
this.startTime = game.runtime();
activeFade = this;
return this;
}
public startUntilDone(duration?: number): Fade {
this.start(duration);
return this.pauseUntilDone();
}
public stop(): Fade {
this.startTime = undefined;
return this;
}
public setStartPalette(colors: Palette): Fade {
this._startPalette = _clone(colors);
return this;
}
public setStartColor(index: number, col: Color): Fade {
this.startPalette().setColor(index, col);
return this;
}
public setEndPalette(colors: Palette): Fade {
this._endPalette = _clone(colors);
return this;
}
public setEndColor(index: number, col: Color): Fade {
this.endPalette().setColor(index, col);
return this;
}
public step(): boolean {
if (!this._endPalette || this.startTime === undefined) {
return true;
}
if (!this.isActive()) {
color.setPalette(this._endPalette);
this.startTime = undefined;
return true;
}
const time = game.runtime() - this.startTime;
const p = new Palette(this._startPalette.length);
for (let i = 0; i < p.length; ++i) {
const col = color.partialColorTransition(
this._startPalette.color(i),
this._endPalette.color(i),
time / this.duration
);
p.setColor(i, col);
}
color.setPalette(p);
return false;
}
public then(h: (fade: Fade) => void): Fade {
h(this);
return this;
}
public pauseUntilDone(): Fade {
pauseUntil(() => !this.isActive());
return this;
}
public reverse(): Fade {
const t = this._startPalette;
this._startPalette = this._endPalette;
this._endPalette = t;
return this;
}
public clone(): Fade {
const fade = new Fade();
if (this._startPalette)
fade._startPalette = _clone(this._startPalette);
if (this._endPalette)
fade._endPalette = _clone(this._endPalette);
return fade;
}
public mapEndRGB(
h: (rgb: RGB, index: number, palette: Palette) => RGB,
firstIndex?: number,
lastIndex?: number
): Fade {
const out = this.clone();
const p = _clone(this.endPalette());
firstIndex = firstIndex | 0;
lastIndex = lastIndex === undefined
? p.length - 1
: Math.min(p.length - 1, lastIndex);
for (let i = firstIndex; i <= lastIndex; ++i) {
const initRGB = RGB.fromHexValue(p.color(i));
const applied = h(initRGB, i, p);
p.setColor(i, applied.hexValue());
}
return out
.setEndPalette(p);
}
public mapEndHSL(
h: (hsl: HSL, index: number, palette: Palette) => HSL,
firstIndex?: number,
lastIndex?: number
): Fade {
const out = this.clone();
const p = _clone(this.endPalette());
firstIndex = firstIndex | 0;
lastIndex = lastIndex === undefined
? p.length - 1
: Math.min(p.length - 1, lastIndex);
for (let i = firstIndex; i <= lastIndex; ++i) {
const initHSL = HSL.fromHexValue(p.color(i));
const applied = h(initHSL, i, p);
p.setColor(i, applied.hexValue());
}
return out
.setEndPalette(p);
}
}
class FadeState {
constructor(
public state: Fade,
public scene: scene.Scene
) { }
}
let activeFade: Fade;
let currentScene: scene.Scene;
let FadeStack: FadeState[];
game.addScenePushHandler(() => {
if (currentScene) {
if (!FadeStack) FadeStack = [];
FadeStack.push(
new FadeState(
activeFade,
currentScene
)
);
activeFade = undefined;
currentScene = undefined;
}
});
game.addScenePopHandler(() => {
const scene = game.currentScene();
currentScene = undefined;
activeFade = undefined;
if (FadeStack && FadeStack.length) {
const nextState = FadeStack.pop();
if (nextState.scene === scene) {
activeFade = nextState.state;
currentScene = nextState.scene;
} else {
FadeStack.push(nextState);
}
}
});
function init() {
if (!currentScene) {
game.forever(() => {
if (activeFade) {
const finished = activeFade.step();
if (finished) {
activeFade = undefined;
}
}
});
currentScene = game.currentScene();
}
}
/**
* Create a Fade from start to end that occurs over the given duration
*/
//% blockId=colorStartFade block="fade from %start to %end||over %duration ms"
//% weight=80
//% duration.shadow=timePicker
export function startFade(start: Palette, end: Palette, duration = 2000) {
if (!start || !end || start.length !== end.length)
return;
activeFade = new Fade();
activeFade.setStartPalette(start);
activeFade.setEndPalette(end);
activeFade.start(duration)
}
export function startFadeUntilDone(start: Palette, end: Palette, duration?: number) {
startFade(start, end, duration);
pauseUntilFadeDone();
}
/**
* Pause until the current fade is completed
*/
//% blockId=colorPauseUntilFadeDone block="pause until current fade done"
//% weight=50
export function pauseUntilFadeDone() {
if (activeFade) {
activeFade.pauseUntilDone();
}
}
}