refactor(editor): 重构编辑器架构并增强行为树执行可视化

This commit is contained in:
YHH
2025-11-04 18:29:28 +08:00
parent adfc7e91b3
commit f9afa22406
44 changed files with 4942 additions and 546 deletions

View File

@@ -7,6 +7,7 @@ import { EditorPluginCategory } from './IEditorPlugin';
import { UIRegistry } from '../Services/UIRegistry';
import { MessageHub } from '../Services/MessageHub';
import { SerializerRegistry } from '../Services/SerializerRegistry';
import { FileActionRegistry } from '../Services/FileActionRegistry';
const logger = createLogger('EditorPluginManager');
@@ -22,6 +23,7 @@ export class EditorPluginManager extends PluginManager {
private uiRegistry: UIRegistry | null = null;
private messageHub: MessageHub | null = null;
private serializerRegistry: SerializerRegistry | null = null;
private fileActionRegistry: FileActionRegistry | null = null;
/**
* 初始化编辑器插件管理器
@@ -32,6 +34,7 @@ export class EditorPluginManager extends PluginManager {
this.uiRegistry = services.resolve(UIRegistry);
this.messageHub = services.resolve(MessageHub);
this.serializerRegistry = services.resolve(SerializerRegistry);
this.fileActionRegistry = services.resolve(FileActionRegistry);
logger.info('EditorPluginManager initialized');
}
@@ -90,6 +93,24 @@ export class EditorPluginManager extends PluginManager {
logger.debug(`Registered ${serializers.length} serializers for ${plugin.name}`);
}
if (plugin.registerFileActionHandlers && this.fileActionRegistry) {
const handlers = plugin.registerFileActionHandlers();
console.log(`[EditorPluginManager] Registering ${handlers.length} file action handlers for ${plugin.name}`);
for (const handler of handlers) {
console.log(`[EditorPluginManager] Handler for extensions:`, handler.extensions);
this.fileActionRegistry.registerActionHandler(handler);
}
logger.debug(`Registered ${handlers.length} file action handlers for ${plugin.name}`);
}
if (plugin.registerFileCreationTemplates && this.fileActionRegistry) {
const templates = plugin.registerFileCreationTemplates();
for (const template of templates) {
this.fileActionRegistry.registerCreationTemplate(template);
}
logger.debug(`Registered ${templates.length} file creation templates for ${plugin.name}`);
}
if (plugin.onEditorReady) {
await plugin.onEditorReady();
}
@@ -332,6 +353,7 @@ export class EditorPluginManager extends PluginManager {
this.uiRegistry = null;
this.messageHub = null;
this.serializerRegistry = null;
this.fileActionRegistry = null;
logger.info('EditorPluginManager disposed');
}

View File

@@ -1,5 +1,6 @@
import type { IPlugin } from '@esengine/ecs-framework';
import type { MenuItem, ToolbarItem, PanelDescriptor } from '../Types/UITypes';
import type { ReactNode } from 'react';
/**
* 编辑器插件类别
@@ -51,6 +52,91 @@ export interface ISerializer<T = any> {
getSupportedType(): string;
}
/**
* 文件上下文菜单项
*/
export interface FileContextMenuItem {
/**
* 菜单项标签
*/
label: string;
/**
* 图标
*/
icon?: ReactNode;
/**
* 点击处理函数
*/
onClick: (filePath: string, parentPath: string) => void | Promise<void>;
/**
* 是否禁用
*/
disabled?: boolean;
/**
* 是否为分隔符
*/
separator?: boolean;
}
/**
* 文件创建模板
*/
export interface FileCreationTemplate {
/**
* 模板名称
*/
label: string;
/**
* 文件扩展名(不含点)
*/
extension: string;
/**
* 默认文件名
*/
defaultFileName: string;
/**
* 图标
*/
icon?: ReactNode;
/**
* 创建文件内容的函数
*/
createContent: (fileName: string) => string | Promise<string>;
}
/**
* 文件操作处理器
*/
export interface FileActionHandler {
/**
* 支持的文件扩展名列表
*/
extensions: string[];
/**
* 双击处理函数
*/
onDoubleClick?: (filePath: string) => void | Promise<void>;
/**
* 打开文件处理函数
*/
onOpen?: (filePath: string) => void | Promise<void>;
/**
* 获取上下文菜单项
*/
getContextMenuItems?: (filePath: string, parentPath: string) => FileContextMenuItem[];
}
/**
* 编辑器插件接口
*
@@ -131,6 +217,16 @@ export interface IEditorPlugin extends IPlugin {
* 获取行为树节点模板
*/
getNodeTemplates?(): any[];
/**
* 注册文件操作处理器
*/
registerFileActionHandlers?(): FileActionHandler[];
/**
* 注册文件创建模板
*/
registerFileCreationTemplates?(): FileCreationTemplate[];
}
/**

View File

@@ -0,0 +1,110 @@
import { IService } from '@esengine/ecs-framework';
import { FileActionHandler, FileCreationTemplate } from '../Plugins/IEditorPlugin';
/**
* 文件操作注册表服务
*
* 管理插件注册的文件操作处理器和文件创建模板
*/
export class FileActionRegistry implements IService {
private actionHandlers: Map<string, FileActionHandler[]> = new Map();
private creationTemplates: FileCreationTemplate[] = [];
/**
* 注册文件操作处理器
*/
registerActionHandler(handler: FileActionHandler): void {
for (const ext of handler.extensions) {
const handlers = this.actionHandlers.get(ext) || [];
handlers.push(handler);
this.actionHandlers.set(ext, handlers);
}
}
/**
* 注册文件创建模板
*/
registerCreationTemplate(template: FileCreationTemplate): void {
this.creationTemplates.push(template);
}
/**
* 获取文件扩展名的处理器
*/
getHandlersForExtension(extension: string): FileActionHandler[] {
return this.actionHandlers.get(extension) || [];
}
/**
* 获取文件的处理器
*/
getHandlersForFile(filePath: string): FileActionHandler[] {
const extension = this.getFileExtension(filePath);
return extension ? this.getHandlersForExtension(extension) : [];
}
/**
* 获取所有文件创建模板
*/
getCreationTemplates(): FileCreationTemplate[] {
return this.creationTemplates;
}
/**
* 处理文件双击
*/
async handleDoubleClick(filePath: string): Promise<boolean> {
const extension = this.getFileExtension(filePath);
console.log('[FileActionRegistry] handleDoubleClick:', filePath);
console.log('[FileActionRegistry] Extension:', extension);
console.log('[FileActionRegistry] Total handlers:', this.actionHandlers.size);
console.log('[FileActionRegistry] Registered extensions:', Array.from(this.actionHandlers.keys()));
const handlers = this.getHandlersForFile(filePath);
console.log('[FileActionRegistry] Found handlers:', handlers.length);
for (const handler of handlers) {
if (handler.onDoubleClick) {
console.log('[FileActionRegistry] Calling handler for extensions:', handler.extensions);
await handler.onDoubleClick(filePath);
return true;
}
}
return false;
}
/**
* 处理文件打开
*/
async handleOpen(filePath: string): Promise<boolean> {
const handlers = this.getHandlersForFile(filePath);
for (const handler of handlers) {
if (handler.onOpen) {
await handler.onOpen(filePath);
return true;
}
}
return false;
}
/**
* 清空所有注册
*/
clear(): void {
this.actionHandlers.clear();
this.creationTemplates = [];
}
/**
* 释放资源
*/
dispose(): void {
this.clear();
}
private getFileExtension(filePath: string): string | null {
const lastDot = filePath.lastIndexOf('.');
if (lastDot === -1) return null;
return filePath.substring(lastDot + 1).toLowerCase();
}
}

