babylon.arcRotateCameraKeyboardMoveInput.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. module BABYLON {
  2. export class ArcRotateCameraKeyboardMoveInput implements ICameraInput<ArcRotateCamera> {
  3. camera: ArcRotateCamera;
  4. private _keys = [];
  5. private _onKeyDown: (e: KeyboardEvent) => any;
  6. private _onKeyUp: (e: KeyboardEvent) => any;
  7. private _onLostFocus: (e: FocusEvent) => any;
  8. private _onFocus: () => void;
  9. private _onBlur: () => void;
  10. @serialize()
  11. public keysUp = [38];
  12. @serialize()
  13. public keysDown = [40];
  14. @serialize()
  15. public keysLeft = [37];
  16. @serialize()
  17. public keysRight = [39];
  18. public attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  19. element.tabIndex = 1;
  20. this._onKeyDown = evt => {
  21. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  22. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  23. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  24. this.keysRight.indexOf(evt.keyCode) !== -1) {
  25. var index = this._keys.indexOf(evt.keyCode);
  26. if (index === -1) {
  27. this._keys.push(evt.keyCode);
  28. }
  29. if (evt.preventDefault) {
  30. if (!noPreventDefault) {
  31. evt.preventDefault();
  32. }
  33. }
  34. }
  35. };
  36. this._onKeyUp = evt => {
  37. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  38. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  39. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  40. this.keysRight.indexOf(evt.keyCode) !== -1) {
  41. var index = this._keys.indexOf(evt.keyCode);
  42. if (index >= 0) {
  43. this._keys.splice(index, 1);
  44. }
  45. if (evt.preventDefault) {
  46. if (!noPreventDefault) {
  47. evt.preventDefault();
  48. }
  49. }
  50. }
  51. };
  52. this._onLostFocus = () => {
  53. this._keys = [];
  54. };
  55. this._onFocus = () => {
  56. element.addEventListener("keydown", this._onKeyDown, false);
  57. element.addEventListener("keyup", this._onKeyUp, false);
  58. }
  59. this._onBlur = () => {
  60. element.removeEventListener("keydown", this._onKeyDown);
  61. element.removeEventListener("keyup", this._onKeyUp);
  62. }
  63. element.addEventListener("focus", this._onFocus);
  64. element.addEventListener("blur", this._onBlur);
  65. Tools.RegisterTopRootEvents([
  66. { name: "blur", handler: this._onLostFocus }
  67. ]);
  68. }
  69. public detachControl(element: HTMLElement) {
  70. if (element && this._onBlur) {
  71. this._onBlur();
  72. element.removeEventListener("focus", this._onFocus);
  73. element.removeEventListener("blur", this._onBlur);
  74. }
  75. Tools.UnregisterTopRootEvents([
  76. { name: "blur", handler: this._onLostFocus }
  77. ]);
  78. this._keys = [];
  79. this._onKeyDown = null;
  80. this._onKeyUp = null;
  81. this._onLostFocus = null;
  82. this._onBlur = null;
  83. this._onFocus = null;
  84. }
  85. public checkInputs() {
  86. if (this._onKeyDown){
  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. camera.inertialAlphaOffset -= 0.01;
  92. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  93. camera.inertialBetaOffset -= 0.01;
  94. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  95. camera.inertialAlphaOffset += 0.01;
  96. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  97. camera.inertialBetaOffset += 0.01;
  98. }
  99. }
  100. }
  101. }
  102. getClassName(): string {
  103. return "ArcRotateCameraKeyboardMoveInput";
  104. }
  105. getSimpleName(){
  106. return "keyboard";
  107. }
  108. }
  109. CameraInputTypes["ArcRotateCameraKeyboardMoveInput"] = ArcRotateCameraKeyboardMoveInput;
  110. }