fix some small bugs - still need to fix the latest test

pull/685/head
Greenlamp 2024-05-14 17:18:31 +02:00
parent a2eab6259b
commit dfd73c3616
5 changed files with 191 additions and 21 deletions

View File

@ -69,14 +69,32 @@ export function assignNewKey(config: InterfaceConfig, settingName, pressedButton
const icon = config.ogIcons[key]; const icon = config.ogIcons[key];
config.icons[previousBind.key] = icon; config.icons[previousBind.key] = icon;
config.currentKeys[settingName].icon = icon; config.currentKeys[settingName].icon = icon;
config.custom[key] = previousBind.action;
config.custom[key] = previousBind.action !== -1 ? previousBind.action : previousBind.from.action;
config.custom[previousBind.key] = -1;
config.currentKeys[settingName].replacedBy = key;
delete config.currentKeys[settingName].from
} }
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 prevKey = deepCopy(previousBind);
const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton); const newBind = getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton);
if (!newBind) { if (newBind && previousBind.action === -1) {
//special case when rebinding deleted key with already assigned key
const toRestore = deepCopy(newBind);
config.custom[newBind.key] = prevKey.from.action;
config.icons[prevKey.key] = newBind.icon;
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].isDeleted = true;
config.currentKeys[settingName].replacedBy = toRestore.key;
} else if (!newBind) {
assignNewKey(config, settingName, pressedButton, previousBind); assignNewKey(config, settingName, pressedButton, previousBind);
} else { } else {
const nextKey = deepCopy(newBind); const nextKey = deepCopy(newBind);
@ -96,7 +114,7 @@ export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedBut
config.custom[newBind.key] = previousBind.action; config.custom[newBind.key] = previousBind.action;
config.icons[previousBind.key] = newBind.icon; config.icons[previousBind.key] = newBind.icon;
config.icons[newBind.key] = previousBind.icon; config.icons[newBind.key] = previousBind.icon;
const nextSettingName = getKeyAndSettingNameFromCurrentKeysWithAction(config, newBind.action).settingName; const nextSettingName = getKeyAndSettingNameFromCurrentKeysWithAction(config, newBind.action, newBind.isAlt).settingName;
config.currentKeys[settingName].from = prevKey; config.currentKeys[settingName].from = prevKey;
config.currentKeys[nextSettingName].from = nextKey; config.currentKeys[nextSettingName].from = nextKey;
} }
@ -112,10 +130,27 @@ export function reloadCurrentKeys(config): void {
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];
if (currentKeys[settingName]?.latestReplacedBy) {
console.log('');
}
if (!currentKeys[settingName]) currentKeys[settingName] = {}; if (!currentKeys[settingName]) currentKeys[settingName] = {};
currentKeys[settingName].key = key; currentKeys[settingName].key = key;
currentKeys[settingName].action = action === undefined ? currentKeys[settingName].action : action; currentKeys[settingName].isAlt = settingName.includes("ALT_");
const previousAction = config.custom[currentKeys[settingName].replacedBy]
if (action === -1 && previousAction !== undefined) {
currentKeys[settingName].action = previousAction;
currentKeys[settingName].icon = icon; currentKeys[settingName].icon = icon;
currentKeys[settingName].latestReplacedBy = config.currentKeys[settingName].replacedBy
delete currentKeys[settingName].replacedBy;
} else if (currentKeys[settingName].isDeleted) {
currentKeys[settingName].action = -1;
currentKeys[settingName].icon = undefined;
currentKeys[settingName].latestIsDeleted = config.currentKeys[settingName].isDeleted
delete currentKeys[settingName].isDeleted;
} else {
currentKeys[settingName].action = action;
currentKeys[settingName].icon = action === -1 ? undefined : icon;
}
} }
config.currentKeys = deepCopy(currentKeys); config.currentKeys = deepCopy(currentKeys);
} }
@ -123,14 +158,22 @@ export function reloadCurrentKeys(config): void {
export function regenerateCustom(config): void { export function regenerateCustom(config): void {
const custom = deepCopy(config.custom); const custom = deepCopy(config.custom);
for (const settingName of Object.keys(config.currentKeys)) { for (const settingName of Object.keys(config.currentKeys)) {
const {key, action} = config.currentKeys[settingName]; const {key, action, latestReplacedBy, latestIsDeleted} = config.currentKeys[settingName];
if (latestReplacedBy) {
custom[key] = -1;
custom[latestReplacedBy] = action;
} else if (!latestIsDeleted) {
custom[key] = action; custom[key] = action;
} }
}
config.custom = deepCopy(custom); config.custom = deepCopy(custom);
} }
export function deleteBind(config, settingName): void { export function deleteBind(config, settingName): void {
const { key } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); const { key } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName);
const prev = deepCopy(config.currentKeys[settingName]);
delete config.currentKeys[settingName].icon delete config.currentKeys[settingName].icon
config.custom[key] = undefined config.currentKeys[settingName].from = prev;
config.custom[key] = -1;
reloadCurrentKeys(config);
} }

