-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.js
More file actions
178 lines (167 loc) · 4.88 KB
/
input.js
File metadata and controls
178 lines (167 loc) · 4.88 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class KeyPress {
constructor(state, processed) {
this.state = state;
this.processed = processed;
}
}
const keys = {};
const mouseButtons = {
0: new KeyPress(false, true),
1: new KeyPress(false, true),
2: new KeyPress(false, true),
3: new KeyPress(false, true),
4: new KeyPress(false, true),
};
const mousePosition = new Vector2(0, -1000);
document.addEventListener("contextmenu", (e) => e.preventDefault());
document.addEventListener('keydown', function (e) {
const k = e.key.toUpperCase();
if(e.repeat) // Cancel Windows' stupid repeating functionality
return;
// Preserve some of the browsers' features
const preserved = [KeyCode.KeyF5, KeyCode.KeyF11, KeyCode.KeyF12];
if (preserved.indexOf(k) === -1)
e.preventDefault();
if (keys[k] === undefined)
keys[k] = new KeyPress((e.type == "keydown"), false);
else {
keys[k].state = (e.type == "keydown");
keys[k].processed = false;
}
})
document.addEventListener('keyup', function (e) {
const k = e.key.toUpperCase();
if (keys[k] === undefined)
keys[k] = new KeyPress((e.type == "keydown"), false);
else {
keys[k].state = (e.type == "keydown");
keys[k].processed = false;
}
})
document.addEventListener("mousemove", (e) => {
if(viewport.rect === undefined)
return;
mousePosition.x = (e.clientX - viewport.rect.left - viewport.offsetX) / viewport.scale;
mousePosition.y = (e.clientY- viewport.rect.top - viewport.offsetY) / viewport.scale;
});
//document.addEventListener("pointermove", handleMouseButtons); pointermove causes problems when moving the mouse around and clicks not registering! So I temporarly removed it
document.addEventListener("pointerdown", handleMouseButtons);
document.addEventListener("pointerup", handleMouseButtons);
function handleMouseButtons(e) {
for (let i = 0; i < 5; i++) {
if ((1 << i) & e.buttons) {
mouseButtons[i].processed = mouseButtons[i].state;
mouseButtons[i].state = true;
}
else {
mouseButtons[i].processed = !mouseButtons[i].state;
mouseButtons[i].state = false;
}
}
}
// Each game loop tick, we're process each keypress to determine whether we pressed it this tick or not
function processKeys() {
for (const key in keys) {
if (!Object.prototype.hasOwnProperty.call(keys, key)) continue;
keys[key].processed = true;
}
for (let i = 0; i < 5; i++)
mouseButtons[i].processed = true;
}
function getKeyDown(keycode) { // We just pressed down the key this tick
if (!Object.prototype.hasOwnProperty.call(keys, keycode)) return false;
return keys[keycode].state && !keys[keycode].processed;
}
function getKey(keycode) { // We are holding down the key
if (!Object.prototype.hasOwnProperty.call(keys, keycode)) return false;
return keys[keycode].state;
}
function getKeyUp(keycode) { // We just released the key this tick
if (!Object.prototype.hasOwnProperty.call(keys, keycode)) return false;
return !keys[keycode].state && !keys[keycode].processed;
}
function getMouseButtonDown(mousebutton) { // We just pressed down the button this tick
return mouseButtons[mousebutton].state && !mouseButtons[mousebutton].processed;
}
function getMouseButton(mousebutton) { // We are holding down the button
return mouseButtons[mousebutton].state;
}
function getMouseButtonUp(mousebutton) { // We just released the button this tick
return !mouseButtons[mousebutton].state && !mouseButtons[mousebutton].processed;
}
const KeyCode = Object.freeze({
KeyA: 'A',
KeyB: 'B',
KeyC: 'C',
KeyD: 'D',
KeyE: 'E',
KeyF: 'F',
KeyG: 'G',
KeyH: 'H',
KeyI: 'I',
KeyJ: 'J',
KeyK: 'K',
KeyL: 'L',
KeyM: 'M',
KeyN: 'N',
KeyO: 'O',
KeyP: 'P',
KeyQ: 'Q',
KeyR: 'R',
KeyS: 'S',
KeyT: 'T',
KeyU: 'U',
KeyV: 'V',
KeyW: 'W',
KeyX: 'X',
KeyY: 'Y',
KeyZ: 'Z',
Key0: '0',
Key1: '1',
Key2: '2',
Key3: '3',
Key4: '4',
Key5: '5',
Key6: '6',
Key7: '7',
Key8: '8',
Key9: '9',
KeyEscape: 'ESCAPE',
KeyEnter: 'ENTER',
KeyTab: 'TAB',
KeyBackspace: 'BACKSPACE',
KeyDelete: 'DELETE',
KeySpace: ' ',
KeyShift: 'SHIFT',
KeyControl: 'CONTROL',
KeyAlt: 'ALT',
KeyMeta: 'META',
KeyCapsLock: 'CAPSLOCK',
KeyArrowUp: 'ARROWUP',
KeyArrowDown: 'ARROWDOWN',
KeyArrowLeft: 'ARROWLEFT',
KeyArrowRight: 'ARROWRIGHT',
KeyHome: 'HOME',
KeyEnd: 'END',
KeyPageUp: 'PAGEUP',
KeyPageDown: 'PAGEDOWN',
KeyF1: 'F1',
KeyF2: 'F2',
KeyF3: 'F3',
KeyF4: 'F4',
KeyF5: 'F5',
KeyF6: 'F6',
KeyF7: 'F7',
KeyF8: 'F8',
KeyF9: 'F9',
KeyF10: 'F10',
KeyF11: 'F11',
KeyF12: 'F12',
});
const MouseButtons = Object.freeze({
LEFT: 0,
RIGHT: 1,
MIDDLE: 2,
BACK: 3,
FORWARD: 4,
})