From 28835f96f36ee1b3521cc8c6126844876d23b89e Mon Sep 17 00:00:00 2001 From: Vince Date: Sat, 15 Sep 2012 01:01:47 +0200 Subject: [PATCH] Cleaning a bit pixel utils + fix empty paste bug --- js/selection/BaseSelection.js | 2 + js/selection/SelectionManager.js | 5 +- js/utils/pixelUtils.js | 99 +++++++++++--------------------- 3 files changed, 36 insertions(+), 70 deletions(-) diff --git a/js/selection/BaseSelection.js b/js/selection/BaseSelection.js index 2458cd1..e0a1fc8 100644 --- a/js/selection/BaseSelection.js +++ b/js/selection/BaseSelection.js @@ -7,6 +7,7 @@ ns.BaseSelection.prototype.reset = function () { this.pixels = []; + this.hasPastedContent = false; }; ns.BaseSelection.prototype.move = function (colDiff, rowDiff) { @@ -28,5 +29,6 @@ pixelWithCopiedColor.copiedColor = targetFrame.getPixel(pixelWithCopiedColor.col, pixelWithCopiedColor.row); } + this.hasPastedContent = true; }; })(); \ No newline at end of file diff --git a/js/selection/SelectionManager.js b/js/selection/SelectionManager.js index 8cb7a2c..621c805 100644 --- a/js/selection/SelectionManager.js +++ b/js/selection/SelectionManager.js @@ -72,7 +72,7 @@ }; ns.SelectionManager.prototype.onPaste_ = function(evt) { - if(this.currentSelection) { + if(this.currentSelection && this.currentSelection.hasPastedContent) { var pixels = this.currentSelection.pixels; var currentFrame = this.framesheet.getCurrentFrame(); for(var i=0, l=pixels.length; i 0) { - loopCount ++; - - var currentItem = queue.pop(); - frame.setPixel(currentItem.col, currentItem.row, replacementColor); - pixels.push({"col": currentItem.col, "row": currentItem.row }); - - for (var i = 0; i < 4; i++) { - var nextCol = currentItem.col + dx[i] - var nextRow = currentItem.row + dy[i] - try { - if (frame.containsPixel(nextCol, nextRow) && frame.getPixel(nextCol, nextRow) == targetColor) { - queue.push({"col": nextCol, "row": nextRow }); - } - } catch(e) { - // Frame out of bound exception. - } - } - - // Security loop breaker: - if(loopCount > 10 * cellCount) { - console.log("loop breaker called") - break; - } - } - return pixels; + return paintedPixels; }, + /** + * Apply the paintbucket tool in a frame at the (col, row) initial position + * with the replacement color. + * + * @param frame pskl.model.Frame The frame target in which we want to paintbucket + * @param col number Column coordinate in the frame + * @param row number Row coordinate in the frame + * @param replacementColor string Hexadecimal color used to fill the area + * + * @return an array of the pixel coordinates paint with the replacement color + */ paintSimilarConnectedPixelsFromFrame: function(frame, col, row, replacementColor) { /** * Queue linear Flood-fill (node, target-color, replacement-color): @@ -146,6 +110,7 @@ * * @private */ + var paintedPixels = []; var queue = []; var dy = [-1, 0, 1, 0]; var dx = [0, 1, 0, -1]; @@ -168,7 +133,8 @@ var currentItem = queue.pop(); frame.setPixel(currentItem.col, currentItem.row, replacementColor); - + paintedPixels.push({"col": currentItem.col, "row": currentItem.row }); + for (var i = 0; i < 4; i++) { var nextCol = currentItem.col + dx[i] var nextRow = currentItem.row + dy[i] @@ -187,6 +153,7 @@ break; } } + return paintedPixels; } }; })(); \ No newline at end of file