piskel/src/js/tools/drawing/selection/LassoSelect.js
2015-10-06 00:21:13 +02:00

83 lines
2.7 KiB
JavaScript

/**
* @provide pskl.tools.drawing.selection.LassoSelect
*
* @require pskl.utils
*/
(function() {
var ns = $.namespace('pskl.tools.drawing.selection');
ns.LassoSelect = function() {
this.toolId = 'tool-lasso-select';
this.helpText = 'Lasso selection';
ns.AbstractDragSelect.call(this);
};
pskl.utils.inherit(ns.LassoSelect, ns.AbstractDragSelect);
/** @override */
ns.LassoSelect.prototype.startDragSelection_ = function (col, row) {
this.pixels = [{col : col, row : row}];
this.previousCol = col;
this.previousRow = row;
$.publish(Events.DRAG_START, [col, row]);
};
/** @override */
ns.LassoSelect.prototype.updateDragSelection_ = function (col, row, color, frame, overlay) {
this.addPixel_(col, row, frame);
// use ShapeSelection during selection, contains only the pixels hovered by the user
var selection = new pskl.selection.ShapeSelection(this.getLassoPixels_());
this.setSelection_(selection, overlay);
};
/** @override */
ns.LassoSelect.prototype.endDragSelection_ = function (col, row, color, frame, overlay) {
this.addPixel_(col, row, frame);
// use LassoSelection to finalize selection, includes pixels inside the lasso shape
var selection = new pskl.selection.LassoSelection(this.getLassoPixels_(), frame);
this.setSelection_(selection, overlay);
$.publish(Events.DRAG_END);
};
/**
* Retrieve the lasso shape as an array of pixels. A line is added between the origin of the selection
* and the last known coordinate to make sure the shape is closed.
*
* @return {Array} array of pixels corresponding to the whole lasso shape
* @private
*/
ns.LassoSelect.prototype.getLassoPixels_ = function () {
var line = this.getLinePixels_(this.previousCol, this.startCol, this.previousRow, this.startRow);
return this.pixels.concat(line);
};
/**
* Add the provided pixel to the lasso pixels Array.
* @private
*/
ns.LassoSelect.prototype.addPixel_ = function (col, row, frame) {
// normalize coordinates to always remain inside the frame
col = pskl.utils.Math.minmax(col, 0, frame.getWidth() - 1);
row = pskl.utils.Math.minmax(row, 0, frame.getHeight() - 1);
// line interpolation needed in case mousemove was too fast
var interpolatedPixels = this.getLinePixels_(col, this.previousCol, row, this.previousRow);
this.pixels = this.pixels.concat(interpolatedPixels);
// update state
this.previousCol = col;
this.previousRow = row;
};
/** @private */
ns.LassoSelect.prototype.setSelection_ = function (selection, overlay) {
this.selection = selection;
$.publish(Events.SELECTION_CREATED, [this.selection]);
overlay.clear();
this.drawSelectionOnOverlay_(overlay);
};
})();