-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput.js
405 lines (340 loc) · 10.4 KB
/
input.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
function istext(e) {
// needs to be before e.ctrlKey check, because AltGr+7 etc. causes e.ctrlKey to be true
if (e.key == "{" || e.key == "}" || e.key == "[" || e.key == "]" || e.key == "~" || e.key == "\\")
return true;
if (e.ctrlKey)
return false;
if (e.key == "Home") return false;
if (e.key == "End") return false;
if (e.key == "PageUp") return false;
if (e.key == "PageDown") return false;
if (e.key == "Insert") return false;
if (e.key == "Delete") return false;
// e.g. ArrowLeft
if (e.key.length > 4)
return false;
if (e.keyCode < 32)
return false;
var keycode = e.key.charCodeAt(0);
//keycode = e.keyCode;
if (keycode >= 32 && keycode <= 126) return true;
if (e.key == "ß") return true; // ß
if (e.key == "ü") return true;
if (e.key == "ö") return true;
if (e.key == "ä") return true;
if (e.key == "Ü") return true;
if (e.key == "Ö") return true;
if (e.key == "Ä") return true;
return false;
}
K_UPARROW = 132;
K_DOWNARROW = 133;
K_LEFTARROW = 134;
K_RIGHTARROW = 135;
K_ALT = 136
K_CTRL = 137
K_SHIFT = 138
K_INS = 139
K_DEL = 140
K_PGDN = 141
K_PGUP = 142
K_HOME = 143
K_END = 144
function toQuakeCode(e) {
if (e.keyCode == 37) return 134; // left array key
if (e.keyCode == 38) return 132; // left array key K_UPARROW
if (e.keyCode == 39) return K_RIGHTARROW; // right array key
if (e.keyCode == 40) return K_DOWNARROW;
if (e.keyCode == 46) return 140; // delete
if (e.keyCode == 36) return K_HOME;
if (e.keyCode == 35) return K_END;
if (e.keyCode == 33) return K_PGUP; // Page up
if (e.keyCode == 34) return K_PGDN; // page down
if (e.keyCode == 16) return K_SHIFT; // page down
//if (e.keyCode == 189) return 95; // _
// from A to Z, keyCode is unaffected by shift
if (e.keyCode >= 60 && e.keyCode <= 90) {
if (e.shiftKey == true)
return e.keyCode
else
return e.keyCode + 32 // lower case ascii letters are from 97 to 122, aka +32
}
return e.keyCode; // add if() if not identical
}
/*
add a JS command like /javascript console.log("bla")
then forward the F1-12 keys aswell, so quake handles it
*/
function handleKeyForQuake(e, pressed) {
// the input logic for quake 3 is kinda messed up...
if (e.keyCode == 8) { // for some reason this is how Q3 takes a "backspace"
if (pressed)
_Com_QueueEvent( 0, SE_CHAR, 8, 1, 0, NULL);
return;
}
if (istext(e) == false) {
//if (pressed)
// _Com_QueueEvent( 0, SE_CHAR, toQuakeCode(e), pressed, 0, NULL );
_Com_QueueEvent( 0, SE_KEY, toQuakeCode(e), pressed, 0, NULL );
return;
}
//var key = e.key.toLowerCase();
// this worked for latin keyboards, but e.g. not russian ones
//var asciiCode = e.key.charCodeAt(0);
var asciiCode = e.keyCode;
var asciiCode32 = e.keyCode;
// pressing w, no matter of shift pressed or not, will always result in e.keyCode==87
// 87 is ascii for 'W', 119 is ascii for 'w'
// so if shift ISNT pressed, we need to add 32 (to get 'w')
//if (e.shiftKey == false)
// asciiCode += 32; // turn 'W' into 'w'
if (e.keyCode >= 60 && e.keyCode <= 90) {
if (e.shiftKey == true)
asciiCode = e.keyCode
else
asciiCode = e.keyCode + 32 // lower case ascii letters are from 97 to 122, aka +32
asciiCode32 += 32
} else {
asciiCode = e.key.charCodeAt(0)
}
if (pressed)
_Com_QueueEvent( 0, SE_CHAR, asciiCode, pressed, 0, NULL );
_Com_QueueEvent( 0, SE_KEY, asciiCode32, pressed, 0, NULL );
}
q3_on_key_down = function(e) {
if (e.key == "t") {
chat_open_and_focus();
e.preventDefault();
} else {
handleKeyForQuake(e, 1);
}
}
on_key_down = function(e_) {
if (e_.defaultPrevented)
{
if (debugKeys)
console.log("on_key_down was prevented")
return;
}
e = e_;
if (e.key == "F10") {
fullwindow();
return;
}
if (e.key == "F11" || e.key == "F12") {
return; // allow F11 for fullscreen and F12 for dev console
}
// allow ctrl+v to trigger document.onpaste, so we have access to clipboard text
const isCtrl = e.keyCode == 17;
const isV = e_.key == "v";
const allowDefault = isCtrl || isV;
if ( ! allowDefault)
e_.preventDefault();
if (debugKeys)
console.log("on_key_down: ", e_, "istext=", istext(e));
q3_on_key_down(e_);
}
q3_on_key_up = function(e) {
handleKeyForQuake(e, 0);
}
on_key_up = function(e_) {
// needs to be done in some event for security reasons and this is kinda the best fit
if (copyMeIntoClipboard.length != 0) {
copyToClipboard(copyMeIntoClipboard);
copyMeIntoClipboard = "";
}
if (e_.defaultPrevented)
{
if (debugKeys)
console.log("on_key_up was prevented")
return;
}
e_.preventDefault();
e = e_;
if (debugKeys)
console.log("on_key_up", e_, "istext=", istext(e));
q3_on_key_up(e_);
}
old_delta_x = 0;
old_delta_y = 0;
debugMouse = false;
q3_on_mouse_move = function(e) {
if (document.pointerLockElement == null)
return;
// fix bug in Chrome that movementX suddenly is large as fuck, e.g. jumping from -30 to -520
var delta_x = e.movementX;
var delta_y = e.movementY;
var factor = delta_x / old_delta_x; // e.g. -520 / 30 = -17.333
if (Math.abs(delta_x) > 200) // bug range seems to be 400 to 500, positive and negative
if (Math.abs(factor) > 5) {
console.log("FIX UP CHROME MOUSE BUG delta_x: " + delta_x + " old_delta_x:" + old_delta_x)
//delta_x = 0; // just use the old value then
return; // ignore whole faulty shit event
}
var factor2 = delta_y / old_delta_y; // e.g. -520 / 30 = -17.333
if (Math.abs(delta_y) > 200) // bug range seems to be 400 to 500, positive and negative
if (Math.abs(factor2) > 5) {
console.log("FIX UP CHROME MOUSE BUG delta_x: " + delta_y + " old_delta_x:" + old_delta_y)
//delta_y = 0; // just use the old value then
return; // ignore whole faulty shit event
}
if (debugMouse)
console.log("mouse delta x/y: ", delta_x, delta_y);
_Com_QueueEvent( 0, SE_MOUSE, delta_x, delta_y, 0, NULL );
old_delta_x = e.movementX;
old_delta_y = e.movementY;
}
on_mouse_move = function(e) {
//pos_left = e.pageX - e.currentTarget.offsetLeft;
//pos_top = e.pageY - e.currentTarget.offsetTop;
pos_left = e.pageX - playcanvas.offsetLeft;
pos_top = e.pageY - playcanvas.offsetTop;
//console.log("px", e.pageX, "py", e.pageY)
//console.log("offsetLeft", e.currentTarget.offsetLeft, "offsetTop", e.currentTarget.offsetTop);
//console.log("pos_left", pos_left, "pos_top", pos_top)
//console.log(e.movementX, e.movementY)
q3_on_mouse_move(e);
e.preventDefault()
}
K_MOUSE1 = 178;
K_MOUSE2 = 179;
K_MOUSE3 = 180;
K_MOUSE4 = 181;
K_MOUSE5 = 182;
K_MWHEELDOWN = 183;
K_MWHEELUP = 184;
q3_mouse_left_pressed = function() {
_Com_QueueEvent( 0, SE_KEY, K_MOUSE1, /*pressed*/1, 0, NULL );
//gltf_maila_hands.children[0].script.anim.animComponent.animClips["ArmatureAction"].session.curTime = 0;
}
q3_mouse_right_pressed = function() {
_Com_QueueEvent( 0, SE_KEY, K_MOUSE2, /*pressed*/1, 0, NULL );
}
q3_mouse_middle_pressed = function() {
_Com_QueueEvent( 0, SE_KEY, K_MOUSE3, /*pressed*/1, 0, NULL );
}
q3_mouse_left_release = function() {
_Com_QueueEvent( 0, SE_KEY, K_MOUSE1, /*pressed*/0, 0, NULL );
}
q3_mouse_right_release = function() {
_Com_QueueEvent( 0, SE_KEY, K_MOUSE2, /*pressed*/0, 0, NULL );
}
q3_mouse_middle_release = function() {
_Com_QueueEvent( 0, SE_KEY, K_MOUSE3, /*pressed*/0, 0, NULL );
}
function q3_mouse(e, pressed) {
//int b;
// switch( e.button.button )
// {
// case SDL_BUTTON_LEFT: b = K_MOUSE1; break;
// case SDL_BUTTON_MIDDLE: b = K_MOUSE3; break;
// case SDL_BUTTON_RIGHT: b = K_MOUSE2; break;
// case SDL_BUTTON_X1: b = K_MOUSE4; break;
// case SDL_BUTTON_X2: b = K_MOUSE5; break;
// default: b = K_AUX1 + ( e.button.button - SDL_BUTTON_X2 + 1 ) % 16; break;
// }
if (pressed) {
switch (e.button) {
case 0: q3_mouse_left_pressed(); break;
case 1: q3_mouse_middle_pressed(); break;
case 2: q3_mouse_right_pressed(); break;
}
} else {
switch (e.button) {
case 0: q3_mouse_left_release(); break;
case 1: q3_mouse_middle_release(); break;
case 2: q3_mouse_right_release(); break;
}
}
}
q3_on_mouse_down = function(e) {
if (document.pointerLockElement != null)
q3_mouse(e, 1);
}
q3_on_mouse_up = function(e) {
q3_mouse(e, 0);
}
on_mouse_down = function(e) {
//console.log(e.button)
q3_on_mouse_down(e);
e.preventDefault()
}
on_mouse_up = function(e) {
//console.log(e.button)
q3_on_mouse_up(e);
e.preventDefault()
}
qtrue = 1;
qfalse = 0;
lastWheel = 0;
q3_on_mouse_wheel = function(e) {
if (e.wheelDelta > 0) {
_Com_QueueEvent( 0, SE_KEY, K_MWHEELUP, qtrue, 0, NULL );
_Com_QueueEvent( 0, SE_KEY, K_MWHEELUP, qfalse, 0, NULL );
} else {
_Com_QueueEvent( 0, SE_KEY, K_MWHEELDOWN, qtrue, 0, NULL );
_Com_QueueEvent( 0, SE_KEY, K_MWHEELDOWN, qfalse, 0, NULL );
}
}
on_mouse_wheel = function(e) {
//_mouse_wheel(e.wheelDelta / 120)
//console.log(e.wheelDelta);
// for some reason Chrome sends two mouse wheels when I do one...
if (Date.now() - lastWheel < 20 )
return;
//console.log("ignore...", Date.now() - lastWheel);
if (debugMouse)
console.log("on_mouse_wheel", e)
q3_on_mouse_wheel(e);
lastWheel = Date.now();
e.preventDefault()
}
function hookKeyEvents() {
document.onkeydown = on_key_down;
document.onkeyup = on_key_up;
}
function unhookKeyEvents() {
document.onkeydown = null;
document.onkeyup = null;
}
function hookMouseEvents() {
document.onmousemove = on_mouse_move;
document.onmousedown = on_mouse_down;
document.onmouseup = on_mouse_up;
document.onmousewheel = on_mouse_wheel;
}
function unhookMouseEvents() {
document.onmousemove = null;
document.onmousedown = null;
document.onmouseup = null;
document.onmousewheel = null;
}
function set_input_events(element) {
overlay.onclick = function(e) {
console.log("overlay.onclick");
if (e.defaultPrevented) { // when user clicked on #chat dont lock the mouse
console.log("overlay.onclick", "e.defaultPrevented");
return;
}
canvas_and_overlay.requestPointerLock();
};
chat_init();
//overlay.onmousemove = on_mouse_move;
//overlay.onmousedown = on_mouse_down;
//overlay.onmouseup = on_mouse_up;
//overlay.onmousewheel = on_mouse_wheel;
hookKeyEvents();
hookMouseEvents();
document.addEventListener('pointerlockchange', function() {
//console.log("on lock change", document.pointerLockElement)
if (document.pointerLockElement == null) {
//console.log("release mouse lock")
//unhookKeyEvents()
//unhookMouseEvents()
} else {
//console.log("enter mouse lock")
hookKeyEvents();
hookMouseEvents();
}
}, false);
}