@realsee/dnalogel
Version:
154 lines (153 loc) • 5.58 kB
JavaScript
var g = Object.defineProperty;
var _ = Object.getOwnPropertySymbols;
var y = Object.prototype.hasOwnProperty, H = Object.prototype.propertyIsEnumerable;
var E = (u, e, t) => e in u ? g(u, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : u[e] = t, b = (u, e) => {
for (var t in e || (e = {}))
y.call(e, t) && E(u, t, e[t]);
if (_)
for (var t of _(e))
H.call(e, t) && E(u, t, e[t]);
return u;
};
var v = (u, e, t) => (E(u, typeof e != "symbol" ? e + "" : e, t), t);
import * as L from "three";
import { getObjectVisible as $ } from "../three/getObjectVisible.js";
import { calculateThreeMouse as x } from "./calculateThreeMouse.js";
class D {
constructor(e) {
v(this, "five");
v(this, "boundObject", {});
v(this, "handleWantsGesture", (e, t) => {
if (t.length !== 1)
return;
const n = t[0];
if (n) {
if (e === "mouseMove")
return this.onDomEvent("hover", n);
if (e === "tap")
return this.onDomEvent("click", n);
}
});
v(this, "onDomEvent", (e, t) => {
var f, i;
const n = this.boundObject[e];
if (!n || (n == null ? void 0 : n.length) === 0 || !t)
return;
const s = (f = t.raycaster) != null ? f : this.getRaycaster(t), r = (() => {
var h;
const d = s.intersectObjects(n, !0);
if (d.length === 0)
return;
const o = this.five.model.intersectRaycaster(s);
if (!(o.length > 0 && d.length > 0 && o[0].distance < d[0].distance))
return (h = d[0]) == null ? void 0 : h.object;
})();
if (!r || !this.objectIsBound(r)) {
for (const d of (i = this.boundObject.hover) != null ? i : [])
d._hovered && this.notify("unHover", d);
return;
}
if (this.notify(e, r))
return !1;
});
v(this, "objectIsBound", (e) => {
let t = e._domEvent, n = e.parent;
for (; typeof t == "undefined" && n; )
t = n._domEvent, n = n.parent;
return !!t;
});
// eslint-disable-next-line max-params
v(this, "notify", (e, t, n, s) => {
var d;
let r = !1, a = [];
const f = [];
let i = t;
for (f.push(i); i.parent; )
i = i.parent, f.push(i);
for (const o of f) {
if (r)
break;
const h = o._domEvent;
if (!h)
continue;
const p = h[`${e}Handler`];
if (p)
for (const [O, l] of p) {
if (l != null && l.checkVisible && !$(o))
continue;
if (e === "hover") {
if (o._hovered)
continue;
o._hovered = !0;
for (const c of (d = this.boundObject.hover) != null ? d : [])
c !== o && c._hovered && this.notify("unHover", c);
}
if (e === "unHover") {
if (!o._hovered)
continue;
o._hovered = !1;
}
const m = O({
type: e,
target: o,
origDomEvent: n,
intersect: s,
stopPropagation: () => {
r = !0;
}
});
a.push(m != null ? m : !0);
}
}
if (e === "click")
return a.some((o) => o === !0);
});
this.five = e, e.on("wantsGesture", this.handleWantsGesture);
}
/**
* @description: added 时自动绑定事件,removed时自动解绑事件,也就是说只有物体在场景中的时候才会触发事件
* @note: 注意:目前需要触发物体的 added 事件和 removed 事件才会生效
* @todo: added 和 removed 还是不太智能
*/
addAutoBindEventListener(e, t, n, s) {
const r = () => this.addEventListener(e, t, n, s), a = () => this.removeEventListener(e, t, n, s);
return e.addEventListener("added", r), e.addEventListener("removed", a), e.addEventListener("dispose", a), () => {
e.removeEventListener("added", r), e.removeEventListener("removed", a), e.removeEventListener("dispose", a);
};
}
/**
* @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, s) {
var r;
e._domEvent || (e._domEvent = {}), e._domEvent[`${t}Handler`] || (e._domEvent[`${t}Handler`] = []), this.boundObject[t] || (this.boundObject[t] = []), e._domEvent[`${t}Handler`].push([n, b({ checkVisible: !0 }, s)]), (r = this.boundObject[t]) == null || r.push(e);
}
removeEventListener(e, t, n, ...s) {
var a, f;
if (!e._domEvent || !e._domEvent[`${t}Handler`])
return;
const r = e._domEvent[`${t}Handler`].findIndex((i) => i[0] === n);
if (r !== -1 && (e._domEvent[`${t}Handler`].splice(r, 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 i = (a = this.boundObject[t]) == null ? void 0 : a.findIndex((d) => d === e);
i !== -1 && ((f = this.boundObject[t]) == null || f.splice(i, 1));
}
}
dispose() {
var e;
(e = this.five) == null || e.off("wantsGesture", this.handleWantsGesture), this.boundObject = {};
}
getRaycaster(e) {
const t = this.five.getElement();
if (!t)
return;
const n = x(e, t), s = new L.Raycaster();
return s.setFromCamera(n, this.five.camera), s;
}
}
export {
D as FiveDomEvents
};