pixel-editor/js/tools/MoveSelectionTool.js

140 lines
4.5 KiB
JavaScript
Raw Normal View History

class MoveSelectionTool extends DrawingTool {
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));
2022-01-22 14:18:10 +03:00
Events.onCustom("ctrl+c", this.copySelection.bind(this), true);
Events.onCustom("ctrl+x", this.cutSelection.bind(this), true);
2021-11-10 00:59:17 +03:00
Events.onCustom("ctrl+v", this.pasteSelection.bind(this));
}
2022-01-22 14:18:10 +03:00
copySelection(event) {
2021-11-10 00:59:17 +03:00
this.lastCopiedSelection = this.currSelection;
2021-11-10 19:39:32 +03:00
this.cutting = false;
2022-01-22 14:18:10 +03:00
if (event)
this.switchFunc(this.selectionTool);
2021-11-10 00:59:17 +03:00
}
2022-01-22 14:18:10 +03:00
cutSelection(event) {
if (currFile.currentLayer.isLocked)
return;
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
this.selectionTool.cutSelection();
2022-01-22 14:18:10 +03:00
if (event)
this.switchFunc(this.selectionTool);
new HistoryState().EditCanvas();
2021-11-10 00:59:17 +03:00
}
pasteSelection() {
if (currFile.currentLayer.isLocked)
return;
2021-11-10 00:59:17 +03:00
if (this.lastCopiedSelection === undefined)
return;
if (!(this.currMousePos[0]/currFile.zoom >= 0 && this.currMousePos[1]/currFile.zoom >= 0 &&
this.currMousePos[0]/currFile.zoom < currFile.canvasSize[0] && this.currMousePos[1]/currFile.zoom < currFile.canvasSize[1]))
this.currMousePos = [currFile.canvasSize[0]*currFile.zoom / 2, currFile.canvasSize[1]*currFile.zoom /2];
2021-11-10 00:59:17 +03:00
// 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;
2022-01-22 14:18:10 +03:00
this.selectionTool.drawSelectedArea();
2021-11-10 00:59:17 +03:00
// 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-01 15:02:18 +03:00
}
onDrag(mousePos) {
super.onDrag(mousePos);
2021-11-09 00:25:30 +03:00
this.selectionTool.moveOffset =
[Math.floor(mousePos[0] / currFile.zoom - currFile.canvasSize[0] / 2 - (this.selectionTool.boundingBoxCenter[0] - currFile.canvasSize[0]/2)),
Math.floor(mousePos[1] / currFile.zoom - currFile.canvasSize[1] / 2- (this.selectionTool.boundingBoxCenter[1] - currFile.canvasSize[1]/2))];
2021-11-10 00:59:17 +03:00
// clear the entire tmp layer
2022-01-07 01:12:09 +03:00
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
2022-01-07 01:12:09 +03:00
currFile.TMPLayer.context.putImageData(this.currSelection,
this.selectionTool.moveOffset[0], this.selectionTool.moveOffset[1]);
// Draw the selection area and outline
this.selectionTool.drawOutline();
this.selectionTool.drawSelectedArea();
2022-01-22 14:18:10 +03:00
this.selectionTool.drawBoundingBox();
2021-11-01 15:02:18 +03:00
}
onEnd(mousePos, mouseTarget) {
super.onEnd(mousePos, mouseTarget);
if (!this.selectionTool.cursorInSelectedArea(mousePos) &&
!Util.isChildOfByClass(mouseTarget, "editor-top-menu")) {
this.endSelection();
// Switch to selection tool
this.switchFunc(this.selectionTool);
}
2021-11-01 15:02:18 +03:00
}
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();
2022-01-22 14:18:10 +03:00
this.endSelection();
2021-11-09 00:25:30 +03:00
}
setSelectionData(data, tool) {
this.currSelection = data;
this.selectionTool = tool;
this.firstTimeMove = true;
}
onHover(mousePos) {
super.onHover(mousePos);
if (this.selectionTool.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
}
}
2022-01-22 14:18:10 +03:00
endSelection(event) {
2021-11-10 00:59:17 +03:00
if (this.currSelection == undefined)
return;
2021-11-09 00:25:30 +03:00
this.currSelection = undefined;
this.selectionTool.pasteSelection();
2022-01-22 14:18:10 +03:00
if (event)
this.switchFunc(this.selectionTool);
2021-11-01 15:02:18 +03:00
}
2021-11-01 14:31:09 +03:00
}