2014-01-11 18:53:45 +00:00
|
|
|
goog.provide('ascii.StateController');
|
|
|
|
|
|
|
|
goog.require('ascii.Vector');
|
|
|
|
|
2014-01-18 00:28:46 +00:00
|
|
|
/**
|
|
|
|
* Draws a line.
|
|
|
|
*/
|
|
|
|
function drawLine(state, startPosition, endPosition, clockwise) {
|
|
|
|
var hX1 = Math.min(startPosition.x, endPosition.x);
|
|
|
|
var vY1 = Math.min(startPosition.y, endPosition.y);
|
|
|
|
var hX2 = Math.max(startPosition.x, endPosition.x);
|
|
|
|
var vY2 = Math.max(startPosition.y, endPosition.y);
|
|
|
|
|
|
|
|
var hY = clockwise ? startPosition.y : endPosition.y;
|
|
|
|
var vX = clockwise ? endPosition.x : startPosition.x;
|
|
|
|
|
|
|
|
while (hX1++ < hX2) {
|
|
|
|
state.drawSpecial(new ascii.Vector(hX1, hY));
|
|
|
|
}
|
|
|
|
while (vY1++ < vY2) {
|
|
|
|
state.drawSpecial(new ascii.Vector(vX, vY1));
|
|
|
|
}
|
|
|
|
state.drawSpecial(startPosition);
|
|
|
|
state.drawSpecial(endPosition);
|
|
|
|
state.drawSpecial(new ascii.Vector(vX, hY));
|
|
|
|
};
|
|
|
|
|
2014-01-12 16:51:16 +00:00
|
|
|
/**
|
|
|
|
* Common interface for different drawing functions, e.g. box, line, etc.
|
|
|
|
* @interface
|
|
|
|
*/
|
|
|
|
function DrawFunction() {}
|
|
|
|
/** Start of drawing. @param {ascii.Vector} position */
|
|
|
|
DrawFunction.prototype.start = function(position) {};
|
|
|
|
/** Drawing move. @param {ascii.Vector} position */
|
|
|
|
DrawFunction.prototype.move = function(position) {};
|
|
|
|
/** End of drawing. @param {ascii.Vector} position */
|
|
|
|
DrawFunction.prototype.end = function(position) {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @implements {DrawFunction}
|
|
|
|
* @param {ascii.State} state
|
|
|
|
*/
|
|
|
|
function DrawBox(state) {
|
|
|
|
this.state = state;
|
|
|
|
/** @type {ascii.Vector} */ this.startPosition = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawBox.prototype.start = function(position) {
|
|
|
|
this.startPosition = position;
|
|
|
|
};
|
|
|
|
DrawBox.prototype.move = function(position) {
|
|
|
|
this.endPosition = position;
|
|
|
|
this.state.clearDraw();
|
2014-01-18 00:28:46 +00:00
|
|
|
drawLine(this.state, this.startPosition, position, true);
|
|
|
|
drawLine(this.state, this.startPosition, position, false);
|
2014-01-12 16:51:16 +00:00
|
|
|
};
|
|
|
|
DrawBox.prototype.end = function(position) {
|
|
|
|
this.state.commitDraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-01-12 19:08:29 +00:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @implements {DrawFunction}
|
|
|
|
* @param {ascii.State} state
|
|
|
|
*/
|
|
|
|
function DrawLine(state) {
|
|
|
|
this.state = state;
|
|
|
|
/** @type {ascii.Vector} */ this.startPosition = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawLine.prototype.start = function(position) {
|
|
|
|
this.startPosition = position;
|
|
|
|
};
|
|
|
|
DrawLine.prototype.move = function(position) {
|
|
|
|
this.state.clearDraw();
|
2014-01-18 00:28:46 +00:00
|
|
|
|
|
|
|
// Try to infer line orientation.
|
|
|
|
var startContext = this.state.getContext(this.startPosition);
|
|
|
|
var endContext = this.state.getContext(position);
|
|
|
|
var clockwise = (startContext.up && startContext.down) ||
|
|
|
|
(endContext.left && endContext.right);
|
|
|
|
|
|
|
|
drawLine(this.state, this.startPosition, position, clockwise);
|
2014-01-12 19:08:29 +00:00
|
|
|
};
|
2014-01-18 00:28:46 +00:00
|
|
|
|
2014-01-12 19:08:29 +00:00
|
|
|
DrawLine.prototype.end = function(position) {
|
|
|
|
this.state.commitDraw();
|
|
|
|
};
|
|
|
|
|
2014-01-12 16:51:16 +00:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @implements {DrawFunction}
|
|
|
|
* @param {ascii.State} state
|
2014-01-13 23:17:02 +00:00
|
|
|
* @param {?string} value
|
2014-01-12 16:51:16 +00:00
|
|
|
*/
|
2014-01-13 23:17:02 +00:00
|
|
|
function DrawFreeform(state, value) {
|
2014-01-12 16:51:16 +00:00
|
|
|
this.state = state;
|
2014-01-13 23:17:02 +00:00
|
|
|
this.value = value;
|
2014-01-12 16:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DrawFreeform.prototype.start = function(position) {
|
2014-01-13 23:17:02 +00:00
|
|
|
this.state.setValue(position, this.value);
|
2014-01-12 16:51:16 +00:00
|
|
|
};
|
|
|
|
DrawFreeform.prototype.move = function(position) {
|
2014-01-13 23:17:02 +00:00
|
|
|
this.state.setValue(position, this.value);
|
2014-01-12 16:51:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DrawFreeform.prototype.end = function(position) {
|
|
|
|
};
|
|
|
|
|
2014-01-18 00:28:46 +00:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @implements {DrawFunction}
|
|
|
|
* @param {ascii.State} state
|
|
|
|
*/
|
|
|
|
function DrawMove(state) {
|
|
|
|
this.state = state;
|
|
|
|
this.ends = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawMove.prototype.start = function(position) {
|
|
|
|
var context = this.state.getContext(position);
|
|
|
|
var directions = [
|
|
|
|
new ascii.Vector(1, 0),
|
|
|
|
new ascii.Vector(-1, 0),
|
|
|
|
new ascii.Vector(0, 1),
|
|
|
|
new ascii.Vector(0, -1) ];
|
|
|
|
|
|
|
|
var ends = [];
|
|
|
|
for (var i in directions) {
|
|
|
|
var midPoint = this.followLine(position, directions[i]);
|
|
|
|
// Clockwise is a lie, it is true if we move vertically first.
|
|
|
|
var clockwise = (directions[i].x != 0);
|
|
|
|
// Ignore any directions that didn't go anywhere.
|
|
|
|
if (position.equals(midPoint)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var midPointContext = this.state.getContext(midPoint);
|
|
|
|
// Special case, a straight line with no turns.
|
|
|
|
if ((midPointContext.left + midPointContext.right +
|
|
|
|
midPointContext.up + midPointContext.down) == 1) {
|
|
|
|
ends.push({position: midPoint, clockwise: clockwise});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Continue following lines from the midpoint.
|
|
|
|
for (var j in directions) {
|
|
|
|
if (directions[i].add(directions[j]).length() == 0) {
|
|
|
|
// Don't go back on ourselves.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var end = this.followLine(midPoint, directions[j]);
|
|
|
|
// Ignore any directions that didn't go anywhere.
|
|
|
|
if (midPoint.equals(end)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ends.push({position: end, clockwise: clockwise});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.ends = ends;
|
|
|
|
};
|
|
|
|
|
|
|
|
DrawMove.prototype.move = function(position) {
|
|
|
|
this.state.clearDraw();
|
|
|
|
for (var i in this.ends) {
|
|
|
|
drawLine(this.state, position, this.ends[i].position, this.ends[i].clockwise);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DrawMove.prototype.end = function(position) {
|
|
|
|
this.state.commitDraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
DrawMove.prototype.followLine = function(startPosition, direction) {
|
|
|
|
var endPosition = startPosition.clone();
|
|
|
|
while (true) {
|
|
|
|
var nextEnd = endPosition.add(direction);
|
|
|
|
if (!this.state.isSpecial(nextEnd)) {
|
|
|
|
return endPosition;
|
|
|
|
}
|
|
|
|
endPosition = nextEnd;
|
|
|
|
var context = this.state.getContext(nextEnd);
|
|
|
|
if (!(context.left && context.right && !context.up && !context.down) &&
|
|
|
|
!(!context.left && !context.right && context.up && context.down)) {
|
|
|
|
return endPosition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-11 18:53:45 +00:00
|
|
|
/**
|
2014-01-12 10:37:38 +00:00
|
|
|
* Handles management of the diagram state. Input events are cleaned in the
|
|
|
|
* parent controller and passed down to this class for dealing with drawing.
|
|
|
|
*
|
2014-01-11 18:53:45 +00:00
|
|
|
* @constructor
|
2014-01-12 10:37:38 +00:00
|
|
|
* @param {ascii.State} state
|
2014-01-11 18:53:45 +00:00
|
|
|
*/
|
|
|
|
ascii.StateController = function(state) {
|
|
|
|
/** @type {ascii.State} */ this.state = state;
|
2014-01-12 16:51:16 +00:00
|
|
|
/** @type {DrawFunction} */ this.drawFunction = new DrawBox(state);
|
2014-01-12 19:08:29 +00:00
|
|
|
|
|
|
|
$('#box-button').click(function(e) {
|
|
|
|
this.drawFunction = new DrawBox(state);
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
$('#line-button').click(function(e) {
|
|
|
|
this.drawFunction = new DrawLine(state);
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
$('#freeform-button').click(function(e) {
|
2014-01-13 23:17:02 +00:00
|
|
|
this.drawFunction = new DrawFreeform(state, 'O');
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
$('#erase-button').click(function(e) {
|
|
|
|
this.drawFunction = new DrawFreeform(state, null);
|
2014-01-12 19:08:29 +00:00
|
|
|
}.bind(this));
|
2014-01-18 00:28:46 +00:00
|
|
|
|
|
|
|
$('#move-button').click(function(e) {
|
|
|
|
this.drawFunction = new DrawMove(state);
|
|
|
|
}.bind(this));
|
2014-01-11 18:53:45 +00:00
|
|
|
};
|
|
|
|
|
2014-01-12 10:37:38 +00:00
|
|
|
/**
|
|
|
|
* Handles a press in the context of the drawing frame.
|
|
|
|
* @param {ascii.Vector} position
|
|
|
|
*/
|
2014-01-11 18:53:45 +00:00
|
|
|
ascii.StateController.prototype.handleDrawingPress = function(position) {
|
2014-01-12 16:51:16 +00:00
|
|
|
this.drawFunction.start(position);
|
2014-01-11 18:53:45 +00:00
|
|
|
};
|
|
|
|
|
2014-01-12 10:37:38 +00:00
|
|
|
/**
|
|
|
|
* Handles a release in the context of the drawing frame.
|
|
|
|
* @param {ascii.Vector} position
|
|
|
|
*/
|
2014-01-11 18:53:45 +00:00
|
|
|
ascii.StateController.prototype.handleDrawingRelease = function(position) {
|
2014-01-12 16:51:16 +00:00
|
|
|
this.drawFunction.end(position);
|
2014-01-11 18:53:45 +00:00
|
|
|
};
|
|
|
|
|
2014-01-12 10:37:38 +00:00
|
|
|
/**
|
|
|
|
* Handles a move in the context of the drawing frame.
|
|
|
|
* @param {ascii.Vector} position
|
|
|
|
*/
|
2014-01-11 18:53:45 +00:00
|
|
|
ascii.StateController.prototype.handleDrawingMove = function(position) {
|
2014-01-12 16:51:16 +00:00
|
|
|
this.drawFunction.move(position);
|
2014-01-11 18:53:45 +00:00
|
|
|
};
|