StaticMeshEvent.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import EMeshType from "./enum/EMeshType.js"
  2. const LongPressMesh = [EMeshType.XAvatar];
  3. export default class StaticMeshEvent extends EventEmitter {
  4. constructor(e) {
  5. super();
  6. E(this, "scene");
  7. E(this, "_staringPointerTime", -1);
  8. E(this, "_pickedMeshID", "0");
  9. E(this, "_pointerDownTime", -1);
  10. E(this, "_currentPickPoint");
  11. E(this, "_longPressDelay", 500);
  12. E(this, "_pointerTapDelay", 200);
  13. E(this, "_pickedMeshType");
  14. E(this, "registerEvent", ()=>{
  15. this.scene.onPrePointerObservable.add(this.onDown, BABYLON.PointerEventTypes.POINTERDOWN),
  16. this.scene.onPrePointerObservable.add(this.onUp, BABYLON.PointerEventTypes.POINTERUP),
  17. this.scene.onPrePointerObservable.add(this.onDoubleTap, BABYLON.PointerEventTypes.POINTERDOUBLETAP),
  18. this.scene.onDispose = ()=>{
  19. this.scene.onPrePointerObservable.removeCallback(this.onUp),
  20. this.scene.onPrePointerObservable.removeCallback(this.onDown),
  21. this.scene.onPrePointerObservable.removeCallback(this.onDoubleTap)
  22. }
  23. }
  24. );
  25. E(this, "onUp", ()=>{
  26. if (Date.now() - this._pointerDownTime < this._pointerTapDelay && !this.scene._inputManager._isPointerSwiping()) {
  27. this.scene._inputManager._totalPointersPressed = 0;
  28. let e = this._currentPickPoint;
  29. e != null && LongPressMesh.indexOf(e.type) == -1 && this.scene._inputManager._totalPointersPressed == 0 && this.emit("pointTap", e),
  30. e != null && LongPressMesh.indexOf(e.type) != -1 && (e = this.onPointerTap(t=>t.isPickable && LongPressMesh.indexOf(t.xtype) == -1),
  31. e != null && this.emit("pointTap", e))
  32. }
  33. }
  34. );
  35. E(this, "onDown", ()=>{
  36. let e = this.onPointerTap(t=>t.isPickable);
  37. this._currentPickPoint = e,
  38. this._pointerDownTime = Date.now(),
  39. e != null && LongPressMesh.indexOf(e.type) != -1 && (this._staringPointerTime = Date.now(),
  40. this._pickedMeshID = e.id,
  41. this._pickedMeshType = e.type,
  42. window.setTimeout(()=>{
  43. e = this.onPointerTap(t=>t.isPickable && t.xtype == this._pickedMeshType && t.xid == this._pickedMeshID),
  44. e !== null && Date.now() - this._staringPointerTime > this._longPressDelay && !this.scene._inputManager._isPointerSwiping() && this.scene._inputManager._totalPointersPressed !== 0 && (this._staringPointerTime = 0,
  45. this.emit("longPress", e))
  46. }
  47. , this._longPressDelay))
  48. }
  49. );
  50. E(this, "onDoubleTap", ()=>{
  51. const e = this.onPointerTap(void 0);
  52. e != null && this.emit("pointDoubleTap", e)
  53. }
  54. );
  55. this.manager = e,
  56. this.scene = e.Scene,
  57. this.registerEvent(),
  58. this._currentPickPoint = null,
  59. this._pickedMeshType = null
  60. }
  61. onPointerTap(e, t=!1) {
  62. var n, o;
  63. let r = new BABYLON.PickingInfo;
  64. if (t) {
  65. const a = this.scene.multiPick(this.scene.pointerX, this.scene.pointerY, e, void 0, void 0);
  66. a && a.length > 1 ? r = a[1] : a && (r = a[0])
  67. } else
  68. r = this.scene.pick(this.scene.pointerX, this.scene.pointerY, e, !1, null);
  69. if (r.hit) {
  70. const a = (n = r == null ? void 0 : r.pickedPoint) == null ? void 0 : n.asArray();
  71. if (a) {
  72. const [s,l,u] = a
  73. , c = xversePosition2Ue4({
  74. x: s,
  75. y: l,
  76. z: u
  77. });
  78. return {
  79. name: (o = r.pickedMesh) == null ? void 0 : o.name,
  80. type: r.pickedMesh.xtype,
  81. id: r.pickedMesh.xid,
  82. point: c
  83. }
  84. }
  85. }
  86. return null
  87. }
  88. }