babylon.freecamera.input.mouse.js 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  38. }
  39. else if (p.type === BABYLON.PointerEventTypes.POINTERUP) {
  40. try {
  41. evt.srcElement.releasePointerCapture(evt.pointerId);
  42. }
  43. catch (e) {
  44. }
  45. _this.previousPosition = null;
  46. if (!noPreventDefault) {
  47. evt.preventDefault();
  48. }
  49. }
  50. else if (p.type === BABYLON.PointerEventTypes.POINTERMOVE) {
  51. if (!_this.previousPosition && !engine.isPointerLock) {
  52. return;
  53. }
  54. var offsetX;
  55. var offsetY;
  56. if (!engine.isPointerLock) {
  57. offsetX = evt.clientX - _this.previousPosition.x;
  58. offsetY = evt.clientY - _this.previousPosition.y;
  59. }
  60. else {
  61. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  62. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  63. }
  64. camera.cameraRotation.y += offsetX / _this.angularSensibility;
  65. camera.cameraRotation.x += offsetY / _this.angularSensibility;
  66. _this.previousPosition = {
  67. x: evt.clientX,
  68. y: evt.clientY
  69. };
  70. if (!noPreventDefault) {
  71. evt.preventDefault();
  72. }
  73. }
  74. };
  75. }
  76. this._observer = this.camera.getScene().onPointerObservable.add(this._pointerInput, BABYLON.PointerEventTypes.POINTERDOWN | BABYLON.PointerEventTypes.POINTERUP | BABYLON.PointerEventTypes.POINTERMOVE);
  77. };
  78. FreeCameraMouseInput.prototype.detachControl = function (element) {
  79. if (this._observer && element) {
  80. this.camera.getScene().onPointerObservable.remove(this._observer);
  81. this._observer = null;
  82. this.previousPosition = null;
  83. }
  84. };
  85. FreeCameraMouseInput.prototype.getTypeName = function () {
  86. return "FreeCameraMouseInput";
  87. };
  88. FreeCameraMouseInput.prototype.getSimpleName = function () {
  89. return "mouse";
  90. };
  91. __decorate([
  92. BABYLON.serialize()
  93. ], FreeCameraMouseInput.prototype, "angularSensibility", void 0);
  94. return FreeCameraMouseInput;
  95. })();
  96. BABYLON.FreeCameraMouseInput = FreeCameraMouseInput;
  97. BABYLON.CameraInputTypes["FreeCameraMouseInput"] = FreeCameraMouseInput;
  98. })(BABYLON || (BABYLON = {}));