From 9beeefd39952b87e477017defbd725ae5720b7c8 Mon Sep 17 00:00:00 2001 From: unsettledgames <47360416+unsettledgames@users.noreply.github.com> Date: Fri, 5 Jun 2020 22:19:48 +0200 Subject: [PATCH] Finished copy / cut /paste Also fixed a minor bug that caused the preview rectangle for the selection tool to not be aligned to the actual selection. --- js/_copyPaste.js | 11 +++++++---- js/_move.js | 22 ++++++++++++++++------ js/_rectSelect.js | 5 ++--- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/js/_copyPaste.js b/js/_copyPaste.js index bae215a..66695be 100644 --- a/js/_copyPaste.js +++ b/js/_copyPaste.js @@ -6,19 +6,22 @@ let copiedStartY; let copiedEndX; let copiedEndY; +// BUG: when merging tmp layer to currentLayer there are offset problems +// FIX: maybe copy the entire tmp layer and paste it so that the merging happens at 0,0 + function copySelection() { - /* copiedEndX = endX; copiedEndY = endY; copiedStartX = startX; copiedStartY = startY; -*/ // Getting the selected pixels clipboardData = currentLayer.context.getImageData(startX, startY, endX - startX + 1, endY - startY + 1); } function pasteSelection() { + endSelection(); + isPasting = true; // Putting the image data on the tmp layer TMPLayer.context.putImageData(clipboardData, copiedStartX, copiedStartY); @@ -35,11 +38,12 @@ function pasteSelection() { moveSelection( copiedStartX + (copiedEndX - copiedStartX) / 2, copiedStartY + (copiedEndY - copiedStartY) / 2, - imageDataToMove.width, imageDataToMove.height); + clipboardData.width, clipboardData.height); //drawRect(copiedStartX, copiedEndX, copiedStartY, copiedEndY); } function cutSelectionTool() { + console.log("Taglio"); // Saving the canvas new HistoryStateEditCanvas(); @@ -59,7 +63,6 @@ function cutSelectionTool() { clipboardData = currentLayer.context.getImageData(startX, startY, endX - startX + 1, endY - startY + 1); currentLayer.context.clearRect(startX - 0.5, startY - 0.5, endX - startX + 1, endY - startY + 1); } - // Moving those pixels from the current layer to the tmp layer //TMPLayer.context.putImageData(imageDataToMove, startX + 1, startY); diff --git a/js/_move.js b/js/_move.js index cff6c21..cbf30f8 100644 --- a/js/_move.js +++ b/js/_move.js @@ -7,7 +7,6 @@ var firstTimeMove = true; // TODO: move with arrows function updateMovePreview(mousePosition) { - // ISSUE selectionCanceled = false; if (firstTimeMove) { @@ -22,8 +21,8 @@ function updateMovePreview(mousePosition) { // put the image data with offset TMPLayer.context.putImageData( imageDataToMove, - Math.round(lastMousePos[0] / zoom - imageDataToMove.width / 2), - Math.round(lastMousePos[1] / zoom - imageDataToMove.height / 2)); + Math.round(lastMousePos[0] / zoom) - imageDataToMove.width / 2, + Math.round(lastMousePos[1] / zoom) - imageDataToMove.height / 2); lastMovePos = lastMousePos; moveSelection(lastMousePos[0] / zoom, lastMousePos[1] / zoom, imageDataToMove.width, imageDataToMove.height); @@ -32,8 +31,12 @@ function updateMovePreview(mousePosition) { function endSelection() { TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height); VFXLayer.context.clearRect(0, 0, VFXLayer.canvas.width, VFXLayer.canvas.height); + let cleanImageData = new ImageData(endX - startX, endY - startY); if (imageDataToMove !== undefined) { + // Saving the current clipboard before editing it in order to merge it with the current layer + cleanImageData.data.set(imageDataToMove.data); + let underlyingImageData = currentLayer.context.getImageData(startX, startY, endX - startX, endY - startY); for (let i=0; i