Skip to content

Commit

Permalink
Enable overriding of gamepad mappings by user agent
Browse files Browse the repository at this point in the history
This allows us to account for a scenario where Firefox's WebVR implementation reports OpenVR Y axes as inverted from WebXR's standard, but Chrome WebVR implementation does not.
  • Loading branch information
toji committed Dec 9, 2019
1 parent c222d74 commit d0fecbf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/devices/GamepadMappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ let openVr = {
},
targetRayTransform: {
orientation: [Math.PI * -0.08, 0, 0, 1]
},
userAgentOverrides: {
"Firefox": {
axes: {
invert: [1, 3]
}
}
}
};

Expand Down
26 changes: 25 additions & 1 deletion src/devices/GamepadXRInputSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ class XRRemappedGamepad {
map = {};
}

// Apply user-agent-specific overrides to the mapping when applicable.
if (map.userAgentOverrides) {
for (let agent in map.userAgentOverrides) {
if (navigator.userAgent.includes(agent)) {
let override = map.userAgentOverrides[agent];

for (let key in override) {
if (key in map) {
// If the key already exists, merge the override values into the
// existing dictionary.
Object.assign(map[key], override[key]);
} else {
// If the base mapping doesn't have this key, insert the override
// values wholesale.
map[key] = override[key];
}
}
break;
}
}
}

let axes = new Array(map.axes && map.axes.length ? map.axes.length : gamepad.axes.length);
let buttons = new Array(map.buttons && map.buttons.length ? map.buttons.length : gamepad.buttons.length);

Expand Down Expand Up @@ -96,7 +118,9 @@ class XRRemappedGamepad {

if (map.axes && map.axes.invert) {
for (let axis of map.axes.invert) {
axes[axis] *= -1;
if (axis < axes.length) {
axes[axis] *= -1;
}
}
}

Expand Down

0 comments on commit d0fecbf

Please sign in to comment.