Skip to content

Commit b8df529

Browse files
committed
Updated Loader and all single-file file types to support multiple load methods. Will now work with argument based loader, a configuration object or an array of objects, per all file types. Moved lots of code out of the Loader plugin and into BaseLoader and the FileType files.
1 parent dec57e2 commit b8df529

13 files changed

Lines changed: 458 additions & 291 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '5a41ebf0-7925-11e7-9739-c5d8872ffd75'
2+
build: '1a3f3c80-793c-11e7-ac05-359898771c99'
33
};
44
module.exports = CHECKSUM;

v3/src/loader/BaseLoader.js

Lines changed: 157 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ var CONST = require('./const');
33
var Set = require('../structs/Set');
44
var XHRSettings = require('./XHRSettings');
55
var Event = require('./events/');
6-
var EventDispatcher = require('../events/EventDispatcher');
6+
// var EventDispatcher = require('../events/EventDispatcher');
77
var Class = require('../utils/Class');
8+
var ParseXMLBitmapFont = require('../gameobjects/bitmaptext/ParseXMLBitmapFont');
89

910
// Phaser.Loader.BaseLoader
1011

@@ -16,9 +17,11 @@ var BaseLoader = new Class({
1617

1718
initialize:
1819

19-
function BaseLoader ()
20+
function BaseLoader (scene)
2021
{
21-
this.events = new EventDispatcher();
22+
this.scene = scene;
23+
24+
this.events = scene.sys.events;
2225

2326
// Move to a 'setURL' method?
2427
this.baseURL = '';
@@ -268,16 +271,163 @@ var BaseLoader = new Class({
268271
this.inflight.clear();
269272
this.queue.clear();
270273

271-
if (this.processCallback)
272-
{
273-
this.processCallback();
274-
}
274+
this.processCallback();
275275

276276
this.state = CONST.LOADER_COMPLETE;
277277

278278
this.events.dispatch(new Event.LOADER_COMPLETE_EVENT(this));
279279
},
280280

281+
// The Loader has finished
282+
processCallback: function ()
283+
{
284+
if (this.storage.size === 0)
285+
{
286+
return;
287+
}
288+
289+
// The global Texture Manager
290+
var cache = this.scene.sys.cache;
291+
var textures = this.scene.sys.textures;
292+
var anims = this.scene.sys.anims;
293+
294+
// Process multiatlas groups first
295+
296+
var file;
297+
var fileA;
298+
var fileB;
299+
300+
for (var key in this._multilist)
301+
{
302+
var data = [];
303+
var images = [];
304+
var keys = this._multilist[key];
305+
306+
for (var i = 0; i < keys.length; i++)
307+
{
308+
file = this.storage.get('key', keys[i]);
309+
310+
if (file)
311+
{
312+
if (file.type === 'image')
313+
{
314+
images.push(file.data);
315+
}
316+
else if (file.type === 'json')
317+
{
318+
data.push(file.data);
319+
}
320+
321+
this.storage.delete(file);
322+
}
323+
}
324+
325+
// Do we have everything needed?
326+
if (images.length + data.length === keys.length)
327+
{
328+
// Yup, add them to the Texture Manager
329+
330+
// Is the data JSON Hash or JSON Array?
331+
if (Array.isArray(data[0].frames))
332+
{
333+
textures.addAtlasJSONArray(key, images, data);
334+
}
335+
else
336+
{
337+
textures.addAtlasJSONHash(key, images, data);
338+
}
339+
}
340+
}
341+
342+
// Process all of the files
343+
344+
// Because AnimationJSON may require images to be loaded first, we process them last
345+
var animJSON = [];
346+
347+
this.storage.each(function (file)
348+
{
349+
switch (file.type)
350+
{
351+
case 'animationJSON':
352+
animJSON.push(file);
353+
break;
354+
355+
case 'image':
356+
case 'svg':
357+
case 'html':
358+
textures.addImage(file.key, file.data);
359+
break;
360+
361+
case 'atlasjson':
362+
363+
fileA = file.fileA;
364+
fileB = file.fileB;
365+
366+
if (fileA.type === 'image')
367+
{
368+
textures.addAtlas(fileA.key, fileA.data, fileB.data);
369+
}
370+
else
371+
{
372+
textures.addAtlas(fileB.key, fileB.data, fileA.data);
373+
}
374+
break;
375+
376+
case 'bitmapfont':
377+
378+
fileA = file.fileA;
379+
fileB = file.fileB;
380+
381+
if (fileA.type === 'image')
382+
{
383+
cache.bitmapFont.add(fileB.key, { data: ParseXMLBitmapFont(fileB.data), texture: fileA.key, frame: null });
384+
textures.addImage(fileA.key, fileA.data);
385+
}
386+
else
387+
{
388+
cache.bitmapFont.add(fileA.key, { data: ParseXMLBitmapFont(fileA.data), texture: fileB.key, frame: null });
389+
textures.addImage(fileB.key, fileB.data);
390+
}
391+
break;
392+
393+
case 'spritesheet':
394+
textures.addSpriteSheet(file.key, file.data, file.config);
395+
break;
396+
397+
case 'json':
398+
cache.json.add(file.key, file.data);
399+
break;
400+
401+
case 'xml':
402+
cache.xml.add(file.key, file.data);
403+
break;
404+
405+
case 'text':
406+
cache.text.add(file.key, file.data);
407+
break;
408+
409+
case 'binary':
410+
cache.binary.add(file.key, file.data);
411+
break;
412+
413+
case 'sound':
414+
cache.sound.add(file.key, file.data);
415+
break;
416+
417+
case 'glsl':
418+
cache.shader.add(file.key, file.data);
419+
break;
420+
}
421+
});
422+
423+
animJSON.forEach(function (file)
424+
{
425+
anims.fromJSON(file.data);
426+
});
427+
428+
this.storage.clear();
429+
},
430+
281431
reset: function ()
282432
{
283433
this.list.clear();

v3/src/loader/filetypes/AnimationJSONFile.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,23 @@ var AnimationJSONFile = function (key, url, path, xhrSettings)
1010
return json;
1111
};
1212

13+
AnimationJSONFile.create = function (loader, key, url, xhrSettings)
14+
{
15+
if (Array.isArray(key))
16+
{
17+
for (var i = 0; i < key.length; i++)
18+
{
19+
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
20+
loader.addFile(new AnimationJSONFile(key[i], url, loader.path, xhrSettings));
21+
}
22+
}
23+
else
24+
{
25+
loader.addFile(new AnimationJSONFile(key, url, loader.path, xhrSettings));
26+
}
27+
28+
// For method chaining
29+
return loader;
30+
};
31+
1332
module.exports = AnimationJSONFile;

v3/src/loader/filetypes/BinaryFile.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Class = require('../../utils/Class');
22
var CONST = require('../const');
33
var File = require('../File');
4+
var GetFastValue = require('../../utils/object/GetFastValue');
45

56
// Phaser.Loader.FileTypes.BinaryFile
67

@@ -12,14 +13,16 @@ var BinaryFile = new Class({
1213

1314
function BinaryFile (key, url, path, xhrSettings)
1415
{
16+
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
17+
1518
var fileConfig = {
1619
type: 'binary',
17-
extension: 'bin',
20+
extension: GetFastValue(key, 'extension', 'bin'),
1821
responseType: 'arraybuffer',
19-
key: key,
20-
url: url,
22+
key: fileKey,
23+
url: GetFastValue(key, 'file', url),
2124
path: path,
22-
xhrSettings: xhrSettings
25+
xhrSettings: GetFastValue(key, 'xhr', xhrSettings)
2326
};
2427

2528
File.call(this, fileConfig);
@@ -38,4 +41,23 @@ var BinaryFile = new Class({
3841

3942
});
4043

44+
BinaryFile.create = function (loader, key, url, xhrSettings)
45+
{
46+
if (Array.isArray(key))
47+
{
48+
for (var i = 0; i < key.length; i++)
49+
{
50+
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
51+
loader.addFile(new BinaryFile(key[i], url, loader.path, xhrSettings));
52+
}
53+
}
54+
else
55+
{
56+
loader.addFile(new BinaryFile(key, url, loader.path, xhrSettings));
57+
}
58+
59+
// For method chaining
60+
return loader;
61+
};
62+
4163
module.exports = BinaryFile;

v3/src/loader/filetypes/GLSLFile.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Class = require('../../utils/Class');
22
var CONST = require('../const');
33
var File = require('../File');
4+
var GetFastValue = require('../../utils/object/GetFastValue');
45

56
// Phaser.Loader.FileTypes.GLSLFile
67

@@ -12,14 +13,16 @@ var GLSLFile = new Class({
1213

1314
function GLSLFile (key, url, path, xhrSettings)
1415
{
16+
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
17+
1518
var fileConfig = {
1619
type: 'glsl',
17-
extension: 'glsl',
20+
extension: GetFastValue(key, 'extension', 'glsl'),
1821
responseType: 'text',
19-
key: key,
20-
url: url,
22+
key: fileKey,
23+
url: GetFastValue(key, 'file', url),
2124
path: path,
22-
xhrSettings: xhrSettings
25+
xhrSettings: GetFastValue(key, 'xhr', xhrSettings)
2326
};
2427

2528
File.call(this, fileConfig);
@@ -38,4 +41,23 @@ var GLSLFile = new Class({
3841

3942
});
4043

44+
GLSLFile.create = function (loader, key, url, xhrSettings)
45+
{
46+
if (Array.isArray(key))
47+
{
48+
for (var i = 0; i < key.length; i++)
49+
{
50+
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
51+
loader.addFile(new GLSLFile(key[i], url, loader.path, xhrSettings));
52+
}
53+
}
54+
else
55+
{
56+
loader.addFile(new GLSLFile(key, url, loader.path, xhrSettings));
57+
}
58+
59+
// For method chaining
60+
return loader;
61+
};
62+
4163
module.exports = GLSLFile;

v3/src/loader/filetypes/HTMLFile.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Class = require('../../utils/Class');
22
var CONST = require('../const');
33
var File = require('../File');
4+
var GetFastValue = require('../../utils/object/GetFastValue');
45

56
// Phaser.Loader.FileTypes.HTMLFile
67

@@ -15,14 +16,16 @@ var HTMLFile = new Class({
1516
if (width === undefined) { width = 512; }
1617
if (height === undefined) { height = 512; }
1718

19+
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
20+
1821
var fileConfig = {
1922
type: 'html',
20-
extension: 'html',
23+
extension: GetFastValue(key, 'extension', 'html'),
2124
responseType: 'text',
22-
key: key,
23-
url: url,
25+
key: fileKey,
26+
url: GetFastValue(key, 'file', url),
2427
path: path,
25-
xhrSettings: xhrSettings,
28+
xhrSettings: GetFastValue(key, 'xhr', xhrSettings),
2629
config: {
2730
width: width,
2831
height: height
@@ -92,4 +95,23 @@ var HTMLFile = new Class({
9295

9396
});
9497

98+
HTMLFile.create = function (loader, key, url, width, height, xhrSettings)
99+
{
100+
if (Array.isArray(key))
101+
{
102+
for (var i = 0; i < key.length; i++)
103+
{
104+
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
105+
loader.addFile(new HTMLFile(key[i], url, width, height, loader.path, xhrSettings));
106+
}
107+
}
108+
else
109+
{
110+
loader.addFile(new HTMLFile(key, url, width, height, loader.path, xhrSettings));
111+
}
112+
113+
// For method chaining
114+
return loader;
115+
};
116+
95117
module.exports = HTMLFile;

0 commit comments

Comments
 (0)