From e8b6e588a5dc5cb80a9e3072961f76dbe7b630a9 Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Fri, 17 May 2024 00:43:23 +0200 Subject: [PATCH] adding tests to welcome new behaviour --- src/configs/cfg_keyboard_azerty.ts | 10 ++++++++ src/configs/configHandler.ts | 40 ++++++++++++++++++++++++++++++ src/test/cfg_keyboard.example.ts | 10 ++++++++ src/test/helpers/menuManip.ts | 15 +++++++++-- src/test/rebinding_setting.test.ts | 35 ++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) diff --git a/src/configs/cfg_keyboard_azerty.ts b/src/configs/cfg_keyboard_azerty.ts index 80bd6346da8..ace981af67a 100644 --- a/src/configs/cfg_keyboard_azerty.ts +++ b/src/configs/cfg_keyboard_azerty.ts @@ -278,6 +278,16 @@ const cfg_keyboard_azerty = { }, main: [], alt: [], + blacklist: [ + "KEY_ENTER", + "KEY_ESC", + "KEY_ARROW_UP", + "KEY_ARROW_DOWN", + "KEY_ARROW_LEFT", + "KEY_ARROW_RIGHT", + "KEY_DELETE", + "KEY_HOME", + ] }; export default cfg_keyboard_azerty; diff --git a/src/configs/configHandler.ts b/src/configs/configHandler.ts index 650cbdc39fd..991fe1d43d1 100644 --- a/src/configs/configHandler.ts +++ b/src/configs/configHandler.ts @@ -211,6 +211,23 @@ export function swap(config, settingNameTarget, keycode) { regenerateIdentifiers(config); } +export function assign(config, settingNameTarget, keycode) { + // first, we need to check if this keycode is already used on another settingName + const previousSettingName = getSettingNameWithKeycode(config, keycode); + // if it was already bound, we delete the bind + if (previousSettingName) { + const previousKey = getKeyWithSettingName(config, previousSettingName); + config.custom[previousKey] = -1; + } + // then, we need to delete the current key for this settingName + const currentKey = getKeyWithSettingName(config, settingNameTarget); + config.custom[currentKey] = -1; + + // then, the new key is assigned to the new settingName + const newKey = getKeyWithKeycode(config, keycode); + config.custom[newKey] = settingNameTarget; +} + /** * Deletes the binding of the specified setting name. * @@ -223,6 +240,29 @@ export function deleteBind(config, settingName) { regenerateIdentifiers(config); } +/** + * Deletes the binding of the specified setting name. but prevent the deletion of keys in the blacklist + * + * @param config - The configuration object containing custom settings. + * @param settingName - The setting name to delete. + */ +export function safeDeleteBind(config, settingName) { + const key = getKeyWithSettingName(config, settingName); + if (config.blacklist.includes(key) || isTheLatestBind(config, settingName)) return; + config.custom[key] = -1; +} + +export function isTheLatestBind(config, settingName) { + const isAlt = settingName.includes("ALT_"); + let altSettingName; + if (isAlt) + altSettingName = settingName.split("ALT_").splice(1)[0]; + else + altSettingName = `ALT_${settingName}`; + const secondButton = getKeyWithSettingName(config, altSettingName); + return secondButton === undefined; +} + /** * Regenerates the identifiers for main and alternate settings. * This allows distinguishing between main and alternate bindings. diff --git a/src/test/cfg_keyboard.example.ts b/src/test/cfg_keyboard.example.ts index dbca2934c9d..cf05cfd5c34 100644 --- a/src/test/cfg_keyboard.example.ts +++ b/src/test/cfg_keyboard.example.ts @@ -315,6 +315,16 @@ const cfg_keyboard_azerty = { }, main: [], alt: [], + blacklist: [ + "KEY_ENTER", + "KEY_ESC", + "KEY_ARROW_UP", + "KEY_ARROW_DOWN", + "KEY_ARROW_LEFT", + "KEY_ARROW_RIGHT", + "KEY_DELETE", + "KEY_HOME", + ] }; export default cfg_keyboard_azerty; diff --git a/src/test/helpers/menuManip.ts b/src/test/helpers/menuManip.ts index d4e4cc9ae18..57cad0a20ac 100644 --- a/src/test/helpers/menuManip.ts +++ b/src/test/helpers/menuManip.ts @@ -6,7 +6,7 @@ import { getIconWithKey, getIconWithKeycode, getIconWithSettingName, - getKeyWithKeycode, getKeyWithSettingName, getKeySolvingConflict, swap + getKeyWithKeycode, getKeyWithSettingName, getKeySolvingConflict, swap, assign, safeDeleteBind } from "#app/configs/configHandler"; export class MenuManip { @@ -44,7 +44,8 @@ export class MenuManip { whenCursorIsOnSetting(settingName) { if (!settingName.includes("Button_")) settingName = "Button_" + settingName; this.settingName = SettingInterface[settingName]; - const buttonName = this.convertNameToButtonString(settingName); + const isAlt = settingName.includes("ALT_"); + const buttonName = isAlt ? settingName.toUpperCase().split("ALT_BUTTON_").splice(1)[0] : settingName.toUpperCase().split("BUTTON_").splice(1)[0]; expect(this.config.settings[this.settingName]).toEqual(Button[buttonName]); return this; } @@ -91,6 +92,16 @@ export class MenuManip { return this; } + whenWeTryToDelete(settingName?: string) { + this.settingName = SettingInterface[settingName] || this.settingName; + safeDeleteBind(this.config, this.settingName); + return this; + } + + confirmAssignment() { + assign(this.config, this.settingName, this.keycode); + } + confirm() { swap(this.config, this.settingName, this.keycode); } diff --git a/src/test/rebinding_setting.test.ts b/src/test/rebinding_setting.test.ts index df54f7eedb3..1efb3de4b04 100644 --- a/src/test/rebinding_setting.test.ts +++ b/src/test/rebinding_setting.test.ts @@ -323,4 +323,39 @@ describe('Test Rebinding', () => { inGame.forTheSource("keyboard").forTheWantedBind("Cycle_Shiny").weShouldSeeTheIcon("R") inGame.forTheSource("keyboard").forTheWantedBind("Cycle_Form").weShouldSeeTheIcon("F") }); + + it("test new assign feature to delete the bind from the previous action instead of swaping it", () => { + inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Button_Left"); + inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("RIGHT").confirmAssignment(); + inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Left"); + inGame.whenWePressOnKeyboard("LEFT").nothingShouldHappen(); + + inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_RIGHT").weWantThisBindInstead("RIGHT").confirmAssignment(); + inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Left"); + inGame.whenWePressOnKeyboard("LEFT").nothingShouldHappen(); + }); + + it("check the key displayed on confirm", () => { + inGame.whenWePressOnKeyboard("ENTER").weShouldTriggerTheButton("Button_Submit"); + inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); + inGame.whenWePressOnKeyboard("DOWN").weShouldTriggerTheButton("Button_Down"); + inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Button_Left"); + inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); + inGame.whenWePressOnKeyboard("ESC").weShouldTriggerTheButton("Button_Menu"); + inGame.whenWePressOnKeyboard("HOME").nothingShouldHappen(); + inTheSettingMenu.whenCursorIsOnSetting("Button_Submit").iconDisplayedIs("KEY_ENTER").whenWeTryToDelete().iconDisplayedIs("KEY_ENTER") + inTheSettingMenu.whenCursorIsOnSetting("Button_Up").iconDisplayedIs("KEY_ARROW_UP").whenWeTryToDelete().iconDisplayedIs("KEY_ARROW_UP") + inTheSettingMenu.whenCursorIsOnSetting("Button_Down").iconDisplayedIs("KEY_ARROW_DOWN").whenWeTryToDelete().iconDisplayedIs("KEY_ARROW_DOWN") + inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").whenWeTryToDelete().iconDisplayedIs("KEY_ARROW_LEFT") + inTheSettingMenu.whenCursorIsOnSetting("Button_Right").iconDisplayedIs("KEY_ARROW_RIGHT").whenWeTryToDelete().iconDisplayedIs("KEY_ARROW_RIGHT") + inTheSettingMenu.whenCursorIsOnSetting("Button_Menu").iconDisplayedIs("KEY_ESC").whenWeTryToDelete().iconDisplayedIs("KEY_ESC") + inTheSettingMenu.whenCursorIsOnSetting("Alt_Button_Up").iconDisplayedIs("KEY_Z").whenWeTryToDelete().thereShouldBeNoIconAnymore(); + }); + + it("check to delete all the binds of an action", () => { + inGame.whenWePressOnKeyboard("V").weShouldTriggerTheButton("Button_Cycle_Variant"); + inGame.whenWePressOnKeyboard("K").weShouldTriggerTheButton("Alt_Button_Cycle_Variant"); + inTheSettingMenu.whenCursorIsOnSetting("Alt_Button_Cycle_Variant").iconDisplayedIs("KEY_K").whenWeTryToDelete().thereShouldBeNoIconAnymore(); + inTheSettingMenu.whenCursorIsOnSetting("Button_Cycle_Variant").iconDisplayedIs("KEY_V").whenWeTryToDelete().iconDisplayedIs("KEY_V") + }); });