2012-08-31 12:45:07 +04:00
|
|
|
/*
|
|
|
|
* @provide pskl.drawingtools.SimplePen
|
|
|
|
*
|
|
|
|
* @require pskl.utils
|
|
|
|
*/
|
|
|
|
(function() {
|
|
|
|
var ns = $.namespace("pskl.drawingtools");
|
|
|
|
|
|
|
|
ns.SimplePen = function() {
|
2012-09-04 16:10:16 +04:00
|
|
|
this.toolId = "tool-pen";
|
2012-08-31 12:45:07 +04:00
|
|
|
};
|
|
|
|
|
2012-09-02 19:49:28 +04:00
|
|
|
this.previousCol = null;
|
|
|
|
this.previousRow = null;
|
|
|
|
|
2012-08-31 12:45:07 +04:00
|
|
|
pskl.utils.inherit(ns.SimplePen, ns.BaseTool);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2012-09-04 16:10:16 +04:00
|
|
|
ns.SimplePen.prototype.applyToolAt = function(col, row, color, drawer) {
|
2012-09-05 00:18:00 +04:00
|
|
|
if (drawer.frame.containsPixel(col, row)) {
|
|
|
|
this.previousCol = col;
|
|
|
|
this.previousRow = row;
|
|
|
|
drawer.frame.setPixel(col, row, color);
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2012-09-05 00:18:00 +04:00
|
|
|
// Draw on canvas:
|
|
|
|
// TODO: Remove that when we have the centralized redraw loop
|
|
|
|
drawer.renderFramePixel(col, row);
|
|
|
|
}
|
2012-08-31 12:45:07 +04:00
|
|
|
};
|
|
|
|
|
2012-09-04 16:10:16 +04:00
|
|
|
ns.SimplePen.prototype.moveToolAt = function(col, row, color, drawer) {
|
2012-09-02 19:49:28 +04:00
|
|
|
|
|
|
|
if((Math.abs(col - this.previousCol) > 1) || (Math.abs(row - this.previousRow) > 1)) {
|
|
|
|
// The pen movement is too fast for the mousemove frequency, there is a gap between the
|
|
|
|
// current point and the previously drawn one.
|
|
|
|
// We fill the gap by calculating missing dots (simple linear interpolation) and draw them.
|
|
|
|
var interpolatedPixels = this.getLinePixels_(col, this.previousCol, row, this.previousRow);
|
|
|
|
for(var i=0, l=interpolatedPixels.length; i<l; i++) {
|
2012-09-04 16:10:16 +04:00
|
|
|
this.applyToolAt(interpolatedPixels[i].col, interpolatedPixels[i].row, color, drawer);
|
2012-09-02 19:49:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-09-04 16:10:16 +04:00
|
|
|
this.applyToolAt(col, row, color, drawer);
|
2012-09-02 19:49:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.previousCol = col;
|
|
|
|
this.previousRow = row;
|
2012-08-31 12:45:07 +04:00
|
|
|
};
|
|
|
|
})();
|