/** * 测量对话框管理器 * 负责管理测量工具对话框的显示、隐藏和测量面板的交互 */ import { BaseDialogManager } from '../core/base-dialog-manager'; import { MeasurePanel } from '../components/measure-panel'; import type { MeasureConfig, MeasureMode, MeasureResult } from '../components/measure-panel/types'; /** * 测量对话框管理器 * 继承自 BaseDialogManager,提供测量工具的对话框管理功能 */ export class MeasureDialogManager extends BaseDialogManager { /** 测量面板实例 */ private panel: MeasurePanel | null = null; /** 测量配置(单位、精度等) */ private config: MeasureConfig | null = null; /** 对话框唯一标识 */ protected get dialogId(): string { return 'measure-dialog'; } /** 对话框标题(国际化 key) */ protected get dialogTitle(): string { return 'measure.dialogTitle'; } /** 对话框宽度 */ protected get dialogWidth(): number { return 250; } /** * 创建对话框内容 * 初始化测量面板并设置回调 */ protected createContent(): HTMLElement { this.panel = new MeasurePanel({ defaultMode: 'distance', defaultExpanded: false, onModeChange: (mode) => { console.log('[MeasureDialogManager] 当前测量方式已切换:', mode); this.registry.engine3d?.activateMeasure(mode); }, onClearAll: () => { console.log('[MeasureDialogManager] 删除全部'); this.registry.engine3d?.clearAllMeasures(); }, onSettings: () => { console.log('[MeasureDialogManager] 打开设置'); }, onExpandedChange: () => { this.dialog?.fitHeight(false); } }); this.panel.init(); this.config = this.panel.getConfig(); const panelWrapper = document.createElement('div'); panelWrapper.style.padding = '12px'; panelWrapper.appendChild(this.panel.element); return panelWrapper; } /** 对话框创建后的回调,自适应高度 */ protected onDialogCreated(): void { this.dialog?.fitHeight(false); } /** 对话框关闭时的回调,取消工具栏按钮激活状态 */ protected onDialogClose(): void { this.registry.toolbar?.setBtnActive('measure', false); } /** 销毁前的清理,停用测量功能并销毁面板 */ protected onBeforeDestroy(): void { if (this.registry.engine3d) { this.registry.engine3d.deactivateMeasure(); } if (this.panel) { this.panel.destroy(); this.panel = null; } } /** * 获取当前激活的测量模式 * @returns 当前测量模式,如 'distance'、'angle' 等 */ public getActiveMode(): MeasureMode | null { return this.panel ? this.panel.getActiveMode() : null; } /** * 切换测量模式 * @param mode 目标测量模式 */ public switchMode(mode: MeasureMode): void { if (!this.panel) return; this.panel.switchMode(mode); } /** * 设置测量结果 * @param result 测量结果对象 */ public setMeasureResult(result: MeasureResult | null): void { if (!this.panel) return; this.panel.setResult(result); } /** * 获取测量配置 * @returns 测量配置副本 */ public getConfig(): MeasureConfig | null { if (this.panel) { this.config = this.panel.getConfig(); } return this.config ? { ...this.config } : null; } /** * 设置测量配置 * @param partial 部分配置 * @param persist 是否持久化 */ public setConfig(partial: Partial, persist: boolean = true): void { if (this.panel) { this.panel.setConfig(partial, persist); this.config = this.panel.getConfig(); this.dialog?.fitHeight(false); return; } const prev = this.config; const next: MeasureConfig = { unit: partial.unit ?? prev?.unit ?? 'mm', precision: partial.precision ?? prev?.precision ?? 2 }; this.config = next; } /** 清除所有测量结果 */ public clearAll(): void { if (!this.panel) return; this.panel.clearAll(); } /** 打开测量设置面板 */ public openSettings(): void { if (!this.panel) return; this.panel.openSettings(); } }