2021-07-22 20:26:40 +03:00
|
|
|
// REFACTOR: let's keep this one for the end
|
2020-03-05 15:34:29 +03:00
|
|
|
var imageDataToMove;
|
2020-03-15 19:11:00 +03:00
|
|
|
var originalDataPosition;
|
2020-03-05 18:13:23 +03:00
|
|
|
var canMoveSelection = false;
|
2020-03-08 00:34:12 +03:00
|
|
|
var lastMovePos;
|
2020-04-09 17:48:19 +03:00
|
|
|
var selectionCanceled = true;
|
|
|
|
var firstTimeMove = true;
|
2020-03-05 15:34:29 +03:00
|
|
|
|
2020-03-05 18:13:23 +03:00
|
|
|
// TODO: move with arrows
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Updates the move preview so that is placed in the right position
|
|
|
|
*
|
|
|
|
* @param {*} mousePosition The position of the cursor
|
|
|
|
*/
|
2020-04-20 17:55:34 +03:00
|
|
|
function updateMovePreview(mousePosition) {
|
2020-12-31 18:47:56 +03:00
|
|
|
// I haven't canceled the selection
|
2020-04-04 10:41:56 +03:00
|
|
|
selectionCanceled = false;
|
2020-04-09 17:48:19 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// If it's the first time that I move the selection, I cut it from its original position
|
2020-04-04 10:41:56 +03:00
|
|
|
if (firstTimeMove) {
|
2020-04-20 17:55:34 +03:00
|
|
|
cutSelection(mousePosition);
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
2020-04-09 17:48:19 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
firstTimeMove = false;
|
2020-04-09 17:48:19 +03:00
|
|
|
|
2020-04-20 17:55:34 +03:00
|
|
|
lastMousePos = mousePosition;
|
2020-04-04 10:41:56 +03:00
|
|
|
// clear the entire tmp layer
|
|
|
|
TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height);
|
2020-12-31 18:47:56 +03:00
|
|
|
// put the image data on the tmp layer with offset
|
2020-04-04 10:41:56 +03:00
|
|
|
TMPLayer.context.putImageData(
|
|
|
|
imageDataToMove,
|
2020-06-05 23:19:48 +03:00
|
|
|
Math.round(lastMousePos[0] / zoom) - imageDataToMove.width / 2,
|
|
|
|
Math.round(lastMousePos[1] / zoom) - imageDataToMove.height / 2);
|
2020-03-05 18:13:23 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
lastMovePos = lastMousePos;
|
2020-12-31 18:47:56 +03:00
|
|
|
// Moving the the rectangular ants
|
2020-04-04 10:41:56 +03:00
|
|
|
moveSelection(lastMousePos[0] / zoom, lastMousePos[1] / zoom, imageDataToMove.width, imageDataToMove.height);
|
2020-03-05 18:13:23 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Ends a selection, meaning that it makes the changes definitive and creates the history states
|
|
|
|
*
|
|
|
|
*/
|
2020-03-05 18:13:23 +03:00
|
|
|
function endSelection() {
|
2020-12-31 18:47:56 +03:00
|
|
|
// Clearing the tmp (move preview) and vfx (ants) layers
|
2020-04-04 10:41:56 +03:00
|
|
|
TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height);
|
|
|
|
VFXLayer.context.clearRect(0, 0, VFXLayer.canvas.width, VFXLayer.canvas.height);
|
2020-12-31 18:47:56 +03:00
|
|
|
// Preparing an empty imageData with the size of the canvas
|
2020-06-05 23:19:48 +03:00
|
|
|
let cleanImageData = new ImageData(endX - startX, endY - startY);
|
2020-03-05 18:13:23 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// If I was moving something
|
2020-04-04 10:41:56 +03:00
|
|
|
if (imageDataToMove !== undefined) {
|
2020-06-05 23:19:48 +03:00
|
|
|
// Saving the current clipboard before editing it in order to merge it with the current layer
|
|
|
|
cleanImageData.data.set(imageDataToMove.data);
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// I have to save the underlying data, so that the transparent pixels in the clipboard
|
|
|
|
// don't override the coloured pixels in the canvas
|
2020-04-04 10:41:56 +03:00
|
|
|
let underlyingImageData = currentLayer.context.getImageData(startX, startY, endX - startX, endY - startY);
|
2020-04-09 17:48:19 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
for (let i=0; i<underlyingImageData.data.length; i+=4) {
|
|
|
|
let currentMovePixel = [
|
|
|
|
imageDataToMove.data[i], imageDataToMove.data[i+1],
|
|
|
|
imageDataToMove.data[i+2], imageDataToMove.data[i+3]
|
|
|
|
];
|
2020-03-08 01:13:35 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
let currentUnderlyingPixel = [
|
|
|
|
underlyingImageData.data[i], underlyingImageData.data[i+1],
|
|
|
|
underlyingImageData.data[i+2], underlyingImageData.data[i+3]
|
|
|
|
];
|
2020-03-08 01:13:35 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// If the pixel of the clipboard is empty, but the one below it isn't, I use the pixel below
|
2020-04-04 10:41:56 +03:00
|
|
|
if (isPixelEmpty(currentMovePixel)) {
|
|
|
|
if (!isPixelEmpty(underlyingImageData)) {
|
|
|
|
imageDataToMove.data[i] = currentUnderlyingPixel[0];
|
|
|
|
imageDataToMove.data[i+1] = currentUnderlyingPixel[1];
|
|
|
|
imageDataToMove.data[i+2] = currentUnderlyingPixel[2];
|
|
|
|
imageDataToMove.data[i+3] = currentUnderlyingPixel[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 01:13:35 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// If I moved the selection before confirming it
|
2020-04-04 10:41:56 +03:00
|
|
|
if (lastMovePos !== undefined) {
|
2020-12-31 18:47:56 +03:00
|
|
|
// I put it in the new position
|
2020-04-04 10:41:56 +03:00
|
|
|
currentLayer.context.putImageData(
|
|
|
|
imageDataToMove,
|
2020-06-05 23:19:48 +03:00
|
|
|
Math.round(lastMovePos[0] / zoom) - imageDataToMove.width / 2,
|
|
|
|
Math.round(lastMovePos[1] / zoom) - imageDataToMove.height / 2);
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
2020-04-20 20:26:00 +03:00
|
|
|
else {
|
2020-12-31 18:47:56 +03:00
|
|
|
// I put it in the same position
|
2020-06-05 23:19:48 +03:00
|
|
|
currentLayer.context.putImageData(
|
|
|
|
imageDataToMove,
|
|
|
|
copiedStartX,
|
|
|
|
copiedStartY);
|
2020-04-20 20:26:00 +03:00
|
|
|
}
|
2020-06-05 23:19:48 +03:00
|
|
|
|
|
|
|
imageDataToMove.data.set(cleanImageData.data);
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
2020-03-08 01:13:35 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// Resetting all the flags
|
2020-04-04 10:41:56 +03:00
|
|
|
selectionCanceled = true;
|
|
|
|
isRectSelecting = false;
|
|
|
|
firstTimeMove = true;
|
|
|
|
imageDataToMove = undefined;
|
2020-04-20 20:26:00 +03:00
|
|
|
isPasting = false;
|
2020-06-05 23:19:48 +03:00
|
|
|
isCutting = false;
|
2020-06-06 22:44:52 +03:00
|
|
|
lastMovePos = undefined;
|
2020-06-19 16:16:22 +03:00
|
|
|
currentLayer.updateLayerPreview();
|
2020-06-05 23:28:28 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// Saving the history
|
2021-07-15 18:10:07 +03:00
|
|
|
new HistoryState().EditCanvas();
|
2020-03-05 15:34:29 +03:00
|
|
|
}
|