freeCameraKeyboardMoveInput.ts 6.7 KB

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