pixel-editor/js/ToolManager.js

154 lines
5.5 KiB
JavaScript
Raw Normal View History

const ToolManager = (() => {
tools = {};
2021-11-09 14:53:19 +03:00
isPanning = false;
tools["brush"] = new BrushTool("brush", {type: 'html'}, switchTool);
tools["eraser"] = new EraserTool("eraser", {type: 'html'}, switchTool);
tools["rectangle"] = new RectangleTool("rectangle", {type: 'html'}, switchTool);
tools["line"] = new LineTool("line", {type: 'html'}, switchTool);
tools["fill"] = new FillTool("fill", {type: 'cursor', style: 'crosshair'}, switchTool);
2021-11-01 14:31:09 +03:00
tools["eyedropper"] = new EyedropperTool("eyedropper", {type: 'cursor', style: 'crosshair'}, switchTool);
tools["pan"] = new PanTool("pan", {type: 'custom'}, switchTool);
tools["zoom"] = new ZoomTool("zoom", {type:'custom'});
tools["moveselection"] = new MoveSelectionTool("moveselection",
{type:'cursor', style:'crosshair'}, switchTool, tools["brush"]);
tools["rectselect"] = new RectangularSelectionTool("rectselect",
{type: 'cursor', style:'crosshair'}, switchTool, tools["moveselection"]);
2021-11-09 00:25:30 +03:00
currTool = tools["brush"];
2021-10-31 20:03:21 +03:00
currTool.onSelect();
currFile.canvasView.style.cursor = 'default';
2021-10-27 11:02:21 +03:00
Events.on("mouseup", window, onMouseUp);
Events.on("mousemove", window, onMouseMove);
Events.on("mousedown", window, onMouseDown);
2021-12-12 20:37:16 +03:00
Events.on("wheel", window, onMouseWheel);
2021-11-01 14:31:09 +03:00
2021-11-09 14:53:19 +03:00
// Bind tool shortcuts
Events.onCustom("tool-shortcut", onShortcut);
function onShortcut(tool) {
if (!EditorState.documentCreated || Dialogue.isOpen())
2021-12-06 13:26:42 +03:00
return;
switchTool(tools[tool]);
}
2021-11-01 14:31:09 +03:00
function onMouseWheel(mouseEvent) {
2021-12-12 20:37:16 +03:00
console.log("MOUSE WHEEL");
if (!EditorState.documentCreated || Dialogue.isOpen())
2021-12-06 13:26:42 +03:00
return;
2021-11-01 14:31:09 +03:00
let mousePos = Input.getCursorPosition(mouseEvent);
tools["zoom"].onMouseWheel(mousePos, mouseEvent.deltaY < 0 ? 'in' : 'out');
2021-11-01 14:31:09 +03:00
}
2021-10-27 11:02:21 +03:00
function onMouseDown(mouseEvent) {
if (!EditorState.documentCreated() || Dialogue.isOpen())
2021-10-27 11:02:21 +03:00
return;
let mousePos = Input.getCursorPosition(mouseEvent);
2021-10-31 14:49:38 +03:00
2021-11-09 01:12:51 +03:00
if (!Input.isDragging()) {
switch(mouseEvent.which) {
case 1:
2021-11-09 14:53:19 +03:00
if (Input.isSpacePressed()) {
tools["pan"].onStart(mousePos, mouseEvent.target);
}
else if (Input.isAltPressed()) {
tools["eyedropper"].onStart(mousePos, mouseEvent.target);
}
else if (!currFile.currentLayer.isLocked || !((Object.getPrototypeOf(currTool) instanceof DrawingTool))) {
2021-11-09 14:53:19 +03:00
currTool.onStart(mousePos, mouseEvent.target);
}
2021-11-09 01:12:51 +03:00
break;
case 2:
tools["pan"].onStart(mousePos, mouseEvent.target);
2021-11-09 01:12:51 +03:00
break;
case 3:
currTool.onRightStart(mousePos, mouseEvent.target);
break;
default:
break;
}
}
2021-10-27 11:02:21 +03:00
}
function onMouseMove(mouseEvent) {
if (!EditorState.documentCreated() || Dialogue.isOpen())
2021-10-27 11:02:21 +03:00
return;
let mousePos = Input.getCursorPosition(mouseEvent);
// Call the hover event
currTool.onHover(mousePos, mouseEvent.target);
if (Input.isDragging()) {
2021-12-12 20:37:16 +03:00
switch (mouseEvent.buttons) {
2021-11-09 01:12:51 +03:00
case 1:
2021-11-09 14:53:19 +03:00
if (Input.isSpacePressed()) {
tools["pan"].onDrag(mousePos, mouseEvent.target);
}
else if (Input.isAltPressed()) {
tools["eyedropper"].onDrag(mousePos, mouseEvent.target);
}
else if (!currFile.currentLayer.isLocked || !((Object.getPrototypeOf(currTool) instanceof DrawingTool))){
2021-11-09 14:53:19 +03:00
currTool.onDrag(mousePos, mouseEvent.target);
}
2021-11-09 01:12:51 +03:00
break;
2021-12-12 20:37:16 +03:00
case 4:
tools["pan"].onDrag(mousePos, mouseEvent.target);
2021-11-09 01:12:51 +03:00
break;
2021-12-12 20:37:16 +03:00
case 2:
2021-11-09 01:12:51 +03:00
currTool.onRightDrag(mousePos, mouseEvent.target);
break;
default:
console.log("wtf");
break;
}
2021-10-27 11:02:21 +03:00
}
}
2021-10-27 11:02:21 +03:00
function onMouseUp(mouseEvent) {
if (!EditorState.documentCreated())
2021-10-27 11:02:21 +03:00
return;
let mousePos = Input.getCursorPosition(mouseEvent);
2021-11-09 01:12:51 +03:00
if (Input.isDragging()) {
switch(mouseEvent.which) {
case 1:
2021-11-09 14:53:19 +03:00
if (Input.isSpacePressed()) {
tools["pan"].onEnd(mousePos, mouseEvent.target);
}
else if (Input.isAltPressed()) {
tools["eyedropper"].onEnd(mousePos, mouseEvent.target);
}
else if (!currFile.currentLayer.isLocked || !((Object.getPrototypeOf(currTool) instanceof DrawingTool))) {
2021-11-09 14:53:19 +03:00
currTool.onEnd(mousePos);
}
2021-11-09 01:12:51 +03:00
break;
case 2:
tools["pan"].onEnd(mousePos);
2021-11-09 01:12:51 +03:00
break;
case 3:
currTool.onRightEnd(mousePos, mouseEvent.target);
break;
default:
break;
}
2021-10-27 11:02:21 +03:00
}
}
function currentTool() {
return currTool;
}
2021-10-27 11:43:51 +03:00
function switchTool(newTool) {
currTool.onDeselect();
currTool = newTool;
currTool.onSelect();
}
2021-10-27 11:02:21 +03:00
return {
currentTool
}
2021-10-27 11:02:21 +03:00
})();