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

156 lines
3.7 KiB
JavaScript
Raw Permalink 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
2018-04-09 10:05:13 +02:00
* @returns {R3.D3.ImageFactory}
2017-01-05 19:34:28 +01:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.ImageFactory = function (
2017-01-05 19:34:28 +01:00
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
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(apiImageFactory)) {
2017-02-01 16:09:34 +01:00
apiImageFactory = {};
}
2017-01-05 19:34:28 +01:00
2018-04-09 10:05:13 +02:00
if (apiImageFactory instanceof R3.D3.ImageFactory) {
return apiImageFactory;
}
2018-04-09 10:05:13 +02:00
R3.D3.API.ImageFactory.call(
2017-02-01 16:09:34 +01:00
this,
apiImageFactory.id,
apiImageFactory.name,
apiImageFactory.baseUrl,
apiImageFactory.parentEntity
);
2017-01-05 19:34:28 +01:00
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(progressCallback)) {
2017-06-16 15:49:53 +02:00
progressCallback = null;
}
this.progressCallback = progressCallback;
2018-04-09 10:05:13 +02:00
R3.Component.call(
2017-06-16 15:49:53 +02:00
this,
2018-04-09 10:05:13 +02:00
R3.Component.COMPONENT_IMAGE_FACTORY
2017-06-16 15:49:53 +02:00
);
2017-02-01 16:09:34 +01:00
this.promiseList = {};
};
2017-01-05 19:34:28 +01:00
2018-04-09 10:05:13 +02:00
R3.D3.ImageFactory.prototype = Object.create(R3.D3.API.ImageFactory.prototype);
R3.D3.ImageFactory.prototype.constructor = R3.D3.ImageFactory;
2017-01-05 19:34:28 +01:00
2018-04-09 10:05:13 +02:00
R3.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
*/
2018-04-09 10:05:13 +02:00
R3.D3.ImageFactory.prototype.updateInstance = function() {
2017-02-01 16:09:34 +01:00
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
*/
2018-04-09 10:05:13 +02:00
R3.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
2018-04-09 10:05:13 +02:00
R3.Event.Emit(
R3.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
/**
2018-04-09 10:05:13 +02:00
* Converts a R3.D3.ImageFactory to a R3.D3.API.ImageFactory
* @returns {R3.D3.API.ImageFactory}
2017-02-01 16:09:34 +01:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.ImageFactory.prototype.toApiObject = function() {
return new R3.D3.API.ImageFactory(
2017-02-01 16:09:34 +01:00
this.id,
this.name,
this.baseUrl,
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.parentEntity)
2017-02-01 16:09:34 +01:00
);
};
/**
2018-04-09 10:05:13 +02:00
* Returns a new R3.D3.ImageFactory from a R3.D3.API.ImageFactory
* @param graphics R3.D3.Graphics
* @param objectImageFactory R3.D3.API.ImageFactory
* @returns {R3.D3.ImageFactory}
2017-02-01 16:09:34 +01:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.ImageFactory.FromObject = function(graphics, objectImageFactory) {
2017-02-01 16:09:34 +01:00
2018-04-09 10:05:13 +02:00
return new R3.D3.ImageFactory(
2017-02-01 16:09:34 +01:00
graphics,
2018-04-09 10:05:13 +02:00
R3.D3.API.ImageFactory.FromObject(objectImageFactory)
2017-02-01 16:09:34 +01:00
);
};