babylon.arcrotatecamera.input.keyboard.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var ArcRotateCameraKeyboardMoveInput = (function () {
  10. function ArcRotateCameraKeyboardMoveInput() {
  11. this._keys = [];
  12. this.keysUp = [38];
  13. this.keysDown = [40];
  14. this.keysLeft = [37];
  15. this.keysRight = [39];
  16. }
  17. ArcRotateCameraKeyboardMoveInput.prototype.attachControl = function (element, noPreventDefault) {
  18. var _this = this;
  19. element.tabIndex = 1;
  20. this._onKeyDown = function (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 = function (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 = function () {
  53. _this._keys = [];
  54. };
  55. element.addEventListener("keydown", this._onKeyDown, false);
  56. element.addEventListener("keyup", this._onKeyUp, false);
  57. BABYLON.Tools.RegisterTopRootEvents([
  58. { name: "blur", handler: this._onLostFocus }
  59. ]);
  60. };
  61. ArcRotateCameraKeyboardMoveInput.prototype.detachControl = function (element) {
  62. if (element) {
  63. element.removeEventListener("keydown", this._onKeyDown);
  64. element.removeEventListener("keyup", this._onKeyUp);
  65. }
  66. BABYLON.Tools.UnregisterTopRootEvents([
  67. { name: "blur", handler: this._onLostFocus }
  68. ]);
  69. this._keys = [];
  70. this._onKeyDown = null;
  71. this._onKeyUp = null;
  72. this._onLostFocus = null;
  73. };
  74. ArcRotateCameraKeyboardMoveInput.prototype.checkInputs = function () {
  75. if (this._onKeyDown) {
  76. var camera = this.camera;
  77. for (var index = 0; index < this._keys.length; index++) {
  78. var keyCode = this._keys[index];
  79. if (this.keysLeft.indexOf(keyCode) !== -1) {
  80. camera.inertialAlphaOffset -= 0.01;
  81. }
  82. else if (this.keysUp.indexOf(keyCode) !== -1) {
  83. camera.inertialBetaOffset -= 0.01;
  84. }
  85. else if (this.keysRight.indexOf(keyCode) !== -1) {
  86. camera.inertialAlphaOffset += 0.01;
  87. }
  88. else if (this.keysDown.indexOf(keyCode) !== -1) {
  89. camera.inertialBetaOffset += 0.01;
  90. }
  91. }
  92. }
  93. };
  94. ArcRotateCameraKeyboardMoveInput.prototype.getTypeName = function () {
  95. return "ArcRotateCameraKeyboardMoveInput";
  96. };
  97. ArcRotateCameraKeyboardMoveInput.prototype.getSimpleName = function () {
  98. return "keyboard";
  99. };
  100. __decorate([
  101. BABYLON.serialize()
  102. ], ArcRotateCameraKeyboardMoveInput.prototype, "keysUp", void 0);
  103. __decorate([
  104. BABYLON.serialize()
  105. ], ArcRotateCameraKeyboardMoveInput.prototype, "keysDown", void 0);
  106. __decorate([
  107. BABYLON.serialize()
  108. ], ArcRotateCameraKeyboardMoveInput.prototype, "keysLeft", void 0);
  109. __decorate([
  110. BABYLON.serialize()
  111. ], ArcRotateCameraKeyboardMoveInput.prototype, "keysRight", void 0);
  112. return ArcRotateCameraKeyboardMoveInput;
  113. }());
  114. BABYLON.ArcRotateCameraKeyboardMoveInput = ArcRotateCameraKeyboardMoveInput;
  115. BABYLON.CameraInputTypes["ArcRotateCameraKeyboardMoveInput"] = ArcRotateCameraKeyboardMoveInput;
  116. })(BABYLON || (BABYLON = {}));