-
Notifications
You must be signed in to change notification settings - Fork 46
/
input2.js
98 lines (75 loc) · 2.84 KB
/
input2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Future new input script that can listen for inputs on specific elements,
* not only on the document.
*
* Also, we will need built-in double-click and triple-click detection
* for mapping tools to, such as ray/arrow drawing.
*/
import docutil from "../util/docutil.js";
function createInputListener(element) {
const mouseDowns = [];
const mouseHelds = [];
const keyDowns = [];
const keyHelds = [];
let mousePos = [0, 0];
const eventHandlers = {};
// Helper function to store event listeners and their handlers
const addListener = (target, eventType, handler) => {
target.addEventListener(eventType, handler);
eventHandlers[eventType] = { target, handler };
};
// Add event listeners and track them
addListener(element, 'mousedown', (e) => {
mouseDowns.push(e.button);
if (!mouseHelds.includes(e.button)) {
mouseHelds.push(e.button);
}
});
addListener(element, 'mouseup', (e) => {
const downIndex = mouseDowns.indexOf(e.button);
if (downIndex !== -1) mouseDowns.splice(downIndex, 1);
const heldIndex = mouseHelds.indexOf(e.button);
if (heldIndex !== -1) mouseHelds.splice(heldIndex, 1);
});
addListener(document, 'keydown', (e) => {
if (!keyDowns.includes(e.code)) keyDowns.push(e.code);
if (!keyHelds.includes(e.code)) keyHelds.push(e.code);
});
addListener(document, 'keyup', (e) => {
const downIndex = keyDowns.indexOf(e.code);
if (downIndex !== -1) keyDowns.splice(downIndex, 1);
const heldIndex = keyHelds.indexOf(e.code);
if (heldIndex !== -1) keyHelds.splice(heldIndex, 1);
});
addListener(element, 'mousemove', (e) => {
mousePos = [e.clientX, e.clientY];
});
return {
isMouseDown: (button) => mouseDowns.includes(button),
isMouseHeld: (button) => mouseHelds.includes(button),
isKeyDown: (keyCode) => keyDowns.includes(keyCode),
isKeyHeld: (keyCode) => keyHelds.includes(keyCode),
getMousePos: () => [...mousePos],
removeEventListeners: () => {
// Remove all event listeners that were added
Object.keys(eventHandlers).forEach((eventType) => {
const { target, handler } = eventHandlers[eventType];
target.removeEventListener(eventType, handler);
});
console.log("Closed event listeners of Input Listener");
}
};
}
//---------------------------------------------------------------------------------------------------------
/**
*
* @param {KeyboardEvent} event
* @param {InputListener} inputListener
* @param {Object} options
*/
function onContextMenu(event, inputListener) {
const targetIsElement = event.target === inputListener.element; // target is the deepest most element affected by the event
// const targetIsElement = event.currentTarget === element; // currentTarget is the element that owns the event listener
if (targetIsElement) event.preventDefault(); // Stop the contextual (right-click) menu from popping up.
}
export default { createInputListener };