refactor: 重构 Manager 架构,引入 ManagerRegistry 和 BaseManager 基类
- 新增 ManagerRegistry 单例注册表,统一管理所有 Manager 实例 - 新增 BaseManager 基类,自动管理事件订阅清理 - 新增 BaseDialogManager 基类,统一对话框生命周期管理 - 重构 15 个 Manager 使用新基类 - 重构 Toolbar 按钮和 Menu 按钮移除 engine 参数依赖 - 删除 BimComponent 基类(已不再使用) - 为所有 Manager 和核心模块添加中文 JSDoc 注释
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
/**
|
||||
* 3D 引擎管理器
|
||||
* 负责管理 3D 渲染引擎的初始化、模型加载和测量功能
|
||||
*/
|
||||
import { Engine, type EngineOptions, type ModelLoadOptions } from '../components/engine';
|
||||
import { BimComponent } from '../core/component';
|
||||
import type { BimEngine } from '../bim-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';
|
||||
@@ -8,34 +11,27 @@ import type { MeasureMode } from '../types/measure';
|
||||
|
||||
/**
|
||||
* 3D 引擎管理器
|
||||
* 负责连接 Engine 组件和 BimEngine,向外部暴露简化的 API
|
||||
* 采用延迟初始化模式,用户需主动调用 initialize() 方法
|
||||
* 封装底层 3D 引擎,提供模型加载、相机控制、测量等功能
|
||||
*/
|
||||
export class EngineManager extends BimComponent {
|
||||
/** 3D 引擎挂载的父容器 */
|
||||
export class EngineManager extends BaseManager {
|
||||
/** 容器元素 */
|
||||
private container: HTMLElement;
|
||||
/** 3D 引擎组件实例 */
|
||||
/** 引擎实例 */
|
||||
private engineInstance: Engine | null = null;
|
||||
/** 右键菜单管理器 */
|
||||
public rightKey: RightKeyManager | null = null;
|
||||
|
||||
public rightKey: RightKeyManager | null = null; // 右键菜单管理器
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param engine 引擎实例
|
||||
* @param container 3D 引擎挂载的目标容器
|
||||
*/
|
||||
constructor(engine: BimEngine, container: HTMLElement) {
|
||||
super(engine);
|
||||
constructor(container: HTMLElement) {
|
||||
super();
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化 3D 引擎
|
||||
* @param options 引擎配置选项(可选,如果不提供则使用默认配置)
|
||||
* @param options 引擎配置选项
|
||||
* @returns 是否初始化成功
|
||||
*/
|
||||
public initialize(options?: Omit<EngineOptions, 'container'>): boolean {
|
||||
// 如果已经初始化,先销毁旧的实例
|
||||
if (this.engineInstance && this.engineInstance.isInitialized()) {
|
||||
console.warn('[EngineManager] 3D Engine already initialized. Destroying old instance...');
|
||||
this.engineInstance.destroy();
|
||||
@@ -43,24 +39,19 @@ export class EngineManager extends BimComponent {
|
||||
}
|
||||
|
||||
try {
|
||||
// 创建 Engine 组件实例
|
||||
// options 中的配置会自动复制给 createEngine 使用
|
||||
this.engineInstance = new Engine({
|
||||
container: this.container,
|
||||
...options, // 合并配置选项
|
||||
...options,
|
||||
});
|
||||
|
||||
// 调用组件的 init 方法初始化引擎
|
||||
this.engineInstance.init();
|
||||
|
||||
// 初始化右键 (移到 return 之前)
|
||||
this.rightKey = new RightKeyManager(this.engine, this.container);
|
||||
this.rightKey = new RightKeyManager(this.container);
|
||||
|
||||
// 注册默认右键菜单
|
||||
this.rightKey.registerHandler((_e) => {
|
||||
return [
|
||||
infoMenuButton(this.engine),
|
||||
homeMenuButton(this.engine)
|
||||
infoMenuButton(),
|
||||
homeMenuButton()
|
||||
];
|
||||
});
|
||||
|
||||
@@ -71,16 +62,19 @@ export class EngineManager extends BimComponent {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检<EFBFBD><EFBFBD><EFBFBD> 3D 引擎是否已初始化
|
||||
* 检查引擎是否已初始化
|
||||
* @returns 是否已初始化
|
||||
*/
|
||||
public isInitialized(): boolean {
|
||||
return this.engineInstance !== null && this.engineInstance.isInitialized();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载 3D 模型
|
||||
* @param url 模型文件 URL
|
||||
* @param options 加载选项(位置、旋转、缩放)
|
||||
* 加载模型
|
||||
* @param url 模型 URL
|
||||
* @param options 加载选项
|
||||
*/
|
||||
public loadModel(url: string, options?: ModelLoadOptions): void {
|
||||
if (!this.engineInstance) {
|
||||
@@ -90,10 +84,9 @@ export class EngineManager extends BimComponent {
|
||||
this.engineInstance.loadModel(url, options);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取原始 3D 引擎实例
|
||||
* 用于直接调用第三方引擎的其他 API
|
||||
* 获取底层引擎实例
|
||||
* @returns 引擎实例
|
||||
*/
|
||||
public getEngine(): any {
|
||||
if (!this.engineInstance) {
|
||||
@@ -103,9 +96,7 @@ export class EngineManager extends BimComponent {
|
||||
return this.engineInstance.getEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* 回到主视角
|
||||
*/
|
||||
/** 相机回到初始位置 */
|
||||
public CameraGoHome(): void {
|
||||
if (!this.engineInstance) {
|
||||
console.warn('[EngineManager] 3D Engine not initialized.');
|
||||
@@ -113,9 +104,10 @@ export class EngineManager extends BimComponent {
|
||||
}
|
||||
this.engineInstance.CameraGoHome();
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活测量功能
|
||||
* @param mode 测量类型
|
||||
* 激活测量模式
|
||||
* @param mode 测量模式
|
||||
*/
|
||||
public activateMeasure(mode: MeasureMode): void {
|
||||
if (!this.engineInstance) {
|
||||
@@ -125,9 +117,7 @@ export class EngineManager extends BimComponent {
|
||||
this.engineInstance.activateMeasure(mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用测量功能
|
||||
*/
|
||||
/** 停用测量模式 */
|
||||
public deactivateMeasure(): void {
|
||||
if (!this.engineInstance) {
|
||||
return;
|
||||
@@ -136,7 +126,8 @@ export class EngineManager extends BimComponent {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前激活的测量类型
|
||||
* 获取当前测量类型
|
||||
* @returns 当前测量模式
|
||||
*/
|
||||
public getCurrentMeasureType(): MeasureMode | null {
|
||||
if (!this.engineInstance) {
|
||||
@@ -145,11 +136,7 @@ export class EngineManager extends BimComponent {
|
||||
return this.engineInstance.getCurrentMeasureType();
|
||||
}
|
||||
|
||||
// ==================== 结束:测量功能方法 ====================
|
||||
|
||||
/**
|
||||
* 销毁 3D 引擎实例
|
||||
*/
|
||||
/** 销毁引擎管理器 */
|
||||
public destroy(): void {
|
||||
if (this.engineInstance) {
|
||||
this.engineInstance.destroy();
|
||||
@@ -159,8 +146,6 @@ export class EngineManager extends BimComponent {
|
||||
this.rightKey.destroy();
|
||||
this.rightKey = null;
|
||||
}
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user