-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepad.js
63 lines (53 loc) · 1.41 KB
/
gamepad.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
var gamepads = [];
var prevRawGamepadTypes = [];
var prevTimestamps = [];
function initGamepad()
{
gamepadtick();
}
function gamepadtick()
{
pollStatus();
scheduleNextTick();
}
function scheduleNextTick()
{
if (window.requestAnimationFrame) {
window.requestAnimationFrame(gamepadtick);
} else if (window.mozRequestAnimationFrame) {
window.mozRequestAnimationFrame(gamepadtick);
} else if (window.webkitRequestAnimationFrame) {
window.webkitRequestAnimationFrame(gamepadtick);
}
}
function pollStatus()
{
pollGamepads();
for (var i in gamepads) {
var gamepad = gamepads[i];
if (gamepad.timestamp &&
(gamepad.timestamp == prevTimestamps[i])) {
continue;
}
prevTimestamps[i] = gamepad.timestamp;
}
}
function pollGamepads()
{
var rawGamepads =
(navigator.getGamepads && navigator.getGamepads()) ||
(navigator.webkitGetGamepads && navigator.webkitGetGamepads());
if (rawGamepads) {
gamepads = [];
var gamepadsChanged = false;
for (var i = 0; i < rawGamepads.length; i++) {
if (typeof rawGamepads[i] != prevRawGamepadTypes[i]) {
gamepadsChanged = true;
prevRawGamepadTypes[i] = typeof rawGamepads[i];
}
if (rawGamepads[i]) {
gamepads.push(rawGamepads[i]);
}
}
}
}