really fix the input system

beta.r3js.org
-=yb4f310 2018-03-20 10:51:43 +01:00
parent 2f16212119
commit 368c3f785a
2 changed files with 288 additions and 172 deletions

View File

@ -35,10 +35,12 @@ GameLib.Controls = function (
break; break;
case (GameLib.API.Controls.CONTROLS_TYPE_FIRST_PERSON) : case (GameLib.API.Controls.CONTROLS_TYPE_FIRST_PERSON) :
linkedObjects.camera = GameLib.D3.Camera; linkedObjects.camera = GameLib.D3.Camera;
delayed = true;
break; break;
case (GameLib.API.Controls.CONTROLS_TYPE_ORBIT) : case (GameLib.API.Controls.CONTROLS_TYPE_ORBIT) :
linkedObjects.camera = GameLib.D3.Camera; linkedObjects.camera = GameLib.D3.Camera;
linkedObjects.target = GameLib.Component; linkedObjects.target = GameLib.Component;
delayed = true;
break; break;
} }

View File

@ -1,5 +1,6 @@
/** /**
* System takes care of updating all the entities (based on their component data) * System takes care of updating all the entities (based on their component data)
* @param graphics
* @param apiSystem GameLib.API.System * @param apiSystem GameLib.API.System
* @constructor * @constructor
*/ */
@ -235,43 +236,25 @@ GameLib.System.Input.prototype.start = function() {
this.selectionModeChange.bind(this) this.selectionModeChange.bind(this)
); );
/**
* Normal Controls
*/
this.touchControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_TOUCH); this.touchControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_TOUCH);
this.keyboardControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_KEYBOARD); this.keyboardControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_KEYBOARD);
this.mouseControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_MOUSE); this.mouseControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_MOUSE);
/**
* Edit Mode Controls
*/
this.editorControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_EDITOR); this.editorControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_EDITOR);
this.orbitControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_ORBIT); this.orbitControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_ORBIT);
this.firstPersonControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_FIRST_PERSON); this.firstPersonControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_FIRST_PERSON);
/** this.setMode();
* 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();
}
}; };
/** /**
@ -293,26 +276,26 @@ GameLib.System.Input.prototype.stop = function() {
this.beforeRenderSubscription.remove(); 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) { 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 = []; 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 * @param data
@ -349,17 +356,32 @@ GameLib.System.Input.prototype.selectionModeChange = function(data) {
GameLib.System.Input.prototype.beforeRender = function(data) { GameLib.System.Input.prototype.beforeRender = function(data) {
this.firstPersonControls.map(
function(controls) { if (this.editMode) {
controls.instance.update(data.delta);
}
);
this.orbitControls.map( this.firstPersonControls.map(
function(controls) { function(control) {
controls.instance.update(data.delta); 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.touchControls.push(data.component);
this.registerTouchControls(data.component); this.setMode();
} }
if (data.component instanceof GameLib.Controls.Keyboard) { if (data.component instanceof GameLib.Controls.Keyboard) {
@ -388,7 +410,7 @@ GameLib.System.Input.prototype.instanceCreated = function(data) {
} }
this.keyboardControls.push(data.component); this.keyboardControls.push(data.component);
this.registerKeyboardControls(data.component); this.setMode();
} }
if (data.component instanceof GameLib.Controls.Mouse) { if (data.component instanceof GameLib.Controls.Mouse) {
@ -399,37 +421,8 @@ GameLib.System.Input.prototype.instanceCreated = function(data) {
} }
this.mouseControls.push(data.component); 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'); console.log('removing touch controls from system');
this.deRegisterTouchControls(data.component); this.deRegisterTouchControl(data.component);
this.touchControls.splice(index, 1); this.touchControls.splice(index, 1);
this.setMode();
} }
if (data.component instanceof GameLib.Controls.Keyboard) { 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'); console.log('removing keyboard controls from system');
this.deRegisterKeyboardControls(data.component); this.deRegisterKeyboardControl(data.component);
this.keyboardControls.splice(index, 1); this.keyboardControls.splice(index, 1);
this.setMode();
} }
if (data.component instanceof GameLib.Controls.Mouse) { 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'); console.log('removing mouse controls from system');
this.deRegisterMouseControls(data.component); this.deRegisterMouseControl(data.component);
this.mouseControls.splice(index, 1); this.mouseControls.splice(index, 1);
this.setMode();
} }
if (data.component instanceof GameLib.Controls.D3.Editor) { if (data.component instanceof GameLib.Controls.D3.Editor) {
console.log('removing editor controls from system');
index = this.editorControls.indexOf(data.component); index = this.editorControls.indexOf(data.component);
if (index === -1) { if (index === -1) {
@ -497,15 +498,19 @@ GameLib.System.Input.prototype.removeComponent = function(data) {
return; return;
} }
console.log('removing editor controls from system'); if (data.component.instance) {
data.component.instance.dispose();
this.deRegisterEditorControls(data.component); }
this.editorControls.splice(index, 1); this.editorControls.splice(index, 1);
this.setMode();
} }
if (data.component instanceof GameLib.Controls.D3.FirstPerson) { if (data.component instanceof GameLib.Controls.D3.FirstPerson) {
console.log('removing first person controls from system');
index = this.firstPersonControls.indexOf(data.component); index = this.firstPersonControls.indexOf(data.component);
if (index === -1) { if (index === -1) {
@ -513,13 +518,19 @@ GameLib.System.Input.prototype.removeComponent = function(data) {
return; return;
} }
console.log('removing first person controls from system'); if (data.component.instance) {
data.component.instance.dispose();
}
this.firstPersonControls.splice(index, 1); this.firstPersonControls.splice(index, 1);
this.setMode();
} }
if (data.component instanceof GameLib.Controls.D3.Orbit) { if (data.component instanceof GameLib.Controls.D3.Orbit) {
console.log('removing orbit controls from system');
index = this.orbitControls.indexOf(data.component); index = this.orbitControls.indexOf(data.component);
if (index === -1) { if (index === -1) {
@ -527,9 +538,13 @@ GameLib.System.Input.prototype.removeComponent = function(data) {
return; return;
} }
console.log('removing orbit controls from system'); if (data.component.instance) {
data.component.instance.dispose();
}
this.orbitControls.splice(index, 1); this.orbitControls.splice(index, 1);
this.setMode();
} }
}; };
@ -548,15 +563,35 @@ GameLib.System.Input.prototype.delayedInstanceEncountered = function(data) {
} }
this.editorControls.push(data.component); 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) { if (!touchControl.canvas || !touchControl.canvas.instance) {
console.warn('no canvas at time of registration of touch controls - this part will be skipped'); 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) { if (!keyboardControl.canvas || !keyboardControl.canvas.instance) {
console.warn('no canvas at time of registration of keyboard controls - this part will be skipped'); 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) { if (!mouseControl.canvas || !mouseControl.canvas.instance) {
console.warn('no canvas at time of registration of mouse controls - this part will be skipped'); 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 * Register all normal mode controls (Touch, Keyboard and Mouse)
* TODO: keyboard / mouse /touch controls are in effect
* @param editorControl
*/ */
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) { GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.Controls.D3).map(
// if (mouseControl.canvas.instance === editorControl.canvas.instance) {
// this.deRegisterMouseControls(mouseControl);
// }
// }.bind(this)
// );
GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.Controls).map(
function(control) { 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) { 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; 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( control.canvas.instance.addEventListener(
'wheel', 'wheel',
this.mouseWheelEdit, this.mouseWheelEdit,
@ -718,59 +775,113 @@ GameLib.System.Input.prototype.registerEditorControls = function() {
this.mouseUpEdit, this.mouseUpEdit,
true 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) }.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( GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.Controls.D3).map(
'mousemove',
this.mouseMoveEdit,
true
);
editorControl.canvas.instance.removeEventListener( function(control) {
'keydown',
this.keyDownEdit,
true
);
editorControl.canvas.instance.removeEventListener( if (!control.canvas || !control.canvas.instance) {
'keyup', console.warn('no canvas at time of registration of controls - are you sure you know what you are doing?');
this.keyUpEdit, return;
true }
);
this.editorControls.map(
function(editorControl) {
editorControl.instance.dispose();
}
);
editorControl.canvas.instance.removeEventListener(
'wheel',
this.mouseWheelEdit,
true
);
editorControl.canvas.instance.removeEventListener( control.canvas.instance.removeEventListener(
'mouseup', 'mousedown',
this.mouseUpEdit, this.mouseDownEdit,
true 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( touchControl.canvas.instance.removeEventListener(
'touchstart', '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( keyboardControl.canvas.instance.removeEventListener(
'keydown', '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( mouseControl.canvas.instance.removeEventListener(
'mousedown', 'mousedown',
@ -1349,13 +1460,16 @@ GameLib.System.Input.prototype.onMouseUpEdit = function(event) {
* @param event * @param event
*/ */
GameLib.System.Input.prototype.onMouseWheelEdit = function(event) { GameLib.System.Input.prototype.onMouseWheelEdit = function(event) {
this.editorControls.map(
function(editorControl) { GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.Controls.D3).map(
editorControl.camera.position.x = editorControl.camera.instance.position.x; function(control) {
editorControl.camera.position.y = editorControl.camera.instance.position.y;
editorControl.camera.position.z = editorControl.camera.instance.position.z; 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) { GameLib.System.Input.prototype.selectFace = function(mesh, face) {