-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
TextureManager.js
1570 lines (1378 loc) · 51.1 KB
/
TextureManager.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CanvasPool = require('../display/canvas/CanvasPool');
var CanvasTexture = require('./CanvasTexture');
var Class = require('../utils/Class');
var Color = require('../display/color/Color');
var CONST = require('../const');
var DynamicTexture = require('./DynamicTexture');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
var Frame = require('./Frame');
var GameEvents = require('../core/events');
var GenerateTexture = require('../create/GenerateTexture');
var GetValue = require('../utils/object/GetValue');
var ImageGameObject = require('../gameobjects/image/Image');
var IsPlainObject = require('../utils/object/IsPlainObject');
var Parser = require('./parsers');
var Rectangle = require('../geom/rectangle/Rectangle');
var Texture = require('./Texture');
/**
* @callback EachTextureCallback
*
* @param {Phaser.Textures.Texture} texture - Each texture in Texture Manager.
* @param {...*} [args] - Additional arguments that will be passed to the callback, after the child.
*/
/**
* @classdesc
* When Phaser boots it will create an instance of this Texture Manager class.
*
* It is a global manager that handles all textures in your game. You can access it from within
* a Scene via the `this.textures` property.
*
* Its role is as a manager for all textures that your game uses. It can create, update and remove
* textures globally, as well as parse texture data from external files, such as sprite sheets
* and texture atlases.
*
* Sprites and other texture-based Game Objects get their texture data directly from this class.
*
* @class TextureManager
* @extends Phaser.Events.EventEmitter
* @memberof Phaser.Textures
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - The Phaser.Game instance this Texture Manager belongs to.
*/
var TextureManager = new Class({
Extends: EventEmitter,
initialize:
function TextureManager (game)
{
EventEmitter.call(this);
/**
* The Game that the Texture Manager belongs to.
*
* A game will only ever have one instance of a Texture Manager.
*
* @name Phaser.Textures.TextureManager#game
* @type {Phaser.Game}
* @since 3.0.0
*/
this.game = game;
/**
* The internal name of this manager.
*
* @name Phaser.Textures.TextureManager#name
* @type {string}
* @readonly
* @since 3.0.0
*/
this.name = 'TextureManager';
/**
* This object contains all Textures that belong to this Texture Manager.
*
* Textures are identified by string-based keys, which are used as the property
* within this object. Therefore, you can access any texture directly from this
* object without any iteration.
*
* You should not typically modify this object directly, but instead use the
* methods provided by the Texture Manager to add and remove entries from it.
*
* @name Phaser.Textures.TextureManager#list
* @type {object}
* @default {}
* @since 3.0.0
*/
this.list = {};
/**
* The temporary canvas element used to save the pixel data of an arbitrary texture
* during the `TextureManager.getPixel` and `getPixelAlpha` methods.
*
* @name Phaser.Textures.TextureManager#_tempCanvas
* @type {HTMLCanvasElement}
* @private
* @since 3.0.0
*/
this._tempCanvas = CanvasPool.create2D(this);
/**
* The 2d context of the `_tempCanvas` element.
*
* @name Phaser.Textures.TextureManager#_tempContext
* @type {CanvasRenderingContext2D}
* @private
* @since 3.0.0
*/
this._tempContext = this._tempCanvas.getContext('2d', { willReadFrequently: true });
/**
* An internal tracking value used for emitting the 'READY' event after all of
* the managers in the game have booted.
*
* @name Phaser.Textures.TextureManager#_pending
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._pending = 0;
/**
* An Image Game Object that belongs to this Texture Manager.
*
* Used as a drawing stamp within Dynamic Textures.
*
* This is not part of the display list and doesn't render.
*
* @name Phaser.Textures.TextureManager#stamp
* @type {Phaser.GameObjects.Image}
* @readonly
* @since 3.60.0
*/
this.stamp;
/**
* The crop Rectangle as used by the Stamp when it needs to crop itself.
*
* @name Phaser.Textures.TextureManager#stampCrop
* @type {Phaser.Geom.Rectangle}
* @since 3.60.0
*/
this.stampCrop = new Rectangle();
/**
* If this flag is `true` then the Texture Manager will never emit any
* warnings to the console log that report missing textures.
*
* @name Phaser.Textures.TextureManager#silentWarnings
* @type {boolean}
* @default false
* @since 3.60.0
*/
this.silentWarnings = false;
game.events.once(GameEvents.BOOT, this.boot, this);
},
/**
* The Boot Handler called by Phaser.Game when it first starts up.
*
* @method Phaser.Textures.TextureManager#boot
* @private
* @since 3.0.0
*/
boot: function ()
{
this._pending = 3;
this.on(Events.LOAD, this.updatePending, this);
this.on(Events.ERROR, this.updatePending, this);
var config = this.game.config;
this.addBase64('__DEFAULT', config.defaultImage);
this.addBase64('__MISSING', config.missingImage);
this.addBase64('__WHITE', config.whiteImage);
this.game.events.once(GameEvents.DESTROY, this.destroy, this);
this.game.events.once(GameEvents.SYSTEM_READY, function (scene)
{
this.stamp = new ImageGameObject(scene).setOrigin(0);
}, this);
},
/**
* After 'onload' or 'onerror' invoked twice, emit 'ready' event.
*
* @method Phaser.Textures.TextureManager#updatePending
* @private
* @since 3.0.0
*/
updatePending: function ()
{
this._pending--;
if (this._pending === 0)
{
this.off(Events.LOAD);
this.off(Events.ERROR);
this.emit(Events.READY);
}
},
/**
* Checks the given texture key and throws a console.warn if the key is already in use, then returns false.
*
* If you wish to avoid the console.warn then use `TextureManager.exists` instead.
*
* @method Phaser.Textures.TextureManager#checkKey
* @since 3.7.0
*
* @param {string} key - The texture key to check.
*
* @return {boolean} `true` if it's safe to use the texture key, otherwise `false`.
*/
checkKey: function (key)
{
if (this.exists(key))
{
if (!this.silentWarnings)
{
// eslint-disable-next-line no-console
console.error('Texture key already in use: ' + key);
}
return false;
}
return true;
},
/**
* Removes a Texture from the Texture Manager and destroys it. This will immediately
* clear all references to it from the Texture Manager, and if it has one, destroy its
* WebGLTexture. This will emit a `removetexture` event.
*
* Note: If you have any Game Objects still using this texture they will start throwing
* errors the next time they try to render. Make sure that removing the texture is the final
* step when clearing down to avoid this.
*
* @method Phaser.Textures.TextureManager#remove
* @fires Phaser.Textures.Events#REMOVE
* @since 3.7.0
*
* @param {(string|Phaser.Textures.Texture)} key - The key of the Texture to remove, or a reference to it.
*
* @return {Phaser.Textures.TextureManager} The Texture Manager.
*/
remove: function (key)
{
if (typeof key === 'string')
{
if (this.exists(key))
{
key = this.get(key);
}
else
{
if (!this.silentWarnings)
{
console.warn('No texture found matching key: ' + key);
}
return this;
}
}
// By this point key should be a Texture, if not, the following fails anyway
var textureKey = key.key;
if (this.list.hasOwnProperty(textureKey))
{
key.destroy();
this.emit(Events.REMOVE, textureKey);
this.emit(Events.REMOVE_KEY + textureKey);
}
return this;
},
/**
* Removes a key from the Texture Manager but does not destroy the Texture that was using the key.
*
* @method Phaser.Textures.TextureManager#removeKey
* @since 3.17.0
*
* @param {string} key - The key to remove from the texture list.
*
* @return {Phaser.Textures.TextureManager} The Texture Manager.
*/
removeKey: function (key)
{
if (this.list.hasOwnProperty(key))
{
delete this.list[key];
}
return this;
},
/**
* Adds a new Texture to the Texture Manager created from the given Base64 encoded data.
*
* It works by creating an `Image` DOM object, then setting the `src` attribute to
* the given base64 encoded data. As a result, the process is asynchronous by its nature,
* so be sure to listen for the events this method dispatches before using the texture.
*
* @method Phaser.Textures.TextureManager#addBase64
* @fires Phaser.Textures.Events#ADD
* @fires Phaser.Textures.Events#ERROR
* @fires Phaser.Textures.Events#LOAD
* @since 3.0.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {*} data - The Base64 encoded data.
*
* @return {this} This Texture Manager instance.
*/
addBase64: function (key, data)
{
if (this.checkKey(key))
{
var _this = this;
var image = new Image();
image.onerror = function ()
{
_this.emit(Events.ERROR, key);
};
image.onload = function ()
{
var texture = _this.create(key, image);
Parser.Image(texture, 0);
_this.emit(Events.ADD, key, texture);
_this.emit(Events.ADD_KEY + key, texture);
_this.emit(Events.LOAD, key, texture);
};
image.src = data;
}
return this;
},
/**
* Gets an existing texture frame and converts it into a base64 encoded image and returns the base64 data.
*
* You can also provide the image type and encoder options.
*
* This will only work with bitmap based texture frames, such as those created from Texture Atlases.
* It will not work with GL Texture objects, such as Shaders, or Render Textures. For those please
* see the WebGL Snapshot function instead.
*
* @method Phaser.Textures.TextureManager#getBase64
* @since 3.12.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {(string|number)} [frame] - The string-based name, or integer based index, of the Frame to get from the Texture.
* @param {string} [type='image/png'] - A DOMString indicating the image format. The default format type is image/png.
* @param {number} [encoderOptions=0.92] - A Number between 0 and 1 indicating the image quality to use for image formats that use lossy compression such as image/jpeg and image/webp. If this argument is anything else, the default value for image quality is used. The default value is 0.92. Other arguments are ignored.
*
* @return {string} The base64 encoded data, or an empty string if the texture frame could not be found.
*/
getBase64: function (key, frame, type, encoderOptions)
{
if (type === undefined) { type = 'image/png'; }
if (encoderOptions === undefined) { encoderOptions = 0.92; }
var data = '';
var textureFrame = this.getFrame(key, frame);
if (textureFrame && (textureFrame.source.isRenderTexture || textureFrame.source.isGLTexture))
{
if (!this.silentWarnings)
{
console.warn('Cannot getBase64 from WebGL Texture');
}
}
else if (textureFrame)
{
var cd = textureFrame.canvasData;
var canvas = CanvasPool.create2D(this, cd.width, cd.height);
var ctx = canvas.getContext('2d', { willReadFrequently: true });
if (cd.width > 0 && cd.height > 0)
{
ctx.drawImage(
textureFrame.source.image,
cd.x,
cd.y,
cd.width,
cd.height,
0,
0,
cd.width,
cd.height
);
}
data = canvas.toDataURL(type, encoderOptions);
CanvasPool.remove(canvas);
}
return data;
},
/**
* Adds a new Texture to the Texture Manager created from the given Image element.
*
* @method Phaser.Textures.TextureManager#addImage
* @fires Phaser.Textures.Events#ADD
* @since 3.0.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {HTMLImageElement} source - The source Image element.
* @param {HTMLImageElement|HTMLCanvasElement} [dataSource] - An optional data Image element.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addImage: function (key, source, dataSource)
{
var texture = null;
if (this.checkKey(key))
{
texture = this.create(key, source);
Parser.Image(texture, 0);
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Takes a WebGL Texture and creates a Phaser Texture from it, which is added to the Texture Manager using the given key.
*
* This allows you to then use the Texture as a normal texture for texture based Game Objects like Sprites.
*
* If the `width` and `height` arguments are omitted, but the WebGL Texture was created by Phaser's WebGL Renderer
* and has `glTexture.width` and `glTexture.height` properties, these values will be used instead.
*
* This is a WebGL only feature.
*
* @method Phaser.Textures.TextureManager#addGLTexture
* @fires Phaser.Textures.Events#ADD
* @since 3.19.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {WebGLTexture} glTexture - The source Render Texture.
* @param {number} [width] - The new width of the Texture. Read from `glTexture.width` if omitted.
* @param {number} [height] - The new height of the Texture. Read from `glTexture.height` if omitted.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addGLTexture: function (key, glTexture, width, height)
{
var texture = null;
if (this.checkKey(key))
{
if (width === undefined) { width = glTexture.width; }
if (height === undefined) { height = glTexture.height; }
texture = this.create(key, glTexture, width, height);
texture.add('__BASE', 0, 0, 0, width, height);
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Adds a Compressed Texture to this Texture Manager.
*
* The texture should typically have been loaded via the `CompressedTextureFile` loader,
* in order to prepare the correct data object this method requires.
*
* You can optionally also pass atlas data to this method, in which case a texture atlas
* will be generated from the given compressed texture, combined with the atlas data.
*
* @method Phaser.Textures.TextureManager#addCompressedTexture
* @fires Phaser.Textures.Events#ADD
* @since 3.60.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {Phaser.Types.Textures.CompressedTextureData} textureData - The Compressed Texture data object.
* @param {object} [atlasData] - Optional Texture Atlas data.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addCompressedTexture: function (key, textureData, atlasData)
{
var texture = null;
if (this.checkKey(key))
{
texture = this.create(key, textureData);
texture.add('__BASE', 0, 0, 0, textureData.width, textureData.height);
if (atlasData)
{
if (Array.isArray(atlasData))
{
for (var i = 0; i < atlasData.length; i++)
{
Parser.JSONHash(texture, i, atlasData[i]);
}
}
else
{
Parser.JSONHash(texture, 0, atlasData);
}
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Adds a Render Texture to the Texture Manager using the given key.
* This allows you to then use the Render Texture as a normal texture for texture based Game Objects like Sprites.
*
* @method Phaser.Textures.TextureManager#addRenderTexture
* @fires Phaser.Textures.Events#ADD
* @since 3.12.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {Phaser.GameObjects.RenderTexture} renderTexture - The source Render Texture.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addRenderTexture: function (key, renderTexture)
{
var texture = null;
if (this.checkKey(key))
{
texture = this.create(key, renderTexture);
texture.add('__BASE', 0, 0, 0, renderTexture.width, renderTexture.height);
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Creates a new Texture using the given config values.
*
* Generated textures consist of a Canvas element to which the texture data is drawn.
*
* Generates a texture based on the given Create configuration object.
*
* The texture is drawn using a fixed-size indexed palette of 16 colors, where the hex value in the
* data cells map to a single color. For example, if the texture config looked like this:
*
* ```javascript
* var star = [
* '.....828.....',
* '....72227....',
* '....82228....',
* '...7222227...',
* '2222222222222',
* '8222222222228',
* '.72222222227.',
* '..787777787..',
* '..877777778..',
* '.78778887787.',
* '.27887.78872.',
* '.787.....787.'
* ];
*
* this.textures.generate('star', { data: star, pixelWidth: 4 });
* ```
*
* Then it would generate a texture that is 52 x 48 pixels in size, because each cell of the data array
* represents 1 pixel multiplied by the `pixelWidth` value. The cell values, such as `8`, maps to color
* number 8 in the palette. If a cell contains a period character `.` then it is transparent.
*
* The default palette is Arne16, but you can specify your own using the `palette` property.
*
* @method Phaser.Textures.TextureManager#generate
* @since 3.0.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {Phaser.Types.Create.GenerateTextureConfig} config - The configuration object needed to generate the texture.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
generate: function (key, config)
{
if (this.checkKey(key))
{
var canvas = CanvasPool.create(this, 1, 1);
config.canvas = canvas;
GenerateTexture(config);
return this.addCanvas(key, canvas);
}
else
{
return null;
}
},
/**
* Creates a new Texture using a blank Canvas element of the size given.
*
* Canvas elements are automatically pooled and calling this method will
* extract a free canvas from the CanvasPool, or create one if none are available.
*
* @method Phaser.Textures.TextureManager#createCanvas
* @since 3.0.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {number} [width=256] - The width of the Canvas element.
* @param {number} [height=256] - The height of the Canvas element.
*
* @return {?Phaser.Textures.CanvasTexture} The Canvas Texture that was created, or `null` if the key is already in use.
*/
createCanvas: function (key, width, height)
{
if (width === undefined) { width = 256; }
if (height === undefined) { height = 256; }
if (this.checkKey(key))
{
var canvas = CanvasPool.create(this, width, height, CONST.CANVAS, true);
return this.addCanvas(key, canvas);
}
return null;
},
/**
* Creates a new Canvas Texture object from an existing Canvas element
* and adds it to this Texture Manager, unless `skipCache` is true.
*
* @method Phaser.Textures.TextureManager#addCanvas
* @fires Phaser.Textures.Events#ADD
* @since 3.0.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {HTMLCanvasElement} source - The Canvas element to form the base of the new Texture.
* @param {boolean} [skipCache=false] - Skip adding this Texture into the Cache?
*
* @return {?Phaser.Textures.CanvasTexture} The Canvas Texture that was created, or `null` if the key is already in use.
*/
addCanvas: function (key, source, skipCache)
{
if (skipCache === undefined) { skipCache = false; }
var texture = null;
if (skipCache)
{
texture = new CanvasTexture(this, key, source, source.width, source.height);
}
else if (this.checkKey(key))
{
texture = new CanvasTexture(this, key, source, source.width, source.height);
this.list[key] = texture;
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Creates a Dynamic Texture instance and adds itself to this Texture Manager.
*
* A Dynamic Texture is a special texture that allows you to draw textures, frames and most kind of
* Game Objects directly to it.
*
* You can take many complex objects and draw them to this one texture, which can then be used as the
* base texture for other Game Objects, such as Sprites. Should you then update this texture, all
* Game Objects using it will instantly be updated as well, reflecting the changes immediately.
*
* It's a powerful way to generate dynamic textures at run-time that are WebGL friendly and don't invoke
* expensive GPU uploads on each change.
*
* See the methods available on the `DynamicTexture` class for more details.
*
* Optionally, you can also pass a Dynamic Texture instance to this method to have
* it added to the Texture Manager.
*
* @method Phaser.Textures.TextureManager#addDynamicTexture
* @fires Phaser.Textures.Events#ADD
* @since 3.60.0
*
* @param {(string|Phaser.Textures.DynamicTexture)} key - The string-based key of this Texture. Must be unique within the Texture Manager. Or, a DynamicTexture instance.
* @param {number} [width=256] - The width of this Dynamic Texture in pixels. Defaults to 256 x 256. Ignored if an instance is passed as the key.
* @param {number} [height=256] - The height of this Dynamic Texture in pixels. Defaults to 256 x 256. Ignored if an instance is passed as the key.
*
* @return {?Phaser.Textures.DynamicTexture} The Dynamic Texture that was created, or `null` if the key is already in use.
*/
addDynamicTexture: function (key, width, height)
{
var texture = null;
if (typeof(key) === 'string' && !this.exists(key))
{
texture = new DynamicTexture(this, key, width, height);
}
else
{
texture = key;
key = texture.key;
}
if (this.checkKey(key))
{
this.list[key] = texture;
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
else
{
texture = null;
}
return texture;
},
/**
* Adds a Texture Atlas to this Texture Manager.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a JSON data file,
* such as those exported by applications like Texture Packer.
*
* It can accept either JSON Array or JSON Hash formats, as exported by Texture Packer and similar software.
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
*
* @method Phaser.Textures.TextureManager#addAtlas
* @since 3.0.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {(HTMLImageElement|HTMLImageElement[]|Phaser.Textures.Texture)} source - The source Image element/s, or a Phaser Texture.
* @param {(object|object[])} data - The Texture Atlas data/s.
* @param {HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]} [dataSource] - An optional data Image element.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addAtlas: function (key, source, data, dataSource)
{
// New Texture Packer format?
if (Array.isArray(data.textures) || Array.isArray(data.frames))
{
return this.addAtlasJSONArray(key, source, data, dataSource);
}
else
{
return this.addAtlasJSONHash(key, source, data, dataSource);
}
},
/**
* Adds a Texture Atlas to this Texture Manager.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a JSON data file,
* such as those exported by applications like Texture Packer.
*
* The frame data of the atlas must be stored in an Array within the JSON.
*
* This is known as a JSON Array in software such as Texture Packer.
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
*
* @method Phaser.Textures.TextureManager#addAtlasJSONArray
* @fires Phaser.Textures.Events#ADD
* @since 3.0.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {(HTMLImageElement|HTMLImageElement[]|Phaser.Textures.Texture)} source - The source Image element/s, or a Phaser Texture.
* @param {(object|object[])} data - The Texture Atlas data/s.
* @param {HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]} [dataSource] - An optional data Image element.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addAtlasJSONArray: function (key, source, data, dataSource)
{
var texture = null;
if (source instanceof Texture)
{
key = source.key;
texture = source;
}
else if (this.checkKey(key))
{
texture = this.create(key, source);
}
if (texture)
{
// Multi-Atlas?
if (Array.isArray(data))
{
var singleAtlasFile = (data.length === 1); // multi-pack with one atlas file for all images
// !! Assumes the textures are in the same order in the source array as in the json data !!
for (var i = 0; i < texture.source.length; i++)
{
var atlasData = singleAtlasFile ? data[0] : data[i];
Parser.JSONArray(texture, i, atlasData);
}
}
else
{
Parser.JSONArray(texture, 0, data);
}
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Adds a Texture Atlas to this Texture Manager.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a JSON data file,
* such as those exported by applications like Texture Packer.
*
* The frame data of the atlas must be stored in an Object within the JSON.
*
* This is known as a JSON Hash in software such as Texture Packer.
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
*
* @method Phaser.Textures.TextureManager#addAtlasJSONHash
* @fires Phaser.Textures.Events#ADD
* @since 3.0.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {(HTMLImageElement|HTMLImageElement[]|Phaser.Textures.Texture)} source - The source Image element/s, or a Phaser Texture.
* @param {(object|object[])} data - The Texture Atlas data/s.
* @param {HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]} [dataSource] - An optional data Image element.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addAtlasJSONHash: function (key, source, data, dataSource)
{
var texture = null;
if (source instanceof Texture)
{
key = source.key;
texture = source;
}
else if (this.checkKey(key))
{
texture = this.create(key, source);
}
if (texture)
{
if (Array.isArray(data))
{
for (var i = 0; i < data.length; i++)
{
Parser.JSONHash(texture, i, data[i]);
}
}
else
{
Parser.JSONHash(texture, 0, data);
}
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Adds a Texture Atlas to this Texture Manager.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a data file,
* such as those exported by applications like Texture Packer.
*
* The frame data of the atlas must be stored in an XML file.
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
*
* @method Phaser.Textures.TextureManager#addAtlasXML
* @fires Phaser.Textures.Events#ADD
* @since 3.7.0
*
* @param {string} key - The unique string-based key of the Texture.
* @param {(HTMLImageElement|Phaser.Textures.Texture)} source - The source Image element, or a Phaser Texture.
* @param {object} data - The Texture Atlas XML data.
* @param {HTMLImageElement|HTMLCanvasElement|HTMLImageElement[]|HTMLCanvasElement[]} [dataSource] - An optional data Image element.
*
* @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use.
*/
addAtlasXML: function (key, source, data, dataSource)
{
var texture = null;
if (source instanceof Texture)
{
key = source.key;
texture = source;
}
else if (this.checkKey(key))
{
texture = this.create(key, source);
}
if (texture)
{
Parser.AtlasXML(texture, 0, data);
if (dataSource)
{
texture.setDataSource(dataSource);
}
this.emit(Events.ADD, key, texture);
this.emit(Events.ADD_KEY + key, texture);
}
return texture;
},
/**
* Adds a Unity Texture Atlas to this Texture Manager.
*
* In Phaser terminology, a Texture Atlas is a combination of an atlas image and a data file,
* such as those exported by applications like Texture Packer or Unity.
*
* The frame data of the atlas must be stored in a Unity YAML file.
*
* As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.
*
* @method Phaser.Textures.TextureManager#addUnityAtlas
* @fires Phaser.Textures.Events#ADD