handled delete bind and assign back

pull/685/head
Greenlamp 2024-05-14 04:05:32 +02:00
parent 82e0934907
commit a2eab6259b
5 changed files with 98 additions and 24 deletions

View File

@ -64,30 +64,42 @@ export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedB
return getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); return getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
} }
export function assignNewKey(config: InterfaceConfig, settingName, pressedButton, previousBind): void {
const key = getKeyFromMapping(config, pressedButton);
const icon = config.ogIcons[key];
config.icons[previousBind.key] = icon;
config.currentKeys[settingName].icon = icon;
config.custom[key] = previousBind.action;
}
export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedButton): void { export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedButton): void {
const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
const prevKey = deepCopy(previousBind); const prevKey = deepCopy(previousBind);
const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton); const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton);
const nextKey = deepCopy(newBind); if (!newBind) {
if (prevKey.key === nextKey.key) { assignNewKey(config, settingName, pressedButton, previousBind);
// special case when back to back and not enough info to get back to previous button
const toRestore = getKeyAndSettingNameFromCurrentKeysWithAction(config, prevKey.from.action);
config.custom[prevKey.key] = prevKey.from.action;
config.icons[prevKey.key] = prevKey.from.icon;
config.custom[toRestore.key] = prevKey.action;
config.icons[toRestore.key] = prevKey.icon;
delete config.currentKeys[settingName].from;
delete config.currentKeys[toRestore.settingName].from;
} else { } else {
config.custom[previousBind.key] = newBind.action; const nextKey = deepCopy(newBind);
config.custom[newBind.key] = previousBind.action; if (prevKey.key === nextKey.key) {
config.icons[previousBind.key] = newBind.icon; // special case when back to back and not enough info to get back to previous button
config.icons[newBind.key] = previousBind.icon; const toRestore = getKeyAndSettingNameFromCurrentKeysWithAction(config, prevKey.from.action);
const nextSettingName = getKeyAndSettingNameFromCurrentKeysWithAction(config, newBind.action).settingName; config.custom[prevKey.key] = prevKey.from.action;
config.currentKeys[settingName].from = prevKey; config.icons[prevKey.key] = prevKey.from.icon;
config.currentKeys[nextSettingName].from = nextKey;
config.custom[toRestore.key] = prevKey.action;
config.icons[toRestore.key] = prevKey.icon;
delete config.currentKeys[settingName].from;
delete config.currentKeys[toRestore.settingName].from;
} else {
config.custom[previousBind.key] = newBind.action;
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;
config.currentKeys[settingName].from = prevKey;
config.currentKeys[nextSettingName].from = nextKey;
}
} }
reloadCurrentKeys(config); reloadCurrentKeys(config);
} }
@ -102,7 +114,7 @@ export function reloadCurrentKeys(config): void {
const icon = config.icons[key]; const icon = config.icons[key];
if (!currentKeys[settingName]) currentKeys[settingName] = {}; if (!currentKeys[settingName]) currentKeys[settingName] = {};
currentKeys[settingName].key = key; currentKeys[settingName].key = key;
currentKeys[settingName].action = action; currentKeys[settingName].action = action === undefined ? currentKeys[settingName].action : action;
currentKeys[settingName].icon = icon; currentKeys[settingName].icon = icon;
} }
config.currentKeys = deepCopy(currentKeys); config.currentKeys = deepCopy(currentKeys);
@ -115,4 +127,10 @@ export function regenerateCustom(config): void {
custom[key] = action; custom[key] = action;
} }
config.custom = deepCopy(custom); config.custom = deepCopy(custom);
}
export function deleteBind(config, settingName): void {
const { key } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
delete config.currentKeys[settingName].icon
config.custom[key] = undefined
} }

View File

