2012-09-01 22:44:55 +00:00
|
|
|
/*
|
2012-09-02 11:19:20 +00:00
|
|
|
* @provide pskl.drawingtools.Stroke
|
2012-09-01 22:44:55 +00:00
|
|
|
*
|
|
|
|
* @require pskl.utils
|
|
|
|
*/
|
|
|
|
(function() {
|
|
|
|
var ns = $.namespace("pskl.drawingtools");
|
|
|
|
|
|
|
|
ns.Stroke = function() {
|
2012-09-15 18:25:45 +00:00
|
|
|
this.toolId = "tool-stroke";
|
|
|
|
this.helpText = "Stroke tool";
|
|
|
|
|
2012-09-01 22:44:55 +00:00
|
|
|
// Stroke's first point coordinates (set in applyToolAt)
|
|
|
|
this.startCol = null;
|
|
|
|
this.startRow = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
pskl.utils.inherit(ns.Stroke, ns.BaseTool);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2012-09-10 21:26:12 +00:00
|
|
|
ns.Stroke.prototype.applyToolAt = function(col, row, color, frame, overlay) {
|
2012-09-01 22:44:55 +00:00
|
|
|
this.startCol = col;
|
|
|
|
this.startRow = row;
|
|
|
|
|
|
|
|
// When drawing a stroke we don't change the model instantly, since the
|
|
|
|
// user can move his cursor to change the stroke direction and length
|
|
|
|
// dynamically. Instead we draw the (preview) stroke in a fake canvas that
|
|
|
|
// overlay the drawing canvas.
|
|
|
|
// We wait for the releaseToolAt callback to impact both the
|
|
|
|
// frame model and canvas rendering.
|
|
|
|
|
|
|
|
// The fake canvas where we will draw the preview of the stroke:
|
|
|
|
// Drawing the first point of the stroke in the fake overlay canvas:
|
2012-09-10 21:26:12 +00:00
|
|
|
overlay.setPixel(col, row, color);
|
2012-09-01 22:44:55 +00:00
|
|
|
};
|
|
|
|
|
2012-09-10 21:26:12 +00:00
|
|
|
ns.Stroke.prototype.moveToolAt = function(col, row, color, frame, overlay) {
|
|
|
|
overlay.clear();
|
2012-09-05 22:35:02 +00:00
|
|
|
|
2012-09-01 22:44:55 +00:00
|
|
|
// When the user moussemove (before releasing), we dynamically compute the
|
|
|
|
// pixel to draw the line and draw this line in the overlay canvas:
|
2012-09-04 12:10:16 +00:00
|
|
|
var strokePoints = this.getLinePixels_(this.startCol, col, this.startRow, row);
|
2012-09-01 22:44:55 +00:00
|
|
|
|
|
|
|
// Drawing current stroke:
|
2012-09-01 23:41:49 +00:00
|
|
|
for(var i = 0; i< strokePoints.length; i++) {
|
|
|
|
|
|
|
|
if(color == Constants.TRANSPARENT_COLOR) {
|
|
|
|
// When mousemoving the stroke tool, we draw in the canvas overlay above the drawing canvas.
|
|
|
|
// If the stroke color is transparent, we won't be
|
|
|
|
// able to see it during the movement.
|
|
|
|
// We set it to a semi-opaque white during the tool mousemove allowing to see colors below the stroke.
|
|
|
|
// When the stroke tool will be released, It will draw a transparent stroke,
|
|
|
|
// eg deleting the equivalent of a stroke.
|
2012-09-03 18:45:25 +00:00
|
|
|
color = Constants.SELECTION_TRANSPARENT_COLOR;
|
2012-09-01 23:41:49 +00:00
|
|
|
}
|
2012-09-10 21:26:12 +00:00
|
|
|
overlay.setPixel(strokePoints[i].col, strokePoints[i].row, color);
|
2012-09-01 22:44:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2012-09-10 21:26:12 +00:00
|
|
|
ns.Stroke.prototype.releaseToolAt = function(col, row, color, frame, overlay) {
|
2012-09-02 11:19:20 +00:00
|
|
|
// If the stroke tool is released outside of the canvas, we cancel the stroke:
|
|
|
|
// TODO: Mutualize this check in common method
|
2012-09-10 21:26:12 +00:00
|
|
|
if(frame.containsPixel(col, row)) {
|
2012-09-04 12:10:16 +00:00
|
|
|
// 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)
|
|
|
|
var strokePoints = this.getLinePixels_(this.startCol, col, this.startRow, row);
|
|
|
|
for(var i = 0; i< strokePoints.length; i++) {
|
|
|
|
// Change model:
|
2012-09-10 21:26:12 +00:00
|
|
|
frame.setPixel(strokePoints[i].col, strokePoints[i].row, color);
|
2012-09-04 12:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-01 22:44:55 +00:00
|
|
|
// For now, we are done with the stroke tool and don't need an overlay anymore:
|
2012-09-10 21:26:12 +00:00
|
|
|
overlay.clear();
|
2012-09-01 22:44:55 +00:00
|
|
|
};
|
|
|
|
})();
|