arcRotateCameraKeyboardMoveInput.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { __decorate } from "../../tslib.es6.js";
  2. import { serialize } from "../../Misc/decorators.js";
  3. import { CameraInputTypes } from "../../Cameras/cameraInputsManager.js";
  4. import { KeyboardEventTypes } from "../../Events/keyboardEvents.js";
  5. import { Tools } from "../../Misc/tools.js";
  6. /**
  7. * Manage the keyboard inputs to control the movement of an arc rotate camera.
  8. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
  9. */
  10. export class ArcRotateCameraKeyboardMoveInput {
  11. constructor() {
  12. /**
  13. * Defines the list of key codes associated with the up action (increase alpha)
  14. */
  15. this.keysUp = [38];
  16. /**
  17. * Defines the list of key codes associated with the down action (decrease alpha)
  18. */
  19. this.keysDown = [40];
  20. /**
  21. * Defines the list of key codes associated with the left action (increase beta)
  22. */
  23. this.keysLeft = [37];
  24. /**
  25. * Defines the list of key codes associated with the right action (decrease beta)
  26. */
  27. this.keysRight = [39];
  28. /**
  29. * Defines the list of key codes associated with the reset action.
  30. * Those keys reset the camera to its last stored state (with the method camera.storeState())
  31. */
  32. this.keysReset = [220];
  33. /**
  34. * Defines the panning sensibility of the inputs.
  35. * (How fast is the camera panning)
  36. */
  37. this.panningSensibility = 50.0;
  38. /**
  39. * Defines the zooming sensibility of the inputs.
  40. * (How fast is the camera zooming)
  41. */
  42. this.zoomingSensibility = 25.0;
  43. /**
  44. * Defines whether maintaining the alt key down switch the movement mode from
  45. * orientation to zoom.
  46. */
  47. this.useAltToZoom = true;
  48. /**
  49. * Rotation speed of the camera
  50. */
  51. this.angularSpeed = 0.01;
  52. this._keys = new Array();
  53. }
  54. /**
  55. * Attach the input controls to a specific dom element to get the input from.
  56. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  57. */
  58. attachControl(noPreventDefault) {
  59. // was there a second variable defined?
  60. // eslint-disable-next-line prefer-rest-params
  61. noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
  62. if (this._onCanvasBlurObserver) {
  63. return;
  64. }
  65. this._scene = this.camera.getScene();
  66. this._engine = this._scene.getEngine();
  67. this._onCanvasBlurObserver = this._engine.onCanvasBlurObservable.add(() => {
  68. this._keys.length = 0;
  69. });
  70. this._onKeyboardObserver = this._scene.onKeyboardObservable.add((info) => {
  71. const evt = info.event;
  72. if (!evt.metaKey) {
  73. if (info.type === KeyboardEventTypes.KEYDOWN) {
  74. this._ctrlPressed = evt.ctrlKey;
  75. this._altPressed = evt.altKey;
  76. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  77. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  78. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  79. this.keysRight.indexOf(evt.keyCode) !== -1 ||
  80. this.keysReset.indexOf(evt.keyCode) !== -1) {
  81. const index = this._keys.indexOf(evt.keyCode);
  82. if (index === -1) {
  83. this._keys.push(evt.keyCode);
  84. }
  85. if (evt.preventDefault) {
  86. if (!noPreventDefault) {
  87. evt.preventDefault();
  88. }
  89. }
  90. }
  91. }
  92. else {
  93. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  94. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  95. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  96. this.keysRight.indexOf(evt.keyCode) !== -1 ||
  97. this.keysReset.indexOf(evt.keyCode) !== -1) {
  98. const index = this._keys.indexOf(evt.keyCode);
  99. if (index >= 0) {
  100. this._keys.splice(index, 1);
  101. }
  102. if (evt.preventDefault) {
  103. if (!noPreventDefault) {
  104. evt.preventDefault();
  105. }
  106. }
  107. }
  108. }
  109. }
  110. });
  111. }
  112. /**
  113. * Detach the current controls from the specified dom element.
  114. */
  115. detachControl() {
  116. if (this._scene) {
  117. if (this._onKeyboardObserver) {
  118. this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
  119. }
  120. if (this._onCanvasBlurObserver) {
  121. this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
  122. }
  123. this._onKeyboardObserver = null;
  124. this._onCanvasBlurObserver = null;
  125. }
  126. this._keys.length = 0;
  127. }
  128. /**
  129. * Update the current camera state depending on the inputs that have been used this frame.
  130. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  131. */
  132. checkInputs() {
  133. if (this._onKeyboardObserver) {
  134. const camera = this.camera;
  135. for (let index = 0; index < this._keys.length; index++) {
  136. const keyCode = this._keys[index];
  137. if (this.keysLeft.indexOf(keyCode) !== -1) {
  138. if (this._ctrlPressed && this.camera._useCtrlForPanning) {
  139. camera.inertialPanningX -= 1 / this.panningSensibility;
  140. }
  141. else {
  142. camera.inertialAlphaOffset -= this.angularSpeed;
  143. }
  144. }
  145. else if (this.keysUp.indexOf(keyCode) !== -1) {
  146. if (this._ctrlPressed && this.camera._useCtrlForPanning) {
  147. camera.inertialPanningY += 1 / this.panningSensibility;
  148. }
  149. else if (this._altPressed && this.useAltToZoom) {
  150. camera.inertialRadiusOffset += 1 / this.zoomingSensibility;
  151. }
  152. else {
  153. camera.inertialBetaOffset -= this.angularSpeed;
  154. }
  155. }
  156. else if (this.keysRight.indexOf(keyCode) !== -1) {
  157. if (this._ctrlPressed && this.camera._useCtrlForPanning) {
  158. camera.inertialPanningX += 1 / this.panningSensibility;
  159. }
  160. else {
  161. camera.inertialAlphaOffset += this.angularSpeed;
  162. }
  163. }
  164. else if (this.keysDown.indexOf(keyCode) !== -1) {
  165. if (this._ctrlPressed && this.camera._useCtrlForPanning) {
  166. camera.inertialPanningY -= 1 / this.panningSensibility;
  167. }
  168. else if (this._altPressed && this.useAltToZoom) {
  169. camera.inertialRadiusOffset -= 1 / this.zoomingSensibility;
  170. }
  171. else {
  172. camera.inertialBetaOffset += this.angularSpeed;
  173. }
  174. }
  175. else if (this.keysReset.indexOf(keyCode) !== -1) {
  176. if (camera.useInputToRestoreState) {
  177. camera.restoreState();
  178. }
  179. }
  180. }
  181. }
  182. }
  183. /**
  184. * Gets the class name of the current input.
  185. * @returns the class name
  186. */
  187. getClassName() {
  188. return "ArcRotateCameraKeyboardMoveInput";
  189. }
  190. /**
  191. * Get the friendly name associated with the input class.
  192. * @returns the input friendly name
  193. */
  194. getSimpleName() {
  195. return "keyboard";
  196. }
  197. }
  198. __decorate([
  199. serialize()
  200. ], ArcRotateCameraKeyboardMoveInput.prototype, "keysUp", void 0);
  201. __decorate([
  202. serialize()
  203. ], ArcRotateCameraKeyboardMoveInput.prototype, "keysDown", void 0);
  204. __decorate([
  205. serialize()
  206. ], ArcRotateCameraKeyboardMoveInput.prototype, "keysLeft", void 0);
  207. __decorate([
  208. serialize()
  209. ], ArcRotateCameraKeyboardMoveInput.prototype, "keysRight", void 0);
  210. __decorate([
  211. serialize()
  212. ], ArcRotateCameraKeyboardMoveInput.prototype, "keysReset", void 0);
  213. __decorate([
  214. serialize()
  215. ], ArcRotateCameraKeyboardMoveInput.prototype, "panningSensibility", void 0);
  216. __decorate([
  217. serialize()
  218. ], ArcRotateCameraKeyboardMoveInput.prototype, "zoomingSensibility", void 0);
  219. __decorate([
  220. serialize()
  221. ], ArcRotateCameraKeyboardMoveInput.prototype, "useAltToZoom", void 0);
  222. __decorate([
  223. serialize()
  224. ], ArcRotateCameraKeyboardMoveInput.prototype, "angularSpeed", void 0);
  225. CameraInputTypes["ArcRotateCameraKeyboardMoveInput"] = ArcRotateCameraKeyboardMoveInput;
  226. //# sourceMappingURL=arcRotateCameraKeyboardMoveInput.js.map