@ -36,6 +36,7 @@ export interface InterfaceConfig {
padID: string; padID: string;
padType: string; padType: string;
gamepadMapping: GamepadMapping; gamepadMapping: GamepadMapping;
ogIcons: IconsMapping;
icons: IconsMapping; icons: IconsMapping;
setting: SettingMapping; setting: SettingMapping;
default: MappingLayout; default: MappingLayout;

View File

@ -1,6 +1,7 @@
import {beforeEach, expect, describe, it} from "vitest"; import {beforeEach, expect, describe, it} from "vitest";
import cfg_keyboard_example, {SettingInterfaceKeyboard} from "#app/test/cfg_keyboard_example"; import cfg_keyboard_example, {SettingInterfaceKeyboard} from "#app/test/cfg_keyboard_example";
import { import {
deleteBind,
getIconWithPressedButton, getIconWithPressedButton,
getIconWithSettingName, getIconWithSettingName,
getKeyAndActionFromCurrentKeysWithSettingName, getKeyAndActionFromCurrentKeysWithSettingName,
@ -23,6 +24,7 @@ describe('Test Keyboard', () => {
beforeEach(() => { beforeEach(() => {
config = deepCopy(cfg_keyboard_example); config = deepCopy(cfg_keyboard_example);
config.custom = {...config.default} config.custom = {...config.default}
config.ogIcons = {...config.icons}
reloadCurrentKeys(config); reloadCurrentKeys(config);
}); });
@ -268,8 +270,6 @@ describe('Test Keyboard', () => {
it('Swap alt with a key not binded yet', () => { it('Swap alt with a key not binded yet', () => {
const settingNameA = SettingInterfaceKeyboard.Alt_Button_Up;
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); 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(Button.UP);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_Z_Key_Dark.png"); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_Z_Key_Dark.png");
@ -279,6 +279,40 @@ describe('Test Keyboard', () => {
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); 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(Button.UP);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_B_Key_Dark.png"); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_B_Key_Dark.png");
}) })
it('Delete 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");
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].icon).toEqual(undefined);
})
it('Delete bind then asign', () => {
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");
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].icon).toEqual(undefined);
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");
})
}); });

View File

@ -2,9 +2,10 @@ import BattleScene from "../../battle-scene";
import {Mode} from "../ui"; import {Mode} from "../ui";
import cfg_keyboard_azerty from "#app/configs/cfg_keyboard_azerty"; import cfg_keyboard_azerty from "#app/configs/cfg_keyboard_azerty";
import {SettingKeyboard, settingKeyboardDefaults, settingKeyboardOptions} from "#app/system/settings-keyboard"; import {SettingKeyboard, settingKeyboardDefaults, settingKeyboardOptions} from "#app/system/settings-keyboard";
import {truncateString} from "#app/utils"; import {reverseValueToKeySetting, truncateString} from "#app/utils";
import AbstractSettingsUiUiHandler from "#app/ui/settings/abstract-settings-ui-handler"; import AbstractSettingsUiUiHandler from "#app/ui/settings/abstract-settings-ui-handler";
import {InterfaceConfig} from "#app/inputs-controller"; import {InterfaceConfig} from "#app/inputs-controller";
import {deleteBind} from "#app/configs/gamepad-utils";
export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandler { export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandler {
@ -18,6 +19,20 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl
this.commonSettingsCount = 1; this.commonSettingsCount = 1;
this.textureOverride = 'keyboard'; this.textureOverride = 'keyboard';
this.localStoragePropertyName = 'settingsKeyboard'; this.localStoragePropertyName = 'settingsKeyboard';
const deleteEvent = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DELETE);
deleteEvent.on('up', this.onDeleteDown, this);
}
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];
deleteBind(this.getActiveConfig(), setting);
need to handle the "no icon"
console.log('setting:', setting);
} }
getActiveConfig(): InterfaceConfig { getActiveConfig(): InterfaceConfig {

View File

@ -352,4 +352,10 @@ export function truncateString(str: String, maxLength: number = 10) {
export function deepCopy(values: object): object { export function deepCopy(values: object): object {
return JSON.parse(JSON.stringify(values)); return JSON.parse(JSON.stringify(values));
}
export function reverseValueToKeySetting(input) {
const words = input.split(' ');
const capitalizedWords = words.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
return capitalizedWords.join('_');
} }