r3-legacy/bak/game-lib-d3-image-factory.js

156 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-01-05 19:34:28 +01:00
/**
* The image factory takes care that we only make requests for Image URLs which we have not already started downloading
* @param graphics
* @param apiImageFactory
2017-02-01 16:09:34 +01:00
* @param progressCallback
* @returns {GameLib.D3.ImageFactory}
2017-01-05 19:34:28 +01:00
* @constructor
*/
GameLib.D3.ImageFactory = function (
graphics,
2017-02-01 16:09:34 +01:00
apiImageFactory,
progressCallback
2017-01-05 19:34:28 +01:00
) {
graphics.isNotThreeThrow();
2017-02-01 16:09:34 +01:00
this.graphics = graphics;
2017-01-05 19:34:28 +01:00
2017-02-01 16:09:34 +01:00
if (GameLib.Utils.UndefinedOrNull(apiImageFactory)) {
apiImageFactory = {};
}
2017-01-05 19:34:28 +01:00
if (apiImageFactory instanceof GameLib.D3.ImageFactory) {
return apiImageFactory;
}
2017-02-01 16:09:34 +01:00
GameLib.D3.API.ImageFactory.call(
this,
apiImageFactory.id,
apiImageFactory.name,
apiImageFactory.baseUrl,
apiImageFactory.parentEntity
);
2017-01-05 19:34:28 +01:00
2017-06-16 15:49:53 +02:00
if (GameLib.Utils.UndefinedOrNull(progressCallback)) {
progressCallback = null;
}
this.progressCallback = progressCallback;
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_IMAGE_FACTORY
);
2017-02-01 16:09:34 +01:00
this.promiseList = {};
};
2017-01-05 19:34:28 +01:00
2017-02-01 16:09:34 +01:00
GameLib.D3.ImageFactory.prototype = Object.create(GameLib.D3.API.ImageFactory.prototype);
GameLib.D3.ImageFactory.prototype.constructor = GameLib.D3.ImageFactory;
2017-01-05 19:34:28 +01:00
2017-02-01 16:09:34 +01:00
GameLib.D3.ImageFactory.prototype.createInstance = function(update) {
2017-06-16 15:49:53 +02:00
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
2017-02-01 16:09:34 +01:00
var instance = null;
if (update) {
instance = this.instance;
} else {
instance = new THREE.ImageLoader();
2017-01-05 19:34:28 +01:00
}
2017-02-01 16:09:34 +01:00
instance.crossOrigin = '';
return instance;
2017-01-05 19:34:28 +01:00
};
/**
2017-02-01 16:09:34 +01:00
* Update instance
*/
GameLib.D3.ImageFactory.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**
* Loads an image, either returns the image immediately if already loaded, or a promise to load the image
* @param imagePath
* @returns {*}
2017-01-05 19:34:28 +01:00
* @constructor
*/
2017-02-01 16:09:34 +01:00
GameLib.D3.ImageFactory.prototype.loadImage = function(
2017-05-22 20:38:00 +02:00
imagePath,
force
2017-01-05 19:34:28 +01:00
) {
2017-02-01 16:09:34 +01:00
imagePath = imagePath.replace(new RegExp('\/*'), '/');
if (!imagePath) {
console.log('Attempted to download bad URL : ' + imagePath);
throw new Error('Bad URL : ' + imagePath);
}
2017-05-22 20:38:00 +02:00
if (!force && this.promiseList[imagePath]) {
2017-02-01 16:09:34 +01:00
return this.promiseList[imagePath];
}
2017-01-05 19:34:28 +01:00
2017-02-01 16:09:34 +01:00
var defer = Q.defer();
2017-01-05 19:34:28 +01:00
2017-02-01 16:09:34 +01:00
this.promiseList[imagePath] = defer.promise;
this.instance.load(
this.baseUrl + imagePath + '?ts=' + Date.now(),
2017-01-05 19:34:28 +01:00
function (image) {
2017-06-08 18:17:03 +02:00
GameLib.Event.Emit(
2017-06-09 16:03:05 +02:00
GameLib.Event.IMAGE_LOADED,
2017-06-08 18:17:03 +02:00
{
imagePath : imagePath,
imageInstance : image
}
);
2017-01-05 19:34:28 +01:00
defer.resolve(image);
2017-02-01 16:09:34 +01:00
}.bind(this),
2017-01-05 19:34:28 +01:00
function onProgress(xhr) {
2017-02-01 16:09:34 +01:00
if (this.progressCallback) {
this.progressCallback((xhr.loaded / xhr.total * 100));
2017-01-05 19:34:28 +01:00
}
2017-02-01 16:09:34 +01:00
}.bind(this),
2017-01-05 19:34:28 +01:00
function onError() {
2017-02-01 16:09:34 +01:00
defer.reject('Failed to download image : ' + this.baseUrl + imagePath);
}.bind(this)
2017-01-05 19:34:28 +01:00
);
2017-02-01 16:09:34 +01:00
return this.promiseList[imagePath];
2017-01-05 19:34:28 +01:00
};
2017-02-01 16:09:34 +01:00
/**
* Converts a GameLib.D3.ImageFactory to a GameLib.D3.API.ImageFactory
* @returns {GameLib.D3.API.ImageFactory}
*/
2017-05-16 14:51:57 +02:00
GameLib.D3.ImageFactory.prototype.toApiObject = function() {
2017-02-01 16:09:34 +01:00
return new GameLib.D3.API.ImageFactory(
this.id,
this.name,
this.baseUrl,
GameLib.Utils.IdOrNull(this.parentEntity)
);
};
/**
* Returns a new GameLib.D3.ImageFactory from a GameLib.D3.API.ImageFactory
* @param graphics GameLib.D3.Graphics
* @param objectImageFactory GameLib.D3.API.ImageFactory
* @returns {GameLib.D3.ImageFactory}
*/
2017-06-14 14:21:57 +02:00
GameLib.D3.ImageFactory.FromObject = function(graphics, objectImageFactory) {
2017-02-01 16:09:34 +01:00
return new GameLib.D3.ImageFactory(
graphics,
2017-06-14 14:21:57 +02:00
GameLib.D3.API.ImageFactory.FromObject(objectImageFactory)
2017-02-01 16:09:34 +01:00
);
};