Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Typescript #31

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"env": {
"test": {
"presets": [["@babel/preset-env"]]
}
}
"presets": ["@babel/preset-env", "@babel/preset-typescript"],
"plugins": ["@babel/plugin-transform-typescript"]
}
25 changes: 25 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: "build-test"
on:
pull_request:
push:
branches:
- "*"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Install dependencies
run: |
npm install
- name: Build packages
run: |
npm run build
- name: Test
run: |
npm run test
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.github
.github/*
!.github/workflows
node_modules
coverage
coverage
246 changes: 246 additions & 0 deletions dist/browser/gamecontroller.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/browser/gamecontroller.min.js

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

32 changes: 32 additions & 0 deletions dist/cjs/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MESSAGES = exports.isGamepadSupported = exports.gameControl = void 0;
// This file is the entry point for browsers, this file set's the global window.gameControl object
__exportStar(require("./types"), exports);
const tools_1 = require("./tools");
Object.defineProperty(exports, "isGamepadSupported", { enumerable: true, get: function () { return tools_1.isGamepadSupported; } });
const constants_1 = require("./constants");
Object.defineProperty(exports, "MESSAGES", { enumerable: true, get: function () { return constants_1.MESSAGES; } });
const gamecontrol_1 = require("./gamecontrol");
exports.gameControl = gamecontrol_1.default;
if ((0, tools_1.isGamepadSupported)()) {
window.gameControl = gamecontrol_1.default;
}
else {
(0, tools_1.error)(constants_1.MESSAGES.NO_SUPPORT);
}
exports.default = gamecontrol_1.default;
13 changes: 13 additions & 0 deletions dist/cjs/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MESSAGES = void 0;
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: (name) => `Button "${name}" does not exist.`,
UNKNOWN_EVENT: 'Unknown event name.',
NO_SUPPORT: 'Your web browser does not support the Gamepad API.'
};
exports.MESSAGES = MESSAGES;
129 changes: 129 additions & 0 deletions dist/cjs/gamecontrol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tools_1 = require("./tools");
const constants_1 = require("./constants");
const gamepad_1 = require("./gamepad");
const gameControl = {
gamepads: {},
axeThreshold: [1.0],
isReady: (0, tools_1.isGamepadSupported)(),
onConnect: function (_gamepad) { },
onDisconnect: function (_index) { },
onBeforeCycle: function () { },
onAfterCycle: function () { },
getGamepads: function () {
return this.gamepads;
},
getGamepad: function (id) {
if (this.gamepads[id]) {
return this.gamepads[id];
}
return null;
},
set: function (property, value) {
const properties = ['axeThreshold'];
if (properties.indexOf(property) >= 0) {
if (property === 'axeThreshold' && (!parseFloat(value) || value < 0.0 || value > 1.0)) {
(0, tools_1.error)(constants_1.MESSAGES.INVALID_VALUE_NUMBER);
return;
}
this[property] = value;
if (property === 'axeThreshold') {
const gps = this.getGamepads();
const ids = Object.keys(gps);
for (let x = 0; x < ids.length; x++) {
gps[ids[x]].set('axeThreshold', this.axeThreshold);
}
}
}
else {
(0, tools_1.error)(constants_1.MESSAGES.INVALID_PROPERTY);
}
},
checkStatus: function () {
const requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame;
const gamepadIds = Object.keys(gameControl.gamepads);
gameControl.onBeforeCycle();
for (let x = 0; x < gamepadIds.length; x++) {
gameControl.gamepads[gamepadIds[x]].checkStatus();
}
gameControl.onAfterCycle();
if (gamepadIds.length > 0) {
requestAnimationFrame(gameControl.checkStatus);
}
},
init: function () {
window.addEventListener('gamepadconnected', e => {
const egp = e.gamepad || e.detail.gamepad;
(0, tools_1.log)(constants_1.MESSAGES.ON);
if (!window.gamepads)
window.gamepads = {};
if (egp) {
if (!window.gamepads[egp.index]) {
window.gamepads[egp.index] = egp;
const gp = gamepad_1.default.init(egp);
gp.set('axeThreshold', this.axeThreshold);
this.gamepads[gp.id] = gp;
this.onConnect(this.gamepads[gp.id]);
}
if (Object.keys(this.gamepads).length === 1)
this.checkStatus();
}
});
window.addEventListener('gamepaddisconnected', e => {
const egp = e.gamepad || e.detail.gamepad;
(0, tools_1.log)(constants_1.MESSAGES.OFF);
if (egp) {
delete window.gamepads[egp.index];
delete this.gamepads[egp.index];
this.onDisconnect(egp.index);
}
});
},
on: function (eventName, callback) {
switch (eventName) {
case 'connect':
this.onConnect = callback;
break;
case 'disconnect':
this.onDisconnect = callback;
break;
case 'beforeCycle':
case 'beforecycle':
this.onBeforeCycle = callback;
break;
case 'afterCycle':
case 'aftercycle':
this.onAfterCycle = callback;
break;
default:
(0, tools_1.error)(constants_1.MESSAGES.UNKNOWN_EVENT);
break;
}
return this;
},
off: function (eventName) {
switch (eventName) {
case 'connect':
this.onConnect = function (_gamepad) { };
break;
case 'disconnect':
this.onDisconnect = function (_index) { };
break;
case 'beforeCycle':
case 'beforecycle':
this.onBeforeCycle = function () { };
break;
case 'afterCycle':
case 'aftercycle':
this.onAfterCycle = function () { };
break;
default:
(0, tools_1.error)(constants_1.MESSAGES.UNKNOWN_EVENT);
break;
}
return this;
}
};
gameControl.init();
exports.default = gameControl;
Loading