1.3.4 • Published 8 months ago

gamepad-wrapper v1.3.4

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Gamepad Wrapper 🎮

npm version language npm download license

A Game-Loop-Based, Non-Event-Driven, Lightweight Gamepad and WebXR Gamepad Wrapper

Table of contents

Key features | Installation | Usage | API | License

Key features

  • Supports both Gamepad API and the derived WebXR Gamepad API
  • Auto detects and applies gamepad mapping type
  • Built-in support for standard and xr-standard button mapping, no more remembering button index
  • Supports threshold tuning for analog input (specify deadzone by defining min/max input value)
  • Has Typescript types!

Installation

To install and set up the library, run:

$ npm install gamepad-wrapper

Or if you prefer using Yarn:

$ yarn add gamepad-wrapper

Usage

To properly import GameWrapper:

// import just the GamepadWrapper class
import { GamepadWrapper } from 'gamepad-wrapper';
// or import button/axes Enums with it
import { GamepadWrapper, BUTTONS, AXES } from 'gamepad-wrapper';

To register and update gamepads with GameWrapper:

let gamepadWrapper;

// get standard gamepad object from gamepadconnected event
window.addEventListener('gamepadconnected', (event) => {
	gamepadWrapper = new GamepadWrapper(event.gamepad);
});

// get xr-standard gamepad object from XRInputSource
// 3D engines like Three.js or Babylon.js usually have
// their higher-level API to get gamepad
for (const source of frame.session.inputSources) {
	gamepadWrapper = new GamepadWrapper(source.gamepad);
}

// make sure to update the gamepadWrapper before other
// interactive game logic to get the latest results
function gameLoop() {
	gamepadWrapper.update();
	// do other stuff
}

To check button states (including linear inputs like triggers):

// the trigger of a WebXR gamdpad is pressed down in current frame
const triggerIsCurrentlyDown = gamepadWrapper.getButton(
	BUTTONS.XR_STANDARD.TRIGGER,
);

// the trigger is down in current frame but not in previous frame
const triggerWasJustPressedDown = gamepadWrapper.getButtonDown(
	BUTTONS.XR_STANDARD.TRIGGER,
);

// the trigger was down in previous frame but not in current frame
const triggerWasJustReleased = gamepadWrapper.getButtonUp(
	BUTTONS.XR_STANDARD.TRIGGER,
);

To check thumbstick states:

// the left thumbstick x-axis value (between 0 to 1.0)
const thumbstickValueX = gamepadWrapper.getAxis(
	AXES.STANDARD.THUMBSTICK_LEFT_X,
);

// 2D vector length from x-axis and y-axis value of the left thumbstick
const thumbstickValue = gamepadWrapper.get2DInputValue(
	BUTTONS.STANDARD.THUMBSTICK_LEFT,
);

API

Constructor

GamepadWrapper( gamepad : Gamepad, options : ConfigOptions | any )

ConfigOptions can be used to tune the deadzone for certain input on the gamepad. For example when an old and dusty gamepad cannot reliably register trigger input as 1.0 when fully pressed down, specifying buttonPressValueMax as 0.98 can help resolve the issue. User can also specify buttonClickThreshold as the threshold to register a click event.

interface ConfigOptions {
	buttonPressValueMin: number | null;
	buttonPressValueMax: number | null;
	buttonClickThreshold: number | null;
}

Properties

.gamepad : Gamepad

The raw gamepad object, data source of the GamepadWrapper instance.

Methods

.getButtonValue

getButtonValue( buttonId : string ) : number

Returns the value of the button identified by buttonId. Value should range from 0.0 to 1.0.

.getButtonValueByIndex

getButtonValueByIndex( buttonIdx : number ) : number

Bypasses the button id mapping and directly query by gamepad button index. Returns the value of the button identified by buttonId. Value should range from 0.0 to 1.0.

.getButton

getButton( buttonId : string ) : boolean

Returns true while the button identified by buttonId is held down.

.getButtonByIndex

