2013-09-28 23:10:12 +04:00
|
|
|
/**
|
2012-08-31 12:45:07 +04:00
|
|
|
* @provide pskl.drawingtools.BaseTool
|
|
|
|
*
|
|
|
|
* @require pskl.utils
|
|
|
|
*/
|
|
|
|
(function() {
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl.drawingtools");
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2014-04-17 03:27:49 +04:00
|
|
|
ns.BaseTool = function() {
|
|
|
|
this.toolId = "tool-base";
|
|
|
|
};
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2013-12-06 01:12:48 +04:00
|
|
|
ns.BaseTool.prototype.applyToolAt = function(col, row, color, frame, overlay, event) {};
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2014-04-17 03:27:49 +04:00
|
|
|
ns.BaseTool.prototype.moveToolAt = function(col, row, color, frame, overlay, event) {};
|
|
|
|
|
|
|
|
ns.BaseTool.prototype.replay = Constants.ABSTRACT_FUNCTION;
|
2012-09-02 02:44:55 +04:00
|
|
|
|
2013-12-06 01:12:48 +04:00
|
|
|
ns.BaseTool.prototype.moveUnactiveToolAt = function(col, row, color, frame, overlay, event) {
|
2014-04-12 01:32:28 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
if (overlay.containsPixel(col, row)) {
|
|
|
|
if (!isNaN(this.highlightedPixelCol) &&
|
|
|
|
!isNaN(this.highlightedPixelRow) &&
|
|
|
|
(this.highlightedPixelRow != row ||
|
|
|
|
this.highlightedPixelCol != col)) {
|
2012-09-16 17:35:30 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
// Clean the previously highlighted pixel:
|
|
|
|
overlay.clear();
|
|
|
|
}
|
2012-09-16 17:35:30 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
// Show the current pixel targeted by the tool:
|
|
|
|
overlay.setPixel(col, row, Constants.TOOL_TARGET_HIGHLIGHT_COLOR);
|
2012-09-16 17:35:30 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
this.highlightedPixelCol = col;
|
2013-09-29 01:52:51 +04:00
|
|
|
this.highlightedPixelRow = row;
|
2014-04-03 00:37:01 +04:00
|
|
|
} else {
|
|
|
|
this.hideHighlightedPixel(overlay);
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
};
|
2012-09-14 00:57:32 +04:00
|
|
|
|
2013-12-19 02:37:17 +04:00
|
|
|
ns.BaseTool.prototype.hideHighlightedPixel = function(overlay) {
|
2013-12-23 18:04:13 +04:00
|
|
|
if (this.highlightedPixelRow !== null && this.highlightedPixelCol !== null) {
|
2014-03-08 20:23:20 +04:00
|
|
|
try {
|
|
|
|
overlay.setPixel(this.highlightedPixelCol, this.highlightedPixelRow, Constants.TRANSPARENT_COLOR);
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('ns.BaseTool.prototype.hideHighlightedPixel failed');
|
|
|
|
}
|
2013-12-23 18:04:13 +04:00
|
|
|
this.highlightedPixelRow = null;
|
|
|
|
this.highlightedPixelCol = null;
|
|
|
|
}
|
2013-12-19 02:37:17 +04:00
|
|
|
};
|
|
|
|
|
2014-04-20 15:15:30 +04:00
|
|
|
ns.BaseTool.prototype.raiseSaveStateEvent = function (replayData) {
|
|
|
|
$.publish(Events.PISKEL_SAVE_STATE, {
|
2014-04-23 01:57:30 +04:00
|
|
|
type : pskl.service.HistoryService.REPLAY,
|
2014-04-20 15:15:30 +04:00
|
|
|
scope : this,
|
|
|
|
replay : replayData
|
|
|
|
});
|
2014-04-17 03:27:49 +04:00
|
|
|
};
|
|
|
|
|
2013-12-19 02:37:17 +04:00
|
|
|
|
2013-12-06 01:12:48 +04:00
|
|
|
ns.BaseTool.prototype.releaseToolAt = function(col, row, color, frame, overlay, event) {};
|
2012-09-02 19:49:28 +04:00
|
|
|
|
2013-08-10 14:11:16 +04: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) {
|
2014-05-21 00:42:39 +04:00
|
|
|
x1 = pskl.utils.normalize(x1, 0);
|
|
|
|
y1 = pskl.utils.normalize(y1, 0);
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var pixels = [];
|
|
|
|
var dx = Math.abs(x1-x0);
|
|
|
|
var dy = Math.abs(y1-y0);
|
|
|
|
var sx = (x0 < x1) ? 1 : -1;
|
|
|
|
var sy = (y0 < y1) ? 1 : -1;
|
|
|
|
var err = dx-dy;
|
2014-05-21 00:42:39 +04:00
|
|
|
var it = 0;
|
2013-08-10 14:11:16 +04:00
|
|
|
while(true){
|
2012-09-02 19:49:28 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
// Do what you need to for this
|
|
|
|
pixels.push({"col": x0, "row": y0});
|
2012-09-02 19:49:28 +04:00
|
|
|
|
2013-09-29 01:52:51 +04:00
|
|
|
if ((x0==x1) && (y0==y1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var e2 = 2*err;
|
|
|
|
if (e2>-dy){
|
|
|
|
err -= dy;
|
|
|
|
x0 += sx;
|
|
|
|
}
|
|
|
|
if (e2 < dx) {
|
|
|
|
err += dx;
|
|
|
|
y0 += sy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pixels;
|
|
|
|
};
|
2012-08-31 12:45:07 +04:00
|
|
|
})();
|