diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.js index 0e91d60..a98a051 100644 --- a/src/game-lib-d3-input-editor.js +++ b/src/game-lib-d3-input-editor.js @@ -53,11 +53,10 @@ GameLib.D3.Input.Editor = function ( * window event handlers when we need to * @type {function()} */ - this.resize = this.onWindowResize.bind(this); - this.mouseMove = this.onMouseMove.bind(this); - this.mouseDown = this.onMouseDown.bind(this); - this.keyPress = this.onKeyPress.bind(this); - this.contextMenu = this.onContextMenu.bind(this); + this.mouseMove = null; + this.mouseDown = null; + this.keyDown = null; + this.contextMenu = null; this.raycaster = new GameLib.D3.Raycaster( this.graphics @@ -76,50 +75,11 @@ GameLib.D3.Input.Editor.prototype = Object.create(GameLib.D3.API.Input.Editor.pr GameLib.D3.Input.Editor.prototype.constructor = GameLib.D3.Input.Editor; GameLib.D3.Input.Editor.prototype.createInstance = function(update) { - var instance = null; if (update) { - instance = this.instance; - instance.camera = this.camera.instance; - return instance; - } else { - instance = new THREE.EditorControls( - this.camera.instance, - this.domElement.instance - ) - } - - // this.domElement.instance.addEventListener( - // 'mousemove', - // this.mouseMove, - // false - // ); - // - // this.domElement.instance.addEventListener( - // 'contextmenu', - // this.contextMenu, - // false - // ); - // - // this.domElement.instance.addEventListener( - // 'mousedown', - // this.mouseDown, - // false - // ); - - this.domElement.instance.addEventListener( - 'keydown', - this.keyPress, - false - ); - - //TODO : window resize - // this.dom.window.addEventListener( - // 'resize', - // this.resize, - // false - // ); + instance = this.instance; + } return instance; }; @@ -156,261 +116,90 @@ GameLib.D3.Input.Editor.FromObjectComponent = function(graphics, objectComponent ); }; -GameLib.D3.Input.Editor.prototype.onWindowResize = function() { - // - // this.domContainer.instance.style.height = (this.window.innerHeight - this.containerHeightOffset) + 'px'; - // this.domContainer.instance.style.width = (this.window.innerWidth - this.containerWidthOffset) + 'px'; - - var width = this.window.innerWidth - this.widthOffset; - - var height = this.window.innerHeight - this.heightOffset; - - //TODO: map the relative viewport sizes and offsets with the size differences - - this.editor.viewports.map( - function(viewport) { - viewport.width = width; - viewport.height = height; - viewport.updateInstance(); - } - ); - - this.editor.game.viewports.map( - function(viewport) { - viewport.width = width; - viewport.height = height; - viewport.updateInstance(); - } - ); - // - // this.scene.cameras[this.scene.activeCameraIndex].aspect = () / window.innerHeight; - // this.scene.cameras[this.scene.activeCameraIndex].updateInstance(); - // - // this.scene.renderers[this.scene.activeRendererIndex].width = window.innerWidth - 400; - // this.scene.renderers[this.scene.activeRendererIndex].height = window.innerHeight; - // this.scene.renderers[this.scene.activeRendererIndex].updateInstance(); -}; - /** * Keypress events - * @param event + * @param entity + * @returns {Function} */ -GameLib.D3.Input.Editor.prototype.onKeyPress = function(event) { +GameLib.D3.Input.Editor.prototype.onKeyDown = function(entity) { - console.log('keypressed ' + event.code); + return function(event) { + console.log('entity ' + entity.name + 'emitted keypress ' + event.code); - if (event.code === 'KeyV') { - //todo - change view - }; + // BELOW is just a test to stop systems from keypress - it works nicely :) + // but of course, when the system is stopped it can no longer be started by keypress + // var inputSystem = entity.getComponent(GameLib.System).reduce( + // function(result, system){ + // if (system.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { + // result = system; + // } + // return result; + // }, + // null + // ); + // + // inputSystem.stop(); + } - if (event.code == "KeyQ") { - - this.editor.allSelected = !this.editor.allSelected; - - this.editor.selectedObjects = []; - - if (this.editor.allSelected) { - for (var property in this.editor.idToObject) { - if (this.editor.idToObject.hasOwnProperty(property)) { - this.editor.selectedObjects.push( - new GameLib.D3.SelectedObject( - this.graphics, - this.editor.idToObject(property) - ) - ) - } - } - } - - if (this.editor.onSelectionChanged) { - this.editor.onSelectionChanged(this.editor); - } - } - - if (event.code == 'KeyG') { - if (!this.meshMoveMode) { - console.log('move mode'); - this.meshMoveMode = true; - } - } - - if (event.code == 'KeyX') { - if (this.meshMoveMode) { - console.log('move along x'); - this.meshMoveXMode = true; - this.meshMoveYMode = false; - this.meshMoveZMode = false; - } - } - - if (event.code == 'KeyY') { - if (this.meshMoveMode) { - console.log('move along y'); - this.meshMoveXMode = false; - this.meshMoveYMode = true; - this.meshMoveZMode = false; - } - } - - if (event.code == 'KeyZ') { - if (this.meshMoveMode) { - console.log('move along z'); - this.meshMoveXMode = false; - this.meshMoveYMode = false; - this.meshMoveZMode = true; - } - } - - if (event.code == 'Escape') { - if (this.meshMoveMode) { - this.meshMoveMode = false; - console.log('TODO: implement restore positions'); - } - } - - if (event.code == 'Enter') { - if (this.meshMoveMode) { - this.meshMoveMode = false; - console.log('TODO: implement apply positions'); - } - } }; /** - * Mouse click events - * @param event - * @returns {boolean} + * MouseMove events + * @param entity + * @returns {Function} */ -GameLib.D3.Input.Editor.prototype.onMouseDown = function(event) { +GameLib.D3.Input.Editor.prototype.onMouseMove = function(entity) { - if (event.button == 2) { + return function(event) { - event.cancelBubble = true; - event.preventDefault(); + } - if (event.stopPropagation) { - event.stopPropagation(); - } - - var meshes = []; - - for (var property in this.editor.idToObject) { - if (this.editor.idToObject.hasOwnProperty(property)) { - if (this.editor.idToObject[property] instanceof GameLib.D3.Mesh) { - meshes.push(this.editor.idToObject[property]); - } - } - } - - var intersects = this.raycaster.getIntersectedObjects(meshes); - - if (intersects.length > 0) { - - var index = -1; - - for (var s = 0; s < this.editor.selectedObjects.length; s++) { - if (this.editor.selectedObjects[s].object == intersects[0]) { - index = s; - break; - } - } - - if (index == -1) { - /** - * The object is not selected, select it - */ - this.selectObject(intersects[0]); - - } else { - /** - * De-select the objec - */ - var delta = Date.now() - this.editor.selectedObjects[index].lastUpdate; - if (delta > this.selectDelayMs) { - this.unselectObject(intersects[0]); - } - } - - if (this.editor.onSelectionChanged) { - this.editor.onSelectionChanged(this.editor); - } - } - - return false; - } - - if (event.button == 0) { - if (this.meshMoveMode) { - this.meshMoveMode = false; - this.meshMoveXMode = false; - this.meshMoveYMode = false; - this.meshMoveZMode = false; - } - } }; /** - * Mouse move events - * @param event + * MouseDown events + * @param entity + * @returns {Function} */ -GameLib.D3.Input.Editor.prototype.onMouseMove = function(event) { +GameLib.D3.Input.Editor.prototype.onMouseDown = function(entity) { - var clientX = event.clientX - this.widthOffset; + return function(event) { - this.mouse.x = ((clientX / (window.innerWidth - this.widthOffset))) * 2 - 1; - this.mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; + if (event.button === 2) { - this.raycaster.instance.setFromCamera( - this.mouse, - this.camera.instance - ); + var renderer = entity.getFirstComponent(GameLib.D3.Renderer); - if (this.meshMoveMode) { + this.mouse.x = (event.offsetX / renderer.instance.domElement.width) * 2 - 1; + this.mouse.y = -(event.offsetY / renderer.instance.domElement.height) * 2 + 1; - var units = event.movementY; + this.raycaster.instance.setFromCamera( + this.mouse, + this.camera.instance + ); - if (this.meshMoveXMode) { - this.moveSelectedObjects('x', units); - } + var meshes = entity.getComponent(GameLib.D3.Mesh); - if (this.meshMoveYMode) { - this.moveSelectedObjects('y', units); - } + var intersects = this.raycaster.getIntersectedObjects(meshes); - if (this.meshMoveZMode) { - this.moveSelectedObjects('z', units); - } - } -}; + if (intersects.length > 0) { -/** - * Moves selected objects along an axis - * @param alongAxis - * @param units - */ -GameLib.D3.Input.Editor.prototype.moveSelectedObjects = function(alongAxis, units) { + /** + * Prevent default action (like context menu or whatever) + */ + event.preventDefault(); - for (var s = 0; s < this.editor.selectedObjects.length; s++) { + /** + * Prevent other event listeners for 'mousedown' from executing their actions + */ + event.stopImmediatePropagation(); - var object = this.editor.selectedObjects[s].object; + intersects[0].instance.material.wireframe = !intersects[0].instance.material.wireframe; - if (object.position) { - if (alongAxis == 'x') { - object.position.x += units; - } - if (alongAxis == 'y') { - object.position.y += units; - } - if (alongAxis == 'z') { - object.position.z += units; - } - - if (object.updateInstance) { - object.updateInstance(); - } - } - } + console.log('object(s) instersected'); + } + } + } }; /** @@ -420,18 +209,243 @@ GameLib.D3.Input.Editor.prototype.moveSelectedObjects = function(alongAxis, unit */ GameLib.D3.Input.Editor.prototype.onContextMenu = function(event){ - if (event.stopPropagation) { - event.stopPropagation(); - } + if (event.stopPropagation) { + event.stopPropagation(); + } - if (event.preventDefault) { - event.preventDefault(); - } + if (event.preventDefault) { + event.preventDefault(); + } - event.cancelBubble = true; - return false; + event.cancelBubble = true; + return false; }; -GameLib.D3.Input.Editor.prototype.update = function(deltaTime) { - return; -}; \ No newline at end of file + +// +// console.log('keypressed ' + event.code); +// +// if (event.code === 'KeyV') { +// //todo - change view +// } +// +// if (event.code == "KeyQ") { +// +// this.editor.allSelected = !this.editor.allSelected; +// +// this.editor.selectedObjects = []; +// +// if (this.editor.allSelected) { +// for (var property in this.editor.idToObject) { +// if (this.editor.idToObject.hasOwnProperty(property)) { +// this.editor.selectedObjects.push( +// new GameLib.D3.SelectedObject( +// this.graphics, +// this.editor.idToObject(property) +// ) +// ) +// } +// } +// } +// +// if (this.editor.onSelectionChanged) { +// this.editor.onSelectionChanged(this.editor); +// } +// } +// +// if (event.code == 'KeyG') { +// if (!this.meshMoveMode) { +// console.log('move mode'); +// this.meshMoveMode = true; +// } +// } +// +// if (event.code == 'KeyX') { +// if (this.meshMoveMode) { +// console.log('move along x'); +// this.meshMoveXMode = true; +// this.meshMoveYMode = false; +// this.meshMoveZMode = false; +// } +// } +// +// if (event.code == 'KeyY') { +// if (this.meshMoveMode) { +// console.log('move along y'); +// this.meshMoveXMode = false; +// this.meshMoveYMode = true; +// this.meshMoveZMode = false; +// } +// } +// +// if (event.code == 'KeyZ') { +// if (this.meshMoveMode) { +// console.log('move along z'); +// this.meshMoveXMode = false; +// this.meshMoveYMode = false; +// this.meshMoveZMode = true; +// } +// } +// +// if (event.code == 'Escape') { +// if (this.meshMoveMode) { +// this.meshMoveMode = false; +// console.log('TODO: implement restore positions'); +// } +// } +// +// if (event.code == 'Enter') { +// if (this.meshMoveMode) { +// this.meshMoveMode = false; +// console.log('TODO: implement apply positions'); +// } +// } +// }; + +// GameLib.D3.Input.Editor.prototype.onMouseDown = function(entity) { +// +// return function(event) { +// +// if (event.button === 2) { +// event.cancelBubble = true; +// +// event.preventDefault(); +// +// if (event.stopPropagation) { +// event.stopPropagation(); +// } +// +// var meshes = entity.queryComponents(GameLib.D3.Mesh); +// +// var intersects = this.raycaster.getIntersectedObjects(meshes); +// +// if (intersects.length > 0) { +// +// console.log('object(s) instersected'); +// +// // var index = -1; +// // +// // for (var s = 0; s < this.editor.selectedObjects.length; s++) { +// // if (this.editor.selectedObjects[s].object == intersects[0]) { +// // index = s; +// // break; +// // } +// // } +// // +// // if (index == -1) { +// // /** +// // * The object is not selected, select it +// // */ +// // this.selectObject(intersects[0]); +// // +// // } else { +// // /** +// // * De-select the objec +// // */ +// // var delta = Date.now() - this.editor.selectedObjects[index].lastUpdate; +// // if (delta > this.selectDelayMs) { +// // this.unselectObject(intersects[0]); +// // } +// // } +// // +// // if (this.editor.onSelectionChanged) { +// // this.editor.onSelectionChanged(this.editor); +// // } +// } +// +// return false; +// } +// } +// }; + +// /** +// * Mouse click events +// * @param event +// * @returns {boolean} +// */ +// GameLib.D3.Input.Editor.prototype.onMouseDown = function(event) { +// +// if (event.button === 2) { +// +// +// +// +// +// } +// +// if (event.button == 0) { +// if (this.meshMoveMode) { +// this.meshMoveMode = false; +// this.meshMoveXMode = false; +// this.meshMoveYMode = false; +// this.meshMoveZMode = false; +// } +// } +// }; + +// /** +// * Mouse move events +// * @param event +// */ +// GameLib.D3.Input.Editor.prototype.onMouseMove = function(event) { +// +// // var clientX = event.clientX - this.widthOffset; +// // this.mouse.x = ((clientX / (window.innerWidth - this.widthOffset))) * 2 - 1; +// // this.mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; +// +// this.mouse.x = event.clientX; +// this.mouse.y = event.clientY; +// +// console.log("mouse (" + this.mouse.x + ", " + this.mouse.y + ")"); +// +// this.raycaster.instance.setFromCamera( +// this.mouse, +// this.camera.instance +// ); +// +// if (this.meshMoveMode) { +// +// var units = event.movementY; +// +// if (this.meshMoveXMode) { +// this.moveSelectedObjects('x', units); +// } +// +// if (this.meshMoveYMode) { +// this.moveSelectedObjects('y', units); +// } +// +// if (this.meshMoveZMode) { +// this.moveSelectedObjects('z', units); +// } +// } +// }; + +// /** +// * Moves selected objects along an axis +// * @param alongAxis +// * @param units +// */ +// GameLib.D3.Input.Editor.prototype.moveSelectedObjects = function(alongAxis, units) { +// +// for (var s = 0; s < this.editor.selectedObjects.length; s++) { +// +// var object = this.editor.selectedObjects[s].object; +// +// if (object.position) { +// if (alongAxis == 'x') { +// object.position.x += units; +// } +// if (alongAxis == 'y') { +// object.position.y += units; +// } +// if (alongAxis == 'z') { +// object.position.z += units; +// } +// +// if (object.updateInstance) { +// object.updateInstance(); +// } +// } +// } +// }; diff --git a/src/game-lib-d3-raycaster.js b/src/game-lib-d3-raycaster.js index 97fe8a9..0f9d70d 100644 --- a/src/game-lib-d3-raycaster.js +++ b/src/game-lib-d3-raycaster.js @@ -154,18 +154,24 @@ GameLib.D3.Raycaster.prototype.getIntersectedObjects = function(meshes) { } ); - var instanceIdToMesh = meshes.reduce( - function (result, mesh) { - result[mesh.instance.id] = mesh; - }, - {} - ); - var intersects = this.instance.intersectObjects(meshInstances); - return intersects.map(function (intersect) { - return instanceIdToMesh[intersect.object.id]; - }); + return intersects.reduce( + + function (result, intersect) { + + meshes.map( + function(mesh){ + if (mesh.instance === intersect.object){ + result.push(mesh); + } + } + ); + + return result; + }, + [] + ); }; diff --git a/src/game-lib-system.js b/src/game-lib-system.js index ad12aea..678e1ab 100644 --- a/src/game-lib-system.js +++ b/src/game-lib-system.js @@ -60,7 +60,30 @@ GameLib.System.SYSTEM_TYPE_ALL = 0x7; GameLib.System.prototype.start = function() { if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { - // this.driveInputObjects = this.entityManager.query([GameLib.D3.Input.Drive]); + + /** + * Hookup all editor input capabilities + */ + var entities = this.entityManager.query([GameLib.D3.Input.Editor]); + + entities.map(function(entity){ + + var component = entity.getFirstComponent(GameLib.D3.Input.Editor); + + component.mouseDown = component.onMouseDown(entity).bind(component); + component.mouseMove = component.onMouseMove(entity).bind(component); + component.keyDown = component.onKeyDown(entity).bind(component); + // component.contextMenu = component.onContextMenu(entity).bind(component); + + + component.domElement.instance.addEventListener('mousedown', component.mouseDown, false); + component.domElement.instance.addEventListener('mousemove', component.mouseMove, false); + component.domElement.instance.addEventListener('keydown', component.keyDown, false); + component.controls = new THREE.EditorControls(component.camera.instance, component.domElement.instance); + + // component.domElement.instance.addEventListener('contextmenu', component.contextMenu, false); + }) + } if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) { @@ -115,11 +138,11 @@ GameLib.System.prototype.start = function() { GameLib.System.prototype.update = function(deltaTime) { if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { - this.driveInputObjects.forEach( - function(object) { - object.update(deltaTime); - } - ); + // this.driveInputObjects.forEach( + // function(object) { + // object.update(deltaTime); + // } + // ); } if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) { @@ -191,7 +214,7 @@ GameLib.System.prototype.update = function(deltaTime) { scene.instance, camera.instance ); - renderer.instance.clearDepth(); + //renderer.instance.clearDepth(); }); } ); @@ -206,7 +229,22 @@ GameLib.System.prototype.update = function(deltaTime) { GameLib.System.prototype.stop = function() { if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { - // this.driveInputObjects = []; + + /** + * Now remove all editor input capabilities + */ + var entities = this.entityManager.query([GameLib.D3.Input.Editor]); + + entities.map(function(entity){ + var component = entity.getFirstComponent(GameLib.D3.Input.Editor); + component.domElement.instance.removeEventListener('mousedown', component.mouseDown, false); + component.domElement.instance.removeEventListener('mousemove', component.mouseMove, false); + component.domElement.instance.removeEventListener('keydown', component.keyDown, false); + // component.domElement.instance.removeEventListener('contextmenu', component.contextMenu, false); + component.controls.dispose(); + }); + + console.log('stopped all input systems'); } if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) {