getButtonByIndex( buttonIdx : number ) : boolean

Bypasses the button id mapping and directly query by gamepad button index. Returns true while the button identified by buttonId is held down.

.getButtonDown

getButtonDown( buttonId : string ) : boolean

Returns true during the frame the user pressed down the button identified by buttonId.

.getButtonDownByIndex

getButtonDownByIndex( buttonIdx : number ) : boolean

Bypasses the button id mapping and directly query by gamepad button index. Returns true during the frame the user pressed down the button identified by buttonId.

.getButtonUp

getButtonUp( buttonId : string ) : boolean

Returns true the first frame the user releases the button identified by buttonId.

.getButtonUpByIndex

getButtonUpByIndex( buttonIdx : number ) : boolean

Bypasses the button id mapping and directly query by gamepad button index. Returns true the first frame the user releases the button identified by buttonId.

.getButtonClick

getButtonClick( buttonId : string ) : boolean

Returns true the first frame the user pressed the button identified by buttonId down to a point exceeding the buttonClickThreshold.

.getButtonClickByIndex

getButtonClickByIndex( buttonIdx : number ) : boolean

Bypasses the button id mapping and directly query by gamepad button index. Returns true the first frame the user pressed the button identified by buttonId down to a point exceeding the buttonClickThreshold.

.getAxis

getAxis( axisId : string ) : number

Returns the value of the axis identified by axisId. Value should range from -1.0 to 1.0.

.getAxisByIndex

getAxisByIndex( axisIdx : number ) : number

Bypasses the button id mapping and directly query by gamepad button index. Returns the value of the axis identified by axisId. Value should range from -1.0 to 1.0.

.get2DInputAngle

get2DInputAngle( buttonId : string ) : number

Returns the angle between the input 2D vector and 2D vector (0, 1).

.get2DInputValue

get2DInputValue( buttonId : string ) : number

Returns the Euclidean length of the input 2D vector.

.getHapticActuator

getHapticActuator( actuatorIdx : number ) : GamepadHapticActuator | never

Returns the GamepadHapticActuator of actuatorIdx, returns null if actuator is not available.

Input Mapping Enums

Standard Gamepad Mapping

Button IDDescriptionXBox
BUTTONS.STANDARD.RC_BOTTOMBottom button in right clusterA
BUTTONS.STANDARD.RC_RIGHTRight button in right clusterB
BUTTONS.STANDARD.RC_LEFTLeft button in right clusterX
BUTTONS.STANDARD.RC_TOPTop button in right clusterY
BUTTONS.STANDARD.BUMPER_LEFTTop left front buttonLeft Bumper
BUTTONS.STANDARD.BUMPER_RIGHTTop right front buttonRight Bumper
BUTTONS.STANDARD.TRIGGER_LEFTBottom left front buttonLeft Trigger
BUTTONS.STANDARD.TRIGGER_RIGHTBottom right front buttonRight Trigger
BUTTONS.STANDARD.CC_LEFTLeft button in center clusterView Button
BUTTONS.STANDARD.CC_RIGHTRight button in center clusterMenu Button
BUTTONS.STANDARD.THUMBSTICK_LEFTLeft stick pressed buttonLeft Stick
BUTTONS.STANDARD.THUMBSTICK_RIGHTRight stick pressed buttonRight Stick
BUTTONS.STANDARD.LC_BOTTOMBottom button in left clusterD-pad Down
BUTTONS.STANDARD.LC_RIGHTRight button in left clusterD-pad Right
BUTTONS.STANDARD.LC_LEFTLeft button in left clusterD-pad Left
BUTTONS.STANDARD.LC_TOPTop button in left clusterD-pad Up
BUTTONS.STANDARD.CC_CENTERCenter button in center clusterXbox Button

License

MIT License © Felix Zhang

1.3.4

8 months ago

1.3.3

8 months ago

1.3.2

8 months ago

1.3.1

8 months ago

1.3.0

8 months ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.1.0

2 years ago