fix bind after multiple delete

pull/685/head
Greenlamp 2024-05-14 18:54:48 +02:00
parent 9c4f04fe46
commit c222f0346d
2 changed files with 44 additions and 2 deletions

View File

@ -84,15 +84,17 @@ export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedBut
if (newBind && previousBind.action === -1) {
//special case when rebinding deleted key with already assigned key
const toRestore = deepCopy(newBind);
const iconFromThePressedButton = config.ogIcons[prevKey.key];
config.custom[newBind.key] = prevKey.from.action;
config.icons[prevKey.key] = newBind.icon;
config.icons[prevKey.key] = newBind.icon || iconFromThePressedButton;
config.icons[newBind.key] = prevKey.from.icon;
delete prevKey.from;
const nextSettingName = getKeyAndSettingNameFromCurrentKeysWithAction(config, newBind.action, newBind.isAlt).settingName;
config.currentKeys[nextSettingName].from = toRestore;
config.currentKeys[nextSettingName].from = toRestore.action === -1 ? config.currentKeys[nextSettingName].from : toRestore;
config.currentKeys[nextSettingName].isDeleted = true;
config.currentKeys[settingName].isDeleted = false;
config.currentKeys[settingName].replacedBy = toRestore.key;
} else if (!newBind) {
assignNewKey(config, settingName, pressedButton, previousBind);
@ -160,6 +162,8 @@ export function regenerateCustom(config): void {
custom[latestReplacedBy] = action;
} else if (!latestIsDeleted) {
custom[key] = action;
} else if (latestIsDeleted) {
custom[key] = -1;
}
}
config.custom = deepCopy(custom);

View File

@ -590,4 +590,42 @@ describe('Test Keyboard', () => {
expect(config.custom["KEY_D"]).toEqual(Button.RIGHT);
})
it('by method: Delete 2 binds, than rebind one of them', () => {
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");
expect(config.custom["KEY_Z"]).toEqual(Button.UP);
deleteBind(config, SettingInterfaceKeyboard.Alt_Button_Up);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z");
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(-1);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual(undefined);
expect(config.custom["KEY_Z"]).toEqual(-1);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].key).toEqual("KEY_S");
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].action).toEqual(Button.DOWN);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].icon).toEqual("T_S_Key_Dark.png");
expect(config.custom["KEY_S"]).toEqual(Button.DOWN);
deleteBind(config, SettingInterfaceKeyboard.Alt_Button_Down);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].key).toEqual("KEY_S");
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].action).toEqual(-1);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].icon).toEqual(undefined);
expect(config.custom["KEY_S"]).toEqual(-1);
swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.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].icon).toEqual("T_Z_Key_Dark.png");
expect(config.custom["KEY_Z"]).toEqual(Button.UP);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].key).toEqual("KEY_S");
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].action).toEqual(-1);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].icon).toEqual(undefined);
expect(config.custom["KEY_S"]).toEqual(-1);
});
});