babylon.flyCameraKeyboardInput.ts 7.6 KB

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