2021-07-20 23:52:51 +03:00
|
|
|
const Input = (() => {
|
|
|
|
let dragging = false;
|
2021-07-22 19:57:44 +03:00
|
|
|
let currentMouseEvent = undefined;
|
2021-12-11 12:48:14 +03:00
|
|
|
let lastMouseTarget = undefined;
|
2021-11-09 14:53:19 +03:00
|
|
|
let spacePressed = false;
|
|
|
|
let altPressed = false;
|
|
|
|
let ctrlPressed = false;
|
2021-07-20 23:52:51 +03:00
|
|
|
|
|
|
|
// Hotkeys when pressing a key
|
|
|
|
Events.on("keydown", document, KeyPress);
|
|
|
|
// Update held keys when releasing a key
|
2021-11-09 14:53:19 +03:00
|
|
|
Events.on("keyup", window, function (e) {
|
|
|
|
if (e.keyCode == 32) spacePressed = false;
|
|
|
|
if (!e.altKey) altPressed = false;
|
|
|
|
if (!e.ctrlKey) ctrlPressed = false;
|
|
|
|
});
|
2021-07-20 23:52:51 +03:00
|
|
|
|
|
|
|
// Update variables on mouse clicks
|
|
|
|
Events.on("mousedown", window, onMouseDown);
|
|
|
|
Events.on("mouseup", window, onMouseUp);
|
|
|
|
|
|
|
|
function onMouseDown(event) {
|
2021-12-11 12:48:14 +03:00
|
|
|
lastMouseTarget = event.target;
|
2021-07-22 19:57:44 +03:00
|
|
|
currentMouseEvent = event;
|
2021-07-20 23:52:51 +03:00
|
|
|
dragging = true;
|
2021-11-09 14:19:57 +03:00
|
|
|
|
|
|
|
if (!Util.isChildOfByClass(event.target, "editor-top-menu")) {
|
|
|
|
TopMenuModule.closeMenu();
|
|
|
|
}
|
2021-07-20 23:52:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onMouseUp(event) {
|
2021-07-22 19:57:44 +03:00
|
|
|
currentMouseEvent = event;
|
2021-07-20 23:52:51 +03:00
|
|
|
dragging = false;
|
2021-11-09 14:19:57 +03:00
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
if (currFile.currentLayer != null && !Util.isChildOfByClass(event.target, "layers-menu-entry")) {
|
2021-11-09 14:19:57 +03:00
|
|
|
LayerList.closeOptionsMenu();
|
|
|
|
}
|
2021-07-20 23:52:51 +03:00
|
|
|
}
|
|
|
|
|
2021-07-22 20:05:58 +03:00
|
|
|
function getCursorPosition(e) {
|
|
|
|
var x;
|
|
|
|
var y;
|
|
|
|
|
|
|
|
if (e.pageX != undefined && e.pageY != undefined) {
|
|
|
|
x = e.pageX;
|
|
|
|
y = e.pageY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
|
|
|
|
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
|
|
|
|
}
|
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
x -= currFile.currentLayer.canvas.offsetLeft;
|
|
|
|
y -= currFile.currentLayer.canvas.offsetTop;
|
2021-07-22 20:05:58 +03:00
|
|
|
|
|
|
|
return [Math.round(x), Math.round(y)];
|
|
|
|
}
|
|
|
|
|
2021-07-20 23:52:51 +03:00
|
|
|
/** Just listens to hotkeys and calls the linked functions
|
2021-07-18 23:44:31 +03:00
|
|
|
*
|
2021-07-20 23:52:51 +03:00
|
|
|
* @param {*} e
|
2021-07-18 23:44:31 +03:00
|
|
|
*/
|
2021-07-20 23:52:51 +03:00
|
|
|
function KeyPress(e) {
|
|
|
|
var keyboardEvent = window.event? event : e;
|
2021-11-09 14:53:19 +03:00
|
|
|
altPressed = e.altKey;
|
|
|
|
ctrlPressed = e.ctrlKey;
|
2021-07-20 23:52:51 +03:00
|
|
|
|
|
|
|
//if the user is typing in an input field or renaming a layer, ignore these hotkeys, unless it's an enter key
|
2021-11-09 14:19:57 +03:00
|
|
|
if (document.activeElement.tagName == 'INPUT' || LayerList.isRenamingLayer()) {
|
2021-07-20 23:52:51 +03:00
|
|
|
if (e.keyCode == 13) {
|
2021-07-23 17:25:15 +03:00
|
|
|
LayerList.closeOptionsMenu();
|
2021-07-20 23:52:51 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:19:57 +03:00
|
|
|
//if no document has been created yet or there is a dialog box open ignore hotkeys
|
2021-12-13 01:08:13 +03:00
|
|
|
if (!EditorState.documentCreated()) return;
|
2021-07-20 23:52:51 +03:00
|
|
|
|
|
|
|
if (e.key === "Escape") {
|
2021-11-10 00:59:17 +03:00
|
|
|
Events.emit("esc-pressed");
|
2021-07-20 23:52:51 +03:00
|
|
|
}
|
2021-12-13 01:08:13 +03:00
|
|
|
else if (!Dialogue.isOpen()){
|
2021-07-20 23:52:51 +03:00
|
|
|
switch (keyboardEvent.keyCode) {
|
|
|
|
//pencil tool - 1, b
|
|
|
|
case 49: case 66:
|
2021-11-09 14:19:57 +03:00
|
|
|
Events.emit("tool-shortcut", "brush");
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
|
|
|
// copy tool c
|
|
|
|
case 67: case 99:
|
2021-11-09 14:19:57 +03:00
|
|
|
if (keyboardEvent.ctrlKey) {
|
|
|
|
Events.emit("ctrl+c");
|
2021-07-20 23:52:51 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
//fill tool - 2, f
|
|
|
|
case 50: case 70:
|
2021-11-09 14:19:57 +03:00
|
|
|
Events.emit("tool-shortcut", "fill");
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
|
|
|
//eyedropper - 3, e
|
|
|
|
case 51: case 69:
|
2021-11-09 14:19:57 +03:00
|
|
|
Events.emit("tool-shortcut", "eyedropper");
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
|
|
|
//pan - 4, p,
|
|
|
|
case 52: case 80:
|
2021-11-09 14:19:57 +03:00
|
|
|
Events.emit("tool-shortcut", "pan");
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
2021-11-09 14:19:57 +03:00
|
|
|
// line - l
|
2021-07-20 23:52:51 +03:00
|
|
|
case 76:
|
2021-11-09 14:19:57 +03:00
|
|
|
Events.emit("tool-shortcut", "line");
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
|
|
|
// eraser -6, r
|
|
|
|
case 54: case 82:
|
2021-11-09 14:19:57 +03:00
|
|
|
Events.emit("tool-shortcut", "eraser");
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
2021-11-09 14:19:57 +03:00
|
|
|
// Rectangular selection m
|
2021-07-20 23:52:51 +03:00
|
|
|
case 77: case 109:
|
2021-11-09 14:19:57 +03:00
|
|
|
Events.emit("tool-shortcut", "rectselect");
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
2022-01-26 01:47:01 +03:00
|
|
|
// Lasso tool, q
|
|
|
|
case 81: case 113:
|
|
|
|
Events.emit("tool-shortcut", "lassoselect");
|
|
|
|
break;
|
2021-07-20 23:52:51 +03:00
|
|
|
// ellipse tool, s
|
|
|
|
case 83:
|
2022-01-26 01:47:01 +03:00
|
|
|
Events.emit("tool-shortcut", "ellipse");
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
|
|
|
// rectangle tool, u
|
|
|
|
case 85:
|
2021-11-09 14:19:57 +03:00
|
|
|
Events.emit("tool-shortcut", "rectangle");
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
2022-01-26 01:47:01 +03:00
|
|
|
// magic wand tool
|
|
|
|
case 87: case 119:
|
|
|
|
Events.emit("tool-shortcut", "magicwand");
|
|
|
|
break;
|
2021-07-20 23:52:51 +03:00
|
|
|
// Paste tool
|
|
|
|
case 86: case 118:
|
2021-11-09 14:19:57 +03:00
|
|
|
if (keyboardEvent.ctrlKey) {
|
|
|
|
Events.emit("ctrl+v");
|
2021-07-20 23:52:51 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 88: case 120:
|
2021-11-09 14:19:57 +03:00
|
|
|
if (keyboardEvent.ctrlKey) {
|
|
|
|
Events.emit("ctrl+x");
|
2021-07-20 23:52:51 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
//Z
|
2021-11-09 14:19:57 +03:00
|
|
|
case 90: case 122:
|
2021-07-20 23:52:51 +03:00
|
|
|
//CTRL+ALT+Z redo
|
|
|
|
if (keyboardEvent.altKey && keyboardEvent.ctrlKey) {
|
|
|
|
History.redo();
|
|
|
|
}
|
|
|
|
//CTRL+Z undo
|
|
|
|
else if (keyboardEvent.ctrlKey) {
|
|
|
|
History.undo();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
//redo - ctrl y
|
|
|
|
case 89:
|
|
|
|
if (keyboardEvent.ctrlKey)
|
|
|
|
History.redo();
|
|
|
|
break;
|
|
|
|
case 32:
|
2021-11-09 14:53:19 +03:00
|
|
|
spacePressed = true;
|
2021-07-20 23:52:51 +03:00
|
|
|
break;
|
2021-12-11 12:48:14 +03:00
|
|
|
case 46:
|
|
|
|
console.log("Pressed del");
|
|
|
|
Events.emit("del");
|
|
|
|
break;
|
2021-07-20 23:52:51 +03:00
|
|
|
}
|
|
|
|
}
|
2021-07-18 23:44:31 +03:00
|
|
|
}
|
2021-11-09 14:19:57 +03:00
|
|
|
|
2021-07-20 23:52:51 +03:00
|
|
|
function isDragging() {
|
|
|
|
return dragging;
|
|
|
|
}
|
|
|
|
|
2021-07-22 19:57:44 +03:00
|
|
|
function getCurrMouseEvent() {
|
|
|
|
return currentMouseEvent;
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:53:19 +03:00
|
|
|
function isAltPressed() {
|
|
|
|
return altPressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isCtrlPressed() {
|
|
|
|
return ctrlPressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSpacePressed() {
|
|
|
|
return spacePressed;
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:48:14 +03:00
|
|
|
function getLastTarget() {
|
|
|
|
return lastMouseTarget;
|
|
|
|
}
|
|
|
|
|
2021-07-20 23:52:51 +03:00
|
|
|
return {
|
2021-07-22 19:57:44 +03:00
|
|
|
isDragging,
|
2021-07-22 20:05:58 +03:00
|
|
|
getCurrMouseEvent,
|
2021-11-09 14:53:19 +03:00
|
|
|
getCursorPosition,
|
|
|
|
isAltPressed,
|
|
|
|
isCtrlPressed,
|
2021-12-11 12:48:14 +03:00
|
|
|
isSpacePressed,
|
|
|
|
getLastTarget
|
2021-07-12 12:36:30 +03:00
|
|
|
}
|
2021-07-20 23:52:51 +03:00
|
|
|
})();
|