fix back to back button binding

pull/685/head
Greenlamp 2024-05-14 02:18:39 +02:00
parent b5841471d2
commit ea4372e1f3
2 changed files with 54 additions and 15 deletions

View File

@ -55,10 +55,7 @@ function getKeyAndSettingNameFromCurrentKeysWithAction(config, _action, alt: boo
}
export function getKeyAndActionFromCurrentKeysWithSettingName(config, settingName) {
for (const _settingName of Object.keys(config.currentKeys)) {
if (_settingName === settingName) return config.currentKeys[_settingName];
}
return null;
return config.currentKeys[settingName];
}
export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton) {
@ -69,27 +66,49 @@ export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedB
export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedButton): void {
const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
const prevKey = deepCopy(previousBind);
const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton);
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 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);
const iconToRestore = config.icons[toRestore.key];
config.custom[prevKey.key] = prevKey.from.action;
config.icons[prevKey.key] = iconToRestore;
config.custom[toRestore.key] = prevKey.action;
config.icons[toRestore.key] = prevKey.icon;
config.currentKeys[settingName].from = prevKey;
config.currentKeys[toRestore.settingName].from = {
key: toRestore.key,
action: prevKey.from.action,
icon: iconToRestore,
};
} 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);
}
export function reloadCurrentKeys(config): void {
// need to rework this to include keys that were not there at the begining
const currentKeys = {};
const currentKeys = config.currentKeys ? deepCopy(config.currentKeys) : {};
for (const key of Object.keys(config.setting)) {
const settingName = config.setting[key];
const action = config.custom[key];
const icon = config.icons[key];
currentKeys[settingName] = {
key,
action,
icon,
}
if (!currentKeys[settingName]) currentKeys[settingName] = {};
currentKeys[settingName].key = key;
currentKeys[settingName].action = action;
currentKeys[settingName].icon = icon;
}
config.currentKeys = deepCopy(currentKeys);
}

View File

@ -6,7 +6,7 @@ import {
getKeyAndActionFromCurrentKeysWithSettingName,
getKeyForSettingName,
getKeyFromMapping,
getKeyWithAction, initCurrentKeys,
getKeyWithAction,
reloadCurrentKeys,
swapCurrentKeys,
} from "#app/configs/gamepad-utils";
@ -23,6 +23,7 @@ describe('Test Keyboard', () => {
const temp = {...cfg_gamepad_example}
config = deepCopy(temp);
config.custom = {...config.default}
config.ogIcons = {...config.icons}
reloadCurrentKeys(config);
});
@ -162,5 +163,24 @@ describe('Test Keyboard', () => {
expect(iconA).toEqual('T_X_B_Color_Alt.png');
expect(iconB).toEqual('T_X_A_Color_Alt.png');
expect(iconC).toEqual('T_X_X_Color_Alt.png');
expect(config.ogIcons["RC_S"]).toEqual("T_X_A_Color_Alt.png")
expect(config.ogIcons["RC_E"]).toEqual("T_X_B_Color_Alt.png")
expect(config.ogIcons["RC_N"]).toEqual("T_X_Y_Color_Alt.png")
expect(config.ogIcons["RC_W"]).toEqual("T_X_X_Color_Alt.png")
});
it('Check 2 swap back to back', () => {
swapCurrentKeys(config, SettingInterfaceGamepad.Button_Action, 1); // cancel
expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].key).toEqual("RC_S");
expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].action).toEqual(Button.CANCEL);
let iconA = getIconWithSettingName(config, SettingInterfaceGamepad.Button_Action);
expect(iconA).toEqual('T_X_B_Color_Alt.png');
swapCurrentKeys(config, SettingInterfaceGamepad.Button_Action, 0); // cancel
let iconB = getIconWithSettingName(config, SettingInterfaceGamepad.Button_Action);
expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].key).toEqual("RC_S");
expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].action).toEqual(Button.ACTION);
expect(iconB).toEqual('T_X_A_Color_Alt.png');
});
});