r3-legacy/src/game-lib-d3-controls-editor.js

147 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-08-24 22:20:40 +02:00
/**
* Controls Superset - The apiControls properties get moved into the Controls object itself, and then the instance is created
* @param graphics GameLib.D3.Graphics
* @param apiControls GameLib.D3.API.Controls
* @param raycaster
* @param renderer
* @constructor
*/
GameLib.D3.Controls.Editor = function (
graphics,
apiControls,
raycaster,
renderer
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(raycaster)) {
raycaster = null;
}
this.raycaster = raycaster;
if (GameLib.Utils.UndefinedOrNull(renderer)) {
renderer = null;
}
this.renderer = renderer;
if (this.raycaster instanceof GameLib.D3.API.Raycaster) {
this.raycaster = new GameLib.D3.Raycaster(
this.graphics,
this.raycaster
);
}
if (this.renderer instanceof GameLib.D3.API.Renderer) {
this.renderer = new GameLib.D3.Renderer(
this.graphics,
this.renderer
)
}
GameLib.D3.Controls.call(
this,
this.graphics,
apiControls
);
};
/**
* Inheritance
* @type {GameLib.D3.Controls}
*/
GameLib.D3.Controls.Editor.prototype = Object.create(GameLib.D3.Controls.prototype);
GameLib.D3.Controls.Editor.prototype.constructor = GameLib.D3.Controls.Editor;
/**
* Create Instance
* @returns {THREE.EditorControls}
*/
GameLib.D3.Controls.Editor.prototype.createInstance = function() {
console.log('delaying controls instance creation - call GameLib.D3.Controls.Editor.delayInstance() to create the instance');
};
GameLib.D3.Controls.Editor.prototype.delayedInstance = function() {
console.log('GameLib.D3.Controls.Editor.delayedInstance() called');
if (!this.renderer) {
throw new Error('No renderer at time of creating instance');
}
if (!this.renderer.camera) {
throw new Error('No camera at time of instance');
}
if (!this.renderer.domElement) {
throw new Error('No dom element at time of instance');
}
if (!this.renderer.camera.instance) {
throw new Error('No camera instance at time of instance');
}
if (!this.renderer.domElement.instance) {
throw new Error('No dom element instance at time of instance');
}
var instance = new THREE.EditorControls(
this.renderer.camera.instance,
this.renderer.domElement.instance
);
return instance;
};
/**
* Update Instance
*/
GameLib.D3.Controls.Editor.prototype.updateInstance = function() {
console.warn('an update instance was called on editor controls - which, if not called from within a running system at the right time will affect the order of input event handling and cause system instability');
if (this.instance) {
this.instance.dispose();
delete this.instance;
}
this.instance = this.delayedInstance();
GameLib.D3.Controls.prototype.updateInstance.call(this);
};
/**
* Converts a GameLib.D3.Controls.Editor to a GameLib.D3.API.Mesh
* @returns {GameLib.D3.API.Controls}
2017-08-24 22:20:40 +02:00
*/
GameLib.D3.Controls.Editor.prototype.toApiObject = function() {
2017-08-29 21:58:11 +02:00
var apiControls = GameLib.D3.Controls.prototype.toApiObject.call(this);
2017-08-24 22:20:40 +02:00
2017-08-29 21:58:11 +02:00
apiControls.raycaster = GameLib.Utils.IdOrNull(this.raycaster);
apiControls.renderer = GameLib.Utils.IdOrNull(this.renderer);
2017-08-24 22:20:40 +02:00
2017-08-29 21:58:11 +02:00
return apiControls;
2017-08-24 22:20:40 +02:00
};
/**
* Construct an Editor Controls object from data
* @param graphics
2017-08-29 21:58:11 +02:00
* @param objectControls
2017-08-24 22:20:40 +02:00
* @returns {GameLib.D3.Controls.Editor}
* @constructor
*/
2017-08-29 21:58:11 +02:00
GameLib.D3.Controls.Editor.FromObject = function(graphics, objectControls) {
2017-08-24 22:20:40 +02:00
var apiControls = GameLib.D3.API.Controls.FromObject(objectControls);
2017-08-24 22:20:40 +02:00
apiControls.renderer = objectControls.renderer;
apiControls.raycaster = objectControls.raycaster;
2017-08-24 22:20:40 +02:00
return new GameLib.D3.Controls.Editor(
graphics,
apiControls
2017-08-24 22:20:40 +02:00
);
};