babylon.freecamera.input.mouse.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 FreeCameraMouseInput = (function () {
  10. function FreeCameraMouseInput(touchEnabled) {
  11. if (touchEnabled === void 0) { touchEnabled = true; }
  12. this.touchEnabled = touchEnabled;
  13. this.angularSensibility = 2000.0;
  14. }
  15. FreeCameraMouseInput.prototype.attachControl = function (element, noPreventDefault) {
  16. var _this = this;
  17. if (!this._pointerInput) {
  18. var camera = this.camera;
  19. var engine = this.camera.getEngine();
  20. this._pointerInput = function (p, s) {
  21. var evt = p.event;
  22. if (!_this.touchEnabled && evt.pointerType === "touch") {
  23. return;
  24. }
  25. if (p.type === BABYLON.PointerEventTypes.POINTERDOWN) {
  26. try {
  27. evt.srcElement.setPointerCapture(evt.pointerId);
  28. }
  29. catch (e) {
  30. }
  31. _this.previousPosition = {
  32. x: evt.clientX,
  33. y: evt.clientY
  34. };
  35. if (!noPreventDefault) {
  36. evt.preventDefault();
  37. element.focus();
  38. }
  39. }
  40. else if (p.type === BABYLON.PointerEventTypes.POINTERUP) {
  41. try {
  42. evt.srcElement.releasePointerCapture(evt.pointerId);
  43. }
  44. catch (e) {
  45. }
  46. _this.previousPosition = null;
  47. if (!noPreventDefault) {
  48. evt.preventDefault();
  49. }
  50. }
  51. else if (p.type === BABYLON.PointerEventTypes.POINTERMOVE) {
  52. if (!_this.previousPosition && !engine.isPointerLock) {
  53. return;
  54. }
  55. var offsetX;
  56. var offsetY;
  57. if (!engine.isPointerLock) {
  58. offsetX = evt.clientX - _this.previousPosition.x;
  59. offsetY = evt.clientY - _this.previousPosition.y;
  60. }
  61. else {
  62. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  63. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  64. }
  65. camera.cameraRotation.y += offsetX / _this.angularSensibility;
  66. camera.cameraRotation.x += offsetY / _this.angularSensibility;
  67. _this.previousPosition = {
  68. x: evt.clientX,
  69. y: evt.clientY
  70. };
  71. if (!noPreventDefault) {
  72. evt.preventDefault();
  73. }
  74. }
  75. };
  76. }
  77. this._observer = this.camera.getScene().onPointerObservable.add(this._pointerInput, BABYLON.PointerEventTypes.POINTERDOWN | BABYLON.PointerEventTypes.POINTERUP | BABYLON.PointerEventTypes.POINTERMOVE);
  78. };
  79. FreeCameraMouseInput.prototype.detachControl = function (element) {
  80. if (this._observer && element) {
  81. this.camera.getScene().onPointerObservable.remove(this._observer);
  82. this._observer = null;
  83. this.previousPosition = null;
  84. }
  85. };
  86. FreeCameraMouseInput.prototype.getTypeName = function () {
  87. return "FreeCameraMouseInput";
  88. };
  89. FreeCameraMouseInput.prototype.getSimpleName = function () {
  90. return "mouse";
  91. };
  92. __decorate([
  93. BABYLON.serialize()
  94. ], FreeCameraMouseInput.prototype, "angularSensibility", void 0);
  95. return FreeCameraMouseInput;
  96. })();
  97. BABYLON.FreeCameraMouseInput = FreeCameraMouseInput;
  98. BABYLON.CameraInputTypes["FreeCameraMouseInput"] = FreeCameraMouseInput;
  99. })(BABYLON || (BABYLON = {}));