fix bug save bindings

pull/685/head
Greenlamp 2024-05-14 00:59:06 +02:00
parent 5399bda0f6
commit 66414e9bbc
5 changed files with 34 additions and 16 deletions

View File

@ -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);
}

View File

@ -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 {

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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;
}