freeCameraKeyboardMoveInput.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 { Vector3 } from "../../Maths/math.vector.js";
  6. import { Tools } from "../../Misc/tools.js";
  7. /**
  8. * Manage the keyboard inputs to control the movement of a free camera.
  9. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
  10. */
  11. export class FreeCameraKeyboardMoveInput {
  12. constructor() {
  13. /**
  14. * Gets or Set the list of keyboard keys used to control the forward move of the camera.
  15. */
  16. this.keysUp = [38];
  17. /**
  18. * Gets or Set the list of keyboard keys used to control the upward move of the camera.
  19. */
  20. this.keysUpward = [33];
  21. /**
  22. * Gets or Set the list of keyboard keys used to control the backward move of the camera.
  23. */
  24. this.keysDown = [40];
  25. /**
  26. * Gets or Set the list of keyboard keys used to control the downward move of the camera.
  27. */
  28. this.keysDownward = [34];
  29. /**
  30. * Gets or Set the list of keyboard keys used to control the left strafe move of the camera.
  31. */
  32. this.keysLeft = [37];
  33. /**
  34. * Gets or Set the list of keyboard keys used to control the right strafe move of the camera.
  35. */
  36. this.keysRight = [39];
  37. /**
  38. * Defines the pointer angular sensibility along the X and Y axis or how fast is the camera rotating.
  39. */
  40. this.rotationSpeed = 0.5;
  41. /**
  42. * Gets or Set the list of keyboard keys used to control the left rotation move of the camera.
  43. */
  44. this.keysRotateLeft = [];
  45. /**
  46. * Gets or Set the list of keyboard keys used to control the right rotation move of the camera.
  47. */
  48. this.keysRotateRight = [];
  49. /**
  50. * Gets or Set the list of keyboard keys used to control the up rotation move of the camera.
  51. */
  52. this.keysRotateUp = [];
  53. /**
  54. * Gets or Set the list of keyboard keys used to control the down rotation move of the camera.
  55. */
  56. this.keysRotateDown = [];
  57. this._keys = new Array();
  58. }
  59. /**
  60. * Attach the input controls to a specific dom element to get the input from.
  61. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  62. */
  63. attachControl(noPreventDefault) {
  64. // eslint-disable-next-line prefer-rest-params
  65. noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
  66. if (this._onCanvasBlurObserver) {
  67. return;
  68. }
  69. this._scene = this.camera.getScene();
  70. this._engine = this._scene.getEngine();
  71. this._onCanvasBlurObserver = this._engine.onCanvasBlurObservable.add(() => {
  72. this._keys.length = 0;
  73. });
  74. this._onKeyboardObserver = this._scene.onKeyboardObservable.add((info) => {
  75. const evt = info.event;
  76. if (!evt.metaKey) {
  77. if (info.type === KeyboardEventTypes.KEYDOWN) {
  78. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  79. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  80. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  81. this.keysRight.indexOf(evt.keyCode) !== -1 ||
  82. this.keysUpward.indexOf(evt.keyCode) !== -1 ||
  83. this.keysDownward.indexOf(evt.keyCode) !== -1 ||
  84. this.keysRotateLeft.indexOf(evt.keyCode) !== -1 ||
  85. this.keysRotateRight.indexOf(evt.keyCode) !== -1 ||
  86. this.keysRotateUp.indexOf(evt.keyCode) !== -1 ||
  87. this.keysRotateDown.indexOf(evt.keyCode) !== -1) {
  88. const index = this._keys.indexOf(evt.keyCode);
  89. if (index === -1) {
  90. this._keys.push(evt.keyCode);
  91. }
  92. if (!noPreventDefault) {
  93. evt.preventDefault();
  94. }
  95. }
  96. }
  97. else {
  98. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  99. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  100. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  101. this.keysRight.indexOf(evt.keyCode) !== -1 ||
  102. this.keysUpward.indexOf(evt.keyCode) !== -1 ||
  103. this.keysDownward.indexOf(evt.keyCode) !== -1 ||
  104. this.keysRotateLeft.indexOf(evt.keyCode) !== -1 ||
  105. this.keysRotateRight.indexOf(evt.keyCode) !== -1 ||
  106. this.keysRotateUp.indexOf(evt.keyCode) !== -1 ||
  107. this.keysRotateDown.indexOf(evt.keyCode) !== -1) {
  108. const index = this._keys.indexOf(evt.keyCode);
  109. if (index >= 0) {
  110. this._keys.splice(index, 1);
  111. }
  112. if (!noPreventDefault) {
  113. evt.preventDefault();
  114. }
  115. }
  116. }
  117. }
  118. });
  119. }
  120. /**
  121. * Detach the current controls from the specified dom element.
  122. */
  123. detachControl() {
  124. if (this._scene) {
  125. if (this._onKeyboardObserver) {
  126. this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
  127. }
  128. if (this._onCanvasBlurObserver) {
  129. this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
  130. }
  131. this._onKeyboardObserver = null;
  132. this._onCanvasBlurObserver = null;
  133. }
  134. this._keys.length = 0;
  135. }
  136. /**
  137. * Update the current camera state depending on the inputs that have been used this frame.
  138. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  139. */
  140. checkInputs() {
  141. if (this._onKeyboardObserver) {
  142. const camera = this.camera;
  143. // Keyboard
  144. for (let index = 0; index < this._keys.length; index++) {
  145. const keyCode = this._keys[index];
  146. const speed = camera._computeLocalCameraSpeed();
  147. if (this.keysLeft.indexOf(keyCode) !== -1) {
  148. camera._localDirection.copyFromFloats(-speed, 0, 0);
  149. }
  150. else if (this.keysUp.indexOf(keyCode) !== -1) {
  151. camera._localDirection.copyFromFloats(0, 0, speed);
  152. }
  153. else if (this.keysRight.indexOf(keyCode) !== -1) {
  154. camera._localDirection.copyFromFloats(speed, 0, 0);
  155. }
  156. else if (this.keysDown.indexOf(keyCode) !== -1) {
  157. camera._localDirection.copyFromFloats(0, 0, -speed);
  158. }
  159. else if (this.keysUpward.indexOf(keyCode) !== -1) {
  160. camera._localDirection.copyFromFloats(0, speed, 0);
  161. }
  162. else if (this.keysDownward.indexOf(keyCode) !== -1) {
  163. camera._localDirection.copyFromFloats(0, -speed, 0);
  164. }
  165. else if (this.keysRotateLeft.indexOf(keyCode) !== -1) {
  166. camera._localDirection.copyFromFloats(0, 0, 0);
  167. camera.cameraRotation.y -= this._getLocalRotation();
  168. }
  169. else if (this.keysRotateRight.indexOf(keyCode) !== -1) {
  170. camera._localDirection.copyFromFloats(0, 0, 0);
  171. camera.cameraRotation.y += this._getLocalRotation();
  172. }
  173. else if (this.keysRotateUp.indexOf(keyCode) !== -1) {
  174. camera._localDirection.copyFromFloats(0, 0, 0);
  175. camera.cameraRotation.x -= this._getLocalRotation();
  176. }
  177. else if (this.keysRotateDown.indexOf(keyCode) !== -1) {
  178. camera._localDirection.copyFromFloats(0, 0, 0);
  179. camera.cameraRotation.x += this._getLocalRotation();
  180. }
  181. if (camera.getScene().useRightHandedSystem) {
  182. camera._localDirection.z *= -1;
  183. }
  184. camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
  185. Vector3.TransformNormalToRef(camera._localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
  186. camera.cameraDirection.addInPlace(camera._transformedDirection);
  187. }
  188. }
  189. }
  190. /**
  191. * Gets the class name of the current input.
  192. * @returns the class name
  193. */
  194. getClassName() {
  195. return "FreeCameraKeyboardMoveInput";
  196. }
  197. /** @internal */
  198. _onLostFocus() {
  199. this._keys.length = 0;
  200. }
  201. /**
  202. * Get the friendly name associated with the input class.
  203. * @returns the input friendly name
  204. */
  205. getSimpleName() {
  206. return "keyboard";
  207. }
  208. _getLocalRotation() {
  209. const handednessMultiplier = this.camera._calculateHandednessMultiplier();
  210. const rotation = ((this.rotationSpeed * this._engine.getDeltaTime()) / 1000) * handednessMultiplier;
  211. return rotation;
  212. }
  213. }
  214. __decorate([
  215. serialize()
  216. ], FreeCameraKeyboardMoveInput.prototype, "keysUp", void 0);
  217. __decorate([
  218. serialize()
  219. ], FreeCameraKeyboardMoveInput.prototype, "keysUpward", void 0);
  220. __decorate([
  221. serialize()
  222. ], FreeCameraKeyboardMoveInput.prototype, "keysDown", void 0);
  223. __decorate([
  224. serialize()
  225. ], FreeCameraKeyboardMoveInput.prototype, "keysDownward", void 0);
  226. __decorate([
  227. serialize()
  228. ], FreeCameraKeyboardMoveInput.prototype, "keysLeft", void 0);
  229. __decorate([
  230. serialize()
  231. ], FreeCameraKeyboardMoveInput.prototype, "keysRight", void 0);
  232. __decorate([
  233. serialize()
  234. ], FreeCameraKeyboardMoveInput.prototype, "rotationSpeed", void 0);
  235. __decorate([
  236. serialize()
  237. ], FreeCameraKeyboardMoveInput.prototype, "keysRotateLeft", void 0);
  238. __decorate([
  239. serialize()
  240. ], FreeCameraKeyboardMoveInput.prototype, "keysRotateRight", void 0);
  241. __decorate([
  242. serialize()
  243. ], FreeCameraKeyboardMoveInput.prototype, "keysRotateUp", void 0);
  244. __decorate([
  245. serialize()
  246. ], FreeCameraKeyboardMoveInput.prototype, "keysRotateDown", void 0);
  247. CameraInputTypes["FreeCameraKeyboardMoveInput"] = FreeCameraKeyboardMoveInput;
  248. //# sourceMappingURL=freeCameraKeyboardMoveInput.js.map