@mesmotronic/xpad
Version:
Simplified Gamepad API for Xbox 360 and Xbox One controllers
109 lines (108 loc) • 3.94 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Xpad_state;
import { EventDispatcher } from "conbine";
import { XpadEvent } from "../events/XpadEvent";
import { XpadState } from "../models/XpadState";
import { computeXpadState } from "../utils/computeXpadState";
export class Xpad extends EventDispatcher {
constructor(index = 0) {
super();
Object.defineProperty(this, "index", {
enumerable: true,
configurable: true,
writable: true,
value: index
});
Object.defineProperty(this, "inputThreshold", {
enumerable: true,
configurable: true,
writable: true,
value: 0.15
});
_Xpad_state.set(this, new XpadState());
/**
* Updates the state of the Xpad
*/
Object.defineProperty(this, "update", {
enumerable: true,
configurable: true,
writable: true,
value: () => {
const { gamepad } = this;
if (gamepad) {
computeXpadState(gamepad, this.state, this.inputThreshold);
}
}
});
// Internal
Object.defineProperty(this, "redispatchEvent", {
enumerable: true,
configurable: true,
writable: true,
value: (event) => {
this.dispatchEvent(event);
}
});
Object.defineProperty(this, "connectedHandler", {
enumerable: true,
configurable: true,
writable: true,
value: (event) => {
if (event.gamepad.index !== this.index)
return;
this.update();
this.dispatchEvent(new XpadEvent(XpadEvent.CONNECT, this.index));
}
});
Object.defineProperty(this, "disconnectedHandler", {
enumerable: true,
configurable: true,
writable: true,
value: (event) => {
if (event.gamepad.index !== this.index)
return;
this.reset();
this.dispatchEvent(new XpadEvent(XpadEvent.DISCONNECT, this.index));
}
});
window.addEventListener('gamepadconnected', this.connectedHandler);
window.addEventListener('gamepaddisconnected', this.disconnectedHandler);
this.state
.addEventListener(XpadEvent.BUTTON_UP, this.redispatchEvent)
.addEventListener(XpadEvent.BUTTON_DOWN, this.redispatchEvent)
.addEventListener(XpadEvent.BUTTON_CHANGE, this.redispatchEvent)
.addEventListener(XpadEvent.STICK_ACTIVE, this.redispatchEvent)
.addEventListener(XpadEvent.STICK_INACTIVE, this.redispatchEvent)
.addEventListener(XpadEvent.STICK_CHANGE, this.redispatchEvent);
}
/**
* Returns true if the Xpad is connected
*/
get connected() {
return !!this.gamepad;
}
/**
* Returns the Gamepad instance for this Xpad
*/
get gamepad() {
const gamepads = navigator.getGamepads();
return gamepads[this.index] ?? null;
}
/**
* Returns the current state of the Xpad
*/
get state() {
return __classPrivateFieldGet(this, _Xpad_state, "f");
}
/**
* Resets the state of the Xpad
*/
reset() {
this.state.reset();
}
}
_Xpad_state = new WeakMap();