diff --git a/src/configs/gamepad-utils.ts b/src/configs/gamepad-utils.ts index 5142dcdec..b6968d369 100644 --- a/src/configs/gamepad-utils.ts +++ b/src/configs/gamepad-utils.ts @@ -1,20 +1,13 @@ import {InterfaceConfig} from "../inputs-controller"; import {Button} from "#app/enums/buttons"; - // Given a button index from an input event, return its naming from the mapping config -export function getKeyFromInputIndex(config: InterfaceConfig, index: number): String | null { +export function getKeyFromMapping(config: InterfaceConfig, index: number): String | null { for (const key of Object.keys(config.gamepadMapping)) { if (config.gamepadMapping[key] === index) return key; } return null; } -export function getKeyFromKeyboardKeyCode(config: InterfaceConfig, key): String | null { - for (const _key of Object.keys(config.gamepadMapping)) { - if (config.gamepadMapping[_key] === key) return _key; - } - return null; -} // Given a setting name, return the key assigned to it from the config file export function getKeyForSettingName(config: InterfaceConfig, settingName: string): String | null { @@ -24,36 +17,79 @@ export function getKeyForSettingName(config: InterfaceConfig, settingName: strin return null; } -// Given a Button, return the custom key assigned to it from the config file -export function getCurrenlyAssignedKeyToAction(config: InterfaceConfig, action: Button, alt: boolean = false): String | null { - // need to find a way to differentiate main/alt button - for (const key of Object.keys(config.custom)) { - if (config.custom[key] === action) return key; - } - return null; +// Given a setting name, return the key assigned to it from the config file +export function getIconForSettingName(config: InterfaceConfig, settingName: string): String | null { + const key = getKeyForSettingName(config, settingName); + return config.icons[key]; } -// Given a setting name, return the custom key for the default action from the config file -export function getCurrentlyAssignedToSettingName(config: InterfaceConfig, settingName: string): String { - const oldKey = getKeyForSettingName(config, settingName) - const action = config.default[oldKey]; - const key = getCurrenlyAssignedKeyToAction(config, action, settingName.includes("ALT_BUTTON_")); +// Given a Button, return the custom key assigned to it from the config file +export function getKeyWithAction(config: InterfaceConfig, action: Button, alt: boolean = false): String | null { + // need to find a way to differentiate main/alt button + const { key } = getKeyAndSettingNameFromCurrentKeysWithAction(config, action, alt); return key; } // Given a button index from an input event, return its icon from the config file -export function getCurrenlyAssignedIconFromInputIndex(config: InterfaceConfig, index: number): String { - const key = getKeyFromInputIndex(config, index); +export function getIconWithPressedButton(config: InterfaceConfig, pressedButton: number): String { + const key = getKeyFromMapping(config, pressedButton); return config.icons[key]; } -export function getCurrenlyAssignedIconFromKeyboardKeyCode(config: InterfaceConfig, key): String { - const _key = getKeyFromKeyboardKeyCode(config, key); - return config.icons[_key]; -} - // Given a setting name, return the icon currently assigned to this setting name -export function getCurrentlyAssignedIconToSettingName(config: InterfaceConfig, settingName: string): string { - const key = getCurrentlyAssignedToSettingName(config, settingName); - return config.icons[key]; +export function getIconWithSettingName(config: InterfaceConfig, settingName: string): string { + const { icon } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName) + return icon; } + +function getKeyAndSettingNameFromCurrentKeysWithAction(config, _action, alt: boolean = false) { + for (const _settingName of Object.keys(config.currentKeys)) { + if (alt && !_settingName.includes("ALT_")) continue; + if (config.currentKeys[_settingName].action === _action) return { + settingName: _settingName, + key: config.currentKeys[_settingName].key, + }; + } + return null; +} + +export function getKeyAndActionFromCurrentKeysWithSettingName(config, settingName) { + for (const _settingName of Object.keys(config.currentKeys)) { + if (_settingName === settingName) return config.currentKeys[_settingName]; + } + return null; +} + +export function getKeyAndActionFromCurrentKeysWithPressedButton(config, pressedButton) { + const key = getKeyFromMapping(config, pressedButton); + const settingName = Object.keys(config.currentKeys).find(_s => config.currentKeys[_s].key === key); + return getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); +} + +export function swapCurrentKeys(config: InterfaceConfig, settingName, pressedButton): void { + const previousBind = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); + 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; + reloadCurrentKeys(config); +} + + +export function reloadCurrentKeys(config): void { + // need to rework this to include keys that were not there at the begining + const currentKeys = {}; + debugger; + 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, + } + } + config.currentKeys = currentKeys; +} \ No newline at end of file diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index f5ff95e51..93010f668 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -7,18 +7,14 @@ import pad_dualshock from "./configs/pad_dualshock"; import {Button} from "./enums/buttons"; import {Mode} from "./ui/ui"; import SettingsGamepadUiHandler from "./ui/settings/settings-gamepad-ui-handler"; -import {SettingGamepad} from "./system/settings-gamepad"; -import { - getCurrenlyAssignedIconFromInputIndex, - getCurrentlyAssignedIconToSettingName, - getKeyFromInputIndex, - getCurrentlyAssignedToSettingName, - getCurrenlyAssignedIconFromKeyboardKeyCode, - getKeyFromKeyboardKeyCode -} from "./configs/gamepad-utils"; import SettingsKeyboardUiHandler from "./ui/settings/settings-keyboard-ui-handler"; import cfg_keyboard_azerty from "./configs/cfg_keyboard_azerty"; -import {SettingKeyboard} from "./system/settings-keyboard"; +import { + getKeyAndActionFromCurrentKeysWithPressedButton, + getKeyFromMapping, + reloadCurrentKeys, swapCurrentKeys +} from "#app/configs/gamepad-utils"; +import {deepCopy} from "./utils"; export interface GamepadMapping { [key: string]: number; @@ -340,8 +336,9 @@ export class InputsController { const allGamepads = this.getGamepadsName(); for (const gamepad of allGamepads) { const gamepadID = gamepad.toLowerCase(); - const config = this.getConfig(gamepadID); + const config = deepCopy(this.getConfig(gamepadID)); config.custom = this.configs[gamepad]?.custom || {...config.default}; + reloadCurrentKeys(config); this.configs[gamepad] = config; this.scene.gameData?.saveCustomMapping(this.chosenGamepad, this.configs[gamepad]?.custom); } @@ -350,8 +347,9 @@ export class InputsController { setupKeyboard(): void { for (const layout of ['default']) { - const config = this.getConfigKeyboard(layout); + const config = deepCopy(this.getConfigKeyboard(layout)); config.custom = this.keyboardConfigs[layout]?.custom || {...config.default}; + reloadCurrentKeys(config); this.keyboardConfigs[layout] = config; this.scene.gameData?.saveCustomKeyboardMapping(this.chosenKeyboard, this.keyboardConfigs[layout]?.custom); } @@ -383,7 +381,7 @@ export class InputsController { this.setupKeyboard(); if (this.keys.includes(keyDown)) return; this.keys.push(keyDown); - const key = getKeyFromKeyboardKeyCode(this.keyboardConfigs[this.chosenKeyboard], keyDown); + const key = getKeyFromMapping(this.keyboardConfigs[this.chosenKeyboard], keyDown); const buttonDown = this.keyboardConfigs[this.chosenKeyboard].custom[key]; this.lastSource = 'keyboard'; if (buttonDown !== undefined) { @@ -400,7 +398,7 @@ export class InputsController { this.keys = this.keys.filter(k => k !== keyDown); if (!this.keyboardConfigs[this.chosenKeyboard]?.padID) this.setupKeyboard(); - const key = getKeyFromKeyboardKeyCode(this.keyboardConfigs[this.chosenKeyboard], keyDown); + const key = getKeyFromMapping(this.keyboardConfigs[this.chosenKeyboard], keyDown); const buttonUp = this.keyboardConfigs[this.chosenKeyboard].custom[key]; if (buttonUp !== undefined) { this.events.emit('input_up', { @@ -427,7 +425,7 @@ export class InputsController { if (!this.chosenGamepad) this.setChosenGamepad(pad.id); if (!this.gamepadSupport || pad.id.toLowerCase() !== this.chosenGamepad.toLowerCase()) return; - const key = getKeyFromInputIndex(this.configs[pad.id], button.index); + const key = getKeyFromMapping(this.configs[pad.id], button.index); const buttonDown = this.configs[pad.id].custom[key]; this.lastSource = 'gamepad'; if (buttonDown !== undefined) { @@ -451,7 +449,7 @@ export class InputsController { gamepadButtonUp(pad: Phaser.Input.Gamepad.Gamepad, button: Phaser.Input.Gamepad.Button, value: number): void { if (!pad) return; if (!this.gamepadSupport || pad.id !== this.chosenGamepad) return; - const key = getKeyFromInputIndex(this.configs[pad.id], button.index); + const key = getKeyFromMapping(this.configs[pad.id], button.index); const buttonUp = this.configs[pad.id]?.custom[key]; if (buttonUp !== undefined) { this.events.emit('input_up', { @@ -747,66 +745,6 @@ export class InputsController { return null; } - /** - * Determines icon for a button pressed on the currently chosen gamepad based on its configuration. - * - * @param button The button for which to retrieve the label and icon. - * @returns Array Tuple containing the pad type and the currently assigned icon for the button index. - */ - getPressedButtonLabel(button: Phaser.Input.Gamepad.Button): [string, string] { - return [this.configs[this.chosenGamepad].padType, getCurrenlyAssignedIconFromInputIndex(this.configs[this.chosenGamepad], button.index)]; - } - - getPressedKeyLabel(key): string { - return getCurrenlyAssignedIconFromKeyboardKeyCode(this.keyboardConfigs[this.chosenKeyboard], key); - } - - /** - * Retrieves the currently assigned icon for a specific setting on the chosen gamepad. - * - * @param target The gamepad setting for which to retrieve the assigned icon. - * @returns string The icon assigned to the specified setting. - */ - getCurrentlyAssignedIconToDisplay(target: SettingGamepad): string { - return getCurrentlyAssignedIconToSettingName(this.configs[this.chosenGamepad], target); - } - - getKeyboardCurrentlyAssignedIconToDisplay(target: SettingKeyboard): string { - return getCurrentlyAssignedIconToSettingName(this.keyboardConfigs[this.chosenKeyboard], target); - } - - /** - * Swaps the binding of two controls on the chosen gamepad configuration. - * It temporarily pauses updates, swaps the key bindings, saves the new configuration, - * and then resumes updates after a short delay. - * - * @param settingName The name of the setting for which to swap the binding. - * @param pressedButton The button index whose binding is to be swapped. - */ - swapBinding(settingName, pressedButton): void { - this.pauseUpdate = true; - const keyTarget = getCurrentlyAssignedToSettingName(this.configs[this.chosenGamepad], settingName) - const keyNewBinding = getKeyFromInputIndex(this.configs[this.chosenGamepad], pressedButton); - const previousActionForThisNewBinding = this.configs[this.chosenGamepad].custom[keyNewBinding]; - const ActionForThisNewBinding = this.configs[this.chosenGamepad].custom[keyTarget]; - this.configs[this.chosenGamepad].custom[keyTarget] = previousActionForThisNewBinding; - this.configs[this.chosenGamepad].custom[keyNewBinding] = ActionForThisNewBinding; - this.scene.gameData.saveCustomMapping(this.chosenGamepad, this.configs[this.chosenGamepad].custom); - setTimeout(() => this.pauseUpdate = false, 500); - } - - swapKeyboardBinding(settingName, pressedButton): void { - this.pauseUpdate = true; - const keyTarget = getCurrentlyAssignedToSettingName(this.keyboardConfigs[this.chosenKeyboard], settingName) - const keyNewBinding = getKeyFromKeyboardKeyCode(this.keyboardConfigs[this.chosenKeyboard], pressedButton); - const previousActionForThisNewBinding = this.keyboardConfigs[this.chosenKeyboard].custom[keyNewBinding]; - const ActionForThisNewBinding = this.keyboardConfigs[this.chosenKeyboard].custom[keyTarget]; - this.keyboardConfigs[this.chosenKeyboard].custom[keyTarget] = previousActionForThisNewBinding; - this.keyboardConfigs[this.chosenKeyboard].custom[keyNewBinding] = ActionForThisNewBinding; - this.scene.gameData.saveCustomKeyboardMapping(this.chosenKeyboard, this.keyboardConfigs[this.chosenKeyboard].custom); - setTimeout(() => this.pauseUpdate = false, 500); - } - /** * Injects a custom mapping configuration into the gamepad configuration for a specific gamepad. * If the gamepad does not have an existing configuration, it initializes one first. @@ -822,4 +760,10 @@ export class InputsController { if (!this.keyboardConfigs[layout]) this.keyboardConfigs[layout] = {}; this.keyboardConfigs[layout].custom = customMappings; } + + swapBinding(config, settingName, pressedButton): void { + this.pauseUpdate = true; + swapCurrentKeys(config, settingName, pressedButton) + setTimeout(() => this.pauseUpdate = false, 500); + } } \ No newline at end of file diff --git a/src/test/cfg_gamepad_example.ts b/src/test/cfg_gamepad_example.ts new file mode 100644 index 000000000..739111288 --- /dev/null +++ b/src/test/cfg_gamepad_example.ts @@ -0,0 +1,97 @@ +import {Button} from "#app/enums/buttons"; + +export enum SettingInterfaceGamepad { + Default_Controller = "DEFAULT_CONTROLLER", + Gamepad_Support = "GAMEPAD_SUPPORT", + Button_Action = "BUTTON_ACTION", + Button_Cancel = "BUTTON_CANCEL", + Button_Menu = "BUTTON_MENU", + Button_Stats = "BUTTON_STATS", + Button_Cycle_Form = "BUTTON_CYCLE_FORM", + Button_Cycle_Shiny = "BUTTON_CYCLE_SHINY", + Button_Cycle_Gender = "BUTTON_CYCLE_GENDER", + Button_Cycle_Ability = "BUTTON_CYCLE_ABILITY", + Button_Cycle_Nature = "BUTTON_CYCLE_NATURE", + Button_Cycle_Variant = "BUTTON_CYCLE_VARIANT", + Button_Speed_Up = "BUTTON_SPEED_UP", + Button_Slow_Down = "BUTTON_SLOW_DOWN", + Button_Submit = "BUTTON_SUBMIT", +} + +/** + * Generic pad mapping + */ +const pad_xbox360 = { + padID: 'Xbox 360 controller (XInput STANDARD GAMEPAD)', + padType: 'xbox', + gamepadMapping: { + RC_S: 0, + RC_E: 1, + RC_W: 2, + RC_N: 3, + START: 9, + SELECT: 8, + LB: 4, + RB: 5, + LT: 6, + RT: 7, + LS: 10, + RS: 11, + LC_N: 12, + LC_S: 13, + LC_W: 14, + LC_E: 15 + }, + icons: { + RC_S: "T_X_A_Color_Alt.png", + RC_E: "T_X_B_Color_Alt.png", + RC_W: "T_X_X_Color_Alt.png", + RC_N: "T_X_Y_Color_Alt.png", + START: "T_X_X_Alt.png", + SELECT: "T_X_Share_Alt.png", + LB: "T_X_LB_Alt.png", + RB: "T_X_RB_Alt.png", + LT: "T_X_LT_Alt.png", + RT: "T_X_RT_Alt.png", + LS: "T_X_Left_Stick_Click_Alt_Alt.png", + RS: "T_X_Right_Stick_Click_Alt_Alt.png", + LC_N: "T_X_Dpad_Up_Alt.png", + LC_S: "T_X_Dpad_Down_Alt.png", + LC_W: "T_X_Dpad_Left_Alt.png", + LC_E: "T_X_Dpad_Right_Alt.png", + }, + setting: { + RC_S: SettingInterfaceGamepad.Button_Action, + RC_E: SettingInterfaceGamepad.Button_Cancel, + RC_W: SettingInterfaceGamepad.Button_Cycle_Nature, + RC_N: SettingInterfaceGamepad.Button_Cycle_Variant, + START: SettingInterfaceGamepad.Button_Menu, + SELECT: SettingInterfaceGamepad.Button_Stats, + LB: SettingInterfaceGamepad.Button_Cycle_Form, + RB: SettingInterfaceGamepad.Button_Cycle_Shiny, + LT: SettingInterfaceGamepad.Button_Cycle_Gender, + RT: SettingInterfaceGamepad.Button_Cycle_Ability, + LS: SettingInterfaceGamepad.Button_Speed_Up, + RS: SettingInterfaceGamepad.Button_Slow_Down, + }, + default: { + RC_S: Button.ACTION, //5 + RC_E: Button.CANCEL, // 6 + RC_W: Button.CYCLE_NATURE, + RC_N: Button.CYCLE_VARIANT, //14 + START: Button.MENU, //7 + SELECT: Button.STATS, //8 + LB: Button.CYCLE_FORM, + RB: Button.CYCLE_SHINY, + LT: Button.CYCLE_GENDER, + RT: Button.CYCLE_ABILITY, + LS: Button.SPEED_UP, + RS: Button.SLOW_DOWN, + LC_N: Button.UP, + LC_S: Button.DOWN, + LC_W: Button.LEFT, + LC_E: Button.RIGHT, + } +}; + +export default pad_xbox360; diff --git a/src/test/cfg_keyboard_example.ts b/src/test/cfg_keyboard_example.ts new file mode 100644 index 000000000..c97594013 --- /dev/null +++ b/src/test/cfg_keyboard_example.ts @@ -0,0 +1,319 @@ +import {Button} from "#app/enums/buttons"; + +export enum SettingInterfaceKeyboard { + Default_Layout = "DEFAULT_LAYOUT", + Button_Up = "BUTTON_UP", + Alt_Button_Up = "ALT_BUTTON_UP", + Button_Down = "BUTTON_DOWN", + Alt_Button_Down = "ALT_BUTTON_DOWN", + Button_Left = "BUTTON_LEFT", + Alt_Button_Left = "ALT_BUTTON_LEFT", + Button_Right = "BUTTON_RIGHT", + Alt_Button_Right = "ALT_BUTTON_RIGHT", + Button_Action = "BUTTON_ACTION", + Alt_Button_Action = "ALT_BUTTON_ACTION", + Button_Cancel = "BUTTON_CANCEL", + Alt_Button_Cancel = "ALT_BUTTON_CANCEL", + Button_Menu = "BUTTON_MENU", + Alt_Button_Menu = "ALT_BUTTON_MENU", + Button_Stats = "BUTTON_STATS", + Alt_Button_Stats = "ALT_BUTTON_STATS", + Button_Cycle_Form = "BUTTON_CYCLE_FORM", + Alt_Button_Cycle_Form = "ALT_BUTTON_CYCLE_FORM", + Button_Cycle_Shiny = "BUTTON_CYCLE_SHINY", + Alt_Button_Cycle_Shiny = "ALT_BUTTON_CYCLE_SHINY", + Button_Cycle_Gender = "BUTTON_CYCLE_GENDER", + Alt_Button_Cycle_Gender = "ALT_BUTTON_CYCLE_GENDER", + Button_Cycle_Ability = "BUTTON_CYCLE_ABILITY", + Alt_Button_Cycle_Ability = "ALT_BUTTON_CYCLE_ABILITY", + Button_Cycle_Nature = "BUTTON_CYCLE_NATURE", + Alt_Button_Cycle_Nature = "ALT_BUTTON_CYCLE_NATURE", + Button_Cycle_Variant = "BUTTON_CYCLE_VARIANT", + Alt_Button_Cycle_Variant = "ALT_BUTTON_CYCLE_VARIANT", + Button_Speed_Up = "BUTTON_SPEED_UP", + Alt_Button_Speed_Up = "ALT_BUTTON_SPEED_UP", + Button_Slow_Down = "BUTTON_SLOW_DOWN", + Alt_Button_Slow_Down = "ALT_BUTTON_SLOW_DOWN", + Button_Submit = "BUTTON_SUBMIT", + Alt_Button_Submit = "ALT_BUTTON_SUBMIT", +} + +const cfg_keyboard_azerty = { + padID: 'keyboard', + padType: 'default', + gamepadMapping: { + KEY_A: Phaser.Input.Keyboard.KeyCodes.A, + KEY_B: Phaser.Input.Keyboard.KeyCodes.B, + KEY_C: Phaser.Input.Keyboard.KeyCodes.C, + KEY_D: Phaser.Input.Keyboard.KeyCodes.D, + KEY_E: Phaser.Input.Keyboard.KeyCodes.E, + KEY_F: Phaser.Input.Keyboard.KeyCodes.F, + KEY_G: Phaser.Input.Keyboard.KeyCodes.G, + KEY_H: Phaser.Input.Keyboard.KeyCodes.H, + KEY_I: Phaser.Input.Keyboard.KeyCodes.I, + KEY_J: Phaser.Input.Keyboard.KeyCodes.J, + KEY_K: Phaser.Input.Keyboard.KeyCodes.K, + KEY_L: Phaser.Input.Keyboard.KeyCodes.L, + KEY_M: Phaser.Input.Keyboard.KeyCodes.M, + KEY_N: Phaser.Input.Keyboard.KeyCodes.N, + KEY_O: Phaser.Input.Keyboard.KeyCodes.O, + KEY_P: Phaser.Input.Keyboard.KeyCodes.P, + KEY_Q: Phaser.Input.Keyboard.KeyCodes.Q, + KEY_R: Phaser.Input.Keyboard.KeyCodes.R, + KEY_S: Phaser.Input.Keyboard.KeyCodes.S, + KEY_T: Phaser.Input.Keyboard.KeyCodes.T, + KEY_U: Phaser.Input.Keyboard.KeyCodes.U, + KEY_V: Phaser.Input.Keyboard.KeyCodes.V, + KEY_W: Phaser.Input.Keyboard.KeyCodes.W, + KEY_X: Phaser.Input.Keyboard.KeyCodes.X, + KEY_Y: Phaser.Input.Keyboard.KeyCodes.Y, + KEY_Z: Phaser.Input.Keyboard.KeyCodes.Z, + KEY_0: Phaser.Input.Keyboard.KeyCodes.ZERO, + KEY_1: Phaser.Input.Keyboard.KeyCodes.ONE, + KEY_2: Phaser.Input.Keyboard.KeyCodes.TWO, + KEY_3: Phaser.Input.Keyboard.KeyCodes.THREE, + KEY_4: Phaser.Input.Keyboard.KeyCodes.FOUR, + KEY_5: Phaser.Input.Keyboard.KeyCodes.FIVE, + KEY_6: Phaser.Input.Keyboard.KeyCodes.SIX, + KEY_7: Phaser.Input.Keyboard.KeyCodes.SEVEN, + KEY_8: Phaser.Input.Keyboard.KeyCodes.EIGHT, + KEY_9: Phaser.Input.Keyboard.KeyCodes.NINE, + KEY_CTRL: Phaser.Input.Keyboard.KeyCodes.CTRL, + KEY_DEL: Phaser.Input.Keyboard.KeyCodes.DELETE, + KEY_END: Phaser.Input.Keyboard.KeyCodes.END, + KEY_ENTER: Phaser.Input.Keyboard.KeyCodes.ENTER, + KEY_ESC: Phaser.Input.Keyboard.KeyCodes.ESC, + KEY_F1: Phaser.Input.Keyboard.KeyCodes.F1, + KEY_F2: Phaser.Input.Keyboard.KeyCodes.F2, + KEY_F3: Phaser.Input.Keyboard.KeyCodes.F3, + KEY_F4: Phaser.Input.Keyboard.KeyCodes.F4, + KEY_F5: Phaser.Input.Keyboard.KeyCodes.F5, + KEY_F6: Phaser.Input.Keyboard.KeyCodes.F6, + KEY_F7: Phaser.Input.Keyboard.KeyCodes.F7, + KEY_F8: Phaser.Input.Keyboard.KeyCodes.F8, + KEY_F9: Phaser.Input.Keyboard.KeyCodes.F9, + KEY_F10: Phaser.Input.Keyboard.KeyCodes.F10, + KEY_F11: Phaser.Input.Keyboard.KeyCodes.F11, + KEY_F12: Phaser.Input.Keyboard.KeyCodes.F12, + KEY_HOME: Phaser.Input.Keyboard.KeyCodes.HOME, + KEY_INSERT: Phaser.Input.Keyboard.KeyCodes.INSERT, + KEY_PAGE_DOWN: Phaser.Input.Keyboard.KeyCodes.PAGE_DOWN, + KEY_PAGE_UP: Phaser.Input.Keyboard.KeyCodes.PAGE_UP, + KEY_PLUS: Phaser.Input.Keyboard.KeyCodes.NUMPAD_ADD, // Assuming numpad plus + KEY_MINUS: Phaser.Input.Keyboard.KeyCodes.NUMPAD_SUBTRACT, // Assuming numpad minus + KEY_QUOTATION: Phaser.Input.Keyboard.KeyCodes.QUOTES, + KEY_SHIFT: Phaser.Input.Keyboard.KeyCodes.SHIFT, + KEY_SPACE: Phaser.Input.Keyboard.KeyCodes.SPACE, + KEY_TAB: Phaser.Input.Keyboard.KeyCodes.TAB, + KEY_TILDE: Phaser.Input.Keyboard.KeyCodes.BACKTICK, + KEY_ARROW_UP: Phaser.Input.Keyboard.KeyCodes.UP, + KEY_ARROW_DOWN: Phaser.Input.Keyboard.KeyCodes.DOWN, + KEY_ARROW_LEFT: Phaser.Input.Keyboard.KeyCodes.LEFT, + KEY_ARROW_RIGHT: Phaser.Input.Keyboard.KeyCodes.RIGHT, + KEY_LEFT_BRACKET: Phaser.Input.Keyboard.KeyCodes.OPEN_BRACKET, + KEY_RIGHT_BRACKET: Phaser.Input.Keyboard.KeyCodes.CLOSED_BRACKET, + KEY_SEMICOLON: Phaser.Input.Keyboard.KeyCodes.SEMICOLON, + KEY_BACKSPACE: Phaser.Input.Keyboard.KeyCodes.BACKSPACE, + KEY_ALT: Phaser.Input.Keyboard.KeyCodes.ALT + }, + icons: { + KEY_A: "T_A_Key_Dark.png", + KEY_B: "T_B_Key_Dark.png", + KEY_C: "T_C_Key_Dark.png", + KEY_D: "T_D_Key_Dark.png", + KEY_E: "T_E_Key_Dark.png", + KEY_F: "T_F_Key_Dark.png", + KEY_G: "T_G_Key_Dark.png", + KEY_H: "T_H_Key_Dark.png", + KEY_I: "T_I_Key_Dark.png", + KEY_J: "T_J_Key_Dark.png", + KEY_K: "T_K_Key_Dark.png", + KEY_L: "T_L_Key_Dark.png", + KEY_M: "T_M_Key_Dark.png", + KEY_N: "T_N_Key_Dark.png", + KEY_O: "T_O_Key_Dark.png", + KEY_P: "T_P_Key_Dark.png", + KEY_Q: "T_Q_Key_Dark.png", + KEY_R: "T_R_Key_Dark.png", + KEY_S: "T_S_Key_Dark.png", + KEY_T: "T_T_Key_Dark.png", + KEY_U: "T_U_Key_Dark.png", + KEY_V: "T_V_Key_Dark.png", + KEY_W: "T_W_Key_Dark.png", + KEY_X: "T_X_Key_Dark.png", + KEY_Y: "T_Y_Key_Dark.png", + KEY_Z: "T_Z_Key_Dark.png", + + KEY_0: "T_0_Key_Dark.png", + KEY_1: "T_1_Key_Dark.png", + KEY_2: "T_2_Key_Dark.png", + KEY_3: "T_3_Key_Dark.png", + KEY_4: "T_4_Key_Dark.png", + KEY_5: "T_5_Key_Dark.png", + KEY_6: "T_6_Key_Dark.png", + KEY_7: "T_7_Key_Dark.png", + KEY_8: "T_8_Key_Dark.png", + KEY_9: "T_9_Key_Dark.png", + + KEY_F1: "T_F1_Key_Dark.png", + KEY_F2: "T_F2_Key_Dark.png", + KEY_F3: "T_F3_Key_Dark.png", + KEY_F4: "T_F4_Key_Dark.png", + KEY_F5: "T_F5_Key_Dark.png", + KEY_F6: "T_F6_Key_Dark.png", + KEY_F7: "T_F7_Key_Dark.png", + KEY_F8: "T_F8_Key_Dark.png", + KEY_F9: "T_F9_Key_Dark.png", + KEY_F10: "T_F10_Key_Dark.png", + KEY_F11: "T_F11_Key_Dark.png", + KEY_F12: "T_F12_Key_Dark.png", + + + KEY_PAGE_DOWN: "T_PageDown_Key_Dark.png", + KEY_PAGE_UP: "T_PageUp_Key_Dark.png", + + KEY_CTRL: "T_Crtl_Key_Dark.png", + KEY_DEL: "T_Del_Key_Dark.png", + KEY_END: "T_End_Key_Dark.png", + KEY_ENTER: "T_Enter_Alt_Key_Dark.png", + KEY_ESC: "T_Esc_Key_Dark.png", + KEY_HOME: "T_Home_Key_Dark.png", + KEY_INSERT: "T_Ins_Key_Dark.png", + + KEY_PLUS: "T_Plus_Tall_Key_Dark.png", + KEY_MINUS: "T_Minus_Key_Dark.png", + KEY_QUOTATION: "T_Quotation_Key_Dark.png", + KEY_SHIFT: "T_Shift_Key_Dark.png", + + KEY_SPACE: "T_Space_Key_Dark.png", + KEY_TAB: "T_Tab_Key_Dark.png", + KEY_TILDE: "T_Tilde_Key_Dark.png", + + KEY_ARROW_UP: "T_Up_Key_Dark.png", + KEY_ARROW_DOWN: "T_Down_Key_Dark.png", + KEY_ARROW_LEFT: "T_Left_Key_Dark.png", + KEY_ARROW_RIGHT: "T_Right_Key_Dark.png", + + KEY_LEFT_BRACKET: "T_Brackets_L_Key_Dark.png", + KEY_RIGHT_BRACKET: "T_Brackets_R_Key_Dark.png", + + KEY_SEMICOLON: "T_Semicolon_Key_Dark.png", + + KEY_BACKSPACE: "T_Backspace_Alt_Key_Dark.png", + KEY_ALT: "T_Alt_Key_Dark.png" + }, + setting: { + KEY_ARROW_UP: SettingInterfaceKeyboard.Button_Up, + KEY_ARROW_DOWN: SettingInterfaceKeyboard.Button_Down, + KEY_ARROW_LEFT: SettingInterfaceKeyboard.Button_Left, + KEY_ARROW_RIGHT: SettingInterfaceKeyboard.Button_Right, + KEY_ENTER: SettingInterfaceKeyboard.Button_Submit, + KEY_SPACE: SettingInterfaceKeyboard.Button_Action, + KEY_BACKSPACE: SettingInterfaceKeyboard.Button_Cancel, + KEY_ESC: SettingInterfaceKeyboard.Button_Menu, + KEY_C: SettingInterfaceKeyboard.Button_Stats, + KEY_R: SettingInterfaceKeyboard.Button_Cycle_Shiny, + KEY_F: SettingInterfaceKeyboard.Button_Cycle_Form, + KEY_G: SettingInterfaceKeyboard.Button_Cycle_Gender, + KEY_E: SettingInterfaceKeyboard.Button_Cycle_Ability, + KEY_N: SettingInterfaceKeyboard.Button_Cycle_Nature, + KEY_V: SettingInterfaceKeyboard.Button_Cycle_Variant, + KEY_PLUS: SettingInterfaceKeyboard.Button_Speed_Up, + KEY_MINUS: SettingInterfaceKeyboard.Button_Slow_Down, + + KEY_Z: SettingInterfaceKeyboard.Alt_Button_Up, + KEY_S: SettingInterfaceKeyboard.Alt_Button_Down, + KEY_Q: SettingInterfaceKeyboard.Alt_Button_Left, + KEY_D: SettingInterfaceKeyboard.Alt_Button_Right, + KEY_CTRL: SettingInterfaceKeyboard.Alt_Button_Submit, + KEY_W: SettingInterfaceKeyboard.Alt_Button_Action, + KEY_X: SettingInterfaceKeyboard.Alt_Button_Cancel, + KEY_TAB: SettingInterfaceKeyboard.Alt_Button_Menu, + KEY_SHIFT: SettingInterfaceKeyboard.Alt_Button_Stats, + KEY_P: SettingInterfaceKeyboard.Alt_Button_Cycle_Shiny, + KEY_M: SettingInterfaceKeyboard.Alt_Button_Cycle_Form, + KEY_O: SettingInterfaceKeyboard.Alt_Button_Cycle_Gender, + KEY_L: SettingInterfaceKeyboard.Alt_Button_Cycle_Ability, + KEY_I: SettingInterfaceKeyboard.Alt_Button_Cycle_Nature, + KEY_K: SettingInterfaceKeyboard.Alt_Button_Cycle_Variant, + KEY_PAGE_UP: SettingInterfaceKeyboard.Alt_Button_Speed_Up, + KEY_PAGE_DOWN: SettingInterfaceKeyboard.Alt_Button_Slow_Down, + }, + default: { + KEY_ARROW_UP: Button.UP, + KEY_ARROW_DOWN: Button.DOWN, + KEY_ARROW_LEFT: Button.LEFT, + KEY_ARROW_RIGHT: Button.RIGHT, + KEY_ENTER: Button.SUBMIT, + KEY_SPACE: Button.ACTION, + KEY_BACKSPACE: Button.CANCEL, + KEY_ESC: Button.MENU, + KEY_C: Button.STATS, + KEY_R: Button.CYCLE_SHINY, + KEY_F: Button.CYCLE_FORM, + KEY_G: Button.CYCLE_GENDER, + KEY_E: Button.CYCLE_ABILITY, + KEY_N: Button.CYCLE_NATURE, + KEY_V: Button.CYCLE_VARIANT, + KEY_PLUS: Button.SPEED_UP, + KEY_MINUS: Button.SLOW_DOWN, + KEY_A: -1, + KEY_B: -1, + KEY_D: Button.RIGHT, + KEY_H: -1, + KEY_I: Button.CYCLE_NATURE, + KEY_J: -1, + KEY_K: Button.CYCLE_VARIANT, + KEY_L: Button.CYCLE_ABILITY, + KEY_M: Button.CYCLE_FORM, + KEY_O: Button.CYCLE_GENDER, + KEY_P: Button.CYCLE_SHINY, + KEY_Q: Button.LEFT, + KEY_S: Button.DOWN, + KEY_T: -1, + KEY_U: -1, + KEY_W: Button.ACTION, + KEY_X: Button.CANCEL, + KEY_Y: -1, + KEY_Z: Button.UP, + KEY_0: -1, + KEY_1: -1, + KEY_2: -1, + KEY_3: -1, + KEY_4: -1, + KEY_5: -1, + KEY_6: -1, + KEY_7: -1, + KEY_8: -1, + KEY_9: -1, + KEY_CTRL: Button.SUBMIT, + KEY_DEL: -1, + KEY_END: -1, + KEY_F1: -1, + KEY_F2: -1, + KEY_F3: -1, + KEY_F4: -1, + KEY_F5: -1, + KEY_F6: -1, + KEY_F7: -1, + KEY_F8: -1, + KEY_F9: -1, + KEY_F10: -1, + KEY_F11: -1, + KEY_F12: -1, + KEY_HOME: -1, + KEY_INSERT: -1, + KEY_PAGE_DOWN: Button.SLOW_DOWN, + KEY_PAGE_UP: Button.SPEED_UP, + KEY_QUOTATION: -1, + KEY_SHIFT: Button.STATS, + KEY_TAB: Button.MENU, + KEY_TILDE: -1, + KEY_LEFT_BRACKET: -1, + KEY_RIGHT_BRACKET: -1, + KEY_SEMICOLON: -1, + KEY_ALT: -1 + } +}; + +export default cfg_keyboard_azerty; diff --git a/src/test/gamepad_remaping.test.ts b/src/test/gamepad_remaping.test.ts new file mode 100644 index 000000000..5e717f6d3 --- /dev/null +++ b/src/test/gamepad_remaping.test.ts @@ -0,0 +1,166 @@ +import {beforeEach, expect, describe, it} from "vitest"; +import cfg_gamepad_example, {SettingInterfaceGamepad} from "./cfg_gamepad_example"; +import { + getIconWithPressedButton, + getIconWithSettingName, + getKeyAndActionFromCurrentKeysWithSettingName, + getKeyForSettingName, + getKeyFromMapping, + getKeyWithAction, initCurrentKeys, + reloadCurrentKeys, + swapCurrentKeys, +} from "#app/configs/gamepad-utils"; +import {Button} from "#app/enums/buttons"; + + +function deepCopy(config) { + return JSON.parse(JSON.stringify(config)); +} + +describe('Test Keyboard', () => { + let config; + beforeEach(() => { + const temp = {...cfg_gamepad_example} + config = deepCopy(temp); + config.custom = {...config.default} + reloadCurrentKeys(config); + }); + + it('Check if config is loaded', () => { + expect(config).not.toBeNull(); + }); + it('Check key for setting name', () => { + const settingName = SettingInterfaceGamepad.Button_Action; + const key = getKeyForSettingName(config, settingName); + expect(config.custom[key]).toEqual(Button.ACTION); + }); + it('Check key for Keyboard KeyCode', () => { + const key = getKeyFromMapping(config, 0); + expect(config.custom[key]).toEqual(Button.ACTION); + }); + it('Check key for currenly Assigned to action not alt', () => { + const key = getKeyWithAction(config, Button.ACTION, false); + expect(key).toEqual('RC_S'); + }); + it('Check key for currenly Assigned to setting name', () => { + const settingName = SettingInterfaceGamepad.Button_Action; + const { key } = getKeyAndActionFromCurrentKeysWithSettingName(config, settingName); + expect(key).toEqual('RC_S'); + }); + it('Check icon for currenly Assigned to key code', () => { + const icon = getIconWithPressedButton(config, 0); + expect(icon).toEqual('T_X_A_Color_Alt.png'); + }); + it('Check icon for currenly Assigned to setting name', () => { + const settingName = SettingInterfaceGamepad.Button_Action; + const icon = getIconWithSettingName(config, settingName); + expect(icon).toEqual('T_X_A_Color_Alt.png'); + }); + + + it('Check if current keys return the same', () => { + const settingNameA = SettingInterfaceGamepad.Button_Action; + const keyA = getKeyForSettingName(config, settingNameA); + const action = config.custom[keyA]; + expect(keyA).toEqual("RC_S"); + expect(action).toEqual(Button.ACTION); + + expect(config.currentKeys[settingNameA].key).toEqual(keyA); + expect(config.currentKeys[settingNameA].action).toEqual(action); + }); + + it('Check if new swap is working', () => { + const settingNameA = SettingInterfaceGamepad.Button_Action; + swapCurrentKeys(config, settingNameA, 1); + expect(config.currentKeys[settingNameA].key).toEqual("RC_S"); + expect(config.currentKeys[settingNameA].action).toEqual(Button.CANCEL); + }); + + it('Check if new double swap is working', () => { + const settingNameA = SettingInterfaceGamepad.Button_Action; + + swapCurrentKeys(config, settingNameA, 1); + expect(config.currentKeys[settingNameA].key).toEqual("RC_S"); + expect(config.currentKeys[settingNameA].action).toEqual(Button.CANCEL); + + swapCurrentKeys(config, settingNameA, 2); + expect(config.currentKeys[settingNameA].key).toEqual("RC_S"); + expect(config.currentKeys[settingNameA].action).toEqual(Button.CYCLE_NATURE); + }); + + it('Check if new triple swap is working', () => { + const settingNameA = SettingInterfaceGamepad.Button_Action; + const settingNameB = SettingInterfaceGamepad.Button_Cancel; + const settingNameC = SettingInterfaceGamepad.Button_Cycle_Nature; + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].key).toEqual("RC_S"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].action).toEqual(Button.ACTION); + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cancel].key).toEqual("RC_E"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cancel].action).toEqual(Button.CANCEL); + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cycle_Nature].key).toEqual("RC_W"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cycle_Nature].action).toEqual(Button.CYCLE_NATURE); + + let iconA = getIconWithSettingName(config, settingNameA); + let iconB = getIconWithSettingName(config, settingNameB); + let iconC = getIconWithSettingName(config, settingNameC); + expect(iconA).toEqual('T_X_A_Color_Alt.png'); + expect(iconB).toEqual('T_X_B_Color_Alt.png'); + expect(iconC).toEqual('T_X_X_Color_Alt.png'); + + 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); + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cancel].key).toEqual("RC_E"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cancel].action).toEqual(Button.ACTION); + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cycle_Nature].key).toEqual("RC_W"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cycle_Nature].action).toEqual(Button.CYCLE_NATURE); + + iconA = getIconWithSettingName(config, settingNameA); + iconB = getIconWithSettingName(config, settingNameB); + iconC = getIconWithSettingName(config, settingNameC); + 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'); + + swapCurrentKeys(config, SettingInterfaceGamepad.Button_Cancel, 2); // cycle nature + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].key).toEqual("RC_S"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].action).toEqual(Button.CANCEL); // 6 + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cancel].key).toEqual("RC_E"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cancel].action).toEqual(Button.CYCLE_NATURE); // 13 + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cycle_Nature].key).toEqual("RC_W"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cycle_Nature].action).toEqual(Button.ACTION); // 5 + + iconA = getIconWithSettingName(config, settingNameA); + iconB = getIconWithSettingName(config, settingNameB); + iconC = getIconWithSettingName(config, settingNameC); + expect(iconA).toEqual('T_X_B_Color_Alt.png'); + expect(iconB).toEqual('T_X_X_Color_Alt.png'); + expect(iconC).toEqual('T_X_A_Color_Alt.png'); + + swapCurrentKeys(config, SettingInterfaceGamepad.Button_Cycle_Nature, 1); // cancel + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].key).toEqual("RC_S"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Action].action).toEqual(Button.CANCEL); + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cancel].key).toEqual("RC_E"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cancel].action).toEqual(Button.ACTION); + + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cycle_Nature].key).toEqual("RC_W"); + expect(config.currentKeys[SettingInterfaceGamepad.Button_Cycle_Nature].action).toEqual(Button.CYCLE_NATURE); + + iconA = getIconWithSettingName(config, settingNameA); + iconB = getIconWithSettingName(config, settingNameB); + iconC = getIconWithSettingName(config, settingNameC); + 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'); + }); +}); \ No newline at end of file diff --git a/src/test/keyboard_remaping.test.ts b/src/test/keyboard_remaping.test.ts new file mode 100644 index 000000000..7c2705ca7 --- /dev/null +++ b/src/test/keyboard_remaping.test.ts @@ -0,0 +1,268 @@ +import {beforeEach, expect, describe, it} from "vitest"; +import cfg_keyboard_example, {SettingInterfaceKeyboard} from "#app/test/cfg_keyboard_example"; +import { + getIconWithPressedButton, + getIconWithSettingName, + getKeyAndActionFromCurrentKeysWithSettingName, + getKeyForSettingName, + getKeyFromMapping, + getKeyWithAction, + reloadCurrentKeys, + swapCurrentKeys, +} from "#app/configs/gamepad-utils"; +import {Button} from "#app/enums/buttons"; + + +function deepCopy(config) { + return JSON.parse(JSON.stringify(config)); +} + + +describe('Test Keyboard', () => { + let config; + beforeEach(() => { + config = deepCopy(cfg_keyboard_example); + config.custom = {...config.default} + reloadCurrentKeys(config); + }); + + it('Check if config is loaded', () => { + expect(config).not.toBeNull(); + }); + it('Check key for setting name', () => { + const settingName = SettingInterfaceKeyboard.Button_Left; + const key = getKeyForSettingName(config, settingName); + expect(config.custom[key]).toEqual(Button.LEFT); + }); + it('Check key for Keyboard KeyCode', () => { + const key = getKeyFromMapping(config, Phaser.Input.Keyboard.KeyCodes.LEFT); + expect(config.custom[key]).toEqual(Button.LEFT); + }); + it('Check key for currenly Assigned to action not alt', () => { + const key = getKeyWithAction(config, Button.LEFT, false); + expect(key).toEqual('KEY_ARROW_LEFT'); + }); + it('Check key for currenly Assigned to action alt', () => { + const key = getKeyWithAction(config, Button.LEFT, true); + expect(key).toEqual('KEY_Q'); + }); + it('Check key for currenly Assigned to setting name', () => { + const settingName = SettingInterfaceKeyboard.Button_Left; + 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); + expect(key).toEqual('KEY_Q'); + }); + it('Check icon for currenly Assigned to key code', () => { + const icon = getIconWithPressedButton(config, Phaser.Input.Keyboard.KeyCodes.LEFT); + expect(icon).toEqual('T_Left_Key_Dark.png'); + }); + it('Check icon for currenly Assigned to key code alt', () => { + const icon = getIconWithPressedButton(config, Phaser.Input.Keyboard.KeyCodes.Q); + expect(icon).toEqual('T_Q_Key_Dark.png'); + }); + it('Check icon for currenly Assigned to setting name', () => { + const settingName = SettingInterfaceKeyboard.Button_Left; + const icon = getIconWithSettingName(config, settingName); + expect(icon).toEqual('T_Left_Key_Dark.png'); + }); + it('Check icon for currenly Assigned to setting name alt', () => { + const settingName = SettingInterfaceKeyboard.Alt_Button_Left; + const icon = getIconWithSettingName(config, settingName); + expect(icon).toEqual('T_Q_Key_Dark.png'); + }); + + + it('Check if current keys return the same', () => { + const settingNameA = SettingInterfaceKeyboard.Button_Left; + const keyA = getKeyForSettingName(config, settingNameA); + const action = config.custom[keyA]; + expect(keyA).toEqual("KEY_ARROW_LEFT"); + expect(action).toEqual(Button.LEFT); + + expect(config.currentKeys[settingNameA].key).toEqual(keyA); + expect(config.currentKeys[settingNameA].action).toEqual(action); + }); + + it('Check if new swap is working', () => { + const settingNameA = SettingInterfaceKeyboard.Button_Left; + swapCurrentKeys(config, settingNameA, Phaser.Input.Keyboard.KeyCodes.RIGHT); + expect(config.currentKeys[settingNameA].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[settingNameA].action).toEqual(Button.RIGHT); + }); + + it('Check if new double swap is working', () => { + const settingNameA = SettingInterfaceKeyboard.Button_Left; + + swapCurrentKeys(config, settingNameA, Phaser.Input.Keyboard.KeyCodes.RIGHT); + expect(config.currentKeys[settingNameA].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[settingNameA].action).toEqual(Button.RIGHT); + + swapCurrentKeys(config, settingNameA, Phaser.Input.Keyboard.KeyCodes.UP); + expect(config.currentKeys[settingNameA].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[settingNameA].action).toEqual(Button.UP); + }); + + it('Check if new triple swap is working', () => { + const settingNameA = SettingInterfaceKeyboard.Button_Left; + const settingNameB = SettingInterfaceKeyboard.Button_Action; + const settingNameC = SettingInterfaceKeyboard.Button_Right; + const settingNameD = SettingInterfaceKeyboard.Button_Up; + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].action).toEqual(Button.LEFT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Action].key).toEqual("KEY_SPACE"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Action].action).toEqual(Button.ACTION); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.UP); + + let iconA = getIconWithSettingName(config, settingNameA); + let iconB = getIconWithSettingName(config, settingNameB); + let iconC = getIconWithSettingName(config, settingNameC); + let iconD = getIconWithSettingName(config, settingNameD); + expect(iconA).toEqual('T_Left_Key_Dark.png'); + expect(iconB).toEqual('T_Space_Key_Dark.png'); + expect(iconC).toEqual('T_Right_Key_Dark.png'); + expect(iconD).toEqual('T_Up_Key_Dark.png'); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Button_Left, Phaser.Input.Keyboard.KeyCodes.RIGHT); // left->RIGHT, right->LEFT + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].action).toEqual(Button.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Action].key).toEqual("KEY_SPACE"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Action].action).toEqual(Button.ACTION); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.LEFT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.UP); + + iconA = getIconWithSettingName(config, settingNameA); + iconB = getIconWithSettingName(config, settingNameB); + iconC = getIconWithSettingName(config, settingNameC); + iconD = getIconWithSettingName(config, settingNameD); + expect(iconA).toEqual('T_Right_Key_Dark.png'); + expect(iconB).toEqual('T_Space_Key_Dark.png'); + expect(iconC).toEqual('T_Left_Key_Dark.png'); + expect(iconD).toEqual('T_Up_Key_Dark.png'); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Button_Action, Phaser.Input.Keyboard.KeyCodes.UP); // action->UP + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].action).toEqual(Button.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Action].key).toEqual("KEY_SPACE"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Action].action).toEqual(Button.UP) + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.LEFT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.ACTION); + + iconA = getIconWithSettingName(config, settingNameA); + iconB = getIconWithSettingName(config, settingNameB); + iconC = getIconWithSettingName(config, settingNameC); + iconD = getIconWithSettingName(config, settingNameD); + expect(iconA).toEqual('T_Right_Key_Dark.png'); + expect(iconB).toEqual('T_Up_Key_Dark.png'); + expect(iconC).toEqual('T_Left_Key_Dark.png'); + expect(iconD).toEqual('T_Space_Key_Dark.png'); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Button_Right, Phaser.Input.Keyboard.KeyCodes.UP); // right->UP, action->LEFT + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].action).toEqual(Button.RIGHT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Action].key).toEqual("KEY_SPACE"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Action].action).toEqual(Button.UP) + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.ACTION); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.LEFT); + + iconA = getIconWithSettingName(config, settingNameA); + iconB = getIconWithSettingName(config, settingNameB); + iconC = getIconWithSettingName(config, settingNameC); + iconD = getIconWithSettingName(config, settingNameD); + expect(iconA).toEqual('T_Right_Key_Dark.png'); + expect(iconB).toEqual('T_Up_Key_Dark.png'); + expect(iconC).toEqual('T_Space_Key_Dark.png'); + expect(iconD).toEqual('T_Left_Key_Dark.png'); + }); + + + it('Swap alt with another main', () => { + const settingNameA = SettingInterfaceKeyboard.Button_Left; + const settingNameB = SettingInterfaceKeyboard.Alt_Button_Right; + const settingNameC = SettingInterfaceKeyboard.Button_Up; + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].action).toEqual(Button.LEFT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].icon).toEqual("T_Left_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].key).toEqual("KEY_D"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].icon).toEqual("T_D_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].icon).toEqual("T_Up_Key_Dark.png"); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Button_Left, Phaser.Input.Keyboard.KeyCodes.D); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].icon).toEqual("T_D_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Left].key).toEqual("KEY_Q"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Left].action).toEqual(Button.LEFT); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Left].icon).toEqual("T_Q_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].icon).toEqual("T_Right_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].key).toEqual("KEY_D"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].action).toEqual(Button.LEFT); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].icon).toEqual("T_Left_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].icon).toEqual("T_Up_Key_Dark.png"); + + swapCurrentKeys(config, SettingInterfaceKeyboard.Button_Up, Phaser.Input.Keyboard.KeyCodes.LEFT); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].key).toEqual("KEY_ARROW_LEFT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].action).toEqual(Button.UP); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Left].icon).toEqual("T_Up_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Left].key).toEqual("KEY_Q"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Left].action).toEqual(Button.LEFT); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Left].icon).toEqual("T_Q_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].key).toEqual("KEY_ARROW_RIGHT"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Right].icon).toEqual("T_Right_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].key).toEqual("KEY_D"); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].action).toEqual(Button.LEFT); + expect(config.currentKeys[SettingInterfaceKeyboard.Alt_Button_Right].icon).toEqual("T_Left_Key_Dark.png"); + + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].key).toEqual("KEY_ARROW_UP"); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].action).toEqual(Button.RIGHT); + expect(config.currentKeys[SettingInterfaceKeyboard.Button_Up].icon).toEqual("T_D_Key_Dark.png"); + }) +}); \ No newline at end of file diff --git a/src/ui/settings/abrast-binding-ui-handler.ts b/src/ui/settings/abrast-binding-ui-handler.ts index 03139d71b..4d73bea18 100644 --- a/src/ui/settings/abrast-binding-ui-handler.ts +++ b/src/ui/settings/abrast-binding-ui-handler.ts @@ -115,7 +115,7 @@ export default abstract class AbstractBindingUiHandler extends UiHandler { this.getUi().bringToTop(this.actionsContainer); this.optionSelectContainer.setVisible(true); - setTimeout(() => this.listening = true, 300); + setTimeout(() => this.listening = true, 100); return true; } diff --git a/src/ui/settings/abstract-settings-ui-handler.ts b/src/ui/settings/abstract-settings-ui-handler.ts index 4b87a7214..e92d3dc68 100644 --- a/src/ui/settings/abstract-settings-ui-handler.ts +++ b/src/ui/settings/abstract-settings-ui-handler.ts @@ -4,8 +4,11 @@ import {Mode} from "../ui"; import {InterfaceConfig} from "../../inputs-controller"; import {addWindow} from "../ui-theme"; import {addTextObject, TextStyle} from "../text"; -import {getCurrentlyAssignedIconToSettingName, getKeyForSettingName} from "../../configs/gamepad-utils"; import {Button} from "../../enums/buttons"; +import { + getKeyAndActionFromCurrentKeysWithSettingName, + getKeyForSettingName +} from "#app/configs/gamepad-utils"; export interface InputsIcons { [key: string]: Phaser.GameObjects.Sprite; @@ -238,9 +241,10 @@ export default abstract class AbstractSettingsUiUiHandler extends UiHandler { if (!activeConfig.custom) return; // For each element in the binding settings, update the icon according to the current assignment. + debugger; for (const elm of this.bindingSettings) { - const key = getKeyForSettingName(activeConfig, elm); // Get the key for the setting name. - const icon = getCurrentlyAssignedIconToSettingName(activeConfig, elm); // Fetch the currently assigned icon for the setting. + // const key = getKeyForSettingName(activeConfig, elm); // Get the key for the setting name. + const {key, icon} = getKeyAndActionFromCurrentKeysWithSettingName(activeConfig, elm); this.inputsIcons[key].setFrame(icon); // Set the icon frame to the inputs icon object. } diff --git a/src/ui/settings/gamepad-binding-ui-handler.ts b/src/ui/settings/gamepad-binding-ui-handler.ts index 684ce93b4..0519e7e44 100644 --- a/src/ui/settings/gamepad-binding-ui-handler.ts +++ b/src/ui/settings/gamepad-binding-ui-handler.ts @@ -1,6 +1,10 @@ import BattleScene from "../../battle-scene"; import AbstractBindingUiHandler from "../settings/abrast-binding-ui-handler"; import {Mode} from "../ui"; +import { + getKeyAndActionFromCurrentKeysWithPressedButton, + getKeyAndActionFromCurrentKeysWithSettingName, +} from "#app/configs/gamepad-utils"; export default class GamepadBindingUiHandler extends AbstractBindingUiHandler { @@ -16,14 +20,18 @@ export default class GamepadBindingUiHandler extends AbstractBindingUiHandler { // Check conditions before processing the button press. if (!this.listening || pad.id !== this.scene.inputController?.chosenGamepad || blacklist.includes(button.index) || this.buttonPressed !== null) return; this.buttonPressed = button.index; - const [type, buttonIcon] = this.scene.inputController.getPressedButtonLabel(button); + const activeConfig = this.scene.inputController.getActiveConfig(); + const type = activeConfig.padType + const buttonIcon = getKeyAndActionFromCurrentKeysWithPressedButton(activeConfig, this.buttonPressed)?.icon if (!buttonIcon) return; - const assignedButtonIcon = this.scene.inputController.getCurrentlyAssignedIconToDisplay(this.target); + const assignedButtonIcon = getKeyAndActionFromCurrentKeysWithSettingName(activeConfig, this.target)?.icon; this.onInputDown(buttonIcon, assignedButtonIcon, type); } swapAction() { - this.scene.inputController.swapBinding(this.target, this.buttonPressed); + const activeConfig = this.scene.inputController.getActiveConfig(); + this.scene.inputController.swapBinding(activeConfig, this.target, this.buttonPressed) + this.scene.gameData.saveCustomMapping(this.scene.inputController?.chosenGamepad, activeConfig.custom); return true; } } \ No newline at end of file diff --git a/src/ui/settings/keyboard-binding-ui-handler.ts b/src/ui/settings/keyboard-binding-ui-handler.ts index 51dbb508c..cd13757d8 100644 --- a/src/ui/settings/keyboard-binding-ui-handler.ts +++ b/src/ui/settings/keyboard-binding-ui-handler.ts @@ -1,6 +1,10 @@ import BattleScene from "../../battle-scene"; import AbstractBindingUiHandler from "../settings/abrast-binding-ui-handler"; import {Mode} from "../ui"; +import { + getKeyAndActionFromCurrentKeysWithPressedButton, + getKeyAndActionFromCurrentKeysWithSettingName, +} from "#app/configs/gamepad-utils"; export default class KeyboardBindingUiHandler extends AbstractBindingUiHandler { @@ -16,14 +20,17 @@ export default class KeyboardBindingUiHandler extends AbstractBindingUiHandler { // // Check conditions before processing the button press. if (!this.listening || this.buttonPressed !== null) return; this.buttonPressed = key; - const buttonIcon = this.scene.inputController.getPressedKeyLabel(key); + const activeConfig = this.scene.inputController.getActiveKeyboardConfig(); + const buttonIcon = getKeyAndActionFromCurrentKeysWithPressedButton(activeConfig, key)?.icon if (!buttonIcon) return; - const assignedButtonIcon = this.scene.inputController.getKeyboardCurrentlyAssignedIconToDisplay(this.target); + const assignedButtonIcon = getKeyAndActionFromCurrentKeysWithSettingName(activeConfig, this.target)?.icon; this.onInputDown(buttonIcon, assignedButtonIcon, 'keyboard'); } swapAction() { - this.scene.inputController.swapKeyboardBinding(this.target, this.buttonPressed); + const activeConfig = this.scene.inputController.getActiveKeyboardConfig(); + this.scene.inputController.swapBinding(activeConfig, this.target, this.buttonPressed) + this.scene.gameData.saveCustomKeyboardMapping(this.scene.inputController?.chosenKeyboard, activeConfig.custom); return true; } diff --git a/src/utils.ts b/src/utils.ts index 25391a890..9ffb02474 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -349,3 +349,7 @@ export function truncateString(str: String, maxLength: number = 10) { } return str; } + +export function deepCopy(values: object): object { + return JSON.parse(JSON.stringify(values)); +} \ No newline at end of file