From ea45fa48428bddd0d69df1d1ca06800b44bae1a1 Mon Sep 17 00:00:00 2001 From: unsettledgames <47360416+unsettledgames@users.noreply.github.com> Date: Mon, 8 Nov 2021 22:25:30 +0100 Subject: [PATCH] Added back rect selection --- js/ColorModule.js | 2 +- js/LayerList.js | 4 +- js/ToolManager.js | 8 +- js/_move.js | 111 --------------------------- js/_rectSelect.js | 70 ----------------- js/_resizeCanvas.js | 2 +- js/_tools.js | 6 +- js/pixel-editor.js | 2 - js/tools/FillTool.js | 4 +- js/tools/MoveSelectionTool.js | 111 ++++++++++++++++++++++++++- js/tools/RectangleTool.js | 7 +- js/tools/RectangularSelectionTool.js | 74 +++++++++++++++++- 12 files changed, 193 insertions(+), 208 deletions(-) delete mode 100644 js/_move.js delete mode 100644 js/_rectSelect.js diff --git a/js/ColorModule.js b/js/ColorModule.js index 2b79241..e541c9c 100644 --- a/js/ColorModule.js +++ b/js/ColorModule.js @@ -401,7 +401,7 @@ const ColorModule = (() => { let dataLength = imageData.length; for (let j=0; j { belowImageData.data[i+2], belowImageData.data[i+3] ]; - if (isPixelEmpty(currentMovePixel)) { - if (!isPixelEmpty(belowImageData)) { + if (Util.isPixelEmpty(currentMovePixel)) { + if (!Util.isPixelEmpty(belowImageData)) { toMergeImageData.data[i] = currentUnderlyingPixel[0]; toMergeImageData.data[i+1] = currentUnderlyingPixel[1]; toMergeImageData.data[i+2] = currentUnderlyingPixel[2]; diff --git a/js/ToolManager.js b/js/ToolManager.js index b43886e..4bcfb88 100644 --- a/js/ToolManager.js +++ b/js/ToolManager.js @@ -9,11 +9,11 @@ const ToolManager = (() => { panTool = new PanTool("pan", {type: 'custom'}, switchTool); zoomTool = new ZoomTool("zoom", {type:'custom'}); - rectSelectTool = new RectangularSelectionTool("rectselect", - {type: 'cursor', style:'crosshair'}, switchTool); moveSelectionTool = new MoveSelectionTool("moveselection", - {type:'cursor', style:'crosshair'}, switchTool); - + {type:'cursor', style:'crosshair'}, switchTool, brushTool); + rectSelectTool = new RectangularSelectionTool("rectselect", + {type: 'cursor', style:'crosshair'}, switchTool, moveSelectionTool); + currTool = brushTool; currTool.onSelect(); canvasView.style.cursor = 'default'; diff --git a/js/_move.js b/js/_move.js deleted file mode 100644 index 7226424..0000000 --- a/js/_move.js +++ /dev/null @@ -1,111 +0,0 @@ -// REFACTOR: let's keep this one for the end -var imageDataToMove; -var originalDataPosition; -var canMoveSelection = false; -var lastMovePos; -var selectionCanceled = true; -var firstTimeMove = true; - -// TODO: move with arrows -/** Updates the move preview so that is placed in the right position - * - * @param {*} mousePosition The position of the cursor - */ -function updateMovePreview(mousePosition) { - // I haven't canceled the selection - selectionCanceled = false; - - // If it's the first time that I move the selection, I cut it from its original position - if (firstTimeMove) { - cutSelection(mousePosition); - } - - firstTimeMove = false; - - lastMousePos = mousePosition; - // clear the entire tmp layer - TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height); - // put the image data on the tmp layer with offset - TMPLayer.context.putImageData( - imageDataToMove, - Math.round(lastMousePos[0] / zoom) - imageDataToMove.width / 2, - Math.round(lastMousePos[1] / zoom) - imageDataToMove.height / 2); - - lastMovePos = lastMousePos; - // Moving the the rectangular ants - moveSelection(lastMousePos[0] / zoom, lastMousePos[1] / zoom, imageDataToMove.width, imageDataToMove.height); -} - -/** Ends a selection, meaning that it makes the changes definitive and creates the history states - * - */ -function endSelection() { - // Clearing the tmp (move preview) and vfx (ants) layers - TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height); - VFXLayer.context.clearRect(0, 0, VFXLayer.canvas.width, VFXLayer.canvas.height); - // Preparing an empty imageData with the size of the canvas - let cleanImageData = new ImageData(endX - startX, endY - startY); - - // If I was moving something - if (imageDataToMove !== undefined) { - // Saving the current clipboard before editing it in order to merge it with the current layer - cleanImageData.data.set(imageDataToMove.data); - - // 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 = currentLayer.context.getImageData(startX, startY, endX - startX, endY - startY); - - for (let i=0; i= 0; i-=4) { - if (!isPixelEmpty( + if (!Util.isPixelEmpty( [imageData.data[i - 3], imageData.data[i - 2], -imageData.data[i - 1], imageData.data[i]])) { pixelPosition = getPixelPosition(i); diff --git a/js/_tools.js b/js/_tools.js index 8d44ea6..037a130 100644 --- a/js/_tools.js +++ b/js/_tools.js @@ -35,7 +35,8 @@ class Tool { } onSelect() { - this.mainButton.parentElement.classList.add("selected"); + if (this.mainButton != undefined) + this.mainButton.parentElement.classList.add("selected"); this.isSelected = true; switch (this.cursorType.type) { @@ -83,7 +84,8 @@ class Tool { } onDeselect() { - this.mainButton.parentElement.classList.remove("selected"); + if (this.mainButton != undefined) + this.mainButton.parentElement.classList.remove("selected"); this.isSelected = false; } diff --git a/js/pixel-editor.js b/js/pixel-editor.js index a2da86e..b7e359b 100644 --- a/js/pixel-editor.js +++ b/js/pixel-editor.js @@ -56,8 +56,6 @@ //=include _toolButtons.js //=include FileManager.js //=include TopMenuModule.js -//=include _rectSelect.js -//=include _move.js //=include _ellipse.js /**event listeners**/ diff --git a/js/tools/FillTool.js b/js/tools/FillTool.js index e7d0c11..14982fa 100644 --- a/js/tools/FillTool.js +++ b/js/tools/FillTool.js @@ -53,7 +53,7 @@ class FillTool extends Tool { //the color of the cluster that is being filled let clusterColor = [tempImage.data[startingPosition],tempImage.data[startingPosition+1],tempImage.data[startingPosition+2], tempImage.data[startingPosition+3]]; - //the new color to fill with + //the color to fill with let fillColor = Color.hexToRgb(currentLayer.context.fillStyle); //if you try to fill with the same color that's already there, exit the function @@ -61,7 +61,6 @@ class FillTool extends Tool { clusterColor[1] == fillColor.g && clusterColor[2] == fillColor.b && clusterColor[3] != 0) { - console.log("Returned"); return; } @@ -90,6 +89,7 @@ class FillTool extends Tool { ++y; reachLeft = false; reachRight = false; + while (y++ < canvasSize[1] - 1 && matchStartColor(tempImage, pixelPos, clusterColor)) { colorPixel(tempImage, pixelPos, fillColor); if (x > 0) { diff --git a/js/tools/MoveSelectionTool.js b/js/tools/MoveSelectionTool.js index 5e28fcb..b76d5ec 100644 --- a/js/tools/MoveSelectionTool.js +++ b/js/tools/MoveSelectionTool.js @@ -1,14 +1,39 @@ class MoveSelectionTool extends Tool { - constructor (name, options, switchFunc) { + currSelection = undefined; + selectionTool = undefined; + endTool = undefined; + switchFunc = undefined; + + constructor (name, options, switchFunc, endTool) { super(name, options, switchFunc); + + this.switchFunc = switchFunc; + this.endTool = endTool; } onStart(mousePos) { super.onStart(mousePos); + + if (!this.cursorInSelectedArea(mousePos)) { + this.endSelection(); + } } onDrag(mousePos) { super.onDrag(mousePos); + + if (this.cursorInSelectedArea(mousePos)) { + this.currSelection = this.selectionTool.moveAnts(mousePos[0]/zoom, + mousePos[1]/zoom, this.currSelection.width, this.currSelection.height); + + // clear the entire tmp layer + TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height); + // put the image data on the tmp layer with offset + TMPLayer.context.putImageData( + this.currSelection.data, + Math.round(mousePos[0] / zoom) - this.currSelection.width / 2, + Math.round(mousePos[1] / zoom) - this.currSelection.height / 2); + } } onEnd(mousePos) { @@ -16,10 +41,90 @@ class MoveSelectionTool extends Tool { } onSelect() { - super.onSelect(mousePos); + super.onSelect(); } onDeselect() { - super.onDeselect(mousePos); + super.onDeselect(); + } + + setSelectionData(data, tool) { + this.currSelection = data; + this.selectionTool = tool; + this.firstTimeMove = true; + } + + onHover(mousePos) { + super.onHover(mousePos); + + if (this.cursorInSelectedArea(mousePos)) { + canvasView.style.cursor = 'move'; + } + else { + canvasView.style.cursor = 'default'; + } + } + + cursorInSelectedArea(cursorPos) { + // Getting the coordinates relatively to the canvas + let x = cursorPos[0] / zoom; + let y = cursorPos[1] / zoom; + + if (this.currSelection.left <= x && x <= this.currSelection.right) { + if (y <= this.currSelection.bottom && y >= this.currSelection.top) { + return true; + } + return false; + } + return false; + } + + endSelection() { + // Clearing the tmp (move preview) and vfx (ants) layers + TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height); + VFXLayer.context.clearRect(0, 0, VFXLayer.canvas.width, 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 = currentLayer.context.getImageData( + this.currSelection.left, this.currSelection.top, + this.currSelection.width, this.currSelection.height + ); + + for (let i=0; i