123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- module BABYLON {
- export class StickValues {
- constructor(public x, public y) {
- }
- }
- export interface GamepadButtonChanges {
- changed: boolean;
- pressChanged: boolean;
- touchChanged: boolean;
- valueChanged: boolean;
- }
-
- export class Gamepad {
- public type: number;
- private _leftStick: StickValues;
- private _rightStick: StickValues;
- private _leftStickAxisX: number;
- private _leftStickAxisY: number;
- private _rightStickAxisX: number;
- private _rightStickAxisY: number;
- private _onleftstickchanged: (values: StickValues) => void;
- private _onrightstickchanged: (values: StickValues) => void;
- public static GAMEPAD = 0;
- public static GENERIC = 1;
- public static XBOX = 2;
- public static POSE_ENABLED = 3;
- constructor(public id: string, public index: number, public browserGamepad, leftStickX: number = 0, leftStickY: number = 1, rightStickX: number = 2, rightStickY: number = 3) {
- 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] };
- }
- }
- public onleftstickchanged(callback: (values: StickValues) => void) {
- this._onleftstickchanged = callback;
- }
- public onrightstickchanged(callback: (values: StickValues) => void) {
- this._onrightstickchanged = callback;
- }
- public get leftStick(): StickValues {
- return this._leftStick;
- }
- public set leftStick(newValues: StickValues) {
- if (this._onleftstickchanged && (this._leftStick.x !== newValues.x || this._leftStick.y !== newValues.y)) {
- this._onleftstickchanged(newValues);
- }
- this._leftStick = newValues;
- }
- public get rightStick(): StickValues {
- return this._rightStick;
- }
- public set rightStick(newValues: StickValues) {
- if (this._onrightstickchanged && (this._rightStick.x !== newValues.x || this._rightStick.y !== newValues.y)) {
- this._onrightstickchanged(newValues);
- }
- this._rightStick = newValues;
- }
- public update() {
- if (this._leftStick) {
- this.leftStick = { x: this.browserGamepad.axes[this._leftStickAxisX], y: this.browserGamepad.axes[this._leftStickAxisY] };
- }
- if (this._rightStick) {
- this.rightStick = { x: this.browserGamepad.axes[this._rightStickAxisX], y: this.browserGamepad.axes[this._rightStickAxisY] };
- }
- }
- public dispose() {
- }
- }
- export class GenericPad extends Gamepad {
- private _buttons: Array<number>;
- private _onbuttondown: (buttonPressed: number) => void;
- private _onbuttonup: (buttonReleased: number) => void;
- public onbuttondown(callback: (buttonPressed: number) => void) {
- this._onbuttondown = callback;
- }
- public onbuttonup(callback: (buttonReleased: number) => void) {
- this._onbuttonup = callback;
- }
- constructor(id: string, index: number, browserGamepad) {
- super(id, index, browserGamepad);
- this.type = Gamepad.GENERIC;
- this._buttons = new Array(browserGamepad.buttons.length);
- }
- private _setButtonValue(newValue: number, currentValue: number, buttonIndex: number): number {
- if (newValue !== currentValue) {
- if (this._onbuttondown && newValue === 1) {
- this._onbuttondown(buttonIndex);
- }
- if (this._onbuttonup && newValue === 0) {
- this._onbuttonup(buttonIndex);
- }
- }
- return newValue;
- }
- public update() {
- super.update();
- for (var index = 0; index < this._buttons.length; index++) {
- this._buttons[index] = this._setButtonValue(this.browserGamepad.buttons[index].value, this._buttons[index], index);
- }
- }
- }
- }
|