2012-09-02 15:19:20 +04:00
|
|
|
/*
|
|
|
|
* @provide pskl.drawingtools.Rectangle
|
|
|
|
*
|
|
|
|
* @require pskl.utils
|
|
|
|
*/
|
|
|
|
(function() {
|
|
|
|
var ns = $.namespace("pskl.drawingtools");
|
|
|
|
|
|
|
|
ns.Rectangle = function() {
|
|
|
|
this.toolId = "tool-rectangle"
|
|
|
|
|
|
|
|
// Rectangle's first point coordinates (set in applyToolAt)
|
|
|
|
this.startCol = null;
|
|
|
|
this.startRow = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
pskl.utils.inherit(ns.Rectangle, ns.BaseTool);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2012-09-04 16:10:16 +04:00
|
|
|
ns.Rectangle.prototype.applyToolAt = function(col, row, color, drawer) {
|
2012-09-02 15:19:20 +04:00
|
|
|
this.startCol = col;
|
|
|
|
this.startRow = row;
|
|
|
|
|
|
|
|
// Drawing the first point of the rectangle in the fake overlay canvas:
|
2012-09-08 17:21:22 +04:00
|
|
|
drawer.overlayFrame.setPixel(col, row, color);
|
2012-09-04 16:10:16 +04:00
|
|
|
drawer.renderOverlay();
|
2012-09-02 15:19:20 +04:00
|
|
|
};
|
|
|
|
|
2012-09-04 16:10:16 +04:00
|
|
|
ns.Rectangle.prototype.moveToolAt = function(col, row, color, drawer) {
|
2012-09-02 15:19:20 +04:00
|
|
|
// Clean overlay canvas:
|
2012-09-04 16:10:16 +04:00
|
|
|
drawer.clearOverlay();
|
|
|
|
|
|
|
|
// When the user moussemove (before releasing), we dynamically compute the
|
|
|
|
// pixel to draw the line and draw this line in the overlay :
|
|
|
|
var strokePoints = this.getRectanglePixels_(this.startCol, col, this.startRow, row);
|
2012-09-02 15:19:20 +04:00
|
|
|
|
|
|
|
// Drawing current stroke:
|
|
|
|
for(var i = 0; i< strokePoints.length; i++) {
|
|
|
|
|
|
|
|
if(color == Constants.TRANSPARENT_COLOR) {
|
2012-09-03 22:45:25 +04:00
|
|
|
color = Constants.SELECTION_TRANSPARENT_COLOR;
|
2012-09-02 15:19:20 +04:00
|
|
|
}
|
2012-09-08 17:21:22 +04:00
|
|
|
drawer.overlayFrame.setPixel(strokePoints[i].col, strokePoints[i].row, color);
|
2012-09-02 15:19:20 +04:00
|
|
|
}
|
2012-09-04 16:10:16 +04:00
|
|
|
drawer.renderOverlay();
|
2012-09-02 15:19:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2012-09-04 16:10:16 +04:00
|
|
|
ns.Rectangle.prototype.releaseToolAt = function(col, row, color, drawer) {
|
2012-09-02 15:19:20 +04:00
|
|
|
// If the stroke tool is released outside of the canvas, we cancel the stroke:
|
2012-09-05 00:18:00 +04:00
|
|
|
if(drawer.frame.containsPixel(col, row)) {
|
2012-09-04 16:10:16 +04:00
|
|
|
var strokePoints = this.getRectanglePixels_(this.startCol, col, this.startRow, row);
|
|
|
|
for(var i = 0; i< strokePoints.length; i++) {
|
|
|
|
// Change model:
|
|
|
|
drawer.frame.setPixel(strokePoints[i].col, strokePoints[i].row, color);
|
|
|
|
}
|
|
|
|
// The user released the tool to draw a line. We will compute the pixel coordinate, impact
|
|
|
|
// the model and draw them in the drawing canvas (not the fake overlay anymore)
|
2012-09-02 15:19:20 +04:00
|
|
|
// Draw in canvas:
|
|
|
|
// TODO: Remove that when we have the centralized redraw loop
|
2012-09-04 16:10:16 +04:00
|
|
|
drawer.renderFrame();
|
2012-09-02 15:19:20 +04:00
|
|
|
}
|
2012-09-04 16:10:16 +04:00
|
|
|
drawer.clearOverlay();
|
2012-09-02 15:19:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of pixels representing the rectangle.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.Rectangle.prototype.getRectanglePixels_ = function(x0, x1, y0, y1) {
|
|
|
|
|
|
|
|
var pixels = [];
|
|
|
|
var swap;
|
|
|
|
|
|
|
|
if(x0 > x1) {
|
|
|
|
swap = x0;
|
|
|
|
x0 = x1;
|
|
|
|
x1 = swap;
|
|
|
|
}
|
|
|
|
if(y0 > y1) {
|
|
|
|
swap = y0;
|
|
|
|
y0 = y1;
|
|
|
|
y1 = swap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creating horizontal sides of the rectangle:
|
|
|
|
for(var x = x0; x <= x1; x++) {
|
|
|
|
pixels.push({"col": x, "row": y0});
|
|
|
|
pixels.push({"col": x, "row": y1});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creating vertical sides of the rectangle:
|
|
|
|
for(var y = y0; y <= y1; y++) {
|
|
|
|
pixels.push({"col": x0, "row": y});
|
|
|
|
pixels.push({"col": x1, "row": y});
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixels;
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|