diff --git a/src/stores/controller.ts b/src/stores/controller.ts index 25c0d06aa..bed73ce8a 100644 --- a/src/stores/controller.ts +++ b/src/stores/controller.ts @@ -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>(new Map()) @@ -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]) => {