# core/gamepadmanager (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/gamepadmanager/

Input manager.

## GamepadManager extends Object

Input manager.

Example: Read pad buttons and sticks

```js
import { Engine, EngineConfiguration, Scene } from "./build/vanilla.js";

class MainScene extends Scene {
	tick(timeDelta) {
		super.tick(timeDelta);
		const engine = this.getEngine();
		const inputManager = engine.getInputManager();
		const gamepadManager = inputManager.getGamepadManager();
		const gamepads = gamepadManager.getAllConnectedGamepads();
		for (const gamepad of gamepads) {
			if (gamepadManager.isButtonJustPressed(gamepad.index, 0)) {
				console.log("jump", gamepad.id);
			}
			const horizontal = gamepadManager.getAxisValue(gamepad.index, 0);
			if (Math.abs(horizontal) > 0.2) {
				console.log("move", horizontal);
			}
		}
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const inputManager = engine.getInputManager();
const gamepadManager = inputManager.getGamepadManager();
gamepadManager.setCallback((gamepadIndex, inputType, inputIndex, value) => {
	console.log(gamepadIndex, inputType, inputIndex, value);
});
engine.run(new MainScene());
```

- `constructor(engine)` (constructor, since 0.0.9): Constructor.
  - `engine` Engine: The engine that owns this manager. (not used)
- `tick(timeDelta)` (method, since 0.0.9): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `connect(gamepad)` (method, since 0.0.9): Gamepad connected.
  - `gamepad` Gamepad: Gamepad to attach. (does nothing if null or its index is already attached)
- `disconnect(gamepad)` (method, since 0.0.9): Gamepad disconnected.
  - `gamepad` Gamepad: Gamepad to release. (does nothing if its index is not attached)
- `setCallback(callback)` (method, since 0.0.9): Sets the callback.
  - `callback` (gamepadIndex: number, inputType: "button" | "value" | "axis", inputIndex: number, value: any) => void: Function called when a button or axis changes. (button gives pressed true or false, value gives an analog value above 0, axis gives -1 ~ 1; not called if null)
- `updateAllGamepads()` (method, since 0.0.9): Updates all gamepads.
- `isButtonPressed(gamepadIndex, buttonIndex)` (method, since 0.0.9): Returns the button pressed state.
  - `gamepadIndex` number: Pad index. (Gamepad.index)
  - `buttonIndex` number: Button index. (W3C standard mapping, 0 is the A or cross button)
  - returns boolean: True if held down. (false for a missing pad or button)
- `isButtonJustPressed(gamepadIndex, buttonIndex)` (method, since 0.4.0): Whether the button was just pressed in this update. (same use as keyboard isKeyJustPressed)
  - `gamepadIndex` number: Pad index. (Gamepad.index)
  - `buttonIndex` number: Button index. (W3C standard mapping, 0 is the A or cross button)
  - returns boolean: True if pressed in this update.
- `isButtonJustReleased(gamepadIndex, buttonIndex)` (method, since 0.4.0): Whether the button was just released in this update.
  - `gamepadIndex` number: Pad index. (Gamepad.index)
  - `buttonIndex` number: Button index. (W3C standard mapping, 0 is the A or cross button)
  - returns boolean: True if released in this update.
- `getButtonValue(gamepadIndex, buttonIndex)` (method, since 0.0.9): Returns the button/trigger analog value (0.0 ~ 1.0).
  - `gamepadIndex` number: Pad index. (Gamepad.index)
  - `buttonIndex` number: Button index. (W3C standard mapping, 0 is the A or cross button)
  - returns number: Analog value. (0 ~ 1, to two decimal places, 0 if missing)
- `getAxisValue(gamepadIndex, axisIndex)` (method, since 0.0.9): Returns the analog stick axis value (-1.0 ~ 1.0).
  - `gamepadIndex` number: Pad index. (Gamepad.index)
  - `axisIndex` number: Axis index. (0 left horizontal, 1 left vertical, 2 right horizontal, 3 right vertical)
  - returns number: Axis value. (-1 ~ 1, to two decimal places, 0 if missing)
