-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgamepadtest.js
114 lines (102 loc) · 4.22 KB
/
gamepadtest.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Gamepad API Test
* Written in 2013 by Ted Mielczarek <[email protected]>
*
* To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
var haveEvents = 'GamepadEvent' in window;
var haveWebkitEvents = 'WebKitGamepadEvent' in window;
var controllers = {};
var rAF = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.requestAnimationFrame;
function connecthandler(e) {
addgamepad(e.gamepad);
}
function addgamepad(gamepad) {
controllers[gamepad.index] = gamepad;
for (var i=0; i<gamepad.buttons.length; i++) {
}
rAF(updateStatus);
}
function disconnecthandler(e) {
removegamepad(e.gamepad);
}
function removegamepad(gamepad) {
var d = document.getElementById("controller" + gamepad.index);
document.body.removeChild(d);
delete controllers[gamepad.index];
}
function updateStatus() {
scangamepads();
for (j in controllers) {
var controller = controllers[j];
// var d = document.getElementById("controller" + j);
// var buttons = d.getElementsByClassName("button");
for (var i=0; i<controller.buttons.length; i++) {
//var b = buttons[i];
var val = controller.buttons[i];
var pressed = val == 1.0;
if (typeof(val) == "object") {
pressed = val.pressed;
val = val.value;
}
//var pct = Math.round(val * 100) + "%";
//b.style.backgroundSize = pct + " " + pct;
if (pressed) {
// for (var iteam = 0; iteam < buttons.length; iteam++) {
// if(buttons[iteam] == i) {
// // the button of this team has been pressed!
// console.log("Team Button pressed" + iteam);
// }
// }
// these calculations on the button index work for the
// wired version of the BUZZ(tm) controller for
// Playstation2:
iteam = Math.floor(i / 5)+1;
ibutton = Math.floor(i % 5);
BIG_BUZZER_IBUTTON = 0;
if(answer_allow_only_iteam > 0 && iteam != answer_allow_only_iteam)
continue;
console.log("Button pressed" +i+"; iteam:" + iteam+"; ibutton:" + ibutton);
// listen to buttons only when the riddle-modal screen is shown
if(ibutton == BIG_BUZZER_IBUTTON && $('#riddle-modal').hasClass("waiting-for-buzzers")) {
if(!$("#player-color-visualisation").hasClass("is-triggered")){
startAudio("sounds/buzzer"+iteam+".mp3", false);
$("#player-color-visualisation").addClass("buzzer-border-team-"+iteam);
$("#player-color-visualisation").addClass("is-triggered");
$('#player-color-visualisation').css({
"display": "inline-block",
});
}
}
} else {
// button is not pressed.
}
}
}
rAF(updateStatus);
}
function scangamepads() {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
for (var i = 0; i < gamepads.length; i++) {
if (gamepads[i]) {
if (!(gamepads[i].index in controllers)) {
addgamepad(gamepads[i]);
} else {
controllers[gamepads[i].index] = gamepads[i];
}
}
}
}
if (haveEvents) {
window.addEventListener("gamepadconnected", connecthandler);
window.addEventListener("gamepaddisconnected", disconnecthandler);
} else if (haveWebkitEvents) {
window.addEventListener("webkitgamepadconnected", connecthandler);
window.addEventListener("webkitgamepaddisconnected", disconnecthandler);
} else {
setInterval(scangamepads, 500);
}