Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
joelfillmore committed May 8, 2015
1 parent dc7b5fa commit e91d4f6
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Set default behaviour, in case users don't have core.autocrlf set.
* text=auto

# Explicitly declare text files we want to always be normalized and converted
# to native line endings on checkout.
*.js text
*.less text
*.html text

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# OS junk files
[Tt]humbs.db
*.DS_Store

# Sublime files
*.sublime-*

# WebStorm files
.idea

# NodeJS modules
node_modules

# Bower components
bower_components

# Project files
[Bb]uild/
184 changes: 184 additions & 0 deletions PxGamepad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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) {

// 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++) {
if (gamepads[i].mapping === 'standard') {
gp = gamepads[i];
break;
}
}
}
}

return gp;
};

// 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(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;

})();
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# PxGamepad
HTML5 gamepad helper for standard mapped controllers

Details:
[http://flightarcade.com/learn/gamepad](http://flightarcade.com/learn/gamepad)

33 changes: 33 additions & 0 deletions gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*global module:false*/
module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jshint: {
files: [ '*.js' ],
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
browser: true,
globals: {
jQuery: true,
console: true
}
}
}
});

grunt.loadNpmTasks('grunt-contrib-jshint');

grunt.registerTask('default', [ 'jshint' ]);

};
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "pxgamepad",
"version": "1.0.0",
"title": "PxGamepad",
"homepage": "http://github.com/thinkpixellab/PxGamepad",
"author": {
"name": "Pixel Lab",
"url": "http://thinkpixellab.com"
},
"repository": {
"type": "git",
"url": "http://github.com/thinkpixellab/pxgamepad.git"
},
"dependencies": {
"grunt": "~0.4.5",
"grunt-contrib-jshint": "~0.11.2"
},
"devDependencies": {},
"keywords": []
}

0 comments on commit e91d4f6

Please sign in to comment.