freeCameraKeyboardMoveInput.ts 6.7 KB

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