import { BaseDialogManager } from '../core/base-dialog-manager'; import { MeasurePanel } from '../components/measure-panel'; import type { MeasureConfig, MeasureResult } from '../components/measure-panel/types'; import { ENGINE_TYPE_TO_MODE, MEASURE_TYPES, type MeasureMode } from '../types/measure'; interface EngineMeasureData { id: string; point1?: { x: number; y: number; z: number }; point2?: { x: number; y: number; z: number }; text: number; type: string; isSelect: boolean; container: any; } export class MeasureDialogManager extends BaseDialogManager { private panel: MeasurePanel | null = null; private config: MeasureConfig | null = null; private unsubscribeMeasureChanged: (() => void) | null = null; protected get dialogId(): string { return 'measure-dialog'; } 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); }, onConfigSave: (config) => { console.log('[MeasureDialogManager] 保存设置:', config); this.registry.engine3d?.saveMeasureSetting({ unit: config.unit, precision: config.precision }); } }); this.panel.init(); this.config = this.panel.getConfig(); if (this.config) { console.log('[MeasureDialogManager] 同步缓存设置到引擎:', this.config); this.registry.engine3d?.saveMeasureSetting({ unit: this.config.unit, precision: this.config.precision }); } const panelWrapper = document.createElement('div'); panelWrapper.style.padding = '12px'; panelWrapper.appendChild(this.panel.element); return panelWrapper; } protected onDialogCreated(): void { this.dialog?.fitHeight(false); const engine = this.registry.engine3d?.getEngine(); if (engine?.events) { const handler = (data: EngineMeasureData) => { console.log('[MeasureDialogManager] 测量值变化:', data); if (data && this.panel) { this.handleMeasureChanged(data); } }; engine.events.on('measure-changed', handler); this.unsubscribeMeasureChanged = () => engine.events.off('measure-changed', handler); } } private handleMeasureChanged(data: EngineMeasureData): void { if (!this.panel) return; const targetMode = ENGINE_TYPE_TO_MODE[data.type]; if (!targetMode) { console.warn('[MeasureDialogManager] 未知测量类型:', data.type); return; } const currentMode = this.panel.getActiveMode(); if (currentMode !== targetMode) { this.panel.switchMode(targetMode, false); } const result = this.convertToMeasureResult(data, targetMode); this.panel.setResult(result); } private convertToMeasureResult(data: EngineMeasureData, mode: MeasureMode): MeasureResult { const config = MEASURE_TYPES[mode]; const result: MeasureResult = {}; if (config.valueType === 'point' && data.point1) { result.xyz = { x: data.point1.x, y: data.point1.y, z: data.point1.z }; } else { (result as any)[config.resultField] = data.text; } return result; } protected onDialogClose(): void { if (this.unsubscribeMeasureChanged) { this.unsubscribeMeasureChanged(); this.unsubscribeMeasureChanged = null; } this.registry.toolbar?.setBtnActive('measure', false); } protected onBeforeDestroy(): void { if (this.unsubscribeMeasureChanged) { this.unsubscribeMeasureChanged(); this.unsubscribeMeasureChanged = null; } if (this.registry.engine3d) { this.registry.engine3d.deactivateMeasure(); } if (this.panel) { this.panel.destroy(); this.panel = null; } } public getActiveMode(): MeasureMode | null { return this.panel ? this.panel.getActiveMode() : null; } public switchMode(mode: MeasureMode): void { if (!this.panel) return; this.panel.switchMode(mode); } public setMeasureResult(result: MeasureResult | null): void { if (!this.panel) return; this.panel.setResult(result); } public getConfig(): MeasureConfig | null { if (this.panel) { this.config = this.panel.getConfig(); } return this.config ? { ...this.config } : null; } 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(); } }