Files
bim_engine/src/managers/engine-manager.ts

170 lines
4.8 KiB
TypeScript
Raw Normal View History

/**
* 3D
* 3D
*/
2025-12-04 18:41:11 +08:00
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';
2025-12-04 18:41:11 +08:00
/**
* 3D
* 3D
2025-12-04 18:41:11 +08:00
*/
export class EngineManager extends BaseManager {
/** 容器元素 */
2025-12-04 18:41:11 +08:00
private container: HTMLElement;
/** 引擎实例 */
2025-12-08 10:02:24 +08:00
private engineInstance: Engine | null = null;
/** 右键菜单管理器 */
public rightKey: RightKeyManager | null = null;
2025-12-04 18:41:11 +08:00
constructor(container: HTMLElement) {
super();
2025-12-04 18:41:11 +08:00
this.container = container;
}
/**
* 3D
* @param options
2025-12-04 18:41:11 +08:00
* @returns
*/
public initialize(options?: Omit<EngineOptions, 'container'>): boolean {
2025-12-08 10:02:24 +08:00
if (this.engineInstance && this.engineInstance.isInitialized()) {
2025-12-04 18:41:11 +08:00
console.warn('[EngineManager] 3D Engine already initialized. Destroying old instance...');
2025-12-08 10:02:24 +08:00
this.engineInstance.destroy();
this.engineInstance = null;
2025-12-04 18:41:11 +08:00
}
try {
2025-12-08 10:02:24 +08:00
this.engineInstance = new Engine({
2025-12-04 18:41:11 +08:00
container: this.container,
...options,
2025-12-04 18:41:11 +08:00
});
2025-12-08 10:02:24 +08:00
this.engineInstance.init();
2025-12-04 18:41:11 +08:00
this.rightKey = new RightKeyManager(this.container);
this.rightKey.registerHandler((_e) => {
return [
infoMenuButton(),
homeMenuButton()
];
});
2025-12-08 10:02:24 +08:00
return this.engineInstance.isInitialized();
2025-12-04 18:41:11 +08:00
} catch (error) {
console.error('[EngineManager] Failed to initialize 3D engine:', error);
2025-12-08 10:02:24 +08:00
this.engineInstance = null;
2025-12-04 18:41:11 +08:00
return false;
}
}
2025-12-04 18:41:11 +08:00
/**
*
* @returns
2025-12-04 18:41:11 +08:00
*/
public isInitialized(): boolean {
2025-12-08 10:02:24 +08:00
return this.engineInstance !== null && this.engineInstance.isInitialized();
2025-12-04 18:41:11 +08:00
}
2025-12-04 18:41:11 +08:00
/**
*
* @param url URL
* @param options
2025-12-04 18:41:11 +08:00
*/
public loadModel(url: string, options?: ModelLoadOptions): void {
if (!this.engineInstance) {
console.warn('[EngineManager] 3D Engine not initialized.');
2025-12-04 18:41:11 +08:00
return;
}
2025-12-08 10:02:24 +08:00
this.engineInstance.loadModel(url, options);
2025-12-04 18:41:11 +08:00
}
/**
*
* @returns
2025-12-04 18:41:11 +08:00
*/
public getEngine(): any {
2025-12-08 10:02:24 +08:00
if (!this.engineInstance) {
2025-12-04 18:41:11 +08:00
console.warn('[EngineManager] 3D Engine not initialized.');
return null;
}
2025-12-08 10:02:24 +08:00
return this.engineInstance.getEngine();
2025-12-04 18:41:11 +08:00
}
/** 相机回到初始位置 */
public CameraGoHome(): void {
if (!this.engineInstance) {
console.warn('[EngineManager] 3D Engine not initialized.');
return;
}
this.engineInstance.CameraGoHome();
}
/** 暂停渲染 */
public pauseRendering(): void {
if (!this.engineInstance) {
console.warn('[EngineManager] 3D Engine not initialized.');
return;
}
this.engineInstance.pauseRendering();
}
/** 恢复渲染 */
public resumeRendering(): void {
if (!this.engineInstance) {
console.warn('[EngineManager] 3D Engine not initialized.');
return;
}
this.engineInstance.resumeRendering();
}
/**
*
* @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();
}
/** 销毁引擎管理器 */
2025-12-04 18:41:11 +08:00
public destroy(): void {
2025-12-08 10:02:24 +08:00
if (this.engineInstance) {
this.engineInstance.destroy();
this.engineInstance = null;
2025-12-04 18:41:11 +08:00
}
if (this.rightKey) {
this.rightKey.destroy();
this.rightKey = null;
}
super.destroy();
2025-12-04 18:41:11 +08:00
}
}