From 8ebb513fb9f98c55c2edde8ae3c45fb86b4fd749 Mon Sep 17 00:00:00 2001 From: ChenX Date: Tue, 5 Dec 2017 14:19:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86GetPoint=E4=BB=A3=E7=A0=81.?= =?UTF-8?q?=20=E5=88=86=E7=A6=BB=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Editor/Editor.ts | 225 +++++++++++++++++++++++++------------------ 1 file changed, 133 insertions(+), 92 deletions(-) diff --git a/src/Editor/Editor.ts b/src/Editor/Editor.ts index 1d6352f75..4eeb1a06d 100644 --- a/src/Editor/Editor.ts +++ b/src/Editor/Editor.ts @@ -36,104 +36,141 @@ export class Editor } GetPoint(prompt?: GetPointPrompt): Promise { + prompt = prompt ? prompt : {}; + this.m_App.m_CursorViewer.ToGetpoint(); this.m_InputState = InputState.GetPoint; + let retValue = new PromptPointResult(); - let removeSnap = this.Snap(prompt ? prompt.BasePoint : undefined); + let removeCalls = []; //删除回调列表 + + removeCalls.push(this.Snap()); + let res = new Promise((resolve, reject) => { - let line: Line, removeDrag; - if (prompt) + //如果有基点,那么绘制直线 + if (prompt.BasePoint) { - let p = this.m_MouseCtrl.m_CurMousePointWCS.clone(); - if (prompt.Callback) - { - prompt.Callback(p); - } - if (prompt.BasePoint) - { - line = new Line(prompt.BasePoint, prompt.BasePoint); + let line = new Line(prompt.BasePoint, prompt.BasePoint); - this.m_App.m_Database.appendEntity(line); + this.m_App.m_Database.appendEntity(line); - this.noSnapList.add(line.Draw(RenderType.Wireframe)); - } - removeDrag = xaop.end(this.m_MouseCtrl, this.m_MouseCtrl.onMouseMove, () => - { - let p = app.m_CursorViewer.ptWcs; - app.m_Viewer.ScreenToWorld(p); + let drawObj = line.Draw(RenderType.Wireframe); - if (prompt.BasePoint) + this.noSnapList.add(drawObj); + + removeCalls.push( + () => + { + this.m_App.m_Database.removeEntityId(line.objectId); + }, + xaop.end(this.m_MouseCtrl, this.m_MouseCtrl.onMouseMove, () => { + let p = this.GetNowPoint(prompt.BasePoint); line.setEndPoint(p); - } - if (prompt.Callback) + this.UpdateScreen(); + }) + ); + } + if (prompt.Callback) + { + removeCalls.push( + xaop.end(this.m_MouseCtrl, this.m_MouseCtrl.onMouseMove, () => { + let p = this.GetNowPoint(prompt.BasePoint); prompt.Callback(p); + this.UpdateScreen(); + }) + ); + } + + //鼠标按下 + removeCalls.push( + xaop.end(this.m_MouseCtrl, this.m_MouseCtrl.onMouseDown, (e: MouseEvent) => + { + if (e.button == MouseKey.Left) + { + retValue.Status = PromptStatus.OK; + retValue.Value = this.GetNowPoint(prompt.BasePoint); + _return(); } - this.UpdateScreen(); }) - } + ); + //键盘按下 + removeCalls.push( + xaop.begin(this.m_KeyCtrl, this.m_KeyCtrl.OnKeyDown, (e: KeyboardEvent) => + { + if (e.keyCode == 27) + { + retValue.Status = PromptStatus.Cancel; + _return(); + } + else if (e.keyCode == 32) + { + retValue.Status = PromptStatus.None; + _return(); + } + }) + ); - let dispose = () => + //返回 + let _return = () => { - if (line) - this.m_App.m_Database.removeEntityId(line.objectId); - - removeSnap(); + this.m_App.m_CursorViewer.ToSelect(); + this.m_InputState = InputState.None; this.noSnapList.clear(); - this.m_App.m_CursorViewer.ToSelect(); - this.m_InputState = InputState.None; - remove(); - remove2(); - if (prompt) + for (let f of removeCalls) { - removeDrag(); + f(); } - resolve(retValue); - - + resolve(retValue); this.UpdateScreen(); } - let remove = xaop.end(this.m_MouseCtrl, this.m_MouseCtrl.onMouseDown, (e: MouseEvent) => - { - if (e.button == MouseKey.Left) - { - retValue.Status = PromptStatus.OK; - retValue.Value = this.m_MouseCtrl.m_CurMousePointWCS; - - let p = app.m_CursorViewer.ptWcs; - app.m_Viewer.ScreenToWorld(p); - retValue.Value = p; - dispose(); - } - }); - - let remove2 = xaop.begin(this.m_KeyCtrl, this.m_KeyCtrl.OnKeyDown, (e: KeyboardEvent) => - { - if (e.keyCode == 27) - { - retValue.Status = PromptStatus.Cancel; - dispose(); - } - else if (e.keyCode == 32) - { - console.log("空格 退出"); - retValue.Status = PromptStatus.None; - dispose(); - } - }) - }) return res; } + /** + * + * 获得当前光标所在的点, 先判断捕捉,在判断极轴捕捉 + * + * @private + * @param {THREE.Vector3} [basePoint] + * @returns {THREE.Vector3} + * @memberof Editor + */ + private GetNowPoint(basePoint?: THREE.Vector3): THREE.Vector3 + { + if (this.m_App.m_CursorViewer.m_LastSnapPoint) + { + let retP = app.m_CursorViewer.ptWcs; + app.m_Viewer.ScreenToWorld(retP); + return retP; + } + if (basePoint) + { + let newP = this.AxisSnap(basePoint); + if (newP) + { + return newP; + } + } + return this.m_MouseCtrl.m_CurMousePointWCS.clone(); + } + noSnapList = new Set(); - Snap(basePoint?: THREE.Vector3) + + /** + * 点捕捉服务. 如果有捕捉 将设置光标的捕捉设置 + * + * @returns + * @memberof Editor + */ + Snap() { let testSnap = () => { @@ -179,39 +216,43 @@ export class Editor } } } + } + testSnap(); + return xaop.end(this.m_MouseCtrl, this.m_MouseCtrl.onMouseMove, testSnap); + } - if (basePoint) - { - let p = this.m_MouseCtrl.m_CurMousePointWCS.clone(); - - let anV = p.clone().sub(basePoint); - let an = Math.atan2(anV.y, anV.x); + /** + * + * 轴捕捉 + * @param {any} basePoint + * @returns {THREE.Vector3} 返回捕捉的点 如果捕捉不到 那么返回空 + * @memberof Editor + */ + AxisSnap(basePoint): THREE.Vector3 + { + let wcs = this.m_MouseCtrl.m_CurMousePointWCS.clone(); - if (an < 0) an += Math.PI * 2; - let newan = GeUtils.fixAngle(an, Math.PI * 0.25); + //相差向量 + let subVec = wcs.clone().sub(basePoint); + let an = Math.atan2(subVec.y, subVec.x); - if (!GeUtils.equaln(newan, an, 0.001)) - { - //dis. - let v0 = new THREE.Vector3(0, 0, 0); - GeUtils.angle(v0, newan, 1); + if (an < 0) an += Math.PI * 2; + let newan = GeUtils.fixAngle(an, Math.PI * 0.25); - let dis = v0.dot(p.sub(basePoint)); - console.log(dis); + if (!GeUtils.equaln(newan, an, 0.001)) + { + //dis. + let v0 = new THREE.Vector3(0, 0, 0); + GeUtils.angle(v0, newan, 1); - let newP = basePoint.clone(); - GeUtils.angle(newP, newan, dis); + let dis = v0.dot(wcs.sub(basePoint)); - app.m_Viewer.WorldToScreen(newP); - app.m_CursorViewer.SerCursorPostion(newP); - app.m_CursorViewer.render(); - return; - } - } + let retP = basePoint.clone(); + GeUtils.angle(retP, newan, dis); + return retP; } - testSnap(); - return xaop.end(this.m_MouseCtrl, this.m_MouseCtrl.onMouseMove, testSnap); + return undefined; } PointToScreen(pt: THREE.Vector3): THREE.Vector2