2012-08-31 08:45:07 +00:00
|
|
|
/*
|
|
|
|
* @provide pskl.drawingtools.BaseTool
|
|
|
|
*
|
|
|
|
* @require pskl.utils
|
|
|
|
*/
|
|
|
|
(function() {
|
|
|
|
var ns = $.namespace("pskl.drawingtools");
|
|
|
|
|
|
|
|
ns.BaseTool = function() {};
|
|
|
|
|
2012-09-10 21:26:12 +00:00
|
|
|
ns.BaseTool.prototype.applyToolAt = function(col, row, color, frame, overlay) {};
|
2012-08-31 08:45:07 +00:00
|
|
|
|
2012-09-10 21:26:12 +00:00
|
|
|
ns.BaseTool.prototype.moveToolAt = function(col, row, color, frame, overlay) {};
|
2012-09-01 22:44:55 +00:00
|
|
|
|
2012-09-16 13:35:30 +00:00
|
|
|
ns.BaseTool.prototype.moveUnactiveToolAt = function(col, row, color, frame, overlay) {
|
|
|
|
if (overlay.containsPixel(col, row)) {
|
2012-09-16 14:42:21 +00:00
|
|
|
if (!isNaN(this.highlightedPixelCol) &&
|
|
|
|
!isNaN(this.highlightedPixelRow) &&
|
|
|
|
(this.highlightedPixelRow != row ||
|
|
|
|
this.highlightedPixelCol != col)) {
|
2012-09-16 13:35:30 +00:00
|
|
|
|
|
|
|
// Clean the previously highlighted pixel:
|
|
|
|
overlay.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the current pixel targeted by the tool:
|
|
|
|
overlay.setPixel(col, row, Constants.TOOL_TARGET_HIGHLIGHT_COLOR);
|
|
|
|
|
2012-09-16 14:42:21 +00:00
|
|
|
this.highlightedPixelCol = col;
|
|
|
|
this.highlightedPixelRow = row;
|
2012-09-16 13:35:30 +00:00
|
|
|
}
|
|
|
|
};
|
2012-09-13 20:57:32 +00:00
|
|
|
|
2012-09-10 21:26:12 +00:00
|
|
|
ns.BaseTool.prototype.releaseToolAt = function(col, row, color, frame, overlay) {};
|
2012-09-02 15:49:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bresenham line algorihtm: Get an array of pixels from
|
|
|
|
* start and end coordinates.
|
|
|
|
*
|
|
|
|
* http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
|
|
|
|
* http://stackoverflow.com/questions/4672279/bresenham-algorithm-in-javascript
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.BaseTool.prototype.getLinePixels_ = function(x0, x1, y0, y1) {
|
|
|
|
|
|
|
|
var pixels = [];
|
|
|
|
var dx = Math.abs(x1-x0);
|
2013-05-27 21:42:53 +00:00
|
|
|
var dy = Math.abs(y1-y0);
|
|
|
|
var sx = (x0 < x1) ? 1 : -1;
|
|
|
|
var sy = (y0 < y1) ? 1 : -1;
|
|
|
|
var err = dx-dy;
|
2012-09-02 15:49:28 +00:00
|
|
|
|
2013-05-27 21:42:53 +00:00
|
|
|
while(true){
|
2012-09-02 15:49:28 +00:00
|
|
|
|
2013-05-27 21:42:53 +00:00
|
|
|
// Do what you need to for this
|
|
|
|
pixels.push({"col": x0, "row": y0});
|
2012-09-02 15:49:28 +00:00
|
|
|
|
2013-05-27 21:42:53 +00:00
|
|
|
if ((x0==x1) && (y0==y1)) break;
|
|
|
|
var e2 = 2*err;
|
|
|
|
if (e2>-dy){
|
|
|
|
err -= dy;
|
|
|
|
x0 += sx;
|
|
|
|
}
|
|
|
|
if (e2 < dx) {
|
|
|
|
err += dx;
|
|
|
|
y0 += sy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pixels;
|
|
|
|
};
|
2012-08-31 08:45:07 +00:00
|
|
|
})();
|