ViewportControls.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {Viewport} from "../Viewport.js";
  2. import {Pointer} from "../input/Pointer.js";
  3. import {Vector2} from "../math/Vector2.js";
  4. /**
  5. * Viewport controls are used to allow the user to control the viewport.
  6. *
  7. * @class
  8. * @param {Viewport} viewport
  9. */
  10. function ViewportControls(viewport)
  11. {
  12. /**
  13. * Viewport being controlled by this object.
  14. */
  15. this.viewport = viewport;
  16. /**
  17. * Button used to drag and viewport around.
  18. *
  19. * On touch enabled devices the touch event is represented as a LEFT button.
  20. */
  21. this.dragButton = Pointer.RIGHT;
  22. /**
  23. * Is set to true allow the viewport to be scalled.
  24. *
  25. * Scaling is performed using the pointer scroll.
  26. */
  27. this.allowScale = true;
  28. /**
  29. * Flag to indicate if the viewport should move when scalling.
  30. *
  31. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  32. */
  33. this.moveOnScale = false;
  34. /**
  35. * If true allows the viewport to be rotated.
  36. *
  37. * Rotation is performed by holding the RIGHT and LEFT pointer buttons and rotating around the initial point.
  38. */
  39. this.allowRotation = true;
  40. /**
  41. * Value of the initial point of rotation if the viewport is being rotated.
  42. *
  43. * Is set to null when the viewport is not being rotated.
  44. */
  45. this.rotationPoint = null;
  46. /**
  47. * Initial rotation of the viewport.
  48. */
  49. this.rotationInitial = 0;
  50. }
  51. /**
  52. * Update the viewport controls using the pointer object.
  53. *
  54. * Should be called every frame before rendering.
  55. *
  56. * @param {Pointer} pointer
  57. */
  58. ViewportControls.prototype.update = function(pointer)
  59. {
  60. // Scale
  61. if(this.allowScale && pointer.wheel !== 0)
  62. {
  63. var scale = pointer.wheel * 1e-3 * this.viewport.scale;
  64. this.viewport.scale -= scale;
  65. this.viewport.matrixNeedsUpdate = true;
  66. // Move on scale
  67. if(this.moveOnScale && pointer.canvas !== null)
  68. {
  69. this.viewport.updateMatrix();
  70. var pointerWorld = this.viewport.inverseMatrix.transformPoint(pointer.position);
  71. var centerWorld = new Vector2(pointer.canvas.width / 2.0, pointer.canvas.height / 2.0);
  72. centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
  73. var delta = pointerWorld.clone();
  74. delta.sub(centerWorld);
  75. delta.multiplyScalar(0.1);
  76. this.viewport.position.sub(delta);
  77. this.viewport.matrixNeedsUpdate = true;
  78. }
  79. }
  80. // Rotation
  81. if(this.allowRotation && pointer.buttonPressed(Pointer.RIGHT) && pointer.buttonPressed(Pointer.LEFT))
  82. {
  83. // Rotation pivot
  84. if(this.rotationPoint === null)
  85. {
  86. this.rotationPoint = pointer.position.clone();
  87. this.rotationInitial = this.viewport.rotation;
  88. }
  89. else
  90. {
  91. var pointer = pointer.position.clone();
  92. pointer.sub(this.rotationPoint);
  93. this.viewport.rotation = this.rotationInitial + pointer.angle();
  94. this.viewport.matrixNeedsUpdate = true;
  95. }
  96. }
  97. // Drag
  98. else
  99. {
  100. this.rotationPoint = null;
  101. if(pointer.buttonPressed(this.dragButton))
  102. {
  103. this.viewport.position.x += pointer.delta.x;
  104. this.viewport.position.y += pointer.delta.y;
  105. this.viewport.matrixNeedsUpdate = true;
  106. }
  107. }
  108. };
  109. export {ViewportControls};