fix back to back button binding
parent
b5841471d2
commit
ea4372e1f3
|
@ -55,10 +55,7 @@ function getKeyAndSettingNameFromCurrentKeysWithAction(config, _action, alt: boo
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getKeyAndActionFromCurrentKeysWithSettingName(config, settingName) {
|
export function getKeyAndActionFromCurrentKeysWithSettingName(config, settingName) {
|
||||||
for (const _settingName of Object.keys(config.currentKeys)) {
|
return config.currentKeys[settingName];
|
||||||
if (_settingName === settingName) return config.currentKeys[_settingName];
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton) {
|
export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton) {
|
||||||
|
@ -69,27 +66,49 @@ export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedB
|
||||||
|
|
||||||
export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedButton): void {
|
export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedButton): void {
|
||||||
const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
|
const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
|
||||||
|
const prevKey = deepCopy(previousBind);
|
||||||
const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton);
|
const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton);
|
||||||
config.custom[previousBind.key] = newBind.action;
|
const nextKey = deepCopy(newBind);
|
||||||
config.custom[newBind.key] = previousBind.action;
|
if (prevKey.key === nextKey.key) {
|
||||||
config.icons[previousBind.key] = newBind.icon;
|
// special case when back to back and not enough info to get back to previous button
|
||||||
config.icons[newBind.key] = previousBind.icon;
|
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);
|
reloadCurrentKeys(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function reloadCurrentKeys(config): void {
|
export function reloadCurrentKeys(config): void {
|
||||||
// need to rework this to include keys that were not there at the begining
|
// 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)) {
|
for (const key of Object.keys(config.setting)) {
|
||||||
const settingName = config.setting[key];
|
const settingName = config.setting[key];
|
||||||
const action = config.custom[key];
|
const action = config.custom[key];
|
||||||
const icon = config.icons[key];
|
const icon = config.icons[key];
|
||||||
currentKeys[settingName] = {
|
if (!currentKeys[settingName]) currentKeys[settingName] = {};
|
||||||
key,
|
currentKeys[settingName].key = key;
|
||||||
action,
|
currentKeys[settingName].action = action;
|
||||||
icon,
|
currentKeys[settingName].icon = icon;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
config.currentKeys = deepCopy(currentKeys);
|
config.currentKeys = deepCopy(currentKeys);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import {
|
||||||
getKeyAndActionFromCurrentKeysWithSettingName,
|
getKeyAndActionFromCurrentKeysWithSettingName,
|
||||||
getKeyForSettingName,
|
getKeyForSettingName,
|
||||||
getKeyFromMapping,
|
getKeyFromMapping,
|
||||||
getKeyWithAction, initCurrentKeys,
|
getKeyWithAction,
|
||||||
reloadCurrentKeys,
|
reloadCurrentKeys,
|
||||||
swapCurrentKeys,
|
swapCurrentKeys,
|
||||||
} from "#app/configs/gamepad-utils";
|
} from "#app/configs/gamepad-utils";
|
||||||
|
@ -23,6 +23,7 @@ describe('Test Keyboard', () => {
|
||||||
const temp = {...cfg_gamepad_example}
|
const temp = {...cfg_gamepad_example}
|
||||||
config = deepCopy(temp);
|
config = deepCopy(temp);
|
||||||
config.custom = {...config.default}
|
config.custom = {...config.default}
|
||||||
|
config.ogIcons = {...config.icons}
|
||||||
reloadCurrentKeys(config);
|
reloadCurrentKeys(config);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -162,5 +163,24 @@ describe('Test Keyboard', () => {
|
||||||
expect(iconA).toEqual('T_X_B_Color_Alt.png');
|
expect(iconA).toEqual('T_X_B_Color_Alt.png');
|
||||||
expect(iconB).toEqual('T_X_A_Color_Alt.png');
|
expect(iconB).toEqual('T_X_A_Color_Alt.png');
|
||||||
expect(iconC).toEqual('T_X_X_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');
|
||||||
});
|
});
|
||||||
});
|
});
|
Loading…
Reference in New Issue