Update gamepad addon with upstream changes

These changes were, again, written by me so GPL need not apply.
This commit is contained in:
GarboMuffin committed 2023-01-08 21:25:22 -06:00
1 parent 9665b32ce6
commit 53bbdfcefd
2 files changed
+28 -24

No files matched your search

+27 -23
View File
@@ -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 = () => {
+1 -1
View File
@@ -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,