freeCameraKeyboardMoveInput.ts 6.7 KB

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