pixel-editor/js/tools/RectangularSelectionTool.js

121 lines
4.9 KiB
JavaScript
Raw Normal View History

2021-11-01 15:02:18 +03:00
class RectangularSelectionTool extends SelectionTool {
2021-11-09 00:25:30 +03:00
constructor (name, options, switchFunc, moveTool) {
super(name, options, switchFunc, moveTool);
2021-11-01 15:02:18 +03:00
Events.on('click', this.mainButton, switchFunc, this);
2022-01-30 02:09:43 +03:00
this.resetTutorial();
this.addTutorialTitle("Rectangular selection tool");
this.addTutorialKey("M", " to select the rectangular selection tool");
this.addTutorialKey("Left drag", " to select a rectangular area");
this.addTutorialKey("Left drag", " to move a selection");
this.addTutorialKey("Esc", " to cancel a selection");
this.addTutorialKey("Click", " outside the selection to cancel it");
this.addTutorialKey("CTRL+C", " to copy a selection");
this.addTutorialKey("CTRL+V", " to paste a selection");
this.addTutorialKey("CTRL+X", " to cut a selection");
this.addTutorialImg("rectselect-tutorial.gif");
2021-11-01 15:02:18 +03:00
}
2022-01-22 14:18:10 +03:00
onStart(mousePos, mouseTarget) {
super.onStart(mousePos, mouseTarget);
if (Util.isChildOfByClass(mouseTarget, "editor-top-menu") ||
!Util.cursorInCanvas(currFile.canvasSize, [mousePos[0]/currFile.zoom, mousePos[1]/currFile.zoom]))
return;
2021-11-01 15:02:18 +03:00
// Avoiding external selections
if (this.startMousePos[0] < 0) {
this.startMousePos[0] = 0;
}
else if (this.startMousePos[0] > currFile.currentLayer.canvas.width) {
this.startMousePos[0] = currFile.currentLayer.canvas.width;
2021-11-01 15:02:18 +03:00
}
if (this.startMousePos[1] < 0) {
this.startMousePos[1] = 0;
}
else if (this.startMousePos[1] > currFile.currentLayer.canvas.height) {
this.startMousePos[1] = currFile.currentLayer.canvas.height;
2021-11-01 15:02:18 +03:00
}
// Drawing the rect
2021-11-09 00:25:30 +03:00
this.drawSelection(this.startMousePos[0], this.startMousePos[1]);
2021-11-01 15:02:18 +03:00
}
2022-01-22 14:18:10 +03:00
onDrag(mousePos, mouseTarget) {
super.onDrag(mousePos, mouseTarget);
if (Util.isChildOfByClass(mouseTarget, "editor-top-menu") ||
!Util.cursorInCanvas(currFile.canvasSize, [mousePos[0]/currFile.zoom, mousePos[1]/currFile.zoom]))
return;
2021-11-01 15:02:18 +03:00
// Drawing the rect
this.endMousePos = [Math.floor(mousePos[0] / currFile.zoom), Math.floor(mousePos[1] / currFile.zoom)];
this.drawSelection(Math.floor(mousePos[0] / currFile.zoom), Math.floor(mousePos[1] / currFile.zoom));
2021-11-01 15:02:18 +03:00
}
2022-01-22 14:18:10 +03:00
onEnd(mousePos, mouseTarget) {
super.onEnd(mousePos, mouseTarget);
2022-01-07 01:12:09 +03:00
if (Util.isChildOfByClass(mouseTarget, "editor-top-menu"))
2022-01-22 14:18:10 +03:00
return;
new HistoryState().EditCanvas();
2021-11-01 15:02:18 +03:00
// Getting the end position
this.endMousePos = [Math.floor(mousePos[0] / currFile.zoom), Math.floor(mousePos[1] / currFile.zoom)];
2021-11-01 15:02:18 +03:00
// Inverting end and start (start must always be the top left corner)
if (this.endMousePos[0] < this.startMousePos[0]) {
let tmp = this.endMousePos[0];
this.endMousePos[0] = this.startMousePos[0];
this.startMousePos[0] = tmp;
}
// Same for the y
if (this.endMousePos[1] < this.startMousePos[1]) {
let tmp = this.endMousePos[1];
this.endMousePos[1] = this.startMousePos[1];
this.startMousePos[1] = tmp;
}
2021-11-09 00:25:30 +03:00
if (Util.cursorInCanvas(currFile.canvasSize, [mousePos[0]/currFile.zoom, mousePos[1]/currFile.zoom])) {
this.boundingBox.minX = this.startMousePos[0] - 1;
this.boundingBox.maxX = this.endMousePos[0] + 1;
this.boundingBox.minY = this.startMousePos[1] - 1;
this.boundingBox.maxY = this.endMousePos[1] + 1;
}
2021-11-09 00:25:30 +03:00
// Switch to the move tool so that the user can move the selection
this.switchFunc(this.moveTool);
2022-01-07 01:12:09 +03:00
// Obtain the selected pixels
this.moveTool.setSelectionData(this.getSelection(), this);
2021-11-01 15:02:18 +03:00
}
cutSelection() {
super.cutSelection();
currFile.currentLayer.context.clearRect(this.currSelection.left-0.5, this.currSelection.top-0.5,
this.currSelection.width, this.currSelection.height);
}
2021-11-01 15:02:18 +03:00
onSelect() {
super.onSelect();
}
onDeselect() {
super.onDeselect();
}
drawSelection() {
2021-11-01 15:02:18 +03:00
// Getting the vfx context
let vfxContext = currFile.VFXLayer.context;
2021-11-01 15:02:18 +03:00
// Clearing the vfx canvas
vfxContext.clearRect(0, 0, currFile.VFXLayer.canvas.width, currFile.VFXLayer.canvas.height);
currFile.VFXLayer.drawLine(this.startMousePos[0], this.startMousePos[1], this.endMousePos[0], this.startMousePos[1], 1);
currFile.VFXLayer.drawLine(this.endMousePos[0], this.startMousePos[1], this.endMousePos[0], this.endMousePos[1], 1);
currFile.VFXLayer.drawLine(this.endMousePos[0], this.endMousePos[1], this.startMousePos[0], this.endMousePos[1], 1);
currFile.VFXLayer.drawLine(this.startMousePos[0], this.endMousePos[1], this.startMousePos[0], this.startMousePos[1], 1);
2021-11-01 14:31:09 +03:00
}
}