Slightly refactored selection tool

Now the move tool doesn't depend on how the selection has been made anymore
This commit is contained in:
Nicola 2021-12-28 23:14:37 +01:00
parent 8df8b3ac54
commit 69d7f12790
4 changed files with 68 additions and 43 deletions

View File

@ -29,6 +29,13 @@ class LassoSelectionTool extends SelectionTool {
onEnd(mousePos) { onEnd(mousePos) {
super.onEnd(mousePos); super.onEnd(mousePos);
new HistoryState().EditCanvas();
this.currentPixels.push[this.startMousePos[0] / currFile.zoom, this.startMousePos[1] / currFile.zoom];
this.getSelection();
// Switch to the move tool so that the user can move the selection
this.switchFunc(this.moveTool);
} }
onSelect() { onSelect() {
@ -46,7 +53,6 @@ class LassoSelectionTool extends SelectionTool {
let prevPoint = []; let prevPoint = [];
currFile.VFXLayer.context.clearRect(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]); currFile.VFXLayer.context.clearRect(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]);
//currFile.VFXLayer.context.setLineDash([2, 2]);
currFile.VFXLayer.context.strokeStyle = 'rgba(0,0,0,1)'; currFile.VFXLayer.context.strokeStyle = 'rgba(0,0,0,1)';
currFile.VFXLayer.context.fillStyle = 'rgba(0,0,0,1)'; currFile.VFXLayer.context.fillStyle = 'rgba(0,0,0,1)';
currFile.VFXLayer.context.lineWidth = 1; currFile.VFXLayer.context.lineWidth = 1;

View File

@ -32,8 +32,7 @@ class MoveSelectionTool extends DrawingTool {
this.endSelection(); this.endSelection();
this.currSelection = this.lastCopiedSelection; this.currSelection = this.lastCopiedSelection;
// Cut the data // Cut the data
currFile.currentLayer.context.clearRect(this.currSelection.left-0.5, this.currSelection.top-0.5, this.selectionTool.cutSelection();
this.currSelection.width, this.currSelection.height);
} }
pasteSelection() { pasteSelection() {
@ -127,47 +126,8 @@ class MoveSelectionTool extends DrawingTool {
endSelection() { endSelection() {
if (this.currSelection == undefined) if (this.currSelection == undefined)
return; return;
// Clearing the tmp (move preview) and vfx (ants) layers
currFile.TMPLayer.context.clearRect(0, 0, currFile.TMPLayer.canvas.width, currFile.TMPLayer.canvas.height);
currFile.VFXLayer.context.clearRect(0, 0, currFile.VFXLayer.canvas.width, currFile.VFXLayer.canvas.height);
// I have to save the underlying data, so that the transparent pixels in the clipboard
// don't override the coloured pixels in the canvas
let underlyingImageData = currFile.currentLayer.context.getImageData(
this.currSelection.left, this.currSelection.top,
this.currSelection.width+1, this.currSelection.height+1
);
let pasteData = this.currSelection.data.data.slice();
for (let i=0; i<underlyingImageData.data.length; i+=4) {
let currentMovePixel = [
pasteData[i], pasteData[i+1], pasteData[i+2], pasteData[i+3]
];
let currentUnderlyingPixel = [
underlyingImageData.data[i], underlyingImageData.data[i+1],
underlyingImageData.data[i+2], underlyingImageData.data[i+3]
];
// If the pixel of the clipboard is empty, but the one below it isn't, I use the pixel below
if (Util.isPixelEmpty(currentMovePixel)) {
if (!Util.isPixelEmpty(currentUnderlyingPixel)) {
pasteData[i] = currentUnderlyingPixel[0];
pasteData[i+1] = currentUnderlyingPixel[1];
pasteData[i+2] = currentUnderlyingPixel[2];
pasteData[i+3] = currentUnderlyingPixel[3];
}
}
}
currFile.currentLayer.context.putImageData(new ImageData(pasteData, this.currSelection.width+1),
this.currSelection.left, this.currSelection.top
);
this.currSelection = undefined; this.currSelection = undefined;
currFile.currentLayer.updateLayerPreview(); this.selectionTool.pasteSelection();
currFile.VFXLayer.canvas.style.zIndex = MIN_Z_INDEX;
// Switch to brush // Switch to brush
this.switchFunc(this.endTool); this.switchFunc(this.endTool);
} }

View File

@ -97,6 +97,61 @@ class RectangularSelectionTool extends SelectionTool {
console.log("data set"); console.log("data set");
} }
copySelection() {
super.copySelection();
}
cutSelection() {
super.cutSelection();
currFile.currentLayer.context.clearRect(this.currSelection.left-0.5, this.currSelection.top-0.5,
this.currSelection.width, this.currSelection.height);
}
pasteSelection() {
super.pasteSelection();
if (this.currSelection == undefined)
return;
// Clearing the tmp (move preview) and vfx (ants) layers
currFile.TMPLayer.context.clearRect(0, 0, currFile.TMPLayer.canvas.width, currFile.TMPLayer.canvas.height);
currFile.VFXLayer.context.clearRect(0, 0, currFile.VFXLayer.canvas.width, currFile.VFXLayer.canvas.height);
// I have to save the underlying data, so that the transparent pixels in the clipboard
// don't override the coloured pixels in the canvas
let underlyingImageData = currFile.currentLayer.context.getImageData(
this.currSelection.left, this.currSelection.top,
this.currSelection.width+1, this.currSelection.height+1
);
let pasteData = this.currSelection.data.data.slice();
for (let i=0; i<underlyingImageData.data.length; i+=4) {
let currentMovePixel = [
pasteData[i], pasteData[i+1], pasteData[i+2], pasteData[i+3]
];
let currentUnderlyingPixel = [
underlyingImageData.data[i], underlyingImageData.data[i+1],
underlyingImageData.data[i+2], underlyingImageData.data[i+3]
];
// If the pixel of the clipboard is empty, but the one below it isn't, I use the pixel below
if (Util.isPixelEmpty(currentMovePixel)) {
if (!Util.isPixelEmpty(currentUnderlyingPixel)) {
pasteData[i] = currentUnderlyingPixel[0];
pasteData[i+1] = currentUnderlyingPixel[1];
pasteData[i+2] = currentUnderlyingPixel[2];
pasteData[i+3] = currentUnderlyingPixel[3];
}
}
}
currFile.currentLayer.context.putImageData(new ImageData(pasteData, this.currSelection.width+1),
this.currSelection.left, this.currSelection.top
);
currFile.currentLayer.updateLayerPreview();
currFile.VFXLayer.canvas.style.zIndex = MIN_Z_INDEX;
}
onSelect() { onSelect() {
super.onSelect(); super.onSelect();
} }

View File

@ -2,4 +2,8 @@ class SelectionTool extends Tool {
constructor(name, options, switchFunc) { constructor(name, options, switchFunc) {
super(name, options, switchFunc); super(name, options, switchFunc);
} }
cutSelection() {}
pasteSelection(){}
copySelection(){}
} }