This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgamepad.js
96 lines (86 loc) · 3.52 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
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
"use strict";
function Init() {
if (navigator.getGamepads === undefined) {
document.getElementById("gamepadSupportedDiv").style.display = "block";
document.getElementById("gamepadDisplayDiv").style.display = "none";
} else {
window.requestAnimationFrame(runAnimation);
}
}
// --------------------------------------
// Animation loop
// --------------------------------------
var buttonPressedOnAnyGamepadEver = false;
var gamepadVisualizers = [];
function runAnimation() {
// Get the latest gamepad state.
var gamepads = navigator.getGamepads();
for (var i = 0; i < gamepads.length; i++) {
var pad = gamepads[i];
if (pad) {
// Gamepads physically plugged into the system will not be visible to JavaScript until
// the user has pressed a button on a gamepad. Note that each browser has slightly different
// behavior for which buttons need to be pressed.
if (!buttonPressedOnAnyGamepadEver) {
document.getElementById("buttonNeverPressedDiv").style.display = "none";
document.getElementById("buttonPressedDiv").style.display = "block";
buttonPressedOnAnyGamepadEver = true;
}
var usingStandardMapping = (pad.mapping && pad.mapping === "standard");
var gamepadVisualizer = usingStandardMapping ? new StandardGamepadVisualizer(pad) : new GenericGamepadVisualizer(pad);
gamepadVisualizers[i] = gamepadVisualizer;
} else {
if (gamepadVisualizers[i]) {
gamepadVisualizers[i].retired = true;
}
}
}
for (var i = 0; i < gamepadVisualizers.length; i++) {
var gamepadVisualizer = gamepadVisualizers[i];
if (gamepadVisualizers[i]) {
gamepadVisualizer.UpdateView();
}
}
window.requestAnimationFrame(runAnimation);
}
// --------------------------------------
// Misc.
// --------------------------------------
function FloatValueAsString(flValue) {
var strVal = flValue.toString();
strVal = strVal.substring(0, 4);
return strVal;
}
var stateTableRowTemplate = '\
<td><div>gpIndex</div></td>\
<td><div>gpTimestamp</div></td>\
<td><div>gpMapping</div></td>\
<td><div>gpConnected</div></td>\
<td><div>gpId</div></td>\
';
function UpdateGamepadStateTable(gamepad, index) {
var connectedStr = "N/A";
var indexStr = "N/A";
var timestampStr = "N/A";
var mappingStr = "N/A";
var idStr = "N/A";
if (gamepad) {
idStr = (gamepad.id !== undefined) ? gamepad.id : "undefined";
mappingStr = (gamepad.mapping !== undefined) ? gamepad.mapping : "undefined";
indexStr = (gamepad.index !== undefined) ? gamepad.index : "undefined";
connectedStr = (gamepad.connected !== undefined) ? gamepad.connected : "undefined";
timestampStr = (gamepad.timestamp !== undefined) ? (gamepad.timestamp / 1000) + "s" : "undefined";
}
var newRow = stateTableRowTemplate;
newRow = newRow.replace(/gpIndex/g, indexStr);
newRow = newRow.replace(/gpTimestamp/g, timestampStr);
newRow = newRow.replace(/gpMapping/g, mappingStr);
newRow = newRow.replace(/gpConnected/g, connectedStr);
newRow = newRow.replace(/gpId/g, idStr);
var containerElem = document.getElementById("gpStateTableRow" + index);
containerElem.innerHTML = newRow.replace(/\[#\]/g, index);
}
function ClearGamepadStateTableRow(index) {
var containerElem = document.getElementById("gpStateTableRow" + index);
containerElem.innerHTML = "";
}