From a2eab6259bd70d699ea5cc0e2ad3b16600606928 Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Tue, 14 May 2024 04:05:32 +0200 Subject: [PATCH] handled delete bind and assign back --- src/configs/gamepad-utils.ts | 58 ++++++++++++------- src/inputs-controller.ts | 1 + src/test/keyboard_remaping.test.ts | 40 ++++++++++++- .../settings/settings-keyboard-ui-handler.ts | 17 +++++- src/utils.ts | 6 ++ 5 files changed, 98 insertions(+), 24 deletions(-) diff --git a/src/configs/gamepad-utils.ts b/src/configs/gamepad-utils.ts index b6445cbf0..3773afb03 100644 --- a/src/configs/gamepad-utils.ts +++ b/src/configs/gamepad-utils.ts @@ -64,30 +64,42 @@ export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedB 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 { const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); const prevKey = deepCopy(previousBind); const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton); - const nextKey = deepCopy(newBind); - if (prevKey.key === nextKey.key) { - // 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; + if (!newBind) { + assignNewKey(config, settingName, pressedButton, previousBind); } 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; + const nextKey = deepCopy(newBind); + if (prevKey.key === nextKey.key) { + // 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 { + 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); } @@ -102,7 +114,7 @@ export function reloadCurrentKeys(config): void { const icon = config.icons[key]; if (!currentKeys[settingName]) currentKeys[settingName] = {}; currentKeys[settingName].key = key; - currentKeys[settingName].action = action; + currentKeys[settingName].action = action === undefined ? currentKeys[settingName].action : action; currentKeys[settingName].icon = icon; } config.currentKeys = deepCopy(currentKeys); @@ -115,4 +127,10 @@ export function regenerateCustom(config): void { custom[key] = action; } config.custom = deepCopy(custom); +} + +export function deleteBind(config, settingName): void { + const { key } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); + delete config.currentKeys[settingName].icon + config.custom[key] = undefined } \ No newline at end of file diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 30072dc00..319017d90 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -36,6 +36,7 @@ export interface InterfaceConfig { padID: string; padType: string; gamepadMapping: GamepadMapping; + ogIcons: IconsMapping; icons: IconsMapping; setting: SettingMapping; default: MappingLayout; diff --git a/src/test/keyboard_remaping.test.ts b/src/test/keyboard_remaping.test.ts index 4564f4243..94552caf2 100644 --- a/src/test/keyboard_remaping.test.ts +++ b/src/test/keyboard_remaping.test.ts @@ -1,6 +1,7 @@ import {beforeEach, expect, describe, it} from "vitest"; import cfg_keyboard_example, {SettingInterfaceKeyboard} from "#app/test/cfg_keyboard_example"; import { + deleteBind, getIconWithPressedButton, getIconWithSettingName, getKeyAndActionFromCurrentKeysWithSettingName, @@ -23,6 +24,7 @@ describe('Test Keyboard', () => { beforeEach(() => { config = deepCopy(cfg_keyboard_example); config.custom = {...config.default} + config.ogIcons = {...config.icons} reloadCurrentKeys(config); }); @@ -268,8 +270,6 @@ describe('Test Keyboard', () => { 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].action).toEqual(Button.UP); 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].action).toEqual(Button.UP); 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"); + }) + }); \ No newline at end of file diff --git a/src/ui/settings/settings-keyboard-ui-handler.ts b/src/ui/settings/settings-keyboard-ui-handler.ts index fdbbb5c07..2564f3966 100644 --- a/src/ui/settings/settings-keyboard-ui-handler.ts +++ b/src/ui/settings/settings-keyboard-ui-handler.ts @@ -2,9 +2,10 @@ import BattleScene from "../../battle-scene"; import {Mode} from "../ui"; import cfg_keyboard_azerty from "#app/configs/cfg_keyboard_azerty"; 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 {InterfaceConfig} from "#app/inputs-controller"; +import {deleteBind} from "#app/configs/gamepad-utils"; export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandler { @@ -18,6 +19,20 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl this.commonSettingsCount = 1; this.textureOverride = 'keyboard'; 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 { diff --git a/src/utils.ts b/src/utils.ts index 82fad57ea..e83815841 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -352,4 +352,10 @@ export function truncateString(str: String, maxLength: number = 10) { export function deepCopy(values: object): object { 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('_'); } \ No newline at end of file