组件注册与添加
This commit is contained in:
47
packages/editor-core/src/Services/ComponentRegistry.ts
Normal file
47
packages/editor-core/src/Services/ComponentRegistry.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Injectable, IService, Component } from '@esengine/ecs-framework';
|
||||
|
||||
export interface ComponentTypeInfo {
|
||||
name: string;
|
||||
type: new (...args: any[]) => Component;
|
||||
category?: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理编辑器中可用的组件类型
|
||||
*/
|
||||
@Injectable()
|
||||
export class ComponentRegistry implements IService {
|
||||
private components: Map<string, ComponentTypeInfo> = new Map();
|
||||
|
||||
public dispose(): void {
|
||||
this.components.clear();
|
||||
}
|
||||
|
||||
public register(info: ComponentTypeInfo): void {
|
||||
this.components.set(info.name, info);
|
||||
}
|
||||
|
||||
public unregister(name: string): void {
|
||||
this.components.delete(name);
|
||||
}
|
||||
|
||||
public getComponent(name: string): ComponentTypeInfo | undefined {
|
||||
return this.components.get(name);
|
||||
}
|
||||
|
||||
public getAllComponents(): ComponentTypeInfo[] {
|
||||
return Array.from(this.components.values());
|
||||
}
|
||||
|
||||
public getComponentsByCategory(category: string): ComponentTypeInfo[] {
|
||||
return this.getAllComponents().filter(c => c.category === category);
|
||||
}
|
||||
|
||||
public createInstance(name: string, ...args: any[]): Component | null {
|
||||
const info = this.components.get(name);
|
||||
if (!info) return null;
|
||||
|
||||
return new info.type(...args);
|
||||
}
|
||||
}
|
||||
@@ -11,5 +11,6 @@ export * from './Services/UIRegistry';
|
||||
export * from './Services/MessageHub';
|
||||
export * from './Services/SerializerRegistry';
|
||||
export * from './Services/EntityStoreService';
|
||||
export * from './Services/ComponentRegistry';
|
||||
|
||||
export * from './Types/UITypes';
|
||||
|
||||
Reference in New Issue
Block a user