# Class InputsController

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

#### Remarks

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.

- Defined in src/inputs-controller.ts:90

#####
Index

### Constructors

constructor

### Properties

buttonLock
configs
disconnectedGamepads
events
gamepadSupport
gamepads
interactions
keys
lastSource
pauseUpdate
scene
selectedDevice

### Methods

assignBinding
deactivatePressedKey
delLastProcessedMovementTime
ensureKeyboardIsInit
gamepadButtonDown
gamepadButtonUp
getActiveConfig
getConfig
getConfigKeyboard
getGamepadsName
getIconForLatestInputRecorded
getLastSourceConfig
getLastSourceDevice
getLastSourceType
init
initChosenGamepad
initChosenLayoutKeyboard
injectConfig
isButtonLocked
keyboardKeyDown
keyboardKeyUp
loseFocus
onDisconnect
onReconnect
refreshGamepads
releaseButtonLock
repeatInputDurationJustPassed
resetConfigs
setButtonLock
setChosenGamepad
setChosenKeyboardLayout
setGamepadSupport
setLastProcessedMovementTime
setupGamepad
setupKeyboard
update

## Constructors

### constructor

- new InputsController(scene): InputsController

- 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

  #### Remarks

  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.

  - Defined in src/inputs-controller.ts:121

## Properties

### `Private` buttonLock

buttonLock: Button

- Defined in src/inputs-controller.ts:95

### `Private` configs

configs: Map<string, InterfaceConfig > = ...

- Defined in src/inputs-controller.ts:97

### `Private` disconnectedGamepads

disconnectedGamepads: String[] = ...

- Defined in src/inputs-controller.ts:102

### events

events: EventEmitter

- Defined in src/inputs-controller.ts:93

### gamepadSupport

gamepadSupport: boolean = true

- Defined in src/inputs-controller.ts:99

### `Private` gamepads

gamepads: Gamepad[] = ...

- Defined in src/inputs-controller.ts:91

### `Private` interactions

interactions: Map< Button , Map<string, boolean>> = ...

- Defined in src/inputs-controller.ts:96

### `Private` keys

keys: number[] = []

- Defined in src/inputs-controller.ts:107

### lastSource

lastSource: string = "keyboard"

- Defined in src/inputs-controller.ts:106

### `Private` pauseUpdate

pauseUpdate: boolean = false

- Defined in src/inputs-controller.ts:104

### `Private` scene

scene: default

- Defined in src/inputs-controller.ts:92

### selectedDevice

selectedDevice: any

- Defined in src/inputs-controller.ts:100

## Methods

### assignBinding

- assignBinding(config, settingName, pressedButton): boolean

- 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

  - Defined in src/inputs-controller.ts:753

### deactivatePressedKey

- deactivatePressedKey(): void

- Deactivates all currently pressed keys and resets their interaction states.

  #### Returns void

  #### Remarks

  This method is used to reset the state of all buttons within the `interactions` dictionary, effectively deactivating any currently pressed keys. It performs the following actions:

  - Releases button lock for predefined buttons, allowing them to be pressed again or properly re-initialized in future interactions.
  - Iterates over all possible button values obtained via `Utils.getEnumValues(Button)`, and for each button:
    - Checks if the button is currently registered in the `interactions` dictionary.
    - Resets `pressTime` to null, indicating that there is no ongoing interaction.
    - Sets `isPressed` to false, marking the button as not currently active.
    - Clears the `source` field, removing the record of which device the button press came from.

  This method is typically called when needing to ensure that all inputs are neutralized.

  - Defined in src/inputs-controller.ts:626

### delLastProcessedMovementTime

- delLastProcessedMovementTime(button): void

