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
// Default analog stick axis threshold used to interpret directional input.
// Callers can override this by passing a custom `axisThreshold` value.
const DEFAULT_AXIS_THRESHOLD = 0.5;
export function mapStandardGamepadState(gamepad, axisThreshold = DEFAULT_AXIS_THRESHOLD) {
const buttons = gamepad?.buttons ?? [];
const axes = gamepad?.axes ?? [];
const up = Boolean(buttons[12]?.pressed) || axes[1] < -axisThreshold;
const down = Boolean(buttons[13]?.pressed) || axes[1] > axisThreshold;
const left = Boolean(buttons[14]?.pressed) || axes[0] < -axisThreshold;
const right = Boolean(buttons[15]?.pressed) || axes[0] > axisThreshold;
return {
a: Boolean(buttons[0]?.pressed),
b: Boolean(buttons[1]?.pressed),
select: Boolean(buttons[8]?.pressed),
start: Boolean(buttons[9]?.pressed),
up,
down,
left,
right
};
}
export function selectPrimaryGamepad(gamepads) {
if (!gamepads) return null;
for (const gamepad of gamepads) {
if (gamepad && gamepad.connected) {
return gamepad;
}
}
return null;
}
/**
* Select up to two connected gamepads from the gamepads array.
*
* @param {Gamepad[]} gamepads - Array of gamepads (may contain null/undefined)
* @returns {Gamepad[]} Array of connected gamepads (0-2 elements)
*/
export function selectGamepads(gamepads) {
if (!gamepads) return [];
const connected = [];
for (const gamepad of gamepads) {
if (gamepad && gamepad.connected) {
connected.push(gamepad);
if (connected.length >= 2) {
break;
}
}
}
return connected;
}