babylon.freeCameraMouseInput.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. module BABYLON {
  2. /**
  3. * Manage the mouse inputs to control the movement of a free camera.
  4. * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
  5. */
  6. export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
  7. /**
  8. * Defines the camera the input is attached to.
  9. */
  10. public camera: FreeCamera;
  11. /**
  12. * Defines the buttons associated with the input to handle camera move.
  13. */
  14. @serialize()
  15. public buttons = [0, 1, 2];
  16. /**
  17. * Defines the pointer angular sensibility along the X and Y axis or how fast is the camera rotating.
  18. */
  19. @serialize()
  20. public angularSensibility = 2000.0;
  21. private _pointerInput: (p: PointerInfo, s: EventState) => void;
  22. private _onMouseMove: Nullable<(e: MouseEvent) => any>;
  23. private _observer: Nullable<Observer<PointerInfo>>;
  24. private previousPosition: Nullable<{ x: number, y: number }> = null;
  25. /**
  26. * Manage the mouse inputs to control the movement of a free camera.
  27. * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
  28. * @param touchEnabled Defines if touch is enabled or not
  29. */
  30. constructor(
  31. /**
  32. * Define if touch is enabled in the mouse input
  33. */
  34. public touchEnabled = true) {
  35. }
  36. /**
  37. * Attach the input controls to a specific dom element to get the input from.
  38. * @param element Defines the element the controls should be listened from
  39. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  40. */
  41. public attachControl(element: HTMLElement, noPreventDefault?: boolean):void {
  42. var engine = this.camera.getEngine();
  43. if (!this._pointerInput) {
  44. this._pointerInput = (p, s) => {
  45. var evt = <PointerEvent>p.event;
  46. if (engine.isInVRExclusivePointerMode) {
  47. return;
  48. }
  49. if (!this.touchEnabled && evt.pointerType === "touch") {
  50. return;
  51. }
  52. if (p.type !== PointerEventTypes.POINTERMOVE && this.buttons.indexOf(evt.button) === -1) {
  53. return;
  54. }
  55. let srcElement = <HTMLElement>(evt.srcElement || evt.target);
  56. if (p.type === PointerEventTypes.POINTERDOWN && srcElement) {
  57. try {
  58. srcElement.setPointerCapture(evt.pointerId);
  59. } catch (e) {
  60. //Nothing to do with the error. Execution will continue.
  61. }
  62. this.previousPosition = {
  63. x: evt.clientX,
  64. y: evt.clientY
  65. };
  66. if (!noPreventDefault) {
  67. evt.preventDefault();
  68. element.focus();
  69. }
  70. }
  71. else if (p.type === PointerEventTypes.POINTERUP && srcElement) {
  72. try {
  73. srcElement.releasePointerCapture(evt.pointerId);
  74. } catch (e) {
  75. //Nothing to do with the error.
  76. }
  77. this.previousPosition = null;
  78. if (!noPreventDefault) {
  79. evt.preventDefault();
  80. }
  81. }
  82. else if (p.type === PointerEventTypes.POINTERMOVE) {
  83. if (!this.previousPosition || engine.isPointerLock) {
  84. return;
  85. }
  86. var offsetX = evt.clientX - this.previousPosition.x;
  87. if (this.camera.getScene().useRightHandedSystem) offsetX *= -1;
  88. if (this.camera.parent && this.camera.parent._getWorldMatrixDeterminant() < 0) offsetX *= -1;
  89. this.camera.cameraRotation.y += offsetX / this.angularSensibility;
  90. var offsetY = evt.clientY - this.previousPosition.y;
  91. this.camera.cameraRotation.x += offsetY / this.angularSensibility;
  92. this.previousPosition = {
  93. x: evt.clientX,
  94. y: evt.clientY
  95. };
  96. if (!noPreventDefault) {
  97. evt.preventDefault();
  98. }
  99. }
  100. }
  101. }
  102. this._onMouseMove = evt => {
  103. if (!engine.isPointerLock) {
  104. return;
  105. }
  106. if (engine.isInVRExclusivePointerMode) {
  107. return;
  108. }
  109. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  110. if (this.camera.getScene().useRightHandedSystem) offsetX *= -1;
  111. if (this.camera.parent && this.camera.parent._getWorldMatrixDeterminant() < 0) offsetX *= -1;
  112. this.camera.cameraRotation.y += offsetX / this.angularSensibility;
  113. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  114. this.camera.cameraRotation.x += offsetY / this.angularSensibility;
  115. this.previousPosition = null;
  116. if (!noPreventDefault) {
  117. evt.preventDefault();
  118. }
  119. };
  120. this._observer = this.camera.getScene().onPointerObservable.add(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);
  121. element.addEventListener("mousemove", this._onMouseMove, false);
  122. }
  123. /**
  124. * Detach the current controls from the specified dom element.
  125. * @param element Defines the element to stop listening the inputs from
  126. */
  127. public detachControl(element: Nullable<HTMLElement>):void {
  128. if (this._observer && element) {
  129. this.camera.getScene().onPointerObservable.remove(this._observer);
  130. if (this._onMouseMove) {
  131. element.removeEventListener("mousemove", this._onMouseMove);
  132. }
  133. this._observer = null;
  134. this._onMouseMove = null;
  135. this.previousPosition = null;
  136. }
  137. }
  138. /**
  139. * Gets the class name of the current intput.
  140. * @returns the class name
  141. */
  142. public getClassName(): string {
  143. return "FreeCameraMouseInput";
  144. }
  145. /**
  146. * Get the friendly name associated with the input class.
  147. * @returns the input friendly name
  148. */
  149. public getSimpleName(): string {
  150. return "mouse";
  151. }
  152. }
  153. (<any>CameraInputTypes)["FreeCameraMouseInput"] = FreeCameraMouseInput;
  154. }