From ea4372e1f37a7fa4fa5829d69bdf9974a9294b36 Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Tue, 14 May 2024 02:18:39 +0200 Subject: [PATCH] fix back to back button binding --- src/configs/gamepad-utils.ts | 47 ++++++++++++++++++++++--------- src/test/gamepad_remaping.test.ts | 22 ++++++++++++++- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/configs/gamepad-utils.ts b/src/configs/gamepad-utils.ts index 76a90d564..473336ef2 100644 --- a/src/configs/gamepad-utils.ts +++ b/src/configs/gamepad-utils.ts @@ -55,10 +55,7 @@ function getKeyAndSettingNameFromCurrentKeysWithAction(config, _action, alt: boo } export function getKeyAndActionFromCurrentKeysWithSettingName(config, settingName) { - for (const _settingName of Object.keys(config.currentKeys)) { - if (_settingName === settingName) return config.currentKeys[_settingName]; - } - return null; + return config.currentKeys[settingName]; } export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton) { @@ -69,27 +66,49 @@ export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedB export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedButton): void { const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); + const prevKey = deepCopy(previousBind); const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton); - config.custom[previousBind.key] = newBind.action; - config.custom[newBind.key] = previousBind.action; - config.icons[previousBind.key] = newBind.icon; - config.icons[newBind.key] = previousBind.icon; + const nextKey = deepCopy(newBind); + if (prevKey.key === nextKey.key) { + // special case when back to back and not enough info to get back to previous button + const toRestore = getKeyAndSettingNameFromCurrentKeysWithAction(config, prevKey.from.action); + const iconToRestore = config.icons[toRestore.key]; + config.custom[prevKey.key] = prevKey.from.action; + config.icons[prevKey.key] = iconToRestore; + + config.custom[toRestore.key] = prevKey.action; + config.icons[toRestore.key] = prevKey.icon; + + config.currentKeys[settingName].from = prevKey; + config.currentKeys[toRestore.settingName].from = { + key: toRestore.key, + action: prevKey.from.action, + icon: iconToRestore, + }; + } else { + config.custom[previousBind.key] = newBind.action; + config.custom[newBind.key] = previousBind.action; + config.icons[previousBind.key] = newBind.icon; + config.icons[newBind.key] = previousBind.icon; + const nextSettingName = getKeyAndSettingNameFromCurrentKeysWithAction(config, newBind.action).settingName; + config.currentKeys[settingName].from = prevKey; + config.currentKeys[nextSettingName].from = nextKey; + } reloadCurrentKeys(config); } export function reloadCurrentKeys(config): void { // need to rework this to include keys that were not there at the begining - const currentKeys = {}; + const currentKeys = config.currentKeys ? deepCopy(config.currentKeys) : {}; for (const key of Object.keys(config.setting)) { const settingName = config.setting[key]; const action = config.custom[key]; const icon = config.icons[key]; - currentKeys[settingName] = { - key, - action, - icon, - } + if (!currentKeys[settingName]) currentKeys[settingName] = {}; + currentKeys[settingName].key = key; + currentKeys[settingName].action = action; + currentKeys[settingName].icon = icon; } config.currentKeys = deepCopy(currentKeys); } diff --git a/src/test/gamepad_remaping.test.ts b/src/test/gamepad_remaping.test.ts index 5e717f6d3..8bd683519 100644 --- a/src/test/gamepad_remaping.test.ts +++ b/src/test/gamepad_remaping.test.ts @@ -6,7 +6,7 @@ import { getKeyAndActionFromCurrentKeysWithSettingName, getKeyForSettingName, getKeyFromMapping, - getKeyWithAction, initCurrentKeys, + getKeyWithAction, reloadCurrentKeys, swapCurrentKeys, } from "#app/configs/gamepad-utils"; @@ -23,6 +23,7 @@ describe('Test Keyboard', () => { const temp = {...cfg_gamepad_example} config = deepCopy(temp); config.custom = {...config.default} + config.ogIcons = {...config.icons} reloadCurrentKeys(config); }); @@ -162,5 +163,24 @@ describe('Test Keyboard', () => { expect(iconA).toEqual('T_X_B_Color_Alt.png'); expect(iconB).toEqual('T_X_A_Color_Alt.png'); expect(iconC).toEqual('T_X_X_Color_Alt.png'); + + expect(config.ogIcons["RC_S"]).toEqual("T_X_A_Color_Alt.png") + expect(config.ogIcons["RC_E"]).toEqual("T_X_B_Color_Alt.png") + expect(config.ogIcons["RC_N"]).toEqual("T_X_Y_Color_Alt.png") + expect(config.ogIcons["RC_W"]).toEqual("T_X_X_Color_Alt.png") + }); + + it('Check 2 swap back to back', () => { + swapCurrentKeys(config, SettingInterfaceGamepad.Button_Action, 1); // cancel + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].key).toEqual("RC_S"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].action).toEqual(Button.CANCEL); + let iconA = getIconWithSettingName(config, SettingInterfaceGamepad.Button_Action); + expect(iconA).toEqual('T_X_B_Color_Alt.png'); + + swapCurrentKeys(config, SettingInterfaceGamepad.Button_Action, 0); // cancel + let iconB = getIconWithSettingName(config, SettingInterfaceGamepad.Button_Action); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].key).toEqual("RC_S"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].action).toEqual(Button.ACTION); + expect(iconB).toEqual('T_X_A_Color_Alt.png'); }); }); \ No newline at end of file