diff --git a/src/configs/gamepad-utils.ts b/src/configs/gamepad-utils.ts index 43c6d0920..76a90d564 100644 --- a/src/configs/gamepad-utils.ts +++ b/src/configs/gamepad-utils.ts @@ -1,5 +1,6 @@ import {InterfaceConfig} from "../inputs-controller"; import {Button} from "#app/enums/buttons"; +import {deepCopy} from "#app/utils"; // Given a button index from an input event, return its naming from the mapping config export function getKeyFromMapping(config: InterfaceConfig, index: number): String | null { @@ -90,5 +91,14 @@ export function reloadCurrentKeys(config): void { icon, } } - config.currentKeys = currentKeys; + config.currentKeys = deepCopy(currentKeys); +} + +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; + } + config.custom = deepCopy(custom); } \ No newline at end of file diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 93010f668..889f5bf8b 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -11,7 +11,7 @@ import SettingsKeyboardUiHandler from "./ui/settings/settings-keyboard-ui-handle import cfg_keyboard_azerty from "./configs/cfg_keyboard_azerty"; import { getKeyAndActionFromCurrentKeysWithPressedButton, - getKeyFromMapping, + getKeyFromMapping, regenerateCustom, reloadCurrentKeys, swapCurrentKeys } from "#app/configs/gamepad-utils"; import {deepCopy} from "./utils"; @@ -338,9 +338,13 @@ export class InputsController { const gamepadID = gamepad.toLowerCase(); const config = deepCopy(this.getConfig(gamepadID)); config.custom = this.configs[gamepad]?.custom || {...config.default}; - reloadCurrentKeys(config); + 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]?.custom); + this.scene.gameData?.saveCustomMapping(this.chosenGamepad, this.configs[gamepad]?.currentKeys); } if (this.chosenGamepad === thisGamepad.id) this.initChosenGamepad(this.chosenGamepad) } @@ -349,9 +353,13 @@ export class InputsController { for (const layout of ['default']) { const config = deepCopy(this.getConfigKeyboard(layout)); config.custom = this.keyboardConfigs[layout]?.custom || {...config.default}; - reloadCurrentKeys(config); + 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]?.custom); + this.scene.gameData?.saveCustomKeyboardMapping(this.chosenKeyboard, this.keyboardConfigs[layout]?.currentKeys); } this.initChosenLayoutKeyboard(this.chosenKeyboard) } @@ -752,13 +760,13 @@ export class InputsController { * @param gamepadName The identifier of the gamepad to configure. * @param customMappings The custom mapping configuration to apply to the gamepad. */ - injectConfig(gamepadName: String, customMappings: MappingLayout): void { + injectConfig(gamepadName: String, customMappings): void { if (!this.configs[gamepadName]) this.configs[gamepadName] = {}; - this.configs[gamepadName].custom = customMappings; + this.configs[gamepadName].currentKeys = customMappings; } - injectKeyboardConfig(layout: string, customMappings: MappingLayout): void { + injectKeyboardConfig(layout: string, customMappings): void { if (!this.keyboardConfigs[layout]) this.keyboardConfigs[layout] = {}; - this.keyboardConfigs[layout].custom = customMappings; + this.keyboardConfigs[layout].currentKeys = customMappings; } swapBinding(config, settingName, pressedButton): void { diff --git a/src/system/game-data.ts b/src/system/game-data.ts index ac2ae9d38..31da8c084 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -498,20 +498,20 @@ export class GameData { return true; } - public saveCustomMapping(gamepadName: string, mapping: MappingLayout): boolean { + public saveCustomMapping(gamepadName: string, currentKeys): boolean { let customMappings: object = {}; if (localStorage.hasOwnProperty('customMapping')) customMappings = JSON.parse(localStorage.getItem('customMapping')); - customMappings[gamepadName] = mapping; + customMappings[gamepadName] = currentKeys; localStorage.setItem('customMapping', JSON.stringify(customMappings)); return true; } - public saveCustomKeyboardMapping(keyboardLayout: string, mapping: MappingLayout): boolean { + public saveCustomKeyboardMapping(keyboardLayout: string, currentKeys): boolean { let customKeyboardMappings: object = {}; if (localStorage.hasOwnProperty('customKeyboardMappings')) customKeyboardMappings = JSON.parse(localStorage.getItem('customKeyboardMappings')); - customKeyboardMappings[keyboardLayout] = mapping; + customKeyboardMappings[keyboardLayout] = currentKeys; localStorage.setItem('customKeyboardMappings', JSON.stringify(customKeyboardMappings)); return true; } diff --git a/src/ui/settings/gamepad-binding-ui-handler.ts b/src/ui/settings/gamepad-binding-ui-handler.ts index 0519e7e44..3bece0542 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.custom); + this.scene.gameData.saveCustomMapping(this.scene.inputController?.chosenGamepad, activeConfig.currentKeys); 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 cd13757d8..f1057bb92 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.custom); + this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, activeConfig.currentKeys); return true; }