phaser/v3/src/loader/File.js

122 lines
2.8 KiB
JavaScript
Raw Normal View History

var GetURL = require('./GetURL');
var CONST = require('./const');
var XHRLoader = require('./XHRLoader');
var XHRSettings = require('./XHRSettings');
var File = function (type, key, url, responseType)
{
// file type (image, json, etc) for sorting within the Loader
this.type = type;
// unique cache key (unique within its file type)
this.key = key;
// The URL of the file, not including baseURL
this.url = url;
// Set when the Loader calls 'load' on this file
this.src = '';
this.xhrSettings = XHRSettings(responseType);
this.xhrLoader = null;
this.state = CONST.FILE_PENDING;
// Set by onProgress (only if loading via XHR)
this.bytesTotal = 0;
this.bytesLoaded = -1;
this.percentComplete = -1;
// For CORs based loading.
// If this is undefined then the File will check BaseLoader.crossOrigin and use that (if set)
this.crossOrigin = undefined;
// The actual processed file data
this.data = undefined;
// Multipart file? (i.e. an atlas and its json together)
this.multipart = undefined;
this.linkFile = undefined;
this.callback = null;
};
File.prototype.constructor = File;
File.prototype = {
resetXHR: function ()
{
this.xhrLoader.onload = undefined;
this.xhrLoader.onerror = undefined;
this.xhrLoader.onprogress = undefined;
},
// Called when the Image loads
// ProgressEvent
onLoad: function (event)
{
2016-12-07 00:27:56 +00:00
console.log('image loaded');
2016-12-07 00:04:32 +00:00
// console.log(event);
// this.onStateChange(LOADING);
2016-12-07 00:27:56 +00:00
// this.process();
2016-12-07 00:04:32 +00:00
this.resetXHR();
this.callback(this, true);
},
onError: function (event)
{
2016-12-07 00:04:32 +00:00
// console.log('image error');
// console.log(event);
this.resetXHR();
this.callback(this, false);
},
onProgress: function (event)
{
this.bytesLoaded = event.loaded;
this.bytesTotal = event.total;
this.percentComplete = Math.min((this.bytesLoaded / this.bytesTotal), 1);
console.log(this.percentComplete + '% (' + this.bytesLoaded + ' bytes)');
},
2016-12-07 00:27:56 +00:00
onProcess: function (callback)
{
2016-12-07 00:27:56 +00:00
this.state = CONST.FILE_PROCESSING;
// If overridden by another class, it must call the callback when finished, then onComplete
callback(this);
this.onComplete();
},
onComplete: function ()
{
2016-12-07 00:04:32 +00:00
console.log('File completed, ready to add to the Loader store');
this.state = CONST.FILE_COMPLETE;
},
// Called by the Loader, starts the actual file downloading
load: function (callback, baseURL, globalXHR)
{
if (baseURL === undefined) { baseURL = ''; }
this.callback = callback;
this.src = GetURL(this, baseURL);
this.xhrLoader = XHRLoader(this, globalXHR);
}
};
module.exports = File;