babylon.arcRotateCameraKeyboardMoveInput.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. module BABYLON {
  2. export class ArcRotateCameraKeyboardMoveInput implements ICameraInput<ArcRotateCamera> {
  3. camera: ArcRotateCamera;
  4. private _keys = [];
  5. @serialize()
  6. public keysUp = [38];
  7. @serialize()
  8. public keysDown = [40];
  9. @serialize()
  10. public keysLeft = [37];
  11. @serialize()
  12. public keysRight = [39];
  13. @serialize()
  14. public keysReset = [220];
  15. @serialize()
  16. public panningSensibility: number = 50.0;
  17. @serialize()
  18. public zoomingSensibility: number = 25.0;
  19. @serialize()
  20. public useAltToZoom: boolean = true;
  21. private _ctrlPressed: boolean;
  22. private _altPressed: boolean;
  23. private _onCanvasBlurObserver: Observer<Engine>;
  24. private _onKeyboardObserver: Observer<KeyboardInfo>;
  25. private _engine: Engine;
  26. private _scene: Scene;
  27. public attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  28. if (this._onCanvasBlurObserver) {
  29. return;
  30. }
  31. this._scene = this.camera.getScene();
  32. this._engine = this._scene.getEngine();
  33. this._onCanvasBlurObserver = this._engine.onCanvasBlurObservable.add(()=>{
  34. this._keys = [];
  35. });
  36. this._onKeyboardObserver = this._scene.onKeyboardObservable.add(info => {
  37. let evt = info.event;
  38. if (info.type === KeyboardEventTypes.KEYDOWN) {
  39. this._ctrlPressed = evt.ctrlKey;
  40. this._altPressed = evt.altKey;
  41. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  42. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  43. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  44. this.keysRight.indexOf(evt.keyCode) !== -1 ||
  45. this.keysReset.indexOf(evt.keyCode) !== -1) {
  46. var index = this._keys.indexOf(evt.keyCode);
  47. if (index === -1) {
  48. this._keys.push(evt.keyCode);
  49. }
  50. if (evt.preventDefault) {
  51. if (!noPreventDefault) {
  52. evt.preventDefault();
  53. }
  54. }
  55. }
  56. }
  57. else {
  58. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  59. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  60. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  61. this.keysRight.indexOf(evt.keyCode) !== -1 ||
  62. this.keysReset.indexOf(evt.keyCode) !== -1) {
  63. var index = this._keys.indexOf(evt.keyCode);
  64. if (index >= 0) {
  65. this._keys.splice(index, 1);
  66. }
  67. if (evt.preventDefault) {
  68. if (!noPreventDefault) {
  69. evt.preventDefault();
  70. }
  71. }
  72. }
  73. }
  74. });
  75. }
  76. public detachControl(element: HTMLElement) {
  77. if (this._scene) {
  78. this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
  79. this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
  80. this._onKeyboardObserver = null;
  81. this._onCanvasBlurObserver = null;
  82. }
  83. this._keys = [];
  84. }
  85. public checkInputs() {
  86. if (this._onKeyboardObserver){
  87. var camera = this.camera;
  88. for (var index = 0; index < this._keys.length; index++) {
  89. var keyCode = this._keys[index];
  90. if (this.keysLeft.indexOf(keyCode) !== -1) {
  91. if (this._ctrlPressed && this.camera._useCtrlForPanning) {
  92. camera.inertialPanningX -= 1 / this.panningSensibility;
  93. } else {
  94. camera.inertialAlphaOffset -= 0.01;
  95. }
  96. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  97. if (this._ctrlPressed && this.camera._useCtrlForPanning) {
  98. camera.inertialPanningY += 1 / this.panningSensibility;
  99. }
  100. else if (this._altPressed && this.useAltToZoom) {
  101. camera.inertialRadiusOffset -= 1 / this.zoomingSensibility;
  102. }
  103. else {
  104. camera.inertialBetaOffset -= 0.01;
  105. }
  106. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  107. if (this._ctrlPressed && this.camera._useCtrlForPanning) {
  108. camera.inertialPanningX += 1 / this.panningSensibility;
  109. } else {
  110. camera.inertialAlphaOffset += 0.01;
  111. }
  112. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  113. if (this._ctrlPressed && this.camera._useCtrlForPanning) {
  114. camera.inertialPanningY -= 1 / this.panningSensibility;
  115. }
  116. else if (this._altPressed && this.useAltToZoom) {
  117. camera.inertialRadiusOffset += 1 / this.zoomingSensibility;
  118. }
  119. else {
  120. camera.inertialBetaOffset += 0.01;
  121. }
  122. } else if (this.keysReset.indexOf(keyCode) !== -1) {
  123. camera.restoreState();
  124. }
  125. }
  126. }
  127. }
  128. getClassName(): string {
  129. return "ArcRotateCameraKeyboardMoveInput";
  130. }
  131. getSimpleName(){
  132. return "keyboard";
  133. }
  134. }
  135. CameraInputTypes["ArcRotateCameraKeyboardMoveInput"] = ArcRotateCameraKeyboardMoveInput;
  136. }