@realsee/dnalogel
Version:
202 lines (201 loc) • 8.5 kB
JavaScript
var w = Object.defineProperty;
var L = Object.getOwnPropertySymbols;
var M = Object.prototype.hasOwnProperty, $ = Object.prototype.propertyIsEnumerable;
var _ = (h, e, t) => e in h ? w(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t, D = (h, e) => {
for (var t in e || (e = {}))
M.call(e, t) && _(h, t, e[t]);
if (L)
for (var t of L(e))
$.call(e, t) && _(h, t, e[t]);
return h;
};
var s = (h, e, t) => (_(h, typeof e != "symbol" ? e + "" : e, t), t);
import * as x from "three";
import { getObjectVisible as I } from "../three/getObjectVisible.js";
import { calculateThreeMouse as W } from "./calculateThreeMouse.js";
class B {
constructor(e, t) {
s(this, "five");
/**
* @description: 拖动中
*/
s(this, "dragging", !1);
s(this, "boundObject", {});
s(this, "config");
s(this, "handleWantsGesture", (e, t) => {
if (t.length !== 1)
return;
const n = t[0];
if (n) {
if (e === "mouseMove" && this.onDomEvent("hover", n), e === "tap") {
const i = this.onDomEvent("click", n), o = this.onDomEvent("wantDblclick", n, void 0, this.boundObject.dblclick);
if (i === !1 || o === !1)
return !1;
}
if (this.dragging)
return !1;
}
});
s(this, "handleDomEvent", (e, t, n) => {
const i = { x: t.clientX, y: t.clientY };
this.onDomEvent(e, i, t, n);
});
s(this, "handleMouseEvent", (e) => {
this.handleDomEvent(e.type, e);
});
s(this, "handleMousedown", (e) => {
this.handleDomEvent("mousedown", e), this.handleDomEvent("dragstart", e, this.haveDragEventObject);
});
s(this, "handleMouseup", (e) => {
var t;
this.dragging = !1, (t = this.haveDragEventObject) == null || t.forEach((n) => {
n._dragging && (n._dragging = !1, this.notify({
eventName: "dragend",
object: n,
originEvent: e,
raycaster: this.getRaycaster({ x: e.clientX, y: e.clientY })
}));
}), this.handleDomEvent("mouseup", e);
});
s(this, "handleMousemove", (e) => {
var t;
this.dragging && ((t = this.boundObject.drag) == null || t.forEach((n) => {
n._dragging && this.notify({
eventName: "drag",
object: n,
originEvent: e,
raycaster: this.getRaycaster({ x: e.clientX, y: e.clientY })
});
}));
});
// eslint-disable-next-line complexity
s(this, "onDomEvent", (e, t, n, i = this.boundObject[e]) => {
var l, m, E;
if (!t || !i || (i == null ? void 0 : i.length) === 0 || !this.five.model.loaded)
return;
const o = (l = t.raycaster) != null ? l : this.getRaycaster(t);
o.params.Points.threshold = 0.02;
const r = o.intersectObjects(i, !0), f = this.five.model.intersectRaycaster(o), u = 0.01;
if (f.length > 0 && r.length > 0 && f[0].distance + u < r[0].distance)
return;
const a = (m = r == null ? void 0 : r[0]) == null ? void 0 : m.object;
if (e === "wantDblclick")
return !1;
if ((!a || !this.objectIsBound(a)) && e === "hover")
for (const v of (E = this.boundObject.hover) != null ? E : [])
v._hovered && this.notify({ eventName: "unHover", object: v });
if (a && a && this.notify({ eventName: e, object: a, originEvent: n, raycaster: o, intersects: r }))
return !1;
});
s(this, "objectIsBound", (e) => {
let t = e._domEvent, n = e.parent;
for (; typeof t == "undefined" && n; )
t = n._domEvent, n = n.parent;
return !!t;
});
s(this, "notify", (e) => {
var E, v, b;
const { eventName: t, object: n, originEvent: i, raycaster: o, intersects: r } = e;
let f = !1, u = [];
const a = [];
let l = n;
for (a.push(l); l.parent; )
l = l.parent, a.push(l);
const m = a.at(-1);
if (!((E = this.config) != null && E.noEmitWhenNotInScene && m.type !== "Scene")) {
for (const d of a) {
if (f)
break;
d.draggable && (t === "dragstart" && (d._dragging = !0, this.dragging = !0), t === "dragend" && (d._dragging = !1, this.dragging = !1));
const y = d._domEvent;
if (!y)
continue;
const O = y[`${t}Handler`];
if (O)
for (const [H, c] of O) {
if (c != null && c.noEmitWhenNotInScene && m.type !== "Scene" || ((v = this.config) != null && v.noEmitWhenHide || c != null && c.noEmitWhenHide) && !I(d))
continue;
if (t === "hover") {
if (d._hovered)
continue;
d._hovered = !0;
for (const g of (b = this.boundObject.hover) != null ? b : [])
g !== d && g._hovered && this.notify({ eventName: "unHover", object: g });
}
if (t === "unHover") {
if (!d._hovered)
continue;
d._hovered = !1;
}
const p = H({
type: t,
target: d,
origDomEvent: i,
raycaster: o,
intersects: r,
stopPropagation: () => {
f = !0;
}
});
u.push(p != null ? p : !0);
}
}
if (t === "click" || t === "wantDblclick")
return u.some((d) => d === !0);
}
});
this.five = e, this.config = t, e.on("wantsGesture", this.handleWantsGesture), document.addEventListener("mousedown", this.handleMousedown), document.addEventListener("dblclick", this.handleMouseEvent), document.addEventListener("mouseup", this.handleMouseup), document.addEventListener("mousemove", this.handleMousemove);
}
get haveDragEventObject() {
return [...new Set([this.boundObject.dragend, this.boundObject.drag, this.boundObject.dragstart].flat())].filter(Boolean);
}
/**
* @description: added 时自动绑定事件,removed时自动解绑事件,也就是说只有物体在场景中的时候才会触发事件
* @note: 注意:目前需要触发物体的 added 事件和 removed 事件才会生效
* @todo: added 和 removed 还是不太智能
*/
addAutoBindEventListener(e, t, n, i) {
const o = () => this.addEventListener(e, t, n, i), r = () => this.removeEventListener(e, t, n, i);
return e.addEventListener("added", o), e.addEventListener("removed", r), e.addEventListener("dispose", r), () => {
e.removeEventListener("added", o), e.removeEventListener("removed", r), e.removeEventListener("dispose", r);
};
}
/**
* @description: add event listener
* @param params.object: object
* @param params.event: event name
* @param params.callback: 返回 false 可以不阻止 five 的 tap 事件; default: true
* @return {void}
*/
addEventListener(e, t, n, i) {
e._domEvent || (e._domEvent = {}), e._domEvent[`${t}Handler`] || (e._domEvent[`${t}Handler`] = []), this.boundObject[t] || (this.boundObject[t] = []), e._domEvent[`${t}Handler`].push([n, D({ noEmitWhenHide: !1, noEmitWhenNotInScene: !1 }, i)]), this.boundObject[t].push(e);
}
removeEventListener(e, t, n, ...i) {
var r, f;
if (!e._domEvent || (t === void 0 && (e._domEvent = {}), !e._domEvent[`${t}Handler`]))
return;
if (n === void 0) {
delete e._domEvent[`${t}Handler`];
return;
}
const o = e._domEvent[`${t}Handler`].findIndex((u) => u[0] === n);
if (o !== -1 && (e._domEvent[`${t}Handler`].splice(o, 1), e._domEvent[`${t}Handler`].length === 0 && delete e._domEvent[`${t}Handler`], e._domEvent && Object.keys(e._domEvent).length === 0 && delete e._domEvent, !e._domEvent)) {
const u = (r = this.boundObject[t]) == null ? void 0 : r.findIndex((a) => a === e);
u !== -1 && ((f = this.boundObject[t]) == null || f.splice(u, 1));
}
}
dispose() {
var e;
(e = this.five) == null || e.off("wantsGesture", this.handleWantsGesture), document.removeEventListener("mousedown", this.handleMousedown), document.removeEventListener("mouseup", this.handleMouseup), document.removeEventListener("mousemove", this.handleMousemove), this.boundObject = {};
}
getRaycaster(e) {
const t = this.five.getElement();
if (!t)
return;
const n = W(e, t), i = new x.Raycaster();
return i.setFromCamera(n, this.five.camera), i;
}
}
export {
B as FiveDomEvents
};