From 665ecc0e4b41b183677ff93cd0bdfd886dcfbbf1 Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Mon, 13 May 2024 02:51:59 +0200 Subject: [PATCH] effective keyboard key swap --- src/configs/cfg_keyboard_azerty.ts | 56 ++++++++++++++++++++++++++++++ src/inputs-controller.ts | 14 +++++++- src/system/game-data.ts | 11 +++++- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/configs/cfg_keyboard_azerty.ts b/src/configs/cfg_keyboard_azerty.ts index 576fa8dbb..b7d1b09a8 100644 --- a/src/configs/cfg_keyboard_azerty.ts +++ b/src/configs/cfg_keyboard_azerty.ts @@ -202,6 +202,62 @@ const cfg_keyboard_azerty = { KEY_V: Button.CYCLE_VARIANT, KEY_PLUS: Button.SPEED_UP, KEY_MINUS: Button.SLOW_DOWN, + KEY_A: -1, + KEY_B: -1, + KEY_D: -1, + KEY_H: -1, + KEY_I: -1, + KEY_J: -1, + KEY_K: -1, + KEY_L: -1, + KEY_M: -1, + KEY_O: -1, + KEY_P: -1, + KEY_Q: -1, + KEY_S: -1, + KEY_T: -1, + KEY_U: -1, + KEY_W: -1, + KEY_X: -1, + KEY_Y: -1, + KEY_Z: -1, + KEY_0: -1, + KEY_1: -1, + KEY_2: -1, + KEY_3: -1, + KEY_4: -1, + KEY_5: -1, + KEY_6: -1, + KEY_7: -1, + KEY_8: -1, + KEY_9: -1, + KEY_CTRL: -1, + KEY_DEL: -1, + KEY_END: -1, + KEY_F1: -1, + KEY_F2: -1, + KEY_F3: -1, + KEY_F4: -1, + KEY_F5: -1, + KEY_F6: -1, + KEY_F7: -1, + KEY_F8: -1, + KEY_F9: -1, + KEY_F10: -1, + KEY_F11: -1, + KEY_F12: -1, + KEY_HOME: -1, + KEY_INSERT: -1, + KEY_PAGE_DOWN: -1, + KEY_PAGE_UP: -1, + KEY_QUOTATION: -1, + KEY_SHIFT: -1, + KEY_TAB: -1, + KEY_TILDE: -1, + KEY_LEFT_BRACKET: -1, + KEY_RIGHT_BRACKET: -1, + KEY_SEMICOLON: -1, + KEY_ALT: -1 } }; diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 88e17f92a..f5ff95e51 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -796,7 +796,15 @@ export class InputsController { } swapKeyboardBinding(settingName, pressedButton): void { - console.log('swap'); + this.pauseUpdate = true; + const keyTarget = getCurrentlyAssignedToSettingName(this.keyboardConfigs[this.chosenKeyboard], settingName) + const keyNewBinding = getKeyFromKeyboardKeyCode(this.keyboardConfigs[this.chosenKeyboard], pressedButton); + const previousActionForThisNewBinding = this.keyboardConfigs[this.chosenKeyboard].custom[keyNewBinding]; + const ActionForThisNewBinding = this.keyboardConfigs[this.chosenKeyboard].custom[keyTarget]; + this.keyboardConfigs[this.chosenKeyboard].custom[keyTarget] = previousActionForThisNewBinding; + this.keyboardConfigs[this.chosenKeyboard].custom[keyNewBinding] = ActionForThisNewBinding; + this.scene.gameData.saveCustomKeyboardMapping(this.chosenKeyboard, this.keyboardConfigs[this.chosenKeyboard].custom); + setTimeout(() => this.pauseUpdate = false, 500); } /** @@ -810,4 +818,8 @@ export class InputsController { if (!this.configs[gamepadName]) this.configs[gamepadName] = {}; this.configs[gamepadName].custom = customMappings; } + injectKeyboardConfig(layout: string, customMappings: MappingLayout): void { + if (!this.keyboardConfigs[layout]) this.keyboardConfigs[layout] = {}; + this.keyboardConfigs[layout].custom = customMappings; + } } \ No newline at end of file diff --git a/src/system/game-data.ts b/src/system/game-data.ts index 9e1c97ae7..fa35d0f28 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -233,6 +233,7 @@ export class GameData { this.loadSettings(); this.loadGamepadSettings(); this.loadCustomMapping(); + this.loadCustomKeyboardMapping(); this.trainerId = Utils.randInt(65536); this.secretId = Utils.randInt(65536); this.starterData = {}; @@ -510,8 +511,16 @@ export class GameData { return true; } + public loadCustomKeyboardMapping(): boolean { + if (!localStorage.hasOwnProperty('customKeyboardMappings')) + return false; + const customKeyboardMappings = JSON.parse(localStorage.getItem('customKeyboardMappings')); + for (const key of Object.keys(customKeyboardMappings)) + this.scene.inputController.injectKeyboardConfig(key, customKeyboardMappings[key]); + + } + public loadCustomMapping(): boolean { - console.log('loadCustomMapping'); if (!localStorage.hasOwnProperty('customMapping')) return false; const customMappings = JSON.parse(localStorage.getItem('customMapping'));