flyCameraKeyboardInput.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * Listen to keyboard events to control the camera.
  9. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
  10. */
  11. export class FlyCameraKeyboardInput {
  12. constructor() {
  13. /**
  14. * The list of keyboard keys used to control the forward move of the camera.
  15. */
  16. this.keysForward = [87];
  17. /**
  18. * The list of keyboard keys used to control the backward move of the camera.
  19. */
  20. this.keysBackward = [83];
  21. /**
  22. * The list of keyboard keys used to control the forward move of the camera.
  23. */
  24. this.keysUp = [69];
  25. /**
  26. * The list of keyboard keys used to control the backward move of the camera.
  27. */
  28. this.keysDown = [81];
  29. /**
  30. * The list of keyboard keys used to control the right strafe move of the camera.
  31. */
  32. this.keysRight = [68];
  33. /**
  34. * The list of keyboard keys used to control the left strafe move of the camera.
  35. */
  36. this.keysLeft = [65];
  37. this._keys = new Array();
  38. }
  39. /**
  40. * Attach the input controls to a specific dom element to get the input from.
  41. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  42. */
  43. attachControl(noPreventDefault) {
  44. // eslint-disable-next-line prefer-rest-params
  45. noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
  46. if (this._onCanvasBlurObserver) {
  47. return;
  48. }
  49. this._scene = this.camera.getScene();
  50. this._engine = this._scene.getEngine();
  51. this._onCanvasBlurObserver = this._engine.onCanvasBlurObservable.add(() => {
  52. this._keys.length = 0;
  53. });
  54. this._onKeyboardObserver = this._scene.onKeyboardObservable.add((info) => {
  55. const evt = info.event;
  56. if (info.type === KeyboardEventTypes.KEYDOWN) {
  57. if (this.keysForward.indexOf(evt.keyCode) !== -1 ||
  58. this.keysBackward.indexOf(evt.keyCode) !== -1 ||
  59. this.keysUp.indexOf(evt.keyCode) !== -1 ||
  60. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  61. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  62. this.keysRight.indexOf(evt.keyCode) !== -1) {
  63. const index = this._keys.indexOf(evt.keyCode);
  64. if (index === -1) {
  65. this._keys.push(evt.keyCode);
  66. }
  67. if (!noPreventDefault) {
  68. evt.preventDefault();
  69. }
  70. }
  71. }
  72. else {
  73. if (this.keysForward.indexOf(evt.keyCode) !== -1 ||
  74. this.keysBackward.indexOf(evt.keyCode) !== -1 ||
  75. this.keysUp.indexOf(evt.keyCode) !== -1 ||
  76. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  77. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  78. this.keysRight.indexOf(evt.keyCode) !== -1) {
  79. const index = this._keys.indexOf(evt.keyCode);
  80. if (index >= 0) {
  81. this._keys.splice(index, 1);
  82. }
  83. if (!noPreventDefault) {
  84. evt.preventDefault();
  85. }
  86. }
  87. }
  88. });
  89. }
  90. /**
  91. * Detach the current controls from the specified dom element.
  92. */
  93. detachControl() {
  94. if (this._scene) {
  95. if (this._onKeyboardObserver) {
  96. this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
  97. }
  98. if (this._onCanvasBlurObserver) {
  99. this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
  100. }
  101. this._onKeyboardObserver = null;
  102. this._onCanvasBlurObserver = null;
  103. }
  104. this._keys.length = 0;
  105. }
  106. /**
  107. * Gets the class name of the current input.
  108. * @returns the class name
  109. */
  110. getClassName() {
  111. return "FlyCameraKeyboardInput";
  112. }
  113. /**
  114. * @internal
  115. */
  116. _onLostFocus() {
  117. this._keys.length = 0;
  118. }
  119. /**
  120. * Get the friendly name associated with the input class.
  121. * @returns the input friendly name
  122. */
  123. getSimpleName() {
  124. return "keyboard";
  125. }
  126. /**
  127. * Update the current camera state depending on the inputs that have been used this frame.
  128. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  129. */
  130. checkInputs() {
  131. if (this._onKeyboardObserver) {
  132. const camera = this.camera;
  133. // Keyboard
  134. for (let index = 0; index < this._keys.length; index++) {
  135. const keyCode = this._keys[index];
  136. const speed = camera._computeLocalCameraSpeed();
  137. if (this.keysForward.indexOf(keyCode) !== -1) {
  138. camera._localDirection.copyFromFloats(0, 0, speed);
  139. }
  140. else if (this.keysBackward.indexOf(keyCode) !== -1) {
  141. camera._localDirection.copyFromFloats(0, 0, -speed);
  142. }
  143. else if (this.keysUp.indexOf(keyCode) !== -1) {
  144. camera._localDirection.copyFromFloats(0, speed, 0);
  145. }
  146. else if (this.keysDown.indexOf(keyCode) !== -1) {
  147. camera._localDirection.copyFromFloats(0, -speed, 0);
  148. }
  149. else if (this.keysRight.indexOf(keyCode) !== -1) {
  150. camera._localDirection.copyFromFloats(speed, 0, 0);
  151. }
  152. else if (this.keysLeft.indexOf(keyCode) !== -1) {
  153. camera._localDirection.copyFromFloats(-speed, 0, 0);
  154. }
  155. if (camera.getScene().useRightHandedSystem) {
  156. camera._localDirection.z *= -1;
  157. }
  158. camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
  159. Vector3.TransformNormalToRef(camera._localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
  160. camera.cameraDirection.addInPlace(camera._transformedDirection);
  161. }
  162. }
  163. }
  164. }
  165. __decorate([
  166. serialize()
  167. ], FlyCameraKeyboardInput.prototype, "keysForward", void 0);
  168. __decorate([
  169. serialize()
  170. ], FlyCameraKeyboardInput.prototype, "keysBackward", void 0);
  171. __decorate([
  172. serialize()
  173. ], FlyCameraKeyboardInput.prototype, "keysUp", void 0);
  174. __decorate([
  175. serialize()
  176. ], FlyCameraKeyboardInput.prototype, "keysDown", void 0);
  177. __decorate([
  178. serialize()
  179. ], FlyCameraKeyboardInput.prototype, "keysRight", void 0);
  180. __decorate([
  181. serialize()
  182. ], FlyCameraKeyboardInput.prototype, "keysLeft", void 0);
  183. CameraInputTypes["FlyCameraKeyboardInput"] = FlyCameraKeyboardInput;
  184. //# sourceMappingURL=flyCameraKeyboardInput.js.map