-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPxGamepad.js
184 lines (151 loc) · 5.07 KB
/
PxGamepad.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
(function() {
function PxGamepad() {
// map button indices to names
this.buttonNames = [
'a',
'b',
'x',
'y',
'leftTop',
'rightTop',
'leftTrigger',
'rightTrigger',
'select',
'start',
'leftStick',
'rightStick',
'dpadUp',
'dpadDown',
'dpadLeft',
'dpadRight'
];
// callbacks for buton up listeners
this.callbacks = {};
// some browsers use an event to provide the gamepad when connected
this.connectedGamepad = null;
this.reset();
}
// reset button and stick state
PxGamepad.prototype.reset = function() {
this.leftStick = { x: 0, y: 0 };
this.rightStick = { x: 0, y: 0 };
this.dpad = { x: 0, y: 0 };
this.buttons = {};
};
// start listening for gamepad connection events
PxGamepad.prototype.start = function() {
this.reset();
this.listeners = {
'gamepadconnected': jQuery.proxy(function(e) {
var gamepad = e.originalEvent.gamepad;
if (gamepad.mapping === 'standard') {
this.connectedGamepad = gamepad;
}
}),
'gamepaddisconnected': jQuery.proxy(function(e) {
var gamepad = e.originalEvent.gamepad;
if (this.connectedGamepad === gamepad) {
this.connectedGamepad = null;
}
})
};
jQuery(window).on(this.listeners);
};
// stop listening to gamepad connection events
PxGamepad.prototype.stop = function() {
jQuery(window).off(this.listeners);
this.connectedGamepad = null;
};
// listen to button up events
PxGamepad.prototype.on = function(buttonName, callback) {
var buttonCallbacks = this.callbacks[buttonName];
if (!buttonCallbacks) {
this.callbacks[buttonName] = [ callback ];
} else {
buttonCallbacks.push(callback);
}
};
// remove button up event listeners
PxGamepad.prototype.off = function(buttonName, callback) {
var buttonCallbacks = this.callbacks[buttonName];
if (buttonCallbacks) {
if (!callback) {
// remove all callbacks
this.callbacks = [];
} else {
// search for specified callback
var callbackIndex = buttonCallbacks.indexOf(callback);
if (callbackIndex >= 0) {
buttonCallbacks.splice(callbackIndex, 1);
}
}
}
};
function buttonPressed(gamepad, index) {
if (!gamepad || !gamepad.buttons || index >= gamepad.buttons.length) {
return false;
}
var b = gamepad.buttons[index];
if (!b) {
return false;
}
if (typeof(b) === "object") {
return b.pressed;
}
return (b === 1.0);
}
// helper to retrieve the currently connected gamepad
PxGamepad.prototype.getGamepad = function() {
// default to connected gamepad
var gp = this.connectedGamepad;
if (gp) {
return gp;
}
// fetch all available gamepads
var gamepads;
if (navigator.getGamepads) {
gamepads = navigator.getGamepads();
} else if (navigator.webkitGetGamepads) {
gamepads = navigator.webkitGetGamepads();
}
// look for a standard mapped gamepad
if (gamepads) {
for (var i = 0, len = gamepads.length; i < len; i++) {
gp = gamepads[i];
if (gp && gp.mapping === 'standard') {
return gp;
}
}
}
return null;
};
// should be called during each frame update
PxGamepad.prototype.update = function() {
// make sure we have a gamepad
var gp = this.getGamepad();
if (!gp) {
return;
}
// check state of each of the buttons
var i, len, name, wasDown, isDown;
for (i = 0, len = this.buttonNames.length; i < len; i++) {
name = this.buttonNames[i];
wasDown = !!this.buttons[name];
isDown = this.buttons[name] = buttonPressed(gp, i);
if (wasDown && !isDown) {
jQuery.each(this.callbacks[name] || [], function(i, callback) {
if (callback) { callback(); }
});
}
}
// update the sticks
this.leftStick.x = gp.axes[0];
this.leftStick.y = gp.axes[1];
this.rightStick.x = gp.axes[2];
this.rightStick.y = gp.axes[3];
// dpad isn't a true stick, infer from buttons
this.dpad.x = (this.buttons.dpadLeft ? -1 : 0) + (this.buttons.dpadRight ? 1 : 0);
this.dpad.y = (this.buttons.dpadUp ? -1 : 0) + (this.buttons.dpadDown ? 1 : 0);
};
window.PxGamepad = PxGamepad;
})();