View File

@ -291,12 +291,12 @@ describe('Test Keyboard', () => {
deleteBind(config, settingNameA) deleteBind(config, settingNameA)
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_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].action).toEqual(-1);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual(undefined); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual(undefined);
}) })
it('Delete bind then asign', () => { it('Delete bind then asign not existing button', () => {
const settingNameA = SettingInterfaceKeyboard.Alt_Button_Up; const settingNameA = SettingInterfaceKeyboard.Alt_Button_Up;
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z"); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_Z");
@ -305,14 +305,137 @@ describe('Test Keyboard', () => {
deleteBind(config, settingNameA) deleteBind(config, settingNameA)
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].key).toEqual("KEY_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].action).toEqual(-1);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual(undefined); 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.B); 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].key).toEqual("KEY_Z");
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].action).toEqual(Button.UP); 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"); expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Up].icon).toEqual("T_B_Key_Dark.png");
expect(config.custom["KEY_B"]).toEqual(Button.UP);
expect(config.custom["KEY_Z"]).toEqual(-1);
})
it('swap bind, then Delete bind then assign 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");
expect(config.custom["KEY_Z"]).toEqual(Button.UP);
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");
expect(config.custom["KEY_B"]).toEqual(Button.UP);
expect(config.custom["KEY_Z"]).toEqual(-1);
deleteBind(config, settingNameA);
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.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");
expect(config.custom["KEY_B"]).toEqual(Button.UP);
})
it('Delete bind then asign not already existing button', () => {
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");
expect(config.custom["KEY_Z"]).toEqual(Button.UP);
deleteBind(config, settingNameA)
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.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].key).toEqual("KEY_L");
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].action).toEqual(Button.CYCLE_ABILITY);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].icon).toEqual("T_L_Key_Dark.png");
expect(config.custom["KEY_Z"]).toEqual(-1);
expect(config.custom["KEY_L"]).toEqual(Button.CYCLE_ABILITY);
swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.L);
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_L_Key_Dark.png");
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].key).toEqual("KEY_L");
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].action).toEqual(-1);
expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Cycle_Ability].icon).toEqual(undefined);
expect(config.custom["KEY_Z"]).toEqual(-1);
expect(config.custom["KEY_L"]).toEqual(Button.UP);
})
it('Custom scenario 2, regenerate customs when init key is not from setting', () => {
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);
swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.T);
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_T_Key_Dark.png");
expect(config.custom["KEY_Z"]).toEqual(-1);
expect(config.custom["KEY_T"]).toEqual(Button.UP);
})
it('change alt to unknown touch than another one alt with another unknown touch', () => {
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);
swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Up, Phaser.Input.Keyboard.KeyCodes.T);
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_T_Key_Dark.png");
expect(config.custom["KEY_Z"]).toEqual(-1);
expect(config.custom["KEY_T"]).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");
swapCurrentKeys(config, SettingInterfaceKeyboard.Alt_Button_Down, Phaser.Input.Keyboard.KeyCodes.U);
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_U_Key_Dark.png");
expect(config.custom["KEY_S"]).toEqual(-1);
expect(config.custom["KEY_U"]).toEqual(Button.DOWN);
expect(config.custom["KEY_Z"]).toEqual(-1);
expect(config.custom["KEY_T"]).toEqual(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_T_Key_Dark.png");
}) })
}); });

