Skip to content

Commit 25c1a5f

Browse files
committed
Added Texture Manager Events and moved 'ready' responsibility to it
1 parent 1d85795 commit 25c1a5f

11 files changed

Lines changed: 139 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ one set of bindings ever created, which makes things a lot cleaner.
297297
* The keyed Data Manager change data event string has changed from `changedata_` to `changedata-` to keep it consistent with other keyed events.
298298
* The Keyboard Plugin `keydown` dynamic event string has changed from `keydown_` to `keydown-` to keep it consistent with other keyed events. Note the change from `_` to `-`.
299299
* The Keyboard Plugin `keyup` dynamic event string has changed from `keyup_` to `keyup-` to keep it consistent with other keyed events. Note the change from `_` to `-`.
300+
* The `texturesready` event emitted by the Texture Manager has changed to be just `ready`.
300301

301302
### Examples and TypeScript
302303

src/core/Game.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var PluginManager = require('../plugins/PluginManager');
2323
var ScaleManager = require('../dom/ScaleManager');
2424
var SceneManager = require('../scene/SceneManager');
2525
var SoundManagerCreator = require('../sound/SoundManagerCreator');
26+
var TextureEvents = require('../textures/events');
2627
var TextureManager = require('../textures/TextureManager');
2728
var TimeStep = require('./TimeStep');
2829
var VisibilityHandler = require('./VisibilityHandler');
@@ -346,7 +347,7 @@ var Game = new Class({
346347
* @method Phaser.Game#boot
347348
* @protected
348349
* @fires Phaser.Core.Events#BOOT
349-
* @fires Phaser.Core.Events#TEXTURES_READY
350+
* @listens Phaser.Textures.Events#READY
350351
* @since 3.0.0
351352
*/
352353
boot: function ()
@@ -378,7 +379,7 @@ var Game = new Class({
378379

379380
// The Texture Manager has to wait on a couple of non-blocking events before it's fully ready.
380381
// So it will emit this internal event when done:
381-
this.events.once(Events.TEXTURES_READY, this.texturesReady, this);
382+
this.events.once(TextureEvents.READY, this.texturesReady, this);
382383
},
383384

384385
/**

src/core/events/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ module.exports = {
2323
READY: require('./READY_EVENT'),
2424
RESUME: require('./RESUME_EVENT'),
2525
STEP: require('./STEP_EVENT'),
26-
TEXTURES_READY: require('./TEXTURES_READY_EVENT'),
2726
VISIBLE: require('./VISIBLE_EVENT')
2827

2928
};

src/textures/TextureManager.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var Class = require('../utils/Class');
1010
var Color = require('../display/color/Color');
1111
var CONST = require('../const');
1212
var EventEmitter = require('eventemitter3');
13+
var Events = require('./events');
14+
var GameEvents = require('../core/events');
1315
var GenerateTexture = require('../create/GenerateTexture');
1416
var GetValue = require('../utils/object/GetValue');
1517
var Parser = require('./parsers');
@@ -109,7 +111,7 @@ var TextureManager = new Class({
109111
*/
110112
this._pending = 0;
111113

112-
game.events.once('boot', this.boot, this);
114+
game.events.once(GameEvents.BOOT, this.boot, this);
113115
},
114116

115117
/**
@@ -129,7 +131,7 @@ var TextureManager = new Class({
129131
this.addBase64('__DEFAULT', this.game.config.defaultImage);
130132
this.addBase64('__MISSING', this.game.config.missingImage);
131133

132-
this.game.events.once('destroy', this.destroy, this);
134+
this.game.events.once(GameEvents.DESTROY, this.destroy, this);
133135
},
134136

135137
/**
@@ -148,7 +150,7 @@ var TextureManager = new Class({
148150
this.off('onload');
149151
this.off('onerror');
150152

151-
this.game.events.emit('texturesready');
153+
this.game.events.emit(GameEvents.TEXTURES_READY);
152154
}
153155
},
154156

@@ -186,6 +188,7 @@ var TextureManager = new Class({
186188
* step when clearing down to avoid this.
187189
*
188190
* @method Phaser.Textures.TextureManager#remove
191+
* @fires Phaser.Textures.Events#REMOVE
189192
* @since 3.7.0
190193
*
191194
* @param {(string|Phaser.Textures.Texture)} key - The key of the Texture to remove, or a reference to it.
@@ -214,7 +217,7 @@ var TextureManager = new Class({
214217

215218
key.destroy();
216219

217-
this.emit('removetexture', key.key);
220+
this.emit(Events.REMOVE, key.key);
218221
}
219222

220223
return this;
@@ -224,6 +227,9 @@ var TextureManager = new Class({
224227
* Adds a new Texture to the Texture Manager created from the given Base64 encoded data.
225228
*
226229
* @method Phaser.Textures.TextureManager#addBase64
230+
* @fires Phaser.Textures.Events#ADD
231+
* @fires Phaser.Textures.Events#ERROR
232+
* @fires Phaser.Textures.Events#LOAD
227233
* @since 3.0.0
228234
*
229235
* @param {string} key - The unique string-based key of the Texture.
@@ -241,7 +247,7 @@ var TextureManager = new Class({
241247

242248
image.onerror = function ()
243249
{
244-
_this.emit('onerror', key);
250+
_this.emit(Events.ERROR, key);
245251
};
246252

247253
image.onload = function ()
@@ -250,9 +256,9 @@ var TextureManager = new Class({
250256

251257
Parser.Image(texture, 0);
252258

253-
_this.emit('addtexture', key, texture);
259+
_this.emit(Events.ADD, key, texture);
254260

255-
_this.emit('onload', key, texture);
261+
_this.emit(Events.LOAD, key, texture);
256262
};
257263

258264
image.src = data;
@@ -316,6 +322,7 @@ var TextureManager = new Class({
316322
* Adds a new Texture to the Texture Manager created from the given Image element.
317323
*
318324
* @method Phaser.Textures.TextureManager#addImage
325+
* @fires Phaser.Textures.Events#ADD
319326
* @since 3.0.0
320327
*
321328
* @param {string} key - The unique string-based key of the Texture.
@@ -339,7 +346,7 @@ var TextureManager = new Class({
339346
texture.setDataSource(dataSource);
340347
}
341348

342-
this.emit('addtexture', key, texture);
349+
this.emit(Events.ADD, key, texture);
343350
}
344351

345352
return texture;
@@ -350,6 +357,7 @@ var TextureManager = new Class({
350357
* This allows you to then use the Render Texture as a normal texture for texture based Game Objects like Sprites.
351358
*
352359
* @method Phaser.Textures.TextureManager#addRenderTexture
360+
* @fires Phaser.Textures.Events#ADD
353361
* @since 3.12.0
354362
*
355363
* @param {string} key - The unique string-based key of the Texture.
@@ -367,7 +375,7 @@ var TextureManager = new Class({
367375

368376
texture.add('__BASE', 0, 0, 0, renderTexture.width, renderTexture.height);
369377

370-
this.emit('addtexture', key, texture);
378+
this.emit(Events.ADD, key, texture);
371379
}
372380

373381
return texture;
@@ -439,6 +447,7 @@ var TextureManager = new Class({
439447
* and adds it to this Texture Manager, unless `skipCache` is true.
440448
*
441449
* @method Phaser.Textures.TextureManager#addCanvas
450+
* @fires Phaser.Textures.Events#ADD
442451
* @since 3.0.0
443452
*
444453
* @param {string} key - The unique string-based key of the Texture.
@@ -463,7 +472,7 @@ var TextureManager = new Class({
463472

464473
this.list[key] = texture;
465474

466-
this.emit('addtexture', key, texture);
475+
this.emit(Events.ADD, key, texture);
467476
}
468477

469478
return texture;
@@ -502,6 +511,7 @@ var TextureManager = new Class({
502511
* This is known as a JSON Array in software such as Texture Packer.
503512
*
504513
* @method Phaser.Textures.TextureManager#addAtlasJSONArray
514+
* @fires Phaser.Textures.Events#ADD
505515
* @since 3.0.0
506516
*
507517
* @param {string} key - The unique string-based key of the Texture.
@@ -542,7 +552,7 @@ var TextureManager = new Class({
542552
texture.setDataSource(dataSource);
543553
}
544554

545-
this.emit('addtexture', key, texture);
555+
this.emit(Events.ADD, key, texture);
546556
}
547557

548558
return texture;
@@ -554,6 +564,7 @@ var TextureManager = new Class({
554564
* This is known as a JSON Hash in software such as Texture Packer.
555565
*
556566
* @method Phaser.Textures.TextureManager#addAtlasJSONHash
567+
* @fires Phaser.Textures.Events#ADD
557568
* @since 3.0.0
558569
*
559570
* @param {string} key - The unique string-based key of the Texture.
@@ -588,7 +599,7 @@ var TextureManager = new Class({
588599
texture.setDataSource(dataSource);
589600
}
590601

591-
this.emit('addtexture', key, texture);
602+
this.emit(Events.ADD, key, texture);
592603
}
593604

594605
return texture;
@@ -599,6 +610,7 @@ var TextureManager = new Class({
599610
* in the XML format.
600611
*
601612
* @method Phaser.Textures.TextureManager#addAtlasXML
613+
* @fires Phaser.Textures.Events#ADD
602614
* @since 3.7.0
603615
*
604616
* @param {string} key - The unique string-based key of the Texture.
@@ -623,7 +635,7 @@ var TextureManager = new Class({
623635
texture.setDataSource(dataSource);
624636
}
625637

626-
this.emit('addtexture', key, texture);
638+
this.emit(Events.ADD, key, texture);
627639
}
628640

629641
return texture;
@@ -634,6 +646,7 @@ var TextureManager = new Class({
634646
* The data must be in the form of a Unity YAML file.
635647
*
636648
* @method Phaser.Textures.TextureManager#addUnityAtlas
649+
* @fires Phaser.Textures.Events#ADD
637650
* @since 3.0.0
638651
*
639652
* @param {string} key - The unique string-based key of the Texture.
@@ -658,7 +671,7 @@ var TextureManager = new Class({
658671
texture.setDataSource(dataSource);
659672
}
660673

661-
this.emit('addtexture', key, texture);
674+
this.emit(Events.ADD, key, texture);
662675
}
663676

664677
return texture;
@@ -682,6 +695,7 @@ var TextureManager = new Class({
682695
* same size and cannot be trimmed or rotated.
683696
*
684697
* @method Phaser.Textures.TextureManager#addSpriteSheet
698+
* @fires Phaser.Textures.Events#ADD
685699
* @since 3.0.0
686700
*
687701
* @param {string} key - The unique string-based key of the Texture.
@@ -703,7 +717,7 @@ var TextureManager = new Class({
703717

704718
Parser.SpriteSheet(texture, 0, 0, 0, width, height, config);
705719

706-
this.emit('addtexture', key, texture);
720+
this.emit(Events.ADD, key, texture);
707721
}
708722

709723
return texture;
@@ -729,6 +743,7 @@ var TextureManager = new Class({
729743
* same size and cannot be trimmed or rotated.
730744
*
731745
* @method Phaser.Textures.TextureManager#addSpriteSheetFromAtlas
746+
* @fires Phaser.Textures.Events#ADD
732747
* @since 3.0.0
733748
*
734749
* @param {string} key - The unique string-based key of the Texture.
@@ -768,7 +783,7 @@ var TextureManager = new Class({
768783
Parser.SpriteSheet(texture, 0, sheet.cutX, sheet.cutY, sheet.cutWidth, sheet.cutHeight, config);
769784
}
770785

771-
this.emit('addtexture', key, texture);
786+
this.emit(Events.ADD, key, texture);
772787

773788
return texture;
774789
}

src/textures/events/ADD_EVENT.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Texture Add Event.
9+
*
10+
* This event is dispatched by the Texture Manager when a texture is added to it.
11+
*
12+
* Listen to this event from within a Scene using: `this.textures.on('addtexture', listener)`.
13+
*
14+
* @event Phaser.Textures.Events#ADD
15+
*
16+
* @param {string} key - The key of the Texture that was added to the Texture Manager.
17+
* @param {Phaser.Textures.Texture} texture - A reference to the Texture that was added to the Texture Manager.
18+
*/
19+
module.exports = 'addtexture';

src/textures/events/ERROR_EVENT.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Texture Load Error Event.
9+
*
10+
* This event is dispatched by the Texture Manager when a texture it requested to load failed.
11+
* This only happens when base64 encoded textures fail. All other texture types are loaded via the Loader Plugin.
12+
*
13+
* Listen to this event from within a Scene using: `this.textures.on('onerror', listener)`.
14+
*
15+
* @event Phaser.Textures.Events#ERROR
16+
*
17+
* @param {string} key - The key of the Texture that failed to load into the Texture Manager.
18+
*/
19+
module.exports = 'onerror';

src/textures/events/LOAD_EVENT.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Texture Load Event.
9+
*
10+
* This event is dispatched by the Texture Manager when a texture has finished loading on it.
11+
* This only happens for base64 encoded textures. All other texture types are loaded via the Loader Plugin.
12+
*
13+
* Listen to this event from within a Scene using: `this.textures.on('onload', listener)`.
14+
*
15+
* This event is dispatched after the [ADD event]{Phaser.Textures.Events#ADD}.
16+
*
17+
* @event Phaser.Textures.Events#LOAD
18+
*
19+
* @param {string} key - The key of the Texture that was loaded by the Texture Manager.
20+
* @param {Phaser.Textures.Texture} texture - A reference to the Texture that was loaded by the Texture Manager.
21+
*/
22+
module.exports = 'onload';
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66

77
/**
8-
* The Game Boot Texture Manager is Ready Event.
8+
* This internal event signifies that the Texture Manager is now ready and the Game can continue booting.
99
*
1010
* When a Phaser Game instance is booting for the first time, the Texture Manager has to wait on a couple of non-blocking
1111
* async events before it's fully ready to carry on. When those complete the Texture Manager emits this event via the Game
1212
* instance, which tells the Game to carry on booting.
1313
*
14-
* @event Phaser.Core.Events#TEXTURES_READY
14+
* @event Phaser.Textures.Events#READY
1515
*/
16-
module.exports = 'texturesready';
16+
module.exports = 'ready';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Texture Remove Event.
9+
*
10+
* This event is dispatched by the Texture Manager when a texture is removed from it.
11+
*
12+
* Listen to this event from within a Scene using: `this.textures.on('removetexture', listener)`.
13+
*
14+
* If you have any Game Objects still using the removed texture, they will start throwing
15+
* errors the next time they try to render. Be sure to clear all use of the texture in this event handler.
16+
*
17+
* @event Phaser.Textures.Events#REMOVE
18+
*
19+
* @param {string} key - The key of the Texture that was removed from the Texture Manager.
20+
*/
21+
module.exports = 'removetexture';

src/textures/events/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* @namespace Phaser.Textures.Events
9+
*/
10+
11+
module.exports = {
12+
13+
ADD: require('./ADD_EVENT'),
14+
ERROR: require('./ERROR_EVENT'),
15+
LOAD: require('./LOAD_EVENT'),
16+
READY: require('./READY_EVENT'),
17+
REMOVE: require('./REMOVE_EVENT')
18+
19+
};

0 commit comments

Comments
 (0)