diff --git a/src/designer/Viewport.ts b/src/designer/Viewport.ts index 2ee9b59..6aa4395 100644 --- a/src/designer/Viewport.ts +++ b/src/designer/Viewport.ts @@ -7,7 +7,8 @@ import type WorldModel from '@/designer/WorldModel.ts' import $ from 'jquery' import { reactive, watch } from 'vue' import MouseMoveInspect from '@/designer/model2DEditor/tools/MouseMoveInspect.ts' -import type { ITools } from '@/designer/model2DEditor/tools/ITools.ts' +import type { ITool } from '@/designer/model2DEditor/tools/ITool.ts' +import RulerInspect from '@/designer/model2DEditor/tools/RulerInspect.ts' /** * 编辑器对象 @@ -26,7 +27,8 @@ export default class Viewport { raycaster: Raycaster animationFrameId: any = null - tools: ITools[] = [ + cursorTool: ITool | null = null + tools: ITool[] = [ new MouseMoveInspect() ] @@ -35,7 +37,7 @@ export default class Viewport { */ resizeObserver?: ResizeObserver - unwatchList: (() => void)[] = [] + watchList: (() => void)[] = [] //@ts-ignore state: ViewportState = reactive({ @@ -146,12 +148,36 @@ export default class Viewport { this.animate() - const unWatchFn = watch(() => this.state.camera.position.y, (newVal) => { - if (this.state.isReady) { - this.updateGridVisibility() + // 监听事件 + this.watchList.push(watch(() => this.state.camera.position.y, (newVal) => { + if (!this.state.isReady) { + return + } + this.updateGridVisibility() + })) + this.watchList.push(watch(() => this.state.cursorMode, (newVal) => { + if (!this.state.isReady) { + return + } + if (this.cursorTool) { + this.cursorTool.destory() + this.cursorTool = null + } + if (newVal === 'normal' || !newVal) { + return } - }) - this.unwatchList.push(unWatchFn) + + if (newVal === 'Ruler') { + // 选择标尺工具 + this.cursorTool = new RulerInspect() + } else { + system.showErrorDialog(`当前鼠标模式 ${newVal} 不支持`) + } + + if (this.cursorTool) { + this.cursorTool.init(this) + } + })) if (this.resizeObserver) { this.resizeObserver.unobserve(this.viewerDom) @@ -159,8 +185,10 @@ export default class Viewport { this.resizeObserver = new ResizeObserver(this.handleResize.bind(this)) this.resizeObserver.observe(this.viewerDom) + // 初始化射线投射器 this.raycaster = new Raycaster() + // 初始化所有常驻工具 for (const tool of this.tools) { tool.init(this) } @@ -311,11 +339,13 @@ export default class Viewport { this.animationFrameId = null } - if (this.unwatchList) { - _.forEach(this.unwatchList, (unWatchFn => { - unWatchFn() + if (this.watchList) { + _.forEach(this.watchList, (unWatchFn => { + if (typeof unWatchFn === 'function') { + unWatchFn() + } })) - this.unwatchList = [] + this.watchList = [] } if (this.tools) { diff --git a/src/designer/model2DEditor/Model2DEditor.vue b/src/designer/model2DEditor/Model2DEditor.vue index f4dcffb..f3e952e 100644 --- a/src/designer/model2DEditor/Model2DEditor.vue +++ b/src/designer/model2DEditor/Model2DEditor.vue @@ -12,7 +12,7 @@
-