From 368c3f785a7e7642900be323835ec13d21229276 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Tue, 20 Mar 2018 10:51:43 +0100 Subject: [PATCH] really fix the input system --- src/game-lib-controls-0.js | 2 + src/game-lib-system-input.js | 458 ++++++++++++++++++++++------------- 2 files changed, 288 insertions(+), 172 deletions(-) diff --git a/src/game-lib-controls-0.js b/src/game-lib-controls-0.js index 036ee0e..17b96db 100644 --- a/src/game-lib-controls-0.js +++ b/src/game-lib-controls-0.js @@ -35,10 +35,12 @@ GameLib.Controls = function ( break; case (GameLib.API.Controls.CONTROLS_TYPE_FIRST_PERSON) : linkedObjects.camera = GameLib.D3.Camera; + delayed = true; break; case (GameLib.API.Controls.CONTROLS_TYPE_ORBIT) : linkedObjects.camera = GameLib.D3.Camera; linkedObjects.target = GameLib.Component; + delayed = true; break; } diff --git a/src/game-lib-system-input.js b/src/game-lib-system-input.js index 0bbdc96..5831833 100644 --- a/src/game-lib-system-input.js +++ b/src/game-lib-system-input.js @@ -1,5 +1,6 @@ /** * System takes care of updating all the entities (based on their component data) + * @param graphics * @param apiSystem GameLib.API.System * @constructor */ @@ -235,43 +236,25 @@ GameLib.System.Input.prototype.start = function() { this.selectionModeChange.bind(this) ); + /** + * Normal Controls + */ this.touchControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_TOUCH); this.keyboardControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_KEYBOARD); this.mouseControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_MOUSE); + /** + * Edit Mode Controls + */ this.editorControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_EDITOR); this.orbitControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_ORBIT); this.firstPersonControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_FIRST_PERSON); - - /** - * If we have touch controls - inject them first so we can override editor controls if necessary - */ - this.touchControls.map( - function(touchControl){ - this.registerTouchControls(touchControl); - }.bind(this) - ); - - this.keyboardControls.map( - function(keyboardControl){ - this.registerKeyboardControls(keyboardControl); - }.bind(this) - ); - - this.mouseControls.map( - function(mouseControl){ - this.registerMouseControls(mouseControl); - }.bind(this) - ); - - if (this.editMode) { - this.registerEditorControls(); - } - + + this.setMode(); }; /** @@ -293,26 +276,26 @@ GameLib.System.Input.prototype.stop = function() { this.beforeRenderSubscription.remove(); - this.touchControls.map( - function(touchControl){ - this.deRegisterTouchControls(touchControl); - }.bind(this) - ); - - this.keyboardControls.map( - function(keyboardControl){ - this.deRegisterKeyboardControls(keyboardControl); - }.bind(this) - ); - - this.mouseControls.map( - function(mouseControl){ - this.deRegisterMouseControls(mouseControl); - }.bind(this) - ); - if (this.editMode) { - this.deRegisterEditorControls(); + this.deRegisterEditModeControls(); + } else { + this.touchControls.map( + function(touchControl){ + this.deRegisterTouchControl(touchControl); + }.bind(this) + ); + + this.keyboardControls.map( + function(keyboardControl){ + this.deRegisterKeyboardControl(keyboardControl); + }.bind(this) + ); + + this.mouseControls.map( + function(mouseControl){ + this.deRegisterMouseControl(mouseControl); + }.bind(this) + ); } this.editorControls = []; @@ -329,6 +312,30 @@ GameLib.System.Input.prototype.stop = function() { }; +GameLib.System.Input.prototype.setMode = function() { + + /** + * De-Register everything + */ + this.deRegisterNormalModeControls(); + + this.deRegisterEditModeControls(); + + /** + * Now register the required controls + */ + if (this.editMode) { + + this.registerEditModeControls(); + + } else { + + this.registerNormalModeControls(); + + } + +}; + /** * * @param data @@ -349,17 +356,32 @@ GameLib.System.Input.prototype.selectionModeChange = function(data) { GameLib.System.Input.prototype.beforeRender = function(data) { - this.firstPersonControls.map( - function(controls) { - controls.instance.update(data.delta); - } - ); + + if (this.editMode) { - this.orbitControls.map( - function(controls) { - controls.instance.update(data.delta); - } - ); + this.firstPersonControls.map( + function(control) { + if (control && control.instance) { + control.instance.update(data.delta); + } + } + ); + + this.orbitControls.map( + function (control) { + if (control && control.instance) { + control.instance.update(data.delta); + } + } + ); + } else { + + /** + * We don't update anything at the moment for normal controls + */ + + } + }; @@ -377,7 +399,7 @@ GameLib.System.Input.prototype.instanceCreated = function(data) { } this.touchControls.push(data.component); - this.registerTouchControls(data.component); + this.setMode(); } if (data.component instanceof GameLib.Controls.Keyboard) { @@ -388,7 +410,7 @@ GameLib.System.Input.prototype.instanceCreated = function(data) { } this.keyboardControls.push(data.component); - this.registerKeyboardControls(data.component); + this.setMode(); } if (data.component instanceof GameLib.Controls.Mouse) { @@ -399,37 +421,8 @@ GameLib.System.Input.prototype.instanceCreated = function(data) { } this.mouseControls.push(data.component); - this.registerMouseControls(data.component); + this.setMode(); } - - if (data.component instanceof GameLib.Controls.D3.FirstPerson) { - - if (this.firstPersonControls.indexOf(data.component) !== -1) { - console.warn('First Person Controls already registered'); - return; - } - - this.firstPersonControls.push(data.component); - - if (this.editMode) { - this.registerEditorControls(); - } - } - - if (data.component instanceof GameLib.Controls.D3.Orbit) { - - if (this.orbitControls.indexOf(data.component) !== -1) { - console.warn('Orbit Controls already registered'); - return; - } - - this.orbitControls.push(data.component); - - if (this.editMode) { - this.registerEditorControls(); - } - } - }; /** @@ -451,9 +444,11 @@ GameLib.System.Input.prototype.removeComponent = function(data) { console.log('removing touch controls from system'); - this.deRegisterTouchControls(data.component); + this.deRegisterTouchControl(data.component); this.touchControls.splice(index, 1); + + this.setMode(); } if (data.component instanceof GameLib.Controls.Keyboard) { @@ -467,9 +462,11 @@ GameLib.System.Input.prototype.removeComponent = function(data) { console.log('removing keyboard controls from system'); - this.deRegisterKeyboardControls(data.component); + this.deRegisterKeyboardControl(data.component); this.keyboardControls.splice(index, 1); + + this.setMode(); } if (data.component instanceof GameLib.Controls.Mouse) { @@ -483,13 +480,17 @@ GameLib.System.Input.prototype.removeComponent = function(data) { console.log('removing mouse controls from system'); - this.deRegisterMouseControls(data.component); + this.deRegisterMouseControl(data.component); this.mouseControls.splice(index, 1); + + this.setMode(); } if (data.component instanceof GameLib.Controls.D3.Editor) { + console.log('removing editor controls from system'); + index = this.editorControls.indexOf(data.component); if (index === -1) { @@ -497,15 +498,19 @@ GameLib.System.Input.prototype.removeComponent = function(data) { return; } - console.log('removing editor controls from system'); - - this.deRegisterEditorControls(data.component); + if (data.component.instance) { + data.component.instance.dispose(); + } this.editorControls.splice(index, 1); + + this.setMode(); } if (data.component instanceof GameLib.Controls.D3.FirstPerson) { + console.log('removing first person controls from system'); + index = this.firstPersonControls.indexOf(data.component); if (index === -1) { @@ -513,13 +518,19 @@ GameLib.System.Input.prototype.removeComponent = function(data) { return; } - console.log('removing first person controls from system'); - + if (data.component.instance) { + data.component.instance.dispose(); + } + this.firstPersonControls.splice(index, 1); + + this.setMode(); } if (data.component instanceof GameLib.Controls.D3.Orbit) { + console.log('removing orbit controls from system'); + index = this.orbitControls.indexOf(data.component); if (index === -1) { @@ -527,9 +538,13 @@ GameLib.System.Input.prototype.removeComponent = function(data) { return; } - console.log('removing orbit controls from system'); + if (data.component.instance) { + data.component.instance.dispose(); + } this.orbitControls.splice(index, 1); + + this.setMode(); } }; @@ -548,15 +563,35 @@ GameLib.System.Input.prototype.delayedInstanceEncountered = function(data) { } this.editorControls.push(data.component); + this.setMode(); + } - if (this.editMode) { - this.registerEditorControls(); + + if (data.component instanceof GameLib.Controls.D3.FirstPerson) { + + if (this.firstPersonControls.indexOf(data.component) !== -1) { + console.warn('First Person Controls already registered'); + return; } + + this.firstPersonControls.push(data.component); + this.setMode(); + } + + if (data.component instanceof GameLib.Controls.D3.Orbit) { + + if (this.orbitControls.indexOf(data.component) !== -1) { + console.warn('Orbit Controls already registered'); + return; + } + + this.orbitControls.push(data.component); + this.setMode(); } }; -GameLib.System.Input.prototype.registerTouchControls = function(touchControl) { +GameLib.System.Input.prototype.registerTouchControl = function(touchControl) { if (!touchControl.canvas || !touchControl.canvas.instance) { console.warn('no canvas at time of registration of touch controls - this part will be skipped'); @@ -586,7 +621,7 @@ GameLib.System.Input.prototype.registerTouchControls = function(touchControl) { ); }; -GameLib.System.Input.prototype.registerKeyboardControls = function(keyboardControl) { +GameLib.System.Input.prototype.registerKeyboardControl = function(keyboardControl) { if (!keyboardControl.canvas || !keyboardControl.canvas.instance) { console.warn('no canvas at time of registration of keyboard controls - this part will be skipped'); @@ -606,7 +641,7 @@ GameLib.System.Input.prototype.registerKeyboardControls = function(keyboardContr ); }; -GameLib.System.Input.prototype.registerMouseControls = function(mouseControl) { +GameLib.System.Input.prototype.registerMouseControl = function(mouseControl) { if (!mouseControl.canvas || !mouseControl.canvas.instance) { console.warn('no canvas at time of registration of mouse controls - this part will be skipped'); @@ -639,38 +674,45 @@ GameLib.System.Input.prototype.registerMouseControls = function(mouseControl) { }; /** - * TODO: change the input mode so that when in 'edit' mode - editor controls take effect only, otherwise the normal - * TODO: keyboard / mouse /touch controls are in effect - * @param editorControl + * Register all normal mode controls (Touch, Keyboard and Mouse) */ -GameLib.System.Input.prototype.registerEditorControls = function() { +GameLib.System.Input.prototype.registerNormalModeControls = function() { + + this.touchControls.map( + function(touchControl){ + this.registerTouchControl(touchControl); + }.bind(this) + ); + + this.keyboardControls.map( + function(keyboardControl){ + this.registerKeyboardControl(keyboardControl); + }.bind(this) + ); + + this.mouseControls.map( + function(mouseControl){ + this.registerMouseControl(mouseControl); + }.bind(this) + ); +}; + +/** + * Register all edit mode controls - (Editor, Firs Person and Orbit) + */ +GameLib.System.Input.prototype.registerEditModeControls = function() { /** - * In the 'Flammentraum' example, we need the mouse controls + * Right now - all edit mode controls happen to live in the namespace GameLib.Controls.D3 + * They are Editor, First Person and Orbit controls */ - // this.mouseControls.map( - // function(mouseControl) { - // if (mouseControl.canvas.instance === editorControl.canvas.instance) { - // this.deRegisterMouseControls(mouseControl); - // } - // }.bind(this) - // ); - - GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.Controls).map( + + GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.Controls.D3).map( + function(control) { - /** - * If we already have keyboard controls, we don't want to add another event listener onto the DOM - */ - this.keyboardControls.map( - function(keyboardControl) { - if (keyboardControl.canvas.instance === control.canvas.instance) { - this.deRegisterKeyboardControls(keyboardControl); - } - }.bind(this) - ); - + if (!control.canvas || !control.canvas.instance) { - console.warn('no canvas at time of registration of editor controls - are you sure you know what you are doing?'); + console.warn('no canvas at time of registration of controls - are you sure you know what you are doing?'); return; } @@ -707,6 +749,21 @@ GameLib.System.Input.prototype.registerEditorControls = function() { } ); + this.orbitControls.map( + function(orbitControl) { + orbitControl.createInstance(); + } + ); + + this.firstPersonControls.map( + function(firstPersonControl) { + firstPersonControl.createInstance(); + } + ); + + /** + * We append the wheel controls after so we can update our camera information + */ control.canvas.instance.addEventListener( 'wheel', this.mouseWheelEdit, @@ -718,59 +775,113 @@ GameLib.System.Input.prototype.registerEditorControls = function() { this.mouseUpEdit, true ); + + }.bind(this) + ); + +}; + +/** + * Remove all normal mode event listeners + */ +GameLib.System.Input.prototype.deRegisterNormalModeControls = function() { + this.touchControls.map( + function(touchControl){ + this.deRegisterTouchControl(touchControl); }.bind(this) ); + this.keyboardControls.map( + function(keyboardControl){ + this.deRegisterKeyboardControl(keyboardControl); + }.bind(this) + ); + this.mouseControls.map( + function(mouseControl){ + this.deRegisterMouseControl(mouseControl); + }.bind(this) + ); }; -GameLib.System.Input.prototype.deRegisterEditorControls = function(editorControl) { +GameLib.System.Input.prototype.deRegisterEditModeControls = function() { - editorControl.canvas.instance.removeEventListener( - 'mousedown', - this.mouseDownEdit, - true - ); - editorControl.canvas.instance.removeEventListener( - 'mousemove', - this.mouseMoveEdit, - true - ); + GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.Controls.D3).map( - editorControl.canvas.instance.removeEventListener( - 'keydown', - this.keyDownEdit, - true - ); + function(control) { - editorControl.canvas.instance.removeEventListener( - 'keyup', - this.keyUpEdit, - true - ); - - this.editorControls.map( - function(editorControl) { - editorControl.instance.dispose(); - } - ); - - editorControl.canvas.instance.removeEventListener( - 'wheel', - this.mouseWheelEdit, - true - ); + if (!control.canvas || !control.canvas.instance) { + console.warn('no canvas at time of registration of controls - are you sure you know what you are doing?'); + return; + } - editorControl.canvas.instance.removeEventListener( - 'mouseup', - this.mouseUpEdit, - true + control.canvas.instance.removeEventListener( + 'mousedown', + this.mouseDownEdit, + true + ); + + control.canvas.instance.removeEventListener( + 'mousemove', + this.mouseMoveEdit, + true + ); + + control.canvas.instance.removeEventListener( + 'keydown', + this.keyDownEdit, + true + ); + + control.canvas.instance.removeEventListener( + 'keyup', + this.keyUpEdit, + true + ); + + this.editorControls.map( + function(editorControl) { + if (editorControl.instance) { + editorControl.instance.dispose(); + } + } + ); + + this.orbitControls.map( + function(orbitControl) { + if (orbitControl.instance) { + orbitControl.instance.dispose(); + } + } + ); + + this.firstPersonControls.map( + function(firstPersonControl) { + if (firstPersonControl.instance) { + firstPersonControl.instance.dispose(); + } + } + ); + + control.canvas.instance.removeEventListener( + 'wheel', + this.mouseWheelEdit, + true + ); + + control.canvas.instance.removeEventListener( + 'mouseup', + this.mouseUpEdit, + true + ); + + }.bind(this) ); }; -GameLib.System.Input.prototype.deRegisterTouchControls = function(touchControl) { +GameLib.System.Input.prototype.deRegisterTouchControl = function(touchControl) { touchControl.canvas.instance.removeEventListener( 'touchstart', @@ -798,7 +909,7 @@ GameLib.System.Input.prototype.deRegisterTouchControls = function(touchControl) }; -GameLib.System.Input.prototype.deRegisterKeyboardControls = function(keyboardControl) { +GameLib.System.Input.prototype.deRegisterKeyboardControl = function(keyboardControl) { keyboardControl.canvas.instance.removeEventListener( 'keydown', @@ -815,7 +926,7 @@ GameLib.System.Input.prototype.deRegisterKeyboardControls = function(keyboardCon }; -GameLib.System.Input.prototype.deRegisterMouseControls = function(mouseControl) { +GameLib.System.Input.prototype.deRegisterMouseControl = function(mouseControl) { mouseControl.canvas.instance.removeEventListener( 'mousedown', @@ -1349,13 +1460,16 @@ GameLib.System.Input.prototype.onMouseUpEdit = function(event) { * @param event */ GameLib.System.Input.prototype.onMouseWheelEdit = function(event) { - this.editorControls.map( - function(editorControl) { - editorControl.camera.position.x = editorControl.camera.instance.position.x; - editorControl.camera.position.y = editorControl.camera.instance.position.y; - editorControl.camera.position.z = editorControl.camera.instance.position.z; + + GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.Controls.D3).map( + function(control) { + + control.camera.position.x = control.camera.instance.position.x; + control.camera.position.y = control.camera.instance.position.y; + control.camera.position.z = control.camera.instance.position.z; } ); + }; GameLib.System.Input.prototype.selectFace = function(mesh, face) {