r3-legacy/src/game-lib-system-input.js

1263 lines
31 KiB
JavaScript
Raw Normal View History

2017-06-19 15:54:02 +02:00
/**
* System takes care of updating all the entities (based on their component data)
* @param apiSystem GameLib.API.System
* @constructor
*/
GameLib.System.Input = function(
apiSystem
2017-06-19 15:54:02 +02:00
) {
GameLib.System.call(
this,
apiSystem
);
2017-08-24 22:20:40 +02:00
this.selectAll = false;
this.controlLeft = false;
2017-09-29 06:55:42 +02:00
this.sensitivityCounter = 0;
this.editorControls = [];
this.touchControls = [];
this.keyboardControls = [];
this.mouseControls = [];
2017-08-24 22:20:40 +02:00
/**
* Touch Controls
* @type {null}
*/
2018-01-09 10:56:41 +01:00
this.touchStart = this.onTouchStart.bind(this);
this.touchMove = this.onTouchMove.bind(this);
this.touchEnd = this.onTouchEnd.bind(this);
this.touchCancel = this.onTouchCancel.bind(this);
2017-09-27 21:44:25 +02:00
/**
* Keyboard Controls
* @type {null}
*/
2018-01-09 10:56:41 +01:00
this.keyboardKeyUp = this.onKeyboardKeyUp.bind(this);
this.keyboardKeyDown = this.onKeyboardKeyDown.bind(this);
2017-09-29 06:09:47 +02:00
/**
* Mouse Controls
* @type {null}
*/
2018-01-09 10:56:41 +01:00
this.mouseDown = this.onMouseDown.bind(this);
this.mouseMove = this.onMouseMove.bind(this);
this.mouseWheel = this.onMouseWheel.bind(this);
this.mouseUp = this.onMouseUp.bind(this);
2017-10-30 06:47:40 +01:00
/**
* Editor Controls
* @type {null}
*/
2018-01-09 10:56:41 +01:00
this.keyDown = this.onKeyDown.bind(this);
this.keyUp = this.onKeyUp.bind(this);
this.mouseDownEdit = this.onMouseDownEdit.bind(this);
this.mouseMoveEdit = this.onMouseMoveEdit.bind(this);
this.mouseWheelEdit = this.onMouseWheelEdit.bind(this);
this.mouseUpEdit = this.onMouseUpEdit.bind(this);
2017-10-27 19:50:18 +02:00
this.delayedInstanceEncounteredSubscription = null;
this.instanceCreatedSubscription = null;
this.removeComponentSubscription = null;
2017-10-27 19:50:18 +02:00
this.mouse = new GameLib.Mouse();
2017-06-19 15:54:02 +02:00
};
GameLib.System.Input.prototype = Object.create(GameLib.System.prototype);
GameLib.System.Input.prototype.constructor = GameLib.System.Input;
2017-08-24 22:20:40 +02:00
/**
*
*/
2017-06-19 15:54:02 +02:00
GameLib.System.Input.prototype.start = function() {
GameLib.System.prototype.start.call(this);
this.instanceCreatedSubscription = GameLib.Event.Subscribe(
GameLib.Event.INSTANCE_CREATED,
this.instanceCreated.bind(this)
);
this.removeComponentSubscription = GameLib.Event.Subscribe(
GameLib.Event.REMOVE_COMPONENT,
this.removeComponent.bind(this)
);
2017-10-27 19:50:18 +02:00
this.delayedInstanceEncounteredSubscription = GameLib.Event.Subscribe(
GameLib.Event.DELAYED_INSTANCE_ENCOUNTERED,
this.delayedInstanceEncountered.bind(this)
2017-10-27 19:50:18 +02:00
);
2018-01-09 10:56:41 +01:00
this.editorControls = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CONTROLS_EDITOR);
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);
/**
2017-09-28 12:03:59 +02:00
* If we have touch controls - inject them first so we can override editor controls if necessary
*/
2018-01-09 10:56:41 +01:00
this.touchControls.map(
function(touchControl){
this.registerTouchControls(touchControl);
}.bind(this)
);
2017-09-27 21:44:25 +02:00
2018-01-09 10:56:41 +01:00
this.keyboardControls.map(
function(keyboardControl){
this.registerKeyboardControls(keyboardControl);
}.bind(this)
);
this.mouseControls.map(
function(mouseControl){
this.registerMouseControls(mouseControl);
}.bind(this)
);
this.editorControls.map(
function(editorControl){
this.registerEditorControls(editorControl);
}.bind(this)
);
};
/**
*
*/
GameLib.System.Input.prototype.stop = function() {
GameLib.System.prototype.stop.call(this);
this.instanceCreatedSubscription.remove();
this.removeComponentSubscription.remove();
this.delayedInstanceEncounteredSubscription.remove();
2018-01-09 10:56:41 +01:00
this.touchControls.map(
function(touchControl){
this.deRegisterTouchControls(touchControl);
}.bind(this)
);
2018-01-09 10:56:41 +01:00
this.keyboardControls.map(
function(keyboardControl){
this.deRegisterKeyboardControls(keyboardControl);
}.bind(this)
);
2018-01-09 10:56:41 +01:00
this.mouseControls.map(
function(mouseControl){
this.deRegisterMouseControls(mouseControl);
}.bind(this)
);
2017-09-29 07:47:12 +02:00
2018-01-09 10:56:41 +01:00
this.editorControls.map(
function(editorControl){
this.deRegisterEditorControls(editorControl);
}.bind(this)
);
this.editorControls = [];
this.touchControls = [];
this.keyboardControls = [];
this.mouseControls = [];
};
/**
* From now on we want to track everything about a component, only from the systems that are active
* @param data
*/
GameLib.System.Input.prototype.instanceCreated = function(data) {
2017-12-02 12:01:18 +01:00
if (data.component instanceof GameLib.Controls.Touch) {
2018-01-09 10:56:41 +01:00
if (this.touchControls.indexOf(data.component) !== -1) {
console.warn('Touch controls already registered');
return;
2017-09-29 07:47:12 +02:00
}
2018-01-09 10:56:41 +01:00
this.touchControls.push(data.component);
this.registerTouchControls(data.component);
}
2017-12-02 12:01:18 +01:00
if (data.component instanceof GameLib.Controls.Keyboard) {
2018-01-09 10:56:41 +01:00
if (this.keyboardControls.indexOf(data.component) !== -1) {
console.warn('Keyboard controls already registered');
return;
}
2018-01-09 10:56:41 +01:00
this.keyboardControls.push(data.component);
this.registerKeyboardControls(data.component);
}
2017-09-29 07:47:12 +02:00
2017-12-02 12:01:18 +01:00
if (data.component instanceof GameLib.Controls.Mouse) {
2018-01-09 10:56:41 +01:00
if (this.mouseControls.indexOf(data.component) !== -1) {
console.warn('Mouse controls already registered');
return;
}
2018-01-09 10:56:41 +01:00
this.mouseControls.push(data.component);
this.registerMouseControls(data.component);
}
};
/**
2018-01-09 10:56:41 +01:00
* Removes controls from this system
* @param data
*/
GameLib.System.Input.prototype.removeComponent = function(data) {
2018-01-09 10:56:41 +01:00
var index;
2017-12-04 13:23:15 +01:00
if (data.component instanceof GameLib.Controls.D3.Editor) {
2018-01-09 10:56:41 +01:00
index = this.editorControls.indexOf(data.component);
2018-01-09 10:56:41 +01:00
if (index === -1) {
console.warn('Failed to find the editor controls in the system - probably it was ignored - ' + data.component.name);
return;
}
2018-01-09 10:56:41 +01:00
console.log('removing editor controls from system');
2018-01-09 10:56:41 +01:00
this.deRegisterEditorControls(data.component);
2018-01-09 10:56:41 +01:00
this.editorControls.splice(index, 1);
}
2018-01-09 10:56:41 +01:00
if (data.component instanceof GameLib.Controls.Touch) {
index = this.touchControls.indexOf(data.component);
if (index === -1) {
console.warn('Failed to find the touch controls in the system - probably it was ignored - ' + data.component.name);
return;
}
2018-01-09 10:56:41 +01:00
console.log('removing touch controls from system');
this.deRegisterTouchControls(data.component);
this.touchControls.splice(index, 1);
}
if (data.component instanceof GameLib.Controls.Keyboard) {
index = this.keyboardControls.indexOf(data.component);
if (index === -1) {
console.warn('Failed to find the keyboard controls in the system - probably it was ignored - ' + data.component.name);
return;
}
console.log('removing keyboard controls from system');
this.deRegisterKeyboardControls(data.component);
this.keyboardControls.splice(index, 1);
}
if (data.component instanceof GameLib.Controls.Mouse) {
index = this.mouseControls.indexOf(data.component);
if (index === -1) {
console.warn('Failed to find the mouse controls in the system - probably it was ignored - ' + data.component.name);
return;
}
console.log('removing mouse controls from system');
this.deRegisterMouseControls(data.component);
this.mouseControls.splice(index, 1);
}
};
/**
* Delayed Instance - we need to check if editControls will block the loading process (since only one will be created)
* @param data
*/
GameLib.System.Input.prototype.delayedInstanceEncountered = function(data) {
2017-12-04 13:23:15 +01:00
if (data.component instanceof GameLib.Controls.D3.Editor) {
2018-01-09 10:56:41 +01:00
if (this.editorControls.indexOf(data.component) !== -1) {
console.warn('Editor controls already registered');
return;
}
2018-01-09 10:56:41 +01:00
this.editorControls.push(data.component);
2018-01-09 10:56:41 +01:00
this.registerEditorControls(data.component);
}
2018-01-09 10:56:41 +01:00
};
2018-01-09 10:56:41 +01:00
GameLib.System.Input.prototype.registerTouchControls = function(touchControl) {
touchControl.domElement.instance.addEventListener(
'touchstart',
this.touchStart,
2018-01-11 14:33:32 +01:00
true
);
touchControl.domElement.instance.addEventListener(
'touchmove',
this.touchMove,
2018-01-11 14:33:32 +01:00
true
);
touchControl.domElement.instance.addEventListener(
'touchend',
this.touchEnd,
2018-01-11 14:33:32 +01:00
true
);
touchControl.domElement.instance.addEventListener(
'touchcancel',
this.touchCancel,
2018-01-11 14:33:32 +01:00
true
);
};
2018-01-09 10:56:41 +01:00
GameLib.System.Input.prototype.registerKeyboardControls = function(keyboardControl) {
keyboardControl.domElement.instance.addEventListener(
'keyup',
this.keyboardKeyUp,
2018-01-11 14:33:32 +01:00
true
);
keyboardControl.domElement.instance.addEventListener(
'keydown',
this.keyboardKeyDown,
2018-01-11 14:33:32 +01:00
true
);
};
2018-01-09 10:56:41 +01:00
GameLib.System.Input.prototype.registerMouseControls = function(mouseControl) {
mouseControl.domElement.instance.addEventListener(
'mousedown',
this.mouseDown,
2018-01-11 14:33:32 +01:00
true
);
mouseControl.domElement.instance.addEventListener(
'mousemove',
this.mouseMove,
2018-01-11 14:33:32 +01:00
true
);
mouseControl.domElement.instance.addEventListener(
'wheel',
this.mouseWheel,
2018-01-11 14:33:32 +01:00
true
);
mouseControl.domElement.instance.addEventListener(
'mouseup',
this.mouseUp,
2018-01-11 14:33:32 +01:00
true
);
};
2018-01-09 10:56:41 +01:00
GameLib.System.Input.prototype.registerEditorControls = function(editorControl) {
/**
* If we already have mouse controls, we don't want to add another event listener onto the DOM
*/
2018-01-09 10:56:41 +01:00
this.mouseControls.map(
function(mouseControl) {
if (mouseControl.domElement.instance === editorControl.domElement.instance) {
this.deRegisterMouseControls(mouseControl);
}
}.bind(this)
);
/**
* 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.domElement.instance === editorControl.domElement.instance) {
this.deRegisterKeyboardControls(keyboardControl);
}
}.bind(this)
);
editorControl.domElement.instance.addEventListener(
'mousedown',
this.mouseDownEdit,
2018-01-11 14:33:32 +01:00
true
);
2017-09-29 07:47:12 +02:00
editorControl.domElement.instance.addEventListener(
'mousemove',
this.mouseMoveEdit,
2018-01-11 14:33:32 +01:00
true
);
2017-09-29 07:47:12 +02:00
2018-01-09 10:56:41 +01:00
editorControl.domElement.instance.addEventListener(
'keydown',
this.keyDown,
2018-01-11 14:33:32 +01:00
true
2018-01-09 10:56:41 +01:00
);
2018-01-09 10:56:41 +01:00
editorControl.domElement.instance.addEventListener(
'keyup',
this.keyUp,
2018-01-11 14:33:32 +01:00
true
2018-01-09 10:56:41 +01:00
);
2018-01-09 10:56:41 +01:00
/**
* The order of creation is important here - it changes the way the DOM reacts to events
*/
editorControl.createInstance();
editorControl.domElement.instance.addEventListener(
'wheel',
this.mouseWheelEdit,
2018-01-11 14:33:32 +01:00
true
);
editorControl.domElement.instance.addEventListener(
'mouseup',
this.mouseUpEdit,
2018-01-11 14:33:32 +01:00
true
);
};
2018-01-09 10:56:41 +01:00
GameLib.System.Input.prototype.deRegisterEditorControls = function(editorControl) {
editorControl.domElement.instance.removeEventListener(
'mousedown',
this.mouseDownEdit,
2018-01-11 14:33:32 +01:00
true
);
editorControl.domElement.instance.removeEventListener(
'mousemove',
this.mouseMoveEdit,
2018-01-11 14:33:32 +01:00
true
);
2018-01-09 10:56:41 +01:00
editorControl.domElement.instance.removeEventListener(
'keydown',
this.keyDown,
2018-01-11 14:33:32 +01:00
true
2018-01-09 10:56:41 +01:00
);
2018-01-09 10:56:41 +01:00
editorControl.domElement.instance.removeEventListener(
'keyup',
this.keyUp,
2018-01-11 14:33:32 +01:00
true
2018-01-09 10:56:41 +01:00
);
2017-09-29 06:09:47 +02:00
editorControl.instance.dispose();
2017-09-29 06:09:47 +02:00
editorControl.domElement.instance.removeEventListener(
'wheel',
this.mouseWheelEdit,
2018-01-11 14:33:32 +01:00
true
);
2017-10-30 06:47:40 +01:00
editorControl.domElement.instance.removeEventListener(
'mouseup',
this.mouseUpEdit,
2018-01-11 14:33:32 +01:00
true
);
2017-10-30 06:47:40 +01:00
};
2017-10-30 06:47:40 +01:00
2018-01-09 10:56:41 +01:00
GameLib.System.Input.prototype.deRegisterTouchControls = function(touchControl) {
touchControl.domElement.instance.removeEventListener(
'touchstart',
this.touchStart,
2018-01-11 14:33:32 +01:00
true
);
touchControl.domElement.instance.removeEventListener(
'touchmove',
this.touchMove,
2018-01-11 14:33:32 +01:00
true
);
2017-10-29 16:28:13 +01:00
touchControl.domElement.instance.removeEventListener(
'touchend',
this.touchEnd,
2018-01-11 14:33:32 +01:00
true
);
2017-10-29 16:28:13 +01:00
touchControl.domElement.instance.removeEventListener(
'touchcancel',
this.touchCancel,
2018-01-11 14:33:32 +01:00
true
);
2017-10-29 16:28:13 +01:00
};
2018-01-09 10:56:41 +01:00
GameLib.System.Input.prototype.deRegisterKeyboardControls = function(keyboardControl) {
2017-10-30 06:47:40 +01:00
keyboardControl.domElement.instance.removeEventListener(
'keydown',
this.keyboardKeyDown,
2018-01-11 14:33:32 +01:00
true
);
keyboardControl.domElement.instance.removeEventListener(
'keyup',
this.keyboardKeyUp,
2018-01-11 14:33:32 +01:00
true
);
2017-10-30 06:47:40 +01:00
};
2018-01-09 10:56:41 +01:00
GameLib.System.Input.prototype.deRegisterMouseControls = function(mouseControl) {
mouseControl.domElement.instance.removeEventListener(
'mousedown',
this.mouseDown,
2018-01-11 14:33:32 +01:00
true
);
mouseControl.domElement.instance.removeEventListener(
'mousemove',
this.mouseMove,
2018-01-11 14:33:32 +01:00
true
);
mouseControl.domElement.instance.removeEventListener(
'wheel',
this.mouseWheel,
2018-01-11 14:33:32 +01:00
true
);
mouseControl.domElement.instance.removeEventListener(
'mouseup',
this.mouseUp,
2018-01-11 14:33:32 +01:00
true
);
2017-09-29 06:09:47 +02:00
};
2017-09-27 21:44:25 +02:00
2017-09-29 06:09:47 +02:00
GameLib.System.Input.prototype.onKeyboardKeyUp = function(event) {
GameLib.Event.Emit(
GameLib.Event.KEY_DOWN,
{
code : event.code
}
);
};
GameLib.System.Input.prototype.onKeyboardKeyDown = function(event) {
GameLib.Event.Emit(
GameLib.Event.KEY_UP,
{
code : event.code
}
);
2017-09-27 21:44:25 +02:00
};
2017-09-27 21:44:25 +02:00
GameLib.System.Input.prototype.onTouchStart = function(event) {
2017-09-29 06:55:42 +02:00
this.sensitivityCounter = 0;
2017-09-27 21:44:25 +02:00
this.touches = {};
for (var t = 0; t < event.touches.length; t++) {
this.touches[event.touches[t].identifier] = {
left : 0,
right : 0,
up : 0,
down : 0,
2017-11-18 14:13:43 +01:00
lastTouchX : event.touches[t].pageX,
lastTouchY : event.touches[t].pageY,
2017-09-27 21:44:25 +02:00
pageX : event.touches[t].pageX,
pageY : event.touches[t].pageY,
cancelled : false,
2017-09-29 06:26:40 +02:00
ended : false
2017-09-27 21:44:25 +02:00
};
}
2017-09-29 06:26:40 +02:00
this.touches.event = event;
2017-09-27 21:44:25 +02:00
GameLib.Event.Emit(
GameLib.Event.TOUCH_START,
this.touches
)
};
2017-09-29 07:47:12 +02:00
GameLib.System.Input.prototype.onTouchMove = function (event) {
2017-09-29 07:29:41 +02:00
2017-09-29 07:47:12 +02:00
this.sensitivityCounter++;
2017-09-27 21:44:25 +02:00
2017-09-29 08:08:41 +02:00
var id = null;
2017-09-29 07:29:41 +02:00
2017-11-19 14:26:32 +01:00
var leftTouch = null;
var rightTouch = null;
2017-11-19 14:52:34 +01:00
var bottomTouch = null;
var topTouch = null;
var inward = false;
var outward = false;
var pinch = false;
var zoom = false;
2017-09-29 06:55:42 +02:00
2017-11-19 14:26:32 +01:00
for (var t = 0; t < event.changedTouches.length; t++) {
2017-09-29 06:55:42 +02:00
2017-11-19 14:26:32 +01:00
id = event.changedTouches[t].identifier;
2017-09-29 06:55:42 +02:00
2017-11-19 14:26:32 +01:00
if (this.touches[id]) {
2017-09-27 21:44:25 +02:00
2017-11-19 14:26:32 +01:00
var diffX = Math.abs(this.touches[id].lastTouchX - event.changedTouches[t].pageX);
var diffY = Math.abs(this.touches[id].lastTouchY - event.changedTouches[t].pageY);
2017-09-27 21:44:25 +02:00
2017-11-19 14:26:32 +01:00
var left = 0;
var right = 0;
var up = 0;
var down = 0;
2017-11-18 14:13:43 +01:00
2017-11-19 14:26:32 +01:00
if (this.touches[id].lastTouchX < event.changedTouches[t].pageX) {
right += diffX;
}
2017-11-18 14:13:43 +01:00
2017-11-19 14:26:32 +01:00
if (this.touches[id].lastTouchX > event.changedTouches[t].pageX) {
left += diffX;
2017-09-29 08:08:41 +02:00
}
2017-11-19 14:26:32 +01:00
if (this.touches[id].lastTouchY > event.changedTouches[t].pageY) {
up += diffY;
}
2017-11-18 14:13:43 +01:00
2017-11-19 14:26:32 +01:00
if (this.touches[id].lastTouchY < event.changedTouches[t].pageY) {
down += diffY;
}
2017-09-29 09:50:14 +02:00
2017-11-19 14:26:32 +01:00
this.touches[id].right = right;
this.touches[id].left = left;
this.touches[id].up = up;
this.touches[id].down = down;
this.touches[id].lastTouchX = event.changedTouches[t].pageX;
this.touches[id].lastTouchY = event.changedTouches[t].pageY;
this.touches[id].pageX = event.changedTouches[t].pageX;
this.touches[id].pageY = event.changedTouches[t].pageY;
2017-09-29 08:08:41 +02:00
}
2017-11-19 14:52:34 +01:00
}
2017-11-19 19:43:53 +01:00
if (event.changedTouches.length === 2) {
if (event.changedTouches[0].pageX < event.changedTouches[1].pageX) {
leftTouch = this.touches[event.changedTouches[0].identifier];
rightTouch = this.touches[event.changedTouches[1].identifier];
} else {
leftTouch = this.touches[event.changedTouches[1].identifier];
rightTouch = this.touches[event.changedTouches[0].identifier];
}
if (event.changedTouches[0].pageY < event.changedTouches[1].pageY) {
bottomTouch = this.touches[event.changedTouches[1].identifier];
topTouch = this.touches[event.changedTouches[0].identifier];
2017-11-19 19:45:39 +01:00
} else {
bottomTouch = this.touches[event.changedTouches[0].identifier];
topTouch = this.touches[event.changedTouches[1].identifier];
2017-11-19 19:43:53 +01:00
}
}
2017-09-29 06:55:42 +02:00
2017-11-19 14:52:34 +01:00
if (leftTouch && leftTouch.left && rightTouch && rightTouch.right) {
outward = true;
}
2017-11-19 14:26:32 +01:00
2017-11-19 14:52:34 +01:00
if (leftTouch && leftTouch.right && rightTouch && rightTouch.left) {
inward = true;
}
2017-11-19 14:26:32 +01:00
2017-11-19 14:52:34 +01:00
if (bottomTouch && bottomTouch.up && topTouch && topTouch.down) {
pinch = true;
}
if (bottomTouch && bottomTouch.down && topTouch && topTouch.up) {
zoom = true;
2017-11-19 14:26:32 +01:00
}
this.touches.event = event;
2017-09-29 06:26:40 +02:00
2017-11-19 14:52:34 +01:00
this.touches.meta = {
inward : inward,
outward : outward,
pinch : pinch,
zoom : zoom
};
2018-01-09 10:56:41 +01:00
// if (this.sensitivityCounter >= this.touchSensitivity) {
//
// this.sensitivityCounter = 0;
2017-09-29 09:50:14 +02:00
2017-09-29 08:08:41 +02:00
GameLib.Event.Emit(
GameLib.Event.TOUCH_MOVE,
this.touches
);
2017-09-29 09:50:14 +02:00
2018-01-09 10:56:41 +01:00
// }
2017-09-29 09:50:14 +02:00
2017-09-27 21:44:25 +02:00
};
2017-09-27 21:44:25 +02:00
GameLib.System.Input.prototype.onTouchCancel = function(event) {
2017-09-29 07:29:41 +02:00
this.sensitivityCounter = 0;
2017-09-27 21:44:25 +02:00
for (var t = 0; t < event.changedTouches.length; t++) {
this.touches[event.changedTouches[t].identifier].cancelled = true;
2017-09-28 12:03:59 +02:00
this.touches[event.changedTouches[t].identifier].event = event;
2017-09-27 21:44:25 +02:00
GameLib.Event.Emit(
GameLib.Event.TOUCH_CANCEL,
this.touches[event.changedTouches[t].identifier]
);
delete this.touches[event.changedTouches[t].identifier];
}
2017-08-24 22:20:40 +02:00
};
2017-09-27 21:44:25 +02:00
GameLib.System.Input.prototype.onTouchEnd = function(event) {
2017-09-29 07:29:41 +02:00
this.sensitivityCounter = 0;
2017-09-27 21:44:25 +02:00
for (var t = 0; t < event.changedTouches.length; t++) {
this.touches[event.changedTouches[t].identifier].ended = true;
2017-09-28 12:03:59 +02:00
this.touches[event.changedTouches[t].identifier].event = event;
2017-09-27 21:44:25 +02:00
GameLib.Event.Emit(
GameLib.Event.TOUCH_END,
this.touches[event.changedTouches[t].identifier]
);
delete this.touches[event.changedTouches[t].identifier];
}
};
2017-08-24 22:20:40 +02:00
GameLib.System.Input.prototype.onKeyDown = function(event) {
console.log('input system emitted keypress ' + event.code);
GameLib.Event.Emit(
GameLib.Event.KEY_DOWN,
{
code : event.code
}
);
var meshes = null;
if (event.code === 'Delete') {
2017-12-04 14:10:12 +01:00
meshes = GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.D3.Mesh);
2017-08-24 22:20:40 +02:00
var deletedMeshes = [];
meshes.map(
function(mesh) {
if (mesh.selected) {
deletedMeshes.push(mesh);
mesh.removeHelper();
var scene = mesh.parentScene;
scene.removeObject(mesh);
scene.buildIdToObject();
}
}.bind(this)
);
GameLib.Event.Emit(
2017-10-27 15:03:16 +02:00
GameLib.Event.REMOVE_MESH,
2017-08-24 22:20:40 +02:00
{
meshes : deletedMeshes
}
);
}
if (event.code === 'ControlLeft') {
this.controlLeft = true;
}
if (event.code === 'KeyA') {
this.selectAll = !this.selectAll;
2017-12-04 14:10:12 +01:00
meshes = GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.D3.Mesh);
2017-08-24 22:20:40 +02:00
meshes.map(function(mesh){
if (this.selectAll) {
this.selectMesh(mesh);
} else {
this.deSelectMesh(mesh);
}
}.bind(this));
GameLib.Event.Emit(
GameLib.Event.BUILD_GUI,
null
)
}
2017-11-15 14:25:37 +01:00
if (event.code === 'KeyP') {
2017-11-21 15:07:04 +01:00
GameLib.Event.Emit(GameLib.Event.GAME_PAUSE);
2017-11-15 14:25:37 +01:00
}
2017-08-24 22:20:40 +02:00
};
GameLib.System.Input.prototype.onKeyUp = function(event) {
GameLib.Event.Emit(
GameLib.Event.KEY_UP,
{
code : event.code
}
);
if (event.code === 'ControlLeft') {
this.controlLeft = false;
}
};
GameLib.System.Input.prototype.onMouseDown = function(event) {
2017-10-30 06:47:40 +01:00
GameLib.Event.Emit(
GameLib.Event.MOUSE_DOWN,
{
event : event
}
)
};
GameLib.System.Input.prototype.onMouseMove = function(event) {
2018-01-09 10:56:41 +01:00
2017-10-30 06:47:40 +01:00
GameLib.Event.Emit(
GameLib.Event.MOUSE_MOVE,
{
event : event
}
)
};
GameLib.System.Input.prototype.onMouseWheel = function(event) {
2018-01-09 10:56:41 +01:00
2017-10-30 06:47:40 +01:00
GameLib.Event.Emit(
GameLib.Event.MOUSE_WHEEL,
{
event : event
}
)
};
GameLib.System.Input.prototype.onMouseUp = function(event) {
2018-01-09 10:56:41 +01:00
2017-10-30 06:47:40 +01:00
GameLib.Event.Emit(
GameLib.Event.MOUSE_UP,
{
event : event
}
)
};
GameLib.System.Input.prototype.onMouseDownEdit = function(event) {
2017-08-24 22:20:40 +02:00
if (event.button === 2) {
2017-08-24 22:20:40 +02:00
this.editorControls.map(
2017-08-24 22:20:40 +02:00
function(editorControl) {
2017-08-24 22:20:40 +02:00
if (this.controlLeft) {
return;
}
2017-08-24 22:20:40 +02:00
this.mouse.x = (event.offsetX / event.target.width ) * 2 - 1;
this.mouse.y = -(event.offsetY / event.target.height) * 2 + 1;
2017-08-24 22:20:40 +02:00
2017-12-04 13:23:15 +01:00
var scenes = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.SCENE);
2017-08-24 22:20:40 +02:00
var intersects = scenes.reduce(
2017-08-24 22:20:40 +02:00
function (result, scene) {
2017-08-24 22:20:40 +02:00
editorControl.raycaster.instance.setFromCamera(
this.mouse,
editorControl.camera.instance
);
2017-08-24 22:20:40 +02:00
intersects = editorControl.raycaster.getIntersectedObjects(scene.meshes);
2017-08-24 22:20:40 +02:00
intersects.map(function (intersect) {
result.push(intersect);
});
2017-08-24 22:20:40 +02:00
return result;
}.bind(this),
[]
);
2017-08-24 22:20:40 +02:00
intersects.sort(
function (a, b) {
if (a.distance < b.distance) {
return -1;
}
2017-08-24 22:20:40 +02:00
if (a.distance > b.distance) {
return 1;
}
2017-08-24 22:20:40 +02:00
return 0;
}
);
2017-08-24 22:20:40 +02:00
var meshes = intersects.map(function (intersect) {
return intersect.mesh;
});
2017-08-24 22:20:40 +02:00
var mesh = meshes[0];
2017-08-24 22:20:40 +02:00
if (mesh) {
2017-08-24 22:20:40 +02:00
/**
* Prevent default action (like context menu or whatever)
*/
event.preventDefault();
2017-08-24 22:20:40 +02:00
/**
* Prevent other event listeners for 'mousedown' from executing their actions
*/
event.stopImmediatePropagation();
2017-08-24 22:20:40 +02:00
if (mesh.selected) {
this.deSelectMesh(mesh);
} else {
this.selectMesh(mesh);
}
/**
* Notify our GUI system to build a GUI
*/
GameLib.Event.Emit(
GameLib.Event.BUILD_GUI,
null
)
}
}.bind(this)
);
}
2017-08-24 22:20:40 +02:00
};
/**
*
* @param event
*/
2017-10-30 06:47:40 +01:00
GameLib.System.Input.prototype.onMouseMoveEdit = function(event) {
2017-08-24 22:20:40 +02:00
};
/**
* Update the camera position etc. after mouse up
* @returns {Function}
* @param event
2017-08-24 22:20:40 +02:00
*/
2017-10-30 06:47:40 +01:00
GameLib.System.Input.prototype.onMouseUpEdit = 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;
editorControl.camera.quaternion.x = editorControl.camera.instance.quaternion.x;
editorControl.camera.quaternion.y = editorControl.camera.instance.quaternion.y;
editorControl.camera.quaternion.z = editorControl.camera.instance.quaternion.z;
editorControl.camera.quaternion.w = editorControl.camera.instance.quaternion.w;
editorControl.camera.lookAt.x = editorControl.instance.center.x;
editorControl.camera.lookAt.y = editorControl.instance.center.y;
editorControl.camera.lookAt.z = editorControl.instance.center.z;
editorControl.camera.lookAt.instance.copy(editorControl.instance.center);
}
);
2017-08-24 22:20:40 +02:00
};
/**
* Update our camera position after moving the mouse wheel
* @returns {Function}
* @param event
2017-08-24 22:20:40 +02:00
*/
2017-10-30 06:47:40 +01:00
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;
}
);
2017-08-24 22:20:40 +02:00
};
GameLib.System.Input.prototype.selectMesh = function(mesh) {
2017-06-19 15:54:02 +02:00
/**
2017-08-24 22:20:40 +02:00
* If mesh is already selected, do nothing
2017-06-19 15:54:02 +02:00
*/
2017-08-24 22:20:40 +02:00
if (mesh.selected === true) {
return;
}
2017-06-19 15:54:02 +02:00
2017-08-24 22:20:40 +02:00
/**
* Notify our component as being 'selected'
* @type {boolean}
*/
mesh.selected = true;
2017-06-19 15:54:02 +02:00
2017-08-24 22:20:40 +02:00
mesh.createHelper();
2017-06-19 15:54:02 +02:00
2017-08-24 22:20:40 +02:00
GameLib.Event.Emit(
GameLib.Event.MESH_SELECTED,
{
mesh : mesh
}
);
};
2017-06-19 15:54:02 +02:00
2017-08-24 22:20:40 +02:00
GameLib.System.Input.prototype.deSelectMesh = function(mesh) {
2017-06-19 15:54:02 +02:00
2017-08-24 22:20:40 +02:00
mesh.selected = false;
2017-06-19 15:54:02 +02:00
2017-08-24 22:20:40 +02:00
mesh.removeHelper();
2017-06-19 15:54:02 +02:00
2017-08-24 22:20:40 +02:00
GameLib.Event.Emit(
GameLib.Event.MESH_DESELECTED,
{
mesh : mesh
}
);
2017-06-19 15:54:02 +02:00
};
2017-09-27 21:44:25 +02:00
2017-06-19 15:54:02 +02:00
2017-08-24 22:20:40 +02:00
//
// console.log('keypressed ' + event.code);
//
// if (event.code === 'KeyV') {
// //todo - change view
// }
//
//
// 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();
// }
//
2017-12-04 13:23:15 +01:00
// var meshes = entity.queryComponents(GameLib.Component.MESH);
2017-08-24 22:20:40 +02:00
//
// 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();
// }
// }
// }
// };