Class to handle emulated gamepads and injecting them into the browser getGamepads() and event listener APIs.

Hierarchy

  • GamepadEmulator

Constructors

  • Creates a new GamepadEmulator object and monkey patches the browser getGamepads() API and gamepad events to report emulated gamepads You should create the GamepadEmulator object before any other libraries or function that may use the gamepad api

    Parameters

    • buttonPressThreshold: number

      the threshold above which a variable button is considered a "pressed" button

    Returns GamepadEmulator

Properties

buttonPressThreshold: number = 0.1

the threshold above which a variable button is considered a "pressed" button

emulatedGamepads: (null | EGamepad)[] = []

A list of all the emulated gamepads, where the index is the "gamepadIndex" passed when AddEmulatedGamepad() was called (Ie: there may be holes in the list), when an emulated gamepad is "connected" ie: call AddEmulatedGamepad(), it is added to this list at the provided index (or returns false if there is already an emulated gamepad at that index). when an emulated gamepad is "disconnected" ie: call removeEmulatedGamepad(), it is removed from this list provided index (or returns false if there is already an emulated gamepad at that index).

emulatedGamepadsMetadata: (null | EGamepadPrivateData)[] = []

A list that mirrors the structure of #emulatedGamepads, but contains data internal to this class for keeping track of their state

getNativeGamepads: (() => (null | Gamepad)[]) = ...

Type declaration

    • (): (null | Gamepad)[]
    • stores a reference to the real, unpatched navigator.getGamepads() function *

      Returns (null | Gamepad)[]

patchedGpadToRealIndexMap: number[] = []

the reverse mapping array of #realGpadToPatchedIndexMap

realGpadToPatchedIndexMap: number[] = []

A list of the indecies of all the real gamepads that have ever been conected durring this browser session, where the array index is the "gamepadIndex" returned by the native gamepad api, and the value is the index that gamepad should be exposed at in the emulated getGamepads() array

undoEventPatch: (() => void) = ...

Type declaration

    • (): void
    • stores the function returned by monkeyPatchGamepadEvents() to undo the gamepad event monkey patch *

      Returns void

instanceRunning: boolean = false

a static class variable to tell if any other instances of the GamepadEmulator class are active, and throw an error if a new one is created

Methods

  • add event listeners to the html/svg button elements of an onscreen gamepad to emulate gamepad input when touched, clicked or dragged

    Parameters

    • gpadIndex: number

      the index of the emulated gamepad to register events for

    • buttonConfigs: (ButtonConfig | VariableButtonConfig)[]

      an array of config objects that set how each of the buttons on the onscreen gamepad should behave, and how they map to the emulated gamepad buttons.

    Returns void

  • add event listeners to the html/svg joystick elements of an onscreen gamepad to emulate gamepad input when dragged with a mouse, touch or pen.

    Parameters

    • gpadIndex: number

      the index of the emulated gamepad to register events for

    • joystickConfigs: JoystickConfig[]

      an array of config objects that set how each of the joysticks on the onscreen gamepad should behave, and how they map to the emulated gamepad axes.

    Returns void

  • Parameters

    • config: JoystickConfig
    • callback: ((touched: boolean, xValue: number, yValue: number) => void)
        • (touched: boolean, xValue: number, yValue: number): void
        • Parameters

          • touched: boolean
          • xValue: number
          • yValue: number

          Returns void

    Returns (() => void)

      • (): void
      • Returns void

  • creates a new emmulated gamepad at the given index as would be read in navigator.getGamepads

    Parameters

    • gpadIndex: number

      the index of the gamepad to create, pass null to create a new gamepad at the next available index

    • overlayMode: boolean

      if a real gamepad is connected at the same index as this emulated one and overlayMode is true, the emulated gamepad values will get merged or overlayed on the real gamepad button and axis values, otherwise the emulated gamepad will be shifted to the next available index (appear as a separate gamepad from the real gamepad)

    • buttonCount: number = DEFAULT_GPAD_BUTTON_COUNT

      normally 18, the number of buttons on the gamepad

    • axisCount: number = DEFAULT_GPAD_AXIS_COUNT

      normally 4, the number of axes on the gamepad

    Returns false | EGamepad

  • removes event listeners added with AddDisplayButtonEventListeners()

    Parameters

    • gpadIndex: number

      the index of the emulated gamepad to un-register events for

    Returns void

  • removes event listeners added with AddDisplayJoystickEventListeners()

    Parameters

    • gpadIndex: number

      the index of the emulated gamepad to un-register events for

    Returns void

  • emulates moving an axis on the gamepad at the given axis index

    Parameters

    • gpadIndex: number

      the index of the emulated gamepad to move the axis on

    • axisIndex: number

      the index of the axis to move

    • value: number

      the value to set the axis to between -1 and 1 (0 = center, -1 = left/up, 1 = right/down)

    Returns void

  • emulates pressing a button on an emulated gamepad at the given gamepad button index

    Parameters

    • gpadIndex: number

      the index of the emulated gamepad (as returned by navigator.getGamepads()) to press the button on

    • buttonIndex: number

      the index of the button to press on the gamepad

    • value: number

      the value to set the button to between 0 and 1 (0 = not pressed, 1 = fully pressed, 0.5 = half pressed) if this value is greater than the pressedThreshold from the constructor, the button will be considered pressed

    • touched: boolean

      whether the button is considered "touched" or not, a "pressed" button is always considered "touched"

    Returns void

  • removes the emmulated gamepad at the passed index as would be read from the list in navigator.getGamepads

    Parameters

    • gpadIndex: number

      the index of the gamepad to remove

    Returns void

  • Destructor

    cleans up any event listeners made by this class and restores the normal navigator.getGamepad() function and gamepad events

    Returns void

  • returns copy of the passed Gamepad object The axies and buttons arrays are deep copied. Every other property is a shallow copy

    Parameters

    • original: null | EGamepad | Gamepad

      the gamepad object to copy

    Returns null | EGamepad

  • Intercepts gamepadconnected & gamepaddisconnected events and re-sends them with the correct gamepad indecies

    Returns (() => void)

      • (): void
      • Returns void

  • overwrite the browser gamepad api getGamepads() to return the emulated gamepad data for gamepad indexes corresponding to emulated gamepads if a real gamepad is found with the same index value as an emulated gamepad, the the navigator.getGamepads() list will either shift the emulated gamepad's index up to make room for the real gamepad when (emulatedGamepad.overlayMode = false), or it will return the emulated gamepad "overlayed" on the real one where buttons pressed or axes moved on both the real gamepad and the emulated one will show up on that gamepad.

    Returns void

  • Searches for the next available index a new emulated gamepad could go and returns that index this means no emulated gamepad is at that index and either the no real gamepad is at that index, or a real gamepad is at that index, but the

    Parameters

    • overlayMode: boolean

      is true.

    Returns number

  • Searches for the next available index a freshly connected real gamepad could go and returns that index this means no real gamepad is mapped to that index and either no emulated gamepad is at that index, or the emulated gamepad is in overlay mode

    Parameters

    • startingIndex: number

      the index to start searching from

    Returns number

Generated using TypeDoc