fix an assign bug with tests

pull/685/head
Greenlamp 2024-05-14 19:39:01 +02:00
parent c222f0346d
commit c7d26ac9bc
2 changed files with 165 additions and 23 deletions

View File

@ -70,9 +70,10 @@ export function assignNewKey(config: InterfaceConfig, settingName, pressedButton
config.icons[previousBind.key] = icon;
config.currentKeys[settingName].icon = icon;
config.custom[key] = previousBind.action !== -1 ? previousBind.action : previousBind.from.action;
config.custom[previousBind.key] = -1;
config.custom[key] = previousBind.action !== -1 ? previousBind.action : previousBind.from.action;
config.currentKeys[settingName].replacedBy = key;
config.currentKeys[settingName].latestIsDeleted = false;
delete config.currentKeys[settingName].from
}
@ -81,7 +82,9 @@ export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedBut
const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
const prevKey = deepCopy(previousBind);
const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton);
if (newBind && previousBind.action === -1) {
if (newBind?.action === -1 && previousBind.action === -1) {
assignNewKey(config, settingName, pressedButton, previousBind);
} else 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];
@ -100,7 +103,7 @@ export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedBut
assignNewKey(config, settingName, pressedButton, previousBind);
} else {
const nextKey = deepCopy(newBind);
if (prevKey.key === nextKey.key) {
if (prevKey.key === nextKey.key && prevKey.from) {
// special case when back to back and not enough info to get back to previous button
const toRestore = getKeyAndSettingNameFromCurrentKeysWithAction(config, prevKey.from.action, settingName.includes("ALT_"));
config.custom[prevKey.key] = prevKey.from.action;

View File

@ -50,12 +50,12 @@ describe('Test Keyboard', () => {
});
it('Check key for currenly Assigned to setting name', () => {
const settingName = SettingInterfaceKeyboard.Button_Left;
const { key } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
const {key} = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
expect(key).toEqual('KEY_ARROW_LEFT');
});
it('Check key for currenly Assigned to setting name alt', () => {
const settingName = SettingInterfaceKeyboard.Alt_Button_Left;
const { key } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
const {key} = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
expect(key).toEqual('KEY_Q');
});
it('Check icon for currenly Assigned to key code', () => {
@ -603,7 +603,6 @@ describe('Test Keyboard', () => {
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");
@ -626,6 +625,146 @@ describe('Test Keyboard', () => {
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].icon).toEqual(undefined);
expect(config.custom["KEY_S"]).toEqual(-1);
swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Down, Phaser.Input.Keyboard.KeyCodes.S);
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(Button.DOWN);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Down].icon).toEqual("T_S_Key_Dark.png");
expect(config.custom["KEY_S"]).toEqual(Button.DOWN);
let keyDown = Phaser.Input.Keyboard.KeyCodes.S;
let key = getKeyFromMapping(config, keyDown);
let buttonDown = config.custom[key];
expect(buttonDown).toEqual(Button.DOWN);
keyDown = Phaser.Input.Keyboard.KeyCodes.Z;
key = getKeyFromMapping(config, keyDown);
buttonDown = config.custom[key];
expect(buttonDown).toEqual(Button.UP);
});
it("test keyboard listener", () => {
const keyDown = Phaser.Input.Keyboard.KeyCodes.S;
const key = getKeyFromMapping(config, keyDown);
const buttonDown = config.custom[key];
expect(buttonDown).toEqual(Button.DOWN);
});
it("another test with 2 delete", () => {
config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up] = {
"key": "KEY_Z",
"isAlt": true,
"action": -1,
"from": {
"key": "KEY_Z",
"isAlt": true,
"action": 0,
"icon": "T_Z_Key_Dark.png"
},
"latestIsDeleted": true
};
regenerateCustom(config);
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);
swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Down, Phaser.Input.Keyboard.KeyCodes.S);
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);
});
it("another test with 2 delete", () => {
config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up] = {
"key": "KEY_Z",
"isAlt": true,
"action": -1,
"from": {
"key": "KEY_Z",
"isAlt": true,
"action": 0,
"icon": "T_Z_Key_Dark.png"
},
"latestIsDeleted": true
};
regenerateCustom(config);
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);
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);
let keyDown = Phaser.Input.Keyboard.KeyCodes.S;
let key = getKeyFromMapping(config, keyDown);
let buttonDown = config.custom[key];
expect(buttonDown).toEqual(Button.DOWN);
keyDown = Phaser.Input.Keyboard.KeyCodes.Z;
key = getKeyFromMapping(config, keyDown);
buttonDown = config.custom[key];
expect(buttonDown).toEqual(Button.UP);
});
it("another test with 2 delete part 2", () => {
config.currentKeys = {
"ALT_BUTTON_UP": {
"key": "KEY_Z",
"isAlt": true,
"action": -1,
"from": {
"key": "KEY_Z",
"isAlt": true,
"action": 0,
"icon": "T_Z_Key_Dark.png"
},
"latestIsDeleted": true
},
"ALT_BUTTON_DOWN": {
"key": "KEY_S",
"isAlt": true,
"action": -1,
"from": {
"key": "KEY_S",
"isAlt": true,
"action": 1,
"icon": "T_S_Key_Dark.png"
},
"latestIsDeleted": true
},
}
regenerateCustom(config);
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);
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);
const keyDown = Phaser.Input.Keyboard.KeyCodes.Z;
const key = getKeyFromMapping(config, keyDown);
const buttonDown = config.custom[key];
expect(buttonDown).toEqual(Button.UP);
});
})
;