Updated Loader so you can pass in your own XHRSettings object with any file, and have that override the XHR defaults for the specific file.

This commit is contained in:
Richard Davey 2016-12-12 22:27:53 +00:00
parent 65828dfab3
commit 822b3a9d0c
10 changed files with 80 additions and 94 deletions

View file

@ -50,24 +50,9 @@ BaseLoader.prototype = {
return -1;
}
// Multipart file?
if (file.linkFile)
{
var fileA = file;
var fileB = file.linkFile;
fileA.path = this.path;
fileB.path = this.path;
this.list.add(fileA);
this.list.add(fileB);
}
else
{
file.path = this.path;
this.list.add(file);
}
return this;
},

View file

@ -2,8 +2,9 @@ var GetURL = require('./GetURL');
var CONST = require('./const');
var XHRLoader = require('./XHRLoader');
var XHRSettings = require('./XHRSettings');
var MergeXHRSettings = require('./MergeXHRSettings');
var File = function (type, key, url, responseType)
var File = function (type, key, url, responseType, xhrSettings)
{
// file type (image, json, etc) for sorting within the Loader
this.type = type;
@ -19,6 +20,11 @@ var File = function (type, key, url, responseType)
this.xhrSettings = XHRSettings(responseType);
if (xhrSettings)
{
this.xhrSettings = MergeXHRSettings(this.xhrSettings, xhrSettings);
}
this.xhrLoader = null;
this.state = CONST.FILE_PENDING;

View file

@ -1,10 +1,10 @@
var ImageFile = require('./ImageFile.js');
var JSONFile = require('./JSONFile.js');
var AtlasJSONFile = function (key, textureURL, atlasURL, path)
var AtlasJSONFile = function (key, textureURL, atlasURL, path, textureXhrSettings, atlasXhrSettings)
{
var image = new ImageFile(key, textureURL, path);
var data = new JSONFile(key, atlasURL, path);
var image = new ImageFile(key, textureURL, path, textureXhrSettings);
var data = new JSONFile(key, atlasURL, path, atlasXhrSettings);
// Link them together
image.linkFile = data;
@ -14,7 +14,7 @@ var AtlasJSONFile = function (key, textureURL, atlasURL, path)
image.linkType = 'atlasjson';
data.linkType = 'atlasjson';
return image;
return { texture: image, data: data };
};
module.exports = AtlasJSONFile;

View file

@ -2,7 +2,7 @@
var CONST = require('../const');
var File = require('../File');
var BinaryFile = function (key, url, path)
var BinaryFile = function (key, url, path, xhrSettings)
{
if (path === undefined) { path = ''; }
@ -20,7 +20,7 @@ var BinaryFile = function (key, url, path)
url = path.concat(url);
}
File.call(this, 'binary', key, url, 'arraybuffer');
File.call(this, 'binary', key, url, 'arraybuffer', xhrSettings);
};
BinaryFile.prototype = Object.create(File.prototype);

View file

@ -2,7 +2,7 @@
var CONST = require('../const');
var File = require('../File');
var GLSLFile = function (key, url, path)
var GLSLFile = function (key, url, path, xhrSettings)
{
if (path === undefined) { path = ''; }
@ -20,7 +20,7 @@ var GLSLFile = function (key, url, path)
url = path.concat(url);
}
File.call(this, 'glsl', key, url, 'text');
File.call(this, 'glsl', key, url, 'text', xhrSettings);
};
GLSLFile.prototype = Object.create(File.prototype);

View file

@ -2,7 +2,7 @@
var CONST = require('../const');
var File = require('../File');
var ImageFile = function (key, url, path)
var ImageFile = function (key, url, path, xhrSettings)
{
if (path === undefined) { path = ''; }
@ -20,7 +20,7 @@ var ImageFile = function (key, url, path)
url = path.concat(url);
}
File.call(this, 'image', key, url, 'blob');
File.call(this, 'image', key, url, 'blob', xhrSettings);
};
ImageFile.prototype = Object.create(File.prototype);

View file

@ -2,7 +2,7 @@
var CONST = require('../const');
var File = require('../File');
var JSONFile = function (key, url, path)
var JSONFile = function (key, url, path, xhrSettings)
{
if (path === undefined) { path = ''; }
@ -20,7 +20,7 @@ var JSONFile = function (key, url, path)
url = path.concat(url);
}
File.call(this, 'json', key, url, 'text');
File.call(this, 'json', key, url, 'text', xhrSettings);
};
JSONFile.prototype = Object.create(File.prototype);

View file

