babylon.freecamera.input.mouse.js 4.6 KB

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