-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgamepad.js
188 lines (183 loc) · 6.16 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
(function() {
var axesStatus, checkButtons, checkForGamePad, listeners, pad, padStatus;
listeners = {};
window.gamepad = {
isSupported: function() {
var gamepads;
gamepads = navigator.webkitGetGamepads || navigator.mozGetGamepads || navigator.getGamepads;
return gamepads !== void 0;
},
get: function(index) {
if (index != null) {
return gamepad.getAll()[index];
}
return gamepad.getAll();
},
getAll: function() {
if (!gamepad.isSupported()) {
return;
}
return navigator.webkitGetGamepads();
},
BUTTONS: {
face1: 0,
face2: 1,
face3: 2,
face4: 3,
leftShoulder: 4,
rightShoulder: 5,
leftShoulderBottom: 6,
rightShoulderBottom: 7,
select: 8,
start: 9,
leftStick: 10,
rightStick: 11,
up: 12,
down: 13,
left: 14,
right: 15
},
AXES: {
leftStickX: 0,
leftStickY: 1,
rightStickX: 2,
rightStickY: 3
},
on: function(event, callback) {
var _ref;
if ((_ref = listeners[event]) != null) {
_ref;
} else {
listeners[event] = [];
};
return listeners[event].push(callback);
},
fire: function(event, data) {
var callback, events, list, _i, _len, _results;
if (data != null) {
data;
} else {
data = {};
};
list = event.split(":");
events = [event, list[1], list[1] + ":" + list[2], list[0] + list[1]];
_results = [];
for (_i = 0, _len = events.length; _i < _len; _i++) {
event = events[_i];
_results.push((function() {
var _j, _len2, _ref, _results2;
if (listeners[event] != null) {
_ref = listeners[event];
_results2 = [];
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
callback = _ref[_j];
data.event = event;
data.button = list[2];
data.state = {
pad: padStatus,
axes: axesStatus
};
_results2.push(callback(data));
}
return _results2;
}
})());
}
return _results;
}
};
for (pad = 0; pad <= 3; pad++) {
gamepad[pad] = {
on: function(event, callback) {
return gamepad.on("" + pad + ":" + event, callback);
}
};
}
checkForGamePad = null;
padStatus = [[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]];
axesStatus = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
checkButtons = function() {
var data, event, index, name, normalized, padNumber, _ref, _ref2;
for (padNumber = 0; padNumber <= 3; padNumber++) {
pad = gamepad.get(padNumber);
if (pad !== void 0) {
_ref = gamepad.BUTTONS;
for (name in _ref) {
index = _ref[name];
if (padStatus[padNumber][index][0] !== pad.buttons[index]) {
padStatus[padNumber][index][0] = pad.buttons[index];
normalized = pad.buttons[index] > 1 - 0.07 ? 1 : 0;
data = {
value: pad.buttons[index],
normalizedValue: normalized,
pad: padNumber
};
if (normalized !== padStatus[padNumber][index][1]) {
padStatus[padNumber][index][1] = normalized;
event = normalized === 1 ? "press" : "release";
gamepad.fire("" + padNumber + ":" + event + ":" + name, data);
}
gamepad.fire("" + padNumber + ":change:" + name, data);
}
}
_ref2 = gamepad.AXES;
for (name in _ref2) {
index = _ref2[name];
if (0.07 < Math.abs(axesStatus[padNumber][index] - pad.axes[index])) {
axesStatus[padNumber][index] = pad.axes[index];
data = {
value: pad.axes[index],
pad: padNumber
};
gamepad.fire("" + padNumber + ":change:" + name, data);
}
}
}
}
return requestAnimationFrame(checkButtons);
};
checkForGamePad = function() {
pad = gamepad.get(0);
if (pad != null) {
console.log("setting up gamepad");
requestAnimationFrame(checkButtons);
return gamepad.fire("ready", {});
} else {
console.log("next");
return setTimeout(checkForGamePad, 1000);
}
};
gamepad.init = function(callback) {
if (callback != null) {
gamepad.on("ready", callback);
}
return checkForGamePad();
};
}).call(this);
gamepad.init();
gamepad.on("press:rightShoulder", Reveal.navigateNext);
gamepad.on("press:rightShoulderBottom", Reveal.navigatePrev);
gamepad.on("press:leftShoulder", Reveal.navigateNext);
gamepad.on("press:leftShoulderBottom", Reveal.navigatePrev);