-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Flash.js
356 lines (311 loc) · 10.4 KB
/
Flash.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
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Clamp = require('../../../math/Clamp');
var Class = require('../../../utils/Class');
var Events = require('../events');
/**
* @classdesc
* A Camera Flash effect.
*
* This effect will flash the camera viewport to the given color, over the duration specified.
*
* Only the camera viewport is flashed. None of the objects it is displaying are impacted, i.e. their colors do
* not change.
*
* The effect will dispatch several events on the Camera itself and you can also specify an `onUpdate` callback,
* which is invoked each frame for the duration of the effect, if required.
*
* @class Flash
* @memberof Phaser.Cameras.Scene2D.Effects
* @constructor
* @since 3.5.0
*
* @param {Phaser.Cameras.Scene2D.Camera} camera - The camera this effect is acting upon.
*/
var Flash = new Class({
initialize:
function Flash (camera)
{
/**
* The Camera this effect belongs to.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#camera
* @type {Phaser.Cameras.Scene2D.Camera}
* @readonly
* @since 3.5.0
*/
this.camera = camera;
/**
* Is this effect actively running?
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#isRunning
* @type {boolean}
* @readonly
* @default false
* @since 3.5.0
*/
this.isRunning = false;
/**
* The duration of the effect, in milliseconds.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#duration
* @type {number}
* @readonly
* @default 0
* @since 3.5.0
*/
this.duration = 0;
/**
* The value of the red color channel the camera will use for the flash effect.
* A value between 0 and 255.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#red
* @type {number}
* @private
* @since 3.5.0
*/
this.red = 0;
/**
* The value of the green color channel the camera will use for the flash effect.
* A value between 0 and 255.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#green
* @type {number}
* @private
* @since 3.5.0
*/
this.green = 0;
/**
* The value of the blue color channel the camera will use for the flash effect.
* A value between 0 and 255.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#blue
* @type {number}
* @private
* @since 3.5.0
*/
this.blue = 0;
/**
* The value of the alpha channel used during the flash effect.
* A value between 0 and 1.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#alpha
* @type {number}
* @since 3.5.0
*/
this.alpha = 1;
/**
* If this effect is running this holds the current percentage of the progress, a value between 0 and 1.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#progress
* @type {number}
* @since 3.5.0
*/
this.progress = 0;
/**
* Effect elapsed timer.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#_elapsed
* @type {number}
* @private
* @since 3.5.0
*/
this._elapsed = 0;
/**
* This is an internal copy of the initial value of `this.alpha`, used to calculate the current alpha value of the fade effect.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#_alpha
* @type {number}
* @private
* @readonly
* @since 3.60.0
*/
this._alpha;
/**
* This callback is invoked every frame for the duration of the effect.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#_onUpdate
* @type {?Phaser.Types.Cameras.Scene2D.CameraFlashCallback}
* @private
* @default null
* @since 3.5.0
*/
this._onUpdate;
/**
* On Complete callback scope.
*
* @name Phaser.Cameras.Scene2D.Effects.Flash#_onUpdateScope
* @type {any}
* @private
* @since 3.5.0
*/
this._onUpdateScope;
},
/**
* Flashes the Camera to or from the given color over the duration specified.
*
* @method Phaser.Cameras.Scene2D.Effects.Flash#start
* @fires Phaser.Cameras.Scene2D.Events#FLASH_START
* @fires Phaser.Cameras.Scene2D.Events#FLASH_COMPLETE
* @since 3.5.0
*
* @param {number} [duration=250] - The duration of the effect in milliseconds.
* @param {number} [red=255] - The amount to flash the red channel towards. A value between 0 and 255.
* @param {number} [green=255] - The amount to flash the green channel towards. A value between 0 and 255.
* @param {number} [blue=255] - The amount to flash the blue channel towards. A value between 0 and 255.
* @param {boolean} [force=false] - Force the effect to start immediately, even if already running.
* @param {Phaser.Types.Cameras.Scene2D.CameraFlashCallback} [callback] - This callback will be invoked every frame for the duration of the effect.
* It is sent two arguments: A reference to the camera and a progress amount between 0 and 1 indicating how complete the effect is.
* @param {any} [context] - The context in which the callback is invoked. Defaults to the Scene to which the Camera belongs.
*
* @return {Phaser.Cameras.Scene2D.Camera} The Camera on which the effect was started.
*/
start: function (duration, red, green, blue, force, callback, context)
{
if (duration === undefined) { duration = 250; }
if (red === undefined) { red = 255; }
if (green === undefined) { green = 255; }
if (blue === undefined) { blue = 255; }
if (force === undefined) { force = false; }
if (callback === undefined) { callback = null; }
if (context === undefined) { context = this.camera.scene; }
if (!force && this.isRunning)
{
return this.camera;
}
this.isRunning = true;
this.duration = duration;
this.progress = 0;
this.red = red;
this.green = green;
this.blue = blue;
this._alpha = this.alpha;
this._elapsed = 0;
this._onUpdate = callback;
this._onUpdateScope = context;
this.camera.emit(Events.FLASH_START, this.camera, this, duration, red, green, blue);
return this.camera;
},
/**
* The main update loop for this effect. Called automatically by the Camera.
*
* @method Phaser.Cameras.Scene2D.Effects.Flash#update
* @since 3.5.0
*
* @param {number} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
* @param {number} delta - The delta time, in ms, elapsed since the last frame.
*/
update: function (time, delta)
{
if (!this.isRunning)
{
return;
}
this._elapsed += delta;
this.progress = Clamp(this._elapsed / this.duration, 0, 1);
if (this._onUpdate)
{
this._onUpdate.call(this._onUpdateScope, this.camera, this.progress);
}
if (this._elapsed < this.duration)
{
this.alpha = this._alpha * (1 - this.progress);
}
else
{
this.effectComplete();
}
},
/**
* Called internally by the Canvas Renderer.
*
* @method Phaser.Cameras.Scene2D.Effects.Flash#postRenderCanvas
* @since 3.5.0
*
* @param {CanvasRenderingContext2D} ctx - The Canvas context to render to.
*
* @return {boolean} `true` if the effect drew to the renderer, otherwise `false`.
*/
postRenderCanvas: function (ctx)
{
if (!this.isRunning)
{
return false;
}
var camera = this.camera;
ctx.fillStyle = 'rgba(' + this.red + ',' + this.green + ',' + this.blue + ',' + this.alpha + ')';
ctx.fillRect(camera.x, camera.y, camera.width, camera.height);
return true;
},
/**
* Called internally by the WebGL Renderer.
*
* @method Phaser.Cameras.Scene2D.Effects.Flash#postRenderWebGL
* @since 3.5.0
*
* @param {Phaser.Renderer.WebGL.Pipelines.MultiPipeline} pipeline - The WebGL Pipeline to render to. Must provide the `drawFillRect` method.
* @param {function} getTintFunction - A function that will return the gl safe tint colors.
*
* @return {boolean} `true` if the effect drew to the renderer, otherwise `false`.
*/
postRenderWebGL: function (pipeline, getTintFunction)
{
if (!this.isRunning)
{
return false;
}
var camera = this.camera;
var red = this.red / 255;
var green = this.green / 255;
var blue = this.blue / 255;
pipeline.drawFillRect(
camera.x, camera.y, camera.width, camera.height,
getTintFunction(blue, green, red, 1),
this.alpha
);
return true;
},
/**
* Called internally when the effect completes.
*
* @method Phaser.Cameras.Scene2D.Effects.Flash#effectComplete
* @fires Phaser.Cameras.Scene2D.Events#FLASH_COMPLETE
* @since 3.5.0
*/
effectComplete: function ()
{
this.alpha = this._alpha;
this._onUpdate = null;
this._onUpdateScope = null;
this.isRunning = false;
this.camera.emit(Events.FLASH_COMPLETE, this.camera, this);
},
/**
* Resets this camera effect.
* If it was previously running, it stops instantly without calling its onComplete callback or emitting an event.
*
* @method Phaser.Cameras.Scene2D.Effects.Flash#reset
* @since 3.5.0
*/
reset: function ()
{
this.isRunning = false;
this._onUpdate = null;
this._onUpdateScope = null;
},
/**
* Destroys this effect, releasing it from the Camera.
*
* @method Phaser.Cameras.Scene2D.Effects.Flash#destroy
* @since 3.5.0
*/
destroy: function ()
{
this.reset();
this.camera = null;
}
});
module.exports = Flash;