ViewportControls.js 3.0 KB

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