Manages and handles all input controls for the game, including keyboard and gamepad interactions.

This class is designed to centralize input management across the game. It facilitates the setup, configuration, and handling of all game inputs, making it easier to manage various input devices such as keyboards and gamepads. The class provides methods for setting up input devices, handling their events, and responding to changes in input state (e.g., button presses, releases).

The InputsController class also includes mechanisms to handle game focus events to ensure input states are correctly reset and managed when the game loses or regains focus, maintaining robust and responsive control handling throughout the game's lifecycle.

Key responsibilities include:

  • Initializing and configuring gamepad and keyboard controls.
  • Emitting events related to specific input actions.
  • Responding to external changes such as gamepad connection/disconnection.
  • Managing game state transitions in response to input events, particularly focus loss and recovery.

Usage of this class is intended to simplify input management across various parts of the game, providing a unified interface for all input-related interactions.

Constructors

  • Initializes a new instance of the game control system, setting up initial state and configurations.

    Parameters

    • scene: default

      The Phaser scene associated with this instance.

    Returns InputsController

    This constructor initializes the game control system with necessary setups for handling inputs. It prepares an interactions array indexed by button identifiers and configures default states for each button. Specific buttons like MENU and STATS are set not to repeat their actions. It concludes by calling the init method to complete the setup.

Properties

events: EventEmitter
gamepadSupport: boolean = true
lastSource: string = "keyboard"
moveTouchControlsHandler: default
selectedDevice: any

Methods

  • Swaps a binding in the configuration.

    Parameters

    • config: any

      The configuration object.

    • settingName: any

      The name of the setting to swap.

    • pressedButton: any

      The button that was pressed.

    Returns boolean

  • Ensures the keyboard is initialized by checking if there is an active configuration for the keyboard. If not, it sets up the keyboard with default configurations.

    Returns void

  • Handles button press events on a gamepad. This method sets the gamepad as chosen on the first input if no gamepad is currently chosen. It checks if gamepad support is enabled and if the event comes from the chosen gamepad. If so, it maps the button press to a specific action using a custom configuration, emits an event for the button press, and records the time of the action.

    Parameters

    • pad: Gamepad

      The gamepad on which the button was pressed.

    • button: Button

      The specific button that was pressed.

    • value: number

      The intensity or value of the button press, if applicable.

    Returns void

  • Responds to a button release event on a gamepad by checking if the gamepad is supported and currently chosen. If conditions are met, it identifies the configured action for the button, emits an event signaling the button release, and clears the record of the button.

    Parameters

    • pad: Gamepad

      The gamepad from which the button was released.

    • button: Button

      The specific button that was released.

    • value: number

      The intensity or value of the button release, if applicable.

    Returns void

  • Retrieves the active configuration for the currently chosen device. It checks if a specific device ID is stored in configurations and returns it.

    Parameters

    Returns any

    InterfaceConfig The configuration object for the active gamepad, or null if not set.

  • Retrieves the configuration object for a gamepad based on its identifier. The method identifies specific gamepad models based on substrings in the identifier and returns predefined configurations for recognized models. If no specific configuration matches, it defaults to a generic gamepad configuration.

    Parameters

    • id: string

      The identifier string of the gamepad.

    Returns InterfaceConfig

    InterfaceConfig The configuration object corresponding to the identified gamepad type.

  • Retrieves the configuration object for a keyboard layout based on its identifier.

    Parameters

    • id: string

      The identifier string of the keyboard layout.

    Returns InterfaceConfig

    InterfaceConfig The configuration object corresponding to the identified keyboard layout.

  • Retrieves the identifiers of all connected gamepads, excluding any that are currently marked as disconnected.

    Returns String[]

    Array An array of strings representing the IDs of the connected gamepads.

  • Sets up event handlers and initializes gamepad and keyboard controls.

    Returns void

    This method configures event listeners for both gamepad and keyboard inputs. It handles gamepad connections/disconnections and button press events, and ensures keyboard controls are set up. Additionally, it manages the game's behavior when it loses focus to prevent unwanted game actions during this state.

  • Initializes the chosen gamepad by setting its identifier in the local storage and updating the UI to reflect the chosen gamepad. If a gamepad name is provided, it uses that as the chosen gamepad; otherwise, it defaults to the currently chosen gamepad.

    Parameters

    • OptionalgamepadName: String

      Optional parameter to specify the name of the gamepad to initialize as chosen.

    Returns void

  • Initializes the chosen keyboard layout by setting its identifier in the local storage and updating the UI to reflect the chosen layout. If a layout name is provided, it uses that as the chosen layout; otherwise, it defaults to the currently chosen layout.

    Parameters

    • OptionallayoutKeyboard: String

      Optional parameter to specify the name of the keyboard layout to initialize as chosen.

    Returns void

  • Injects a custom mapping configuration into the configuration for a specific gamepad. If the device does not have an existing configuration, it initializes one first.

    Parameters

    • selectedDevice: string

      The identifier of the device to configure.

    • mappingConfigs: any

      The mapping configuration to apply to the device.

    Returns void

  • Handles the keydown event for the keyboard.

    Parameters

    • event: any

      The keyboard event.

    Returns void

  • Handles the keyup event for the keyboard.

    Parameters

    • event: any

      The keyboard event.

    Returns void

  • Handles actions to take when the game loses focus, such as deactivating pressed keys.

    Returns void

    This method is triggered when the game or the browser tab loses focus. It ensures that any keys pressed are deactivated to prevent stuck keys affecting gameplay when the game is not active.

  • Handles the disconnection of a gamepad by adding its identifier to a list of disconnected gamepads. This is necessary because Phaser retains memory of previously connected gamepads, and without tracking disconnections, it would be impossible to determine the connection status of gamepads. This method ensures that disconnected gamepads are recognized and can be appropriately hidden in the gamepad selection menu.

    Parameters

    • thisGamepad: Gamepad

      The gamepad that has been disconnected.

    Returns void

  • Updates the tracking of disconnected gamepads when a gamepad is reconnected. It removes the reconnected gamepad's identifier from the disconnectedGamepads array, effectively updating its status to connected.

    Parameters

    • thisGamepad: Gamepad

      The gamepad that has been reconnected.

    Returns void

  • Refreshes and re-indexes the list of connected gamepads.

    Returns void

    This method updates the list of gamepads to exclude any that are undefined. It corrects the index of each gamepad to account for any previously undefined entries, ensuring that all gamepads are properly indexed and can be accurately referenced within the game.

  • Sets the currently chosen gamepad and initializes related settings. This method first deactivates any active key presses and then initializes the gamepad settings.

    Parameters

    • gamepad: String

      The identifier of the gamepad to set as chosen.

    Returns void

  • Sets the currently chosen keyboard layout and initializes related settings.

    Parameters

    • layoutKeyboard: String

      The identifier of the keyboard layout to set as chosen.

    Returns void

  • Enables or disables support for gamepad input.

    Parameters

    • value: boolean

      A boolean indicating whether gamepad support should be enabled (true) or disabled (false).

    Returns void

    This method toggles gamepad support. If disabled, it also ensures that all currently pressed gamepad buttons are deactivated to avoid stuck inputs.

  • Initializes or updates configurations for connected gamepads. It retrieves the names of all connected gamepads, sets up their configurations according to stored or default settings, and ensures these configurations are saved. If the connected gamepad is the currently chosen one, it reinitializes the chosen gamepad settings.

    Parameters

    • thisGamepad: Gamepad

      The gamepad that is being set up.

    Returns void