- Clears the last interaction for a specified button.

  #### Parameters

  - button: Button

    The button for which to clear the interaction.

  #### Returns void

  #### Remarks

  This method resets the interaction details of the button, allowing it to be processed as a new input when pressed again. If the button is not registered in the `interactions` dictionary, this method returns immediately, otherwise:

  - `pressTime` is cleared. This was previously storing the timestamp of when the button was initially pressed.
  - `isPressed` is set to false, indicating that the button is no longer being pressed.
  - `source` is set to null, which had been indicating the device from which the button input was originating.

  It releases the button lock, which prevents the button from being processed repeatedly until it's explicitly released.

  - Defined in src/inputs-controller.ts:597

### ensureKeyboardIsInit

- ensureKeyboardIsInit(): void

- 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

  - Defined in src/inputs-controller.ts:394

### gamepadButtonDown

- gamepadButtonDown(pad, button, value): 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

  - Defined in src/inputs-controller.ts:452

### gamepadButtonUp

- gamepadButtonUp(pad, button, value): 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

  - Defined in src/inputs-controller.ts:486

### getActiveConfig

- getActiveConfig(device): any

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

  #### Parameters

  - device: Device

  #### Returns any

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

  - Defined in src/inputs-controller.ts:689

### getConfig

- getConfig(id): InterfaceConfig

- 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.

  - Defined in src/inputs-controller.ts:512

### getConfigKeyboard

- getConfigKeyboard(id): InterfaceConfig

- 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.

  - Defined in src/inputs-controller.ts:534

### getGamepadsName

- getGamepadsName(): String[]

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

  #### Returns String[]

  Array<String> An array of strings representing the IDs of the connected gamepads.

  - Defined in src/inputs-controller.ts:280

### getIconForLatestInputRecorded

- getIconForLatestInputRecorded(settingName): any

- #### Parameters

  - settingName: any

  #### Returns any

  - Defined in src/inputs-controller.ts:696

### getLastSourceConfig

- getLastSourceConfig(): any

- #### Returns any

  - Defined in src/inputs-controller.ts:711

### getLastSourceDevice

- getLastSourceDevice(): Device

- #### Returns Device

  - Defined in src/inputs-controller.ts:703

### getLastSourceType

- getLastSourceType(): any

- #### Returns any

  - Defined in src/inputs-controller.ts:719

### init

- init(): void

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

  #### Returns void

  #### Remarks

  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.

  - Defined in src/inputs-controller.ts:149

### initChosenGamepad

- initChosenGamepad(gamepadName?): void

- 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

  - `Optional` gamepadName: String

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

  #### Returns void

  - Defined in src/inputs-controller.ts:289

### initChosenLayoutKeyboard

- initChosenLayoutKeyboard(layoutKeyboard?): 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

  - `Optional` layoutKeyboard: String

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

  #### Returns void

  - Defined in src/inputs-controller.ts:302

### injectConfig

- injectConfig(selectedDevice, mappingConfigs): 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

  - Defined in src/inputs-controller.ts:731

### isButtonLocked

- isButtonLocked(button): boolean

- Checks if a specific button is currently locked.

  #### Parameters

  - button: Button

    The button to check for a lock status.

  #### Returns boolean

  `true` if the button is locked, otherwise `false`.

  #### Remarks

  This method is used to determine if a given button is currently prevented from being processed due to a lock. It checks against two separate lock variables, allowing for up to two buttons to be locked simultaneously.

  - Defined in src/inputs-controller.ts:650

### keyboardKeyDown

- keyboardKeyDown(event): void

- Handles the keydown event for the keyboard.

  #### Parameters

  - event: any

    The keyboard event.

  #### Returns void

  - Defined in src/inputs-controller.ts:405

### keyboardKeyUp

- keyboardKeyUp(event): void

- Handles the keyup event for the keyboard.

  #### Parameters

  - event: any

    The keyboard event.

  #### Returns void

  - Defined in src/inputs-controller.ts:428

### loseFocus

- loseFocus(): void

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

  #### Returns void

  #### Remarks

  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.

  - Defined in src/inputs-controller.ts:193

### onDisconnect

- onDisconnect(thisGamepad): void

