handled delete bind and assign back
parent
82e0934907
commit
a2eab6259b
|
@ -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);
|
||||
|
@ -116,3 +128,9 @@ export function regenerateCustom(config): void {
|
|||
}
|
||||
config.custom = deepCopy(custom);
|
||||
}
|
||||
|
||||
export function deleteBind(config, settingName): void {
|
||||
const { key } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
|
||||
delete config.currentKeys[settingName].icon
|
||||
config.custom[key] = undefined
|
||||
}
|
|
@ -36,6 +36,7 @@ export interface InterfaceConfig {
|
|||
padID: string;
|
||||
padType: string;
|
||||
gamepadMapping: GamepadMapping;
|
||||
ogIcons: IconsMapping;
|
||||
icons: IconsMapping;
|
||||
setting: SettingMapping;
|
||||
default: MappingLayout;
|
||||
|
|
|
@ -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");
|
||||
})
|
||||
|
||||
});
|
|
@ -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 {
|
||||
|
|
|
@ -353,3 +353,9 @@ 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('_');
|
||||
}
|
Loading…
Reference in New Issue