-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
52 lines (45 loc) · 1.56 KB
/
controller.js
File metadata and controls
52 lines (45 loc) · 1.56 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
window.addEventListener('gamepadconnected', (e) => {
console.log('Gamepad connected at index %d: %s. %d buttons, %d axes.', e.gamepad.index, e.gamepad.id, e.gamepad.buttons.length, e.gamepad.axes.length);
});
window.addEventListener('gamepadbuttondown', function (e) {
console.log('Button pressed:', e.button);
});
let button0 = false;
let button1 = false;
let timeout = false;
function updateGamepad() {
requestAnimationFrame(updateGamepad);
// We'll only get the first gamepad in our list.
let gamepads = [];
if (navigator.getGamepads) gamepads = navigator.getGamepads();
else if (navigator.webkitGetGamepads) gamepads = navigator.webkitGetGamepads();
const gamepad = gamepads[0];
// If our gamepad isn't connected, stop here.
if (timeout || !gamepad) return;
if (button0 !== gamepad.buttons[0].pressed) {
console.log('Button 1 pressed:', gamepad.buttons[0].pressed);
if (gamepad.buttons[0].pressed) {
switchPlaylist(0);
timeout = true;
startTimeout();
}
}
button0 = gamepad.buttons[0].pressed;
if (button1 !== gamepad.buttons[1].pressed) {
switchPlaylist(3);
console.log('Button 2 pressed:', gamepad.buttons[1].pressed);
if (gamepad.buttons[1].pressed) {
switchPlaylist(2);
timeout = true;
startTimeout();
}
}
button1 = gamepad.buttons[1].pressed;
}
function startTimeout() {
setTimeout(() => {
timeout = false;
console.log('reset timeout');
}, 3000);
}
updateGamepad();