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 51bd57c commit 9334f43
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/stores/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type controllerUpdateCallback = (
export const useControllerStore = defineStore('controller', () => {
const joysticks = ref<Map<number, Joystick>>(new Map())
const updateCallbacks = ref<controllerUpdateCallback[]>([])
const protocolMapping = useStorage('cockpit-protocol-mapping-v3', cockpitStandardToProtocols)
const protocolMapping = useStorage('cockpit-protocol-mapping-v5.4', cockpitStandardToProtocols)
const cockpitStdMappings = useStorage('cockpit-standard-mappings', availableGamepadToCockpitMaps)
const availableAxesActions = allAvailableAxes
const availableButtonActions = allAvailableButtons
Expand Down 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 9334f43

Please sign in to comment.