diff --git a/src/configs/gamepad-utils.ts b/src/configs/gamepad-utils.ts index 3773afb03..d48064ac3 100644 --- a/src/configs/gamepad-utils.ts +++ b/src/configs/gamepad-utils.ts @@ -69,14 +69,32 @@ export function assignNewKey(config: InterfaceConfig, settingName, pressedButton const icon = config.ogIcons[key]; config.icons[previousBind.key] = icon; config.currentKeys[settingName].icon = icon; - config.custom[key] = previousBind.action; + + config.custom[key] = previousBind.action !== -1 ? previousBind.action : previousBind.from.action; + config.custom[previousBind.key] = -1; + config.currentKeys[settingName].replacedBy = key; + + delete config.currentKeys[settingName].from } export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedButton): void { const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); const prevKey = deepCopy(previousBind); const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton); - if (!newBind) { + if (newBind && previousBind.action === -1) { + //special case when rebinding deleted key with already assigned key + const toRestore = deepCopy(newBind); + config.custom[newBind.key] = prevKey.from.action; + config.icons[prevKey.key] = newBind.icon; + config.icons[newBind.key] = prevKey.from.icon; + + delete prevKey.from; + + const nextSettingName = getKeyAndSettingNameFromCurrentKeysWithAction(config, newBind.action, newBind.isAlt).settingName; + config.currentKeys[nextSettingName].from = toRestore; + config.currentKeys[nextSettingName].isDeleted = true; + config.currentKeys[settingName].replacedBy = toRestore.key; + } else if (!newBind) { assignNewKey(config, settingName, pressedButton, previousBind); } else { const nextKey = deepCopy(newBind); @@ -96,7 +114,7 @@ export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedBut 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; + const nextSettingName = getKeyAndSettingNameFromCurrentKeysWithAction(config, newBind.action, newBind.isAlt).settingName; config.currentKeys[settingName].from = prevKey; config.currentKeys[nextSettingName].from = nextKey; } @@ -112,10 +130,27 @@ export function reloadCurrentKeys(config): void { const settingName = config.setting[key]; const action = config.custom[key]; const icon = config.icons[key]; + if (currentKeys[settingName]?.latestReplacedBy) { + console.log(''); + } if (!currentKeys[settingName]) currentKeys[settingName] = {}; currentKeys[settingName].key = key; - currentKeys[settingName].action = action === undefined ? currentKeys[settingName].action : action; - currentKeys[settingName].icon = icon; + currentKeys[settingName].isAlt = settingName.includes("ALT_"); + const previousAction = config.custom[currentKeys[settingName].replacedBy] + if (action === -1 && previousAction !== undefined) { + currentKeys[settingName].action = previousAction; + currentKeys[settingName].icon = icon; + currentKeys[settingName].latestReplacedBy = config.currentKeys[settingName].replacedBy + delete currentKeys[settingName].replacedBy; + } else if (currentKeys[settingName].isDeleted) { + currentKeys[settingName].action = -1; + currentKeys[settingName].icon = undefined; + currentKeys[settingName].latestIsDeleted = config.currentKeys[settingName].isDeleted + delete currentKeys[settingName].isDeleted; + } else { + currentKeys[settingName].action = action; + currentKeys[settingName].icon = action === -1 ? undefined : icon; + } } config.currentKeys = deepCopy(currentKeys); } @@ -123,14 +158,22 @@ export function reloadCurrentKeys(config): void { export function regenerateCustom(config): void { const custom = deepCopy(config.custom); for (const settingName of Object.keys(config.currentKeys)) { - const {key, action} = config.currentKeys[settingName]; - custom[key] = action; + const {key, action, latestReplacedBy, latestIsDeleted} = config.currentKeys[settingName]; + if (latestReplacedBy) { + custom[key] = -1; + custom[latestReplacedBy] = action; + } else if (!latestIsDeleted) { + custom[key] = action; + } } config.custom = deepCopy(custom); } export function deleteBind(config, settingName): void { const { key } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); + const prev = deepCopy(config.currentKeys[settingName]); delete config.currentKeys[settingName].icon - config.custom[key] = undefined + config.currentKeys[settingName].from = prev; + config.custom[key] = -1; + reloadCurrentKeys(config); } \ No newline at end of file diff --git a/src/test/keyboard_remaping.test.ts b/src/test/keyboard_remaping.test.ts index 94552caf2..d27d806e5 100644 --- a/src/test/keyboard_remaping.test.ts +++ b/src/test/keyboard_remaping.test.ts @@ -291,12 +291,12 @@ describe('Test Keyboard', () => { deleteBind(config, settingNameA) expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); - expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(-1); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual(undefined); }) - it('Delete bind then asign', () => { + it('Delete bind then asign not existing button', () => { const settingNameA = SettingInterfaceKeyboard.Alt_Button_Up; expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); @@ -305,14 +305,137 @@ describe('Test Keyboard', () => { deleteBind(config, settingNameA) expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); - expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(-1); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual(undefined); + expect(config.custom["KEY_Z"]).toEqual(-1); swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.B); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_B_Key_Dark.png"); + expect(config.custom["KEY_B"]).toEqual(Button.UP); + expect(config.custom["KEY_Z"]).toEqual(-1); + }) + + + it('swap bind, then Delete bind then assign bind', () => { + const settingNameA = SettingInterfaceKeyboard.Alt_Button_Up; + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_Z_Key_Dark.png"); + expect(config.custom["KEY_Z"]).toEqual(Button.UP); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.B); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_B_Key_Dark.png"); + expect(config.custom["KEY_B"]).toEqual(Button.UP); + expect(config.custom["KEY_Z"]).toEqual(-1); + + deleteBind(config, settingNameA); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(-1); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual(undefined); + expect(config.custom["KEY_Z"]).toEqual(-1); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.B); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_B_Key_Dark.png"); + expect(config.custom["KEY_B"]).toEqual(Button.UP); + }) + + + it('Delete bind then asign not already existing button', () => { + const settingNameA = SettingInterfaceKeyboard.Alt_Button_Up; + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_Z_Key_Dark.png"); + expect(config.custom["KEY_Z"]).toEqual(Button.UP); + deleteBind(config, settingNameA) + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(-1); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual(undefined); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].key).toEqual("KEY_L"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].action).toEqual(Button.CYCLE_ABILITY); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].icon).toEqual("T_L_Key_Dark.png"); + expect(config.custom["KEY_Z"]).toEqual(-1); + expect(config.custom["KEY_L"]).toEqual(Button.CYCLE_ABILITY); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.L); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_L_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].key).toEqual("KEY_L"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].action).toEqual(-1); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].icon).toEqual(undefined); + + expect(config.custom["KEY_Z"]).toEqual(-1); + expect(config.custom["KEY_L"]).toEqual(Button.UP); + }) + + + it('Custom scenario 2, regenerate customs when init key is not from setting', () => { + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_Z_Key_Dark.png"); + expect(config.custom["KEY_Z"]).toEqual(Button.UP); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.T); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_T_Key_Dark.png"); + + expect(config.custom["KEY_Z"]).toEqual(-1); + expect(config.custom["KEY_T"]).toEqual(Button.UP); + }) + + + it('change alt to unknown touch than another one alt with another unknown touch', () => { + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_Z_Key_Dark.png"); + expect(config.custom["KEY_Z"]).toEqual(Button.UP); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.T); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_T_Key_Dark.png"); + + expect(config.custom["KEY_Z"]).toEqual(-1); + expect(config.custom["KEY_T"]).toEqual(Button.UP); + + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].key).toEqual("KEY_S"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].action).toEqual(Button.DOWN); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].icon).toEqual("T_S_Key_Dark.png"); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Down, Phaser.Input.Keyboard.KeyCodes.U); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].key).toEqual("KEY_S"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].action).toEqual(Button.DOWN); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].icon).toEqual("T_U_Key_Dark.png"); + + + expect(config.custom["KEY_S"]).toEqual(-1); + expect(config.custom["KEY_U"]).toEqual(Button.DOWN); + expect(config.custom["KEY_Z"]).toEqual(-1); + expect(config.custom["KEY_T"]).toEqual(Button.UP); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_T_Key_Dark.png"); }) }); \ No newline at end of file diff --git a/src/ui/settings/gamepad-binding-ui-handler.ts b/src/ui/settings/gamepad-binding-ui-handler.ts index e9345e825..96ec0595f 100644 --- a/src/ui/settings/gamepad-binding-ui-handler.ts +++ b/src/ui/settings/gamepad-binding-ui-handler.ts @@ -2,8 +2,7 @@ import BattleScene from "../../battle-scene"; import AbstractBindingUiHandler from "../settings/abrast-binding-ui-handler"; import {Mode} from "../ui"; import { - getKeyAndActionFromCurrentKeysWithPressedButton, - getKeyAndActionFromCurrentKeysWithSettingName, getKeyFromMapping, + getKeyAndActionFromCurrentKeysWithSettingName, getKeyFromMapping, regenerateCustom, } from "#app/configs/gamepad-utils"; @@ -33,6 +32,7 @@ export default class GamepadBindingUiHandler extends AbstractBindingUiHandler { const activeConfig = this.scene.inputController.getActiveConfig(); this.scene.inputController.swapBinding(activeConfig, this.target, this.buttonPressed) this.scene.gameData.saveCustomMapping(this.scene.inputController?.chosenGamepad, activeConfig.currentKeys); + regenerateCustom(activeConfig); return true; } } \ No newline at end of file diff --git a/src/ui/settings/keyboard-binding-ui-handler.ts b/src/ui/settings/keyboard-binding-ui-handler.ts index 242d0dfef..e2d6599b0 100644 --- a/src/ui/settings/keyboard-binding-ui-handler.ts +++ b/src/ui/settings/keyboard-binding-ui-handler.ts @@ -2,8 +2,7 @@ import BattleScene from "../../battle-scene"; import AbstractBindingUiHandler from "../settings/abrast-binding-ui-handler"; import {Mode} from "../ui"; import { - getKeyAndActionFromCurrentKeysWithPressedButton, - getKeyAndActionFromCurrentKeysWithSettingName, getKeyFromMapping, + getKeyAndActionFromCurrentKeysWithSettingName, getKeyFromMapping, regenerateCustom, } from "#app/configs/gamepad-utils"; @@ -32,6 +31,7 @@ export default class KeyboardBindingUiHandler extends AbstractBindingUiHandler { const activeConfig = this.scene.inputController.getActiveKeyboardConfig(); this.scene.inputController.swapBinding(activeConfig, this.target, this.buttonPressed) this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, activeConfig.currentKeys); + regenerateCustom(activeConfig); return true; } diff --git a/src/ui/settings/settings-keyboard-ui-handler.ts b/src/ui/settings/settings-keyboard-ui-handler.ts index 2564f3966..4c3962f21 100644 --- a/src/ui/settings/settings-keyboard-ui-handler.ts +++ b/src/ui/settings/settings-keyboard-ui-handler.ts @@ -26,13 +26,13 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl onDeleteDown(): void { const cursor = this.cursor + this.scrollCursor; // Calculate the absolute cursor position. - console.log('delete pressed', cursor, this.settingLabels[cursor].text); const selection = this.settingLabels[cursor].text; const key = reverseValueToKeySetting(selection); const setting = SettingKeyboard[key]; + const activeConfig = this.getActiveConfig(); deleteBind(this.getActiveConfig(), setting); - need to handle the "no icon" - console.log('setting:', setting); + this.saveCustomKeyboardMappingToLocalStorage(activeConfig); + this.updateBindings(); } getActiveConfig(): InterfaceConfig { @@ -76,8 +76,12 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl } - saveSettingToLocalStorage(setting, cursor): void { - if (this.settingDevice[setting] !== this.settingDevice.Default_Layout) - this.scene.gameData.saveKeyboardSetting(setting, cursor) + saveCustomKeyboardMappingToLocalStorage(config): void { + this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, config.currentKeys); + } + + saveSettingToLocalStorage(settingName, cursor): void { + if (this.settingDevice[settingName] !== this.settingDevice.Default_Layout) + this.scene.gameData.saveKeyboardSetting(settingName, cursor) } } \ No newline at end of file