View File

@@ -0,0 +1,180 @@
import { IService } from '@esengine/ecs-framework';
import { ComponentType } from 'react';
/**
* 窗口描述符
*/
export interface WindowDescriptor {
/**
* 窗口唯一标识
*/
id: string;
/**
* 窗口组件
*/
component: ComponentType<any>;
/**
* 窗口标题
*/
title?: string;
/**
* 默认宽度
*/
defaultWidth?: number;
/**
* 默认高度
*/
defaultHeight?: number;
}
/**
* 窗口实例
*/
export interface WindowInstance {
/**
* 窗口描述符
*/
descriptor: WindowDescriptor;
/**
* 是否打开
*/
isOpen: boolean;
/**
* 窗口参数
*/
params?: Record<string, any>;
}
/**
* 窗口注册表服务
*
* 管理插件注册的窗口组件
*/
export class WindowRegistry implements IService {
private windows: Map<string, WindowDescriptor> = new Map();
private openWindows: Map<string, WindowInstance> = new Map();
private listeners: Set<() => void> = new Set();
/**
* 注册窗口
*/
registerWindow(descriptor: WindowDescriptor): void {
if (this.windows.has(descriptor.id)) {
console.warn(`Window ${descriptor.id} is already registered`);
return;
}
this.windows.set(descriptor.id, descriptor);
console.log(`[WindowRegistry] Registered window: ${descriptor.id}`);
}
/**
* 取消注册窗口
*/
unregisterWindow(windowId: string): void {
this.windows.delete(windowId);
this.openWindows.delete(windowId);
this.notifyListeners();
}
/**
* 获取窗口描述符
*/
getWindow(windowId: string): WindowDescriptor | undefined {
return this.windows.get(windowId);
}
/**
* 获取所有窗口描述符
*/
getAllWindows(): WindowDescriptor[] {
return Array.from(this.windows.values());
}
/**
* 打开窗口
*/
openWindow(windowId: string, params?: Record<string, any>): void {
const descriptor = this.windows.get(windowId);
if (!descriptor) {
console.warn(`Window ${windowId} is not registered`);
return;
}
console.log(`[WindowRegistry] Opening window: ${windowId}`, params);
this.openWindows.set(windowId, {
descriptor,
isOpen: true,
params
});
this.notifyListeners();
}
/**
* 关闭窗口
*/
closeWindow(windowId: string): void {
console.log(`[WindowRegistry] Closing window: ${windowId}`);
this.openWindows.delete(windowId);
this.notifyListeners();
}
/**
* 获取打开的窗口实例
*/
getOpenWindow(windowId: string): WindowInstance | undefined {
return this.openWindows.get(windowId);
}
/**
* 获取所有打开的窗口
*/
getAllOpenWindows(): WindowInstance[] {
return Array.from(this.openWindows.values());
}
/**
* 检查窗口是否打开
*/
isWindowOpen(windowId: string): boolean {
return this.openWindows.has(windowId);
}
/**
* 添加变化监听器
*/
addListener(listener: () => void): () => void {
this.listeners.add(listener);
return () => {
this.listeners.delete(listener);
};
}
/**
* 通知所有监听器
*/
private notifyListeners(): void {
this.listeners.forEach(listener => listener());
}
/**
* 清空所有窗口
*/
clear(): void {
this.windows.clear();
this.openWindows.clear();
this.listeners.clear();
}
/**
* 释放资源
*/
dispose(): void {
this.clear();
}
}

View File

@@ -146,6 +146,11 @@ export interface PanelDescriptor {
* 排序权重
*/
order?: number;
/**
* 是否为动态面板(不默认显示,需要手动打开)
*/
isDynamic?: boolean;
}
/**

View File

@@ -19,6 +19,7 @@ export * from './Services/ComponentDiscoveryService';
export * from './Services/LogService';
export * from './Services/SettingsRegistry';
export * from './Services/SceneManagerService';
export * from './Services/FileActionRegistry';
export * from './Types/UITypes';
export * from './Types/IFileAPI';