-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepadpool.html
124 lines (102 loc) · 3 KB
/
gamepadpool.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<html>
<script>
const haveEvents = 'ongamepadconnected' in window;
const controllers = {};
function connecthandler(e) {
addgamepad(e.gamepad);
}
function addgamepad(gamepad) {
controllers[gamepad.index] = gamepad;
const d = document.createElement("div");
d.setAttribute("id", `controller${gamepad.index}`);
const t = document.createElement("h1");
t.textContent = `gamepad: ${gamepad.id}`;
d.appendChild(t);
const b = document.createElement("div");
b.className = "buttons";
gamepad.buttons.forEach((button, i) => {
const e = document.createElement("span");
e.className = "button";
e.textContent = i;
b.appendChild(e);
});
d.appendChild(b);
const a = document.createElement("div");
a.className = "axes";
gamepad.axes.forEach((axis, i) => {
const p = document.createElement("progress");
p.className = "axis";
p.setAttribute("max", "2");
p.setAttribute("value", "1");
p.textContent = i;
a.appendChild(p);
});
d.appendChild(a);
// See https://github.com/luser/gamepadtest/blob/master/index.html
const start = document.getElementById("start");
if (start) {
start.style.display = "none";
}
document.body.appendChild(d);
requestAnimationFrame(updateStatus);
}
function disconnecthandler(e) {
removegamepad(e.gamepad);
}
function removegamepad(gamepad) {
const d = document.getElementById(`controller${gamepad.index}`);
document.body.removeChild(d);
delete controllers[gamepad.index];
}
function updateStatus() {
if (!haveEvents) {
scangamepads();
}
for (let controller in controllers) {
i=0;
const d = document.getElementById(`controller${i}`);
const buttons = d.getElementsByClassName("button");
controller = controllers[controller];
controller.buttons.forEach((button, i) => {
const b = buttons[i];
let pressed = button === 1.0;
let val = button;
if (typeof button === "object") {
pressed = val.pressed;
val = val.value;
}
const pct = `${Math.round(val * 100)}%`;
b.style.backgroundSize = `${pct} ${pct}`;
b.className = pressed ? "button pressed" : "button";
if( pressed ) {
console.log("Manoj-->> Button num= " + i +" val= " + val );
}
});
const axes = d.getElementsByClassName("axis");
controller.axes.forEach((axis, i) => {
const a = axes[i];
// a.textContent = `${i}: ${controller.axis.toFixed(4)}`;
//a.setAttribute("value", controller.axis + 1);
});
}
requestAnimationFrame(updateStatus);
}
function scangamepads() {
const gamepads = navigator.getGamepads();
for (const gamepad of gamepads) {
if (gamepad) { // Can be null if disconnected during the session
if (gamepad.index in controllers) {
controllers[gamepad.index] = gamepad;
} else {
addgamepad(gamepad);
}
}
}
}
window.addEventListener("gamepadconnected", connecthandler);
window.addEventListener("gamepaddisconnected", disconnecthandler);
if (!haveEvents) {
setInterval(scangamepads, 500);
}
</script>
</html>