11var CONST = require ( '../../loader/const' ) ;
22var BaseLoader = require ( '../../loader/BaseLoader' ) ;
3- var ImageLoader = require ( '../../loader/filetypes/ImageFile' ) ;
3+ var ImageFile = require ( '../../loader/filetypes/ImageFile' ) ;
4+ var JSONFile = require ( '../../loader/filetypes/JSONFile' ) ;
45var AtlasJSONFile = require ( '../../loader/filetypes/AtlasJSONFile' ) ;
6+ var NumberArray = require ( '../../utils/array/NumberArray' ) ;
57
68var Loader = function ( state )
79{
@@ -12,14 +14,25 @@ var Loader = function (state)
1214 * @protected
1315 */
1416 this . state = state ;
17+
18+ this . _multilist = { } ;
1519} ;
1620
1721Loader . prototype = Object . create ( BaseLoader . prototype ) ;
1822Loader . prototype . constructor = Loader ;
1923
2024Loader . prototype . image = function ( key , url )
2125{
22- var file = new ImageLoader ( key , url , this . path ) ;
26+ var file = new ImageFile ( key , url , this . path ) ;
27+
28+ this . addFile ( file ) ;
29+
30+ return this ;
31+ } ;
32+
33+ Loader . prototype . json = function ( key , url )
34+ {
35+ var file = new JSONFile ( key , url , this . path ) ;
2336
2437 this . addFile ( file ) ;
2538
@@ -35,24 +48,14 @@ Loader.prototype.atlas = function (key, textureURL, atlasURL)
3548 return this ;
3649} ;
3750
38- /**
39- * @method Phaser.Loader#multiatlas
40- * @param {string } key - Unique asset key of the texture atlas file.
41- * @param {array|integer } textureURLs - An array of PNG files, or an integer.
42- * @param {array } [atlasURLs] - An array of JSON files.
43- * @param {number } [format] - The format of the data. Can be Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY (the default), Phaser.Loader.TEXTURE_ATLAS_JSON_HASH or Phaser.Loader.TEXTURE_ATLAS_XML_STARLING.
44- * @return {Phaser.Loader } This Loader instance.
45- */
46- Loader . prototype . multiatlas = function ( key , textureURLs , atlasURLs , format )
51+ Loader . prototype . multiatlas = function ( key , textureURLs , atlasURLs )
4752{
48- if ( format === undefined ) { format = CONST . TEXTURE_ATLAS_JSON_ARRAY ; }
49-
5053 if ( typeof textureURLs === 'number' )
5154 {
5255 var total = textureURLs ;
5356
54- textureURLs = Phaser . ArrayUtils . numberArray ( 0 , total , key + '-' , '.png' ) ;
55- atlasURLs = Phaser . ArrayUtils . numberArray ( 0 , total , key + '-' , '.json' ) ;
57+ textureURLs = NumberArray ( 0 , total , key + '-' , '.png' ) ;
58+ atlasURLs = NumberArray ( 0 , total , key + '-' , '.json' ) ;
5659 }
5760 else
5861 {
@@ -69,37 +72,31 @@ Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs, format)
6972
7073 var i = 0 ;
7174 var multiKey ;
72- var imgs = [ ] ;
73- var data = [ ] ;
75+
76+ this . _multilist [ key ] = [ ] ;
7477
7578 for ( i = 0 ; i < textureURLs . length ; i ++ )
7679 {
77- // TODO - Add support for compressed textures
78- multiKey = '_MA_' + key + '_' + i . toString ( ) ;
80+ multiKey = '_MA_IMG_' + key + '_' + i . toString ( ) ;
7981
80- imgs . push ( multiKey ) ;
82+ this . addFile ( new ImageFile ( multiKey , textureURLs [ i ] , this . path ) ) ;
8183
82- this . addToFileList ( 'image' , multiKey , textureURLs [ i ] , { multiatlas : true } ) ;
84+ this . _multilist [ key ] . push ( multiKey ) ;
8385 }
8486
8587 for ( i = 0 ; i < atlasURLs . length ; i ++ )
8688 {
87- multiKey = '_MA_ ' + key + '_' + i . toString ( ) ;
89+ multiKey = '_MA_JSON_ ' + key + '_' + i . toString ( ) ;
8890
89- data . push ( multiKey ) ;
91+ this . addFile ( new JSONFile ( multiKey , atlasURLs [ i ] , this . path ) ) ;
9092
91- // Check if this can support XML as well?
92- this . addToFileList ( 'json' , multiKey , atlasURLs [ i ] , { multiatlas : true } ) ;
93+ this . _multilist [ key ] . push ( multiKey ) ;
9394 }
94-
95- this . _multilist . push ( { key : key , images : imgs , data : data , format : format } ) ;
96-
97-
9895} ;
9996
97+ // The Loader has finished
10098Loader . prototype . processCallback = function ( )
10199{
102- // All of the files have loaded. Now to put them into the Cache.
103100 if ( this . storage . size === 0 )
104101 {
105102 return ;
@@ -108,6 +105,52 @@ Loader.prototype.processCallback = function ()
108105 // The global Texture Manager
109106 var textures = this . state . sys . textures ;
110107
108+ // Process multiatlas groups first
109+
110+ var file ;
111+
112+ for ( var key in this . _multilist )
113+ {
114+ var data = [ ] ;
115+ var images = [ ] ;
116+ var keys = this . _multilist [ key ] ;
117+
118+ for ( var i = 0 ; i < keys . length ; i ++ )
119+ {
120+ file = this . storage . get ( 'key' , keys [ i ] ) ;
121+
122+ if ( file )
123+ {
124+ if ( file . type === 'image' )
125+ {
126+ images . push ( file . data ) ;
127+ }
128+ else if ( file . type === 'json' )
129+ {
130+ data . push ( file . data ) ;
131+ }
132+
133+ this . storage . delete ( file ) ;
134+ }
135+ }
136+
137+ // Do we have everything needed?
138+ if ( images . length + data . length === keys . length )
139+ {
140+ // Yup, add them to the Texture Manager
141+
142+ // Is the data JSON Hash or JSON Array?
143+ if ( Array . isArray ( data [ 0 ] . frames ) )
144+ {
145+ textures . addAtlasJSONArray ( key , images , data ) ;
146+ }
147+ else
148+ {
149+ textures . addAtlasJSONHash ( key , images , data ) ;
150+ }
151+ }
152+ }
153+
111154 this . storage . each ( function ( file )
112155 {
113156 if ( file . type === 'image' )
@@ -128,6 +171,10 @@ Loader.prototype.processCallback = function ()
128171 textures . addAtlas ( fileB . key , fileB . data , fileA . data ) ;
129172 }
130173 }
174+ else if ( file . type === 'json' )
175+ {
176+ // console.dir(file.data);
177+ }
131178 } ) ;
132179
133180 this . storage . clear ( ) ;
0 commit comments