followCameraMouseWheelInput.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { __decorate } from "../../tslib.es6.js";
  2. import { serialize } from "../../Misc/decorators.js";
  3. import { CameraInputTypes } from "../../Cameras/cameraInputsManager.js";
  4. import { PointerEventTypes } from "../../Events/pointerEvents.js";
  5. import { Tools } from "../../Misc/tools.js";
  6. import { Logger } from "../../Misc/logger.js";
  7. /**
  8. * Manage the mouse wheel inputs to control a follow camera.
  9. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
  10. */
  11. export class FollowCameraMouseWheelInput {
  12. constructor() {
  13. /**
  14. * Moue wheel controls zoom. (Mouse wheel modifies camera.radius value.)
  15. */
  16. this.axisControlRadius = true;
  17. /**
  18. * Moue wheel controls height. (Mouse wheel modifies camera.heightOffset value.)
  19. */
  20. this.axisControlHeight = false;
  21. /**
  22. * Moue wheel controls angle. (Mouse wheel modifies camera.rotationOffset value.)
  23. */
  24. this.axisControlRotation = false;
  25. /**
  26. * Gets or Set the mouse wheel precision or how fast is the camera moves in
  27. * relation to mouseWheel events.
  28. */
  29. this.wheelPrecision = 3.0;
  30. /**
  31. * wheelDeltaPercentage will be used instead of wheelPrecision if different from 0.
  32. * It defines the percentage of current camera.radius to use as delta when wheel is used.
  33. */
  34. this.wheelDeltaPercentage = 0;
  35. }
  36. /**
  37. * Attach the input controls to a specific dom element to get the input from.
  38. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  39. */
  40. attachControl(noPreventDefault) {
  41. noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
  42. this._wheel = (p) => {
  43. // sanity check - this should be a PointerWheel event.
  44. if (p.type !== PointerEventTypes.POINTERWHEEL) {
  45. return;
  46. }
  47. const event = p.event;
  48. let delta = 0;
  49. const wheelDelta = Math.max(-1, Math.min(1, event.deltaY));
  50. if (this.wheelDeltaPercentage) {
  51. if (+this.axisControlRadius + +this.axisControlHeight + +this.axisControlRotation) {
  52. Logger.Warn("wheelDeltaPercentage only usable when mouse wheel " +
  53. "controls ONE axis. " +
  54. "Currently enabled: " +
  55. "axisControlRadius: " +
  56. this.axisControlRadius +
  57. ", axisControlHeightOffset: " +
  58. this.axisControlHeight +
  59. ", axisControlRotationOffset: " +
  60. this.axisControlRotation);
  61. }
  62. if (this.axisControlRadius) {
  63. delta = wheelDelta * 0.01 * this.wheelDeltaPercentage * this.camera.radius;
  64. }
  65. else if (this.axisControlHeight) {
  66. delta = wheelDelta * 0.01 * this.wheelDeltaPercentage * this.camera.heightOffset;
  67. }
  68. else if (this.axisControlRotation) {
  69. delta = wheelDelta * 0.01 * this.wheelDeltaPercentage * this.camera.rotationOffset;
  70. }
  71. }
  72. else {
  73. delta = wheelDelta * this.wheelPrecision;
  74. }
  75. if (delta) {
  76. if (this.axisControlRadius) {
  77. this.camera.radius += delta;
  78. }
  79. else if (this.axisControlHeight) {
  80. this.camera.heightOffset -= delta;
  81. }
  82. else if (this.axisControlRotation) {
  83. this.camera.rotationOffset -= delta;
  84. }
  85. }
  86. if (event.preventDefault) {
  87. if (!noPreventDefault) {
  88. event.preventDefault();
  89. }
  90. }
  91. };
  92. this._observer = this.camera.getScene()._inputManager._addCameraPointerObserver(this._wheel, PointerEventTypes.POINTERWHEEL);
  93. }
  94. /**
  95. * Detach the current controls from the specified dom element.
  96. */
  97. detachControl() {
  98. if (this._observer) {
  99. this.camera.getScene()._inputManager._removeCameraPointerObserver(this._observer);
  100. this._observer = null;
  101. this._wheel = null;
  102. }
  103. }
  104. /**
  105. * Gets the class name of the current input.
  106. * @returns the class name
  107. */
  108. getClassName() {
  109. return "ArcRotateCameraMouseWheelInput";
  110. }
  111. /**
  112. * Get the friendly name associated with the input class.
  113. * @returns the input friendly name
  114. */
  115. getSimpleName() {
  116. return "mousewheel";
  117. }
  118. }
  119. __decorate([
  120. serialize()
  121. ], FollowCameraMouseWheelInput.prototype, "axisControlRadius", void 0);
  122. __decorate([
  123. serialize()
  124. ], FollowCameraMouseWheelInput.prototype, "axisControlHeight", void 0);
  125. __decorate([
  126. serialize()
  127. ], FollowCameraMouseWheelInput.prototype, "axisControlRotation", void 0);
  128. __decorate([
  129. serialize()
  130. ], FollowCameraMouseWheelInput.prototype, "wheelPrecision", void 0);
  131. __decorate([
  132. serialize()
  133. ], FollowCameraMouseWheelInput.prototype, "wheelDeltaPercentage", void 0);
  134. CameraInputTypes["FollowCameraMouseWheelInput"] = FollowCameraMouseWheelInput;
  135. //# sourceMappingURL=followCameraMouseWheelInput.js.map