freeCameraMouseInput.ts 7.7 KB

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