2021-11-01 15:02:18 +03:00
|
|
|
class SelectionTool extends Tool {
|
2021-12-29 01:38:28 +03:00
|
|
|
switchFunc = undefined;
|
|
|
|
moveTool = undefined;
|
|
|
|
|
2022-01-06 01:54:29 +03:00
|
|
|
boundingBox = {minX: 9999999, maxX: -1, minY: 9999999, maxY: -1};
|
|
|
|
currSelection = {};
|
|
|
|
|
2022-01-07 01:12:09 +03:00
|
|
|
outlineData = undefined;
|
2022-01-06 01:54:29 +03:00
|
|
|
previewData = undefined;
|
|
|
|
selectedPixel = undefined;
|
|
|
|
|
2022-01-07 01:12:09 +03:00
|
|
|
moveOffset = [0, 0];
|
2022-01-08 01:10:50 +03:00
|
|
|
boundingBoxCenter = [0,0];
|
2022-01-10 01:42:37 +03:00
|
|
|
reconstruct = {left:false, right:false, top:false, bottom:false};
|
2022-01-07 01:12:09 +03:00
|
|
|
|
2021-12-29 01:38:28 +03:00
|
|
|
constructor(name, options, switchFunc, moveTool) {
|
|
|
|
super(name, options);
|
|
|
|
|
|
|
|
this.moveTool = moveTool;
|
|
|
|
this.switchFunc = switchFunc;
|
2021-11-01 15:02:18 +03:00
|
|
|
}
|
2021-12-29 01:14:37 +03:00
|
|
|
|
2022-01-22 14:18:10 +03:00
|
|
|
onStart(mousePos, mouseTarget) {
|
2022-01-06 01:54:29 +03:00
|
|
|
super.onStart(mousePos);
|
|
|
|
|
2022-01-22 14:18:10 +03:00
|
|
|
if (mouseTarget == undefined || Util.isChildOfByClass(mouseTarget, "editor-top-menu") ||
|
|
|
|
!Util.cursorInCanvas(currFile.canvasSize, [mousePos[0]/currFile.zoom, mousePos[1]/currFile.zoom]))
|
|
|
|
return;
|
|
|
|
|
2022-01-08 01:10:50 +03:00
|
|
|
// Putting the vfx layer on top of everything
|
|
|
|
currFile.VFXLayer.canvas.style.zIndex = MAX_Z_INDEX;
|
|
|
|
currFile.VFXLayer.context.fillStyle = "rgba(0,0,0,1)";
|
|
|
|
|
|
|
|
this.startMousePos = [Math.floor(mousePos[0] / currFile.zoom),
|
|
|
|
Math.floor(mousePos[1] / currFile.zoom)];
|
|
|
|
this.endMousePos = [this.startMousePos[0], this.startMousePos[1]];
|
|
|
|
|
2022-01-06 01:54:29 +03:00
|
|
|
let mouseX = mousePos[0] / currFile.zoom;
|
|
|
|
let mouseY = mousePos[1] / currFile.zoom;
|
|
|
|
|
|
|
|
this.boundingBox = {minX: 9999999, maxX: -1, minY: 9999999, maxY: -1};
|
2022-01-10 01:42:37 +03:00
|
|
|
this.reconstruct = {left:false, right:false, top:false, bottom:false};
|
|
|
|
|
2022-01-06 01:54:29 +03:00
|
|
|
this.currSelection = {};
|
2022-01-07 01:12:09 +03:00
|
|
|
this.moveOffset = [0, 0];
|
2022-01-06 01:54:29 +03:00
|
|
|
|
2022-02-23 19:16:23 +03:00
|
|
|
this.updateBoundingBox(
|
|
|
|
Math.min(Math.max(mouseX, 0), currFile.canvasSize[0]-1),
|
|
|
|
Math.min(Math.max(mouseY, 0), currFile.canvasSize[1]-1)
|
|
|
|
);
|
2022-01-06 01:54:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onDrag(mousePos) {
|
2022-01-08 01:10:50 +03:00
|
|
|
super.onDrag(mousePos);
|
|
|
|
|
2022-01-06 01:54:29 +03:00
|
|
|
let mouseX = mousePos[0] / currFile.zoom;
|
|
|
|
let mouseY = mousePos[1] / currFile.zoom;
|
|
|
|
|
2022-01-10 01:42:37 +03:00
|
|
|
if (mouseX > currFile.canvasSize[0])
|
|
|
|
this.reconstruct.right = true;
|
|
|
|
else if (mouseX < 0)
|
|
|
|
this.reconstruct.left = true;
|
|
|
|
|
|
|
|
if (mouseY > currFile.canvasSize[1])
|
|
|
|
this.reconstruct.bottom = true;
|
|
|
|
else if (mouseY < 0)
|
|
|
|
this.reconstruct.top = true;
|
|
|
|
|
|
|
|
|
2022-02-05 00:13:11 +03:00
|
|
|
if (Util.cursorInCanvas(currFile.canvasSize, [mousePos[0]/currFile.zoom, mousePos[1]/currFile.zoom])) {
|
2022-02-23 19:16:23 +03:00
|
|
|
this.updateBoundingBox(
|
|
|
|
Math.min(Math.max(mouseX, 0), currFile.canvasSize[0]-1),
|
|
|
|
Math.min(Math.max(mouseY, 0), currFile.canvasSize[1]-1)
|
|
|
|
);
|
2022-02-05 00:13:11 +03:00
|
|
|
}
|
2022-01-06 01:54:29 +03:00
|
|
|
}
|
|
|
|
|
2022-01-22 14:18:10 +03:00
|
|
|
onEnd(mousePos, mouseTarget) {
|
2022-01-08 01:10:50 +03:00
|
|
|
super.onEnd(mousePos);
|
|
|
|
|
2022-02-05 00:13:11 +03:00
|
|
|
if (mouseTarget == undefined || Util.isChildOfByClass(mouseTarget, "editor-top-menu"))
|
2022-01-22 14:18:10 +03:00
|
|
|
return;
|
|
|
|
|
2022-01-10 01:42:37 +03:00
|
|
|
let mouseX = mousePos[0] / currFile.zoom;
|
|
|
|
let mouseY = mousePos[1] / currFile.zoom;
|
|
|
|
|
2022-02-05 00:13:11 +03:00
|
|
|
if (Util.cursorInCanvas(currFile.canvasSize, [mousePos[0]/currFile.zoom, mousePos[1]/currFile.zoom])) {
|
2022-02-23 19:16:23 +03:00
|
|
|
this.updateBoundingBox(
|
|
|
|
Math.min(Math.max(mouseX, 0), currFile.canvasSize[0]-1),
|
|
|
|
Math.min(Math.max(mouseY, 0), currFile.canvasSize[1]-1)
|
|
|
|
);
|
2022-02-05 00:13:11 +03:00
|
|
|
}
|
2022-01-10 01:42:37 +03:00
|
|
|
|
2022-01-08 01:10:50 +03:00
|
|
|
this.boundingBoxCenter = [this.boundingBox.minX + (this.boundingBox.maxX - this.boundingBox.minX) / 2,
|
|
|
|
this.boundingBox.minY + (this.boundingBox.maxY - this.boundingBox.minY) / 2];
|
|
|
|
}
|
|
|
|
|
2022-01-08 01:22:42 +03:00
|
|
|
cutSelection() {
|
|
|
|
let currLayerData = currFile.currentLayer.context.getImageData(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]).data;
|
|
|
|
|
|
|
|
// Save the selected pixels so that they can be moved and pasted back in the right place
|
|
|
|
for (const key in this.currSelection) {
|
|
|
|
let x = parseInt(key.split(",")[0]);
|
|
|
|
let y = parseInt(key.split(",")[1]);
|
2022-01-25 02:33:23 +03:00
|
|
|
let index = (y * currFile.canvasSize[0] + x) * 4;
|
2022-01-08 01:22:42 +03:00
|
|
|
|
|
|
|
for (let i=0; i<4; i++) {
|
|
|
|
// Save the pixel
|
|
|
|
this.previewData.data[index + i] = currLayerData[index + i];
|
|
|
|
// Delete the data below
|
|
|
|
currLayerData[index + i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currFile.currentLayer.context.putImageData(new ImageData(currLayerData, currFile.canvasSize[0]), 0, 0);
|
|
|
|
}
|
2021-12-29 01:38:28 +03:00
|
|
|
|
2022-01-08 01:10:50 +03:00
|
|
|
pasteSelection(){
|
|
|
|
if (this.currSelection == undefined)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]);
|
|
|
|
let pasteData = currFile.TMPLayer.context.getImageData(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]);
|
|
|
|
|
|
|
|
// Clearing the tmp (move preview) and vfx (ants) layers
|
|
|
|
currFile.TMPLayer.context.clearRect(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]);
|
|
|
|
currFile.VFXLayer.context.clearRect(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]);
|
|
|
|
|
2022-01-23 15:32:23 +03:00
|
|
|
Util.pasteData(underlyingImageData, pasteData, currFile.currentLayer.context);
|
2022-01-08 01:10:50 +03:00
|
|
|
currFile.currentLayer.updateLayerPreview();
|
|
|
|
|
|
|
|
currFile.VFXLayer.canvas.style.zIndex = MIN_Z_INDEX;
|
|
|
|
}
|
2021-12-29 01:38:28 +03:00
|
|
|
|
2022-01-06 01:54:29 +03:00
|
|
|
cursorInSelectedArea(mousePos) {
|
2022-01-08 01:10:50 +03:00
|
|
|
let floored = [Math.floor(mousePos[0] / currFile.zoom) - this.moveOffset[0],
|
|
|
|
Math.floor(mousePos[1] / currFile.zoom) - this.moveOffset[1]];
|
2022-01-06 01:54:29 +03:00
|
|
|
|
|
|
|
if (this.currSelection[floored] != undefined)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit(pixel, visited, data) {
|
|
|
|
let toVisit = [pixel];
|
|
|
|
let selected = [];
|
|
|
|
let currVisited = {};
|
|
|
|
|
|
|
|
currFile.TMPLayer.context.clearRect(0, 0, 512, 512);
|
|
|
|
|
|
|
|
while (toVisit.length > 0) {
|
|
|
|
pixel = toVisit.pop();
|
|
|
|
selected.push(pixel);
|
|
|
|
|
|
|
|
visited[pixel] = true;
|
|
|
|
currVisited[pixel] = true;
|
|
|
|
|
|
|
|
let col = Util.getPixelColor(data, pixel[0], pixel[1], currFile.canvasSize[0]);
|
|
|
|
if (col[3] == 255)
|
|
|
|
continue;
|
|
|
|
if (this.isBorderOfBox(pixel))
|
|
|
|
return [];
|
|
|
|
|
|
|
|
let top, bottom, left, right;
|
|
|
|
if (pixel[1] > 0)
|
|
|
|
top = [pixel[0], pixel[1] - 1];
|
|
|
|
else
|
|
|
|
top = undefined;
|
|
|
|
|
|
|
|
if (pixel[0] > 0)
|
|
|
|
left = [pixel[0] - 1, pixel[1]];
|
|
|
|
else
|
|
|
|
left = undefined;
|
|
|
|
|
|
|
|
if (pixel[1] < currFile.canvasSize[1])
|
|
|
|
bottom = [pixel[0], pixel[1] + 1];
|
|
|
|
else
|
|
|
|
bottom = undefined;
|
|
|
|
|
|
|
|
if (pixel[0] < currFile.canvasSize[0])
|
|
|
|
right = [pixel[0] + 1, pixel[1]];
|
|
|
|
else
|
|
|
|
right = undefined;
|
|
|
|
|
|
|
|
if (right != undefined && currVisited[right] == undefined)
|
|
|
|
toVisit.push(right);
|
|
|
|
if (left != undefined && currVisited[left] == undefined)
|
|
|
|
toVisit.push(left);
|
|
|
|
if (top != undefined && currVisited[top] == undefined)
|
|
|
|
toVisit.push(top);
|
|
|
|
if (bottom != undefined && currVisited[bottom] == undefined)
|
|
|
|
toVisit.push(bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSelection() {
|
|
|
|
let selected = [];
|
|
|
|
let visited = {};
|
|
|
|
let data = currFile.VFXLayer.context.getImageData(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]).data;
|
2022-01-10 01:42:37 +03:00
|
|
|
|
2022-01-07 01:12:09 +03:00
|
|
|
// BFS: a pixel that causes the algorithm to visit a pixel of the bounding box is outside the
|
|
|
|
// selection. Otherwise, since the algorithm stops visiting when it reaches the outline,
|
|
|
|
// the pixel is inside the selection (and so are all the ones that have been visited)
|
2022-01-06 01:54:29 +03:00
|
|
|
for (let x=this.boundingBox.minX-1; x<=this.boundingBox.maxX+1; x++) {
|
|
|
|
for (let y=this.boundingBox.minY-1; y<=this.boundingBox.maxY+1; y++) {
|
|
|
|
if (visited[[x, y]] == undefined) {
|
|
|
|
let insidePixels = this.visit([x,y], visited, data);
|
|
|
|
|
|
|
|
for (let i=0; i<insidePixels.length; i++) {
|
|
|
|
selected.push(insidePixels[i]);
|
|
|
|
this.currSelection[insidePixels[i]] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 19:16:23 +03:00
|
|
|
////console.log('this.currSelection === ',this.currSelection);
|
2022-01-06 01:54:29 +03:00
|
|
|
|
2022-01-07 01:12:09 +03:00
|
|
|
// Save the selection outline
|
|
|
|
this.outlineData = currFile.VFXLayer.context.getImageData(this.boundingBox.minX,
|
|
|
|
this.boundingBox.minY, this.boundingBox.maxX - this.boundingBox.minX,
|
|
|
|
this.boundingBox.maxY - this.boundingBox.minY);
|
|
|
|
// Create the image data containing the selected pixels
|
|
|
|
this.previewData = new ImageData(currFile.canvasSize[0], currFile.canvasSize[1]);
|
|
|
|
|
2022-01-08 01:22:42 +03:00
|
|
|
// Cut the selection
|
2022-01-22 14:18:10 +03:00
|
|
|
this.cutSelection();
|
2022-01-08 01:22:42 +03:00
|
|
|
// Put it on the TMP layer
|
2022-01-08 01:10:50 +03:00
|
|
|
currFile.TMPLayer.context.putImageData(this.previewData, 0, 0);
|
2022-01-22 14:18:10 +03:00
|
|
|
// Draw the selected area and the bounding box
|
2022-01-07 01:12:09 +03:00
|
|
|
this.drawSelectedArea();
|
2022-01-22 14:18:10 +03:00
|
|
|
this.drawBoundingBox();
|
2022-01-07 01:12:09 +03:00
|
|
|
|
|
|
|
return this.previewData;
|
|
|
|
}
|
|
|
|
|
|
|
|
drawSelectedArea() {
|
|
|
|
for (const key in this.currSelection) {
|
|
|
|
let x = parseInt(key.split(",")[0]);
|
|
|
|
let y = parseInt(key.split(",")[1]);
|
|
|
|
|
2022-01-08 01:10:50 +03:00
|
|
|
currFile.VFXLayer.context.fillStyle = "rgba(10, 0, 40, 0.3)";
|
|
|
|
currFile.VFXLayer.context.fillRect(x + this.moveOffset[0], y + this.moveOffset[1], 1, 1);
|
2022-01-06 01:54:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 01:10:50 +03:00
|
|
|
drawOutline() {
|
|
|
|
currFile.VFXLayer.context.clearRect(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]);
|
|
|
|
currFile.VFXLayer.context.putImageData(this.outlineData, this.boundingBox.minX + this.moveOffset[0],
|
|
|
|
this.boundingBox.minY + this.moveOffset[1]);
|
|
|
|
}
|
|
|
|
|
2022-02-23 19:16:23 +03:00
|
|
|
drawBoundingBox(xo = 0, yo = 0) {
|
2022-01-22 14:18:10 +03:00
|
|
|
currFile.VFXLayer.context.fillStyle = "red";
|
2022-02-23 19:16:23 +03:00
|
|
|
|
|
|
|
currFile.VFXLayer.context.fillRect(
|
|
|
|
this.boundingBox.minX + this.moveOffset[0] - xo,
|
|
|
|
this.boundingBox.minY + this.moveOffset[1] - yo,
|
|
|
|
1, 1);
|
|
|
|
currFile.VFXLayer.context.fillRect(
|
|
|
|
this.boundingBox.minX + this.moveOffset[0] - xo,
|
|
|
|
this.boundingBox.maxY + this.moveOffset[1] + yo,
|
|
|
|
1, 1);
|
|
|
|
currFile.VFXLayer.context.fillRect(
|
|
|
|
this.boundingBox.maxX + this.moveOffset[0] + xo,
|
|
|
|
this.boundingBox.minY + this.moveOffset[1] - yo,
|
|
|
|
1, 1);
|
|
|
|
currFile.VFXLayer.context.fillRect(
|
|
|
|
this.boundingBox.maxX + this.moveOffset[0] + xo,
|
|
|
|
this.boundingBox.maxY + this.moveOffset[1] + yo,
|
|
|
|
1, 1);
|
2022-01-22 14:18:10 +03:00
|
|
|
}
|
|
|
|
|
2022-01-06 01:54:29 +03:00
|
|
|
isBorderOfBox(pixel) {
|
|
|
|
return pixel[0] == this.boundingBox.minX || pixel[0] == this.boundingBox.maxX ||
|
|
|
|
pixel[1] == this.boundingBox.minY || pixel[1] == this.boundingBox.maxY;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBoundingBox(mouseX, mouseY) {
|
|
|
|
if (mouseX > this.boundingBox.maxX)
|
|
|
|
this.boundingBox.maxX = Math.floor(mouseX);
|
|
|
|
if (mouseX < this.boundingBox.minX)
|
|
|
|
this.boundingBox.minX = Math.floor(mouseX);
|
|
|
|
if (mouseY < this.boundingBox.minY)
|
|
|
|
this.boundingBox.minY = Math.floor(mouseY);
|
|
|
|
if (mouseY > this.boundingBox.maxY)
|
|
|
|
this.boundingBox.maxY = Math.floor(mouseY);
|
|
|
|
}
|
2021-11-01 15:02:18 +03:00
|
|
|
}
|