mirror of
https://github.com/photonstorm/phaser
synced 2025-02-17 06:28:30 +00:00
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.
This commit is contained in:
parent
dec57e2915
commit
b8df529ff1
13 changed files with 465 additions and 298 deletions
|
@ -1,4 +1,4 @@
|
|||
var CHECKSUM = {
|
||||
build: '5a41ebf0-7925-11e7-9739-c5d8872ffd75'
|
||||
build: '1a3f3c80-793c-11e7-ac05-359898771c99'
|
||||
};
|
||||
module.exports = CHECKSUM;
|
|
@ -3,8 +3,9 @@ var CONST = require('./const');
|
|||
var Set = require('../structs/Set');
|
||||
var XHRSettings = require('./XHRSettings');
|
||||
var Event = require('./events/');
|
||||
var EventDispatcher = require('../events/EventDispatcher');
|
||||
// var EventDispatcher = require('../events/EventDispatcher');
|
||||
var Class = require('../utils/Class');
|
||||
var ParseXMLBitmapFont = require('../gameobjects/bitmaptext/ParseXMLBitmapFont');
|
||||
|
||||
// Phaser.Loader.BaseLoader
|
||||
|
||||
|
@ -16,9 +17,11 @@ var BaseLoader = new Class({
|
|||
|
||||
initialize:
|
||||
|
||||
function BaseLoader ()
|
||||
function BaseLoader (scene)
|
||||
{
|
||||
this.events = new EventDispatcher();
|
||||
this.scene = scene;
|
||||
|
||||
this.events = scene.sys.events;
|
||||
|
||||
// Move to a 'setURL' method?
|
||||
this.baseURL = '';
|
||||
|
@ -268,16 +271,163 @@ var BaseLoader = new Class({
|
|||
this.inflight.clear();
|
||||
this.queue.clear();
|
||||
|
||||
if (this.processCallback)
|
||||
{
|
||||
this.processCallback();
|
||||
}
|
||||
this.processCallback();
|
||||
|
||||
this.state = CONST.LOADER_COMPLETE;
|
||||
|
||||
this.events.dispatch(new Event.LOADER_COMPLETE_EVENT(this));
|
||||
},
|
||||
|
||||
// The Loader has finished
|
||||
processCallback: function ()
|
||||
{
|
||||
if (this.storage.size === 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// The global Texture Manager
|
||||
var cache = this.scene.sys.cache;
|
||||
var textures = this.scene.sys.textures;
|
||||
var anims = this.scene.sys.anims;
|
||||
|
||||
// Process multiatlas groups first
|
||||
|
||||
var file;
|
||||
var fileA;
|
||||
var fileB;
|
||||
|
||||
for (var key in this._multilist)
|
||||
{
|
||||
var data = [];
|
||||
var images = [];
|
||||
var keys = this._multilist[key];
|
||||
|
||||
for (var i = 0; i < keys.length; i++)
|
||||
{
|
||||
file = this.storage.get('key', keys[i]);
|
||||
|
||||
if (file)
|
||||
{
|
||||
if (file.type === 'image')
|
||||
{
|
||||
images.push(file.data);
|
||||
}
|
||||
else if (file.type === 'json')
|
||||
{
|
||||
data.push(file.data);
|
||||
}
|
||||
|
||||
this.storage.delete(file);
|
||||
}
|
||||
}
|
||||
|
||||
// Do we have everything needed?
|
||||
if (images.length + data.length === keys.length)
|
||||
{
|
||||
// Yup, add them to the Texture Manager
|
||||
|
||||
// Is the data JSON Hash or JSON Array?
|
||||
if (Array.isArray(data[0].frames))
|
||||
{
|
||||
textures.addAtlasJSONArray(key, images, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
textures.addAtlasJSONHash(key, images, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process all of the files
|
||||
|
||||
// Because AnimationJSON may require images to be loaded first, we process them last
|
||||
var animJSON = [];
|
||||
|
||||
this.storage.each(function (file)
|
||||
{
|
||||
switch (file.type)
|
||||
{
|
||||
case 'animationJSON':
|
||||
animJSON.push(file);
|
||||
break;
|
||||
|
||||
case 'image':
|
||||
case 'svg':
|
||||
case 'html':
|
||||
textures.addImage(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'atlasjson':
|
||||
|
||||
fileA = file.fileA;
|
||||
fileB = file.fileB;
|
||||
|
||||
if (fileA.type === 'image')
|
||||
{
|
||||
textures.addAtlas(fileA.key, fileA.data, fileB.data);
|
||||
}
|
||||
else
|
||||
{
|
||||
textures.addAtlas(fileB.key, fileB.data, fileA.data);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'bitmapfont':
|
||||
|
||||
fileA = file.fileA;
|
||||
fileB = file.fileB;
|
||||
|
||||
if (fileA.type === 'image')
|
||||
{
|
||||
cache.bitmapFont.add(fileB.key, { data: ParseXMLBitmapFont(fileB.data), texture: fileA.key, frame: null });
|
||||
textures.addImage(fileA.key, fileA.data);
|
||||
}
|
||||
else
|
||||
{
|
||||
cache.bitmapFont.add(fileA.key, { data: ParseXMLBitmapFont(fileA.data), texture: fileB.key, frame: null });
|
||||
textures.addImage(fileB.key, fileB.data);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'spritesheet':
|
||||
textures.addSpriteSheet(file.key, file.data, file.config);
|
||||
break;
|
||||
|
||||
case 'json':
|
||||
cache.json.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'xml':
|
||||
cache.xml.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'text':
|
||||
cache.text.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'binary':
|
||||
cache.binary.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'sound':
|
||||
cache.sound.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'glsl':
|
||||
cache.shader.add(file.key, file.data);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
animJSON.forEach(function (file)
|
||||
{
|
||||
anims.fromJSON(file.data);
|
||||
});
|
||||
|
||||
this.storage.clear();
|
||||
},
|
||||
|
||||
reset: function ()
|
||||
{
|
||||
this.list.clear();
|
||||
|
|
|
@ -10,4 +10,23 @@ var AnimationJSONFile = function (key, url, path, xhrSettings)
|
|||
return json;
|
||||
};
|
||||
|
||||
AnimationJSONFile.create = function (loader, key, url, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new AnimationJSONFile(key[i], url, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new AnimationJSONFile(key, url, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = AnimationJSONFile;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var CONST = require('../const');
|
||||
var File = require('../File');
|
||||
var GetFastValue = require('../../utils/object/GetFastValue');
|
||||
|
||||
// Phaser.Loader.FileTypes.BinaryFile
|
||||
|
||||
|
@ -12,14 +13,16 @@ var BinaryFile = new Class({
|
|||
|
||||
function BinaryFile (key, url, path, xhrSettings)
|
||||
{
|
||||
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
|
||||
|
||||
var fileConfig = {
|
||||
type: 'binary',
|
||||
extension: 'bin',
|
||||
extension: GetFastValue(key, 'extension', 'bin'),
|
||||
responseType: 'arraybuffer',
|
||||
key: key,
|
||||
url: url,
|
||||
key: fileKey,
|
||||
url: GetFastValue(key, 'file', url),
|
||||
path: path,
|
||||
xhrSettings: xhrSettings
|
||||
xhrSettings: GetFastValue(key, 'xhr', xhrSettings)
|
||||
};
|
||||
|
||||
File.call(this, fileConfig);
|
||||
|
@ -38,4 +41,23 @@ var BinaryFile = new Class({
|
|||
|
||||
});
|
||||
|
||||
BinaryFile.create = function (loader, key, url, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new BinaryFile(key[i], url, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new BinaryFile(key, url, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = BinaryFile;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var CONST = require('../const');
|
||||
var File = require('../File');
|
||||
var GetFastValue = require('../../utils/object/GetFastValue');
|
||||
|
||||
// Phaser.Loader.FileTypes.GLSLFile
|
||||
|
||||
|
@ -12,14 +13,16 @@ var GLSLFile = new Class({
|
|||
|
||||
function GLSLFile (key, url, path, xhrSettings)
|
||||
{
|
||||
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
|
||||
|
||||
var fileConfig = {
|
||||
type: 'glsl',
|
||||
extension: 'glsl',
|
||||
extension: GetFastValue(key, 'extension', 'glsl'),
|
||||
responseType: 'text',
|
||||
key: key,
|
||||
url: url,
|
||||
key: fileKey,
|
||||
url: GetFastValue(key, 'file', url),
|
||||
path: path,
|
||||
xhrSettings: xhrSettings
|
||||
xhrSettings: GetFastValue(key, 'xhr', xhrSettings)
|
||||
};
|
||||
|
||||
File.call(this, fileConfig);
|
||||
|
@ -38,4 +41,23 @@ var GLSLFile = new Class({
|
|||
|
||||
});
|
||||
|
||||
GLSLFile.create = function (loader, key, url, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new GLSLFile(key[i], url, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new GLSLFile(key, url, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = GLSLFile;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var CONST = require('../const');
|
||||
var File = require('../File');
|
||||
var GetFastValue = require('../../utils/object/GetFastValue');
|
||||
|
||||
// Phaser.Loader.FileTypes.HTMLFile
|
||||
|
||||
|
@ -15,14 +16,16 @@ var HTMLFile = new Class({
|
|||
if (width === undefined) { width = 512; }
|
||||
if (height === undefined) { height = 512; }
|
||||
|
||||
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
|
||||
|
||||
var fileConfig = {
|
||||
type: 'html',
|
||||
extension: 'html',
|
||||
extension: GetFastValue(key, 'extension', 'html'),
|
||||
responseType: 'text',
|
||||
key: key,
|
||||
url: url,
|
||||
key: fileKey,
|
||||
url: GetFastValue(key, 'file', url),
|
||||
path: path,
|
||||
xhrSettings: xhrSettings,
|
||||
xhrSettings: GetFastValue(key, 'xhr', xhrSettings),
|
||||
config: {
|
||||
width: width,
|
||||
height: height
|
||||
|
@ -92,4 +95,23 @@ var HTMLFile = new Class({
|
|||
|
||||
});
|
||||
|
||||
HTMLFile.create = function (loader, key, url, width, height, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new HTMLFile(key[i], url, width, height, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new HTMLFile(key, url, width, height, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = HTMLFile;
|
||||
|
|
|
@ -12,10 +12,10 @@ var ImageFile = new Class({
|
|||
initialize:
|
||||
|
||||
// this.load.image('pic', 'assets/pics/taikodrummaster.jpg');
|
||||
// this.load.image({ key: 'pic', texture: 'assets/pics/taikodrummaster.jpg' });
|
||||
// this.load.image({ key: 'pic', file: 'assets/pics/taikodrummaster.jpg' });
|
||||
// this.load.image({
|
||||
// key: 'bunny',
|
||||
// texture: 'assets/sprites/bunny.png',
|
||||
// file: 'assets/sprites/bunny.png',
|
||||
// xhr: {
|
||||
// user: 'root',
|
||||
// password: 'th3G1bs0n',
|
||||
|
@ -35,9 +35,9 @@ var ImageFile = new Class({
|
|||
extension: GetFastValue(key, 'extension', 'png'),
|
||||
responseType: 'blob',
|
||||
key: fileKey,
|
||||
url: GetFastValue(key, 'texture', url),
|
||||
url: GetFastValue(key, 'file', url),
|
||||
path: path,
|
||||
xhrSettings: GetFastValue(key, 'xhr', {}),
|
||||
xhrSettings: GetFastValue(key, 'xhr', xhrSettings),
|
||||
config: GetFastValue(key, 'config', config)
|
||||
};
|
||||
|
||||
|
@ -77,4 +77,23 @@ var ImageFile = new Class({
|
|||
|
||||
});
|
||||
|
||||
ImageFile.create = function (loader, key, url, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new ImageFile(key[i], url, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new ImageFile(key, url, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = ImageFile;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var CONST = require('../const');
|
||||
var File = require('../File');
|
||||
var GetFastValue = require('../../utils/object/GetFastValue');
|
||||
|
||||
// Phaser.Loader.FileTypes.JSONFile
|
||||
|
||||
|
@ -12,14 +13,16 @@ var JSONFile = new Class({
|
|||
|
||||
function JSONFile (key, url, path, xhrSettings)
|
||||
{
|
||||
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
|
||||
|
||||
var fileConfig = {
|
||||
type: 'json',
|
||||
extension: 'json',
|
||||
extension: GetFastValue(key, 'extension', 'json'),
|
||||
responseType: 'text',
|
||||
key: key,
|
||||
url: url,
|
||||
key: fileKey,
|
||||
url: GetFastValue(key, 'file', url),
|
||||
path: path,
|
||||
xhrSettings: xhrSettings
|
||||
xhrSettings: GetFastValue(key, 'xhr', xhrSettings)
|
||||
};
|
||||
|
||||
File.call(this, fileConfig);
|
||||
|
@ -38,4 +41,23 @@ var JSONFile = new Class({
|
|||
|
||||
});
|
||||
|
||||
JSONFile.create = function (loader, key, url, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new JSONFile(key[i], url, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new JSONFile(key, url, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = JSONFile;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var CONST = require('../const');
|
||||
var File = require('../File');
|
||||
var GetFastValue = require('../../utils/object/GetFastValue');
|
||||
|
||||
// Phaser.Loader.FileTypes.SVGFile
|
||||
|
||||
|
@ -12,14 +13,16 @@ var SVGFile = new Class({
|
|||
|
||||
function SVGFile (key, url, path, xhrSettings)
|
||||
{
|
||||
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
|
||||
|
||||
var fileConfig = {
|
||||
type: 'svg',
|
||||
extension: 'svg',
|
||||
extension: GetFastValue(key, 'extension', 'svg'),
|
||||
responseType: 'text',
|
||||
key: key,
|
||||
url: url,
|
||||
key: fileKey,
|
||||
url: GetFastValue(key, 'file', url),
|
||||
path: path,
|
||||
xhrSettings: xhrSettings
|
||||
xhrSettings: GetFastValue(key, 'xhr', xhrSettings)
|
||||
};
|
||||
|
||||
File.call(this, fileConfig);
|
||||
|
@ -86,4 +89,23 @@ var SVGFile = new Class({
|
|||
|
||||
});
|
||||
|
||||
SVGFile.create = function (loader, key, url, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new SVGFile(key[i], url, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new SVGFile(key, url, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = SVGFile;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var CONST = require('../const');
|
||||
var File = require('../File');
|
||||
var GetFastValue = require('../../utils/object/GetFastValue');
|
||||
|
||||
// Phaser.Loader.FileTypes.ScriptFile
|
||||
|
||||
|
@ -12,14 +13,16 @@ var ScriptFile = new Class({
|
|||
|
||||
function ScriptFile (key, url, path, xhrSettings)
|
||||
{
|
||||
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
|
||||
|
||||
var fileConfig = {
|
||||
type: 'script',
|
||||
extension: 'js',
|
||||
extension: GetFastValue(key, 'extension', 'js'),
|
||||
responseType: 'text',
|
||||
key: key,
|
||||
url: url,
|
||||
key: fileKey,
|
||||
url: GetFastValue(key, 'file', url),
|
||||
path: path,
|
||||
xhrSettings: xhrSettings
|
||||
xhrSettings: GetFastValue(key, 'xhr', xhrSettings)
|
||||
};
|
||||
|
||||
File.call(this, fileConfig);
|
||||
|
@ -44,4 +47,23 @@ var ScriptFile = new Class({
|
|||
|
||||
});
|
||||
|
||||
ScriptFile.create = function (loader, key, url, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new ScriptFile(key[i], url, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new ScriptFile(key, url, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = ScriptFile;
|
||||
|
|
|
@ -12,4 +12,23 @@ var SpriteSheet = function (key, url, config, path, xhrSettings)
|
|||
return image;
|
||||
};
|
||||
|
||||
SpriteSheet.create = function (loader, key, url, config, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new SpriteSheet(key[i], url, null, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new SpriteSheet(key, url, config, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = SpriteSheet;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var CONST = require('../const');
|
||||
var File = require('../File');
|
||||
var GetFastValue = require('../../utils/object/GetFastValue');
|
||||
var ParseXML = require('../../dom/ParseXML');
|
||||
|
||||
// Phaser.Loader.FileTypes.XMLFile
|
||||
|
@ -13,14 +14,16 @@ var XMLFile = new Class({
|
|||
|
||||
function XMLFile (key, url, path, xhrSettings)
|
||||
{
|
||||
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
|
||||
|
||||
var fileConfig = {
|
||||
type: 'xml',
|
||||
extension: 'xml',
|
||||
extension: GetFastValue(key, 'extension', 'xml'),
|
||||
responseType: 'text',
|
||||
key: key,
|
||||
url: url,
|
||||
key: fileKey,
|
||||
url: GetFastValue(key, 'file', url),
|
||||
path: path,
|
||||
xhrSettings: xhrSettings
|
||||
xhrSettings: GetFastValue(key, 'xhr', xhrSettings)
|
||||
};
|
||||
|
||||
File.call(this, fileConfig);
|
||||
|
@ -44,4 +47,23 @@ var XMLFile = new Class({
|
|||
|
||||
});
|
||||
|
||||
XMLFile.create = function (loader, key, url, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
|
||||
loader.addFile(new XMLFile(key[i], url, loader.path, xhrSettings));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loader.addFile(new XMLFile(key, url, loader.path, xhrSettings));
|
||||
}
|
||||
|
||||
// For method chaining
|
||||
return loader;
|
||||
};
|
||||
|
||||
module.exports = XMLFile;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
var BaseLoader = require('../loader/BaseLoader');
|
||||
var Class = require('../utils/Class');
|
||||
// var CONST = require('../loader/const');
|
||||
var NumberArray = require('../utils/array/NumberArray');
|
||||
|
||||
var AnimationJSONFile = require('../loader/filetypes/AnimationJSONFile');
|
||||
|
@ -17,8 +16,6 @@ var SVGFile = require('../loader/filetypes/SVGFile');
|
|||
var TextFile = require('../loader/filetypes/TextFile');
|
||||
var XMLFile = require('../loader/filetypes/XMLFile');
|
||||
|
||||
var ParseXMLBitmapFont = require('../gameobjects/bitmaptext/ParseXMLBitmapFont');
|
||||
|
||||
var Loader = new Class({
|
||||
|
||||
Extends: BaseLoader,
|
||||
|
@ -27,151 +24,72 @@ var Loader = new Class({
|
|||
|
||||
function Loader (scene)
|
||||
{
|
||||
BaseLoader.call(this);
|
||||
|
||||
/**
|
||||
* @property {Phaser.Scene} scene - The Scene that owns this Factory
|
||||
* @protected
|
||||
*/
|
||||
this.scene = scene;
|
||||
BaseLoader.call(this, scene);
|
||||
|
||||
this._multilist = {};
|
||||
},
|
||||
|
||||
loadArray: function (files)
|
||||
{
|
||||
if (Array.isArray(files))
|
||||
{
|
||||
for (var i = 0; i < files.length; i++)
|
||||
{
|
||||
this.file(files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return (this.list.size > 0);
|
||||
},
|
||||
|
||||
file: function (file)
|
||||
{
|
||||
var entry;
|
||||
|
||||
switch (file.type)
|
||||
{
|
||||
case 'image':
|
||||
case 'json':
|
||||
case 'xml':
|
||||
case 'binary':
|
||||
case 'text':
|
||||
case 'glsl':
|
||||
case 'svg':
|
||||
entry = this[file.type](file.key, file.url, file.xhrSettings);
|
||||
break;
|
||||
|
||||
case 'spritesheet':
|
||||
entry = this.spritesheet(file.key, file.url, file.config, file.xhrSettings);
|
||||
break;
|
||||
|
||||
case 'atlas':
|
||||
entry = this.atlas(file.key, file.textureURL, file.atlasURL, file.textureXhrSettings, file.atlasXhrSettings);
|
||||
break;
|
||||
|
||||
case 'bitmapFont':
|
||||
entry = this.bitmapFont(file.key, file.textureURL, file.xmlURL, file.textureXhrSettings, file.xmlXhrSettings);
|
||||
break;
|
||||
|
||||
case 'multiatlas':
|
||||
entry = this.multiatlas(file.key, file.textureURLs, file.atlasURLs, file.textureXhrSettings, file.atlasXhrSettings);
|
||||
break;
|
||||
}
|
||||
|
||||
return entry;
|
||||
},
|
||||
|
||||
// key can be either a string, an object or an array of objects
|
||||
|
||||
image: function (key, url, xhrSettings)
|
||||
{
|
||||
if (Array.isArray(key))
|
||||
{
|
||||
var files = [];
|
||||
|
||||
for (var i = 0; i < key.length; i++)
|
||||
{
|
||||
files.push(this.image(key[i]));
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
else
|
||||
{
|
||||
var file = new ImageFile(key, url, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
}
|
||||
return ImageFile.create(this, key, url, xhrSettings);
|
||||
},
|
||||
|
||||
animation: function (key, url, xhrSettings)
|
||||
{
|
||||
var file = new AnimationJSONFile(key, url, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
return AnimationJSONFile.create(this, key, url, xhrSettings);
|
||||
},
|
||||
|
||||
json: function (key, url, xhrSettings)
|
||||
{
|
||||
var file = new JSONFile(key, url, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
return JSONFile.create(this, key, url, xhrSettings);
|
||||
},
|
||||
|
||||
script: function (key, url, xhrSettings)
|
||||
{
|
||||
var file = new ScriptFile(key, url, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
return ScriptFile.create(this, key, url, xhrSettings);
|
||||
},
|
||||
|
||||
xml: function (key, url, xhrSettings)
|
||||
{
|
||||
var file = new XMLFile(key, url, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
return XMLFile.create(this, key, url, xhrSettings);
|
||||
},
|
||||
|
||||
binary: function (key, url, xhrSettings)
|
||||
{
|
||||
var file = new BinaryFile(key, url, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
return BinaryFile.create(this, key, url, xhrSettings);
|
||||
},
|
||||
|
||||
text: function (key, url, xhrSettings)
|
||||
{
|
||||
var file = new TextFile(key, url, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
return TextFile.create(this, key, url, xhrSettings);
|
||||
},
|
||||
|
||||
glsl: function (key, url, xhrSettings)
|
||||
{
|
||||
var file = new GLSLFile(key, url, this.path, xhrSettings);
|
||||
return GLSLFile.create(this, key, url, xhrSettings);
|
||||
},
|
||||
|
||||
return this.addFile(file);
|
||||
html: function (key, url, width, height, xhrSettings)
|
||||
{
|
||||
return HTMLFile.create(this, key, url, width, height, xhrSettings);
|
||||
},
|
||||
|
||||
svg: function (key, url, xhrSettings)
|
||||
{
|
||||
return SVGFile.create(this, key, url, xhrSettings);
|
||||
},
|
||||
|
||||
// config can include: frameWidth, frameHeight, startFrame, endFrame, margin, spacing
|
||||
spritesheet: function (key, url, config, xhrSettings)
|
||||
{
|
||||
var file = new SpriteSheet(key, url, config, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
return SpriteSheet.create(this, key, url, config, xhrSettings);
|
||||
},
|
||||
|
||||
html: function (key, url, width, height, xhrSettings)
|
||||
{
|
||||
var file = new HTMLFile(key, url, width, height, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
},
|
||||
// ---------------------------------------------------
|
||||
// Multi-File Loaders
|
||||
// ---------------------------------------------------
|
||||
|
||||
atlas: function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
|
||||
{
|
||||
|
@ -195,13 +113,6 @@ var Loader = new Class({
|
|||
return this;
|
||||
},
|
||||
|
||||
svg: function (key, url, xhrSettings)
|
||||
{
|
||||
var file = new SVGFile(key, url, this.path, xhrSettings);
|
||||
|
||||
return this.addFile(file);
|
||||
},
|
||||
|
||||
multiatlas: function (key, textureURLs, atlasURLs, textureXhrSettings, atlasXhrSettings)
|
||||
{
|
||||
if (typeof textureURLs === 'number')
|
||||
|
@ -252,159 +163,54 @@ var Loader = new Class({
|
|||
|
||||
this._multilist[key].push(multiKey);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
loadArray: function (files)
|
||||
{
|
||||
if (Array.isArray(files))
|
||||
{
|
||||
for (var i = 0; i < files.length; i++)
|
||||
{
|
||||
this.file(files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return (this.list.size > 0);
|
||||
},
|
||||
|
||||
// The Loader has finished
|
||||
processCallback: function ()
|
||||
file: function (file)
|
||||
{
|
||||
if (this.storage.size === 0)
|
||||
var entry;
|
||||
|
||||
switch (file.type)
|
||||
{
|
||||
return;
|
||||
case 'spritesheet':
|
||||
entry = this.spritesheet(file.key, file.url, file.config, file.xhrSettings);
|
||||
break;
|
||||
|
||||
case 'atlas':
|
||||
entry = this.atlas(file.key, file.textureURL, file.atlasURL, file.textureXhrSettings, file.atlasXhrSettings);
|
||||
break;
|
||||
|
||||
case 'bitmapFont':
|
||||
entry = this.bitmapFont(file.key, file.textureURL, file.xmlURL, file.textureXhrSettings, file.xmlXhrSettings);
|
||||
break;
|
||||
|
||||
case 'multiatlas':
|
||||
entry = this.multiatlas(file.key, file.textureURLs, file.atlasURLs, file.textureXhrSettings, file.atlasXhrSettings);
|
||||
break;
|
||||
|
||||
// image, json, xml, binary, text, glsl, svg
|
||||
default:
|
||||
entry = this[file.type](file.key, file.url, file.xhrSettings);
|
||||
break;
|
||||
}
|
||||
|
||||
// The global Texture Manager
|
||||
var cache = this.scene.sys.cache;
|
||||
var textures = this.scene.sys.textures;
|
||||
var anims = this.scene.sys.anims;
|
||||
|
||||
// Process multiatlas groups first
|
||||
|
||||
var file;
|
||||
var fileA;
|
||||
var fileB;
|
||||
|
||||
for (var key in this._multilist)
|
||||
{
|
||||
var data = [];
|
||||
var images = [];
|
||||
var keys = this._multilist[key];
|
||||
|
||||
for (var i = 0; i < keys.length; i++)
|
||||
{
|
||||
file = this.storage.get('key', keys[i]);
|
||||
|
||||
if (file)
|
||||
{
|
||||
if (file.type === 'image')
|
||||
{
|
||||
images.push(file.data);
|
||||
}
|
||||
else if (file.type === 'json')
|
||||
{
|
||||
data.push(file.data);
|
||||
}
|
||||
|
||||
this.storage.delete(file);
|
||||
}
|
||||
}
|
||||
|
||||
// Do we have everything needed?
|
||||
if (images.length + data.length === keys.length)
|
||||
{
|
||||
// Yup, add them to the Texture Manager
|
||||
|
||||
// Is the data JSON Hash or JSON Array?
|
||||
if (Array.isArray(data[0].frames))
|
||||
{
|
||||
textures.addAtlasJSONArray(key, images, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
textures.addAtlasJSONHash(key, images, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process all of the files
|
||||
|
||||
// Because AnimationJSON may require images to be loaded first, we process them last
|
||||
var animJSON = [];
|
||||
|
||||
this.storage.each(function (file)
|
||||
{
|
||||
switch (file.type)
|
||||
{
|
||||
case 'animationJSON':
|
||||
animJSON.push(file);
|
||||
break;
|
||||
|
||||
case 'image':
|
||||
case 'svg':
|
||||
case 'html':
|
||||
textures.addImage(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'atlasjson':
|
||||
|
||||
fileA = file.fileA;
|
||||
fileB = file.fileB;
|
||||
|
||||
if (fileA.type === 'image')
|
||||
{
|
||||
textures.addAtlas(fileA.key, fileA.data, fileB.data);
|
||||
}
|
||||
else
|
||||
{
|
||||
textures.addAtlas(fileB.key, fileB.data, fileA.data);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'bitmapfont':
|
||||
|
||||
fileA = file.fileA;
|
||||
fileB = file.fileB;
|
||||
|
||||
if (fileA.type === 'image')
|
||||
{
|
||||
cache.bitmapFont.add(fileB.key, { data: ParseXMLBitmapFont(fileB.data), texture: fileA.key, frame: null });
|
||||
textures.addImage(fileA.key, fileA.data);
|
||||
}
|
||||
else
|
||||
{
|
||||
cache.bitmapFont.add(fileA.key, { data: ParseXMLBitmapFont(fileA.data), texture: fileB.key, frame: null });
|
||||
textures.addImage(fileB.key, fileB.data);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'spritesheet':
|
||||
textures.addSpriteSheet(file.key, file.data, file.config);
|
||||
break;
|
||||
|
||||
case 'json':
|
||||
cache.json.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'xml':
|
||||
cache.xml.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'text':
|
||||
cache.text.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'binary':
|
||||
cache.binary.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'sound':
|
||||
cache.sound.add(file.key, file.data);
|
||||
break;
|
||||
|
||||
case 'glsl':
|
||||
cache.shader.add(file.key, file.data);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
animJSON.forEach(function (file)
|
||||
{
|
||||
anims.fromJSON(file.data);
|
||||
});
|
||||
|
||||
this.storage.clear();
|
||||
}
|
||||
return entry;
|
||||
},
|
||||
*/
|
||||
|
||||
});
|
||||
|
||||
module.exports = Loader;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue