UNPKG

@realsee/dnalogel

Version:
119 lines (118 loc) 5.27 kB
var p = Object.defineProperty; var v = (c, i, e) => i in c ? p(c, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : c[i] = e; var u = (c, i, e) => (v(c, typeof i != "symbol" ? i + "" : i, e), e); import { BaseController as f } from "../Base/BaseController.js"; import * as m from "three"; import { getMouseRaycaster as g } from "../utils/getMouseRaycaster.js"; class w extends f { constructor(...e) { super(...e); u(this, "name", "MoveController"); u(this, "startInfo"); u(this, "_moveByMouseEnable", !1); u(this, "mouseInfo"); u(this, "mousedownEventListenerDisposer"); const t = this.helperObject3D; this.hoverListener([t.xArrow, t.yArrow, t.zArrow]); const o = this.dragStart.bind(this), r = this.dragging.bind(this), h = this.dragEnd.bind(this), s = this.show.bind(this), n = this.hide.bind(this); this.domEvents.addEventListener(this.helperObject3D, "mousedown", o), document.addEventListener("mousemove", r), document.addEventListener("mouseup", h), this.hooks.on("rotateStart", n), this.hooks.on("rotateEnd", s), this.hooks.on("moveByMouseEnable", n), this.hooks.on("moveByMouseDisable", s), this.disposers.push(() => { this.domEvents.removeEventListener(this.helperObject3D, "mousedown", o), document.removeEventListener("mousemove", r), document.removeEventListener("mouseup", h), this.hooks.off("rotateStart", n), this.hooks.off("rotateEnd", s), this.hooks.off("moveByMouseEnable", n), this.hooks.off("moveByMouseDisable", s); }), this.moveByMouseEnable && this.moveByMouse(); } get moveByMouseEnable() { return this._moveByMouseEnable; } dispose() { var e; super.dispose(), (e = this.disposers) == null || e.forEach((t) => t == null ? void 0 : t()); } /** * @description: 跟随鼠标移动 */ moveByMouse() { this._moveByMouseEnable = !0; const e = this.handleMouseDown.bind(this), t = this.handleMouseUp.bind(this); this.hooks.emit("moveByMouseEnable"), setTimeout(() => { this.container.addEventListener("mousedown", e), this.container.addEventListener("mouseup", t); }), this.mousedownEventListenerDisposer = () => { this.container.removeEventListener("mousedown", e), this.container.removeEventListener("mouseup", t); }; } /** * @description: 禁用跟随鼠标移动 */ disableMoveByMouse() { var e; this._moveByMouseEnable = !1, this.hooks.emit("moveByMouseDisable"), (e = this.mousedownEventListenerDisposer) == null || e.call(this); } onIntersectionOnModelUpdate(e) { !this.moveByMouseEnable || this.hooks.emit("wantToMove", e.point) || (this.originObject3D.position.copy(e.point), this.hooks.emit("setObjectPosition", e.point), this.hooks.emit("move", e.point)); } onApplyOriginObjectScale(e) { } handleMouseDown(e) { const { x: t, y: o } = e; this.mouseInfo = { x: t, y: o, mouseDownTimestamp: Date.now() }; } handleMouseUp(e) { if (!this.mouseInfo) return; const { x: t, y: o } = e; this.mouseInfo.x === t && this.mouseInfo.y === o && Date.now() - this.mouseInfo.mouseDownTimestamp < 500 && (this.disableMoveByMouse(), this.mouseInfo = void 0); } /** * @description: 拖动开始,找出拖的Direction */ dragStart(e) { if (this._moveByMouseEnable || this.isDragging) return; const t = e == null ? void 0 : e.intersect; if (!t) return this.dragEnd(); const o = (t == null ? void 0 : t.object).direction; if (!o) return this.dragEnd(); const r = new m.Vector3(1, 0, 0).applyQuaternion(this.helperObject3D.quaternion), h = new m.Vector3(0, 1, 0).applyQuaternion(this.helperObject3D.quaternion), s = new m.Vector3(0, 0, 1).applyQuaternion(this.helperObject3D.quaternion), n = (() => { switch (o) { case "x": return r; case "y": return h; case "z": return s; } })(), l = (() => { switch (o) { case "x": case "y": return s; case "z": return r; } })(), a = t.point.clone(), d = new m.Plane(); d.setFromNormalAndCoplanarPoint(l, a), this.startInfo = { startVectorProject: a.clone().projectOnVector(n), directionVector: n, plane: d }, this.helperObject3D.showDraggingHelper([o]), this.hooks.emit("moveStart"), this.isDragging = !0; } dragging(e) { if (!this.isDragging || !this.startInfo) return; const t = g(this.camera, e, this.container); return t ? (this.move(t), !1) : this.dragEnd(); } move(e) { if (!this.startInfo) return this.dragEnd(); const { plane: t, directionVector: o, startVectorProject: r } = this.startInfo, h = this.originObject3D, s = e.ray.intersectPlane(t, new m.Vector3()); if (!s) return; const n = s.clone().projectOnVector(o), l = n.clone().sub(r), a = new m.Matrix4(); a.setPosition(l); const d = h.position.clone().applyMatrix4(a); this.hooks.emit("wantToMove", d) || (h.applyMatrix4(a), r.copy(n), this.hooks.emit("applyObjectPosition", { matrix: a }), this.hooks.emit("move", d)); } dragEnd() { this.isDragging && (this.startInfo = void 0, this.isDragging = !1, this.helperObject3D.show(), this.hooks.emit("moveEnd")); } } export { w as MoveController };