@ -2,7 +2,7 @@
var CONST = require('../const');
var File = require('../File');
var TextFile = function (key, url, path)
var TextFile = function (key, url, path, xhrSettings)
{
if (path === undefined) { path = ''; }
@ -20,7 +20,7 @@ var TextFile = function (key, url, path)
url = path.concat(url);
}
File.call(this, 'text', key, url, 'text');
File.call(this, 'text', key, url, 'text', xhrSettings);
};
TextFile.prototype = Object.create(File.prototype);

View file

@ -3,7 +3,7 @@ var CONST = require('../const');
var File = require('../File');
var ParseXML = require('../../dom/ParseXML');
var XMLFile = function (key, url, path)
var XMLFile = function (key, url, path, xhrSettings)
{
if (path === undefined) { path = ''; }
@ -21,7 +21,7 @@ var XMLFile = function (key, url, path)
url = path.concat(url);
}
File.call(this, 'xml', key, url, 'text');
File.call(this, 'xml', key, url, 'text', xhrSettings);
};
XMLFile.prototype = Object.create(File.prototype);

View file

@ -26,70 +26,60 @@ var Loader = function (state)
Loader.prototype = Object.create(BaseLoader.prototype);
Loader.prototype.constructor = Loader;
Loader.prototype.image = function (key, url)
Loader.prototype.image = function (key, url, xhrSettings)
{
var file = new ImageFile(key, url, this.path);
var file = new ImageFile(key, url, this.path, xhrSettings);
this.addFile(file);
return this.addFile(file);
};
Loader.prototype.json = function (key, url, xhrSettings)
{
var file = new JSONFile(key, url, this.path, xhrSettings);
return this.addFile(file);
};
Loader.prototype.xml = function (key, url, xhrSettings)
{
var file = new XMLFile(key, url, this.path, xhrSettings);
return this.addFile(file);
};
Loader.prototype.binary = function (key, url, xhrSettings)
{
var file = new BinaryFile(key, url, this.path, xhrSettings);
return this.addFile(file);
};
Loader.prototype.text = function (key, url, xhrSettings)
{
var file = new TextFile(key, url, this.path, xhrSettings);
return this.addFile(file);
};
Loader.prototype.glsl = function (key, url, xhrSettings)
{
var file = new GLSLFile(key, url, this.path, xhrSettings);
return this.addFile(file);
};
Loader.prototype.atlas = function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
{
// Returns an object with two properties: 'texture' and 'data'
var files = new AtlasJSONFile(key, textureURL, atlasURL, this.path, textureXhrSettings, atlasXhrSettings);
this.addFile(files.texture);
this.addFile(files.data);
return this;
};
Loader.prototype.json = function (key, url)
{
var file = new JSONFile(key, url, this.path);
this.addFile(file);
return this;
};
Loader.prototype.xml = function (key, url)
{
var file = new XMLFile(key, url, this.path);
this.addFile(file);
return this;
};
Loader.prototype.binary = function (key, url)
{
var file = new BinaryFile(key, url, this.path);
this.addFile(file);
return this;
};
Loader.prototype.text = function (key, url)
{
var file = new TextFile(key, url, this.path);
this.addFile(file);
return this;
};
Loader.prototype.glsl = function (key, url)
{
var file = new GLSLFile(key, url, this.path);
this.addFile(file);
return this;
};
Loader.prototype.atlas = function (key, textureURL, atlasURL)
{
var file = new AtlasJSONFile(key, textureURL, atlasURL, this.path);
this.addFile(file);
return this;
};
Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs)
Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs, textureXhrSettings, atlasXhrSettings)
{
if (typeof textureURLs === 'number')
{
@ -111,6 +101,7 @@ Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs)
}
}
var file;
var i = 0;
var multiKey;
@ -120,7 +111,9 @@ Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs)
{
multiKey = '_MA_IMG_' + key + '_' + i.toString();
this.addFile(new ImageFile(multiKey, textureURLs[i], this.path));
file = new ImageFile(multiKey, textureURLs[i], this.path, textureXhrSettings);
this.addFile(file);
this._multilist[key].push(multiKey);
}
@ -129,7 +122,9 @@ Loader.prototype.multiatlas = function (key, textureURLs, atlasURLs)
{
multiKey = '_MA_JSON_' + key + '_' + i.toString();
this.addFile(new JSONFile(multiKey, atlasURLs[i], this.path));
file = new JSONFile(multiKey, atlasURLs[i], this.path, atlasXhrSettings);
this.addFile(file);
this._multilist[key].push(multiKey);
}