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

1460 lines
37 KiB
JavaScript

/**
* R3.System.Input
* @constructor
*/
R3.System.Input = function() {
R3.System.call(
this
);
this.selectAll = false;
this.applicationMode = R3.API.Project.APPLICATION_MODE_EDIT;
R3.Event.Emit(
R3.Event.GET_APPLICATION_MODE,
function(applicationMode) {
this.applicationMode = applicationMode;
}.bind(this)
);
this.grabMode = false;
this.grabModeDirection = 'x';
this.controlLeft = false;
this.sensitivityCounter = 0;
this.playAudio = true;
this.editorControls = [];
this.orbitControls = [];
this.firstPersonControls = [];
this.touchControls = [];
this.keyboardControls = [];
this.mouseControls = [];
/**
* Touch Controls
* @type {null}
*/
this.touchStart = this.onTouchStart.bind(this);
this.touchMove = this.onTouchMove.bind(this);
this.touchEnd = this.onTouchEnd.bind(this);
this.touchCancel = this.onTouchCancel.bind(this);
/**
* Keyboard Controls
* @type {null}
*/
this.keyboardKeyUp = this.onKeyboardKeyUp.bind(this);
this.keyboardKeyDown = this.onKeyboardKeyDown.bind(this);
/**
* Mouse Controls
* @type {null}
*/
this.mouseDown = this.onMouseDown.bind(this);
this.mouseMove = this.onMouseMove.bind(this);
this.mouseWheel = this.onMouseWheel.bind(this);
this.mouseUp = this.onMouseUp.bind(this);
this.instanceCreatedSubscription = null;
this.removeComponentSubscription = null;
this.canvasChangeSubscription = null;
this.selectionModeChangeSubscription = null;
this.beforeRenderSubscription = null;
this.applicationModeSubscription = null;
this.selectionMode = R3.System.Input.SELECTION_MODE_DEFAULT;
};
R3.System.Input.prototype = Object.create(R3.System.prototype);
R3.System.Input.prototype.constructor = R3.System.Input;
R3.System.Input.SELECTION_MODE_MESH = 0x1;
R3.System.Input.SELECTION_MODE_FACE = 0x2;
R3.System.Input.SELECTION_MODE_DEFAULT = 0x1;
R3.System.Input.KEY_CANCEL = 3;
R3.System.Input.KEY_HELP = 6;
R3.System.Input.KEY_BACK_SPACE = 8;
R3.System.Input.KEY_TAB = 9;
R3.System.Input.KEY_CLEAR = 12;
R3.System.Input.KEY_RETURN = 13;
R3.System.Input.KEY_ENTER = 14;
R3.System.Input.KEY_SHIFT = 16;
R3.System.Input.KEY_CONTROL = 17;
R3.System.Input.KEY_ALT = 18;
R3.System.Input.KEY_PAUSE = 19;
R3.System.Input.KEY_CAPS_LOCK = 20;
R3.System.Input.KEY_ESCAPE = 27;
R3.System.Input.KEY_SPACE = 32;
R3.System.Input.KEY_PAGE_UP = 33;
R3.System.Input.KEY_PAGE_DOWN = 34;
R3.System.Input.KEY_END = 35;
R3.System.Input.KEY_HOME = 36;
R3.System.Input.KEY_LEFT = 37;
R3.System.Input.KEY_UP = 38;
R3.System.Input.KEY_RIGHT = 39;
R3.System.Input.KEY_DOWN = 40;
R3.System.Input.KEY_PRINTSCREEN = 44;
R3.System.Input.KEY_INSERT = 45;
R3.System.Input.KEY_DELETE = 46;
R3.System.Input.KEY_0 = 48;
R3.System.Input.KEY_1 = 49;
R3.System.Input.KEY_2 = 50;
R3.System.Input.KEY_3 = 51;
R3.System.Input.KEY_4 = 52;
R3.System.Input.KEY_5 = 53;
R3.System.Input.KEY_6 = 54;
R3.System.Input.KEY_7 = 55;
R3.System.Input.KEY_8 = 56;
R3.System.Input.KEY_9 = 57;
R3.System.Input.KEY_SEMICOLON = 59;
R3.System.Input.KEY_EQUALS = 61;
R3.System.Input.KEY_A = 65;
R3.System.Input.KEY_B = 66;
R3.System.Input.KEY_C = 67;
R3.System.Input.KEY_D = 68;
R3.System.Input.KEY_E = 69;
R3.System.Input.KEY_F = 70;
R3.System.Input.KEY_G = 71;
R3.System.Input.KEY_H = 72;
R3.System.Input.KEY_I = 73;
R3.System.Input.KEY_J = 74;
R3.System.Input.KEY_K = 75;
R3.System.Input.KEY_L = 76;
R3.System.Input.KEY_M = 77;
R3.System.Input.KEY_N = 78;
R3.System.Input.KEY_O = 79;
R3.System.Input.KEY_P = 80;
R3.System.Input.KEY_Q = 81;
R3.System.Input.KEY_R = 82;
R3.System.Input.KEY_S = 83;
R3.System.Input.KEY_T = 84;
R3.System.Input.KEY_U = 85;
R3.System.Input.KEY_V = 86;
R3.System.Input.KEY_W = 87;
R3.System.Input.KEY_X = 88;
R3.System.Input.KEY_Y = 89;
R3.System.Input.KEY_Z = 90;
R3.System.Input.KEY_CONTEXT_MENU = 93;
R3.System.Input.KEY_NUMPAD0 = 96;
R3.System.Input.KEY_NUMPAD1 = 97;
R3.System.Input.KEY_NUMPAD2 = 98;
R3.System.Input.KEY_NUMPAD3 = 99;
R3.System.Input.KEY_NUMPAD4 = 100;
R3.System.Input.KEY_NUMPAD5 = 101;
R3.System.Input.KEY_NUMPAD6 = 102;
R3.System.Input.KEY_NUMPAD7 = 103;
R3.System.Input.KEY_NUMPAD8 = 104;
R3.System.Input.KEY_NUMPAD9 = 105;
R3.System.Input.KEY_MULTIPLY = 106;
R3.System.Input.KEY_ADD = 107;
R3.System.Input.KEY_SEPARATOR = 108;
R3.System.Input.KEY_SUBTRACT = 109;
R3.System.Input.KEY_DECIMAL = 110;
R3.System.Input.KEY_DIVIDE = 111;
R3.System.Input.KEY_F1 = 112;
R3.System.Input.KEY_F2 = 113;
R3.System.Input.KEY_F3 = 114;
R3.System.Input.KEY_F4 = 115;
R3.System.Input.KEY_F5 = 116;
R3.System.Input.KEY_F6 = 117;
R3.System.Input.KEY_F7 = 118;
R3.System.Input.KEY_F8 = 119;
R3.System.Input.KEY_F9 = 120;
R3.System.Input.KEY_F10 = 121;
R3.System.Input.KEY_F11 = 122;
R3.System.Input.KEY_F12 = 123;
R3.System.Input.KEY_F13 = 124;
R3.System.Input.KEY_F14 = 125;
R3.System.Input.KEY_F15 = 126;
R3.System.Input.KEY_F16 = 127;
R3.System.Input.KEY_F17 = 128;
R3.System.Input.KEY_F18 = 129;
R3.System.Input.KEY_F19 = 130;
R3.System.Input.KEY_F20 = 131;
R3.System.Input.KEY_F21 = 132;
R3.System.Input.KEY_F22 = 133;
R3.System.Input.KEY_F23 = 134;
R3.System.Input.KEY_F24 = 135;
R3.System.Input.KEY_NUM_LOCK = 144;
R3.System.Input.KEY_SCROLL_LOCK = 145;
R3.System.Input.KEY_COMMA = 188;
R3.System.Input.KEY_PERIOD = 190;
R3.System.Input.KEY_SLASH = 191;
R3.System.Input.KEY_BACK_QUOTE = 192;
R3.System.Input.KEY_OPEN_BRACKET = 219;
R3.System.Input.KEY_BACK_SLASH = 220;
R3.System.Input.KEY_CLOSE_BRACKET = 221;
R3.System.Input.KEY_QUOTE = 222;
R3.System.Input.KEY_META = 224;
/**
*
*/
R3.System.Input.prototype.start = function() {
R3.System.prototype.start.call(this);
this.instanceCreatedSubscription = R3.Event.Subscribe(
R3.Event.INSTANCE_CREATED,
this.instanceCreated.bind(this)
);
this.removeComponentSubscription = R3.Event.Subscribe(
R3.Event.REMOVE_COMPONENT,
this.removeComponent.bind(this)
);
this.canvasChangeSubscription = R3.Event.Subscribe(
R3.Event.CANVAS_CHANGE,
this.canvasChange.bind(this)
);
this.beforeRenderSubscription = R3.Event.Subscribe(
R3.Event.BEFORE_RENDER,
this.beforeRender.bind(this)
);
this.selectionModeChangeSubscription = R3.Event.Subscribe(
R3.Event.SELECTION_MODE_CHANGE,
this.selectionModeChange.bind(this)
);
/**
* Normal Controls
*/
this.touchControls = R3.EntityManager.Instance.findComponentsByConstructor(R3.Controls.Touch);
this.keyboardControls = R3.EntityManager.Instance.findComponentsByConstructor(R3.Controls.Keyboard);
this.mouseControls = R3.EntityManager.Instance.findComponentsByConstructor(R3.Controls.Mouse);
this.orbitControls = R3.EntityManager.Instance.findComponentsByConstructor(R3.Controls.D3.Orbit);
this.firstPersonControls = R3.EntityManager.Instance.findComponentsByConstructor(R3.Controls.D3.FirstPerson);
};
/**
*
*/
R3.System.Input.prototype.stop = function() {
R3.System.prototype.stop.call(this);
this.instanceCreatedSubscription.remove();
this.removeComponentSubscription.remove();
this.canvasChangeSubscription.remove();
this.selectionModeChangeSubscription.remove();
this.beforeRenderSubscription.remove();
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.orbitControls.map(
function(orbitControl) {
orbitControl.instance.dispose();
}
);
this.firstPersonControls.map(
function(firstPersonControl) {
firstPersonControl.instance.dispose();
}
);
this.editorControls = [];
this.firstPersonControls = [];
this.orbitControls = [];
this.touchControls = [];
this.keyboardControls = [];
this.mouseControls = [];
};
/**
*
* @param data
*/
R3.System.Input.prototype.canvasChange = function(data) {
if (data.component instanceof R3.Controls) {
console.log('todo: implement dom element change');
}
};
/**
* Changes the selection mode from face to mesh etc.
* @param data
*/
R3.System.Input.prototype.selectionModeChange = function(data) {
this.selectionMode = data.selectionMode;
};
R3.System.Input.prototype.beforeRender = function(project) {
if (project.applicationMode === R3.API.Project.APPLICATION_MODE_EDIT) {
project.controls.map(
function(control) {
if (
control instanceof R3.Controls.D3.Orbit ||
control instanceof R3.Controls.D3.FirstPerson
) {
control.instance.update(project.clock.getDelta());
control.camera.updateFromInstance('position');
control.camera.updateFromInstance('quaternion');
control.camera.updateFromInstance('rotation');
if (control.camera instanceof R3.D3.Camera.Perspective.Stereo) {
control.camera.instance.userData.stereo.update(control.camera.instance);
}
}
}
);
} else {
/**
* We don't update anything at the moment for normal controls
*/
}
};
/**
* From now on we want to track everything about a component from the systems that are active
* @param data
*/
R3.System.Input.prototype.instanceCreated = function(data) {
if (data.component instanceof R3.Controls.Touch) {
R3.Utils.PushUnique(this.touchControls, data.component);
this.registerTouchControl(data.component);
}
if (data.component instanceof R3.Controls.Keyboard) {
R3.Utils.PushUnique(this.keyboardControls, data.component);
this.registerKeyboardControl(data.component);
}
if (data.component instanceof R3.Controls.Mouse) {
R3.Utils.PushUnique(this.mouseControls, data.component);
this.registerMouseControl(data.component);
}
if (data.component instanceof R3.Controls.D3.Orbit) {
R3.Utils.PushUnique(this.orbitControls, data.component);
}
if (data.component instanceof R3.Controls.D3.FirstPerson) {
R3.Utils.PushUnique(this.firstPersonControls, data.component);
}
};
/**
* Removes controls from this system
* @param data
*/
R3.System.Input.prototype.removeComponent = function(data) {
var index;
if (data.component instanceof R3.Controls.Touch) {
index = this.touchControls.indexOf(data.component);
if (index !== -1) {
this.deRegisterTouchControl(data.component);
this.touchControls.splice(index, 1);
} else {
console.log('Failed to remove ' + R3.GetComponentName(data.component) + ' from R3.System.Input');
}
}
if (data.component instanceof R3.Controls.Keyboard) {
index = this.keyboardControls.indexOf(data.component);
if (index !== -1) {
this.deRegisterKeyboardControl(data.component);
this.keyboardControls.splice(index, 1);
} else {
console.log('Failed to remove ' + R3.GetComponentName(data.component) + ' from R3.System.Input');
}
}
if (data.component instanceof R3.Controls.Mouse) {
index = this.mouseControls.indexOf(data.component);
if (index !== -1) {
this.deRegisterMouseControl(data.component);
this.mouseControls.splice(index, 1);
} else {
console.log('Failed to remove ' + R3.GetComponentName(data.component) + ' from R3.System.Input');
}
}
if (data.component instanceof R3.Controls.D3.FirstPerson) {
index = this.firstPersonControls.indexOf(data.component);
if (index !== -1) {
data.component.instance.dispose();
this.firstPersonControls.splice(index, 1);
} else {
console.log('Failed to remove ' + R3.GetComponentName(data.component) + ' from R3.System.Input');
}
}
if (data.component instanceof R3.Controls.D3.Orbit) {
index = this.orbitControls.indexOf(data.component);
if (index !== -1) {
data.component.instance.dispose();
this.orbitControls.splice(index, 1);
} else {
console.log('Failed to remove ' + R3.GetComponentName(data.component) + ' from R3.System.Input');
}
}
};
R3.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');
return;
}
touchControl.canvas.instance.addEventListener(
'touchstart',
this.touchStart,
true
);
touchControl.canvas.instance.addEventListener(
'touchmove',
this.touchMove,
true
);
touchControl.canvas.instance.addEventListener(
'touchend',
this.touchEnd,
true
);
touchControl.canvas.instance.addEventListener(
'touchcancel',
this.touchCancel,
true
);
};
R3.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');
return;
}
keyboardControl.canvas.instance.addEventListener(
'keyup',
this.keyboardKeyUp,
true
);
keyboardControl.canvas.instance.addEventListener(
'keydown',
this.keyboardKeyDown,
true
);
};
R3.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');
return;
}
mouseControl.canvas.instance.addEventListener(
'mousedown',
this.mouseDown,
false
);
mouseControl.canvas.instance.addEventListener(
'mousemove',
this.mouseMove,
false
);
mouseControl.canvas.instance.addEventListener(
'wheel',
this.mouseWheel,
false
);
mouseControl.canvas.instance.addEventListener(
'mouseup',
this.mouseUp,
false
);
};
R3.System.Input.prototype.deRegisterTouchControl = function(touchControl) {
touchControl.canvas.instance.removeEventListener(
'touchstart',
this.touchStart,
true
);
touchControl.canvas.instance.removeEventListener(
'touchmove',
this.touchMove,
true
);
touchControl.canvas.instance.removeEventListener(
'touchend',
this.touchEnd,
true
);
touchControl.canvas.instance.removeEventListener(
'touchcancel',
this.touchCancel,
true
);
};
R3.System.Input.prototype.deRegisterKeyboardControl = function(keyboardControl) {
keyboardControl.canvas.instance.removeEventListener(
'keydown',
this.keyboardKeyDown,
true
);
keyboardControl.canvas.instance.removeEventListener(
'keyup',
this.keyboardKeyUp,
true
);
};
R3.System.Input.prototype.deRegisterMouseControl = function(mouseControl) {
mouseControl.canvas.instance.removeEventListener(
'mousedown',
this.mouseDown,
false
);
mouseControl.canvas.instance.removeEventListener(
'mousemove',
this.mouseMove,
false
);
mouseControl.canvas.instance.removeEventListener(
'wheel',
this.mouseWheel,
false
);
mouseControl.canvas.instance.removeEventListener(
'mouseup',
this.mouseUp,
false
);
};
R3.System.Input.prototype.onKeyboardKeyUp = function(event) {
R3.Event.Emit(
R3.Event.KEY_UP,
{
code : event.code || event.key,
keyCode : event.keyCode
}
);
};
R3.System.Input.prototype.onKeyboardKeyDown = function(event) {
R3.Event.Emit(
R3.Event.KEY_DOWN,
{
code : event.code || event.key,
keyCode : event.keyCode
}
);
};
R3.System.Input.prototype.onTouchStart = function(event) {
this.sensitivityCounter = 0;
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,
lastTouchX : event.touches[t].pageX,
lastTouchY : event.touches[t].pageY,
pageX : event.touches[t].pageX,
pageY : event.touches[t].pageY,
cancelled : false,
ended : false
};
}
this.touches.event = event;
R3.Event.Emit(
R3.Event.TOUCH_START,
this.touches
)
};
R3.System.Input.prototype.onTouchMove = function(event) {
this.sensitivityCounter++;
var id = null;
var leftTouch = null;
var rightTouch = null;
var bottomTouch = null;
var topTouch = null;
var inward = false;
var outward = false;
var pinch = false;
var zoom = false;
var totalUp = 0;
var totalDown = 0;
var totalLeft = 0;
var totalRight = 0;
for (var t = 0; t < event.changedTouches.length; t++) {
id = event.changedTouches[t].identifier;
if (this.touches[id]) {
var diffX = Math.abs(this.touches[id].lastTouchX - event.changedTouches[t].pageX);
var diffY = Math.abs(this.touches[id].lastTouchY - event.changedTouches[t].pageY);
var left = 0;
var right = 0;
var up = 0;
var down = 0;
if (this.touches[id].lastTouchX < event.changedTouches[t].pageX) {
right += diffX;
}
if (this.touches[id].lastTouchX > event.changedTouches[t].pageX) {
left += diffX;
}
if (this.touches[id].lastTouchY > event.changedTouches[t].pageY) {
up += diffY;
}
if (this.touches[id].lastTouchY < event.changedTouches[t].pageY) {
down += diffY;
}
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;
totalLeft += left;
totalRight += right;
totalUp += up;
totalDown += down;
}
}
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];
} else {
bottomTouch = this.touches[event.changedTouches[0].identifier];
topTouch = this.touches[event.changedTouches[1].identifier];
}
}
if (leftTouch && leftTouch.left && rightTouch && rightTouch.right) {
outward = true;
}
if (leftTouch && leftTouch.right && rightTouch && rightTouch.left) {
inward = true;
}
if (bottomTouch && bottomTouch.up && topTouch && topTouch.down) {
pinch = true;
}
if (bottomTouch && bottomTouch.down && topTouch && topTouch.up) {
zoom = true;
}
this.touches.event = event;
this.touches.meta = {
inward : inward,
outward : outward,
pinch : pinch,
zoom : zoom,
totalLeft : totalLeft,
totalRight : totalRight,
totalUp : totalUp,
totalDown : totalDown,
movementX : totalRight - totalLeft,
movementY : totalDown - totalUp
};
// if (this.sensitivityCounter >= this.touchSensitivity) {
//
// this.sensitivityCounter = 0;
R3.Event.Emit(
R3.Event.TOUCH_MOVE,
this.touches
);
// }
};
R3.System.Input.prototype.onTouchCancel = function(event) {
this.sensitivityCounter = 0;
for (var t = 0; t < event.changedTouches.length; t++) {
this.touches[event.changedTouches[t].identifier].cancelled = true;
this.touches[event.changedTouches[t].identifier].event = event;
R3.Event.Emit(
R3.Event.TOUCH_CANCEL,
this.touches[event.changedTouches[t].identifier]
);
delete this.touches[event.changedTouches[t].identifier];
}
};
R3.System.Input.prototype.onTouchEnd = function(event) {
this.sensitivityCounter = 0;
for (var t = 0; t < event.changedTouches.length; t++) {
this.touches[event.changedTouches[t].identifier].ended = true;
this.touches[event.changedTouches[t].identifier].event = event;
R3.Event.Emit(
R3.Event.TOUCH_END,
this.touches[event.changedTouches[t].identifier]
);
delete this.touches[event.changedTouches[t].identifier];
}
};
R3.System.Input.prototype.onKeyDownEdit = function(event) {
console.log('input system emitted keypress ' + event.code);
R3.Event.Emit(
R3.Event.KEY_DOWN,
{
code : event.code || event.key,
keyCode : event.keyCode
}
);
var meshes = null;
if (event.keyCode === R3.System.Input.KEY_DELETE) {
meshes = R3.EntityManager.Instance.findComponentsByConstructor(R3.D3.Mesh);
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)
);
R3.Event.Emit(
R3.Event.REMOVE_MESH,
{
meshes : deletedMeshes
}
);
}
if (event.keyCode === R3.System.Input.KEY_CONTROL) {
this.controlLeft = true;
}
if (event.keyCode === R3.System.Input.KEY_A) {
this.selectAll = !this.selectAll;
if (this.selectionMode === R3.System.Input.SELECTION_MODE_MESH) {
meshes = R3.EntityManager.Instance.findComponentsByConstructor(R3.D3.Mesh);
meshes.map(function(mesh) {
if (this.selectAll) {
this.selectMesh(mesh);
} else {
this.deselectMesh(mesh);
}
}.bind(this));
} else {
console.warn('todo: implement face select all');
}
R3.Event.Emit(
R3.Event.BUILD_GUI,
null
)
}
if (event.keyCode === R3.System.Input.KEY_P) {
R3.Event.Emit(R3.Event.GAME_PAUSE);
}
if (event.keyCode === R3.System.Input.KEY_G) {
this.grabMode = !this.grabMode;
if (this.grabMode) {
console.log('grab mode active, translating on ' + this.grabModeDirection + ' axis');
} else {
console.log('grab mode de-activated');
}
}
};
R3.System.Input.prototype.onKeyUpEdit = function(event) {
R3.Event.Emit(
R3.Event.KEY_UP,
{
code : event.code || event.key,
keyCode : event.keyCode
}
);
if (event.code === 'ControlLeft') {
this.controlLeft = false;
}
};
R3.System.Input.prototype.onMouseDown = function(event) {
R3.Event.Emit(
R3.Event.MOUSE_DOWN,
{
event : event
}
)
};
R3.System.Input.prototype.onMouseMove = function(event) {
R3.Event.Emit(
R3.Event.MOUSE_MOVE,
{
event : event
}
)
};
R3.System.Input.prototype.onMouseWheel = function(event) {
R3.Event.Emit(
R3.Event.MOUSE_WHEEL,
{
event : event
}
)
};
R3.System.Input.prototype.onMouseUp = function(event) {
R3.Event.Emit(
R3.Event.MOUSE_UP,
{
event : event
}
)
};
R3.System.Input.prototype.onMouseDownEdit = function(event) {
if (event.button === 2) {
R3.EntityManager.Instance.findComponentsByConstructor(R3.Controls.D3).map(
function(control) {
if (this.controlLeft) {
return;
}
this.mouse.x = (event.offsetX / event.target.width ) * 2 - 1;
this.mouse.y = -(event.offsetY / event.target.height) * 2 + 1;
R3.Event.Emit(
R3.Event.GET_RENDER_CONFIGURATION,
null,
function(configuration) {
var scenes = configuration.activeScenes;
var camera = configuration.activeCamera;
var intersects = scenes.reduce(
function(result, scene) {
this.raycaster.setFromCamera(
this.mouse,
camera
);
this.raycaster.getIntersectedObjects(scene.meshes).map(
function(intersect) {
result.push(intersect);
}
);
this.raycaster.getIntersectedObjects(scene.clones).map(
function(intersect) {
result.push(intersect);
}
);
return result;
}.bind(this),
[]
);
if (intersects.length < 1) {
return;
}
/**
* Find the closest intersected mesh
*/
intersects.sort(
function(a, b) {
if (a.distance < b.distance) {
return -1;
}
if (a.distance > b.distance) {
return 1;
}
return 0;
}
);
var mesh = intersects[0].mesh;
var face = intersects[0].face;
/**
* Prevent default action (like context menu or whatever)
*/
event.preventDefault();
/**
* Prevent other event listeners for 'mousedown' from executing their actions
*/
event.stopImmediatePropagation();
if (this.selectionMode === R3.System.Input.SELECTION_MODE_MESH) {
if (mesh.selected) {
this.deselectMesh(mesh);
} else {
this.selectMesh(mesh);
}
} else {
if (face.selected) {
this.deselectFace(mesh, face);
} else {
this.selectFace(mesh, face);
}
}
/**
* Notify our GUI system to build a GUI
*/
R3.Event.Emit(
R3.Event.BUILD_GUI,
null
)
}.bind(this)
);
}.bind(this)
);
}
};
/**
*
* @param event
*/
R3.System.Input.prototype.onMouseMoveEdit = function(event) {
R3.EntityManager.Instance.queryComponents(R3.Component.MOUSE).map(
function(mouse) {
mouse.x = event.clientX;
mouse.y = event.clientY;
}
);
R3.EntityManager.Instance.findComponentsByConstructor(R3.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;
control.camera.rotation.x = control.camera.instance.rotation.x;
control.camera.rotation.y = control.camera.instance.rotation.y;
control.camera.rotation.z = control.camera.instance.rotation.z;
control.camera.quaternion.x = control.camera.instance.quaternion.x;
control.camera.quaternion.y = control.camera.instance.quaternion.y;
control.camera.quaternion.z = control.camera.instance.quaternion.z;
control.camera.quaternion.w = control.camera.instance.quaternion.w;
}
);
};
/**
* Update the camera position etc. after mouse up
* @returns {Function}
* @param event
*/
R3.System.Input.prototype.onMouseUpEdit = function(event) {
};
/**
* Update our camera position after moving the mouse wheel
* @returns {Function}
* @param event
*/
R3.System.Input.prototype.onMouseWheelEdit = function(event) {
R3.EntityManager.Instance.findComponentsByConstructor(R3.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;
}
);
};
R3.System.Input.prototype.selectFace = function(mesh, face) {
/**
* If mesh is already selected, do nothing
*/
if (face.selected === true) {
return;
}
/**
* Notify our component as being 'selected'
* @type {boolean}
*/
face.selected = true;
face.createHelper(mesh);
R3.Event.Emit(
R3.Event.MESH_FACE_SELECTED,
{
mesh : mesh,
face : face
}
);
};
R3.System.Input.prototype.selectMesh = function(mesh) {
/**
* If mesh is already selected, do nothing
*/
if (mesh.selected === true) {
return;
}
/**
* Notify our component as being 'selected'
* @type {boolean}
*/
mesh.selected = true;
mesh.createHelper();
this.orbitControls.map(
function(controls){
controls.target = mesh;
controls.updateInstance('target');
}
);
R3.Event.Emit(
R3.Event.MESH_SELECTED,
{
mesh : mesh
}
);
};
R3.System.Input.prototype.deselectFace = function(mesh, face) {
face.selected = false;
face.removeHelper(mesh);
R3.Event.Emit(
R3.Event.MESH_FACE_DESELECTED,
{
mesh : mesh,
face : face
}
);
};
R3.System.Input.prototype.deselectMesh = function(mesh) {
mesh.selected = false;
mesh.removeHelper();
this.orbitControls.map(
function(controls){
controls.target = null;
controls.updateInstance('target');
}
);
R3.Event.Emit(
R3.Event.MESH_DESELECTED,
{
mesh : mesh
}
);
};
//
// 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');
// }
// }
// };
// R3.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(R3.Component.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}
// */
// R3.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
// */
// R3.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
// */
// R3.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();
// }
// }
// }
// };