flyCameraKeyboardInput.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { Nullable } from "../../types";
  2. import { serialize } from "../../Misc/decorators";
  3. import { Observer } from "../../Misc/observable";
  4. import { ICameraInput, CameraInputTypes } from "../../Cameras/cameraInputsManager";
  5. import { FlyCamera } from "../../Cameras/flyCamera";
  6. import { Engine } from "../../Engines/engine";
  7. import { KeyboardInfo, KeyboardEventTypes } from "../../Events/keyboardEvents";
  8. import { Scene } from "../../scene";
  9. import { Vector3 } from "../../Maths/math.vector";
  10. /**
  11. * Listen to keyboard events to control the camera.
  12. * @see https://doc.babylonjs.com/how_to/customizing_camera_inputs
  13. */
  14. export class FlyCameraKeyboardInput implements ICameraInput<FlyCamera> {
  15. /**
  16. * Defines the camera the input is attached to.
  17. */
  18. public camera: FlyCamera;
  19. /**
  20. * The list of keyboard keys used to control the forward move of the camera.
  21. */
  22. @serialize()
  23. public keysForward = [87];
  24. /**
  25. * The list of keyboard keys used to control the backward move of the camera.
  26. */
  27. @serialize()
  28. public keysBackward = [83];
  29. /**
  30. * The list of keyboard keys used to control the forward move of the camera.
  31. */
  32. @serialize()
  33. public keysUp = [69];
  34. /**
  35. * The list of keyboard keys used to control the backward move of the camera.
  36. */
  37. @serialize()
  38. public keysDown = [81];
  39. /**
  40. * The list of keyboard keys used to control the right strafe move of the camera.
  41. */
  42. @serialize()
  43. public keysRight = [68];
  44. /**
  45. * The list of keyboard keys used to control the left strafe move of the camera.
  46. */
  47. @serialize()
  48. public keysLeft = [65];
  49. private _keys = new Array<number>();
  50. private _onCanvasBlurObserver: Nullable<Observer<Engine>>;
  51. private _onKeyboardObserver: Nullable<Observer<KeyboardInfo>>;
  52. private _engine: Engine;
  53. private _scene: Scene;
  54. /**
  55. * Attach the input controls to a specific dom element to get the input from.
  56. * @param element Defines the element the controls should be listened from
  57. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  58. */
  59. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  60. if (this._onCanvasBlurObserver) {
  61. return;
  62. }
  63. this._scene = this.camera.getScene();
  64. this._engine = this._scene.getEngine();
  65. this._onCanvasBlurObserver = this._engine.onCanvasBlurObservable.add(() => {
  66. this._keys = [];
  67. });
  68. this._onKeyboardObserver = this._scene.onKeyboardObservable.add((info) => {
  69. let evt = info.event;
  70. if (info.type === KeyboardEventTypes.KEYDOWN) {
  71. if (this.keysForward.indexOf(evt.keyCode) !== -1 ||
  72. this.keysBackward.indexOf(evt.keyCode) !== -1 ||
  73. this.keysUp.indexOf(evt.keyCode) !== -1 ||
  74. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  75. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  76. this.keysRight.indexOf(evt.keyCode) !== -1) {
  77. var index = this._keys.indexOf(evt.keyCode);
  78. if (index === -1) {
  79. this._keys.push(evt.keyCode);
  80. }
  81. if (!noPreventDefault) {
  82. evt.preventDefault();
  83. }
  84. }
  85. } else {
  86. if (this.keysForward.indexOf(evt.keyCode) !== -1 ||
  87. this.keysBackward.indexOf(evt.keyCode) !== -1 ||
  88. this.keysUp.indexOf(evt.keyCode) !== -1 ||
  89. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  90. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  91. this.keysRight.indexOf(evt.keyCode) !== -1) {
  92. var index = this._keys.indexOf(evt.keyCode);
  93. if (index >= 0) {
  94. this._keys.splice(index, 1);
  95. }
  96. if (!noPreventDefault) {
  97. evt.preventDefault();
  98. }
  99. }
  100. }
  101. });
  102. }
  103. /**
  104. * Detach the current controls from the specified dom element.
  105. * @param element Defines the element to stop listening the inputs from
  106. */
  107. public detachControl(element: Nullable<HTMLElement>): void {
  108. if (this._scene) {
  109. if (this._onKeyboardObserver) {
  110. this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
  111. }
  112. if (this._onCanvasBlurObserver) {
  113. this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
  114. }
  115. this._onKeyboardObserver = null;
  116. this._onCanvasBlurObserver = null;
  117. }
  118. this._keys = [];
  119. }
  120. /**
  121. * Gets the class name of the current intput.
  122. * @returns the class name
  123. */
  124. public getClassName(): string {
  125. return "FlyCameraKeyboardInput";
  126. }
  127. /** @hidden */
  128. public _onLostFocus(e: FocusEvent): void {
  129. this._keys = [];
  130. }
  131. /**
  132. * Get the friendly name associated with the input class.
  133. * @returns the input friendly name
  134. */
  135. public getSimpleName(): string {
  136. return "keyboard";
  137. }
  138. /**
  139. * Update the current camera state depending on the inputs that have been used this frame.
  140. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  141. */
  142. public checkInputs(): void {
  143. if (this._onKeyboardObserver) {
  144. var camera = this.camera;
  145. // Keyboard
  146. for (var index = 0; index < this._keys.length; index++) {
  147. var keyCode = this._keys[index];
  148. var speed = camera._computeLocalCameraSpeed();
  149. if (this.keysForward.indexOf(keyCode) !== -1) {
  150. camera._localDirection.copyFromFloats(0, 0, speed);
  151. } else
  152. if (this.keysBackward.indexOf(keyCode) !== -1) {
  153. camera._localDirection.copyFromFloats(0, 0, -speed);
  154. } else
  155. if (this.keysUp.indexOf(keyCode) !== -1) {
  156. camera._localDirection.copyFromFloats(0, speed, 0);
  157. } else
  158. if (this.keysDown.indexOf(keyCode) !== -1) {
  159. camera._localDirection.copyFromFloats(0, -speed, 0);
  160. } else
  161. if (this.keysRight.indexOf(keyCode) !== -1) {
  162. camera._localDirection.copyFromFloats(speed, 0, 0);
  163. } else
  164. if (this.keysLeft.indexOf(keyCode) !== -1) {
  165. camera._localDirection.copyFromFloats(-speed, 0, 0);
  166. }
  167. if (camera.getScene().useRightHandedSystem) {
  168. camera._localDirection.z *= -1;
  169. }
  170. camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
  171. Vector3.TransformNormalToRef(camera._localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
  172. camera.cameraDirection.addInPlace(camera._transformedDirection);
  173. }
  174. }
  175. }
  176. }
  177. (<any>CameraInputTypes)["FlyCameraKeyboardInput"] = FlyCameraKeyboardInput;