awesome input system and editor controls component

beta.r3js.org
-=yb4f310 2017-05-12 15:21:04 +02:00
parent 625fcfedb9
commit be9b88503d
3 changed files with 358 additions and 300 deletions

View File

@ -53,11 +53,10 @@ GameLib.D3.Input.Editor = function (
* window event handlers when we need to * window event handlers when we need to
* @type {function()} * @type {function()}
*/ */
this.resize = this.onWindowResize.bind(this); this.mouseMove = null;
this.mouseMove = this.onMouseMove.bind(this); this.mouseDown = null;
this.mouseDown = this.onMouseDown.bind(this); this.keyDown = null;
this.keyPress = this.onKeyPress.bind(this); this.contextMenu = null;
this.contextMenu = this.onContextMenu.bind(this);
this.raycaster = new GameLib.D3.Raycaster( this.raycaster = new GameLib.D3.Raycaster(
this.graphics 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.constructor = GameLib.D3.Input.Editor;
GameLib.D3.Input.Editor.prototype.createInstance = function(update) { GameLib.D3.Input.Editor.prototype.createInstance = function(update) {
var instance = null; var instance = null;
if (update) { if (update) {
instance = this.instance; 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
// );
return 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 * 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') { // BELOW is just a test to stop systems from keypress - it works nicely :)
//todo - change view // 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 * MouseMove events
* @param event * @param entity
* @returns {boolean} * @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 * MouseDown events
* @param event * @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; if (event.button === 2) {
this.mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
this.raycaster.instance.setFromCamera( var renderer = entity.getFirstComponent(GameLib.D3.Renderer);
this.mouse,
this.camera.instance
);
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) { var meshes = entity.getComponent(GameLib.D3.Mesh);
this.moveSelectedObjects('x', units);
}
if (this.meshMoveYMode) { var intersects = this.raycaster.getIntersectedObjects(meshes);
this.moveSelectedObjects('y', units);
}
if (this.meshMoveZMode) { if (intersects.length > 0) {
this.moveSelectedObjects('z', units);
}
}
};
/** /**
* Moves selected objects along an axis * Prevent default action (like context menu or whatever)
* @param alongAxis */
* @param units event.preventDefault();
*/
GameLib.D3.Input.Editor.prototype.moveSelectedObjects = function(alongAxis, units) {
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) { console.log('object(s) instersected');
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();
}
}
}
}; };
/** /**
@ -420,18 +209,243 @@ GameLib.D3.Input.Editor.prototype.moveSelectedObjects = function(alongAxis, unit
*/ */
GameLib.D3.Input.Editor.prototype.onContextMenu = function(event){ GameLib.D3.Input.Editor.prototype.onContextMenu = function(event){
if (event.stopPropagation) { if (event.stopPropagation) {
event.stopPropagation(); event.stopPropagation();
} }
if (event.preventDefault) { if (event.preventDefault) {
event.preventDefault(); event.preventDefault();
} }
event.cancelBubble = true; event.cancelBubble = true;
return false; return false;
}; };
GameLib.D3.Input.Editor.prototype.update = function(deltaTime) {
return; //
}; // 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();
// }
// }
// }
// };

View File

@ -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); var intersects = this.instance.intersectObjects(meshInstances);
return intersects.map(function (intersect) { return intersects.reduce(
return instanceIdToMesh[intersect.object.id];
}); function (result, intersect) {
meshes.map(
function(mesh){
if (mesh.instance === intersect.object){
result.push(mesh);
}
}
);
return result;
},
[]
);
}; };

View File

@ -60,7 +60,30 @@ GameLib.System.SYSTEM_TYPE_ALL = 0x7;
GameLib.System.prototype.start = function() { GameLib.System.prototype.start = function() {
if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { 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) { if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) {
@ -115,11 +138,11 @@ GameLib.System.prototype.start = function() {
GameLib.System.prototype.update = function(deltaTime) { GameLib.System.prototype.update = function(deltaTime) {
if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) {
this.driveInputObjects.forEach( // this.driveInputObjects.forEach(
function(object) { // function(object) {
object.update(deltaTime); // object.update(deltaTime);
} // }
); // );
} }
if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) { if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) {
@ -191,7 +214,7 @@ GameLib.System.prototype.update = function(deltaTime) {
scene.instance, scene.instance,
camera.instance camera.instance
); );
renderer.instance.clearDepth(); //renderer.instance.clearDepth();
}); });
} }
); );
@ -206,7 +229,22 @@ GameLib.System.prototype.update = function(deltaTime) {
GameLib.System.prototype.stop = function() { GameLib.System.prototype.stop = function() {
if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) { 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) { if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) {