diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 319017d90..40b51addb 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -340,13 +340,14 @@ export class InputsController { const config = deepCopy(this.getConfig(gamepadID)); config.custom = this.configs[gamepad]?.custom || {...config.default}; config.ogIcons = {...config.icons}; + config.icons = this.configs[gamepad]?.icons || {...config.icons}; config.currentKeys = this.configs[gamepad]?.currentKeys; if (!this.configs[gamepad]?.currentKeys) reloadCurrentKeys(config); else regenerateCustom(config); this.configs[gamepad] = config; - this.scene.gameData?.saveCustomMapping(this.chosenGamepad, this.configs[gamepad]?.currentKeys); + this.scene.gameData?.saveCustomMapping(this.chosenGamepad, this.configs[gamepad]?.currentKeys, this.configs[gamepad]?.icons); } if (this.chosenGamepad === thisGamepad.id) this.initChosenGamepad(this.chosenGamepad) } @@ -355,14 +356,15 @@ export class InputsController { for (const layout of ['default']) { const config = deepCopy(this.getConfigKeyboard(layout)); config.custom = this.keyboardConfigs[layout]?.custom || {...config.default}; - config.currentKeys = this.keyboardConfigs[layout]?.currentKeys; config.ogIcons = {...config.icons}; + config.icons = this.keyboardConfigs[layout]?.icons || {...config.icons}; + config.currentKeys = this.keyboardConfigs[layout]?.currentKeys; if (!this.keyboardConfigs[layout]?.currentKeys) reloadCurrentKeys(config); else regenerateCustom(config); this.keyboardConfigs[layout] = config; - this.scene.gameData?.saveCustomKeyboardMapping(this.chosenKeyboard, this.keyboardConfigs[layout]?.currentKeys); + this.scene.gameData?.saveCustomKeyboardMapping(this.chosenKeyboard, this.keyboardConfigs[layout]?.currentKeys, this.keyboardConfigs[layout]?.icons); } this.initChosenLayoutKeyboard(this.chosenKeyboard) } @@ -765,11 +767,13 @@ export class InputsController { */ injectConfig(gamepadName: String, customMappings): void { if (!this.configs[gamepadName]) this.configs[gamepadName] = {}; - this.configs[gamepadName].currentKeys = customMappings; + this.configs[gamepadName].currentKeys = customMappings.currentKeys; + this.configs[gamepadName].icons = customMappings.icons; } injectKeyboardConfig(layout: string, customMappings): void { if (!this.keyboardConfigs[layout]) this.keyboardConfigs[layout] = {}; - this.keyboardConfigs[layout].currentKeys = customMappings; + this.keyboardConfigs[layout].currentKeys = customMappings.currentKeys; + this.keyboardConfigs[layout].icons = customMappings.icons; } swapBinding(config, settingName, pressedButton): void { diff --git a/src/system/game-data.ts b/src/system/game-data.ts index 31da8c084..4bf0496fe 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -498,20 +498,26 @@ export class GameData { return true; } - public saveCustomMapping(gamepadName: string, currentKeys): boolean { + public saveCustomMapping(gamepadName: string, currentKeys, icons): boolean { let customMappings: object = {}; if (localStorage.hasOwnProperty('customMapping')) customMappings = JSON.parse(localStorage.getItem('customMapping')); - customMappings[gamepadName] = currentKeys; + customMappings[gamepadName] = { + currentKeys, + icons + }; localStorage.setItem('customMapping', JSON.stringify(customMappings)); return true; } - public saveCustomKeyboardMapping(keyboardLayout: string, currentKeys): boolean { + public saveCustomKeyboardMapping(keyboardLayout: string, currentKeys, icons): boolean { let customKeyboardMappings: object = {}; if (localStorage.hasOwnProperty('customKeyboardMappings')) customKeyboardMappings = JSON.parse(localStorage.getItem('customKeyboardMappings')); - customKeyboardMappings[keyboardLayout] = currentKeys; + customKeyboardMappings[keyboardLayout] = { + currentKeys, + icons + }; localStorage.setItem('customKeyboardMappings', JSON.stringify(customKeyboardMappings)); return true; } diff --git a/src/test/keyboard_remaping.test.ts b/src/test/keyboard_remaping.test.ts index 8eae9dd42..ccdbd6a33 100644 --- a/src/test/keyboard_remaping.test.ts +++ b/src/test/keyboard_remaping.test.ts @@ -7,7 +7,7 @@ import { getKeyAndActionFromCurrentKeysWithSettingName, getKeyForSettingName, getKeyFromMapping, - getKeyWithAction, + getKeyWithAction, regenerateCustom, reloadCurrentKeys, swapCurrentKeys, } from "#app/configs/gamepad-utils"; @@ -443,4 +443,151 @@ describe('Test Keyboard', () => { expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_T_Key_Dark.png"); }) + + it('reload scenario with 1 bind already reassigned', () => { + config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up] = { + "key": "KEY_Z", + "isAlt": true, + "action": 3, + "icon": "T_D_Key_Dark.png", + "from": { + "key": "KEY_Z", + "isAlt": true, + "action": 0, + "icon": "T_Z_Key_Dark.png" + } + }; + config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right] = { + "key": "KEY_D", + "isAlt": true, + "action": 0, + "icon": "T_Z_Key_Dark.png", + "from": { + "key": "KEY_D", + "isAlt": true, + "action": 3, + "icon": "T_D_Key_Dark.png" + } + } + config.icons["KEY_D"] = "T_Z_Key_Dark.png"; + config.icons["KEY_Z"] = "T_D_Key_Dark.png"; + regenerateCustom(config); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_D_Key_Dark.png"); + expect(config.custom["KEY_Z"]).toEqual(Button.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].key).toEqual("KEY_D"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].icon).toEqual("T_Z_Key_Dark.png"); + expect(config.custom["KEY_D"]).toEqual(Button.UP); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].icon).toEqual("T_Up_Key_Dark.png"); + expect(config.custom["KEY_ARROW_UP"]).toEqual(Button.UP); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Button_Up, Phaser.Input.Keyboard.KeyCodes.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_D_Key_Dark.png"); + expect(config.custom["KEY_Z"]).toEqual(Button.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].key).toEqual("KEY_D"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].icon).toEqual("T_Z_Key_Dark.png"); + expect(config.custom["KEY_D"]).toEqual(Button.UP); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].icon).toEqual("T_Right_Key_Dark.png"); + expect(config.custom["KEY_ARROW_UP"]).toEqual(Button.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].icon).toEqual("T_Up_Key_Dark.png"); + expect(config.custom["KEY_ARROW_RIGHT"]).toEqual(Button.UP); + }); + + + it('Swap multiple touch alt and main', () => { + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].icon).toEqual("T_Up_Key_Dark.png"); + expect(config.custom["KEY_ARROW_UP"]).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_Z_Key_Dark.png"); + expect(config.custom["KEY_Z"]).toEqual(Button.UP); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].icon).toEqual("T_Right_Key_Dark.png"); + expect(config.custom["KEY_ARROW_RIGHT"]).toEqual(Button.RIGHT); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Button_Up, Phaser.Input.Keyboard.KeyCodes.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].icon).toEqual("T_Right_Key_Dark.png"); + expect(config.custom["KEY_ARROW_UP"]).toEqual(Button.RIGHT); + + 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); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].icon).toEqual("T_Up_Key_Dark.png"); + expect(config.custom["KEY_ARROW_RIGHT"]).toEqual(Button.UP); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.D); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].icon).toEqual("T_Right_Key_Dark.png"); + expect(config.custom["KEY_ARROW_UP"]).toEqual(Button.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_D_Key_Dark.png"); + expect(config.custom["KEY_Z"]).toEqual(Button.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].icon).toEqual("T_Up_Key_Dark.png"); + expect(config.custom["KEY_ARROW_RIGHT"]).toEqual(Button.UP); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].key).toEqual("KEY_D"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].icon).toEqual("T_Z_Key_Dark.png"); + expect(config.custom["KEY_D"]).toEqual(Button.UP); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.Z); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].icon).toEqual("T_Right_Key_Dark.png"); + expect(config.custom["KEY_ARROW_UP"]).toEqual(Button.RIGHT); + + 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); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].icon).toEqual("T_Up_Key_Dark.png"); + expect(config.custom["KEY_ARROW_RIGHT"]).toEqual(Button.UP); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].key).toEqual("KEY_D"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].icon).toEqual("T_D_Key_Dark.png"); + expect(config.custom["KEY_D"]).toEqual(Button.RIGHT); + }) + }); \ 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 96ec0595f..ca0b696dc 100644 --- a/src/ui/settings/gamepad-binding-ui-handler.ts +++ b/src/ui/settings/gamepad-binding-ui-handler.ts @@ -31,7 +31,7 @@ export default class GamepadBindingUiHandler extends AbstractBindingUiHandler { swapAction() { 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); + this.scene.gameData.saveCustomMapping(this.scene.inputController?.chosenGamepad, activeConfig.currentKeys, activeConfig.icons); regenerateCustom(activeConfig); return true; } diff --git a/src/ui/settings/keyboard-binding-ui-handler.ts b/src/ui/settings/keyboard-binding-ui-handler.ts index e2d6599b0..d2e0c792c 100644 --- a/src/ui/settings/keyboard-binding-ui-handler.ts +++ b/src/ui/settings/keyboard-binding-ui-handler.ts @@ -30,7 +30,7 @@ export default class KeyboardBindingUiHandler extends AbstractBindingUiHandler { swapAction() { 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); + this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, activeConfig.currentKeys, activeConfig.icons); 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 4c3962f21..ad2b04cf4 100644 --- a/src/ui/settings/settings-keyboard-ui-handler.ts +++ b/src/ui/settings/settings-keyboard-ui-handler.ts @@ -77,7 +77,7 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl } saveCustomKeyboardMappingToLocalStorage(config): void { - this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, config.currentKeys); + this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, config.currentKeys, config.icons); } saveSettingToLocalStorage(settingName, cursor): void {