Fixed cut bug, deleted _copyPaste.js

This commit is contained in:
unsettledgames 2021-11-10 17:39:32 +01:00
parent b4c069c156
commit 1d33259abf
4 changed files with 15 additions and 93 deletions

View File

@ -55,13 +55,13 @@ const TopMenuModule = (() => {
break;
// REFACTOR: move the binding to the Selection IIFE or something like that once it's done
case 'Paste':
Events.on('click', currSubmenuButton, pasteSelection);
Events.on('click', currSubmenuButton, function(){Events.emit("ctrl+v");});
break;
case 'Copy':
Events.on('click', currSubmenuButton, copySelection);
Events.on('click', currSubmenuButton, function(){Events.emit("ctrl+c");});
break;
case 'Cut':
Events.on('click', currSubmenuButton, cutSelectionTool);
Events.on('click', currSubmenuButton, function(){Events.emit("ctrl+x");});
break;
case 'Cancel':
//Events.on('click', currSubmenuButton, tool.pencil.switchTo);

View File

@ -1,85 +0,0 @@
// Data saved when copying or cutting
let clipboardData;
// Tells if the user is pasting something or not
let isPasting = false;
// Coordinates of the copied (or cut) selection
let copiedStartX;
let copiedStartY;
let copiedEndX;
let copiedEndY;
/** Copies the current selection to the clipboard
*
*/
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);
}
/** Pastes the clipboard data onto the current layer
*
*/
function pasteSelection() {
// Can't paste if the layer is locked
if (currentLayer.isLocked) {
return;
}
// Cancel the current selection
endSelection();
// I'm pasting
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;
imageDataToMove = clipboardData;
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,
clipboardData.width, clipboardData.height);
//drawRect(copiedStartX, copiedEndX, copiedStartY, copiedEndY);
}
/** Cuts the current selection and copies it to the clipboard
*
*/
function cutSelectionTool() {
// Saving the coordinates
copiedEndX = endX;
copiedEndY = endY;
copiedStartX = startX;
copiedStartY = startY;
// Getting the selected pixels
// If I'm already moving a selection
if (imageDataToMove !== undefined) {
// I just save that selection in the clipboard
clipboardData = imageDataToMove;
// And clear the underlying space
TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height);
// The image has been cleared, so I don't have anything to move anymore
imageDataToMove = undefined;
}
else {
// Otherwise, I copy the current selection into the clipboard
copySelection();
// And clear the selection
currentLayer.context.clearRect(startX - 0.5, startY - 0.5, endX - startX + 1, endY - startY + 1);
}
}

View File

@ -44,7 +44,6 @@
/**functions**/
//=include _checkerboard.js
//=include _copyPaste.js
//=include _resizeCanvas.js
//=include _resizeSprite.js
//=include _colorPicker.js

View File

@ -4,6 +4,7 @@ class MoveSelectionTool extends Tool {
endTool = undefined;
switchFunc = undefined;
lastCopiedSelection = undefined;
cutting = false;
constructor (name, options, switchFunc, endTool) {
super(name, options, switchFunc);
@ -20,9 +21,11 @@ class MoveSelectionTool extends Tool {
copySelection() {
this.lastCopiedSelection = this.currSelection;
this.cutting = false;
}
cutSelection() {
this.cutting = true;
this.lastCopiedSelection = this.currSelection;
this.endSelection();
this.currSelection = this.lastCopiedSelection;
@ -35,7 +38,11 @@ class MoveSelectionTool extends Tool {
if (this.lastCopiedSelection === undefined)
return;
// Finish the current selection and start a new one with the same data
this.endSelection();
if (!this.cutting) {
this.endSelection();
}
this.cutting = false;
this.switchFunc(this);
this.currSelection = this.lastCopiedSelection;
@ -46,10 +53,11 @@ class MoveSelectionTool extends Tool {
new HistoryState().EditCanvas();
}
onStart(mousePos) {
super.onStart(mousePos);
onStart(mousePos, mouseTarget) {
super.onStart(mousePos, mouseTarget);
if (!this.cursorInSelectedArea(mousePos)) {
if (!this.cursorInSelectedArea(mousePos) &&
!Util.isChildOfByClass(mouseTarget, "editor-top-menu")) {
this.endSelection();
}
}