arcRotateCameraMouseWheelInput.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. module BABYLON {
  2. /**
  3. * Manage the mouse wheel inputs to control an arc rotate camera.
  4. * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
  5. */
  6. export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCamera> {
  7. /**
  8. * Defines the camera the input is attached to.
  9. */
  10. public camera: ArcRotateCamera;
  11. /**
  12. * Gets or Set the mouse wheel precision or how fast is the camera zooming.
  13. */
  14. @serialize()
  15. public wheelPrecision = 3.0;
  16. /**
  17. * wheelDeltaPercentage will be used instead of wheelPrecision if different from 0.
  18. * It defines the percentage of current camera.radius to use as delta when wheel is used.
  19. */
  20. @serialize()
  21. public wheelDeltaPercentage = 0;
  22. private _wheel: Nullable<(p: PointerInfo, s: EventState) => void>;
  23. private _observer: Nullable<Observer<PointerInfo>>;
  24. /**
  25. * Attach the input controls to a specific dom element to get the input from.
  26. * @param element Defines the element the controls should be listened from
  27. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  28. */
  29. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  30. this._wheel = (p, s) => {
  31. //sanity check - this should be a PointerWheel event.
  32. if (p.type !== PointerEventTypes.POINTERWHEEL) { return; }
  33. var event = <MouseWheelEvent>p.event;
  34. var delta = 0;
  35. if (event.wheelDelta) {
  36. if (this.wheelDeltaPercentage) {
  37. var wheelDelta = (event.wheelDelta * 0.01 * this.wheelDeltaPercentage) * this.camera.radius;
  38. if (event.wheelDelta > 0) {
  39. delta = wheelDelta / (1.0 + this.wheelDeltaPercentage);
  40. } else {
  41. delta = wheelDelta * (1.0 + this.wheelDeltaPercentage);
  42. }
  43. } else {
  44. delta = event.wheelDelta / (this.wheelPrecision * 40);
  45. }
  46. } else {
  47. let deltaValue = event.deltaY || event.detail;
  48. delta = -deltaValue / this.wheelPrecision;
  49. }
  50. if (delta) {
  51. this.camera.inertialRadiusOffset += delta;
  52. }
  53. if (event.preventDefault) {
  54. if (!noPreventDefault) {
  55. event.preventDefault();
  56. }
  57. }
  58. };
  59. this._observer = this.camera.getScene().onPointerObservable.add(this._wheel, PointerEventTypes.POINTERWHEEL);
  60. }
  61. /**
  62. * Detach the current controls from the specified dom element.
  63. * @param element Defines the element to stop listening the inputs from
  64. */
  65. public detachControl(element: Nullable<HTMLElement>): void {
  66. if (this._observer && element) {
  67. this.camera.getScene().onPointerObservable.remove(this._observer);
  68. this._observer = null;
  69. this._wheel = null;
  70. }
  71. }
  72. /**
  73. * Gets the class name of the current intput.
  74. * @returns the class name
  75. */
  76. public getClassName(): string {
  77. return "ArcRotateCameraMouseWheelInput";
  78. }
  79. /**
  80. * Get the friendly name associated with the input class.
  81. * @returns the input friendly name
  82. */
  83. public getSimpleName(): string {
  84. return "mousewheel";
  85. }
  86. }
  87. (<any>CameraInputTypes)["ArcRotateCameraMouseWheelInput"] = ArcRotateCameraMouseWheelInput;
  88. }