2017-01-21 14:55:18 +00:00
|
|
|
import * as c from './constants';
|
|
|
|
import Controller from './controller';
|
|
|
|
import Vector from './vector';
|
2014-02-23 18:42:20 +00:00
|
|
|
|
2014-02-23 21:06:37 +00:00
|
|
|
/**
|
2017-01-21 14:55:18 +00:00
|
|
|
* Handles desktop inputs, and passes them onto the main controller.
|
2014-02-23 18:42:20 +00:00
|
|
|
*/
|
2017-01-21 14:55:18 +00:00
|
|
|
export class DesktopController {
|
|
|
|
/**
|
|
|
|
* @param {Controller} controller
|
|
|
|
*/
|
|
|
|
constructor(controller) {
|
|
|
|
/** @type {Controller} */ this.controller = controller;
|
|
|
|
/** @type {boolean} */ this.isDragging = false;
|
|
|
|
|
|
|
|
this.installBindings();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} delta
|
|
|
|
*/
|
|
|
|
handleZoom(delta) {
|
|
|
|
var newzoom = this.controller.view.zoom * (delta > 0 ? 1.1 : 0.9);
|
|
|
|
newzoom = Math.max(Math.min(newzoom, 5), 0.2);
|
|
|
|
this.controller.view.setZoom(newzoom);
|
|
|
|
}
|
2014-02-23 18:42:20 +00:00
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/**
|
|
|
|
* Installs input bindings associated with keyboard controls.
|
|
|
|
*/
|
|
|
|
installBindings() {
|
2017-10-04 09:07:05 +00:00
|
|
|
var canvas = $(this.controller.view.canvas);
|
|
|
|
canvas.on('mousewheel', e => {
|
2017-01-21 14:55:18 +00:00
|
|
|
this.handleZoom(e.originalEvent.wheelDelta);
|
|
|
|
});
|
|
|
|
|
2017-10-04 09:07:05 +00:00
|
|
|
canvas.mousedown(e => {
|
2017-01-21 14:55:18 +00:00
|
|
|
// Can drag by holding either the control or meta (Apple) key.
|
|
|
|
if (e.ctrlKey || e.metaKey) {
|
2017-10-04 09:07:05 +00:00
|
|
|
this.controller.startDrag(Vector.fromMouseEvent(e));
|
2017-01-21 14:55:18 +00:00
|
|
|
} else {
|
2017-10-04 09:07:05 +00:00
|
|
|
this.controller.startDraw(Vector.fromMouseEvent(e));
|
2017-01-21 14:55:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pass these events through to the main controller.
|
2017-10-04 09:07:05 +00:00
|
|
|
canvas.mouseup(e => {
|
2017-01-21 14:55:18 +00:00
|
|
|
this.controller.endAll();
|
|
|
|
});
|
|
|
|
|
2017-10-04 09:07:05 +00:00
|
|
|
canvas.mouseleave(e => {
|
2017-01-21 14:55:18 +00:00
|
|
|
this.controller.endAll();
|
|
|
|
});
|
|
|
|
|
2017-10-04 09:07:05 +00:00
|
|
|
canvas.mousemove(e => {
|
|
|
|
this.controller.handleMove(Vector.fromMouseEvent(e));
|
2017-01-21 14:55:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-02-23 18:42:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles touch inputs, and passes them onto the main controller.
|
|
|
|
*/
|
2017-01-21 14:55:18 +00:00
|
|
|
export class TouchController {
|
|
|
|
/**
|
|
|
|
* @param {Controller} controller
|
|
|
|
*/
|
|
|
|
constructor(controller) {
|
|
|
|
/** @type {Controller} */ this.controller = controller;
|
2014-02-23 18:42:20 +00:00
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/** @type {Vector} */ this.pressVector;
|
2014-02-23 21:06:37 +00:00
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/** @type {number} */ this.originalZoom;
|
|
|
|
/** @type {number} */ this.zoomLength;
|
2014-02-23 21:06:37 +00:00
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/** @type {number} */ this.pressTimestamp;
|
|
|
|
/** @type {boolean} */ this.dragStarted = false;
|
|
|
|
/** @type {boolean} */ this.zoomStarted = false;
|
2014-02-23 18:42:20 +00:00
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
this.installBindings();
|
|
|
|
}
|
2014-02-23 18:42:20 +00:00
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/**
|
|
|
|
* @param {Vector} position
|
|
|
|
*/
|
|
|
|
handlePress(position) {
|
|
|
|
this.pressVector = position;
|
|
|
|
this.pressTimestamp = $.now();
|
|
|
|
this.dragStarted = false;
|
|
|
|
|
|
|
|
// If a drag or zoom didn't start and if we didn't release already, then handle it as a draw.
|
|
|
|
window.setTimeout(() => {
|
|
|
|
if (!this.dragStarted && !this.zoomStarted && this.pressVector != null) {
|
|
|
|
this.controller.startDraw(position);
|
|
|
|
}
|
|
|
|
}, c.DRAG_LATENCY);
|
|
|
|
}
|
2014-02-23 18:42:20 +00:00
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/**
|
|
|
|
* The multi-touch version of handlePress.
|
|
|
|
* @param {Vector} positionOne
|
|
|
|
* @param {Vector} positionTwo
|
|
|
|
*/
|
|
|
|
handlePressMulti(positionOne, positionTwo) {
|
|
|
|
// A second finger as been placed, cancel whatever we were doing.
|
|
|
|
this.controller.endAll();
|
|
|
|
this.zoomStarted = true;
|
|
|
|
this.dragStarted = false;
|
|
|
|
this.zoomLength = positionOne.subtract(positionTwo).length();
|
|
|
|
this.originalZoom = this.controller.view.zoom;
|
|
|
|
}
|
2014-02-23 21:06:37 +00:00
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/**
|
|
|
|
* @param {Vector} position
|
|
|
|
*/
|
|
|
|
handleMove(position) {
|
|
|
|
// Initiate a drag if we have moved enough, quickly enough.
|
|
|
|
if (!this.dragStarted &&
|
|
|
|
($.now() - this.pressTimestamp) < c.DRAG_LATENCY &&
|
|
|
|
position.subtract(this.pressVector).length() > c.DRAG_ACCURACY) {
|
|
|
|
this.dragStarted = true;
|
|
|
|
this.controller.startDrag(position);
|
|
|
|
}
|
|
|
|
// Pass on the event.
|
|
|
|
this.controller.handleMove(position);
|
2014-02-23 18:42:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/**
|
|
|
|
* The multi-touch version of handleMove, effectively only deals with zooming.
|
|
|
|
* @param {Vector} positionOne
|
|
|
|
* @param {Vector} positionTwo
|
|
|
|
*/
|
|
|
|
handleMoveMulti(positionOne, positionTwo) {
|
|
|
|
if (this.zoomStarted) {
|
|
|
|
var newZoom = this.originalZoom *
|
|
|
|
positionOne.subtract(positionTwo).length() / this.zoomLength;
|
|
|
|
newZoom = Math.max(Math.min(newZoom, 5), 0.5);
|
|
|
|
this.controller.view.setZoom(newZoom);
|
|
|
|
}
|
2014-02-23 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/**
|
|
|
|
* Ends all current actions, cleans up any state.
|
|
|
|
*/
|
|
|
|
reset() {
|
|
|
|
this.dragStarted = false;
|
|
|
|
this.zoomStarted = false;
|
|
|
|
this.pressVector = null;
|
|
|
|
}
|
2014-02-23 21:06:37 +00:00
|
|
|
|
2017-01-21 14:55:18 +00:00
|
|
|
/**
|
|
|
|
* Installs input bindings associated with touch controls.
|
|
|
|
*/
|
|
|
|
installBindings() {
|
2017-10-04 09:07:05 +00:00
|
|
|
var canvas = $(this.controller.view.canvas);
|
2017-01-21 14:55:18 +00:00
|
|
|
|
2017-10-04 09:07:05 +00:00
|
|
|
canvas.on('touchstart', e => {
|
2017-01-21 14:55:18 +00:00
|
|
|
e.preventDefault();
|
|
|
|
if (e.originalEvent.touches.length == 1) {
|
2017-10-04 09:07:05 +00:00
|
|
|
this.handlePress(Vector.fromTouchEvent(e));
|
2017-01-21 14:55:18 +00:00
|
|
|
} else if (e.originalEvent.touches.length > 1) {
|
2017-10-04 09:07:05 +00:00
|
|
|
this.handlePressMulti(
|
|
|
|
Vector.fromTouchEvent(e, 0),
|
|
|
|
Vector.fromTouchEvent(e, 1));
|
2017-01-21 14:55:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-04 09:07:05 +00:00
|
|
|
canvas.on('touchmove', e => {
|
2017-01-21 14:55:18 +00:00
|
|
|
e.preventDefault();
|
|
|
|
if (e.originalEvent.touches.length == 1) {
|
2017-10-04 09:07:05 +00:00
|
|
|
this.handleMove(Vector.fromTouchEvent(e));
|
2017-01-21 14:55:18 +00:00
|
|
|
} else if (e.originalEvent.touches.length > 1) {
|
2017-10-04 09:07:05 +00:00
|
|
|
this.handleMoveMulti(
|
|
|
|
Vector.fromTouchEvent(e, 0),
|
|
|
|
Vector.fromTouchEvent(e, 1));
|
2017-01-21 14:55:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pass through, no special handling.
|
2017-10-04 09:07:05 +00:00
|
|
|
canvas.on('touchend', e => {
|
2017-01-21 14:55:18 +00:00
|
|
|
e.preventDefault();
|
|
|
|
this.reset();
|
|
|
|
this.controller.endAll();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|