freeCameraKeyboardMoveInput.ts 7.0 KB

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