2020-12-31 15:05:51 +03:00
|
|
|
// Data saved when copying or cutting
|
2020-04-20 17:55:34 +03:00
|
|
|
let clipboardData;
|
2020-12-31 15:05:51 +03:00
|
|
|
// Tells if the user is pasting something or not
|
2020-04-20 20:26:00 +03:00
|
|
|
let isPasting = false;
|
|
|
|
|
2020-12-31 15:05:51 +03:00
|
|
|
// Coordinates of the copied (or cut) selection
|
2020-04-20 20:26:00 +03:00
|
|
|
let copiedStartX;
|
|
|
|
let copiedStartY;
|
|
|
|
let copiedEndX;
|
|
|
|
let copiedEndY;
|
2020-04-20 17:55:34 +03:00
|
|
|
|
2020-12-31 15:05:51 +03:00
|
|
|
/** Copies the current selection to the clipboard
|
|
|
|
*
|
|
|
|
*/
|
2020-04-20 17:55:34 +03:00
|
|
|
function copySelection() {
|
2020-04-20 20:26:00 +03:00
|
|
|
copiedEndX = endX;
|
|
|
|
copiedEndY = endY;
|
|
|
|
|
|
|
|
copiedStartX = startX;
|
|
|
|
copiedStartY = startY;
|
|
|
|
// Getting the selected pixels
|
|
|
|
clipboardData = currentLayer.context.getImageData(startX, startY, endX - startX + 1, endY - startY + 1);
|
2020-04-20 17:55:34 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 15:05:51 +03:00
|
|
|
/** Pastes the clipboard data onto the current layer
|
|
|
|
*
|
|
|
|
*/
|
2020-04-20 17:55:34 +03:00
|
|
|
function pasteSelection() {
|
2020-06-19 13:57:07 +03:00
|
|
|
// Can't paste if the layer is locked
|
|
|
|
if (currentLayer.isLocked) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-31 15:05:51 +03:00
|
|
|
|
|
|
|
// Cancel the current selection
|
2020-06-05 23:19:48 +03:00
|
|
|
endSelection();
|
|
|
|
|
2020-12-31 15:05:51 +03:00
|
|
|
// I'm pasting
|
2020-04-20 20:26:00 +03:00
|
|
|
isPasting = true;
|
|
|
|
// Putting the image data on the tmp layer
|
|
|
|
TMPLayer.context.putImageData(clipboardData, copiedStartX, copiedStartY);
|
|
|
|
|
|
|
|
// Setting up the move tool to move the pasted value
|
|
|
|
selectionCanceled = false;
|
2020-04-20 17:55:34 +03:00
|
|
|
imageDataToMove = clipboardData;
|
2020-04-20 20:26:00 +03:00
|
|
|
firstTimeMove = false;
|
|
|
|
isRectSelecting = false;
|
|
|
|
|
|
|
|
// Switching to the move tool
|
|
|
|
tool.moveselection.switchTo();
|
|
|
|
// Updating the rectangle preview
|
|
|
|
moveSelection(
|
|
|
|
copiedStartX + (copiedEndX - copiedStartX) / 2,
|
|
|
|
copiedStartY + (copiedEndY - copiedStartY) / 2,
|
2020-06-05 23:19:48 +03:00
|
|
|
clipboardData.width, clipboardData.height);
|
2020-04-20 20:26:00 +03:00
|
|
|
//drawRect(copiedStartX, copiedEndX, copiedStartY, copiedEndY);
|
2020-04-20 17:55:34 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 15:05:51 +03:00
|
|
|
/** Cuts the current selection and copies it to the clipboard
|
|
|
|
*
|
|
|
|
*/
|
2020-04-20 17:55:34 +03:00
|
|
|
function cutSelectionTool() {
|
2020-12-31 15:05:51 +03:00
|
|
|
// Saving the coordinates
|
2020-04-20 20:26:00 +03:00
|
|
|
copiedEndX = endX;
|
|
|
|
copiedEndY = endY;
|
|
|
|
|
|
|
|
copiedStartX = startX;
|
|
|
|
copiedStartY = startY;
|
|
|
|
|
2020-04-20 17:55:34 +03:00
|
|
|
// Getting the selected pixels
|
2020-12-31 15:05:51 +03:00
|
|
|
// If I'm already moving a selection
|
2020-04-20 20:26:00 +03:00
|
|
|
if (imageDataToMove !== undefined) {
|
2020-12-31 15:05:51 +03:00
|
|
|
// I just save that selection in the clipboard
|
2020-04-20 20:26:00 +03:00
|
|
|
clipboardData = imageDataToMove;
|
2020-12-31 15:05:51 +03:00
|
|
|
// And clear the underlying space
|
2020-04-20 20:26:00 +03:00
|
|
|
TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height);
|
2020-12-31 15:05:51 +03:00
|
|
|
// The image has been cleared, so I don't have anything to move anymore
|
2020-04-20 20:26:00 +03:00
|
|
|
imageDataToMove = undefined;
|
|
|
|
}
|
|
|
|
else {
|
2020-12-31 15:05:51 +03:00
|
|
|
// Otherwise, I copy the current selection into the clipboard
|
|
|
|
copySelection();
|
|
|
|
// And clear the selection
|
2020-04-20 20:26:00 +03:00
|
|
|
currentLayer.context.clearRect(startX - 0.5, startY - 0.5, endX - startX + 1, endY - startY + 1);
|
|
|
|
}
|
2020-04-20 17:55:34 +03:00
|
|
|
}
|