@mesmotronic/xpad
Version:
Simplified Gamepad API for Xbox 360 and Xbox One controllers
112 lines (111 loc) • 4.87 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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _XpadState_buttons;
import { EventDispatcher } from "conbine";
import { XpadStick } from "./XpadStick";
import { XpadEvent } from "../events/XpadEvent";
export class XpadState extends EventDispatcher {
constructor() {
super();
Object.defineProperty(this, "leftStick", {
enumerable: true,
configurable: true,
writable: true,
value: new XpadStick(0, 0, 0)
});
Object.defineProperty(this, "rightStick", {
enumerable: true,
configurable: true,
writable: true,
value: new XpadStick(0, 0, 1)
});
Object.defineProperty(this, "dpad", {
enumerable: true,
configurable: true,
writable: true,
value: new XpadStick(0, 0, 2)
});
/**
* Enable or disable D-pad input as part of the anyStick calculation
*/
Object.defineProperty(this, "dpadEnabled", {
enumerable: true,
configurable: true,
writable: true,
value: true
});
_XpadState_buttons.set(this, Array(16).fill(0));
// Internal
Object.defineProperty(this, "redispatchEvent", {
enumerable: true,
configurable: true,
writable: true,
value: (event) => {
this.dispatchEvent(event);
}
});
this.sticks.forEach((stick) => {
stick.addEventListener(XpadEvent.STICK_ACTIVE, this.redispatchEvent);
stick.addEventListener(XpadEvent.STICK_INACTIVE, this.redispatchEvent);
stick.addEventListener(XpadEvent.STICK_CHANGE, this.redispatchEvent);
});
}
get buttons() {
return __classPrivateFieldGet(this, _XpadState_buttons, "f");
}
set buttons(value) {
const buttonChanges = value.map((value, index) => value !== __classPrivateFieldGet(this, _XpadState_buttons, "f")[index]);
buttonChanges.forEach((changed, index) => {
if (changed) {
const newValue = value[index];
if (newValue !== 0 && __classPrivateFieldGet(this, _XpadState_buttons, "f")[index] === 0) {
this.dispatchEvent(new XpadEvent(XpadEvent.BUTTON_DOWN, index, newValue));
}
else if (newValue === 0 && __classPrivateFieldGet(this, _XpadState_buttons, "f")[index] !== 0) {
this.dispatchEvent(new XpadEvent(XpadEvent.BUTTON_UP, index, newValue));
}
this.dispatchEvent(new XpadEvent(XpadEvent.BUTTON_CHANGE, index, newValue));
}
});
__classPrivateFieldSet(this, _XpadState_buttons, value, "f");
}
get sticks() {
return [this.leftStick, this.rightStick, this.dpad];
}
/**
* Get the current state of the left stick, right stick or D-pad
*/
get anyStick() {
return new XpadStick(this.leftStick.x || this.rightStick.x || (this.dpadEnabled && this.dpad.x) || 0, this.leftStick.y || this.rightStick.y || (this.dpadEnabled && this.dpad.y) || 0);
}
/**
* @deprecated Use anyStick instead
*/
get anyAxes() {
console.warn("XpadState.anyAxes is deprecated, use anyStick instead");
return this.anyStick;
}
/**
* Check if any button is pressed (excludes D-pad and stick buttons)
*/
get anyButton() {
// Buttons: A (0), B (1), X (2), Y (3), LB (4), RB (5), LT (6), RT (7)
return Math.max(...this.buttons.slice(0, 8));
}
reset() {
this.leftStick = new XpadStick();
this.rightStick = new XpadStick();
this.dpad = new XpadStick();
this.buttons.fill(0);
}
}
_XpadState_buttons = new WeakMap();