- `getAllConnectedGamepads()` (method, since 0.0.9): Returns all connected gamepads.
  - returns Gamepad[]: List of connected gamepads. (the browser's current Gamepad objects)
- `getConnectedGamepadCount()` (method, since 0.0.9): Returns the number of connected gamepads.
  - returns number: Number of connected gamepads.
- `getConnectedGamepad(gamepadIndex)` (method, since 0.0.9): Returns the connected gamepad.
  - `gamepadIndex` number: Index in connection order. (0 ~ connected count - 1, not Gamepad.index)
  - returns Gamepad | undefined: The gamepad at that position. (undefined if out of range)
- `setInputLogEnabled(isEnabled)` (method, since 0.4.0): Sets whether to log to the console whenever buttons and axes change. (off by default, connection and disconnection are always logged)
  - `isEnabled` boolean: If true, logs to the console.
- `isInputLogEnabled()` (method, since 0.4.0): Returns whether to log to the console whenever buttons and axes change.
  - returns boolean: True if logging to the console.
- `getActiveGamepadId()` (method, since 0.4.0): Returns the name of the pad whose button was pressed or axis tilted far in the last update. (empty string if none, to tell the last used device)
  - returns string: Pad name. (Gamepad.id, empty text if none)
- `vibrate(strongMagnitude, weakMagnitude, seconds)` (method, since 0.4.0): Vibrates all connected pads. (strength 0 ~ 1, dual-rumble of the browser's Gamepad.vibrationActuator)
  - Pads without a vibration actuator and browsers (Safari, etc.) are skipped.
  - Calling again before the previous vibration ends replaces it with the new one. (the browser rejecting the previous promise is ignored)
  - `strongMagnitude` number: Strength of the heavy motor. (deep rumble)
  - `weakMagnitude` number: Strength of the light motor. (fine vibration)
  - `seconds` number: Vibration time. (seconds)

  Example: Rumble connected pads for 0.25 seconds

  ```js
  import { Engine, EngineConfiguration } from "./build/vanilla.js";
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  const inputManager = engine.getInputManager();
  const gamepadManager = inputManager.getGamepadManager();
  gamepadManager.vibrate(1, 0.3, 0.25);
  const connectedGamepadCount = gamepadManager.getConnectedGamepadCount();
  console.log(connectedGamepadCount);
  ```

## Functions and constants

- `readGamepadKind(gamepadId)` (method, since 0.4.0): Classifies the type by pad name. (PlayStation 054c, Nintendo 057e, Xbox 045e and names, other if unknown)
  - `gamepadId` string: Gamepad.id
  - returns string: A GamepadKind value.

## GamepadAnalogStickCode (enum)

Gamepad analog stick identifiers. (W3C Standard Gamepad mapping)

- `LEFT_X` = `0`: Left stick horizontal axis.
- `LEFT_Y` = `1`: Left stick vertical axis.
- `RIGHT_X` = `2`: Right stick horizontal axis.
- `RIGHT_Y` = `3`: Right stick vertical axis.

## GamepadTriggerCode (enum)

Gamepad trigger identifiers. (W3C Standard Gamepad mapping)

- `L2` = `6`: Left trigger.
- `R2` = `7`: Right trigger.

## GamepadButtonCode (enum)

Gamepad button identifiers. (W3C Standard Gamepad mapping)

- `A_CROSS` = `0`: Face Buttons
- `B_CIRCLE` = `1`: Right face button. (Xbox B, PlayStation circle)
- `X_SQUARE` = `2`: Left face button. (Xbox X, PlayStation square)
- `Y_TRIANGLE` = `3`: Top face button. (Xbox Y, PlayStation triangle)
- `L1` = `4`: Bumpers
- `R1` = `5`: Right bumper.
- `L2` = `6`: Triggers (also recognized as buttons)
- `R2` = `7`: Right trigger button.
- `SHARE_VIEW` = `8`: System
- `OPTIONS_MENU` = `9`: Options, menu button.
- `L3` = `10`: Stick Clicks
- `R3` = `11`: Right stick click.
- `DPAD_UP` = `12`: D-Pad
- `DPAD_DOWN` = `13`: D-pad down.
- `DPAD_LEFT` = `14`: D-pad left.
- `DPAD_RIGHT` = `15`: D-pad right.
- `HOME_PS` = `16`: Center
- `TOUCHPAD` = `17`: Touchpad click.

## GamepadKind (enum)

Gamepad type. (used to show button hints in that pad's notation)

- `xbox` = `xbox`: Xbox pad.
- `playstation` = `playstation`: PlayStation pad.
- `nintendo` = `nintendo`: Nintendo pad.
- `other` = `other`: Other pad, or unknown.
