modern/js/controls/initDesktopEventListeners.js
2021-01-17 17:17:19 +01:00

116 lines
3.2 KiB
JavaScript
Executable File

import dims from "./dimensions";
export default function (scope) {
"use strict";
let downX = 0;
let downY = 0;
window.addEventListener("mousemove", function (ev) {
if (this.type !== "anim") {
scope.nm[0] = ev.clientX / dims.w * 2 - 1;
scope.nm[1] = ev.clientY / dims.h * -2 + 1;
}
}.bind(scope), false);
scope.domElement.addEventListener("mousedown", function (ev) {
downX = ev.clientX / dims.w * 2 - 1;
downY = ev.clientY / dims.h * -2 + 1;
scope.mouseArray = [
[downX, downY],
[downX, downY],
[downX, downY],
[downX, downY],
[downX, downY],
[downX, downY],
[downX, downY],
[downX, downY],
[downX, downY],
[downX, downY]
]
this.dragMultiplier = 1;
this.pressed = true;
}.bind(scope), false);
scope.domElement.addEventListener("mouseup", function (ev) {
this.pressed = false;
if (Math.abs(downX - scope.nm[0]) + Math.abs(downY - scope.nm[1]) < 0.05) {
//If we have an intersect object
if (this.intersects) {
//If the name of the object under the cursor === floor then set controls to FP, translate to Cursor pos
if (this.intersects.object.name === "floor") {
this.setFP(this.intersects);
this.activeObject.uuid = undefined;
}
//If the object under the cursor is clickable &
//not the activeObject & the camera is not animating translate the Camera to it
else if (
this.intersects.object.userData.clickable === true &&
this.intersects.object.uuid !== this.activeObject.uuid &&
this.type !== "anim") {
this.setOrbit(this.intersects.object);
}
}
} else {
this.dragMultiplier = 1.1;
this.dragVector.x = (scope.mouseArray[9][1] - scope.mouseArray[7][1]) * -1;
this.dragVector.y = scope.mouseArray[9][0] - scope.mouseArray[7][0];
}
if (this.saveCamTransform) {
localStorage.camTransform = [
this.target.position.x,
this.target.position.y,
this.target.position.z,
this.target.rotation.x,
this.target.rotation.y,
this.target.rotation.z
];
}
}.bind(scope), false);
scope.domElement.addEventListener("mouseleave", function () {
this.pressed = false;
}.bind(scope), false);
window.addEventListener("keydown", function (e) {
switch (e.key) {
case "Escape":
if (this.type === "orbit" && this.activeObject.uuid) {
this.setFP({
point: {
x: this.savePosition.x,
y: 0,
z: this.savePosition.z
}
});
}
break;
default:
break;
}
}.bind(scope), false);
}