2014-01-07 22:49:54 +00:00
|
|
|
/**
|
|
|
|
* Handles user input events and modifies state.
|
|
|
|
* Core logic comes through this class.
|
|
|
|
*/
|
2014-01-09 20:18:46 +00:00
|
|
|
goog.provide('ascii.Controller');
|
2014-01-07 22:49:54 +00:00
|
|
|
|
2014-01-11 16:40:01 +00:00
|
|
|
goog.require('ascii.Vector');
|
2014-01-09 20:18:46 +00:00
|
|
|
goog.require('ascii.View');
|
2014-01-07 22:49:54 +00:00
|
|
|
|
2014-01-11 17:41:44 +00:00
|
|
|
/** @const */ var DRAG_LATENCY = 200; // Milliseconds.
|
|
|
|
/** @const */ var DRAG_ACCURACY = 5; // Pixels.
|
|
|
|
|
2014-01-07 22:49:54 +00:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
*/
|
2014-01-11 16:40:01 +00:00
|
|
|
ascii.Controller = function(view, state) {
|
|
|
|
/** @type {ascii.View} */ this.view = view;
|
|
|
|
/** @type {ascii.State} */ this.state = state;
|
|
|
|
|
2014-01-11 17:41:44 +00:00
|
|
|
/** @type {ascii.Vector} */ this.dragOrigin;
|
2014-01-11 16:40:01 +00:00
|
|
|
/** @type {ascii.Vector} */ this.pressVector;
|
2014-01-11 17:41:44 +00:00
|
|
|
/** @type {number} */ this.pressTimestamp;
|
2014-01-07 22:49:54 +00:00
|
|
|
|
|
|
|
this.installDesktopBindings();
|
|
|
|
};
|
|
|
|
|
2014-01-09 20:18:46 +00:00
|
|
|
ascii.Controller.prototype.handlePress = function(x, y) {
|
2014-01-11 16:40:01 +00:00
|
|
|
this.pressVector = new ascii.Vector(x, y);
|
2014-01-11 17:41:44 +00:00
|
|
|
this.pressTimestamp = $.now();
|
2014-01-08 23:06:08 +00:00
|
|
|
};
|
|
|
|
|
2014-01-09 20:18:46 +00:00
|
|
|
ascii.Controller.prototype.handleMove = function(x, y) {
|
2014-01-11 17:41:44 +00:00
|
|
|
var position = new ascii.Vector(x, y);
|
|
|
|
|
|
|
|
if (this.pressVector == null) { return; } // No clicks, so just ignore.
|
|
|
|
|
|
|
|
// Initiate a drag if we have moved enough, quickly enough.
|
|
|
|
if (this.dragOrigin == null &&
|
|
|
|
($.now() - this.pressTimestamp) < DRAG_LATENCY &&
|
|
|
|
position.subtract(this.pressVector).length() > DRAG_ACCURACY) {
|
|
|
|
this.dragOrigin = this.view.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drag in progress, update the view origin.
|
|
|
|
if (this.dragOrigin != null) {
|
|
|
|
this.view.offset = this.dragOrigin.add(
|
|
|
|
this.pressVector
|
|
|
|
.subtract(new ascii.Vector(x, y))
|
|
|
|
.scale(1/this.view.zoom));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drag wasn't initiated in time, treat this as a drawing event.
|
|
|
|
if (this.dragOrigin == null && ($.now() - this.pressTimestamp) >= DRAG_LATENCY) {
|
|
|
|
// TODO: Draw stuff.
|
|
|
|
this.state.getCell(this.view.screenToCell(position)).value = 'O';
|
2014-01-08 23:06:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-09 20:18:46 +00:00
|
|
|
ascii.Controller.prototype.handleRelease = function(x, y) {
|
2014-01-11 16:40:01 +00:00
|
|
|
var position = new ascii.Vector(x, y);
|
2014-01-11 17:41:44 +00:00
|
|
|
// Drag wasn't initiated in time, treat this as a drawing event.
|
|
|
|
if (this.dragOrigin == null &&
|
|
|
|
($.now() - this.pressTimestamp) >= DRAG_LATENCY &&
|
|
|
|
position.subtract(this.pressVector).length() > DRAG_ACCURACY) {
|
|
|
|
// TODO: Draw stuff.
|
2014-01-09 22:38:08 +00:00
|
|
|
}
|
2014-01-11 16:40:01 +00:00
|
|
|
this.pressVector = null;
|
2014-01-11 17:41:44 +00:00
|
|
|
this.pressTimestamp = 0;
|
|
|
|
this.dragOrigin = null;
|
2014-01-07 22:49:54 +00:00
|
|
|
};
|
|
|
|
|
2014-01-09 20:18:46 +00:00
|
|
|
ascii.Controller.prototype.handleZoom = function(delta) {
|
2014-01-07 22:49:54 +00:00
|
|
|
this.view.zoom *= delta > 0 ? 1.1 : 0.9;
|
|
|
|
this.view.zoom = Math.max(Math.min(this.view.zoom, 5), 0.2);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-01-09 20:18:46 +00:00
|
|
|
ascii.Controller.prototype.installDesktopBindings = function() {
|
2014-01-07 22:49:54 +00:00
|
|
|
var controller = this;
|
|
|
|
$(this.view.canvas).bind('mousewheel', function(e) {
|
|
|
|
controller.handleZoom(e.originalEvent.wheelDelta);
|
|
|
|
});
|
2014-01-08 23:06:08 +00:00
|
|
|
$(this.view.canvas).mousedown(function(e) {
|
|
|
|
controller.handlePress(e.clientX, e.clientY);
|
|
|
|
});
|
|
|
|
$(this.view.canvas).mouseup(function(e) {
|
|
|
|
controller.handleRelease(e.clientX, e.clientY);
|
|
|
|
});
|
|
|
|
$(this.view.canvas).mousemove(function(e) {
|
|
|
|
controller.handleMove(e.clientX, e.clientY);
|
2014-01-07 22:49:54 +00:00
|
|
|
});
|
2014-01-11 17:41:44 +00:00
|
|
|
$(window).resize(function(e) { controller.view.resizeCanvas() });
|
2014-01-07 22:49:54 +00:00
|
|
|
};
|