From 53bbdfcefdac819d5ae6783b33950474da0e0fc4 Mon Sep 17 00:00:00 2001 From: GarboMuffin Date: Sun, 8 Jan 2023 21:25:22 -0600 Subject: [PATCH] Update gamepad addon with upstream changes These changes were, again, written by me so GPL need not apply. --- src/addons/gamepad/gamepadlib.js | 50 +++++++++++++++++--------------- src/addons/gamepad/userscript.js | 2 +- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/addons/gamepad/gamepadlib.js b/src/addons/gamepad/gamepadlib.js index b509c2c..35314f6 100644 --- a/src/addons/gamepad/gamepadlib.js +++ b/src/addons/gamepad/gamepadlib.js @@ -137,6 +137,8 @@ const padWithEmptyMappings = (array, length) => { return array; }; +const createEmptyMappingList = (length) => padWithEmptyMappings([], length); + const getMovementConfiguration = (usedKeys) => ({ usesArrows: usedKeys.has("ArrowUp") || usedKeys.has("ArrowDown") || usedKeys.has("ArrowRight") || usedKeys.has("ArrowLeft"), @@ -157,16 +159,22 @@ class GamepadData { } resetMappings() { + this.hints = this.gamepadLib.getHints(); this.buttonMappings = this.getDefaultButtonMappings().map(transformAndCopyMapping); this.axesMappings = this.getDefaultAxisMappings().map(transformAndCopyMapping); } + clearMappings() { + this.buttonMappings = createEmptyMappingList(this.gamepad.buttons.length); + this.axesMappings = createEmptyMappingList(this.gamepad.axes.length); + } + getDefaultButtonMappings() { let buttons; - if (this.gamepadLib.hints.importedSettings) { - buttons = this.gamepadLib.hints.importedSettings.buttons; + if (this.hints.importedSettings) { + buttons = this.hints.importedSettings.buttons; } else { - const usedKeys = this.gamepadLib.hints.usedKeys; + const usedKeys = this.hints.usedKeys; const alreadyUsedKeys = new Set(); const { usesArrows, usesWASD } = getMovementConfiguration(usedKeys); if (usesWASD) { @@ -358,14 +366,14 @@ class GamepadData { getDefaultAxisMappings() { let axes = []; - if (this.gamepadLib.hints.importedSettings) { - axes = this.gamepadLib.hints.importedSettings.axes; + if (this.hints.importedSettings) { + axes = this.hints.importedSettings.axes; } else { // Only return default axis mappings when there are 4 axes, like an xbox controller // If there isn't exactly 4, we can't really predict what the axes mean // Some controllers map the dpad to *both* buttons and axes at the same time, which would cause conflicts. if (this.gamepad.axes.length === 4) { - const usedKeys = this.gamepadLib.hints.usedKeys; + const usedKeys = this.hints.usedKeys; const { usesArrows, usesWASD } = getMovementConfiguration(usedKeys); if (usesWASD) { axes.push(defaultAxesMappings.wasd[0]); @@ -420,8 +428,6 @@ class GamepadLib extends EventTarget { this.connectCallbacks = []; - this.hints = defaultHints(); - this.keysPressedThisFrame = new Set(); this.oldKeysPressed = new Set(); @@ -450,26 +456,28 @@ class GamepadLib extends EventTarget { }); } - ensureHintsGenerated() { - if (this.hints.generated) { - return; - } - if (this.getHintsLazily) { - Object.assign(this.hints, this.getHintsLazily()); - } - this.hints.generated = true; + getHints() { + return Object.assign(defaultHints(), this.getUserHints()); + } + + getUserHints() { + // to be overridden by users + return {}; } resetControls() { - this.hints = defaultHints(); - this.ensureHintsGenerated(); for (const gamepad of this.gamepads.values()) { gamepad.resetMappings(); } } + clearControls() { + for (const gamepad of this.gamepads.values()) { + gamepad.clearMappings(); + } + } + handleConnect(e) { - this.ensureHintsGenerated(); for (const callback of this.connectCallbacks) { callback(); } @@ -640,10 +648,6 @@ class GamepadLib extends EventTarget { this.dispatchMouseMove(this.virtualCursor.x, this.virtualCursor.y); } } - - editor() { - throw new Error('removed'); - } } GamepadLib.browserHasBrokenGamepadAPI = () => { diff --git a/src/addons/gamepad/userscript.js b/src/addons/gamepad/userscript.js index 340501f..bb21dcc 100644 --- a/src/addons/gamepad/userscript.js +++ b/src/addons/gamepad/userscript.js @@ -100,7 +100,7 @@ export default async function (scaffolding, pointerlock) { const gamepad = new GamepadLib(); const parsedOptions = parseOptionsComment(); - gamepad.getHintsLazily = () => { + gamepad.getUserHints = () => { if (parsedOptions) { return { importedSettings: parsedOptions,