gamepad.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { Observable } from "../Misc/observable.js";
  2. /**
  3. * Represents a gamepad control stick position
  4. */
  5. export class StickValues {
  6. /**
  7. * Initializes the gamepad x and y control stick values
  8. * @param x The x component of the gamepad control stick value
  9. * @param y The y component of the gamepad control stick value
  10. */
  11. constructor(
  12. /**
  13. * The x component of the control stick
  14. */
  15. x,
  16. /**
  17. * The y component of the control stick
  18. */
  19. y) {
  20. this.x = x;
  21. this.y = y;
  22. }
  23. }
  24. /**
  25. * Represents a gamepad
  26. */
  27. export class Gamepad {
  28. /**
  29. * Specifies if the gamepad has been connected
  30. */
  31. get isConnected() {
  32. return this._isConnected;
  33. }
  34. /**
  35. * Initializes the gamepad
  36. * @param id The id of the gamepad
  37. * @param index The index of the gamepad
  38. * @param browserGamepad The browser gamepad
  39. * @param leftStickX The x component of the left joystick
  40. * @param leftStickY The y component of the left joystick
  41. * @param rightStickX The x component of the right joystick
  42. * @param rightStickY The y component of the right joystick
  43. */
  44. constructor(
  45. /**
  46. * The id of the gamepad
  47. */
  48. id,
  49. /**
  50. * The index of the gamepad
  51. */
  52. index,
  53. /**
  54. * The browser gamepad
  55. */
  56. browserGamepad, leftStickX = 0, leftStickY = 1, rightStickX = 2, rightStickY = 3) {
  57. this.id = id;
  58. this.index = index;
  59. this.browserGamepad = browserGamepad;
  60. this._leftStick = { x: 0, y: 0 };
  61. this._rightStick = { x: 0, y: 0 };
  62. /** @internal */
  63. this._isConnected = true;
  64. /**
  65. * Specifies whether the left control stick should be Y-inverted
  66. */
  67. this._invertLeftStickY = false;
  68. this.type = Gamepad.GAMEPAD;
  69. this._leftStickAxisX = leftStickX;
  70. this._leftStickAxisY = leftStickY;
  71. this._rightStickAxisX = rightStickX;
  72. this._rightStickAxisY = rightStickY;
  73. if (this.browserGamepad.axes.length >= 2) {
  74. this._leftStick = { x: this.browserGamepad.axes[this._leftStickAxisX], y: this.browserGamepad.axes[this._leftStickAxisY] };
  75. }
  76. if (this.browserGamepad.axes.length >= 4) {
  77. this._rightStick = { x: this.browserGamepad.axes[this._rightStickAxisX], y: this.browserGamepad.axes[this._rightStickAxisY] };
  78. }
  79. }
  80. /**
  81. * Callback triggered when the left joystick has changed
  82. * @param callback callback to trigger
  83. */
  84. onleftstickchanged(callback) {
  85. this._onleftstickchanged = callback;
  86. }
  87. /**
  88. * Callback triggered when the right joystick has changed
  89. * @param callback callback to trigger
  90. */
  91. onrightstickchanged(callback) {
  92. this._onrightstickchanged = callback;
  93. }
  94. /**
  95. * Gets the left joystick
  96. */
  97. get leftStick() {
  98. return this._leftStick;
  99. }
  100. /**
  101. * Sets the left joystick values
  102. */
  103. set leftStick(newValues) {
  104. if (this._onleftstickchanged && (this._leftStick.x !== newValues.x || this._leftStick.y !== newValues.y)) {
  105. this._onleftstickchanged(newValues);
  106. }
  107. this._leftStick = newValues;
  108. }
  109. /**
  110. * Gets the right joystick
  111. */
  112. get rightStick() {
  113. return this._rightStick;
  114. }
  115. /**
  116. * Sets the right joystick value
  117. */
  118. set rightStick(newValues) {
  119. if (this._onrightstickchanged && (this._rightStick.x !== newValues.x || this._rightStick.y !== newValues.y)) {
  120. this._onrightstickchanged(newValues);
  121. }
  122. this._rightStick = newValues;
  123. }
  124. /**
  125. * Updates the gamepad joystick positions
  126. */
  127. update() {
  128. if (this._leftStick) {
  129. this.leftStick = { x: this.browserGamepad.axes[this._leftStickAxisX], y: this.browserGamepad.axes[this._leftStickAxisY] };
  130. if (this._invertLeftStickY) {
  131. this.leftStick.y *= -1;
  132. }
  133. }
  134. if (this._rightStick) {
  135. this.rightStick = { x: this.browserGamepad.axes[this._rightStickAxisX], y: this.browserGamepad.axes[this._rightStickAxisY] };
  136. }
  137. }
  138. /**
  139. * Disposes the gamepad
  140. */
  141. dispose() { }
  142. }
  143. /**
  144. * Represents a gamepad controller
  145. */
  146. Gamepad.GAMEPAD = 0;
  147. /**
  148. * Represents a generic controller
  149. */
  150. Gamepad.GENERIC = 1;
  151. /**
  152. * Represents an XBox controller
  153. */
  154. Gamepad.XBOX = 2;
  155. /**
  156. * Represents a pose-enabled controller
  157. */
  158. Gamepad.POSE_ENABLED = 3;
  159. /**
  160. * Represents an Dual Shock controller
  161. */
  162. Gamepad.DUALSHOCK = 4;
  163. /**
  164. * Represents a generic gamepad
  165. */
  166. export class GenericPad extends Gamepad {
  167. /**
  168. * Callback triggered when a button has been pressed
  169. * @param callback Called when a button has been pressed
  170. */
  171. onbuttondown(callback) {
  172. this._onbuttondown = callback;
  173. }
  174. /**
  175. * Callback triggered when a button has been released
  176. * @param callback Called when a button has been released
  177. */
  178. onbuttonup(callback) {
  179. this._onbuttonup = callback;
  180. }
  181. /**
  182. * Initializes the generic gamepad
  183. * @param id The id of the generic gamepad
  184. * @param index The index of the generic gamepad
  185. * @param browserGamepad The browser gamepad
  186. */
  187. constructor(id, index, browserGamepad) {
  188. super(id, index, browserGamepad);
  189. /**
  190. * Observable triggered when a button has been pressed
  191. */
  192. this.onButtonDownObservable = new Observable();
  193. /**
  194. * Observable triggered when a button has been released
  195. */
  196. this.onButtonUpObservable = new Observable();
  197. this.type = Gamepad.GENERIC;
  198. this._buttons = new Array(browserGamepad.buttons.length);
  199. }
  200. _setButtonValue(newValue, currentValue, buttonIndex) {
  201. if (newValue !== currentValue) {
  202. if (newValue === 1) {
  203. if (this._onbuttondown) {
  204. this._onbuttondown(buttonIndex);
  205. }
  206. this.onButtonDownObservable.notifyObservers(buttonIndex);
  207. }
  208. if (newValue === 0) {
  209. if (this._onbuttonup) {
  210. this._onbuttonup(buttonIndex);
  211. }
  212. this.onButtonUpObservable.notifyObservers(buttonIndex);
  213. }
  214. }
  215. return newValue;
  216. }
  217. /**
  218. * Updates the generic gamepad
  219. */
  220. update() {
  221. super.update();
  222. for (let index = 0; index < this._buttons.length; index++) {
  223. this._buttons[index] = this._setButtonValue(this.browserGamepad.buttons[index].value, this._buttons[index], index);
  224. }
  225. }
  226. /**
  227. * Disposes the generic gamepad
  228. */
  229. dispose() {
  230. super.dispose();
  231. this.onButtonDownObservable.clear();
  232. this.onButtonUpObservable.clear();
  233. }
  234. }
  235. //# sourceMappingURL=gamepad.js.map