Repair gamepad input state tracking

This commit is contained in:
Tony Garnock-Jones 2023-01-10 15:14:01 +01:00
parent 45890f0103
commit feb595cb55
1 changed files with 20 additions and 8 deletions

View File

@ -12,6 +12,8 @@ import {
WebXRSessionManager, WebXRSessionManager,
} from '@babylonjs/core/Legacy/legacy'; } from '@babylonjs/core/Legacy/legacy';
import { log } from './log.js';
if ((navigator as any).oscpu?.startsWith('Linux')) { if ((navigator as any).oscpu?.startsWith('Linux')) {
// ^ oscpu is undefined on chrome on Android, at least... // ^ oscpu is undefined on chrome on Android, at least...
@ -91,7 +93,7 @@ export class RunningEngine {
leanBase: { position: Vector3 } | null = null; leanBase: { position: Vector3 } | null = null;
recenterBase: { rotation: Quaternion } | null = null; recenterBase: { rotation: Quaternion } | null = null;
padStates: Map<Gamepad, GamepadState> = new Map(); padStates: Map<number, GamepadState> = new Map();
static async start( static async start(
interactivity: Interactivity, interactivity: Interactivity,
@ -154,20 +156,30 @@ export class RunningEngine {
} }
this.engine.runRenderLoop(() => { this.engine.runRenderLoop(() => {
Array.from(navigator.getGamepads()).forEach(gp => { try {
if (gp !== null) this.checkGamepadInput(gp); Array.from(navigator.getGamepads()).forEach(gp => {
}); if (gp !== null) this.checkGamepadInput(gp);
this.scene.render(); });
this.scene.render();
} catch (e) {
console.error('Error in render loop', e);
throw e;
}
}); });
window.addEventListener("resize", () => this.engine.resize()); window.addEventListener("resize", () => this.engine.resize());
} }
padStateFor(gp: Gamepad): GamepadState { padStateFor(gp: Gamepad): GamepadState {
const state = this.padStates.get(gp); const state = this.padStates.get(gp.index);
if (state) return state; if (state) {
// Apparently reusing Gamepad instances across frames doesn't work,
// so we update (!) the stored instance here:
state.gp = gp;
return state;
}
const newState = new GamepadState(gp); const newState = new GamepadState(gp);
this.padStates.set(gp, newState); this.padStates.set(gp.index, newState);
return newState; return newState;
} }