r3-legacy/bak/r3-d3-editor.js

538 lines
14 KiB
JavaScript
Raw Permalink Normal View History

2017-01-10 17:04:30 +01:00
/**
* Creates a Editor object
2018-04-09 10:05:13 +02:00
* @param graphics R3.D3.Graphics
* @param apiEditor R3.D3.API.Editor
2017-01-13 16:19:51 +01:00
* @param onSelectionChanged
2017-01-19 17:50:11 +01:00
* @param onSelectObject
* @param onDeSelectObject
2017-01-10 17:04:30 +01:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Editor = function(
2017-01-10 17:04:30 +01:00
graphics,
2017-01-12 17:40:17 +01:00
apiEditor,
2017-01-18 16:03:44 +01:00
onSelectionChanged,
onSelectObject,
onDeSelectObject
2017-01-10 17:04:30 +01:00
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(apiEditor)) {
2017-01-12 17:40:17 +01:00
apiEditor = {};
}
2018-04-09 10:05:13 +02:00
if (apiEditor instanceof R3.D3.Editor) {
return apiEditor;
}
2018-04-09 10:05:13 +02:00
R3.D3.API.Editor.call(
2017-01-10 17:04:30 +01:00
this,
apiEditor.id,
apiEditor.name,
2017-01-20 13:40:27 +01:00
apiEditor.baseUrl,
apiEditor.path,
2017-02-21 18:55:18 +01:00
apiEditor.imageFactory,
2017-01-20 13:40:27 +01:00
apiEditor.games,
apiEditor.scenes,
apiEditor.cameras,
apiEditor.composers,
apiEditor.viewports,
apiEditor.renderers,
apiEditor.renderTargets,
apiEditor.systems,
apiEditor.entityManager,
2017-01-10 17:04:30 +01:00
apiEditor.allSelected,
2017-01-20 13:40:27 +01:00
apiEditor.selectedObjects,
2017-01-10 17:04:30 +01:00
apiEditor.parentEntity
);
2017-01-12 17:40:17 +01:00
2018-04-09 10:05:13 +02:00
if (this.imageFactory instanceof R3.D3.API.ImageFactory) {
this.imageFactory = new R3.D3.ImageFactory(
2017-02-21 18:55:18 +01:00
this.graphics,
this.imageFactory
);
}
2017-01-20 13:40:27 +01:00
if (this.games) {
this.games = this.games.map(
function (apiGame) {
2018-04-09 10:05:13 +02:00
if (apiGame instanceof R3.D3.API.Game) {
return new R3.D3.Game(
2017-01-20 13:40:27 +01:00
this.graphics,
2017-02-21 18:55:18 +01:00
apiGame,
this.imageFactory
2017-01-20 13:40:27 +01:00
)
}
else {
console.warn('game not of type API.Game');
throw new Error('game not of type API.Game');
}
}.bind(this)
)
2017-01-13 16:19:51 +01:00
}
2017-01-12 17:40:17 +01:00
2017-01-20 13:40:27 +01:00
this.scenes = this.scenes.map(
function (apiScene) {
2018-04-09 10:05:13 +02:00
if (apiScene instanceof R3.D3.API.Scene) {
return new R3.D3.Scene(
2017-01-20 13:40:27 +01:00
this.graphics,
apiScene,
this.imageFactory,
true
)
} else {
console.warn('apiScene not of type API.Scene');
throw new Error('apiScene not of type API.Scene');
}
}.bind(this)
);
this.cameras = this.cameras.map(
function (apiCamera) {
2018-04-09 10:05:13 +02:00
if (apiCamera instanceof R3.D3.API.Camera) {
return new R3.D3.Camera(
2017-01-20 13:40:27 +01:00
this.graphics,
apiCamera
)
} else {
console.warn('apiCamera not of type API.Camera');
throw new Error('apiCamera not of type API.Camera');
}
}.bind(this)
);
this.composers = this.composers.map(
function (apiComposer) {
2018-04-09 10:05:13 +02:00
if (apiComposer instanceof R3.D3.API.Composer) {
return new R3.D3.Composer(
2017-01-20 13:40:27 +01:00
this.graphics,
apiComposer
)
} else {
console.warn('apiComposer not of type API.Composer');
throw new Error('apiComposer not of type API.Composer');
}
}.bind(this)
);
this.viewports = this.viewports.map(
function (apiViewport) {
2018-04-09 10:05:13 +02:00
if (apiViewport instanceof R3.D3.API.Viewport) {
return new R3.D3.Viewport(
2017-01-12 17:40:17 +01:00
this.graphics,
apiViewport
)
} else {
2017-01-20 13:40:27 +01:00
console.warn('apiViewport not of type API.Viewport');
throw new Error('apiViewport not of type API.Viewport');
}
}.bind(this)
);
this.renderers = this.renderers.map(
function (apiRenderer) {
2018-04-09 10:05:13 +02:00
if (apiRenderer instanceof R3.D3.API.Renderer) {
return new R3.D3.Renderer(
2017-01-20 13:40:27 +01:00
this.graphics,
apiRenderer
)
} else {
console.warn('apiRenderer not of type API.Renderer');
throw new Error('apiRenderer not of type API.Renderer');
}
}.bind(this)
);
this.renderTargets = this.renderTargets.map(
function (apiRenderTarget) {
2018-04-09 10:05:13 +02:00
if (apiRenderTarget instanceof R3.D3.API.RenderTarget) {
return new R3.D3.RenderTarget(
2017-01-20 13:40:27 +01:00
this.graphics,
apiRenderTarget
)
} else {
console.warn('apiRenderTarget not of type API.RenderTarget');
throw new Error('apiRenderTarget not of type API.RenderTarget');
2017-01-12 17:40:17 +01:00
}
2017-01-20 13:40:27 +01:00
}.bind(this)
);
2017-01-12 17:40:17 +01:00
2017-01-20 13:40:27 +01:00
this.systems = this.systems.map(
function (apiSystem) {
2018-04-09 10:05:13 +02:00
if (apiSystem instanceof R3.API.System) {
return new R3.System(
2017-01-20 13:40:27 +01:00
this.graphics,
apiSystem
)
} else {
console.warn('apiSystem not of type API.System');
throw new Error('apiSystem not of type API.System');
}
2017-01-12 17:40:17 +01:00
}.bind(this)
);
2017-01-20 13:40:27 +01:00
if (this.entityManager) {
2018-04-09 10:05:13 +02:00
if (this.entityManager instanceof R3.API.EntityManager) {
this.entityManager = new R3.EntityManager(
2017-01-20 13:40:27 +01:00
this.graphics,
this.entityManager
);
} else {
console.warn('entityManager not of type API.EntityManager');
throw new Error('entityManager not of type API.EntityManager');
}
}
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(onSelectionChanged)) {
2017-01-13 16:19:51 +01:00
onSelectionChanged = null;
2017-01-12 17:40:17 +01:00
}
2017-01-13 16:19:51 +01:00
this.onSelectionChanged = onSelectionChanged;
2017-01-10 17:04:30 +01:00
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(onSelectObject)) {
2017-01-18 16:03:44 +01:00
onSelectObject = null;
}
this.onSelectObject = onSelectObject;
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(onDeSelectObject)) {
2017-01-18 16:03:44 +01:00
onDeSelectObject = null;
}
this.onDeSelectObject = onDeSelectObject;
2017-02-21 18:55:18 +01:00
this.meshMoveMode = false;
this.meshMoveXMode = false;
this.meshMoveYMode = false;
this.meshMoveZMode = false;
2017-02-22 16:06:27 +01:00
this.buildIdToObject();
this.linkObjects(this.idToObject);
2017-01-10 17:04:30 +01:00
this.instance = this.createInstance();
};
2018-04-09 10:05:13 +02:00
R3.D3.Editor.prototype = Object.create(R3.D3.API.Editor.prototype);
R3.D3.Editor.prototype.constructor = R3.D3.Editor;
2017-01-10 17:04:30 +01:00
/**
* Creates a camera instance of 'graphics' type (only THREE for now)
* @returns {THREE.Editor}
*/
2018-04-09 10:05:13 +02:00
R3.D3.Editor.prototype.createInstance = function(update) {
2017-01-10 17:04:30 +01:00
var instance = null;
if (update) {
instance = this.instance;
}
return instance;
};
/**
* Updates the instance with the current state
*/
2018-04-09 10:05:13 +02:00
R3.D3.Editor.prototype.updateInstance = function() {
2017-01-10 17:04:30 +01:00
this.instance = this.createInstance(true);
};
/**
2018-04-09 10:05:13 +02:00
* Converts a R3.D3.Editor to a new R3.D3.API.Editor
* @returns {R3.D3.API.Editor}
2017-01-10 17:04:30 +01:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.Editor.prototype.toApiObject = function() {
2017-01-10 17:04:30 +01:00
2017-02-21 18:55:18 +01:00
var apiImageFactory = null;
2017-01-20 13:40:27 +01:00
var apiGames = [];
var apiScenes = [];
var apiCameras = [];
var apiComposers = [];
var apiViewports = [];
var apiRenderers = [];
var apiRenderTargets = [];
var apiSystems = [];
var apiEntityManager = null;
2017-02-21 18:55:18 +01:00
2018-04-09 10:05:13 +02:00
if (this.imageFactory instanceof R3.D3.ImageFactory) {
2017-05-16 14:51:57 +02:00
apiImageFactory = this.imageFactory.toApiObject();
2017-02-21 18:55:18 +01:00
}
2017-01-20 13:40:27 +01:00
if (this.games) {
apiGames = this.games.map(
function(game) {
2018-04-09 10:05:13 +02:00
if (game instanceof R3.D3.Game) {
2017-05-16 14:51:57 +02:00
return game.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('game not an instance of Game');
throw new Error('game not an instance of Game');
}
}
);
2017-01-12 04:44:01 +01:00
}
2017-01-20 13:40:27 +01:00
if (this.scenes) {
apiScenes = this.scenes.map(
function(scene) {
2018-04-09 10:05:13 +02:00
if (scene instanceof R3.D3.Scene) {
2017-05-16 14:51:57 +02:00
return scene.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('scene not an instance of Scene');
throw new Error('scene not an instance of Scene');
}
}
);
}
if (this.cameras) {
apiCameras = this.cameras.map(
function(camera) {
2018-04-09 10:05:13 +02:00
if (camera instanceof R3.D3.Camera) {
2017-05-16 14:51:57 +02:00
return camera.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('camera not an instance of Camera');
throw new Error('camera not an instance of Camera');
}
}
);
}
if (this.composers) {
apiComposers = this.composers.map(
function(composer) {
2018-04-09 10:05:13 +02:00
if (composer instanceof R3.D3.Composer) {
2017-05-16 14:51:57 +02:00
return composer.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('composer not an instance of Composer');
throw new Error('composer not an instance of Composer');
}
}
);
}
if (this.viewports) {
apiViewports = this.viewports.map(
function(viewport) {
2018-04-09 10:05:13 +02:00
if (viewport instanceof R3.D3.Viewport) {
2017-05-16 14:51:57 +02:00
return viewport.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('viewport not an instance of Viewport');
throw new Error('viewport not an instance of Viewport');
}
}
);
}
if (this.renderers) {
apiRenderers = this.renderers.map(
function(renderer) {
2018-04-09 10:05:13 +02:00
if (renderer instanceof R3.D3.Renderer) {
2017-05-16 14:51:57 +02:00
return renderer.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('renderer not an instance of Renderer');
throw new Error('renderer not an instance of Renderer');
}
}
);
}
if (this.renderTargets) {
apiRenderTargets = this.renderTargets.map(
function(renderTarget) {
2018-04-09 10:05:13 +02:00
if (renderTarget instanceof R3.D3.RenderTarget) {
2017-05-16 14:51:57 +02:00
return renderTarget.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('renderTarget not an instance of RenderTarget');
throw new Error('renderTarget not an instance of RenderTarget');
}
}
);
}
if (this.systems) {
apiSystems = this.systems.map(
function(system) {
2018-04-09 10:05:13 +02:00
if (system instanceof R3.System) {
2017-05-16 14:51:57 +02:00
return system.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('system not an instance of System');
throw new Error('system not an instance of System');
}
}
);
}
2017-01-10 17:04:30 +01:00
2017-01-20 13:40:27 +01:00
if (this.entityManager) {
2018-04-09 10:05:13 +02:00
if (this.entityManager instanceof R3.EntityManager) {
2017-05-16 14:51:57 +02:00
apiEntityManager = this.entityManager.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('entityManager not an instance of EntityManager');
throw new Error('entityManager not an instance of EntityManager');
}
}
2018-04-09 10:05:13 +02:00
return new R3.D3.API.Editor(
2017-01-10 17:04:30 +01:00
this.id,
this.name,
2017-01-20 13:40:27 +01:00
this.baseUrl,
this.path,
2017-02-21 18:55:18 +01:00
apiImageFactory,
2017-01-20 13:40:27 +01:00
apiGames,
apiScenes,
apiCameras,
apiComposers,
apiViewports,
apiRenderers,
apiRenderTargets,
apiSystems,
apiEntityManager,
2017-01-10 17:04:30 +01:00
this.allSelected,
2017-01-12 04:44:01 +01:00
this.selectedObjects,
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.parentEntity)
2017-01-10 17:04:30 +01:00
);
};
/**
2018-04-09 10:05:13 +02:00
* Converts from an Object Editor to a R3.D3.Editor
* @param graphics R3.D3.Graphics
2017-01-10 17:04:30 +01:00
* @param objectEditor Object
2018-04-09 10:05:13 +02:00
* @returns {R3.D3.Editor}
2017-01-10 17:04:30 +01:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Editor.FromObjectEditor = function(graphics, objectEditor) {
2017-01-10 17:04:30 +01:00
2018-04-09 10:05:13 +02:00
var apiEditor = R3.D3.API.Editor.FromObjectEditor(objectEditor);
2017-01-10 17:04:30 +01:00
2018-04-09 10:05:13 +02:00
return new R3.D3.Editor(
2017-01-10 17:04:30 +01:00
graphics,
apiEditor
);
};
2017-01-18 16:03:44 +01:00
/**
2018-04-09 10:05:13 +02:00
* Selects a R3 Object
* @param object R3.*
2017-01-18 16:03:44 +01:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.Editor.prototype.selectObject = function(object) {
2017-01-18 16:03:44 +01:00
this.selectedObjects.push(
2018-04-09 10:05:13 +02:00
new R3.D3.SelectedObject(
2017-01-18 16:03:44 +01:00
this.graphics,
2017-01-19 17:50:11 +01:00
object
2017-01-18 16:03:44 +01:00
)
);
if (this.onSelectObject) {
this.onSelectObject();
}
};
/**
2018-04-09 10:05:13 +02:00
* Selects a R3 Object
* @param object R3.*
2017-01-18 16:03:44 +01:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.Editor.prototype.deSelectObject = function(object) {
2017-01-18 16:03:44 +01:00
var results = this.selectedObjects.reduce(
function(results, selectedObject) {
if (selectedObject.object.id == object.id) {
results.removed = selectedObject;
} else {
results.rest.push(selectedObject);
}
return results;
},
{
removed : null,
rest : []
}
);
if (this.onDeSelectObject) {
this.onDeSelectObject(results);
}
2017-01-19 17:50:11 +01:00
};
2017-05-09 15:44:29 +02:00
2018-04-09 10:05:13 +02:00
R3.D3.Editor.prototype.setSize = function(width, height) {
2017-05-09 15:44:29 +02:00
this.viewports.map(
function(viewport){
viewport.width = width;
viewport.height = height;
viewport.updateInstance();
}
);
this.renderers.map(
function(renderer) {
renderer.setSize(width, height);
}
);
};
/**
* Adds a scene to the Editor
2018-04-09 10:05:13 +02:00
* @param scene R3.D3.API.Scene
*/
2018-04-09 10:05:13 +02:00
R3.D3.Editor.prototype.addScene = function(scene) {
2018-04-09 10:05:13 +02:00
if (!scene instanceof R3.D3.Scene &&
!scene instanceof R3.D3.API.Scene) {
throw new Error('Unhandled scene type : ' + scene);
}
2018-04-09 10:05:13 +02:00
scene = new R3.D3.Scene(this.graphics, scene);
this.scenes.push(scene);
this.buildIdToObject();
/**
* We need to add the meshes as components of this scene to the entity 'editor' of the 'entityManager' in this.
*
* We need a notify mechanism to notify the system of new mesh components
*/
scene.meshes.map(
function(mesh){
/**
* Get all entities with 3D renderers and some systems
*/
var entities = this.entityManager.query(
[
2018-04-09 10:05:13 +02:00
R3.D3.Renderer,
R3.System
]
);
entities.map(
function(entity){
/**
* Add all meshes to this entity
*/
entity.addComponent(mesh);
/**
* Now we need to notify all systems of this new components
*/
2018-04-09 10:05:13 +02:00
var systems = entity.getComponents(R3.System);
systems.map(
function(system){
2018-04-09 10:05:13 +02:00
system.notify('meshes', R3.D3.Mesh)
}.bind(this)
)
}.bind(this)
);
}.bind(this)
);
};