-
Notifications
You must be signed in to change notification settings - Fork 11
/
simple-input-js-test.html
99 lines (84 loc) · 3.09 KB
/
simple-input-js-test.html
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
97
98
99
<!DOCTYPE html>
<html>
<head>
<title>Input.js test page</title>
<script type="text/javascript" src="input.js"></script>
<script type="text/javascript">
var gamepads = [];
function connect(e) {
var container = document.createElement("div");
container.setAttribute("id", "gamepad" + e.gamepad.index);
document.body.appendChild(container);
try {
var gamepad = new Input.Device(e.gamepad);
} catch (e) {
container.innerHTML = e;
return;
}
gamepads[gamepad.index] = gamepad;
document.getElementById("start").style.display = "none";
var header = document.createElement("h2");
header.appendChild(document.createTextNode("Gamepad: " + gamepad.id));
container.appendChild(header);
header = document.createElement("h3");
header.appendChild(document.createTextNode("Axes"));
container.appendChild(header);
var ul = document.createElement("ul");
var iter = Object.keys(gamepad.axes);
for (var i in iter) {
var li = document.createElement("li");
var prog = document.createElement("progress");
prog.setAttribute("max", "2");
prog.setAttribute("value", "1");
prog.setAttribute("id", gamepad.id + "-" + iter[i]);
li.appendChild(document.createTextNode(iter[i]));
li.appendChild(prog);
ul.appendChild(li);
}
container.appendChild(ul);
header = document.createElement("h3");
header.appendChild(document.createTextNode("Buttons"));
container.appendChild(header);
ul = document.createElement("ul");
iter = Object.keys(gamepad.buttons);
for (var i in Object.keys(gamepad.buttons)) {
li = document.createElement("li");
prog = document.createElement("progress");
prog.setAttribute("max", "1");
prog.setAttribute("value", "0");
prog.setAttribute("id", gamepad.id + "-" + iter[i]);
li.appendChild(document.createTextNode(iter[i]));
li.appendChild(prog);
ul.appendChild(li);
}
container.appendChild(ul);
window.mozRequestAnimationFrame(updateStatus);
}
function updateStatus(e) {
for (var g in gamepads) {
var gamepad = gamepads[g];
var id = gamepad.id + "-";
var iter = Object.keys(gamepad.axes);
for (var i in iter) {
document.getElementById(id + iter[i]).setAttribute("value", gamepad.axes[iter[i]] + 1);
}
iter = Object.keys(gamepad.buttons);
for (var i in iter) {
document.getElementById(id + iter[i]).setAttribute("value", gamepad.buttons[iter[i]]);
}
}
window.mozRequestAnimationFrame(updateStatus);
}
function disconnect(e) {
var d = document.getElementById("gamepad" + e.gamepad.index);
document.body.removeChild(d);
delete joys[e.gamepad.index];
}
window.addEventListener("MozGamepadConnected", connect);
window.addEventListener("MozGamepadDiconnected", disconnect);
</script>
</head>
<body>
<h1 id="start">Press a button on your controller to start</h1>
</body>
</html>