Skip to content

Commit

Permalink
Discard gamepad indices when converting to inputs
Browse files Browse the repository at this point in the history
Remove RobotInputDatum, which marked protos Input objects with the
source gamepad index, and just send the Input objects from renderer to
main. I'm not sure how to communicate the index information to Runtime
or if it's important at all. Omitting this may have introduced a bug but
ts is happy for now, so...
  • Loading branch information
Hal-9k1 committed Oct 18, 2024
1 parent c9d1a20 commit 7a4691c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 41 deletions.
16 changes: 1 addition & 15 deletions src/common/IpcEventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,22 +309,8 @@ export interface MainQuitData {
* a specified opmode.
*/
export type MainUpdateRobotModeData = RobotRunMode;
/**
* Describes a single snapshot of gamepad or keyboard input.
*/
export interface MainRobotInputDatum {
/**
* The index of the gamepad that produced this input, or null if the input was produced by a
* keyboard.
*/
gamepadIndex: number | null;
/**
* The input, encoded as a protos Input object.
*/
input: RobotInput;
}
/**
* Data for the main-robot-input event sent by the renderer with gamepad or keyboard inputs bound
* for the robot.
*/
export type MainRobotInputData = MainRobotInputDatum[];
export type MainRobotInputData = RobotInput[];
43 changes: 17 additions & 26 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,41 +263,32 @@ export default function App() {
const onKeyUp = ({ key }: { key: string }) => onKeyChange(key, false);
window.addEventListener('keyup', onKeyUp);
const gamepadUpdateInterval = setInterval(() => {
// Possible bug requires testing: gamepad indices are not preserved after filtering
// Filter removes disconnected and 'ghost'/duplicate gamepads (can be distinguished by
// different mapping)
const gamepadInputs = navigator
.getGamepads()
// Preserve indices before filtering:
.map((gp, idx) => ({ gp, idx }))
// Filter null and 'ghost' gamepads:
.filter(
(obj): obj is { gp: Gamepad; idx: number } =>
obj.gp !== null && obj.gp.mapping === 'standard',
)
.map(({ gp, idx }: { gp: Gamepad; idx: number }) => {
.filter((gp): gp is Gamepad => gp !== null && gp.mapping === 'standard')
.map((gp) => {
let buttonBitmap: number = 0;
gp.buttons.forEach((button, buttonIdx) => {
if (button.pressed) {
buttonBitmap |= 1 << buttonIdx;
}
});
return {
gamepadIndex: idx,
input: new RobotInput({
connected: gp.connected,
axes: gp.axes.slice(),
buttons: buttonBitmap,
source: RobotInputSource.GAMEPAD,
}),
};
return new RobotInput({
connected: gp.connected,
axes: gp.axes.slice(),
buttons: buttonBitmap,
source: RobotInputSource.GAMEPAD,
});
});
const keyboardInput = {
gamepadIndex: null,
input: new RobotInput({
connected: keyboardControlsEnabled,
axes: [],
buttons: keyboardControlsEnabled ? Number(keyboardBitmap) : 0,
source: RobotInputSource.KEYBOARD,
}),
};
const keyboardInput = new RobotInput({
connected: keyboardControlsEnabled,
axes: [],
buttons: keyboardControlsEnabled ? Number(keyboardBitmap) : 0,
source: RobotInputSource.KEYBOARD,
});
// Possible bug requires testing: is Runtime ok with mixed input sources in same packet?
window.electron.ipcRenderer.sendMessage('main-robot-input', [
...gamepadInputs,
Expand Down

0 comments on commit 7a4691c

Please sign in to comment.