class GamepadHandler { gamepads; timeout; listeners; constructor() { this.gamepads = []; this.listeners = {}; this.timeout = null; this.loop(); } terminate() { window.clearTimeout(this.timeout); } getGamepads() { return navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []); } loop() { this.updateGamepadState(); this.timeout = setTimeout(this.loop.bind(this), 10); } updateGamepadState() { const gamepads = this.getGamepads(); gamepads.forEach((gamepad, index) => { if (!gamepad) return; let hasGamepad = false; this.gamepads.forEach((oldGamepad, oldIndex) => { if (oldGamepad.index !== gamepad.index) return; const gamepadToSave = { axes: [], buttons: {}, index: oldGamepad.index, id: oldGamepad.id } hasGamepad = true; oldGamepad.axes.forEach((axis, axisIndex) => { const val = (axis < 0.01 && axis > -0.01) ? 0 : axis; const newVal = (gamepad.axes[axisIndex] < 0.01 && gamepad.axes[axisIndex] > -0.01) ? 0 : gamepad.axes[axisIndex]; if (newVal !== val) { const axis = ['LEFT_STICK_X', 'LEFT_STICK_Y', 'RIGHT_STICK_X', 'RIGHT_STICK_Y'][axisIndex]; if (!axis) return; this.dispatchEvent('axischanged', {axis: axis, value: newVal, index: gamepad.index, gamepadIndex: gamepad.index}); } gamepadToSave.axes[axisIndex] = newVal; }) gamepad.buttons.forEach((button, buttonIndex) => { let pressed = oldGamepad.buttons[buttonIndex] === 1.0; if (typeof oldGamepad.buttons[buttonIndex] === "object") { pressed = oldGamepad.buttons[buttonIndex].pressed; } let pressed2 = button === 1.0; if (typeof button === "object") { pressed2 = button.pressed; } gamepadToSave.buttons[buttonIndex] = {pressed:pressed2}; if (pressed !== pressed2) { if (pressed2) { this.dispatchEvent('buttondown', {index: buttonIndex, gamepadIndex: gamepad.index}); } else { this.dispatchEvent('buttonup', {index: buttonIndex, gamepadIndex: gamepad.index}); } } }) this.gamepads[oldIndex] = gamepadToSave; }) if (!hasGamepad) { this.gamepads.push(gamepads[index]); this.dispatchEvent('connected', {gamepadIndex: gamepad.index}); } }); for (let j=0; j