followCameraPointersInput.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { Nullable } from "../../types";
  2. import { serialize } from "../../Misc/decorators";
  3. import { FollowCamera } from "../../Cameras/followCamera";
  4. import { CameraInputTypes } from "../../Cameras/cameraInputsManager";
  5. import { BaseCameraPointersInput } from "../../Cameras/Inputs/BaseCameraPointersInput";
  6. import { PointerTouch } from "../../Events/pointerEvents";
  7. /**
  8. * Manage the pointers inputs to control an follow camera.
  9. * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
  10. */
  11. export class FollowCameraPointersInput extends BaseCameraPointersInput {
  12. /**
  13. * Defines the camera the input is attached to.
  14. */
  15. public camera: FollowCamera;
  16. /**
  17. * Gets the class name of the current input.
  18. * @returns the class name
  19. */
  20. public getClassName(): string {
  21. return "FollowCameraPointersInput";
  22. }
  23. /**
  24. * Defines the pointer angular sensibility along the X axis or how fast is
  25. * the camera rotating.
  26. * A negative number will reverse the axis direction.
  27. */
  28. @serialize()
  29. public angularSensibilityX = 1;
  30. /**
  31. * Defines the pointer angular sensibility along the Y axis or how fast is
  32. * the camera rotating.
  33. * A negative number will reverse the axis direction.
  34. */
  35. @serialize()
  36. public angularSensibilityY = 1;
  37. /**
  38. * Defines the pointer pinch precision or how fast is the camera zooming.
  39. * A negative number will reverse the axis direction.
  40. */
  41. @serialize()
  42. public pinchPrecision = 10000.0;
  43. /**
  44. * pinchDeltaPercentage will be used instead of pinchPrecision if different
  45. * from 0.
  46. * It defines the percentage of current camera.radius to use as delta when
  47. * pinch zoom is used.
  48. */
  49. @serialize()
  50. public pinchDeltaPercentage = 0;
  51. /**
  52. * Pointer X axis controls zoom. (X axis modifies camera.radius value.)
  53. */
  54. @serialize()
  55. public axisXControlRadius: boolean = false;
  56. /**
  57. * Pointer X axis controls height. (X axis modifies camera.heightOffset value.)
  58. */
  59. @serialize()
  60. public axisXControlHeight: boolean = false;
  61. /**
  62. * Pointer X axis controls angle. (X axis modifies camera.rotationOffset value.)
  63. */
  64. @serialize()
  65. public axisXControlRotation: boolean = true;
  66. /**
  67. * Pointer Y axis controls zoom. (Y axis modifies camera.radius value.)
  68. */
  69. @serialize()
  70. public axisYControlRadius: boolean = false;
  71. /**
  72. * Pointer Y axis controls height. (Y axis modifies camera.heightOffset value.)
  73. */
  74. @serialize()
  75. public axisYControlHeight: boolean = true;
  76. /**
  77. * Pointer Y axis controls angle. (Y axis modifies camera.rotationOffset value.)
  78. */
  79. @serialize()
  80. public axisYControlRotation: boolean = false;
  81. /**
  82. * Pinch controls zoom. (Pinch modifies camera.radius value.)
  83. */
  84. @serialize()
  85. public axisPinchControlRadius: boolean = true;
  86. /**
  87. * Pinch controls height. (Pinch modifies camera.heightOffset value.)
  88. */
  89. @serialize()
  90. public axisPinchControlHeight: boolean = false;
  91. /**
  92. * Pinch controls angle. (Pinch modifies camera.rotationOffset value.)
  93. */
  94. @serialize()
  95. public axisPinchControlRotation: boolean = false;
  96. /**
  97. * Log error messages if basic misconfiguration has occurred.
  98. */
  99. public warningEnable: boolean = true;
  100. protected onTouch(pointA: Nullable<PointerTouch>,
  101. offsetX: number,
  102. offsetY: number): void
  103. {
  104. this._warning();
  105. if (this.axisXControlRotation) {
  106. this.camera.rotationOffset += offsetX / this.angularSensibilityX;
  107. } else if (this.axisYControlRotation) {
  108. this.camera.rotationOffset += offsetY / this.angularSensibilityX;
  109. }
  110. if (this.axisXControlHeight) {
  111. this.camera.heightOffset += offsetX / this.angularSensibilityY;
  112. } else if (this.axisYControlHeight) {
  113. this.camera.heightOffset += offsetY / this.angularSensibilityY;
  114. }
  115. if (this.axisXControlRadius) {
  116. this.camera.radius -= offsetX / this.angularSensibilityY;
  117. } else if (this.axisYControlRadius) {
  118. this.camera.radius -= offsetY / this.angularSensibilityY;
  119. }
  120. }
  121. protected onMultiTouch(pointA: Nullable<PointerTouch>,
  122. pointB: Nullable<PointerTouch>,
  123. previousPinchSquaredDistance: number,
  124. pinchSquaredDistance: number,
  125. previousMultiTouchPanPosition: Nullable<PointerTouch>,
  126. multiTouchPanPosition: Nullable<PointerTouch>): void
  127. {
  128. if (previousPinchSquaredDistance === 0 && previousMultiTouchPanPosition === null) {
  129. // First time this method is called for new pinch.
  130. // Next time this is called there will be a
  131. // previousPinchSquaredDistance and pinchSquaredDistance to compare.
  132. return;
  133. }
  134. if (pinchSquaredDistance === 0 && multiTouchPanPosition === null) {
  135. // Last time this method is called at the end of a pinch.
  136. return;
  137. }
  138. var pinchDelta =
  139. (pinchSquaredDistance - previousPinchSquaredDistance) /
  140. (this.pinchPrecision * (this.angularSensibilityX + this.angularSensibilityY) / 2);
  141. if (this.pinchDeltaPercentage) {
  142. pinchDelta *= 0.01 * this.pinchDeltaPercentage;
  143. if (this.axisPinchControlRotation) {
  144. this.camera.rotationOffset += pinchDelta * this.camera.rotationOffset;
  145. }
  146. if (this.axisPinchControlHeight) {
  147. this.camera.heightOffset += pinchDelta * this.camera.heightOffset;
  148. }
  149. if (this.axisPinchControlRadius) {
  150. this.camera.radius -= pinchDelta * this.camera.radius;
  151. }
  152. } else {
  153. if (this.axisPinchControlRotation) {
  154. this.camera.rotationOffset += pinchDelta;
  155. }
  156. if (this.axisPinchControlHeight) {
  157. this.camera.heightOffset += pinchDelta;
  158. }
  159. if (this.axisPinchControlRadius) {
  160. this.camera.radius -= pinchDelta;
  161. }
  162. }
  163. }
  164. /* Check for obvious misconfiguration. */
  165. private _warningCounter: number = 0;
  166. private _warning(): void {
  167. if (!this.warningEnable || this._warningCounter++ % 100 !== 0) {
  168. return;
  169. }
  170. let warn = "It probably only makes sense to control ONE camera " +
  171. "property with each pointer axis. Set 'warningEnable = false' " +
  172. "if you are sure. Currently enabled: ";
  173. console.assert((<number>(<unknown>this.axisXControlRotation) +
  174. <number>(<unknown>this.axisXControlHeight) +
  175. <number>(<unknown>this.axisXControlRadius)) <= 1,
  176. warn +
  177. "axisXControlRotation: " + this.axisXControlRotation +
  178. ", axisXControlHeight: " + this.axisXControlHeight +
  179. ", axisXControlRadius: " + this.axisXControlRadius);
  180. console.assert((<number>(<unknown>this.axisYControlRotation) +
  181. <number>(<unknown>this.axisYControlHeight) +
  182. <number>(<unknown>this.axisYControlRadius)) <= 1,
  183. warn +
  184. "axisYControlRotation: " + this.axisYControlRotation +
  185. ", axisYControlHeight: " + this.axisYControlHeight +
  186. ", axisYControlRadius: " + this.axisYControlRadius);
  187. console.assert((<number>(<unknown>this.axisPinchControlRotation) +
  188. <number>(<unknown>this.axisPinchControlHeight) +
  189. <number>(<unknown>this.axisPinchControlRadius)) <= 1,
  190. warn +
  191. "axisPinchControlRotation: " + this.axisPinchControlRotation +
  192. ", axisPinchControlHeight: " + this.axisPinchControlHeight +
  193. ", axisPinchControlRadius: " + this.axisPinchControlRadius);
  194. }
  195. }
  196. (<any>CameraInputTypes)["FollowCameraPointersInput"] = FollowCameraPointersInput;