babylon.freecamera.input.mouse.js 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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() {
  11. this.angularSensibility = 2000.0;
  12. }
  13. FreeCameraMouseInput.prototype.attachControl = function (element, noPreventDefault) {
  14. var _this = this;
  15. if (!this._onMouseDown) {
  16. var camera = this.camera;
  17. var engine = this.camera.getEngine();
  18. this._onMouseDown = function (evt) {
  19. _this.previousPosition = {
  20. x: evt.clientX,
  21. y: evt.clientY
  22. };
  23. if (!noPreventDefault) {
  24. evt.preventDefault();
  25. }
  26. };
  27. this._onMouseUp = function (evt) {
  28. _this.previousPosition = null;
  29. if (!noPreventDefault) {
  30. evt.preventDefault();
  31. }
  32. };
  33. this._onMouseOut = function (evt) {
  34. _this.previousPosition = null;
  35. if (!noPreventDefault) {
  36. evt.preventDefault();
  37. }
  38. };
  39. this._onMouseMove = function (evt) {
  40. if (!_this.previousPosition && !engine.isPointerLock) {
  41. return;
  42. }
  43. var offsetX;
  44. var offsetY;
  45. if (!engine.isPointerLock) {
  46. offsetX = evt.clientX - _this.previousPosition.x;
  47. offsetY = evt.clientY - _this.previousPosition.y;
  48. }
  49. else {
  50. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  51. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  52. }
  53. camera.cameraRotation.y += offsetX / _this.angularSensibility;
  54. camera.cameraRotation.x += offsetY / _this.angularSensibility;
  55. _this.previousPosition = {
  56. x: evt.clientX,
  57. y: evt.clientY
  58. };
  59. if (!noPreventDefault) {
  60. evt.preventDefault();
  61. }
  62. };
  63. }
  64. element.addEventListener("mousedown", this._onMouseDown, false);
  65. element.addEventListener("mouseup", this._onMouseUp, false);
  66. element.addEventListener("mouseout", this._onMouseOut, false);
  67. element.addEventListener("mousemove", this._onMouseMove, false);
  68. };
  69. FreeCameraMouseInput.prototype.detachControl = function (element) {
  70. if (this._onMouseDown && element) {
  71. this.previousPosition = null;
  72. element.removeEventListener("mousedown", this._onMouseDown);
  73. element.removeEventListener("mouseup", this._onMouseUp);
  74. element.removeEventListener("mouseout", this._onMouseOut);
  75. element.removeEventListener("mousemove", this._onMouseMove);
  76. this._onMouseDown = null;
  77. this._onMouseUp = null;
  78. this._onMouseOut = null;
  79. this._onMouseMove = null;
  80. }
  81. };
  82. FreeCameraMouseInput.prototype.getTypeName = function () {
  83. return "FreeCameraMouseInput";
  84. };
  85. FreeCameraMouseInput.prototype.getSimpleName = function () {
  86. return "mouse";
  87. };
  88. __decorate([
  89. BABYLON.serialize()
  90. ], FreeCameraMouseInput.prototype, "angularSensibility", void 0);
  91. return FreeCameraMouseInput;
  92. }());
  93. BABYLON.FreeCameraMouseInput = FreeCameraMouseInput;
  94. BABYLON.CameraInputTypes["FreeCameraMouseInput"] = FreeCameraMouseInput;
  95. })(BABYLON || (BABYLON = {}));