pixel-editor/js/tools/MoveSelectionTool.js

170 lines
5.8 KiB
JavaScript
Raw Normal View History

2021-11-01 14:31:09 +03:00
class MoveSelectionTool extends Tool {
2021-11-09 00:25:30 +03:00
currSelection = undefined;
selectionTool = undefined;
endTool = undefined;
switchFunc = undefined;
2021-11-10 00:59:17 +03:00
lastCopiedSelection = undefined;
2021-11-10 19:39:32 +03:00
cutting = false;
2021-11-09 00:25:30 +03:00
constructor (name, options, switchFunc, endTool) {
2021-11-01 14:31:09 +03:00
super(name, options, switchFunc);
2021-11-09 00:25:30 +03:00
this.switchFunc = switchFunc;
this.endTool = endTool;
2021-11-10 00:59:17 +03:00
Events.onCustom("esc-pressed", this.endSelection.bind(this));
Events.onCustom("ctrl+c", this.copySelection.bind(this));
Events.onCustom("ctrl+x", this.cutSelection.bind(this));
Events.onCustom("ctrl+v", this.pasteSelection.bind(this));
}
copySelection() {
this.lastCopiedSelection = this.currSelection;
2021-11-10 19:39:32 +03:00
this.cutting = false;
2021-11-10 00:59:17 +03:00
}
cutSelection() {
2021-11-10 19:39:32 +03:00
this.cutting = true;
2021-11-10 00:59:17 +03:00
this.lastCopiedSelection = this.currSelection;
this.endSelection();
this.currSelection = this.lastCopiedSelection;
// Cut the data
currFile.currentLayer.context.clearRect(this.currSelection.left-0.5, this.currSelection.top-0.5,
2021-11-10 00:59:17 +03:00
this.currSelection.width, this.currSelection.height);
}
pasteSelection() {
if (this.lastCopiedSelection === undefined)
return;
// Finish the current selection and start a new one with the same data
2021-11-10 19:39:32 +03:00
if (!this.cutting) {
this.endSelection();
}
this.cutting = false;
2021-11-10 00:59:17 +03:00
this.switchFunc(this);
this.currSelection = this.lastCopiedSelection;
// Putting the vfx layer on top of everything
currFile.VFXLayer.canvas.style.zIndex = MAX_Z_INDEX;
2021-11-10 00:59:17 +03:00
this.onDrag(this.currMousePos);
new HistoryState().EditCanvas();
2021-11-01 14:31:09 +03:00
}
2021-11-01 15:02:18 +03:00
2021-11-10 19:39:32 +03:00
onStart(mousePos, mouseTarget) {
super.onStart(mousePos, mouseTarget);
2021-11-09 00:25:30 +03:00
2021-11-10 19:39:32 +03:00
if (!this.cursorInSelectedArea(mousePos) &&
!Util.isChildOfByClass(mouseTarget, "editor-top-menu")) {
2021-11-09 00:25:30 +03:00
this.endSelection();
}
2021-11-01 15:02:18 +03:00
}
onDrag(mousePos) {
super.onDrag(mousePos);
2021-11-09 00:25:30 +03:00
this.currSelection = this.selectionTool.moveAnts(mousePos[0]/currFile.zoom,
mousePos[1]/currFile.zoom, this.currSelection.width, this.currSelection.height);
2021-11-10 00:59:17 +03:00
// clear the entire tmp layer
currFile.TMPLayer.context.clearRect(0, 0, currFile.TMPLayer.canvas.width, currFile.TMPLayer.canvas.height);
2021-11-10 00:59:17 +03:00
// put the image data on the tmp layer with offset
currFile.TMPLayer.context.putImageData(
2021-11-10 00:59:17 +03:00
this.currSelection.data,
Math.round(mousePos[0] / currFile.zoom) - this.currSelection.width / 2,
Math.round(mousePos[1] / currFile.zoom) - this.currSelection.height / 2);
2021-11-01 15:02:18 +03:00
}
onEnd(mousePos) {
super.onEnd(mousePos);
}
onSelect() {
2021-11-09 00:25:30 +03:00
super.onSelect();
2021-11-01 15:02:18 +03:00
}
onDeselect() {
2021-11-09 00:25:30 +03:00
super.onDeselect();
}
setSelectionData(data, tool) {
this.currSelection = data;
this.selectionTool = tool;
this.firstTimeMove = true;
}
onHover(mousePos) {
super.onHover(mousePos);
if (this.cursorInSelectedArea(mousePos)) {
currFile.canvasView.style.cursor = 'move';
2021-11-09 00:25:30 +03:00
}
else {
currFile.canvasView.style.cursor = 'default';
2021-11-09 00:25:30 +03:00
}
}
cursorInSelectedArea(cursorPos) {
// Getting the coordinates relatively to the canvas
let x = cursorPos[0] / currFile.zoom;
let y = cursorPos[1] / currFile.zoom;
2021-11-09 00:25:30 +03:00
if (this.currSelection.left <= x && x <= this.currSelection.right) {
if (y <= this.currSelection.bottom && y >= this.currSelection.top) {
return true;
}
return false;
}
return false;
}
endSelection() {
2021-11-10 00:59:17 +03:00
if (this.currSelection == undefined)
return;
2021-11-09 00:25:30 +03:00
// Clearing the tmp (move preview) and vfx (ants) layers
currFile.TMPLayer.context.clearRect(0, 0, currFile.TMPLayer.canvas.width, currFile.TMPLayer.canvas.height);
currFile.VFXLayer.context.clearRect(0, 0, currFile.VFXLayer.canvas.width, currFile.VFXLayer.canvas.height);
2021-11-09 00:25:30 +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
let underlyingImageData = currFile.currentLayer.context.getImageData(
2021-11-09 00:25:30 +03:00
this.currSelection.left, this.currSelection.top,
this.currSelection.width+1, this.currSelection.height+1
2021-11-09 00:25:30 +03:00
);
2021-11-10 00:59:17 +03:00
let pasteData = this.currSelection.data.data.slice();
2021-11-09 00:25:30 +03:00
for (let i=0; i<underlyingImageData.data.length; i+=4) {
let currentMovePixel = [
pasteData[i], pasteData[i+1], pasteData[i+2], pasteData[i+3]
2021-11-09 00:25:30 +03:00
];
let currentUnderlyingPixel = [
underlyingImageData.data[i], underlyingImageData.data[i+1],
underlyingImageData.data[i+2], underlyingImageData.data[i+3]
];
// If the pixel of the clipboard is empty, but the one below it isn't, I use the pixel below
if (Util.isPixelEmpty(currentMovePixel)) {
if (!Util.isPixelEmpty(currentUnderlyingPixel)) {
pasteData[i] = currentUnderlyingPixel[0];
pasteData[i+1] = currentUnderlyingPixel[1];
pasteData[i+2] = currentUnderlyingPixel[2];
pasteData[i+3] = currentUnderlyingPixel[3];
2021-11-09 00:25:30 +03:00
}
}
}
currFile.currentLayer.context.putImageData(new ImageData(pasteData, this.currSelection.width+1),
this.currSelection.left, this.currSelection.top
);
2021-11-09 00:25:30 +03:00
this.currSelection = undefined;
currFile.currentLayer.updateLayerPreview();
currFile.VFXLayer.canvas.style.zIndex = MIN_Z_INDEX;
2021-11-09 00:25:30 +03:00
// Switch to brush
this.switchFunc(this.endTool);
2021-11-01 15:02:18 +03:00
}
2021-11-01 14:31:09 +03:00
}