Add ability to source all layers to bucket fill tool

This commit is contained in:
blurymind 2019-11-11 22:59:26 +00:00
parent 025c34d07c
commit 47d7abab18
2 changed files with 20 additions and 8 deletions

View File

@ -10,6 +10,8 @@
this.toolId = 'tool-paint-bucket'; this.toolId = 'tool-paint-bucket';
this.helpText = 'Paint bucket tool'; this.helpText = 'Paint bucket tool';
this.shortcut = pskl.service.keyboard.Shortcuts.TOOL.PAINT_BUCKET; this.shortcut = pskl.service.keyboard.Shortcuts.TOOL.PAINT_BUCKET;
this.tooltipDescriptors = [{ key: 'ctrl', description: 'Source only current layer' }];
}; };
pskl.utils.inherit(ns.PaintBucket, ns.BaseTool); pskl.utils.inherit(ns.PaintBucket, ns.BaseTool);
@ -19,7 +21,15 @@
*/ */
ns.PaintBucket.prototype.applyToolAt = function(col, row, frame, overlay, event) { ns.PaintBucket.prototype.applyToolAt = function(col, row, frame, overlay, event) {
var color = this.getToolColor(); var color = this.getToolColor();
pskl.PixelUtils.paintSimilarConnectedPixelsFromFrame(frame, col, row, color);
var sourceOnlyCurrentLayer = pskl.utils.UserAgent.isMac ? event.metaKey : event.ctrlKey;
var sourceFrame = frame;
if (!sourceOnlyCurrentLayer) {
var currentFrameIndex = pskl.app.piskelController.getCurrentFrameIndex();
sourceFrame = pskl.utils.LayerUtils.mergeFrameAt(pskl.app.piskelController.getLayers(), currentFrameIndex);
};
pskl.PixelUtils.paintSimilarConnectedPixelsFromFrame(sourceFrame, frame, col, row, color);
this.raiseSaveStateEvent({ this.raiseSaveStateEvent({
col : col, col : col,
@ -29,6 +39,6 @@
}; };
ns.PaintBucket.prototype.replay = function (frame, replayData) { ns.PaintBucket.prototype.replay = function (frame, replayData) {
pskl.PixelUtils.paintSimilarConnectedPixelsFromFrame(frame, replayData.col, replayData.row, replayData.color); pskl.PixelUtils.paintSimilarConnectedPixelsFromFrame(frame, frame, replayData.col, replayData.row, replayData.color);
}; };
})(); })();

View File

@ -100,14 +100,15 @@
* Apply the paintbucket tool in a frame at the (col, row) initial position * Apply the paintbucket tool in a frame at the (col, row) initial position
* with the replacement color. * with the replacement color.
* *
* @param frame pskl.model.Frame The frame target in which we want to paintbucket * @param sourceFrame pskl.model.Frame The frame source which we use for reference to discover pixels to fill. This is disposed.
* @param targetFrame pskl.model.Frame The frame target in which we want to paintbucket. We set color on this frame.
* @param col number Column coordinate in the frame * @param col number Column coordinate in the frame
* @param row number Row coordinate in the frame * @param row number Row coordinate in the frame
* @param replacementColor string Hexadecimal color used to fill the area * @param replacementColor string Hexadecimal color used to fill the area
* *
* @return an array of the pixel coordinates paint with the replacement color * @return an array of the pixel coordinates paint with the replacement color
*/ */
paintSimilarConnectedPixelsFromFrame: function(frame, col, row, replacementColor) { paintSimilarConnectedPixelsFromFrame: function(sourceFrame, targetFrame, col, row, replacementColor) {
/** /**
* Queue linear Flood-fill (node, target-color, replacement-color): * Queue linear Flood-fill (node, target-color, replacement-color):
* 1. Set Q to the empty queue. * 1. Set Q to the empty queue.
@ -131,7 +132,7 @@
var targetColor; var targetColor;
try { try {
targetColor = frame.getPixel(col, row); targetColor = sourceFrame.getPixel(col, row);
} catch (e) { } catch (e) {
// Frame out of bound exception. // Frame out of bound exception.
} }
@ -144,9 +145,10 @@
col : col, col : col,
row : row row : row
}; };
var paintedPixels = pskl.PixelUtils.visitConnectedPixels(startPixel, frame, function (pixel) { var paintedPixels = pskl.PixelUtils.visitConnectedPixels(startPixel, sourceFrame, function (pixel) {
if (frame.getPixel(pixel.col, pixel.row) == targetColor) { if (sourceFrame.getPixel(pixel.col, pixel.row) == targetColor) {
frame.setPixel(pixel.col, pixel.row, replacementColor); sourceFrame.setPixel(pixel.col, pixel.row, replacementColor);
targetFrame.setPixel(pixel.col, pixel.row, replacementColor);
return true; return true;
} }
return false; return false;