babylon.freecamera.input.mouse.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. module BABYLON {
  2. export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
  3. camera: FreeCamera;
  4. @serialize()
  5. public angularSensibility = 2000.0;
  6. private _pointerInput: (p: PointerInfo, s: EventState) => void;
  7. private _observer: Observer<PointerInfo>;
  8. private previousPosition: { x: number, y: number };
  9. constructor(public touchEnabled = true) {
  10. }
  11. attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  12. if (!this._pointerInput) {
  13. var camera = this.camera;
  14. var engine = this.camera.getEngine();
  15. this._pointerInput = (p, s) => {
  16. var evt = <PointerEvent>p.event;
  17. if (!this.touchEnabled && evt.pointerType === "touch") {
  18. return;
  19. }
  20. if (p.type === PointerEventTypes.POINTERDOWN) {
  21. try {
  22. evt.srcElement.setPointerCapture(evt.pointerId);
  23. } catch (e) {
  24. //Nothing to do with the error. Execution will continue.
  25. }
  26. this.previousPosition = {
  27. x: evt.clientX,
  28. y: evt.clientY
  29. };
  30. if (!noPreventDefault) {
  31. evt.preventDefault();
  32. element.focus();
  33. }
  34. }
  35. else if (p.type === PointerEventTypes.POINTERUP) {
  36. try {
  37. evt.srcElement.releasePointerCapture(evt.pointerId);
  38. } catch (e) {
  39. //Nothing to do with the error.
  40. }
  41. this.previousPosition = null;
  42. if (!noPreventDefault) {
  43. evt.preventDefault();
  44. }
  45. }
  46. else if (p.type === PointerEventTypes.POINTERMOVE) {
  47. if (!this.previousPosition && !engine.isPointerLock) {
  48. return;
  49. }
  50. var offsetX;
  51. var offsetY;
  52. if (!engine.isPointerLock) {
  53. offsetX = evt.clientX - this.previousPosition.x;
  54. offsetY = evt.clientY - this.previousPosition.y;
  55. } else {
  56. offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  57. offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  58. }
  59. camera.cameraRotation.y += offsetX / this.angularSensibility;
  60. camera.cameraRotation.x += offsetY / this.angularSensibility;
  61. this.previousPosition = {
  62. x: evt.clientX,
  63. y: evt.clientY
  64. };
  65. if (!noPreventDefault) {
  66. evt.preventDefault();
  67. }
  68. }
  69. }
  70. }
  71. this._observer = this.camera.getScene().onPointerObservable.add(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);
  72. }
  73. detachControl(element: HTMLElement) {
  74. if (this._observer && element) {
  75. this.camera.getScene().onPointerObservable.remove(this._observer);
  76. this._observer = null;
  77. this.previousPosition = null;
  78. }
  79. }
  80. getTypeName(): string {
  81. return "FreeCameraMouseInput";
  82. }
  83. getSimpleName() {
  84. return "mouse";
  85. }
  86. }
  87. CameraInputTypes["FreeCameraMouseInput"] = FreeCameraMouseInput;
  88. }