freeCameraKeyboardMoveInput.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.vector";
  9. import { Engine } from "../../Engines/engine";
  10. import { Tools } from "../../Misc/tools";
  11. /**
  12. * Manage the keyboard inputs to control the movement of a free camera.
  13. * @see https://doc.babylonjs.com/how_to/customizing_camera_inputs
  14. */
  15. export class FreeCameraKeyboardMoveInput implements ICameraInput<FreeCamera> {
  16. /**
  17. * Defines the camera the input is attached to.
  18. */
  19. public camera: FreeCamera;
  20. /**
  21. * Gets or Set the list of keyboard keys used to control the forward move of the camera.
  22. */
  23. @serialize()
  24. public keysUp = [38];
  25. /**
  26. * Gets or Set the list of keyboard keys used to control the upward move of the camera.
  27. */
  28. @serialize()
  29. public keysUpward = [33];
  30. /**
  31. * Gets or Set the list of keyboard keys used to control the backward move of the camera.
  32. */
  33. @serialize()
  34. public keysDown = [40];
  35. /**
  36. * Gets or Set the list of keyboard keys used to control the downward move of the camera.
  37. */
  38. @serialize()
  39. public keysDownward = [34];
  40. /**
  41. * Gets or Set the list of keyboard keys used to control the left strafe move of the camera.
  42. */
  43. @serialize()
  44. public keysLeft = [37];
  45. /**
  46. * Gets or Set the list of keyboard keys used to control the right strafe move of the camera.
  47. */
  48. @serialize()
  49. public keysRight = [39];
  50. private _keys = new Array<number>();
  51. private _onCanvasBlurObserver: Nullable<Observer<Engine>>;
  52. private _onKeyboardObserver: Nullable<Observer<KeyboardInfo>>;
  53. private _engine: Engine;
  54. private _scene: Scene;
  55. /**
  56. * Attach the input controls to a specific dom element to get the input 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(noPreventDefault?: boolean): void {
  60. noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
  61. if (this._onCanvasBlurObserver) {
  62. return;
  63. }
  64. this._scene = this.camera.getScene();
  65. this._engine = this._scene.getEngine();
  66. this._onCanvasBlurObserver = this._engine.onCanvasBlurObservable.add(() => {
  67. this._keys = [];
  68. });
  69. this._onKeyboardObserver = this._scene.onKeyboardObservable.add((info) => {
  70. let evt = info.event;
  71. if (!evt.metaKey) {
  72. if (info.type === KeyboardEventTypes.KEYDOWN) {
  73. if (this.keysUp.indexOf(evt.keyCode) !== -1 || this.keysDown.indexOf(evt.keyCode) !== -1 || this.keysLeft.indexOf(evt.keyCode) !== -1 || this.keysRight.indexOf(evt.keyCode) !== -1 || this.keysUpward.indexOf(evt.keyCode) !== -1 || this.keysDownward.indexOf(evt.keyCode) !== -1) {
  74. var index = this._keys.indexOf(evt.keyCode);
  75. if (index === -1) {
  76. this._keys.push(evt.keyCode);
  77. }
  78. if (!noPreventDefault) {
  79. evt.preventDefault();
  80. }
  81. }
  82. } else {
  83. if (this.keysUp.indexOf(evt.keyCode) !== -1 || this.keysDown.indexOf(evt.keyCode) !== -1 || this.keysLeft.indexOf(evt.keyCode) !== -1 || this.keysRight.indexOf(evt.keyCode) !== -1 || this.keysUpward.indexOf(evt.keyCode) !== -1 || this.keysDownward.indexOf(evt.keyCode) !== -1) {
  84. var index = this._keys.indexOf(evt.keyCode);
  85. if (index >= 0) {
  86. this._keys.splice(index, 1);
  87. }
  88. if (!noPreventDefault) {
  89. evt.preventDefault();
  90. }
  91. }
  92. }
  93. }
  94. });
  95. }
  96. /**
  97. * Detach the current controls from the specified dom element.
  98. */
  99. public detachControl(): void;
  100. /**
  101. * Detach the current controls from the specified dom element.
  102. * @param ignored defines an ignored parameter kept for backward compatibility. If you want to define the source input element, you can set engine.inputElement before calling camera.attachControl
  103. */
  104. public detachControl(ignored?: any): void {
  105. if (this._scene) {
  106. if (this._onKeyboardObserver) {
  107. this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
  108. }
  109. if (this._onCanvasBlurObserver) {
  110. this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
  111. }
  112. this._onKeyboardObserver = null;
  113. this._onCanvasBlurObserver = null;
  114. }
  115. this._keys = [];
  116. }
  117. /**
  118. * Update the current camera state depending on the inputs that have been used this frame.
  119. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  120. */
  121. public checkInputs(): void {
  122. if (this._onKeyboardObserver) {
  123. var camera = this.camera;
  124. // Keyboard
  125. for (var index = 0; index < this._keys.length; index++) {
  126. var keyCode = this._keys[index];
  127. var speed = camera._computeLocalCameraSpeed();
  128. if (this.keysLeft.indexOf(keyCode) !== -1) {
  129. camera._localDirection.copyFromFloats(-speed, 0, 0);
  130. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  131. camera._localDirection.copyFromFloats(0, 0, speed);
  132. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  133. camera._localDirection.copyFromFloats(speed, 0, 0);
  134. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  135. camera._localDirection.copyFromFloats(0, 0, -speed);
  136. } else if (this.keysUpward.indexOf(keyCode) !== -1) {
  137. camera._localDirection.copyFromFloats(0, speed, 0);
  138. } else if (this.keysDownward.indexOf(keyCode) !== -1) {
  139. camera._localDirection.copyFromFloats(0, -speed, 0);
  140. }
  141. if (camera.getScene().useRightHandedSystem) {
  142. camera._localDirection.z *= -1;
  143. }
  144. camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
  145. Vector3.TransformNormalToRef(camera._localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
  146. camera.cameraDirection.addInPlace(camera._transformedDirection);
  147. }
  148. }
  149. }
  150. /**
  151. * Gets the class name of the current intput.
  152. * @returns the class name
  153. */
  154. public getClassName(): string {
  155. return "FreeCameraKeyboardMoveInput";
  156. }
  157. /** @hidden */
  158. public _onLostFocus(): void {
  159. this._keys = [];
  160. }
  161. /**
  162. * Get the friendly name associated with the input class.
  163. * @returns the input friendly name
  164. */
  165. public getSimpleName(): string {
  166. return "keyboard";
  167. }
  168. }
  169. (<any>CameraInputTypes)["FreeCameraKeyboardMoveInput"] = FreeCameraKeyboardMoveInput;