-
Notifications
You must be signed in to change notification settings - Fork 207
/
io-manager.js
884 lines (839 loc) · 24.9 KB
/
io-manager.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*
io manager reads inputs from the browser.
some inputs are implicit, like resize.
the functionality is implemented in other managers.
*/
import * as THREE from 'three';
import metaversefile from 'metaversefile';
import cameraManager from './camera-manager.js';
// import controlsManager from './controls-manager.js';
import game from './game.js';
import {world} from './world.js';
import voiceInput from './voice-input/voice-input.js';
// import * as universe from './universe.js';
// import {toggle as inventoryToggle} from './inventory.js';
import {isInIframe, getVelocityDampingFactor} from './util.js';
import {getRenderer, /*renderer2,*/ scene, camera, dolly, getContainerElement} from './renderer.js';
import physicsManager from './physics-manager.js';
/* import {menuActions} from './mithril-ui/store/actions.js';
import {menuState} from './mithril-ui/store/state.js'; */
// import physx from './physx.js';
// import {airFriction, flyFriction} from './constants.js';
import transformControls from './transform-controls.js';
import storyManager from './story.js';
// import domRenderer from './dom-renderer.jsx';
import raycastManager from './raycast-manager.js';
// const localVector = new THREE.Vector3();
// const localVector2 = new THREE.Vector3();
const localVector3 = new THREE.Vector3();
// const localVector2D = new THREE.Vector2();
// const localVector2D2 = new THREE.Vector2();
// const localQuaternion = new THREE.Quaternion();
const localQuaternion2 = new THREE.Quaternion();
const localEuler = new THREE.Euler();
const localMatrix2 = new THREE.Matrix4();
const localMatrix3 = new THREE.Matrix4();
const localRaycaster = new THREE.Raycaster();
const zeroVector = new THREE.Vector3();
//
const physicsScene = physicsManager.getScene();
//
const ioManager = new EventTarget();
ioManager.lastAxes = [[0, 0, 0, 0], [0, 0, 0, 0]];
ioManager.lastButtons = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]];
ioManager.currentWeaponValue = 0;
ioManager.lastWeaponValue = 0;
ioManager.currentTeleport = false;
ioManager.lastTeleport = false;
ioManager.currentMenuDown = false;
ioManager.lastMenuDown = false;
ioManager.menuExpanded = false;
ioManager.lastMenuExpanded = false;
ioManager.currentWeaponGrabs = [false, false];
ioManager.lastWeaponGrabs = [false, false];
ioManager.currentWalked = false;
ioManager.lastCtrlKey = false;
ioManager.keys = {
up: false,
down: false,
left: false,
right: false,
forward: false,
backward: false,
shift: false,
doubleTap: false,
space: false,
ctrl: false,
};
const lastKeysDownTime = {
keyW: 0,
keyA: 0,
keyS: 0,
keyD: 0,
keyE: 0,
};
const resetKeys = () => {
for (const k in ioManager.keys) {
ioManager.keys[k] = false;
}
};
cameraManager.addEventListener('pointerlockchange', () => {
resetKeys();
});
const _inputFocused = () => document.activeElement && (document.activeElement.tagName === 'INPUT' || document.activeElement.getAttribute('contenteditable') !== null);
ioManager.inputFocused = _inputFocused;
const _updateHorizontal = direction => {
if (ioManager.keys.left) {
direction.x -= 1;
}
if (ioManager.keys.right) {
direction.x += 1;
}
if (ioManager.keys.up) {
direction.z -= 1;
}
if (ioManager.keys.down) {
direction.z += 1;
}
};
const _updateVertical = direction => {
if (ioManager.keys.space) {
direction.y += 1;
}
if (ioManager.keys.ctrl) {
direction.y -= 1;
}
};
const keysDirection = new THREE.Vector3();
ioManager.keysDirection = keysDirection;
const _updateIo = timeDiff => {
const renderer = getRenderer();
const xrCamera = renderer.xr.getSession() ? renderer.xr.getCamera(camera) : camera;
if (renderer.xr.getSession()) {
ioManager.currentWalked = false;
const inputSources = Array.from(renderer.xr.getSession().inputSources);
for (let i = 0; i < inputSources.length; i++) {
const inputSource = inputSources[i];
const {handedness, gamepad} = inputSource;
if (gamepad && gamepad.buttons.length >= 2) {
const index = handedness === 'right' ? 1 : 0;
// axes
const {axes: axesSrc, buttons: buttonsSrc} = gamepad;
const axes = [
axesSrc[0] || 0,
axesSrc[1] || 0,
axesSrc[2] || 0,
axesSrc[3] || 0,
];
const buttons = [
buttonsSrc[0] ? buttonsSrc[0].value : 0,
buttonsSrc[1] ? buttonsSrc[1].value : 0,
buttonsSrc[2] ? buttonsSrc[2].value : 0,
buttonsSrc[3] ? buttonsSrc[3].value : 0,
buttonsSrc[4] ? buttonsSrc[4].value : 0,
buttonsSrc[5] ? buttonsSrc[4].value : 0,
];
if (handedness === 'left') {
const dx = axes[0] + axes[2];
const dy = axes[1] + axes[3];
if (Math.abs(dx) >= 0.01 || Math.abs(dy) >= 0.01) {
localEuler.setFromQuaternion(xrCamera.quaternion, 'YXZ');
localEuler.x = 0;
localEuler.z = 0;
localVector3.set(dx, 0, dy)
.applyEuler(localEuler)
.multiplyScalar(0.05);
dolly.matrix
// .premultiply(localMatrix2.makeTranslation(-xrCamera.position.x, -xrCamera.position.y, -xrCamera.position.z))
.premultiply(localMatrix3.makeTranslation(localVector3.x, localVector3.y, localVector3.z))
// .premultiply(localMatrix2.copy(localMatrix2).invert())
.decompose(dolly.position, dolly.quaternion, dolly.scale);
ioManager.currentWalked = true;
}
ioManager.currentWeaponGrabs[1] = buttons[1] > 0.5;
} else if (handedness === 'right') {
const _applyRotation = r => {
dolly.matrix
.premultiply(localMatrix2.makeTranslation(-xrCamera.position.x, -xrCamera.position.y, -xrCamera.position.z))
.premultiply(localMatrix3.makeRotationFromQuaternion(localQuaternion2.setFromAxisAngle(localVector3.set(0, 1, 0), r)))
.premultiply(localMatrix2.copy(localMatrix2).invert())
.decompose(dolly.position, dolly.quaternion, dolly.scale);
};
if (
(axes[0] < -0.75 && !(ioManager.lastAxes[index][0] < -0.75)) ||
(axes[2] < -0.75 && !(ioManager.lastAxes[index][2] < -0.75))
) {
_applyRotation(Math.PI * 0.2);
} else if (
(axes[0] > 0.75 && !(ioManager.lastAxes[index][0] > 0.75)) ||
(axes[2] > 0.75 && !(ioManager.lastAxes[index][2] > 0.75))
) {
_applyRotation(-Math.PI * 0.2);
}
ioManager.currentTeleport = (axes[1] < -0.75 || axes[3] < -0.75);
ioManager.currentMenuDown = (axes[1] > 0.75 || axes[3] > 0.75);
ioManager.currentWeaponDown = buttonsSrc[0].pressed;
ioManager.currentWeaponValue = buttons[0];
ioManager.currentWeaponGrabs[0] = buttonsSrc[1].pressed;
if (
buttons[3] >= 0.5 && ioManager.lastButtons[index][3] < 0.5 &&
!(Math.abs(axes[0]) > 0.5 || Math.abs(axes[1]) > 0.5 || Math.abs(axes[2]) > 0.5 || Math.abs(axes[3]) > 0.5) &&
!game.isJumping() &&
!game.isSitting()
) {
game.jump();
}
}
ioManager.lastAxes[index][0] = axes[0];
ioManager.lastAxes[index][1] = axes[1];
ioManager.lastAxes[index][2] = axes[2];
ioManager.lastAxes[index][3] = axes[3];
ioManager.lastButtons[index][0] = buttons[0];
ioManager.lastButtons[index][1] = buttons[1];
ioManager.lastButtons[index][2] = buttons[2];
ioManager.lastButtons[index][3] = buttons[3];
ioManager.lastButtons[index][4] = buttons[4];
}
}
} else {
keysDirection.set(0, 0, 0);
const localPlayer = metaversefile.useLocalPlayer();
_updateHorizontal(keysDirection);
if (keysDirection.equals(zeroVector)) {
if (localPlayer.hasAction('narutoRun')) {
keysDirection.copy(cameraManager.lastNonzeroDirectionVector);
}
} else {
cameraManager.lastNonzeroDirectionVector.copy(keysDirection);
}
if (localPlayer.hasAction('swim')){
if(ioManager.keys.shift && keysDirection.length() > 0){
localPlayer.getAction('swim').animationType = 'freestyle';
}
else if(!ioManager.keys.shift && keysDirection.length() > 0){
localPlayer.getAction('swim').animationType = 'breaststroke';
}
else{
localPlayer.getAction('swim').animationType = 'null';
}
}
if (localPlayer.hasAction('fly') || localPlayer.hasAction('swim')) {
keysDirection.applyQuaternion(camera.quaternion);
_updateVertical(keysDirection);
} else {
const cameraEuler = camera.rotation.clone();
cameraEuler.x = 0;
cameraEuler.z = 0;
keysDirection.applyEuler(cameraEuler);
if (ioManager.keys.ctrl && !ioManager.lastCtrlKey && game.isGrounded()) {
game.toggleCrouch();
}
ioManager.lastCtrlKey = ioManager.keys.ctrl;
}
if (keysDirection.length() > 0 && physicsScene.getPhysicsEnabled() && movementEnabled) {
localPlayer.characterPhysics.applyWasd(
keysDirection.normalize()
.multiplyScalar(game.getSpeed() * timeDiff)
);
}
}
};
ioManager.update = _updateIo;
let movementEnabled = true;
// ioManager.getMovmentEnabled = () => movementEnabled;
ioManager.setMovementEnabled = newMovementEnabled => {
movementEnabled = newMovementEnabled;
};
const _updateIoPost = () => {
ioManager.lastTeleport = ioManager.currentTeleport;
ioManager.lastMenuDown = ioManager.currentMenuDown;
ioManager.lastWeaponDown = ioManager.currentWeaponDown;
ioManager.lastWeaponValue = ioManager.currentWeaponValue;
ioManager.lastMenuExpanded = ioManager.menuExpanded;
for (let i = 0; i < 2; i++) {
ioManager.lastWeaponGrabs[i] = ioManager.currentWeaponGrabs[i];
}
};
ioManager.updatePost = _updateIoPost;
ioManager.bindInterface = () => {
const iframed = isInIframe();
if (!iframed) {
document.body.classList.remove('no-ui');
}
};
/* const _setTransformMode = transformMode => {
if (transformControls.getTransformMode() !== transformMode) {
transformControls.setTransformMode(transformMode);
} else {
transformControls.setTransformMode('disabled');
}
}; */
const doubleTapTime = 200;
ioManager.keydown = e => {
if (_inputFocused() || e.repeat) {
return;
}
if (e.keyCode === 18) { // alt
e.preventDefault();
e.stopPropagation();
return;
}
// HACK: these keybindings control developer avatar animation offset settings in avatars.js
/* if (e.which === 74) {
window.lol -= 0.01;
console.log(window.lol);
} else if (e.which === 75) {
window.lol += 0.01;
console.log(window.lol);
} else if (e.which === 78) {
window.lol2 += 0.01;
console.log(window.lol2);
} else if (e.which === 77) {
window.lol2 += 0.01;
console.log(window.lol2);
} */
switch (e.which) {
case 9: { // tab
break;
}
case 49: // 1
case 50: // 2
case 51: // 3
case 52: // 4
case 53: // 5
case 54: // 6
case 55: // 7
case 56: // 8
{
game.selectLoadout(e.which - 49);
break;
}
case 87: { // W
ioManager.keys.up = true;
if (!cameraManager.pointerLockElement) {
game.menuVertical(-1);
}
const now = performance.now();
const timeDiff = now - lastKeysDownTime.keyW;
if (timeDiff < doubleTapTime && ioManager.keys.shift) {
ioManager.keys.doubleTap = true;
game.menuDoubleTap();
}
lastKeysDownTime.keyW = now;
lastKeysDownTime.keyS = 0;
break;
}
case 65: { // A
ioManager.keys.left = true;
if (!cameraManager.pointerLockElement) {
game.menuHorizontal(-1);
}
const now = performance.now();
const timeDiff = now - lastKeysDownTime.keyA;
if (timeDiff < doubleTapTime && ioManager.keys.shift) {
ioManager.keys.doubleTap = true;
game.menuDoubleTap();
}
lastKeysDownTime.keyA = now;
lastKeysDownTime.keyD = 0;
break;
}
case 83: { // S
if (e.ctrlKey) {
e.preventDefault();
e.stopPropagation();
game.saveScene();
} else {
ioManager.keys.down = true;
if (!cameraManager.pointerLockElement) {
if (game.menuOpen) {
game.menuVertical(1);
} else {
// if (!game.dragging) {
// _setTransformMode('scale');
// }
}
}
const now = performance.now();
const timeDiff = now - lastKeysDownTime.keyS;
if (timeDiff < doubleTapTime && ioManager.keys.shift) {
ioManager.keys.doubleTap = true;
game.menuDoubleTap();
}
lastKeysDownTime.keyS = now;
lastKeysDownTime.keyW = 0;
}
break;
}
case 68: { // D
ioManager.keys.right = true;
if (!cameraManager.pointerLockElement) {
game.menuHorizontal(1);
}
const now = performance.now();
const timeDiff = now - lastKeysDownTime.keyD;
if (timeDiff < doubleTapTime && ioManager.keys.shift) {
ioManager.keys.doubleTap = true;
game.menuDoubleTap();
}
lastKeysDownTime.keyD = now;
lastKeysDownTime.keyA = 0;
break;
}
case 70: { // F
e.preventDefault();
e.stopPropagation();
if (game.canPush()) {
ioManager.keys.forward = true;
} else {
/* if (game.canJumpOff()) {
game.jumpOff();
} */
game.toggleFly();
}
break;
}
/* case 90: { // Z
if (!e.ctrlKey) {
if (game.canStartBuild()) {
game.startBuild('wall');
} else if (game.canBuild()) {
game.setBuildMode('wall');
}
}
break;
} */
case 88: { // X
if (!e.ctrlKey) {
/* if (game.canStartBuild()) {
game.startBuild('floor');
} else if (game.canBuild()) {
game.setBuildMode('floor');
} else { */
game.menuDelete();
// }
}
break;
}
case 67: { // C
// if (!e.ctrlKey) {
/* if (game.canStartBuild()) {
game.startBuild('stair');
} else if (game.canBuild()) {
game.setBuildMode('stair');
} else */if (game.canPush()) {
ioManager.keys.backward = true;
} else {
ioManager.keys.ctrl = true;
}
// }
break;
}
/* case 73: { // I
inventoryToggle();
break;
} */
/* case 82: { // R
e.preventDefault();
e.stopPropagation();
document.getElementById('key-r').click(); // equip
break;
} */
case 71: { // G
// game.menuDrop();
// game.menuGDown();
game.menuSwitchCharacter();
break;
}
case 86: { // V
// if (!_inputFocused()) {
e.preventDefault();
e.stopPropagation();
game.menuVDown(e);
// }
break;
}
case 66: { // B
// if (!_inputFocused()) {
e.preventDefault();
e.stopPropagation();
game.menuBDown(e);
// }
break;
}
case 69: { // E
// if (cameraManager.pointerLockElement) {
const now = performance.now();
const timeDiff = now - lastKeysDownTime.keyE;
if (timeDiff < doubleTapTime) {
game.menuMiddleToggle();
} else {
game.menuMiddleUp();
if (game.canRotate()) {
game.menuRotate(-1);
} else {
game.menuActivateDown();
}
}
lastKeysDownTime.keyE = now;
// }
break;
}
case 84: { // T
e.preventDefault();
e.stopPropagation();
voiceInput.toggleMic();
break;
}
case 89: { // Y
e.preventDefault();
e.stopPropagation();
voiceInput.toggleSpeech();
break;
}
case 80: { // P
// game.destroyWorld()
// game.menuPhysics();
break;
}
case 82: { // R
if (cameraManager.pointerLockElement) {
if (game.canRotate()) {
game.menuRotate(1);
} else {
game.dropSelectedApp();
}
} else {
// if (!game.dragging) {
// _setTransformMode('rotate');
// }
}
break;
}
case 16: { // shift
ioManager.keys.shift = true;
break;
}
case 32: { // space
ioManager.keys.space = true;
// if (controlsManager.isPossessed()) {
if (!game.isJumping()) {
game.jump('jump');
} else if (!game.isDoubleJumping()) {
game.doubleJump();
}
// }
break;
}
/* case 17: { // ctrl
ioManager.keys.ctrl = true;
break;
} */
case 81: { // Q
if (e.ctrlKey) {
if (cameraManager.pointerLockElement) {
cameraManager.exitPointerLock();
} else {
cameraManager.requestPointerLock();
}
} else {
// game.setWeaponWheel(true);
if (game.canToggleAxis()) {
game.toggleAxis();
} else {
// clear conflicting aim with quick menu
game.menuUnaim();
}
}
break;
}
/* case 13: { // enter
game.enter();
break;
} */
/* case 77: { // M
game.toggleMap();
break;
} */
case 74: { // J
game.inventoryHack = !game.inventoryHack;
break;
}
case 27: { // esc
game.setContextMenu(false);
break;
}
case 72: { // H
const debug = metaversefile.useDebug();
debug.toggle();
break;
}
case 192: { // tilde
game.toggleEditMode();
break;
}
}
};
ioManager.keypress = e => {
// nothing
};
ioManager.wheel = e => {
if (storyManager.handleWheel(e)) {
// nothing
} else {
if (physicsScene.getPhysicsEnabled()) {
const renderer = getRenderer();
if (renderer && (e.target === renderer.domElement || e.target.id === 'app')) {
cameraManager.handleWheelEvent(e);
}
}
}
};
ioManager.keyup = e => {
if (_inputFocused() || e.repeat) {
return;
}
if (e.keyCode === 18) { // alt
e.preventDefault();
e.stopPropagation();
return;
}
switch (e.which) {
/* case 81: { // Q
game.setWeaponWheel(false);
break;
} */
case 87: { // W
ioManager.keys.up = false;
break;
}
case 65: { // A
ioManager.keys.left = false;
break;
}
case 83: { // S
ioManager.keys.down = false;
break;
}
case 68: { // D
ioManager.keys.right = false;
break;
}
case 32: { // space
ioManager.keys.space = false;
break;
}
/* case 17: { // ctrl
ioManager.keys.ctrl = false;
break;
} */
case 69: { // E
if (cameraManager.pointerLockElement) {
game.menuActivateUp();
}
break;
}
case 70: { // F
// if (cameraManager.pointerLockElement) {
ioManager.keys.forward = false;
// }
break;
}
case 67: { // C
// if (cameraManager.pointerLockElement) {
ioManager.keys.backward = false;
ioManager.keys.ctrl = false;
// }
break;
}
/* case 71: { // G
// if (!_inputFocused()) {
e.preventDefault();
e.stopPropagation();
game.menuGUp();
// }
break;
} */
case 86: { // V
// if (!_inputFocused()) {
e.preventDefault();
e.stopPropagation();
game.menuVUp();
// }
break;
}
case 66: { // B
// if (!_inputFocused()) {
e.preventDefault();
e.stopPropagation();
game.menuBUp();
// }
break;
}
case 16: { // shift
ioManager.keys.shift = false;
ioManager.keys.doubleTap = false;
game.menuUnDoubleTap();
break;
}
case 46: { // delete
const object = game.getMouseSelectedObject();
if (object) {
game.setMouseHoverObject(null);
game.setMouseSelectedObject(null);
world.removeObject(object.instanceId);
}
break;
}
case 27: {
// if (game.getMouseSelectedObject()) {
game.setMouseSelectedObject(null);
// }
}
}
};
ioManager.mousemove = e => {
/* if (game.weaponWheel) {
game.updateWeaponWheel(e);
} else { */
if (cameraManager.pointerLockElement) {
cameraManager.handleMouseMove(e);
} else {
if (game.dragging) {
game.menuDrag(e);
game.menuDragRight(e);
}
}
raycastManager.setLastMouseEvent(e);
// }
};
ioManager.mouseleave = e => {
const renderer = getRenderer();
renderer.domElement.classList.remove('hover');
};
ioManager.click = e => {
if (cameraManager.pointerLockElement) {
game.menuClick(e);
} else {
// game.setContextMenu(false);
if (!game.hoverEnabled) {
cameraManager.requestPointerLock();
}
/* if (controlsManager.isPossessed()) {
cameraManager.requestPointerLock();
} else {
const mouseHoverObject = game.getMouseHoverObject();
const mouseHoverPhysicsId = game.getMouseHoverPhysicsId();
if (mouseHoverObject) {
const mouseSelectedObject = game.getMouseSelectedObject();
if (mouseHoverObject !== mouseSelectedObject) {
game.setMouseSelectedObject(mouseHoverObject, mouseHoverPhysicsId);
} else {
game.setMouseSelectedObject(null);
}
}
} */
}
raycastManager.setLastMouseEvent(e);
};
ioManager.dblclick = e => {
// nothing
};
// let mouseDown = false;
let lastMouseButtons = 0;
ioManager.mousedown = e => {
const changedButtons = lastMouseButtons ^ e.buttons;
if (cameraManager.pointerLockElement) {
if ((changedButtons & 1) && (e.buttons & 1)) { // left
game.menuMouseDown();
}
if ((changedButtons & 2) && (e.buttons & 2)) { // right
game.menuAim();
}
} else {
if ((changedButtons & 1) && (e.buttons & 1)) { // left
const raycaster = raycastManager.getMouseRaycaster(e);
if (raycaster) {
transformControls.handleMouseDown(raycaster);
}
}
if ((changedButtons & 1) && (e.buttons & 2)) { // right
game.menuDragdownRight();
game.setContextMenu(false);
}
}
if ((changedButtons & 4) && (e.buttons & 4)) { // middle
e.preventDefault();
if (!cameraManager.pointerLockElement) {
cameraManager.requestPointerLock();
}
game.menuMiddleDown();
}
lastMouseButtons = e.buttons;
// mouseDown = true;
raycastManager.setLastMouseEvent(e);
};
ioManager.mouseup = e => {
const changedButtons = lastMouseButtons ^ e.buttons;
// if (mouseDown) {
if (cameraManager.pointerLockElement) {
if ((changedButtons & 1) && !(e.buttons & 1)) { // left
game.menuMouseUp();
}
if ((changedButtons & 2) && !(e.buttons & 2)) { // right
game.menuUnaim();
}
} else {
if ((changedButtons & 2) && !(e.buttons & 2)) { // right
game.menuDragupRight();
}
}
if ((changedButtons & 4) && !(e.buttons & 4)) { // middle
game.menuMiddleUp();
}
// mouseDown = false;
// }
lastMouseButtons = e.buttons;
raycastManager.setLastMouseEvent(e);
};
ioManager.paste = e => {
if (!window.document.activeElement) {
const items = Array.from(e.clipboardData.items);
if (items.length > 0) {
e.preventDefault();
console.log('paste items', items);
/* let s = await new Promise((accept, reject) => {
items[0].getAsString(accept);
});
s = s.replace(/[\n\r]+/g, '').slice(0, 256);
game.menuPaste(s); */
}
}
};
ioManager.bindInput = () => {
/* window.addEventListener('paste', async e => {
if (!_inputFocused()) {
e.preventDefault();
const items = Array.from(e.clipboardData.items);
if (items.length > 0) {
let s = await new Promise((accept, reject) => {
items[0].getAsString(accept);
});
s = s.replace(/[\n\r]+/g, '').slice(0, 256);
game.menuPaste(s);
}
}
}); */
/* window.addEventListener('wheel', e => {
// console.log('target', e.target);
if (physicsScene.getPhysicsEnabled()) {
const renderer = getRenderer();
if (renderer && (e.target === renderer.domElement || e.target.id === 'app')) {
cameraManager.handleWheelEvent(e);
}
}
}, {
passive: false,
}); */
};
export default ioManager;