Skip to content

Commit

Permalink
Merge pull request #12 from alvaromontoro/feature/create-after-events
Browse files Browse the repository at this point in the history
Feature/create after events
Solves issues #1 and #2
  • Loading branch information
alvaromontoro committed Sep 2, 2019
2 parents 8c74bc4 + 528f643 commit 3cf0ac9
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 91 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Expand Up @@ -2,6 +2,14 @@

All notable changes to this project will be documented in this file.

## [v1.4.0] - 2019-09-02

### Added

- After event for buttons and axe/joysticks
- Before event for buttons and axe/joysticks
- Refactor code to make library 20% smaller

## [v1.3.0] - 2019-09-01

### Added
Expand Down
22 changes: 17 additions & 5 deletions dist/gamecontroller.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gamecontroller.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "gamecontroller.js",
"version": "1.3.0",
"version": "1.4.0",
"description": "A JavaScript library that lets you handle, configure, and use gamepad and controllers on a browser, using the Gamepad API",
"main": "dist/index.js",
"scripts": {
Expand Down
Binary file added public/gamecontrollerjs-event-flow.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/constants.js
@@ -0,0 +1,11 @@
const MESSAGES = {
ON: 'Gamepad detected.',
OFF: 'Gamepad disconnected.',
INVALID_PROPERTY: 'Invalid property.',
INVALID_VALUE_NUMBER: 'Invalid value. It must be a number between 0.00 and 1.00.',
INVALID_BUTTON: 'Button that does not exist.',
UNKNOWN_EVENT: 'Unknown event name.',
NO_SUPPORT: 'Your web browser does not support the Gamepad API.'
};

export { MESSAGES };
15 changes: 8 additions & 7 deletions src/gamecontrol.js
@@ -1,4 +1,5 @@
import { log, isGamepadSupported } from './tools';
import { log, error, isGamepadSupported } from './tools';
import { MESSAGES } from './constants';
import gamepad from './gamepad';

const gameControl = {
Expand All @@ -22,7 +23,7 @@ const gameControl = {
const properties = ['axeThreshold'];
if (properties.indexOf(property) >= 0) {
if (property === 'axeThreshold' && (!parseFloat(value) || value < 0.0 || value > 1.0)) {
log(`Invalid axeThreshold. The value must be a number between 0.00 and 1.00.`, 'error');
error(MESSAGES.INVALID_VALUE_NUMBER);
return;
}

Expand All @@ -36,7 +37,7 @@ const gameControl = {
}
}
} else {
log(`Invalid property (${property})`, 'error');
error(MESSAGES.INVALID_PROPERTY);
}
},
checkStatus: function() {
Expand All @@ -58,7 +59,7 @@ const gameControl = {
},
init: function() {
window.addEventListener('gamepadconnected', e => {
log('Gamepad detected.');
log(MESSAGES.ON);
if (!window.gamepads) window.gamepads = {};
if (!window.gamepads[e.gamepad.index]) {
window.gamepads[e.gamepad.index] = e.gamepad;
Expand All @@ -70,7 +71,7 @@ const gameControl = {
if (Object.keys(this.gamepads).length === 1) this.checkStatus();
});
window.addEventListener('gamepaddisconnected', e => {
log('Gamepad disconnected.');
log(MESSAGES.OFF);
delete window.gamepads[e.gamepad.index];
delete this.gamepads[e.gamepad.index];
this.onDisconnect(e.gamepad.index);
Expand All @@ -93,7 +94,7 @@ const gameControl = {
this.onAfterCycle = callback;
break;
default:
log('Unknown event name', 'error');
error(MESSAGES.UNKNOWN_EVENT);
break;
}
return this;
Expand All @@ -115,7 +116,7 @@ const gameControl = {
this.onAfterCycle = function() {};
break;
default:
log('Unknown event name', 'error');
error(MESSAGES.UNKNOWN_EVENT);
break;
}
return this;
Expand Down

0 comments on commit 3cf0ac9

Please sign in to comment.