pixel-editor/js/ToolManager.js

97 lines
2.9 KiB
JavaScript
Raw Normal View History

const ToolManager = (() => {
2021-10-31 20:03:21 +03:00
brushTool = new BrushTool("brush", {type: 'html'}, switchTool);
eraserTool = new EraserTool("eraser", {type: 'html'}, switchTool);
rectangleTool = new RectangleTool("rectangle", {type: 'html'}, switchTool);
lineTool = new LineTool("line", {type: 'html'}, switchTool);
fillTool = new FillTool("fill", {type: 'cursor', style: 'crosshair'}, switchTool);
2021-11-01 14:31:09 +03:00
eyedropperTool = new EyedropperTool("eyedropper", {type: 'cursor', style: 'crosshair'}, switchTool);
panTool = new PanTool("pan", {type: 'custom'}, switchTool);
2021-11-01 14:31:09 +03:00
zoomTool = new ZoomTool("zoom", {type:'custom'});
2021-11-01 15:02:18 +03:00
rectSelectTool = new RectangularSelectionTool("rectselect",
2021-11-01 14:31:09 +03:00
{type: 'cursor', style:'crosshair'}, switchTool);
moveSelectionTool = new MoveSelectionTool("moveselection",
{type:'cursor', style:'crosshair'}, switchTool);
2021-10-27 11:43:51 +03:00
2021-10-31 20:03:21 +03:00
currTool = brushTool;
currTool.onSelect();
2021-10-31 20:31:45 +03:00
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-11-01 14:31:09 +03:00
Events.on("mousewheel", window, onMouseWheel);
function onMouseWheel(mouseEvent) {
let mousePos = Input.getCursorPosition(mouseEvent);
zoomTool.onMouseWheel(mousePos, mouseEvent.deltaY < 0 ? 'in' : 'out');
}
2021-10-27 11:02:21 +03:00
function onMouseDown(mouseEvent) {
if (!Startup.documentCreated())
return;
let mousePos = Input.getCursorPosition(mouseEvent);
2021-10-31 14:49:38 +03:00
2021-10-27 11:02:21 +03:00
switch(mouseEvent.which) {
case 1:
if (!Input.isDragging()) {
2021-10-31 20:31:45 +03:00
currTool.onStart(mousePos, mouseEvent.target);
2021-10-27 11:02:21 +03:00
}
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
function onMouseMove(mouseEvent) {
if (!Startup.documentCreated())
return;
let mousePos = Input.getCursorPosition(mouseEvent);
// Call the hover event
currTool.onHover(mousePos, mouseEvent.target);
if (Input.isDragging()) {
currTool.onDrag(mousePos, mouseEvent.target);
}
}
2021-10-27 11:02:21 +03:00
function onMouseUp(mouseEvent) {
if (!Startup.documentCreated())
return;
let mousePos = Input.getCursorPosition(mouseEvent);
switch(mouseEvent.which) {
2021-10-31 14:49:38 +03:00
case 1:
2021-10-27 11:02:21 +03:00
if (Input.isDragging()) {
currTool.onEnd(mousePos);
}
break;
case 2:
break;
2021-10-31 14:49:38 +03:00
case 3:
break;
2021-10-27 11:02:21 +03:00
default:
break;
}
}
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
})();