Skip to content
Browse files

Updated button change events, added axis event, improved example.

  • Loading branch information...
1 parent 872749b commit ab1963255d25629dd0d01eb61bb01aac479ebcc9 @kallaspriit committed Feb 26, 2013
Showing with 136 additions and 83 deletions.
  1. +120 −72 gamepad.js
  2. +1 −1 gamepad.min.js
  3. +15 −10 index.html
View
192 gamepad.js
@@ -21,13 +21,16 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
+(function(window) {
+'use strict';
+
/**
* Provides simple interface and multi-platform support for the gamepad API.
*
* You can change the deadzone and maximizeThreshold parameters to suit your
* taste but the defaults should generally work fine.
*/
-Gamepad = function() {
+var Gamepad = function() {
this.gamepads = [];
this.listeners = {};
this.platform = null;
@@ -61,14 +64,17 @@ Gamepad.Type = {
* question and tick provides the list of all connected gamepads.
*
* BUTTON_DOWN and BUTTON_UP events provide an alternative to polling button states at each tick.
+ *
+ * AXIS_CHANGED is called if a value of some specific axis changes.
*/
Gamepad.Event = {
CONNECTED: 'connected',
DISCONNECTED: 'disconnected',
TICK: 'tick',
UNSUPPORTED: 'unsupported',
- BUTTON_DOWN: 'buttonDown',
- BUTTON_UP: 'buttonUp'
+ BUTTON_DOWN: 'button-down',
+ BUTTON_UP: 'button-up',
+ AXIS_CHANGED: 'axis-changed'
};
/**
@@ -222,7 +228,7 @@ Gamepad.Mapping = {
RIGHT_STICK_Y: 3
}
}
-}
+};
/**
* Initializes the gamepad.
@@ -243,10 +249,9 @@ Gamepad.prototype.init = function() {
case Gamepad.Platform.UNSUPPORTED:
return false;
- break;
}
- if (typeof(window.requestAnimationFrame) == 'undefined') {
+ if (typeof(window.requestAnimationFrame) === 'undefined') {
window.requestAnimationFrame = window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame;
}
@@ -259,12 +264,12 @@ Gamepad.prototype.init = function() {
/**
* Binds a listener to a gamepad event.
*
- * @param {string} event Event to bind to, one of Gamepad.Event..
- * @param {function} listener Listener to call when given event occurs
+ * @param {String} event Event to bind to, one of Gamepad.Event..
+ * @param {Function} listener Listener to call when given event occurs
* @return {Gamepad} Returns self
*/
Gamepad.prototype.bind = function(event, listener) {
- if (typeof(this.listeners[event]) == 'undefined') {
+ if (typeof(this.listeners[event]) === 'undefined') {
this.listeners[event] = [];
}
@@ -276,7 +281,7 @@ Gamepad.prototype.bind = function(event, listener) {
/**
* Returns the number of connected gamepads.
*
- * @return {number}
+ * @return {Number}
*/
Gamepad.prototype.count = function() {
return this.gamepads.length;
@@ -285,11 +290,11 @@ Gamepad.prototype.count = function() {
/**
* Fires an internal event with given data.
*
- * @param {string} Event to fire, one of Gamepad.Event..
- * @param {any} data Data to pass to the listener
+ * @param {String} Event to fire, one of Gamepad.Event..
+ * @param {*} data Data to pass to the listener
*/
Gamepad.prototype._fire = function(event, data) {
- if (typeof(this.listeners[event]) == 'undefined') {
+ if (typeof(this.listeners[event]) === 'undefined') {
return;
}
@@ -301,12 +306,12 @@ Gamepad.prototype._fire = function(event, data) {
/**
* Resolves platform.
*
- * @return {string} One of Gamepad.Platform..
+ * @return {String} One of Gamepad.Platform..
*/
Gamepad.prototype._resolvePlatform = function() {
if (
- typeof(navigator.webkitGamepads) != 'undefined'
- || typeof(navigator.webkitGetGamepads) != 'undefined'
+ typeof(window.navigator.webkitGamepads) !== 'undefined'
+ || typeof(window.navigator.webkitGetGamepads) !== 'undefined'
) {
return Gamepad.Platform.WEBKIT;
} else {
@@ -338,25 +343,25 @@ Gamepad.prototype._setupFirefox = function() {
/**
* Returns mapping for given type.
*
- * @param {string} type One of Gamepad.Type..
- * @return {object} Mapping or null if not supported
+ * @param {String} type One of Gamepad.Type..
+ * @return {Object|null} Mapping or null if not supported
*/
Gamepad.prototype._getMapping = function(type) {
switch (type) {
case Gamepad.Type.PLAYSTATION:
- if (this.platform == Gamepad.Platform.FIREFOX) {
+ if (this.platform === Gamepad.Platform.FIREFOX) {
return Gamepad.Mapping.PLAYSTATION_FIREFOX;
- } else if (this.platform == Gamepad.Platform.WEBKIT) {
+ } else if (this.platform === Gamepad.Platform.WEBKIT) {
return Gamepad.Mapping.PLAYSTATION_WEBKIT;
} else {
return null;
}
break;
case Gamepad.Type.LOGITECH:
- if (this.platform == Gamepad.Platform.FIREFOX) {
+ if (this.platform === Gamepad.Platform.FIREFOX) {
return Gamepad.Mapping.LOGITECH_FIREFOX;
- } else if (this.platform == Gamepad.Platform.WEBKIT) {
+ } else if (this.platform === Gamepad.Platform.WEBKIT) {
return Gamepad.Mapping.LOGITECH_WEBKIT;
} else {
return null;
@@ -365,7 +370,6 @@ Gamepad.prototype._getMapping = function(type) {
case Gamepad.Type.XBOX:
return Gamepad.Mapping.XBOX;
- break;
}
return null;
@@ -374,39 +378,40 @@ Gamepad.prototype._getMapping = function(type) {
/**
* Registers given gamepad.
*
- * @param {object} gamepad Gamepad to connect to
- * @return {boolean} Was connecting the gamepad successful
+ * @param {Object} gamepad Gamepad to connect to
+ * @return {Boolean} Was connecting the gamepad successful
*/
Gamepad.prototype._connect = function(gamepad) {
gamepad.type = this._resolveControllerType(gamepad.id);
- if (gamepad.type == Gamepad.Type.UNSUPPORTED) {
+ if (gamepad.type === Gamepad.Type.UNSUPPORTED) {
this._fire(Gamepad.Event.UNSUPPORTED, gamepad);
return false;
}
gamepad.mapping = this._getMapping(gamepad.type);
- if (gamepad.mapping == null) {
+ if (gamepad.mapping === null) {
this._fire(Gamepad.Event.UNSUPPORTED, gamepad);
return false;
}
- gamepad.prevState = {};
+ gamepad.lastState = {};
gamepad.state = {};
- var key;
+ var key,
+ axis;
for (key in gamepad.mapping.buttons) {
gamepad.state[key] = 0;
- gamepad.prevState[key] = 0;
+ gamepad.lastState[key] = 0;
}
- for (key in gamepad.mapping.axes) {
- gamepad.state[key] = 0;
- gamepad.prevState[key] = 0;
+ for (axis in gamepad.mapping.axes) {
+ gamepad.state[axis] = 0;
+ gamepad.lastState[axis] = 0;
}
this.gamepads[gamepad.index] = gamepad;
@@ -419,18 +424,18 @@ Gamepad.prototype._connect = function(gamepad) {
/**
* Disconnects from given gamepad.
*
- * @param {object} gamepad Gamepad to disconnect
+ * @param {Object} gamepad Gamepad to disconnect
*/
Gamepad.prototype._disconnect = function(gamepad) {
var newGamepads = [],
i;
- if (typeof(this.gamepads[gamepad.index]) != 'undefined') {
+ if (typeof(this.gamepads[gamepad.index]) !== 'undefined') {
delete this.gamepads[gamepad.index];
}
for (i = 0; i < this.gamepads.length; i++) {
- if (typeof(this.gamepads[i]) != 'undefined') {
+ if (typeof(this.gamepads[i]) !== 'undefined') {
newGamepads[i] = this.gamepads[i];
}
}
@@ -443,20 +448,20 @@ Gamepad.prototype._disconnect = function(gamepad) {
/**
* Resolves controller type from its id.
*
- * @param {string} id Controller id
- * @return {string} Controller type, one of Gamepad.Type..
+ * @param {String} id Controller id
+ * @return {String} Controller type, one of Gamepad.Type
*/
Gamepad.prototype._resolveControllerType = function(id) {
id = id.toLowerCase();
- if (id.indexOf('playstation') != -1) {
+ if (id.indexOf('playstation') !== -1) {
return Gamepad.Type.PLAYSTATION;
} else if (
- id.indexOf('logitech') != -1
- || id.indexOf('wireless gamepad') != -1
+ id.indexOf('logitech') !== -1
+ || id.indexOf('wireless gamepad') !== -1
) {
return Gamepad.Type.LOGITECH;
- } else if (id.indexOf('xbox') != -1) {
+ } else if (id.indexOf('xbox') !== -1) {
return Gamepad.Type.XBOX;
} else {
return Gamepad.Type.UNSUPPORTED;
@@ -469,6 +474,7 @@ Gamepad.prototype._resolveControllerType = function(id) {
Gamepad.prototype._update = function() {
var self = this,
controlName,
+ isDown,
mapping,
value,
i;
@@ -484,54 +490,92 @@ Gamepad.prototype._update = function() {
}
for (i = 0; i < this.gamepads.length; i++) {
- if (typeof(this.gamepads[i]) == 'undefined') {
+ if (typeof(this.gamepads[i]) === 'undefined') {
continue;
}
for (controlName in this.gamepads[i].mapping.buttons) {
mapping = this.gamepads[i].mapping.buttons[controlName];
-
- this.gamepads[i].prevState[controlName] = this.gamepads[i].state[controlName];
- if (typeof(mapping) == 'function') {
- this.gamepads[i].state[controlName] = mapping(
+ if (typeof(mapping) === 'function') {
+ value = mapping(
this.gamepads[i],
this
);
} else {
value = this.gamepads[i].buttons[mapping];
+ }
+
+ isDown = value > 0.5 ? 1 : 0;
- if (typeof(this.gamepads[i].buttons[mapping]) != 'undefined') {
- this.gamepads[i].state[controlName] = value;
+ this.gamepads[i].state[controlName] = value;
+
+ if (isDown !== this.gamepads[i].lastState[controlName]) {
+ if (value > 0.5) {
+ this._fire(
+ Gamepad.Event.BUTTON_DOWN,
+ {
+ gamepad: this.gamepads[i],
+ mapping: mapping,
+ control: controlName
+ }
+ );
+ } else if (value < 0.5) {
+ this._fire(
+ Gamepad.Event.BUTTON_UP,
+ {
+ gamepad: this.gamepads[i],
+ mapping: mapping,
+ control: controlName
+ }
+ );
}
- }
- if(this.gamepads[i].prevState[controlName] != this.gamepads[i].state[controlName]) {
- if(this.gamepads[i].state[controlName] == 1) {
- this._fire(Gamepad.Event.BUTTON_DOWN, {gamepadIndex: i, control: controlName} );
- } else {
- this._fire(Gamepad.Event.BUTTON_UP, {gamepadIndex: i, control: controlName} );
+ if (value !== 0 && value !== 1) {
+ this._fire(
+ Gamepad.Event.AXIS_CHANGED,
+ {
+ gamepad: this.gamepads[i],
+ mapping: mapping,
+ axis: controlName,
+ value: value
+ }
+ );
}
}
+
+ this.gamepads[i].lastState[controlName] = isDown;
}
for (controlName in this.gamepads[i].mapping.axes) {
mapping = this.gamepads[i].mapping.axes[controlName];
- if (typeof(mapping) == 'function') {
- this.gamepads[i].state[controlName] = mapping(
+ if (typeof(mapping) === 'function') {
+ value = mapping(
this.gamepads[i],
this
);
} else {
value = this._applyDeadzoneMaximize(
this.gamepads[i].axes[mapping]
);
-
- if (typeof(this.gamepads[i].axes[mapping]) != 'undefined') {
- this.gamepads[i].state[controlName] = value;
- }
}
+
+ this.gamepads[i].state[controlName] = value;
+
+ if (value !== this.gamepads[i].lastState[controlName]) {
+ this._fire(
+ Gamepad.Event.AXIS_CHANGED,
+ {
+ gamepad: this.gamepads[i],
+ mapping: mapping,
+ axis: controlName,
+ value: value
+ }
+ );
+ }
+
+ this.gamepads[i].lastState[controlName] = value;
}
}
@@ -550,33 +594,33 @@ Gamepad.prototype._update = function() {
Gamepad.prototype._updateWebkit = function() {
var gamepads;
- if (typeof(navigator.webkitGamepads) == 'object') {
- gamepads = navigator.webkitGamepads;
- } else if (typeof(navigator.webkitGetGamepads) == 'function') {
- gamepads = navigator.webkitGetGamepads();
+ if (typeof(window.navigator.webkitGamepads) === 'object') {
+ gamepads = window.navigator.webkitGamepads;
+ } else if (typeof(window.navigator.webkitGetGamepads) === 'function') {
+ gamepads = window.navigator.webkitGetGamepads();
} else {
return; // should not happen
}
- if (gamepads.length != this.gamepads.length) {
+ if (gamepads.length !== this.gamepads.length) {
var gamepad,
i;
for (i = 0; i < gamepads.length; i++) {
gamepad = gamepads[i];
if (
- typeof(gamepad) != 'undefined'
- && typeof(this.gamepads[gamepad.index]) == 'undefined'
+ typeof(gamepad) !== 'undefined'
+ && typeof(this.gamepads[gamepad.index]) === 'undefined'
) {
this._connect(gamepad);
}
}
for (i = 0; i < this.gamepads.length; i++) {
if (
- typeof(this.gamepads[i]) != 'undefined'
- && typeof(gamepads[i]) == 'undefined'
+ typeof(this.gamepads[i]) !== 'undefined'
+ && typeof(gamepads[i]) === 'undefined'
) {
this._disconnect(this.gamepads[i]);
}
@@ -605,10 +649,10 @@ Gamepad.prototype._applyDeadzoneMaximize = function(
deadzone,
maximizeThreshold
) {
- deadzone = typeof(deadzone) != 'undefined'
+ deadzone = typeof(deadzone) !== 'undefined'
? deadzone
: this.deadzone;
- maximizeThreshold = typeof(maximizeThreshold) != 'undefined'
+ maximizeThreshold = typeof(maximizeThreshold) !== 'undefined'
? maximizeThreshold
: this.maximizeThreshold;
@@ -628,3 +672,7 @@ Gamepad.prototype._applyDeadzoneMaximize = function(
return value;
};
+
+window.Gamepad = Gamepad;
+
+})(window);
View
2 gamepad.min.js
@@ -1 +1 @@
-Gamepad=function(){this.gamepads=[];this.listeners={};this.platform=null;this.deadzone=.03;this.maximizeThreshold=.97};Gamepad.Platform={UNSUPPORTED:"unsupported",WEBKIT:"webkit",FIREFOX:"firefox"};Gamepad.Type={PLAYSTATION:"playstation",LOGITECH:"logitech",XBOX:"xbox",UNSUPPORTED:"unsupported"};Gamepad.Event={CONNECTED:"connected",DISCONNECTED:"disconnected",TICK:"tick",UNSUPPORTED:"unsupported",BUTTON_DOWN:"buttonDown",BUTTON_UP:"buttonUp"};Gamepad.Mapping={PLAYSTATION_FIREFOX:{buttons:{CROSS:14,CIRCLE:13,SQUARE:15,TRIANGLE:12,LB1:10,RB1:11,LEFT_STICK:1,RIGHT_STICK:2,START:3,SELECT:0,HOME:16,DPAD_UP:4,DPAD_DOWN:6,DPAD_LEFT:7,DPAD_RIGHT:5},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:2,RIGHT_STICK_Y:3}},PLAYSTATION_WEBKIT:{buttons:{CROSS:0,CIRCLE:1,SQUARE:2,TRIANGLE:3,LB1:4,RB1:5,LB2:6,RB2:7,LEFT_STICK:10,RIGHT_STICK:11,START:9,SELECT:8,HOME:16,DPAD_UP:12,DPAD_DOWN:13,DPAD_LEFT:14,DPAD_RIGHT:15},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:2,RIGHT_STICK_Y:3}},LOGITECH_FIREFOX:{buttons:{A:0,B:1,X:2,Y:3,LB:4,RB:5,LEFT_STICK:8,RIGHT_STICK:9,START:7,BACK:6,HOME:10,DPAD_UP:11,DPAD_DOWN:12,DPAD_LEFT:13,DPAD_RIGHT:14},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:3,RIGHT_STICK_Y:4,LEFT_TRIGGER:function(e,t){if(e.axes[2]>0){return t._applyDeadzoneMaximize(e.axes[2])}else{return 0}},RIGHT_TRIGGER:function(e,t){if(e.axes[2]<0){return t._applyDeadzoneMaximize(e.axes[2]*-1)}else{return 0}}}},LOGITECH_WEBKIT:{buttons:{A:1,B:2,X:0,Y:3,LB:4,RB:5,LEFT_TRIGGER:6,RIGHT_TRIGGER:7,LEFT_STICK:10,RIGHT_STICK:11,START:9,BACK:8,HOME:10,DPAD_UP:11,DPAD_DOWN:12,DPAD_LEFT:13,DPAD_RIGHT:14},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:2,RIGHT_STICK_Y:3}},XBOX:{buttons:{A:0,B:1,X:2,Y:3,LB:4,RB:5,LEFT_TRIGGER:6,RIGHT_TRIGGER:7,LEFT_STICK:10,RIGHT_STICK:11,START:9,BACK:8,DPAD_UP:12,DPAD_DOWN:13,DPAD_LEFT:14,DPAD_RIGHT:15},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:2,RIGHT_STICK_Y:3}}};Gamepad.prototype.init=function(){this.platform=this._resolvePlatform();switch(this.platform){case Gamepad.Platform.WEBKIT:this._setupWebkit();break;case Gamepad.Platform.FIREFOX:this._setupFirefox();break;case Gamepad.Platform.UNSUPPORTED:return false;break}if(typeof window.requestAnimationFrame=="undefined"){window.requestAnimationFrame=window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame}this._update();return true};Gamepad.prototype.bind=function(e,t){if(typeof this.listeners[e]=="undefined"){this.listeners[e]=[]}this.listeners[e].push(t);return this};Gamepad.prototype.count=function(){return this.gamepads.length};Gamepad.prototype._fire=function(e,t){if(typeof this.listeners[e]=="undefined"){return}for(var n=0;n<this.listeners[e].length;n++){this.listeners[e][n].apply(this.listeners[e][n],[t])}};Gamepad.prototype._resolvePlatform=function(){if(typeof navigator.webkitGamepads!="undefined"||typeof navigator.webkitGetGamepads!="undefined"){return Gamepad.Platform.WEBKIT}else{return Gamepad.Platform.FIREFOX}};Gamepad.prototype._setupWebkit=function(){};Gamepad.prototype._setupFirefox=function(){var e=this;window.addEventListener("MozGamepadConnected",function(t){e._connect(t.gamepad)});window.addEventListener("MozGamepadDisconnected",function(t){e._disconnect(t.gamepad)})};Gamepad.prototype._getMapping=function(e){switch(e){case Gamepad.Type.PLAYSTATION:if(this.platform==Gamepad.Platform.FIREFOX){return Gamepad.Mapping.PLAYSTATION_FIREFOX}else if(this.platform==Gamepad.Platform.WEBKIT){return Gamepad.Mapping.PLAYSTATION_WEBKIT}else{return null}break;case Gamepad.Type.LOGITECH:if(this.platform==Gamepad.Platform.FIREFOX){return Gamepad.Mapping.LOGITECH_FIREFOX}else if(this.platform==Gamepad.Platform.WEBKIT){return Gamepad.Mapping.LOGITECH_WEBKIT}else{return null}break;case Gamepad.Type.XBOX:return Gamepad.Mapping.XBOX;break}return null};Gamepad.prototype._connect=function(e){e.type=this._resolveControllerType(e.id);if(e.type==Gamepad.Type.UNSUPPORTED){this._fire(Gamepad.Event.UNSUPPORTED,e);return false}e.mapping=this._getMapping(e.type);if(e.mapping==null){this._fire(Gamepad.Event.UNSUPPORTED,e);return false}e.prevState={};e.state={};var t;for(t in e.mapping.buttons){e.state[t]=0;e.prevState[t]=0}for(t in e.mapping.axes){e.state[t]=0;e.prevState[t]=0}this.gamepads[e.index]=e;this._fire(Gamepad.Event.CONNECTED,e);return true};Gamepad.prototype._disconnect=function(e){var t=[],n;if(typeof this.gamepads[e.index]!="undefined"){delete this.gamepads[e.index]}for(n=0;n<this.gamepads.length;n++){if(typeof this.gamepads[n]!="undefined"){t[n]=this.gamepads[n]}}this.gamepads=t;this._fire(Gamepad.Event.DISCONNECTED,e)};Gamepad.prototype._resolveControllerType=function(e){e=e.toLowerCase();if(e.indexOf("playstation")!=-1){return Gamepad.Type.PLAYSTATION}else if(e.indexOf("logitech")!=-1||e.indexOf("wireless gamepad")!=-1){return Gamepad.Type.LOGITECH}else if(e.indexOf("xbox")!=-1){return Gamepad.Type.XBOX}else{return Gamepad.Type.UNSUPPORTED}};Gamepad.prototype._update=function(){var e=this,t,n,r,i;switch(this.platform){case Gamepad.Platform.WEBKIT:this._updateWebkit();break;case Gamepad.Platform.FIREFOX:this._updateFirefox();break}for(i=0;i<this.gamepads.length;i++){if(typeof this.gamepads[i]=="undefined"){continue}for(t in this.gamepads[i].mapping.buttons){n=this.gamepads[i].mapping.buttons[t];this.gamepads[i].prevState[t]=this.gamepads[i].state[t];if(typeof n=="function"){this.gamepads[i].state[t]=n(this.gamepads[i],this)}else{r=this.gamepads[i].buttons[n];if(typeof this.gamepads[i].buttons[n]!="undefined"){this.gamepads[i].state[t]=r}}if(this.gamepads[i].prevState[t]!=this.gamepads[i].state[t]){if(this.gamepads[i].state[t]==1){this._fire(Gamepad.Event.BUTTON_DOWN,{gamepadIndex:i,control:t})}else{this._fire(Gamepad.Event.BUTTON_UP,{gamepadIndex:i,control:t})}}}for(t in this.gamepads[i].mapping.axes){n=this.gamepads[i].mapping.axes[t];if(typeof n=="function"){this.gamepads[i].state[t]=n(this.gamepads[i],this)}else{r=this._applyDeadzoneMaximize(this.gamepads[i].axes[n]);if(typeof this.gamepads[i].axes[n]!="undefined"){this.gamepads[i].state[t]=r}}}}if(this.gamepads.length>0){this._fire(Gamepad.Event.TICK,this.gamepads)}window.requestAnimationFrame(function(){e._update()})};Gamepad.prototype._updateWebkit=function(){var e;if(typeof navigator.webkitGamepads=="object"){e=navigator.webkitGamepads}else if(typeof navigator.webkitGetGamepads=="function"){e=navigator.webkitGetGamepads()}else{return}if(e.length!=this.gamepads.length){var t,n;for(n=0;n<e.length;n++){t=e[n];if(typeof t!="undefined"&&typeof this.gamepads[t.index]=="undefined"){this._connect(t)}}for(n=0;n<this.gamepads.length;n++){if(typeof this.gamepads[n]!="undefined"&&typeof e[n]=="undefined"){this._disconnect(this.gamepads[n])}}}};Gamepad.prototype._updateFirefox=function(){};Gamepad.prototype._applyDeadzoneMaximize=function(e,t,n){t=typeof t!="undefined"?t:this.deadzone;n=typeof n!="undefined"?n:this.maximizeThreshold;if(e>=0){if(e<t){e=0}else if(e>n){e=1}}else{if(e>-t){e=0}else if(e<-n){e=-1}}return e}
+(function(e){"use strict";var t=function(){this.gamepads=[];this.listeners={};this.platform=null;this.deadzone=.03;this.maximizeThreshold=.97};t.Platform={UNSUPPORTED:"unsupported",WEBKIT:"webkit",FIREFOX:"firefox"};t.Type={PLAYSTATION:"playstation",LOGITECH:"logitech",XBOX:"xbox",UNSUPPORTED:"unsupported"};t.Event={CONNECTED:"connected",DISCONNECTED:"disconnected",TICK:"tick",UNSUPPORTED:"unsupported",BUTTON_DOWN:"button-down",BUTTON_UP:"button-up",AXIS_CHANGED:"axis-changed"};t.Mapping={PLAYSTATION_FIREFOX:{buttons:{CROSS:14,CIRCLE:13,SQUARE:15,TRIANGLE:12,LB1:10,RB1:11,LEFT_STICK:1,RIGHT_STICK:2,START:3,SELECT:0,HOME:16,DPAD_UP:4,DPAD_DOWN:6,DPAD_LEFT:7,DPAD_RIGHT:5},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:2,RIGHT_STICK_Y:3}},PLAYSTATION_WEBKIT:{buttons:{CROSS:0,CIRCLE:1,SQUARE:2,TRIANGLE:3,LB1:4,RB1:5,LB2:6,RB2:7,LEFT_STICK:10,RIGHT_STICK:11,START:9,SELECT:8,HOME:16,DPAD_UP:12,DPAD_DOWN:13,DPAD_LEFT:14,DPAD_RIGHT:15},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:2,RIGHT_STICK_Y:3}},LOGITECH_FIREFOX:{buttons:{A:0,B:1,X:2,Y:3,LB:4,RB:5,LEFT_STICK:8,RIGHT_STICK:9,START:7,BACK:6,HOME:10,DPAD_UP:11,DPAD_DOWN:12,DPAD_LEFT:13,DPAD_RIGHT:14},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:3,RIGHT_STICK_Y:4,LEFT_TRIGGER:function(e,t){if(e.axes[2]>0){return t._applyDeadzoneMaximize(e.axes[2])}else{return 0}},RIGHT_TRIGGER:function(e,t){if(e.axes[2]<0){return t._applyDeadzoneMaximize(e.axes[2]*-1)}else{return 0}}}},LOGITECH_WEBKIT:{buttons:{A:1,B:2,X:0,Y:3,LB:4,RB:5,LEFT_TRIGGER:6,RIGHT_TRIGGER:7,LEFT_STICK:10,RIGHT_STICK:11,START:9,BACK:8,HOME:10,DPAD_UP:11,DPAD_DOWN:12,DPAD_LEFT:13,DPAD_RIGHT:14},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:2,RIGHT_STICK_Y:3}},XBOX:{buttons:{A:0,B:1,X:2,Y:3,LB:4,RB:5,LEFT_TRIGGER:6,RIGHT_TRIGGER:7,LEFT_STICK:10,RIGHT_STICK:11,START:9,BACK:8,DPAD_UP:12,DPAD_DOWN:13,DPAD_LEFT:14,DPAD_RIGHT:15},axes:{LEFT_STICK_X:0,LEFT_STICK_Y:1,RIGHT_STICK_X:2,RIGHT_STICK_Y:3}}};t.prototype.init=function(){this.platform=this._resolvePlatform();switch(this.platform){case t.Platform.WEBKIT:this._setupWebkit();break;case t.Platform.FIREFOX:this._setupFirefox();break;case t.Platform.UNSUPPORTED:return false}if(typeof e.requestAnimationFrame==="undefined"){e.requestAnimationFrame=e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame}this._update();return true};t.prototype.bind=function(e,t){if(typeof this.listeners[e]==="undefined"){this.listeners[e]=[]}this.listeners[e].push(t);return this};t.prototype.count=function(){return this.gamepads.length};t.prototype._fire=function(e,t){if(typeof this.listeners[e]==="undefined"){return}for(var n=0;n<this.listeners[e].length;n++){this.listeners[e][n].apply(this.listeners[e][n],[t])}};t.prototype._resolvePlatform=function(){if(typeof e.navigator.webkitGamepads!=="undefined"||typeof e.navigator.webkitGetGamepads!=="undefined"){return t.Platform.WEBKIT}else{return t.Platform.FIREFOX}};t.prototype._setupWebkit=function(){};t.prototype._setupFirefox=function(){var t=this;e.addEventListener("MozGamepadConnected",function(e){t._connect(e.gamepad)});e.addEventListener("MozGamepadDisconnected",function(e){t._disconnect(e.gamepad)})};t.prototype._getMapping=function(e){switch(e){case t.Type.PLAYSTATION:if(this.platform===t.Platform.FIREFOX){return t.Mapping.PLAYSTATION_FIREFOX}else if(this.platform===t.Platform.WEBKIT){return t.Mapping.PLAYSTATION_WEBKIT}else{return null}break;case t.Type.LOGITECH:if(this.platform===t.Platform.FIREFOX){return t.Mapping.LOGITECH_FIREFOX}else if(this.platform===t.Platform.WEBKIT){return t.Mapping.LOGITECH_WEBKIT}else{return null}break;case t.Type.XBOX:return t.Mapping.XBOX}return null};t.prototype._connect=function(e){e.type=this._resolveControllerType(e.id);if(e.type===t.Type.UNSUPPORTED){this._fire(t.Event.UNSUPPORTED,e);return false}e.mapping=this._getMapping(e.type);if(e.mapping===null){this._fire(t.Event.UNSUPPORTED,e);return false}e.lastState={};e.state={};var n,r;for(n in e.mapping.buttons){e.state[n]=0;e.lastState[n]=0}for(r in e.mapping.axes){e.state[r]=0;e.lastState[r]=0}this.gamepads[e.index]=e;this._fire(t.Event.CONNECTED,e);return true};t.prototype._disconnect=function(e){var n=[],r;if(typeof this.gamepads[e.index]!=="undefined"){delete this.gamepads[e.index]}for(r=0;r<this.gamepads.length;r++){if(typeof this.gamepads[r]!=="undefined"){n[r]=this.gamepads[r]}}this.gamepads=n;this._fire(t.Event.DISCONNECTED,e)};t.prototype._resolveControllerType=function(e){e=e.toLowerCase();if(e.indexOf("playstation")!==-1){return t.Type.PLAYSTATION}else if(e.indexOf("logitech")!==-1||e.indexOf("wireless gamepad")!==-1){return t.Type.LOGITECH}else if(e.indexOf("xbox")!==-1){return t.Type.XBOX}else{return t.Type.UNSUPPORTED}};t.prototype._update=function(){var n=this,r,i,s,o,u;switch(this.platform){case t.Platform.WEBKIT:this._updateWebkit();break;case t.Platform.FIREFOX:this._updateFirefox();break}for(u=0;u<this.gamepads.length;u++){if(typeof this.gamepads[u]==="undefined"){continue}for(r in this.gamepads[u].mapping.buttons){s=this.gamepads[u].mapping.buttons[r];if(typeof s==="function"){o=s(this.gamepads[u],this)}else{o=this.gamepads[u].buttons[s]}i=o>.5?1:0;this.gamepads[u].state[r]=o;if(i!==this.gamepads[u].lastState[r]){if(o>.5){this._fire(t.Event.BUTTON_DOWN,{gamepad:this.gamepads[u],mapping:s,control:r})}else if(o<.5){this._fire(t.Event.BUTTON_UP,{gamepad:this.gamepads[u],mapping:s,control:r})}if(o!==0&&o!==1){this._fire(t.Event.AXIS_CHANGED,{gamepad:this.gamepads[u],mapping:s,axis:r,value:o})}}this.gamepads[u].lastState[r]=i}for(r in this.gamepads[u].mapping.axes){s=this.gamepads[u].mapping.axes[r];if(typeof s==="function"){o=s(this.gamepads[u],this)}else{o=this._applyDeadzoneMaximize(this.gamepads[u].axes[s])}this.gamepads[u].state[r]=o;if(o!==this.gamepads[u].lastState[r]){this._fire(t.Event.AXIS_CHANGED,{gamepad:this.gamepads[u],mapping:s,axis:r,value:o})}this.gamepads[u].lastState[r]=o}}if(this.gamepads.length>0){this._fire(t.Event.TICK,this.gamepads)}e.requestAnimationFrame(function(){n._update()})};t.prototype._updateWebkit=function(){var t;if(typeof e.navigator.webkitGamepads==="object"){t=e.navigator.webkitGamepads}else if(typeof e.navigator.webkitGetGamepads==="function"){t=e.navigator.webkitGetGamepads()}else{return}if(t.length!==this.gamepads.length){var n,r;for(r=0;r<t.length;r++){n=t[r];if(typeof n!=="undefined"&&typeof this.gamepads[n.index]==="undefined"){this._connect(n)}}for(r=0;r<this.gamepads.length;r++){if(typeof this.gamepads[r]!=="undefined"&&typeof t[r]==="undefined"){this._disconnect(this.gamepads[r])}}}};t.prototype._updateFirefox=function(){};t.prototype._applyDeadzoneMaximize=function(e,t,n){t=typeof t!=="undefined"?t:this.deadzone;n=typeof n!=="undefined"?n:this.maximizeThreshold;if(e>=0){if(e<t){e=0}else if(e>n){e=1}}else{if(e>-t){e=0}else if(e<-n){e=-1}}return e};e.Gamepad=t})(window)
View
25 index.html
@@ -21,13 +21,16 @@
var mainWrap = $('#gamepad-' + device.index),
statesWrap,
+ logWrap,
control,
value;
- mainWrap.append('<ul id="states-' + device.index + '"></ul>');
-
+ mainWrap.append('<strong>State</strong><ul id="states-' + device.index + '"></ul>');
+ mainWrap.append('<strong>Events</strong><ul id="log-' + device.index + '"></ul>');
+
statesWrap = $('#states-' + device.index)
-
+ logWrap = $('#log-' + device.index)
+
for (control in device.state) {
value = device.state[control];
@@ -62,20 +65,22 @@
for (control in gamepads[i].state) {
value = gamepads[i].state[control];
-
-
-
+
$('#state-' + gamepads[i].index + '-' + control + '').html(value);
}
}
});
- gamepad.bind(Gamepad.Event.BUTTON_DOWN, function(data) {
- console.log('pad index:' + data.gamepadIndex + ' button:' + data.control + ' down');
+ gamepad.bind(Gamepad.Event.BUTTON_DOWN, function(e) {
+ $('#log-' + e.gamepad.index).append('<li>' + e.control + ' [' + e.mapping + '] down</li>');
});
- gamepad.bind(Gamepad.Event.BUTTON_UP, function(data) {
- console.log('pad index:' + data.gamepadIndex + ' button:' + data.control + ' up');
+ gamepad.bind(Gamepad.Event.BUTTON_UP, function(e) {
+ $('#log-' + e.gamepad.index).append('<li>' + e.control + ' [' + e.mapping + '] up</li>');
+ });
+
+ gamepad.bind(Gamepad.Event.AXIS_CHANGED, function(e) {
+ $('#log-' + e.gamepad.index).append('<li>' + e.axis + ' [' + e.mapping + '] changed to ' + e.value + '</li>');
});
if (!gamepad.init()) {

0 comments on commit ab19632

Please sign in to comment.
Something went wrong with that request. Please try again.