freeCameraKeyboardMoveInput.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /**
  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 upward move of the camera.
  26. */
  27. @serialize()
  28. public keysUpward = [33];
  29. /**
  30. * Gets or Set the list of keyboard keys used to control the backward move of the camera.
  31. */
  32. @serialize()
  33. public keysDown = [40];
  34. /**
  35. * Gets or Set the list of keyboard keys used to control the downward move of the camera.
  36. */
  37. @serialize()
  38. public keysDownward = [34];
  39. /**
  40. * Gets or Set the list of keyboard keys used to control the left strafe move of the camera.
  41. */
  42. @serialize()
  43. public keysLeft = [37];
  44. /**
  45. * Gets or Set the list of keyboard keys used to control the right strafe move of the camera.
  46. */
  47. @serialize()
  48. public keysRight = [39];
  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 (!evt.metaKey) {
  71. if (info.type === KeyboardEventTypes.KEYDOWN) {
  72. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  73. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  74. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  75. this.keysRight.indexOf(evt.keyCode) !== -1 ||
  76. this.keysUpward.indexOf(evt.keyCode) !== -1 ||
  77. this.keysDownward.indexOf(evt.keyCode) !== -1) {
  78. var index = this._keys.indexOf(evt.keyCode);
  79. if (index === -1) {
  80. this._keys.push(evt.keyCode);
  81. }
  82. if (!noPreventDefault) {
  83. evt.preventDefault();
  84. }
  85. }
  86. } else {
  87. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  88. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  89. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  90. this.keysRight.indexOf(evt.keyCode) !== -1 ||
  91. this.keysUpward.indexOf(evt.keyCode) !== -1 ||
  92. this.keysDownward.indexOf(evt.keyCode) !== -1) {
  93. var index = this._keys.indexOf(evt.keyCode);
  94. if (index >= 0) {
  95. this._keys.splice(index, 1);
  96. }
  97. if (!noPreventDefault) {
  98. evt.preventDefault();
  99. }
  100. }
  101. }
  102. }
  103. });
  104. }
  105. /**
  106. * Detach the current controls from the specified dom element.
  107. * @param element Defines the element to stop listening the inputs from
  108. */
  109. public detachControl(element: Nullable<HTMLElement>): void {
  110. if (this._scene) {
  111. if (this._onKeyboardObserver) {
  112. this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
  113. }
  114. if (this._onCanvasBlurObserver) {
  115. this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
  116. }
  117. this._onKeyboardObserver = null;
  118. this._onCanvasBlurObserver = null;
  119. }
  120. this._keys = [];
  121. }
  122. /**
  123. * Update the current camera state depending on the inputs that have been used this frame.
  124. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  125. */
  126. public checkInputs(): void {
  127. if (this._onKeyboardObserver) {
  128. var camera = this.camera;
  129. // Keyboard
  130. for (var index = 0; index < this._keys.length; index++) {
  131. var keyCode = this._keys[index];
  132. var speed = camera._computeLocalCameraSpeed();
  133. if (this.keysLeft.indexOf(keyCode) !== -1) {
  134. camera._localDirection.copyFromFloats(-speed, 0, 0);
  135. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  136. camera._localDirection.copyFromFloats(0, 0, speed);
  137. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  138. camera._localDirection.copyFromFloats(speed, 0, 0);
  139. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  140. camera._localDirection.copyFromFloats(0, 0, -speed);
  141. } else if (this.keysUpward.indexOf(keyCode) !== -1) {
  142. camera._localDirection.copyFromFloats(0, speed, 0);
  143. } else if (this.keysDownward.indexOf(keyCode) !== -1) {
  144. camera._localDirection.copyFromFloats(0, -speed, 0);
  145. }
  146. if (camera.getScene().useRightHandedSystem) {
  147. camera._localDirection.z *= -1;
  148. }
  149. camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
  150. Vector3.TransformNormalToRef(camera._localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
  151. camera.cameraDirection.addInPlace(camera._transformedDirection);
  152. }
  153. }
  154. }
  155. /**
  156. * Gets the class name of the current intput.
  157. * @returns the class name
  158. */
  159. public getClassName(): string {
  160. return "FreeCameraKeyboardMoveInput";
  161. }
  162. /** @hidden */
  163. public _onLostFocus(): void {
  164. this._keys = [];
  165. }
  166. /**
  167. * Get the friendly name associated with the input class.
  168. * @returns the input friendly name
  169. */
  170. public getSimpleName(): string {
  171. return "keyboard";
  172. }
  173. }
  174. (<any>CameraInputTypes)["FreeCameraKeyboardMoveInput"] = FreeCameraKeyboardMoveInput;