2
0

ViewportControls.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * The user controls the viewport using pointer input (e.g. mouse, touchscreen)
  8. *
  9. * @class
  10. * @param {Viewport} viewport
  11. */
  12. function ViewportControls(viewport)
  13. {
  14. /**
  15. * Viewport being controlled by this object.
  16. *
  17. * @type {Viewport}
  18. */
  19. this.viewport = viewport;
  20. /**
  21. * Button used to drag and viewport around.
  22. *
  23. * On touch enabled devices the touch event is represented as a LEFT button.
  24. *
  25. * @type {number}
  26. */
  27. this.dragButton = Pointer.RIGHT;
  28. /**
  29. * Button used to rotate the viewport.
  30. *
  31. * @type {number}
  32. */
  33. this.rotateButton = Pointer.MIDDLE;
  34. /**
  35. * Is set to true allow the viewport to be scalled.
  36. *
  37. * Scaling is performed using the pointer scroll.
  38. *
  39. * @type {boolean}
  40. */
  41. this.allowScale = true;
  42. /**
  43. * Flag to indicate if the viewport should automatically be recentered.
  44. *
  45. * This will cause the viewport center property to be automatically set based on an heuristic defined by the user.
  46. *
  47. * @type {number}
  48. */
  49. this.recenterViewport = ViewportControls.RECENTER_NONE;
  50. /**
  51. * If true allows the viewport to be rotated.
  52. *
  53. * Rotation is performed by holding the RIGHT and LEFT pointer buttons and rotating around the initial point.
  54. *
  55. * @type {boolean}
  56. */
  57. this.allowRotation = true;
  58. /**
  59. * Value of the initial point of rotation if the viewport is being rotated.
  60. *
  61. * Is the value of the pointer position when the rotation starts.
  62. *
  63. * Is set to null when the viewport is not being rotated.
  64. *
  65. * @type {Vector2 | null}
  66. */
  67. this.rotationPoint = null;
  68. /**
  69. * Initial rotation of the viewport.
  70. *
  71. * Is set to the current rotation of the viewport when the rotation starts.
  72. *
  73. * @type {number}
  74. */
  75. this.rotationInitial = 0;
  76. }
  77. /**
  78. * Viewport is not automatically recentered.
  79. *
  80. * The center point can be set manually by the developer.
  81. *
  82. * @type {number}
  83. */
  84. ViewportControls.RECENTER_NONE = 0;
  85. /**
  86. * Recenter the viewport automatically to the canvas.
  87. *
  88. * This will ensure that rotation and scaling will not cause the viewport to move around.
  89. *
  90. * @type {number}
  91. */
  92. ViewportControls.RECENTER_CANVAS = 1;
  93. /**
  94. * Viewport should automatically center on the pointer position.
  95. *
  96. * The viewport will simulataniously move to the pointer position while scalling.
  97. *
  98. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  99. *
  100. * @type {number}
  101. */
  102. ViewportControls.RECENTER_POINTER = 2;
  103. /**
  104. * Update the viewport controls using the pointer object.
  105. *
  106. * Should be called every frame before rendering.
  107. *
  108. * @param {Pointer} pointer Pointer used to control the viewport.
  109. */
  110. ViewportControls.prototype.update = function(pointer)
  111. {
  112. // Scale
  113. if(this.allowScale && pointer.wheel !== 0)
  114. {
  115. var scale = pointer.wheel * 1e-3 * this.viewport.scale;
  116. this.viewport.scale -= scale;
  117. this.viewport.matrixNeedsUpdate = true;
  118. }
  119. // Rotation
  120. if(this.allowRotation && pointer.buttonPressed(this.rotateButton))
  121. {
  122. // Rotation pivot
  123. if(this.rotationPoint === null)
  124. {
  125. this.rotationPoint = pointer.position.clone();
  126. this.rotationInitial = this.viewport.rotation;
  127. }
  128. else
  129. {
  130. var point = pointer.position.clone();
  131. point.sub(this.rotationPoint);
  132. this.viewport.rotation = this.rotationInitial + point.angle();
  133. this.viewport.matrixNeedsUpdate = true;
  134. }
  135. return;
  136. } else {
  137. this.rotationPoint = null;
  138. }
  139. // Drag
  140. if(pointer.buttonPressed(this.dragButton))
  141. {
  142. this.viewport.position.add(pointer.delta);
  143. this.viewport.matrixNeedsUpdate = true;
  144. }
  145. if (pointer.canvas === null) {
  146. return;
  147. }
  148. // Center viewport on canvas
  149. if (this.recenterViewport === ViewportControls.RECENTER_CANVAS) {
  150. var centerWorld = new Vector2(pointer.canvas.width / 2.0, pointer.canvas.height / 2.0);
  151. centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
  152. this.viewport.center.copy(centerWorld);
  153. this.viewport.matrixNeedsUpdate = true;
  154. }
  155. // Center viewport on pointer
  156. else if(this.recenterViewport === ViewportControls.RECENTER_POINTER)
  157. {
  158. var pointerWorld = this.viewport.inverseMatrix.transformPoint(pointer.position);
  159. this.viewport.center.copy(pointerWorld);
  160. this.viewport.matrixNeedsUpdate = true;
  161. }
  162. };
  163. export {ViewportControls};