2020-03-04 16:36:40 +03:00
|
|
|
var isRectSelecting = false;
|
2020-03-04 17:46:25 +03:00
|
|
|
let startX;
|
|
|
|
let startY;
|
2020-03-05 15:34:29 +03:00
|
|
|
let endX;
|
|
|
|
let endY;
|
2020-03-04 16:36:40 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Starts the selection: saves the canvas, sets the start coordinates
|
|
|
|
*
|
|
|
|
* @param {*} mouseEvent
|
|
|
|
*/
|
2020-03-04 17:46:25 +03:00
|
|
|
function startRectSelection(mouseEvent) {
|
2020-04-04 10:41:56 +03:00
|
|
|
// Saving the canvas
|
2021-07-15 18:10:07 +03:00
|
|
|
new HistoryState().EditCanvas();
|
2020-04-04 10:41:56 +03:00
|
|
|
// Putting the vfx layer on top of everything
|
|
|
|
VFXCanvas.style.zIndex = MAX_Z_INDEX;
|
|
|
|
|
|
|
|
// Saving the start coords of the rect
|
|
|
|
let cursorPos = getCursorPosition(mouseEvent);
|
|
|
|
startX = Math.round(cursorPos[0] / zoom) - 0.5;
|
|
|
|
startY = Math.round(cursorPos[1] / zoom) - 0.5;
|
|
|
|
|
|
|
|
// Avoiding external selections
|
|
|
|
if (startX < 0) {
|
|
|
|
startX = 0;
|
|
|
|
}
|
|
|
|
else if (startX > currentLayer.canvas.width) {
|
|
|
|
startX = currentLayer.canvas.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startY < 0) {
|
|
|
|
startY = 0;
|
|
|
|
}
|
|
|
|
else if (startY > currentLayer.canvas.height) {
|
|
|
|
startY = currentLayer.canvas.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drawing the rect
|
|
|
|
drawRect(startX, startY);
|
|
|
|
selectionCanceled = false;
|
2020-03-04 16:36:40 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Updates the selection
|
|
|
|
*
|
|
|
|
* @param {*} mouseEvent
|
|
|
|
*/
|
2020-03-04 17:46:25 +03:00
|
|
|
function updateRectSelection(mouseEvent) {
|
2020-03-04 21:38:35 +03:00
|
|
|
let pos = getCursorPosition(mouseEvent);
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-04 17:46:25 +03:00
|
|
|
// Drawing the rect
|
2020-03-05 18:13:23 +03:00
|
|
|
drawRect(Math.round(pos[0] / zoom) + 0.5, Math.round(pos[1] / zoom) + 0.5);
|
2020-03-04 16:36:40 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Ends the selection: sets the end coordiantes
|
|
|
|
*
|
|
|
|
* @param {*} mouseEvent
|
|
|
|
*/
|
2020-03-04 17:46:25 +03:00
|
|
|
function endRectSelection(mouseEvent) {
|
2020-03-04 21:38:35 +03:00
|
|
|
// Getting the end position
|
|
|
|
let currentPos = getCursorPosition(mouseEvent);
|
2020-03-15 18:32:48 +03:00
|
|
|
endX = Math.round(currentPos[0] / zoom) + 0.5;
|
|
|
|
endY = Math.round(currentPos[1] / zoom) + 0.5;
|
2020-03-05 18:13:23 +03:00
|
|
|
|
|
|
|
// Inverting end and start (start must always be the top left corner)
|
|
|
|
if (endX < startX) {
|
|
|
|
let tmp = endX;
|
|
|
|
endX = startX;
|
|
|
|
startX = tmp;
|
|
|
|
}
|
|
|
|
// Same for the y
|
|
|
|
if (endY < startY) {
|
|
|
|
let tmp = endY;
|
|
|
|
endY = startY;
|
|
|
|
startY = tmp;
|
|
|
|
}
|
2020-03-04 21:38:35 +03:00
|
|
|
|
2020-04-09 17:48:19 +03:00
|
|
|
// Selecting the move tool
|
2020-04-15 03:01:31 +03:00
|
|
|
currentTool = tool.moveselection;
|
2020-04-09 17:48:19 +03:00
|
|
|
currentToolTemp = currentTool;
|
|
|
|
|
2020-03-05 18:13:23 +03:00
|
|
|
// Resetting this
|
2020-03-04 17:46:25 +03:00
|
|
|
isRectSelecting = false;
|
2020-04-09 17:48:19 +03:00
|
|
|
|
2020-04-15 03:01:31 +03:00
|
|
|
// Updating the cursor
|
|
|
|
currentTool.updateCursor();
|
2020-04-09 17:48:19 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Cuts the selection from its canvas and puts it in the tmp layer so it can be moved
|
|
|
|
*
|
|
|
|
* @param {*} mousePosition
|
|
|
|
*/
|
2020-04-20 17:55:34 +03:00
|
|
|
function cutSelection(mousePosition) {
|
2020-03-04 21:38:35 +03:00
|
|
|
// Getting the selected pixels
|
2020-03-15 18:32:48 +03:00
|
|
|
imageDataToMove = currentLayer.context.getImageData(startX, startY, endX - startX + 1, endY - startY + 1);
|
2020-04-20 20:26:00 +03:00
|
|
|
|
2020-04-09 18:20:40 +03:00
|
|
|
currentLayer.context.clearRect(startX - 0.5, startY - 0.5, endX - startX + 1, endY - startY + 1);
|
2020-03-05 18:13:23 +03:00
|
|
|
// Moving those pixels from the current layer to the tmp layer
|
2020-04-09 18:20:40 +03:00
|
|
|
TMPLayer.context.putImageData(imageDataToMove, startX + 1, startY);
|
2020-03-04 17:46:25 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Draws a dashed rectangle representing the selection
|
|
|
|
*
|
|
|
|
* @param {*} x Current end x coordinate of the selection
|
|
|
|
* @param {*} y Current end y coordinate of the selection
|
|
|
|
*/
|
2020-03-04 17:46:25 +03:00
|
|
|
function drawRect(x, y) {
|
2020-04-04 10:41:56 +03:00
|
|
|
// Getting the vfx context
|
|
|
|
let vfxContext = VFXCanvas.getContext('2d');
|
2020-03-04 17:46:25 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
// Clearing the vfx canvas
|
|
|
|
vfxContext.clearRect(0, 0, VFXCanvas.width, VFXCanvas.height);
|
|
|
|
vfxContext.lineWidth = 1;
|
|
|
|
vfxContext.strokeStyle = 'black';
|
|
|
|
vfxContext.setLineDash([4]);
|
2020-03-04 21:38:35 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
// Drawing the rect
|
|
|
|
vfxContext.beginPath();
|
|
|
|
vfxContext.rect(startX, startY, x - startX, y - startY);
|
2020-03-04 21:38:35 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
vfxContext.stroke();
|
2020-03-04 17:46:25 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 21:38:35 +03:00
|
|
|
function applyChanges() {
|
2020-04-04 10:41:56 +03:00
|
|
|
VFXCanvas.style.zIndex = MIN_Z_INDEX;
|
2020-03-05 15:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether the pointer is inside the selected area or not
|
|
|
|
function cursorInSelectedArea() {
|
2020-12-31 18:47:56 +03:00
|
|
|
// Getting the cursor position
|
2020-04-04 10:41:56 +03:00
|
|
|
let cursorPos = getCursorPosition(currentMouseEvent);
|
2020-12-31 18:47:56 +03:00
|
|
|
// Getting the coordinates relatively to the canvas
|
2020-04-04 10:41:56 +03:00
|
|
|
let x = cursorPos[0] / zoom;
|
|
|
|
let y = cursorPos[1] / zoom;
|
2020-03-05 15:34:29 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// This is to avoid rightX or topY being less than leftX or bottomY
|
2020-04-04 10:41:56 +03:00
|
|
|
let leftX = Math.min(startX, endX);
|
|
|
|
let rightX = Math.max(startX, endX);
|
|
|
|
let topY = Math.max(startY, endY);
|
|
|
|
let bottomY = Math.min(startY, endY);
|
2020-03-05 15:34:29 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
if (leftX <= x && x <= rightX) {
|
|
|
|
if (bottomY <= y && y <= topY) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-05 15:34:29 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-05 15:34:29 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
return false;
|
2020-03-05 18:13:23 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Moves the rect ants to the specified position
|
|
|
|
*
|
|
|
|
* @param {*} x X coordinate of the rect ants
|
|
|
|
* @param {*} y Y coordinat of the rect ants
|
|
|
|
* @param {*} width Width of the selection
|
|
|
|
* @param {*} height Height of the selectin
|
|
|
|
*/
|
2020-03-05 18:13:23 +03:00
|
|
|
function moveSelection(x, y, width, height) {
|
2020-04-04 10:41:56 +03:00
|
|
|
// Getting the vfx context
|
|
|
|
let vfxContext = VFXCanvas.getContext('2d');
|
2020-03-05 18:13:23 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
// Clearing the vfx canvas
|
|
|
|
vfxContext.clearRect(0, 0, VFXCanvas.width, VFXCanvas.height);
|
|
|
|
vfxContext.lineWidth = 1;
|
|
|
|
vfxContext.setLineDash([4]);
|
2020-03-05 18:13:23 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// Fixing the coordinates
|
2020-06-05 23:19:48 +03:00
|
|
|
startX = Math.round(Math.round(x) - 0.5 - Math.round(width / 2)) + 0.5;
|
|
|
|
startY = Math.round(Math.round(y) - 0.5 - Math.round(height / 2)) + 0.5;
|
2020-04-04 10:41:56 +03:00
|
|
|
endX = startX + Math.round(width);
|
|
|
|
endY = startY + Math.round(height);
|
2020-03-05 18:13:23 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
// Drawing the rect
|
|
|
|
vfxContext.beginPath();
|
|
|
|
vfxContext.rect(startX, startY, width, height);
|
2020-03-05 18:13:23 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
vfxContext.stroke();
|
2020-03-04 21:38:35 +03:00
|
|
|
}
|