Skip to content

Commit

Permalink
joystick-protocol: Output active joystick actions directly to consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Dec 7, 2023
1 parent ab39b69 commit e509b04
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/stores/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {
JoystickProtocol,
} from '@/types/joystick'

export type controllerUpdateCallback = (state: JoystickState, protocolMapping: ProtocolControllerMapping) => void
export type controllerUpdateCallback = (
state: JoystickState,
protocolActionsMapping: JoystickProtocolActionsMapping,
activeButtonActions: ProtocolAction[]
) => void

export const useControllerStore = defineStore('controller', () => {
const joysticks = ref<Map<number, Joystick>>(new Map())
Expand Down Expand Up @@ -62,10 +66,39 @@ export const useControllerStore = defineStore('controller', () => {
joystick.gamepadToCockpitMap = cockpitStdMappings.value[joystickModel]

for (const callback of updateCallbacks.value) {
callback(joystick.state, protocolMapping.value)
callback(joystick.state, protocolMapping.value, activeButtonActions(joystick.state, protocolMapping.value))
}
}

const activeButtonActions = (
joystickState: JoystickState,
mapping: JoystickProtocolActionsMapping
): ProtocolAction[] => {
let modifierKeyId = modifierKeyActions.regular.id

Object.entries(mapping.buttonsCorrespondencies.regular).forEach((e) => {
const buttonActive = joystickState.buttons[Number(e[0])] ?? 0 > 0.5
const isModifier = Object.values(modifierKeyActions)
.map((a) => JSON.stringify(a))
.includes(JSON.stringify(e[1].action))
if (buttonActive && isModifier) {
modifierKeyId = e[1].action.id
}
})

const modKeyAction = modifierKeyActions[modifierKeyId as CockpitModifierKeyOption]

const activeActions = joystickState.buttons
.map((btnState, idx) => ({ id: idx, value: btnState }))
.filter((btn) => btn.value ?? 0 > 0.5)
.map(
(btn) =>
mapping.buttonsCorrespondencies[modifierKeyId as CockpitModifierKeyOption][btn.id as JoystickButton].action
)

return activeActions.concat(modKeyAction)
}

// If there's a mapping in our database that is not on the user storage, add it to the user
// This will happen whenever a new joystick profile is added to Cockpit's database
Object.entries(availableGamepadToCockpitMaps).forEach(([k, v]) => {
Expand Down

0 comments on commit e509b04

Please sign in to comment.