2017-09-02 15:14:10 -04:00
|
|
|
// pathfinder/client/src/camera.ts
|
|
|
|
//
|
|
|
|
// Copyright © 2017 The Pathfinder Project Developers.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
import * as glmatrix from 'gl-matrix';
|
|
|
|
import {PathfinderView} from "./view";
|
|
|
|
|
2017-09-02 16:41:08 -04:00
|
|
|
const ORTHOGRAPHIC_ZOOM_SPEED: number = 1.0 / 100.0;
|
|
|
|
|
2017-09-10 16:17:24 -04:00
|
|
|
const ZOOM_IN_FACTOR: number = 1.2;
|
|
|
|
const ZOOM_OUT_FACTOR: number = 1.0 / ZOOM_IN_FACTOR;
|
|
|
|
|
2017-09-02 16:41:08 -04:00
|
|
|
const PERSPECTIVE_MOVEMENT_SPEED: number = 10.0;
|
|
|
|
const PERSPECTIVE_ROTATION_SPEED: number = 1.0 / 300.0;
|
|
|
|
|
|
|
|
const MOVEMENT_INTERVAL_DELAY: number = 10;
|
2017-09-02 15:14:10 -04:00
|
|
|
|
2017-09-05 22:47:19 -04:00
|
|
|
const INITIAL_TRANSLATION: glmatrix.vec3 = glmatrix.vec3.fromValues(0.0, 0.0, -1000.0);
|
|
|
|
|
2017-09-02 15:14:10 -04:00
|
|
|
export abstract class Camera {
|
|
|
|
constructor(canvas: HTMLCanvasElement) {
|
|
|
|
this.canvas = canvas;
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:17:24 -04:00
|
|
|
abstract zoomIn(): void;
|
|
|
|
abstract zoomOut(): void;
|
|
|
|
|
2017-09-02 15:14:10 -04:00
|
|
|
protected canvas: HTMLCanvasElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class OrthographicCamera extends Camera {
|
|
|
|
constructor(canvas: HTMLCanvasElement) {
|
|
|
|
super(canvas);
|
|
|
|
|
|
|
|
this.translation = glmatrix.vec2.create();
|
|
|
|
this.scale = 1.0;
|
|
|
|
|
|
|
|
this.canvas.addEventListener('wheel', event => this.onWheel(event), false);
|
2017-09-11 15:21:50 -04:00
|
|
|
this.canvas.addEventListener('mousedown', event => this.onMouseDown(event), false);
|
|
|
|
this.canvas.addEventListener('mouseup', event => this.onMouseUp(event), false);
|
|
|
|
this.canvas.addEventListener('mousemove', event => this.onMouseMove(event), false);
|
2017-09-02 15:14:10 -04:00
|
|
|
|
|
|
|
this.onPan = null;
|
|
|
|
this.onZoom = null;
|
|
|
|
}
|
|
|
|
|
2017-09-11 15:21:50 -04:00
|
|
|
onWheel(event: MouseWheelEvent): void {
|
2017-09-02 15:14:10 -04:00
|
|
|
event.preventDefault();
|
|
|
|
|
2017-09-11 15:21:50 -04:00
|
|
|
if (!event.ctrlKey) {
|
|
|
|
this.pan(glmatrix.vec2.fromValues(-event.deltaX, event.deltaY));
|
|
|
|
return;
|
2017-09-02 15:14:10 -04:00
|
|
|
}
|
2017-09-11 15:21:50 -04:00
|
|
|
|
|
|
|
// Zoom event: see https://developer.mozilla.org/en-US/docs/Web/Events/wheel
|
|
|
|
const mouseLocation = glmatrix.vec2.fromValues(event.clientX, event.clientY);
|
|
|
|
const canvasLocation = this.canvas.getBoundingClientRect();
|
|
|
|
mouseLocation[0] -= canvasLocation.left;
|
|
|
|
mouseLocation[1] = canvasLocation.bottom - mouseLocation[1];
|
|
|
|
glmatrix.vec2.scale(mouseLocation, mouseLocation, window.devicePixelRatio);
|
|
|
|
|
|
|
|
const scale = 1.0 - event.deltaY * window.devicePixelRatio * ORTHOGRAPHIC_ZOOM_SPEED;
|
|
|
|
this.zoom(scale, mouseLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
private onMouseDown(event: MouseEvent): void {
|
|
|
|
this.canvas.classList.add('pf-grabbing');
|
|
|
|
}
|
|
|
|
|
|
|
|
private onMouseUp(event: MouseEvent): void {
|
|
|
|
this.canvas.classList.remove('pf-grabbing');
|
|
|
|
}
|
|
|
|
|
|
|
|
private onMouseMove(event: MouseEvent): void {
|
|
|
|
if ((event.buttons & 1) !== 0)
|
|
|
|
this.pan(glmatrix.vec2.fromValues(event.movementX, -event.movementY));
|
|
|
|
}
|
|
|
|
|
|
|
|
private pan(delta: glmatrix.vec2): void {
|
|
|
|
// Pan event.
|
|
|
|
glmatrix.vec2.scale(delta, delta, window.devicePixelRatio);
|
|
|
|
glmatrix.vec2.add(this.translation, this.translation, delta);
|
|
|
|
|
|
|
|
if (this.onPan != null)
|
|
|
|
this.onPan();
|
2017-09-02 15:14:10 -04:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:17:24 -04:00
|
|
|
zoomIn(): void {
|
|
|
|
this.zoom(ZOOM_IN_FACTOR, this.centerPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
zoomOut(): void {
|
|
|
|
this.zoom(ZOOM_OUT_FACTOR, this.centerPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private zoom(scale: number, point: glmatrix.vec2): void {
|
|
|
|
const absoluteTranslation = glmatrix.vec2.create();
|
|
|
|
glmatrix.vec2.sub(absoluteTranslation, this.translation, point);
|
|
|
|
glmatrix.vec2.scale(absoluteTranslation, absoluteTranslation, 1.0 / this.scale);
|
|
|
|
|
|
|
|
this.scale *= scale;
|
|
|
|
|
|
|
|
glmatrix.vec2.scale(absoluteTranslation, absoluteTranslation, this.scale);
|
|
|
|
glmatrix.vec2.add(this.translation, absoluteTranslation, point);
|
|
|
|
|
|
|
|
if (this.onZoom != null)
|
|
|
|
this.onZoom();
|
|
|
|
}
|
|
|
|
|
|
|
|
private get centerPoint(): glmatrix.vec2 {
|
|
|
|
return glmatrix.vec2.fromValues(this.canvas.width * 0.5, this.canvas.height * 0.5);
|
|
|
|
}
|
|
|
|
|
2017-09-02 15:14:10 -04:00
|
|
|
onPan: (() => void) | null;
|
|
|
|
onZoom: (() => void) | null;
|
|
|
|
|
|
|
|
translation: glmatrix.vec2;
|
|
|
|
scale: number;
|
|
|
|
}
|
2017-09-02 16:41:08 -04:00
|
|
|
|
|
|
|
export class PerspectiveCamera extends Camera {
|
|
|
|
constructor(canvas: HTMLCanvasElement) {
|
|
|
|
super(canvas);
|
|
|
|
|
2017-09-05 22:47:19 -04:00
|
|
|
this.translation = glmatrix.vec3.clone(INITIAL_TRANSLATION);
|
2017-09-02 16:41:08 -04:00
|
|
|
this.rotation = glmatrix.vec2.create();
|
|
|
|
this.movementDelta = glmatrix.vec3.create();
|
|
|
|
this.movementInterval = null;
|
|
|
|
|
|
|
|
this.canvas.addEventListener('mousedown', event => this.onMouseDown(event), false);
|
|
|
|
this.canvas.addEventListener('mouseup', event => this.onMouseUp(event), false);
|
|
|
|
this.canvas.addEventListener('mousemove', event => this.onMouseMove(event), false);
|
|
|
|
|
|
|
|
this.onChange = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private onMouseDown(event: MouseEvent): void {
|
|
|
|
if (document.pointerLockElement !== this.canvas) {
|
|
|
|
this.canvas.requestPointerLock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-03 22:24:28 -04:00
|
|
|
this.movementDelta = glmatrix.vec3.fromValues(0.0, 0.0, PERSPECTIVE_MOVEMENT_SPEED);
|
2017-09-02 16:41:08 -04:00
|
|
|
if (event.button !== 1)
|
|
|
|
this.movementDelta[0] = -this.movementDelta[0];
|
|
|
|
|
2017-09-03 22:24:28 -04:00
|
|
|
if (this.movementInterval == null)
|
|
|
|
this.movementInterval = window.setInterval(() => this.move(), MOVEMENT_INTERVAL_DELAY);
|
2017-09-02 16:41:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private onMouseUp(event: MouseEvent): void {
|
|
|
|
if (this.movementInterval != null) {
|
|
|
|
window.clearInterval(this.movementInterval);
|
2017-09-03 22:24:28 -04:00
|
|
|
this.movementInterval = null;
|
2017-09-02 16:41:08 -04:00
|
|
|
this.movementDelta = glmatrix.vec3.create();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private move() {
|
2017-09-03 22:24:28 -04:00
|
|
|
const invRotationMatrix = glmatrix.mat4.create();
|
|
|
|
glmatrix.mat4.invert(invRotationMatrix, this.rotationMatrix);
|
|
|
|
|
2017-09-02 16:41:08 -04:00
|
|
|
const delta = glmatrix.vec3.clone(this.movementDelta);
|
2017-09-03 22:24:28 -04:00
|
|
|
glmatrix.vec3.transformMat4(delta, delta, invRotationMatrix);
|
2017-09-02 16:41:08 -04:00
|
|
|
glmatrix.vec3.add(this.translation, this.translation, delta);
|
|
|
|
|
|
|
|
if (this.onChange != null)
|
|
|
|
this.onChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
private onMouseMove(event: MouseEvent): void {
|
|
|
|
if (document.pointerLockElement !== this.canvas)
|
|
|
|
return;
|
|
|
|
|
2017-09-09 03:04:35 -04:00
|
|
|
this.rotation[0] += event.movementX * PERSPECTIVE_ROTATION_SPEED;
|
|
|
|
this.rotation[1] += event.movementY * PERSPECTIVE_ROTATION_SPEED;
|
2017-09-02 16:41:08 -04:00
|
|
|
|
|
|
|
if (this.onChange != null)
|
|
|
|
this.onChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
get rotationMatrix(): glmatrix.mat4 {
|
|
|
|
const matrix = glmatrix.mat4.create();
|
2017-09-09 03:04:35 -04:00
|
|
|
glmatrix.mat4.fromXRotation(matrix, this.rotation[1]);
|
|
|
|
glmatrix.mat4.rotateY(matrix, matrix, this.rotation[0]);
|
2017-09-02 16:41:08 -04:00
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:17:24 -04:00
|
|
|
zoomIn(): void {
|
|
|
|
// TODO(pcwalton)
|
|
|
|
}
|
|
|
|
|
|
|
|
zoomOut(): void {
|
|
|
|
// TODO(pcwalton)
|
|
|
|
}
|
|
|
|
|
2017-09-02 16:41:08 -04:00
|
|
|
onChange: (() => void) | null;
|
|
|
|
|
|
|
|
translation: glmatrix.vec3;
|
|
|
|
|
|
|
|
/// Pitch and yaw Euler angles.
|
|
|
|
rotation: glmatrix.vec2;
|
|
|
|
|
|
|
|
private movementDelta: glmatrix.vec3;
|
|
|
|
private movementInterval: number | null;
|
|
|
|
}
|