@babylonjs/core
Version:
Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.
235 lines • 6.9 kB
JavaScript
import { Observable } from "../Misc/observable.js";
/**
* Represents a gamepad control stick position
*/
export class StickValues {
/**
* Initializes the gamepad x and y control stick values
* @param x The x component of the gamepad control stick value
* @param y The y component of the gamepad control stick value
*/
constructor(
/**
* The x component of the control stick
*/
x,
/**
* The y component of the control stick
*/
y) {
this.x = x;
this.y = y;
}
}
/**
* Represents a gamepad
*/
export class Gamepad {
/**
* Specifies if the gamepad has been connected
*/
get isConnected() {
return this._isConnected;
}
/**
* Initializes the gamepad
* @param id The id of the gamepad
* @param index The index of the gamepad
* @param browserGamepad The browser gamepad
* @param leftStickX The x component of the left joystick
* @param leftStickY The y component of the left joystick
* @param rightStickX The x component of the right joystick
* @param rightStickY The y component of the right joystick
*/
constructor(
/**
* The id of the gamepad
*/
id,
/**
* The index of the gamepad
*/
index,
/**
* The browser gamepad
*/
browserGamepad, leftStickX = 0, leftStickY = 1, rightStickX = 2, rightStickY = 3) {
this.id = id;
this.index = index;
this.browserGamepad = browserGamepad;
this._leftStick = { x: 0, y: 0 };
this._rightStick = { x: 0, y: 0 };
/** @internal */
this._isConnected = true;
/**
* Specifies whether the left control stick should be Y-inverted
*/
this._invertLeftStickY = false;
this.type = Gamepad.GAMEPAD;
this._leftStickAxisX = leftStickX;
this._leftStickAxisY = leftStickY;
this._rightStickAxisX = rightStickX;
this._rightStickAxisY = rightStickY;
if (this.browserGamepad.axes.length >= 2) {
this._leftStick = { x: this.browserGamepad.axes[this._leftStickAxisX], y: this.browserGamepad.axes[this._leftStickAxisY] };
}
if (this.browserGamepad.axes.length >= 4) {
this._rightStick = { x: this.browserGamepad.axes[this._rightStickAxisX], y: this.browserGamepad.axes[this._rightStickAxisY] };
}
}
/**
* Callback triggered when the left joystick has changed
* @param callback callback to trigger
*/
onleftstickchanged(callback) {
this._onleftstickchanged = callback;
}
/**
* Callback triggered when the right joystick has changed
* @param callback callback to trigger
*/
onrightstickchanged(callback) {
this._onrightstickchanged = callback;
}
/**
* Gets the left joystick
*/
get leftStick() {
return this._leftStick;
}
/**
* Sets the left joystick values
*/
set leftStick(newValues) {
if (this._onleftstickchanged && (this._leftStick.x !== newValues.x || this._leftStick.y !== newValues.y)) {
this._onleftstickchanged(newValues);
}
this._leftStick = newValues;
}
/**
* Gets the right joystick
*/
get rightStick() {
return this._rightStick;
}
/**
* Sets the right joystick value
*/
set rightStick(newValues) {
if (this._onrightstickchanged && (this._rightStick.x !== newValues.x || this._rightStick.y !== newValues.y)) {
this._onrightstickchanged(newValues);
}
this._rightStick = newValues;
}
/**
* Updates the gamepad joystick positions
*/
update() {
if (this._leftStick) {
this.leftStick = { x: this.browserGamepad.axes[this._leftStickAxisX], y: this.browserGamepad.axes[this._leftStickAxisY] };
if (this._invertLeftStickY) {
this.leftStick.y *= -1;
}
}
if (this._rightStick) {
this.rightStick = { x: this.browserGamepad.axes[this._rightStickAxisX], y: this.browserGamepad.axes[this._rightStickAxisY] };
}
}
/**
* Disposes the gamepad
*/
dispose() { }
}
/**
* Represents a gamepad controller
*/
Gamepad.GAMEPAD = 0;
/**
* Represents a generic controller
*/
Gamepad.GENERIC = 1;
/**
* Represents an XBox controller
*/
Gamepad.XBOX = 2;
/**
* Represents a pose-enabled controller
*/
Gamepad.POSE_ENABLED = 3;
/**
* Represents an Dual Shock controller
*/
Gamepad.DUALSHOCK = 4;
/**
* Represents a generic gamepad
*/
export class GenericPad extends Gamepad {
/**
* Callback triggered when a button has been pressed
* @param callback Called when a button has been pressed
*/
onbuttondown(callback) {
this._onbuttondown = callback;
}
/**
* Callback triggered when a button has been released
* @param callback Called when a button has been released
*/
onbuttonup(callback) {
this._onbuttonup = callback;
}
/**
* Initializes the generic gamepad
* @param id The id of the generic gamepad
* @param index The index of the generic gamepad
* @param browserGamepad The browser gamepad
*/
constructor(id, index, browserGamepad) {
super(id, index, browserGamepad);
/**
* Observable triggered when a button has been pressed
*/
this.onButtonDownObservable = new Observable();
/**
* Observable triggered when a button has been released
*/
this.onButtonUpObservable = new Observable();
this.type = Gamepad.GENERIC;
this._buttons = new Array(browserGamepad.buttons.length);
}
_setButtonValue(newValue, currentValue, buttonIndex) {
if (newValue !== currentValue) {
if (newValue === 1) {
if (this._onbuttondown) {
this._onbuttondown(buttonIndex);
}
this.onButtonDownObservable.notifyObservers(buttonIndex);
}
if (newValue === 0) {
if (this._onbuttonup) {
this._onbuttonup(buttonIndex);
}
this.onButtonUpObservable.notifyObservers(buttonIndex);
}
}
return newValue;
}
/**
* Updates the generic gamepad
*/
update() {
super.update();
for (let index = 0; index < this._buttons.length; index++) {
this._buttons[index] = this._setButtonValue(this.browserGamepad.buttons[index].value, this._buttons[index], index);
}
}
/**
* Disposes the generic gamepad
*/
dispose() {
super.dispose();
this.onButtonDownObservable.clear();
this.onButtonUpObservable.clear();
}
}
//# sourceMappingURL=gamepad.js.map