Skip to content

Commit

Permalink
Merge pull request #3 from Tom32i/v1
Browse files Browse the repository at this point in the history
V1
  • Loading branch information
Tom32i authored Jul 21, 2020
2 parents f64aaa9 + edd12f6 commit bac1f15
Show file tree
Hide file tree
Showing 19 changed files with 6,145 additions and 1,077 deletions.
35 changes: 35 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"rules": {
"indent": [
"error",
4,
{ "SwitchCase": 1 }
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
],
"no-console": [
"error",
{ "allow": ["info", "error"] }
]
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules/*
bower_components/*
gamepad.js
33 changes: 0 additions & 33 deletions .jshintrc

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.SILENT:
.PHONY: test

# Install dependencies
install:
npm install

# Launch watch
watch:
npx webpack --watch --mode=development

# Build lib
build:
npx webpack --mode=production

# Launch demo client
demo:
php -S 0.0.0.0:8000 -t .

# Lint and code style fix
lint:
npx eslint src/* --ext .js,.json --fix

# Test
test: build
npx mocha

# Publish package
publish: build
npm publish . --access public
100 changes: 62 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
gamepad.js
================

Simple HTML5 Gamepad API handler
A simple HTML5 Gamepad handler that provides keyboard-like events for Gamepad sticks and button.

Demo here: [http://tom32i.github.io/gamepad.js/](http://tom32i.github.io/gamepad.js/)
Try it right now in your browser: [http://tom32i.github.io/gamepad.js/](http://tom32i.github.io/gamepad.js/)

## Installation:

npm i gamepad.js
npm install gamepad.js

## Import

HTML:

```html
<script src="gamepad.js"></script>
<script>const { GamepadListener } = gamepad;</script>
```

ES6:

```javascript
import { GamepadListener } from 'gamepad.js';
```

Node:

```javascript
const { GamepadListener } = require('gamepad.js');
```

## Usage:

```javascript
var listener = new GamepadListener();
const listener = new GamepadListener(/* options*/);
```

## Options:
### Configuration:

__analog:__ (boolean: default true)
Set to false to get fixed value: ex for a stick 0/1/-1. Used to reduce the number of change event triggered if you dont need analog values.

__precision:__ (int: default 0 (no rounding))
__precision:__ (integer: default 0 (no rounding))
When in analog mode, set the number of number you want after decimal. Used to reduce the muber of event triggered but keep analog values.

__deadZone:__ (float: from 0 to 1)
Expand All @@ -31,7 +52,7 @@ Stick moves below 30% from default positon won't trigger a change.
Theses options can be set for the whole gamepad:

```javascript
var listener = new GamepadListener({
const listener = new GamepadListener({
analog: false,
deadZone: 0.3
});
Expand All @@ -40,7 +61,7 @@ var listener = new GamepadListener({
Or distinctly for sticks and buttons:

```javascript
var listener = new GamepadListener({
const listener = new GamepadListener({
button: {
analog: false
},
Expand All @@ -51,73 +72,76 @@ var listener = new GamepadListener({
});
```

## Events:
### Events:

* 'gamepad:connected': When a new gamepad is connected.
* 'gamepad:disconnected': When a gamepad is disconnected.
* 'gamepad:axis': When a gamepad axis changes.
* 'gamepad:{gamepad}:axis': When a specific gamepad axis changes, '{gamepad}' being the numeric index.
* 'gamepad:{gamepad}:axis:{axis}': When a specific axis on a specific gamepad changes, '{axis}' being the numeric index of the axis.
* 'gamepad:button': When a gamepad button changes.
* 'gamepad:{gamepad}:button': When a specific gamepad button changes, '{gamepad}' being the numeric index.
* 'gamepad:{gamepad}:button:{button}': When a specific button on a specific gamepad changes, '{button}' being the numeric index of the button.
* `gamepad:connected: When a new gamepad is connected.
* `gamepad:disconnected: When a gamepad is disconnected.
* `gamepad:axis: When a gamepad axis changes.
* `gamepad:{gamepad}:axis: When a specific gamepad axis changes, '{gamepad}' being the numeric index.
* `gamepad:{gamepad}:axis:{axis}: When a specific axis on a specific gamepad changes, '{axis}' being the numeric index of the axis.
* `gamepad:button: When a gamepad button changes.
* `gamepad:{gamepad}:button: When a specific gamepad button changes, '{gamepad}' being the numeric index.
* `gamepad:{gamepad}:button:{button}: When a specific button on a specific gamepad changes, '{button}' being the numeric index of the button.

__Listen for value change on gampads:__

```javascript

listener.on('gamepad:connected', function (event) {
/**
* event: CustomEvent
* event:
* detail: {
* gamepad: Gamepad,
* index: 0
* index: 0, // Gamepad index [0-3]
* gamepad, // Native Gamepad object
* }
*/
});

listener.on('gamepad:disconnected', function (event) {
/**
* event: CustomEvent
* event:
* detail: {
* index: 0
* index: 0,
* // Native Gamepad object is no longer available
* }
*/
});

listener.on('gamepad:axis', function (event) {
/**
* event: CustomEvent
* event:
* detail: {
* axis: 1,
* gamepad: Gamepad,
* value: -0.34
* index: 0, // Gamepad index [0-3]
* stick: 0, // Stick index [0-N]
* axis: 1, // Axis index [0-1]
* value: -0.34, // Value (float if analog, otherise integer)
* gamepad, // Native Gamepad object
* }
*/
});

listener.on('gamepad:0:button', function (event) {
/**
* event: CustomEvent
* event:
* detail: {
* gamepad: Gamepad,
* button: GamepadButton,
* pressed: true,
* index: 7,
* value: 0.56
* index: 2, // Gamepad index [0-3]
* button: 4, // Button index [0-N]
* value: 0.56, // Value (float if analog, otherise integer)
* pressed: true, // Boolean
* gamepad, // Native Gamepad object
* }
*/
});

listener.on('gamepad:0:button:5', function (event) {
/**
* event: CustomEvent
* event:
* detail: {
* gamepad: Gamepad,
* button: GamepadButton,
* pressed: true,
* index: 5,
* value: 1
* index: 3, // Gamepad index [0-3]
* button: 8, // Button index [0-N]
* value: 1, // Value (float if analog, otherise integer)
* pressed: true, // Boolean
* gamepad, // Native Gamepad object
* }
*/
});
Expand Down
29 changes: 0 additions & 29 deletions bower.json

This file was deleted.

Loading

0 comments on commit bac1f15

Please sign in to comment.