78 lines
2.1 KiB
JavaScript
78 lines
2.1 KiB
JavaScript
class Camera {
|
|
constructor() {
|
|
this.mouse;
|
|
this.rotation = {
|
|
x: 0,
|
|
y: 0
|
|
}
|
|
this.lastMouse;
|
|
this.mousePressed = false;
|
|
this.translate = {
|
|
x: 0,
|
|
y: 0,
|
|
z: 0
|
|
}
|
|
}
|
|
|
|
async init() {
|
|
this.mouse = {
|
|
x: 0,
|
|
y: 0
|
|
}
|
|
window.addEventListener('mousedown', this.mouseDown.bind(this));
|
|
window.addEventListener('mouseup', this.mouseUp.bind(this));
|
|
window.addEventListener('mousemove', this.mouseMove.bind(this), {passive: true});
|
|
eventHandler.addEvent('keys-ArrowUp, keys-ArrowDown, keys-ArrowLeft, keys-ArrowRight, keys-KeyQ, keys-KeyE', this.keyPressed.bind(this));
|
|
}
|
|
|
|
mouseDown() {
|
|
this.mousePressed = true;
|
|
this.lastMouse = null;
|
|
}
|
|
|
|
mouseUp() {
|
|
this.mousePressed = false;
|
|
this.lastMouse = null;
|
|
}
|
|
|
|
mouseMove(event) {
|
|
if (!this.mousePressed || gui.modal.open) {
|
|
return;
|
|
}
|
|
if (this.lastMouse) {
|
|
let mouse = this.mouse,
|
|
rotate = this.rotation;
|
|
mouse.x += (this.lastMouse.x - event.clientX) * 0.2;
|
|
mouse.y += (this.lastMouse.y - event.clientY) * 0.2;
|
|
rotate.x = VTUtils.map(mouse.x, -c.width, c.width, 180, -180, false);
|
|
rotate.y = VTUtils.map(mouse.y, -c.height, c.height, 180, -180, false);
|
|
}
|
|
this.lastMouse = {
|
|
x: event.clientX,
|
|
y: event.clientY
|
|
}
|
|
}
|
|
|
|
keyPressed(data) {
|
|
switch (data) {
|
|
case 'keys-ArrowUp':
|
|
this.translate.z += 10;
|
|
break;
|
|
case 'keys-ArrowDown':
|
|
this.translate.z -= 10;
|
|
break;
|
|
case 'keys-ArrowLeft':
|
|
this.translate.x -= 10;
|
|
break;
|
|
case 'keys-ArrowRight':
|
|
this.translate.x += 10;
|
|
break;
|
|
case 'keys-KeyQ':
|
|
this.translate.y += 10;
|
|
break;
|
|
case 'keys-KeyE':
|
|
this.translate.y -= 10;
|
|
break;
|
|
}
|
|
}
|
|
} |