flyCameraKeyboardInput.ts 7.6 KB

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