Skip to content

Commit

Permalink
joystick-protocol: Prevent unwanted mapping behaviors from users
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Dec 7, 2023
1 parent fca4cb1 commit 194ad9b
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/stores/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,38 @@ export const useControllerStore = defineStore('controller', () => {
return activeActions.concat(modKeyAction)
}

setInterval(() => {
// eslint-disable-next-line jsdoc/require-jsdoc
const btnsToUnmap: { modKey: CockpitModifierKeyOption; button: JoystickButton }[] = []
Object.entries(protocolMapping.value.buttonsCorrespondencies.regular).forEach((v) => {
if (v[1].action.protocol == JoystickProtocol.CockpitModifierKey) {
btnsToUnmap.push({ modKey: v[1].action.id as CockpitModifierKeyOption, button: Number(v[0]) as JoystickButton })
}
})

Object.entries(protocolMapping.value.buttonsCorrespondencies).forEach(([modKey, mapping]) => {
Object.entries(mapping).forEach(([btn, action]) => {
const modKeyAction = modifierKeyActions[modKey as CockpitModifierKeyOption]
if (JSON.stringify(action.action) !== JSON.stringify(modKeyAction)) return
Swal.fire({ text: "Cannot map modifier key to it's own layout.", icon: 'warning', timer: 5000 })
protocolMapping.value.buttonsCorrespondencies[modKey as CockpitModifierKeyOption][
Number(btn) as JoystickButton
].action = otherAvailableActions.no_function
})
})

btnsToUnmap.forEach((v) => {
const actionToUnmap = protocolMapping.value.buttonsCorrespondencies[v.modKey][v.button].action
if (JSON.stringify(actionToUnmap) === JSON.stringify(otherAvailableActions.no_function)) return
Swal.fire({
text: `Unmapping '${actionToUnmap.name} from ${v.modKey} layout. Cannot use same button as the modifier.`,
icon: 'warning',
timer: 5000,
})
protocolMapping.value.buttonsCorrespondencies[v.modKey][v.button].action = otherAvailableActions.no_function
})
}, 1000)

// 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 194ad9b

Please sign in to comment.