gamepad-helper
Version:
Wrapper class for the HTML5 Gamepad API.
70 lines • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GamepadHelper = void 0;
class GamepadHelper {
static gamepads = {};
static gamepadsLastUpdate;
static logOutput = false;
/** Return the state of the specified button */
static getButtonValue(gamepad, button) {
return GamepadHelper.gamepads[gamepad]?.buttons[button]?.value;
}
/** Return the state of the specified axis */
static getAxisValue(gamepad, axis) {
return GamepadHelper.gamepads[gamepad]?.axes[axis];
}
/** Main method intended to be called from within a game loop.
* Queries the state of each button on each connected gamepad and dispatches an event to the document if there is change in state.
*/
static update() {
GamepadHelper.checkForNewGamepads();
// skip if there are no gamepads connected
if (!GamepadHelper.gamepads || Object.keys(GamepadHelper.gamepads).length < 1)
return;
if (GamepadHelper.gamepadsLastUpdate &&
Object.keys(GamepadHelper.gamepadsLastUpdate).length > 0) {
Object.values(GamepadHelper.gamepads).forEach((gamepad) => {
// check for changes in button state
for (let i = 0; i < gamepad.buttons.length; i++) {
const buttonLastUpdate = GamepadHelper.gamepadsLastUpdate[gamepad.index]?.buttons[i];
const button = gamepad.buttons[i];
if (button?.value == 1 && buttonLastUpdate?.value == 0) {
GamepadHelper.dispatchButtonDownEvent(gamepad.index, i);
}
else if (button?.value == 0 && buttonLastUpdate?.value == 1) {
GamepadHelper.dispatchButtonUpEvent(gamepad.index, i);
}
}
});
}
// copy to a separate object in order to compare fields next update
GamepadHelper.gamepadsLastUpdate = Object.assign({}, GamepadHelper.gamepads);
}
static checkForNewGamepads() {
// @ts-ignore
const gamepads = navigator.getGamepads();
for (const gamepad of gamepads) {
if (gamepad) {
if (!GamepadHelper.gamepads[gamepad.index]) {
GamepadHelper.logOutput &&
console.log("gamepad " + gamepad.index + " connected");
}
GamepadHelper.gamepads[gamepad.index] = gamepad;
}
}
}
static dispatchButtonDownEvent(gamepad, button) {
GamepadHelper.logOutput && console.log(`gamepad ${gamepad}: button ${button} is down`);
document.dispatchEvent(new CustomEvent("gamepadbuttondown", {
detail: { gamepad, button }
}));
}
static dispatchButtonUpEvent(gamepad, button) {
GamepadHelper.logOutput && console.log(`gamepad ${gamepad}: button ${button} is up`);
document.dispatchEvent(new CustomEvent("gamepadbuttonup", {
detail: { gamepad, button }
}));
}
}
exports.GamepadHelper = GamepadHelper;
//# sourceMappingURL=index.js.map