From 4058404c614431f0c697a347e9097befddc540b0 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Lehmkuhl Date: Wed, 6 Dec 2023 21:46:09 -0300 Subject: [PATCH] joystick-protocol: Prevent unwanted mapping behaviors from users --- src/stores/controller.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/stores/controller.ts b/src/stores/controller.ts index ef927222a..ff1495b34 100644 --- a/src/stores/controller.ts +++ b/src/stores/controller.ts @@ -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]) => {