Skip to content

Commit 0d36ded

Browse files
authored
Input source codes fixes (#7842)
* Refactor keyCode definition in KeyboardMouseSource to use a constant KEYS object * Rename KEYS to KEY_CODES for clarity and consistency in KeyboardMouseSource * Refactor GamepadSource to use BUTTON_CODES constant for button definitions and improve button count handling
1 parent b8a51a4 commit 0d36ded

File tree

2 files changed

+68
-65
lines changed

2 files changed

+68
-65
lines changed

src/extras/input/sources/gamepad-source.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import { InputSource } from '../input.js';
22

3+
const BUTTON_CODES = /** @type {const} */ ({
4+
A: 0,
5+
B: 1,
6+
X: 2,
7+
Y: 3,
8+
LB: 4,
9+
RB: 5,
10+
LT: 6,
11+
RT: 7,
12+
SELECT: 8,
13+
START: 9,
14+
LEFT_STICK: 10,
15+
RIGHT_STICK: 11
16+
});
17+
const BUTTON_COUNT = Object.keys(BUTTON_CODES).length;
18+
319
/**
420
* Game pad input source class
521
*
@@ -18,30 +34,17 @@ class GamepadSource extends InputSource {
1834
*
1935
* @readonly
2036
*/
21-
static buttonCode = {
22-
A: 0,
23-
B: 1,
24-
X: 2,
25-
Y: 3,
26-
LB: 4,
27-
RB: 5,
28-
LT: 6,
29-
RT: 7,
30-
SELECT: 8,
31-
START: 9,
32-
LEFT_STICK: 10,
33-
RIGHT_STICK: 11
34-
};
37+
static buttonCode = BUTTON_CODES;
3538

3639
/**
3740
* @type {number[]}
3841
* @private
3942
*/
40-
_buttonPrev = Array(11).fill(0);
43+
_buttonPrev = Array(BUTTON_COUNT).fill(0);
4144

4245
constructor() {
4346
super({
44-
buttons: Array(11).fill(0),
47+
buttons: Array(BUTTON_COUNT).fill(0),
4548
leftStick: [0, 0],
4649
rightStick: [0, 0]
4750
});
@@ -71,7 +74,7 @@ class GamepadSource extends InputSource {
7174
}
7275

7376
// check if gamepad has enough buttons
74-
if (gp.buttons.length < 12) {
77+
if (gp.buttons.length < BUTTON_COUNT) {
7578
continue;
7679
}
7780

src/extras/input/sources/keyboard-mouse-source.js

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,52 @@
11
import { InputSource } from '../input.js';
22

3-
/** @type {AddEventListenerOptions & EventListenerOptions} */
4-
const PASSIVE = { passive: false };
5-
const KEY_COUNT = 42;
3+
const PASSIVE = /** @type {AddEventListenerOptions & EventListenerOptions} */ ({ passive: false });
4+
const KEY_CODES = /** @type {const} */ ({
5+
A: 0,
6+
B: 1,
7+
C: 2,
8+
D: 3,
9+
E: 4,
10+
F: 5,
11+
G: 6,
12+
H: 7,
13+
I: 8,
14+
J: 9,
15+
K: 10,
16+
L: 11,
17+
M: 12,
18+
N: 13,
19+
O: 14,
20+
P: 15,
21+
Q: 16,
22+
R: 17,
23+
S: 18,
24+
T: 19,
25+
U: 20,
26+
V: 21,
27+
W: 22,
28+
X: 23,
29+
Y: 24,
30+
Z: 25,
31+
'0': 26,
32+
'1': 27,
33+
'2': 28,
34+
'3': 29,
35+
'4': 30,
36+
'5': 31,
37+
'6': 32,
38+
'7': 33,
39+
'8': 34,
40+
'9': 35,
41+
UP: 36,
42+
DOWN: 37,
43+
LEFT: 38,
44+
RIGHT: 39,
45+
SPACE: 40,
46+
SHIFT: 41,
47+
CTRL: 42
48+
});
49+
const KEY_COUNT = Object.keys(KEY_CODES).length;
650

751
const array = Array(KEY_COUNT).fill(0);
852

@@ -25,51 +69,7 @@ class KeyboardMouseSource extends InputSource {
2569
*
2670
* @readonly
2771
*/
28-
static keyCode = {
29-
A: 0,
30-
B: 1,
31-
C: 2,
32-
D: 3,
33-
E: 4,
34-
F: 5,
35-
G: 6,
36-
H: 7,
37-
I: 8,
38-
J: 9,
39-
K: 10,
40-
L: 11,
41-
M: 12,
42-
N: 13,
43-
O: 14,
44-
P: 15,
45-
Q: 16,
46-
R: 17,
47-
S: 18,
48-
T: 19,
49-
U: 20,
50-
V: 21,
51-
W: 22,
52-
X: 23,
53-
Y: 24,
54-
Z: 25,
55-
'0': 26,
56-
'1': 27,
57-
'2': 28,
58-
'3': 29,
59-
'4': 30,
60-
'5': 31,
61-
'6': 32,
62-
'7': 33,
63-
'8': 34,
64-
'9': 35,
65-
UP: 36,
66-
DOWN: 37,
67-
LEFT: 38,
68-
RIGHT: 39,
69-
SPACE: 40,
70-
SHIFT: 41,
71-
CTRL: 42
72-
};
72+
static keyCode = KEY_CODES;
7373

7474
/**
7575
* @type {number}

0 commit comments

Comments
 (0)