diff --git a/src/configs/gamepad-utils.ts b/src/configs/gamepad-utils.ts index a01813db2..b0cd75e99 100644 --- a/src/configs/gamepad-utils.ts +++ b/src/configs/gamepad-utils.ts @@ -3,63 +3,46 @@ import {SettingGamepad} from "#app/system/settings-gamepad"; import {Button} from "#app/enums/buttons"; -export function getKeyForButtonIndex(config: GamepadConfig, index: number): String { +// Given a button index from an input event, return its naming from the mapping config +export function getKeyFromInputIndex(config: GamepadConfig, index: number): String | null { for (const key of Object.keys(config.gamepadMapping)) { - const id = config.gamepadMapping[key]; - if (id === index) return key; - } - return null; -} -export function getButtonIndexForKey(config: GamepadConfig, _key: string): number { - for (const key of Object.keys(config.gamepadMapping)) { - if (key === _key) return config.gamepadMapping[key]; + if (config.gamepadMapping[key] === index) return key; } return null; } -export function getIconForCustomIndex(config: GamepadConfig, index: number): String { - const key = getKeyForButtonIndex(config, index); - return config.icons[key]; -} - -export function getKeyForRebindedAction(config: GamepadConfig, action: Button): String { - for (const key of Object.keys(config.default)) { - if (config.default[key] === action) return key; +// Given a setting name, return the key assigned to it from the config file +export function getKeyForSettingName(config: GamepadConfig, settingName: SettingGamepad): String | null { + for (const key of Object.keys(config.setting)) { + if (config.setting[key] === settingName) return key; } return null; } -export function getKeyForAction(config: GamepadConfig, action: Button): String { +// Given a Button, return the custom key assigned to it from the config file +export function getCurrenlyAssignedKeyToAction(config: GamepadConfig, action: Button): String | null { for (const key of Object.keys(config.custom)) { if (config.custom[key] === action) return key; } return null; } -export function getKeyForRebindedSettingName(config: GamepadConfig, settingName: SettingGamepad): String { +// Given a setting name, return the custom key for the default action from the config file +export function getCurrentlyAssignedToSettingName(config: GamepadConfig, settingName: SettingGamepad): String { const oldKey = getKeyForSettingName(config, settingName) - const action = config.custom[oldKey]; - const key = getKeyForRebindedAction(config, action); + const action = config.default[oldKey]; + const key = getCurrenlyAssignedKeyToAction(config, action); return key; } -export function getIconForRebindedKey(config: GamepadConfig, _key): String { - const action = config.custom[_key]; - const key = getKeyForRebindedAction(config, action); +// Given a button index from an input event, return its icon from the config file +export function getCurrenlyAssignedIconFromInputIndex(config: GamepadConfig, index: number): String { + const key = getKeyFromInputIndex(config, index); return config.icons[key]; } -export function getKeyForSettingName(config: GamepadConfig, settingName: SettingGamepad) { - for (const key of Object.keys(config.setting)) { - const name = config.setting[key]; - if (name === settingName) return key; - } - return null; -} - -export function getIconForSettingName(config: GamepadConfig, settingName: SettingGamepad) { - const key = getKeyForSettingName(config, settingName); - const action = config.default[key]; - const rebindedKey = getKeyForAction(config, action); - return config.icons[rebindedKey]; +// Given a setting name, return the icon currently assigned to this setting name +export function getCurrentlyAssignedIconToSettingName(config: GamepadConfig, settingName: SettingGamepad) { + const key = getCurrentlyAssignedToSettingName(config, settingName); + return config.icons[key]; } diff --git a/src/configs/pad_generic.ts b/src/configs/pad_generic.ts index de07ef478..1e38c6aca 100644 --- a/src/configs/pad_generic.ts +++ b/src/configs/pad_generic.ts @@ -64,9 +64,9 @@ const pad_generic = { RC_N: Button.CYCLE_VARIANT, START: Button.MENU, SELECT: Button.STATS, - LB: Button.CYCLE_FORM, - RB: Button.CYCLE_SHINY, - LT: Button.CYCLE_GENDER, + LB: Button.CYCLE_FORM, //10 + RB: Button.CYCLE_SHINY, //9 + LT: Button.CYCLE_GENDER, //11 RT: Button.CYCLE_ABILITY, LS: Button.SPEED_UP, RS: Button.SLOW_DOWN, diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 95b0ab6df..35a5b45ff 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -9,10 +9,8 @@ import {Mode} from "./ui/ui"; import SettingsGamepadUiHandler from "./ui/settings-gamepad-ui-handler"; import {SettingGamepad} from "./system/settings-gamepad"; import { - getButtonIndexForKey, - getIconForCustomIndex, getIconForRebindedKey, getIconForSettingName, - getKeyForButtonIndex, getKeyForRebindedSettingName, - getKeyForSettingName + getCurrenlyAssignedIconFromInputIndex, getCurrentlyAssignedIconToSettingName, + getKeyFromInputIndex, getCurrentlyAssignedToSettingName } from "./configs/gamepad-utils"; export interface GamepadMapping { @@ -88,6 +86,8 @@ export class InputsController { private pauseUpdate: boolean = false; + public lastSource: string = 'keyboard'; + /** * Initializes a new instance of the game control system, setting up initial state and configurations. * @@ -276,11 +276,11 @@ export class InputsController { // const chosenIsConnected = gamepadsLeft.some(g => g.id === this.chosenGamepad); // if the chosen gamepad is disconnected, and we got others gamepad connected // if (!chosenIsConnected && gamepadsLeft?.length) { - // We remove the previously chosen gamepad - // this.clearChosenGamepad(); - // and we set the first of the gamepad still connected as the chosen one. - // this.setChosenGamepad(gamepadsLeft[0].id); - // return; + // We remove the previously chosen gamepad + // this.clearChosenGamepad(); + // and we set the first of the gamepad still connected as the chosen one. + // this.setChosenGamepad(gamepadsLeft[0].id); + // return; // } } @@ -351,8 +351,9 @@ export class InputsController { if (!this.chosenGamepad) // at the very first input, if we have not yet a chosen gamepad, we set it this.setChosenGamepad(pad.id); if (!this.gamepadSupport || pad.id.toLowerCase() !== this.chosenGamepad.toLowerCase()) return; - const key = getKeyForButtonIndex(this.configs[pad.id], button.index); + const key = getKeyFromInputIndex(this.configs[pad.id], button.index); const buttonDown = this.configs[pad.id].custom[key]; + this.lastSource = 'gamepad'; if (buttonDown !== undefined) { this.events.emit('input_down', { controller_type: 'gamepad', @@ -378,7 +379,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 = getKeyForButtonIndex(this.configs[pad.id], button.index); + const key = getKeyFromInputIndex(this.configs[pad.id], button.index); const buttonUp = this.configs[pad.id]?.custom[key]; if (buttonUp !== undefined) { this.events.emit('input_up', { @@ -465,6 +466,7 @@ export class InputsController { this.buttonKeys.forEach((row, index) => { for (const key of row) { key.on('down', () => { + this.lastSource = 'keyboard'; this.events.emit('input_down', { controller_type: 'keyboard', button: index, @@ -506,6 +508,7 @@ export class InputsController { return pad_dualshock; } + // return pad_dualshock; return pad_generic; } @@ -649,23 +652,23 @@ export class InputsController { else if (this.buttonLock2 === button) this.buttonLock2 = null; } - getActiveConfig() :GamepadConfig { + getActiveConfig(): GamepadConfig { if (this.configs[this.chosenGamepad]?.padID) return this.configs[this.chosenGamepad] return null; } getPressedButtonLabel(button: Phaser.Input.Gamepad.Button) { - return [this.configs[this.chosenGamepad].padType, getIconForCustomIndex(this.configs[this.chosenGamepad], button.index)]; + return [this.configs[this.chosenGamepad].padType, getCurrenlyAssignedIconFromInputIndex(this.configs[this.chosenGamepad], button.index)]; } - getCurrentButtonLabel(target: SettingGamepad) { - return getIconForSettingName(this.configs[this.chosenGamepad], target); + getCurrentlyAssignedIconToDisplay(target: SettingGamepad) { + return getCurrentlyAssignedIconToSettingName(this.configs[this.chosenGamepad], target); } - swapBinding(target, newBinding) { + swapBinding(settingName, pressedButton) { this.pauseUpdate = true; - const keyTarget = getKeyForRebindedSettingName(this.configs[this.chosenGamepad], target) - const keyNewBinding = getKeyForButtonIndex(this.configs[this.chosenGamepad], newBinding); + 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; @@ -674,7 +677,7 @@ export class InputsController { setTimeout(() => this.pauseUpdate = false, 500); } - loadConfig(gamepadName: String, customMappings: MappingLayout): void { + injectConfig(gamepadName: String, customMappings: MappingLayout): void { if (!this.configs[gamepadName]) this.configs[gamepadName] = {}; this.configs[gamepadName].custom = customMappings; } diff --git a/src/system/game-data.ts b/src/system/game-data.ts index a131cd318..0aedf14e1 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -499,7 +499,7 @@ export class GameData { return false; const customMappings = JSON.parse(localStorage.getItem('customMapping')); for (const key of Object.keys(customMappings)) - this.scene.inputController.loadConfig(key, customMappings[key]); + this.scene.inputController.injectConfig(key, customMappings[key]); } diff --git a/src/system/settings-gamepad.ts b/src/system/settings-gamepad.ts index 94c2ad951..bca9de689 100644 --- a/src/system/settings-gamepad.ts +++ b/src/system/settings-gamepad.ts @@ -12,8 +12,8 @@ export enum SettingGamepad { Button_Cancel = "BUTTON_CANCEL", Button_Menu = "BUTTON_MENU", Button_Stats = "BUTTON_STATS", - Button_Cycle_Shiny = "BUTTON_CYCLE_SHINY", 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", @@ -30,8 +30,8 @@ export const settingGamepadOptions: SettingOptions = { [SettingGamepad.Button_Cancel]: [`KEY ${Button.CANCEL.toString()}`, 'Change'], [SettingGamepad.Button_Menu]: [`KEY ${Button.MENU.toString()}`, 'Change'], [SettingGamepad.Button_Stats]: [`KEY ${Button.STATS.toString()}`, 'Change'], - [SettingGamepad.Button_Cycle_Shiny]: [`KEY ${Button.CYCLE_SHINY.toString()}`, 'Change'], [SettingGamepad.Button_Cycle_Form]: [`KEY ${Button.CYCLE_FORM.toString()}`, 'Change'], + [SettingGamepad.Button_Cycle_Shiny]: [`KEY ${Button.CYCLE_SHINY.toString()}`, 'Change'], [SettingGamepad.Button_Cycle_Gender]: [`KEY ${Button.CYCLE_GENDER.toString()}`, 'Change'], [SettingGamepad.Button_Cycle_Ability]: [`KEY ${Button.CYCLE_ABILITY.toString()}`, 'Change'], [SettingGamepad.Button_Cycle_Nature]: [`KEY ${Button.CYCLE_NATURE.toString()}`, 'Change'], @@ -48,8 +48,8 @@ export const settingGamepadDefaults: SettingDefaults = { [SettingGamepad.Button_Cancel]: 0, [SettingGamepad.Button_Menu]: 0, [SettingGamepad.Button_Stats]: 0, - [SettingGamepad.Button_Cycle_Shiny]: 0, [SettingGamepad.Button_Cycle_Form]: 0, + [SettingGamepad.Button_Cycle_Shiny]: 0, [SettingGamepad.Button_Cycle_Gender]: 0, [SettingGamepad.Button_Cycle_Ability]: 0, [SettingGamepad.Button_Cycle_Nature]: 0, @@ -64,8 +64,8 @@ export const noOptionsCursors: Array = [ SettingGamepad.Button_Cancel, SettingGamepad.Button_Menu, SettingGamepad.Button_Stats, - SettingGamepad.Button_Cycle_Shiny, SettingGamepad.Button_Cycle_Form, + SettingGamepad.Button_Cycle_Shiny, SettingGamepad.Button_Cycle_Gender, SettingGamepad.Button_Cycle_Ability, SettingGamepad.Button_Cycle_Nature, diff --git a/src/ui/gamepad-binding-ui-handler.ts b/src/ui/gamepad-binding-ui-handler.ts index 9f35df36c..8d3a93c12 100644 --- a/src/ui/gamepad-binding-ui-handler.ts +++ b/src/ui/gamepad-binding-ui-handler.ts @@ -97,7 +97,7 @@ export default class GamepadBindingUiHandler extends UiHandler { 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 assignedButtonIcon = this.scene.inputController.getCurrentButtonLabel(this.target); + const assignedButtonIcon = this.scene.inputController.getCurrentlyAssignedIconToDisplay(this.target); this.newButtonIcon.setTexture(type); this.newButtonIcon.setFrame(buttonIcon); this.targetButtonIcon.setTexture(type); diff --git a/src/ui/settings-gamepad-ui-handler.ts b/src/ui/settings-gamepad-ui-handler.ts index 22e736e11..b21a38ef4 100644 --- a/src/ui/settings-gamepad-ui-handler.ts +++ b/src/ui/settings-gamepad-ui-handler.ts @@ -12,7 +12,7 @@ import { } from "../system/settings-gamepad"; import {truncateString} from "../utils"; import { - getIconForSettingName, + getCurrentlyAssignedIconToSettingName, getKeyForSettingName } from "#app/configs/gamepad-utils"; import pad_xbox360 from "#app/configs/pad_xbox360"; @@ -20,6 +20,20 @@ import pad_dualshock from "#app/configs/pad_dualshock"; import pad_unlicensedSNES from "#app/configs/pad_unlicensedSNES"; import {GamepadConfig} from "#app/inputs-controller"; +export interface InputsIcons { + [key: string]: Phaser.GameObjects.Sprite; +} + +export interface LayoutConfig { + optionsContainer: Phaser.GameObjects.Container; + inputsIcons: InputsIcons; + settingLabels: Phaser.GameObjects.Text[]; + optionValueLabels: Phaser.GameObjects.Text[][]; + optionCursors: integer[]; + keys: string[]; + bindingSettings: Array; +} + export default class SettingsGamepadUiHandler extends UiHandler { private settingsContainer: Phaser.GameObjects.Container; private optionsContainer: Phaser.GameObjects.Container; @@ -39,10 +53,10 @@ export default class SettingsGamepadUiHandler extends UiHandler { private reloadI18n: boolean; private gamepads: Array; - private inputsIcons; + private inputsIcons: InputsIcons; - private layout = new Map(); - private keys; + private layout: Map = new Map(); + private keys: Array; constructor(scene: BattleScene, mode?: Mode) { super(scene, mode); @@ -202,8 +216,7 @@ export default class SettingsGamepadUiHandler extends UiHandler { if (!activeConfig.custom) return; for (const elm of bindingSettings) { const key = getKeyForSettingName(activeConfig, elm); - const icon = getIconForSettingName(activeConfig, elm); - if (!this.inputsIcons[key]) debugger; + const icon = getCurrentlyAssignedIconToSettingName(activeConfig, elm); this.inputsIcons[key].setFrame(icon); } this.setCursor(0); @@ -370,17 +383,13 @@ export default class SettingsGamepadUiHandler extends UiHandler { } } - clear() { + clear(): void { super.clear(); this.settingsContainer.setVisible(false); this.eraseCursor(); - if (this.reloadRequired) { - this.reloadRequired = false; - this.scene.reset(true, false, true); - } } - eraseCursor() { + eraseCursor(): void { if (this.cursorObj) this.cursorObj.destroy(); this.cursorObj = null;