BaseCameraMouseWheelInput.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { __decorate } from "../../tslib.es6.js";
  2. import { serialize } from "../../Misc/decorators.js";
  3. import { Observable } from "../../Misc/observable.js";
  4. import { PointerEventTypes } from "../../Events/pointerEvents.js";
  5. import { EventConstants } from "../../Events/deviceInputEvents.js";
  6. import { Tools } from "../../Misc/tools.js";
  7. /**
  8. * Base class for mouse wheel input..
  9. * See FollowCameraMouseWheelInput in src/Cameras/Inputs/freeCameraMouseWheelInput.ts
  10. * for example usage.
  11. */
  12. export class BaseCameraMouseWheelInput {
  13. constructor() {
  14. /**
  15. * How fast is the camera moves in relation to X axis mouseWheel events.
  16. * Use negative value to reverse direction.
  17. */
  18. this.wheelPrecisionX = 3.0;
  19. /**
  20. * How fast is the camera moves in relation to Y axis mouseWheel events.
  21. * Use negative value to reverse direction.
  22. */
  23. this.wheelPrecisionY = 3.0;
  24. /**
  25. * How fast is the camera moves in relation to Z axis mouseWheel events.
  26. * Use negative value to reverse direction.
  27. */
  28. this.wheelPrecisionZ = 3.0;
  29. /**
  30. * Observable for when a mouse wheel move event occurs.
  31. */
  32. this.onChangedObservable = new Observable();
  33. /**
  34. * Incremental value of multiple mouse wheel movements of the X axis.
  35. * Should be zero-ed when read.
  36. */
  37. this._wheelDeltaX = 0;
  38. /**
  39. * Incremental value of multiple mouse wheel movements of the Y axis.
  40. * Should be zero-ed when read.
  41. */
  42. this._wheelDeltaY = 0;
  43. /**
  44. * Incremental value of multiple mouse wheel movements of the Z axis.
  45. * Should be zero-ed when read.
  46. */
  47. this._wheelDeltaZ = 0;
  48. /**
  49. * Firefox uses a different scheme to report scroll distances to other
  50. * browsers. Rather than use complicated methods to calculate the exact
  51. * multiple we need to apply, let's just cheat and use a constant.
  52. * https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaMode
  53. * https://stackoverflow.com/questions/20110224/what-is-the-height-of-a-line-in-a-wheel-event-deltamode-dom-delta-line
  54. */
  55. this._ffMultiplier = 12;
  56. /**
  57. * Different event attributes for wheel data fall into a few set ranges.
  58. * Some relevant but dated date here:
  59. * https://stackoverflow.com/questions/5527601/normalizing-mousewheel-speed-across-browsers
  60. */
  61. this._normalize = 120;
  62. }
  63. /**
  64. * Attach the input controls to a specific dom element to get the input from.
  65. * @param noPreventDefault Defines whether event caught by the controls
  66. * should call preventdefault().
  67. * (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  68. */
  69. attachControl(noPreventDefault) {
  70. noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
  71. this._wheel = (pointer) => {
  72. // sanity check - this should be a PointerWheel event.
  73. if (pointer.type !== PointerEventTypes.POINTERWHEEL) {
  74. return;
  75. }
  76. const event = pointer.event;
  77. const platformScale = event.deltaMode === EventConstants.DOM_DELTA_LINE ? this._ffMultiplier : 1; // If this happens to be set to DOM_DELTA_LINE, adjust accordingly
  78. this._wheelDeltaX += (this.wheelPrecisionX * platformScale * event.deltaX) / this._normalize;
  79. this._wheelDeltaY -= (this.wheelPrecisionY * platformScale * event.deltaY) / this._normalize;
  80. this._wheelDeltaZ += (this.wheelPrecisionZ * platformScale * event.deltaZ) / this._normalize;
  81. if (event.preventDefault) {
  82. if (!noPreventDefault) {
  83. event.preventDefault();
  84. }
  85. }
  86. };
  87. this._observer = this.camera.getScene()._inputManager._addCameraPointerObserver(this._wheel, PointerEventTypes.POINTERWHEEL);
  88. }
  89. /**
  90. * Detach the current controls from the specified dom element.
  91. */
  92. detachControl() {
  93. if (this._observer) {
  94. this.camera.getScene()._inputManager._removeCameraPointerObserver(this._observer);
  95. this._observer = null;
  96. this._wheel = null;
  97. }
  98. if (this.onChangedObservable) {
  99. this.onChangedObservable.clear();
  100. }
  101. }
  102. /**
  103. * Called for each rendered frame.
  104. */
  105. checkInputs() {
  106. this.onChangedObservable.notifyObservers({
  107. wheelDeltaX: this._wheelDeltaX,
  108. wheelDeltaY: this._wheelDeltaY,
  109. wheelDeltaZ: this._wheelDeltaZ,
  110. });
  111. // Clear deltas.
  112. this._wheelDeltaX = 0;
  113. this._wheelDeltaY = 0;
  114. this._wheelDeltaZ = 0;
  115. }
  116. /**
  117. * Gets the class name of the current input.
  118. * @returns the class name
  119. */
  120. getClassName() {
  121. return "BaseCameraMouseWheelInput";
  122. }
  123. /**
  124. * Get the friendly name associated with the input class.
  125. * @returns the input friendly name
  126. */
  127. getSimpleName() {
  128. return "mousewheel";
  129. }
  130. }
  131. __decorate([
  132. serialize()
  133. ], BaseCameraMouseWheelInput.prototype, "wheelPrecisionX", void 0);
  134. __decorate([
  135. serialize()
  136. ], BaseCameraMouseWheelInput.prototype, "wheelPrecisionY", void 0);
  137. __decorate([
  138. serialize()
  139. ], BaseCameraMouseWheelInput.prototype, "wheelPrecisionZ", void 0);
  140. //# sourceMappingURL=BaseCameraMouseWheelInput.js.map