babylon.gearVRController.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. module BABYLON {
  2. /**
  3. * Gear VR Controller
  4. */
  5. export class GearVRController extends WebVRController {
  6. /**
  7. * Base Url for the controller model.
  8. */
  9. public static MODEL_BASE_URL:string = 'https://controllers.babylonjs.com/generic/';
  10. /**
  11. * File name for the controller model.
  12. */
  13. public static MODEL_FILENAME:string = 'generic.babylon';
  14. /**
  15. * Gamepad Id prefix used to identify this controller.
  16. */
  17. public static readonly GAMEPAD_ID_PREFIX: string = 'Gear VR'; // id is 'Gear VR Controller'
  18. private readonly _buttonIndexToObservableNameMap = [
  19. 'onTrackpadChangedObservable', // Trackpad
  20. 'onTriggerStateChangedObservable' // Trigger
  21. ]
  22. /**
  23. * Creates a new GearVRController from a gamepad
  24. * @param vrGamepad the gamepad that the controller should be created from
  25. */
  26. constructor(vrGamepad: any) {
  27. super(vrGamepad);
  28. this.controllerType = PoseEnabledControllerType.GEAR_VR;
  29. }
  30. /**
  31. * Implements abstract method on WebVRController class, loading controller meshes and calling this.attachToMesh if successful.
  32. * @param scene scene in which to add meshes
  33. * @param meshLoaded optional callback function that will be called if the mesh loads successfully.
  34. */
  35. public initControllerMesh(scene: Scene, meshLoaded?: (mesh: AbstractMesh) => void) {
  36. SceneLoader.ImportMesh("", GearVRController.MODEL_BASE_URL, GearVRController.MODEL_FILENAME, scene, (newMeshes) => {
  37. this._defaultModel = newMeshes[1];
  38. this.attachToMesh(this._defaultModel);
  39. if (meshLoaded) {
  40. meshLoaded(this._defaultModel);
  41. }
  42. });
  43. }
  44. /**
  45. * Called once for each button that changed state since the last frame
  46. * @param buttonIdx Which button index changed
  47. * @param state New state of the button
  48. * @param changes Which properties on the state changed since last frame
  49. */
  50. protected _handleButtonChange(buttonIdx: number, state: ExtendedGamepadButton, changes: GamepadButtonChanges) {
  51. if (buttonIdx < this._buttonIndexToObservableNameMap.length) {
  52. const observableName : string = this._buttonIndexToObservableNameMap[buttonIdx];
  53. // Only emit events for buttons that we know how to map from index to observable
  54. let observable = (<any>this)[observableName];
  55. if (observable) {
  56. observable.notifyObservers(state);
  57. }
  58. }
  59. }
  60. }
  61. }