StaticMeshEvent.js 3.9 KB

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