babylon.gamepad.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. module BABYLON {
  2. export class StickValues {
  3. constructor(public x, public y) {
  4. }
  5. }
  6. export interface GamepadButtonChanges {
  7. changed: boolean;
  8. pressChanged: boolean;
  9. touchChanged: boolean;
  10. valueChanged: boolean;
  11. }
  12. export class Gamepad {
  13. public type: number;
  14. private _leftStick: StickValues;
  15. private _rightStick: StickValues;
  16. private _leftStickAxisX: number;
  17. private _leftStickAxisY: number;
  18. private _rightStickAxisX: number;
  19. private _rightStickAxisY: number;
  20. private _onleftstickchanged: (values: StickValues) => void;
  21. private _onrightstickchanged: (values: StickValues) => void;
  22. public static GAMEPAD = 0;
  23. public static GENERIC = 1;
  24. public static XBOX = 2;
  25. public static POSE_ENABLED = 3;
  26. constructor(public id: string, public index: number, public browserGamepad, leftStickX: number = 0, leftStickY: number = 1, rightStickX: number = 2, rightStickY: number = 3) {
  27. this.type = Gamepad.GAMEPAD;
  28. this._leftStickAxisX = leftStickX;
  29. this._leftStickAxisY = leftStickY;
  30. this._rightStickAxisX = rightStickX;
  31. this._rightStickAxisY = rightStickY;
  32. if (this.browserGamepad.axes.length >= 2) {
  33. this._leftStick = { x: this.browserGamepad.axes[this._leftStickAxisX], y: this.browserGamepad.axes[this._leftStickAxisY] };
  34. }
  35. if (this.browserGamepad.axes.length >= 4) {
  36. this._rightStick = { x: this.browserGamepad.axes[this._rightStickAxisX], y: this.browserGamepad.axes[this._rightStickAxisY] };
  37. }
  38. }
  39. public onleftstickchanged(callback: (values: StickValues) => void) {
  40. this._onleftstickchanged = callback;
  41. }
  42. public onrightstickchanged(callback: (values: StickValues) => void) {
  43. this._onrightstickchanged = callback;
  44. }
  45. public get leftStick(): StickValues {
  46. return this._leftStick;
  47. }
  48. public set leftStick(newValues: StickValues) {
  49. if (this._onleftstickchanged && (this._leftStick.x !== newValues.x || this._leftStick.y !== newValues.y)) {
  50. this._onleftstickchanged(newValues);
  51. }
  52. this._leftStick = newValues;
  53. }
  54. public get rightStick(): StickValues {
  55. return this._rightStick;
  56. }
  57. public set rightStick(newValues: StickValues) {
  58. if (this._onrightstickchanged && (this._rightStick.x !== newValues.x || this._rightStick.y !== newValues.y)) {
  59. this._onrightstickchanged(newValues);
  60. }
  61. this._rightStick = newValues;
  62. }
  63. public update() {
  64. if (this._leftStick) {
  65. this.leftStick = { x: this.browserGamepad.axes[this._leftStickAxisX], y: this.browserGamepad.axes[this._leftStickAxisY] };
  66. }
  67. if (this._rightStick) {
  68. this.rightStick = { x: this.browserGamepad.axes[this._rightStickAxisX], y: this.browserGamepad.axes[this._rightStickAxisY] };
  69. }
  70. }
  71. public dispose() {
  72. }
  73. }
  74. export class GenericPad extends Gamepad {
  75. private _buttons: Array<number>;
  76. private _onbuttondown: (buttonPressed: number) => void;
  77. private _onbuttonup: (buttonReleased: number) => void;
  78. public onbuttondown(callback: (buttonPressed: number) => void) {
  79. this._onbuttondown = callback;
  80. }
  81. public onbuttonup(callback: (buttonReleased: number) => void) {
  82. this._onbuttonup = callback;
  83. }
  84. constructor(id: string, index: number, browserGamepad) {
  85. super(id, index, browserGamepad);
  86. this.type = Gamepad.GENERIC;
  87. this._buttons = new Array(browserGamepad.buttons.length);
  88. }
  89. private _setButtonValue(newValue: number, currentValue: number, buttonIndex: number): number {
  90. if (newValue !== currentValue) {
  91. if (this._onbuttondown && newValue === 1) {
  92. this._onbuttondown(buttonIndex);
  93. }
  94. if (this._onbuttonup && newValue === 0) {
  95. this._onbuttonup(buttonIndex);
  96. }
  97. }
  98. return newValue;
  99. }
  100. public update() {
  101. super.update();
  102. for (var index = 0; index < this._buttons.length; index++) {
  103. this._buttons[index] = this._setButtonValue(this.browserGamepad.buttons[index].value, this._buttons[index], index);
  104. }
  105. }
  106. }
  107. }