- 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

  - Defined in src/inputs-controller.ts:318

### onReconnect

- onReconnect(thisGamepad): 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

  - Defined in src/inputs-controller.ts:329

### refreshGamepads

- refreshGamepads(): void

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

  #### Returns void

  #### Remarks

  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.

  - Defined in src/inputs-controller.ts:379

### releaseButtonLock

- releaseButtonLock(button): void

- Releases a lock on a specific button, allowing it to be processed again.

  #### Parameters

  - button: Button

    The button whose lock is to be released.

  #### Returns void

  #### Remarks

  This method checks lock variable. If either lock matches the specified button, that lock is cleared. This action frees the button to be processed again, ensuring it can respond to new inputs.

  - Defined in src/inputs-controller.ts:677

### repeatInputDurationJustPassed

- repeatInputDurationJustPassed(button): boolean

- repeatInputDurationJustPassed returns true if

  #### Parameters

  - button: Button

    has been held down long enough to fire a repeated input. A button must claim the buttonLock before firing a repeated input - this is to prevent multiple buttons from firing repeatedly.

  #### Returns boolean

  - Defined in src/inputs-controller.ts:547

### resetConfigs

- resetConfigs(): void

- #### Returns void

  - Defined in src/inputs-controller.ts:738

### setButtonLock

- setButtonLock(button): void

- Sets a lock on a given button.

  #### Parameters

  - button: Button

    The button to lock.

  #### Returns void

  #### Remarks

  This method ensures that a button is not processed multiple times inadvertently. It checks if the button is already locked.

  - Defined in src/inputs-controller.ts:663

### setChosenGamepad

- setChosenGamepad(gamepad): void

- 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

  - Defined in src/inputs-controller.ts:220

### setChosenKeyboardLayout

- setChosenKeyboardLayout(layoutKeyboard): 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

  - Defined in src/inputs-controller.ts:230

### setGamepadSupport

- setGamepadSupport(value): 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

  #### Remarks

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

  - Defined in src/inputs-controller.ts:205

### setLastProcessedMovementTime

- setLastProcessedMovementTime(button, source?, sourceName?): void

- This method updates the interaction state to reflect that the button is pressed.

  #### Parameters

  - button: Button

    The button for which to set the interaction.

  - source: String = "keyboard"

    The source of the input (defaults to 'keyboard'). This helps identify the origin of the input, especially useful in environments with multiple input devices.

  - `Optional` sourceName: String

  #### Returns void

  #### Remarks

  This method is responsible for updating the interaction state of a button within the `interactions` dictionary. If the button is not already registered, this method returns immediately. When invoked, it performs the following updates:

  - `pressTime`: Sets this to the current time, representing when the button was initially pressed.
  - `isPressed`: Marks the button as currently being pressed.
  - `source`: Identifies the source device of the input, which can vary across different hardware (e.g., keyboard, gamepad).

  Additionally, this method locks the button (by calling `setButtonLock`) to prevent it from being re-processed until it is released, ensuring that each press is handled distinctly.

  - Defined in src/inputs-controller.ts:572

### setupGamepad

- setupGamepad(thisGamepad): void

- 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

  - Defined in src/inputs-controller.ts:341

### setupKeyboard

- setupKeyboard(): void

- Initializes or updates configurations for connected keyboards.

  #### Returns void

  - Defined in src/inputs-controller.ts:361

### update

- update(): void

- Updates the interaction handling by processing input states. This method gives priority to certain buttons by reversing the order in which they are checked. This method loops through all button values, checks for valid and timely interactions, and conditionally processes or ignores them based on the current state of gamepad support and other criteria.

  It handles special conditions such as the absence of gamepad support or mismatches between the source of the input and the currently chosen gamepad. It also respects the paused state of updates to prevent unwanted input processing.

  If an interaction is valid and should be processed, it emits an 'input_down' event with details of the interaction.

  #### Returns void

  - Defined in src/inputs-controller.ts:246