/** * 3D 引擎管理器 * 负责管理 3D 渲染引擎的初始化、模型加载和测量功能 */ import { Engine, type EngineOptions, type ModelLoadOptions } from '../components/engine'; import { BaseManager } from '../core/base-manager'; import { RightKeyManager } from './right-key-manager'; import { infoMenuButton } from '../components/menu/buttons/info'; import { homeMenuButton } from '../components/menu/buttons/home'; import type { MeasureMode } from '../types/measure'; /** * 3D 引擎管理器 * 封装底层 3D 引擎,提供模型加载、相机控制、测量等功能 */ export class EngineManager extends BaseManager { /** 容器元素 */ private container: HTMLElement; /** 引擎实例 */ private engineInstance: Engine | null = null; /** 右键菜单管理器 */ public rightKey: RightKeyManager | null = null; constructor(container: HTMLElement) { super(); this.container = container; } /** * 初始化 3D 引擎 * @param options 引擎配置选项 * @returns 是否初始化成功 */ public initialize(options?: Omit): boolean { if (this.engineInstance && this.engineInstance.isInitialized()) { console.warn('[EngineManager] 3D Engine already initialized. Destroying old instance...'); this.engineInstance.destroy(); this.engineInstance = null; } try { this.engineInstance = new Engine({ container: this.container, ...options, }); this.engineInstance.init(); this.rightKey = new RightKeyManager(this.container); this.rightKey.registerHandler((_e) => { return [ infoMenuButton(), homeMenuButton() ]; }); return this.engineInstance.isInitialized(); } catch (error) { console.error('[EngineManager] Failed to initialize 3D engine:', error); this.engineInstance = null; return false; } } /** * 检查引擎是否已初始化 * @returns 是否已初始化 */ public isInitialized(): boolean { return this.engineInstance !== null && this.engineInstance.isInitialized(); } /** * 加载模型 * @param url 模型 URL * @param options 加载选项 */ public loadModel(url: string, options?: ModelLoadOptions): void { if (!this.engineInstance) { console.warn('[EngineManager] 3D Engine not initialized.'); return; } this.engineInstance.loadModel(url, options); } /** * 获取底层引擎实例 * @returns 引擎实例 */ public getEngine(): any { if (!this.engineInstance) { console.warn('[EngineManager] 3D Engine not initialized.'); return null; } return this.engineInstance.getEngine(); } /** 相机回到初始位置 */ public CameraGoHome(): void { if (!this.engineInstance) { console.warn('[EngineManager] 3D Engine not initialized.'); return; } this.engineInstance.CameraGoHome(); } /** * 激活测量模式 * @param mode 测量模式 */ public activateMeasure(mode: MeasureMode): void { if (!this.engineInstance) { console.warn('[EngineManager] 3D Engine not initialized.'); return; } this.engineInstance.activateMeasure(mode); } /** 停用测量模式 */ public deactivateMeasure(): void { if (!this.engineInstance) { return; } this.engineInstance.deactivateMeasure(); } /** * 获取当前测量类型 * @returns 当前测量模式 */ public getCurrentMeasureType(): MeasureMode | null { if (!this.engineInstance) { return null; } return this.engineInstance.getCurrentMeasureType(); } /** 销毁引擎管理器 */ public destroy(): void { if (this.engineInstance) { this.engineInstance.destroy(); this.engineInstance = null; } if (this.rightKey) { this.rightKey.destroy(); this.rightKey = null; } super.destroy(); } }