From 79c0116e81123568b4d18362bfcc017a82ec665a Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Fri, 17 May 2024 12:24:50 +0200 Subject: [PATCH] fix indirect delete blacklisted button --- src/configs/configHandler.ts | 8 +++++- src/test/helpers/menuManip.ts | 9 +++++-- src/test/rebinding_setting.test.ts | 25 ++++++++++++------- src/ui/settings/abrast-binding-ui-handler.ts | 10 +++++--- .../settings/keyboard-binding-ui-handler.ts | 2 +- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/configs/configHandler.ts b/src/configs/configHandler.ts index 645931118fd..bb7ac5d17c7 100644 --- a/src/configs/configHandler.ts +++ b/src/configs/configHandler.ts @@ -120,7 +120,7 @@ export function assign(config, settingNameTarget, keycode): boolean { // first, we need to check if this keycode is already used on another settingName const previousSettingName = getSettingNameWithKeycode(config, keycode); const key = getKeyWithSettingName(config, previousSettingName); - if (!canIAssignThisKey(config, key)) return false; + if (!canIAssignThisKey(config, key) || ! canIOverrideThisSetting(config, settingNameTarget)) return false; // if it was already bound, we delete the bind if (previousSettingName) { const previousKey = getKeyWithSettingName(config, previousSettingName); @@ -169,6 +169,12 @@ export function canIAssignThisKey(config, key) { return true; } +export function canIOverrideThisSetting(config, settingName) { + const key = getKeyWithSettingName(config, settingName); + if (config.blacklist?.includes(key) || isTheLatestBind(config, settingName)) return false; + return true; +} + export function canIDeleteThisKey(config, key) { return canIAssignThisKey(config, key); } diff --git a/src/test/helpers/menuManip.ts b/src/test/helpers/menuManip.ts index d181692f2f5..6b86f4828d3 100644 --- a/src/test/helpers/menuManip.ts +++ b/src/test/helpers/menuManip.ts @@ -8,7 +8,7 @@ import { getKeyWithKeycode, getKeyWithSettingName, assign, - getSettingNameWithKeycode, canIAssignThisKey, canIDeleteThisKey + getSettingNameWithKeycode, canIAssignThisKey, canIDeleteThisKey, canIOverrideThisSetting } from "#app/configs/configHandler"; export class MenuManip { @@ -112,12 +112,17 @@ export class MenuManip { assign(this.config, this.settingName, this.keycode); } - weCantConfirm() { + weCantAssignThisKey() { const key = getKeyWithKeycode(this.config, this.keycode); expect(canIAssignThisKey(this.config, key)).toEqual(false); return this; } + weCantOverrideThisBind() { + expect(canIOverrideThisSetting(this.config, this.settingName)).toEqual(false); + return this; + } + weCantDelete() { const key = getKeyWithSettingName(this.config, this.settingName); expect(canIDeleteThisKey(this.config, key)).toEqual(false); diff --git a/src/test/rebinding_setting.test.ts b/src/test/rebinding_setting.test.ts index 05bfe66af48..f47a0780fb2 100644 --- a/src/test/rebinding_setting.test.ts +++ b/src/test/rebinding_setting.test.ts @@ -103,30 +103,37 @@ describe('Test Rebinding', () => { it('Check prevent rebind indirectly the d-pad buttons', () => { inTheSettingMenu.whenCursorIsOnSetting("Alt_Button_Left").iconDisplayedIs("Q") inTheSettingMenu.whenCursorIsOnSetting("Alt_Button_Right").iconDisplayedIs("D") - inTheSettingMenu.whenCursorIsOnSetting("Alt_Button_Left").iconDisplayedIs("Q").weWantThisBindInstead("LEFT").weCantConfirm().butLetsForceIt(); + inTheSettingMenu.whenCursorIsOnSetting("Alt_Button_Left").iconDisplayedIs("Q").weWantThisBindInstead("LEFT").weCantAssignThisKey().butLetsForceIt(); inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Left"); inGame.whenWePressOnKeyboard("Q").weShouldTriggerTheButton("Alt_Button_Left"); inGame.whenWePressOnKeyboard("D").weShouldTriggerTheButton("Alt_Button_Right"); }); + it('Swap alt with a d-pad main', () => { + inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); + inGame.whenWePressOnKeyboard("Z").weShouldTriggerTheButton("Alt_Button_Up"); + inTheSettingMenu.whenCursorIsOnSetting("Button_Up").iconDisplayedIs("KEY_ARROW_UP").weWantThisBindInstead("Z").weCantOverrideThisBind().butLetsForceIt(); + inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); + inGame.whenWePressOnKeyboard("Z").weShouldTriggerTheButton("Alt_Button_Up"); + }); + it('Check if double assign d-pad is blocked', () => { - inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Button_Left"); inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); - inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("RIGHT").weCantConfirm().butLetsForceIt(); + inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("RIGHT").weCantOverrideThisBind().weCantAssignThisKey().butLetsForceIt(); inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Button_Left"); inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); - inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("UP").weCantConfirm().butLetsForceIt(); + inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("UP").weCantOverrideThisBind().weCantAssignThisKey().butLetsForceIt(); inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Button_Left"); inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); - inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("RIGHT").weCantConfirm().butLetsForceIt(); + inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("RIGHT").weCantOverrideThisBind().weCantAssignThisKey().butLetsForceIt(); inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Button_Left"); inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); @@ -158,18 +165,18 @@ describe('Test Rebinding', () => { }); it('Check if triple swap d-pad is prevented', () => { - inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("RIGHT").weCantConfirm().butLetsForceIt(); + inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("RIGHT").weCantOverrideThisBind().weCantAssignThisKey().butLetsForceIt(); inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Button_Left"); inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); - inTheSettingMenu.whenCursorIsOnSetting("Button_Right").iconDisplayedIs("KEY_ARROW_RIGHT").weWantThisBindInstead("UP").weCantConfirm().butLetsForceIt(); + inTheSettingMenu.whenCursorIsOnSetting("Button_Right").iconDisplayedIs("KEY_ARROW_RIGHT").weWantThisBindInstead("UP").weCantOverrideThisBind().weCantAssignThisKey().butLetsForceIt(); inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Button_Left"); inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); - inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("LEFT").weCantConfirm().butLetsForceIt(); + inTheSettingMenu.whenCursorIsOnSetting("Button_Left").iconDisplayedIs("KEY_ARROW_LEFT").weWantThisBindInstead("LEFT").weCantOverrideThisBind().weCantAssignThisKey().butLetsForceIt(); inGame.whenWePressOnKeyboard("LEFT").weShouldTriggerTheButton("Button_Left"); inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); @@ -327,7 +334,7 @@ describe('Test Rebinding', () => { inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); inGame.whenWePressOnKeyboard("Z").weShouldTriggerTheButton("Alt_Button_Up"); inGame.whenWePressOnKeyboard("D").weShouldTriggerTheButton("Alt_Button_Right"); - inTheSettingMenu.whenCursorIsOnSetting("Button_Up").iconDisplayedIs("KEY_ARROW_UP").weWantThisBindInstead("RIGHT").weCantConfirm().butLetsForceIt(); + inTheSettingMenu.whenCursorIsOnSetting("Button_Up").iconDisplayedIs("KEY_ARROW_UP").weWantThisBindInstead("RIGHT").weCantOverrideThisBind().weCantAssignThisKey().butLetsForceIt(); inGame.whenWePressOnKeyboard("UP").weShouldTriggerTheButton("Button_Up"); inGame.whenWePressOnKeyboard("RIGHT").weShouldTriggerTheButton("Button_Right"); inGame.whenWePressOnKeyboard("Z").weShouldTriggerTheButton("Alt_Button_Up"); diff --git a/src/ui/settings/abrast-binding-ui-handler.ts b/src/ui/settings/abrast-binding-ui-handler.ts index 8d344db1e95..9a46129a017 100644 --- a/src/ui/settings/abrast-binding-ui-handler.ts +++ b/src/ui/settings/abrast-binding-ui-handler.ts @@ -241,11 +241,13 @@ export default abstract class AbstractBindingUiHandler extends UiHandler { onInputDown(buttonIcon: string, assignedButtonIcon: string, type: string): void { this.newButtonIcon.setTexture(type); this.newButtonIcon.setFrame(buttonIcon); - this.targetButtonIcon.setTexture(type); - this.targetButtonIcon.setFrame(assignedButtonIcon); + if (assignedButtonIcon) { + this.targetButtonIcon.setTexture(type); + this.targetButtonIcon.setFrame(assignedButtonIcon); + this.targetButtonIcon.setVisible(true); + this.swapText.setVisible(true); + } this.newButtonIcon.setVisible(true); - this.targetButtonIcon.setVisible(true); - this.swapText.setVisible(true); this.setCursor(0); this.actionsContainer.setVisible(true); } diff --git a/src/ui/settings/keyboard-binding-ui-handler.ts b/src/ui/settings/keyboard-binding-ui-handler.ts index d007f2758da..4d7d19c6a8e 100644 --- a/src/ui/settings/keyboard-binding-ui-handler.ts +++ b/src/ui/settings/keyboard-binding-ui-handler.ts @@ -27,7 +27,7 @@ export default class KeyboardBindingUiHandler extends AbstractBindingUiHandler { if (!buttonIcon) return; this.buttonPressed = key; const assignedButtonIcon = getIconWithSettingName(activeConfig, this.target); - this.onInputDown(buttonIcon, assignedButtonIcon, 'keyboard'); + this.onInputDown(buttonIcon, null, 'keyboard'); } swapAction(): boolean {