diff --git a/src/configs/gamepad-utils.ts b/src/configs/gamepad-utils.ts index d8be247ef..df31ed827 100644 --- a/src/configs/gamepad-utils.ts +++ b/src/configs/gamepad-utils.ts @@ -2,15 +2,21 @@ import {GamepadConfig} from "../inputs-controller"; import {SettingGamepad} from "#app/system/settings-gamepad"; -export function getKeyForButtonIndex(config: GamepadConfig, index: integer): String { +export function getKeyForButtonIndex(config: GamepadConfig, index: number): String { 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]; + } + return null; +} -export function getIconForCustomIndex(config: GamepadConfig, index: integer): String { +export function getIconForCustomIndex(config: GamepadConfig, index: number): String { const key = getKeyForButtonIndex(config, index); return config.icons[key]; } @@ -21,4 +27,4 @@ export function getKeyForSettingName(config: GamepadConfig, settingName: Setting if (name === settingName) return key; } return null; -} \ No newline at end of file +} diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 2568f49ee..f4e0e8056 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -8,7 +8,12 @@ import {Button} from "./enums/buttons"; import {Mode} from "./ui/ui"; import SettingsGamepadUiHandler from "./ui/settings-gamepad-ui-handler"; import {SettingGamepad} from "./system/settings-gamepad"; -import {getIconForCustomIndex, getKeyForButtonIndex} from "#app/configs/gamepad-utils"; +import { + getButtonIndexForKey, + getIconForCustomIndex, + getKeyForButtonIndex, + getKeyForSettingName +} from "./configs/gamepad-utils"; export interface GamepadMapping { [key: string]: number; @@ -33,6 +38,7 @@ export interface GamepadConfig { icons: IconsMapping; setting: SettingMapping; default: DefaultMapping; + custom: DefaultMapping; } export interface ActionGamepadMapping { @@ -323,10 +329,6 @@ export class InputsController { } } - getButtonLabel(button: Phaser.Input.Gamepad.Button) { - return getIconForCustomIndex(this.configs[this.chosenGamepad], button.index); - } - /** * Handles the 'down' event for gamepad buttons, emitting appropriate events and updating the interaction state. * @@ -648,4 +650,24 @@ export class InputsController { getActiveConfig() :GamepadConfig { return this.configs[this.chosenGamepad] || pad_generic; } + + getPressedButtonLabel(button: Phaser.Input.Gamepad.Button) { + return [this.configs[this.chosenGamepad].padType, getIconForCustomIndex(this.configs[this.chosenGamepad], button.index)]; + } + + getCurrentButtonLabel(target: SettingGamepad) { + const key = getKeyForSettingName(this.configs[this.chosenGamepad], target); + const id = getButtonIndexForKey(this.configs[this.chosenGamepad], key); + return getIconForCustomIndex(this.configs[this.chosenGamepad], id); + } + + swapBinding(target, newBinding) { + this.deactivatePressedKey(); + const keyTarget = getKeyForSettingName(this.configs[this.chosenGamepad], target); + const keyNewBinding = getKeyForButtonIndex(this.configs[this.chosenGamepad], newBinding); + 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; + } } \ No newline at end of file diff --git a/src/system/settings-gamepad.ts b/src/system/settings-gamepad.ts index ea71d0e12..9d987e3df 100644 --- a/src/system/settings-gamepad.ts +++ b/src/system/settings-gamepad.ts @@ -102,9 +102,10 @@ export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, v case SettingGamepad.Button_Slow_Down: if (value) { if (scene.ui) { - const cancelHandler = () => { + const cancelHandler = (success: boolean = false) => { scene.ui.revertMode(); - return false; + (scene.ui.getHandler() as SettingsGamepadUiHandler).updateBindings(); + return success; }; scene.ui.setOverlayMode(Mode.GAMEPAD_BINDING, { target: setting, diff --git a/src/ui/gamepad-binding-ui-handler.ts b/src/ui/gamepad-binding-ui-handler.ts index fa4357798..1d34c5285 100644 --- a/src/ui/gamepad-binding-ui-handler.ts +++ b/src/ui/gamepad-binding-ui-handler.ts @@ -5,6 +5,7 @@ import {Button} from "../enums/buttons"; import {addWindow} from "./ui-theme"; import {addTextObject, TextStyle} from "#app/ui/text"; import Phaser from "phaser"; +import {SettingGamepad} from "../system/settings-gamepad"; export default class GamepadBindingUiHandler extends UiHandler { @@ -24,7 +25,7 @@ export default class GamepadBindingUiHandler extends UiHandler { private previousIconXbox: Phaser.GameObjects.Sprite; private iconDualshock: Phaser.GameObjects.Sprite; private cancelFn; - private target; + private target: SettingGamepad; constructor(scene: BattleScene, mode: Mode = Mode.GAMEPAD_BINDING) { super(scene, mode); @@ -104,7 +105,8 @@ export default class GamepadBindingUiHandler extends UiHandler { const blacklist = [12, 13, 14, 15]; 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.getButtonLabel(button); + const [type, buttonIcon] = this.scene.inputController.getPressedButtonLabel(button); + const assignedButtonIcon = this.scene.inputController.getCurrentButtonLabel(this.target); switch (type) { case 'dualshock': this.iconXbox.setVisible(false); @@ -118,6 +120,7 @@ export default class GamepadBindingUiHandler extends UiHandler { this.iconDualshock.setVisible(false); this.iconXbox.setFrame(buttonIcon); this.iconXbox.setVisible(true); + this.previousIconXbox.setFrame(assignedButtonIcon); this.previousIconXbox.setVisible(true); this.alreadyAssignedText.setVisible(true); break @@ -164,6 +167,10 @@ export default class GamepadBindingUiHandler extends UiHandler { // Reverts UI to its previous state on cancel. // this.scene.ui.revertMode(); this.cancelFn(); + } else { + success = true; + this.scene.inputController.swapBinding(this.target, this.buttonPressed); + this.cancelFn(success); } break; } diff --git a/src/ui/settings-gamepad-ui-handler.ts b/src/ui/settings-gamepad-ui-handler.ts index d14196db2..b0be6b5b2 100644 --- a/src/ui/settings-gamepad-ui-handler.ts +++ b/src/ui/settings-gamepad-ui-handler.ts @@ -139,6 +139,12 @@ export default class SettingsGamepadUiHandler extends UiHandler { this.settingsContainer.setVisible(false); } + updateBindings(): void { + // for (const elm of noOptionsCursors) { + // console.log('elm:', elm); + // } + } + show(args: any[]): boolean { super.show(args);