View File

@ -2,8 +2,7 @@ import BattleScene from "../../battle-scene";
import AbstractBindingUiHandler from "../settings/abrast-binding-ui-handler"; import AbstractBindingUiHandler from "../settings/abrast-binding-ui-handler";
import {Mode} from "../ui"; import {Mode} from "../ui";
import { import {
getKeyAndActionFromCurrentKeysWithPressedButton, getKeyAndActionFromCurrentKeysWithSettingName, getKeyFromMapping, regenerateCustom,
getKeyAndActionFromCurrentKeysWithSettingName, getKeyFromMapping,
} from "#app/configs/gamepad-utils"; } from "#app/configs/gamepad-utils";
@ -33,6 +32,7 @@ export default class GamepadBindingUiHandler extends AbstractBindingUiHandler {
const activeConfig = this.scene.inputController.getActiveConfig(); const activeConfig = this.scene.inputController.getActiveConfig();
this.scene.inputController.swapBinding(activeConfig, this.target, this.buttonPressed) this.scene.inputController.swapBinding(activeConfig, this.target, this.buttonPressed)
this.scene.gameData.saveCustomMapping(this.scene.inputController?.chosenGamepad, activeConfig.currentKeys); this.scene.gameData.saveCustomMapping(this.scene.inputController?.chosenGamepad, activeConfig.currentKeys);
regenerateCustom(activeConfig);
return true; return true;
} }
} }

View File

@ -2,8 +2,7 @@ import BattleScene from "../../battle-scene";
import AbstractBindingUiHandler from "../settings/abrast-binding-ui-handler"; import AbstractBindingUiHandler from "../settings/abrast-binding-ui-handler";
import {Mode} from "../ui"; import {Mode} from "../ui";
import { import {
getKeyAndActionFromCurrentKeysWithPressedButton, getKeyAndActionFromCurrentKeysWithSettingName, getKeyFromMapping, regenerateCustom,
getKeyAndActionFromCurrentKeysWithSettingName, getKeyFromMapping,
} from "#app/configs/gamepad-utils"; } from "#app/configs/gamepad-utils";
@ -32,6 +31,7 @@ export default class KeyboardBindingUiHandler extends AbstractBindingUiHandler {
const activeConfig = this.scene.inputController.getActiveKeyboardConfig(); const activeConfig = this.scene.inputController.getActiveKeyboardConfig();
this.scene.inputController.swapBinding(activeConfig, this.target, this.buttonPressed) this.scene.inputController.swapBinding(activeConfig, this.target, this.buttonPressed)
this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, activeConfig.currentKeys); this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, activeConfig.currentKeys);
regenerateCustom(activeConfig);
return true; return true;
} }

View File

@ -26,13 +26,13 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl
onDeleteDown(): void { onDeleteDown(): void {
const cursor = this.cursor + this.scrollCursor; // Calculate the absolute cursor position. 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 selection = this.settingLabels[cursor].text;
const key = reverseValueToKeySetting(selection); const key = reverseValueToKeySetting(selection);
const setting = SettingKeyboard[key]; const setting = SettingKeyboard[key];
const activeConfig = this.getActiveConfig();
deleteBind(this.getActiveConfig(), setting); deleteBind(this.getActiveConfig(), setting);
need to handle the "no icon" this.saveCustomKeyboardMappingToLocalStorage(activeConfig);
console.log('setting:', setting); this.updateBindings();
} }
getActiveConfig(): InterfaceConfig { getActiveConfig(): InterfaceConfig {
@ -76,8 +76,12 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl
} }
saveSettingToLocalStorage(setting, cursor): void { saveCustomKeyboardMappingToLocalStorage(config): void {
if (this.settingDevice[setting] !== this.settingDevice.Default_Layout) this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, config.currentKeys);
this.scene.gameData.saveKeyboardSetting(setting, cursor) }
saveSettingToLocalStorage(settingName, cursor): void {
if (this.settingDevice[settingName] !== this.settingDevice.Default_Layout)
this.scene.gameData.saveKeyboardSetting(settingName, cursor)
} }
} }