2025-10-14 22:53:26 +08:00
|
|
|
import { useState, useEffect } from 'react';
|
2025-10-14 23:31:09 +08:00
|
|
|
import { Core, Scene } from '@esengine/ecs-framework';
|
2025-10-15 17:21:59 +08:00
|
|
|
import { EditorPluginManager, UIRegistry, MessageHub, SerializerRegistry, EntityStoreService, ComponentRegistry, LocaleService, ProjectService, ComponentDiscoveryService, ComponentLoaderService, PropertyMetadataService, LogService } from '@esengine/editor-core';
|
2025-10-14 22:53:26 +08:00
|
|
|
import { SceneInspectorPlugin } from './plugins/SceneInspectorPlugin';
|
2025-10-15 09:34:44 +08:00
|
|
|
import { StartupPage } from './components/StartupPage';
|
2025-10-14 23:31:09 +08:00
|
|
|
import { SceneHierarchy } from './components/SceneHierarchy';
|
|
|
|
|
import { EntityInspector } from './components/EntityInspector';
|
2025-10-15 09:34:44 +08:00
|
|
|
import { AssetBrowser } from './components/AssetBrowser';
|
2025-10-15 17:21:59 +08:00
|
|
|
import { ConsolePanel } from './components/ConsolePanel';
|
2025-10-15 20:10:52 +08:00
|
|
|
import { PluginManagerWindow } from './components/PluginManagerWindow';
|
2025-10-15 17:28:45 +08:00
|
|
|
import { Viewport } from './components/Viewport';
|
2025-10-15 18:24:13 +08:00
|
|
|
import { MenuBar } from './components/MenuBar';
|
2025-10-15 09:58:45 +08:00
|
|
|
import { DockContainer, DockablePanel } from './components/DockContainer';
|
2025-10-14 22:53:26 +08:00
|
|
|
import { TauriAPI } from './api/tauri';
|
2025-10-14 23:56:54 +08:00
|
|
|
import { useLocale } from './hooks/useLocale';
|
|
|
|
|
import { en, zh } from './locales';
|
2025-10-15 18:24:13 +08:00
|
|
|
import { Loader2, Globe } from 'lucide-react';
|
2025-10-14 22:53:26 +08:00
|
|
|
import './styles/App.css';
|
|
|
|
|
|
2025-10-15 00:15:12 +08:00
|
|
|
const coreInstance = Core.create({ debug: true });
|
2025-10-14 23:56:54 +08:00
|
|
|
|
|
|
|
|
const localeService = new LocaleService();
|
|
|
|
|
localeService.registerTranslations('en', en);
|
|
|
|
|
localeService.registerTranslations('zh', zh);
|
|
|
|
|
Core.services.registerInstance(LocaleService, localeService);
|
|
|
|
|
|
2025-10-14 22:53:26 +08:00
|
|
|
function App() {
|
2025-10-14 23:31:09 +08:00
|
|
|
const [initialized, setInitialized] = useState(false);
|
2025-10-15 09:34:44 +08:00
|
|
|
const [projectLoaded, setProjectLoaded] = useState(false);
|
2025-10-15 18:29:48 +08:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
const [loadingMessage, setLoadingMessage] = useState('');
|
2025-10-15 09:34:44 +08:00
|
|
|
const [currentProjectPath, setCurrentProjectPath] = useState<string | null>(null);
|
2025-10-14 22:53:26 +08:00
|
|
|
const [pluginManager, setPluginManager] = useState<EditorPluginManager | null>(null);
|
2025-10-14 23:31:09 +08:00
|
|
|
const [entityStore, setEntityStore] = useState<EntityStoreService | null>(null);
|
|
|
|
|
const [messageHub, setMessageHub] = useState<MessageHub | null>(null);
|
2025-10-15 17:21:59 +08:00
|
|
|
const [logService, setLogService] = useState<LogService | null>(null);
|
2025-10-14 23:56:54 +08:00
|
|
|
const { t, locale, changeLocale } = useLocale();
|
|
|
|
|
const [status, setStatus] = useState(t('header.status.initializing'));
|
2025-10-15 09:58:45 +08:00
|
|
|
const [panels, setPanels] = useState<DockablePanel[]>([]);
|
2025-10-15 20:10:52 +08:00
|
|
|
const [showPluginManager, setShowPluginManager] = useState(false);
|
2025-10-14 22:53:26 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const initializeEditor = async () => {
|
|
|
|
|
try {
|
2025-10-15 17:15:05 +08:00
|
|
|
(window as any).__ECS_FRAMEWORK__ = await import('@esengine/ecs-framework');
|
|
|
|
|
|
2025-10-14 23:31:09 +08:00
|
|
|
const editorScene = new Scene();
|
|
|
|
|
Core.setScene(editorScene);
|
|
|
|
|
|
2025-10-14 22:53:26 +08:00
|
|
|
const uiRegistry = new UIRegistry();
|
|
|
|
|
const messageHub = new MessageHub();
|
|
|
|
|
const serializerRegistry = new SerializerRegistry();
|
2025-10-14 23:31:09 +08:00
|
|
|
const entityStore = new EntityStoreService(messageHub);
|
2025-10-14 23:42:06 +08:00
|
|
|
const componentRegistry = new ComponentRegistry();
|
2025-10-15 00:23:19 +08:00
|
|
|
const projectService = new ProjectService(messageHub);
|
2025-10-15 00:40:27 +08:00
|
|
|
const componentDiscovery = new ComponentDiscoveryService(messageHub);
|
|
|
|
|
const componentLoader = new ComponentLoaderService(messageHub, componentRegistry);
|
2025-10-15 17:15:05 +08:00
|
|
|
const propertyMetadata = new PropertyMetadataService();
|
2025-10-15 17:21:59 +08:00
|
|
|
const logService = new LogService();
|
2025-10-14 22:53:26 +08:00
|
|
|
|
|
|
|
|
Core.services.registerInstance(UIRegistry, uiRegistry);
|
|
|
|
|
Core.services.registerInstance(MessageHub, messageHub);
|
|
|
|
|
Core.services.registerInstance(SerializerRegistry, serializerRegistry);
|
2025-10-14 23:31:09 +08:00
|
|
|
Core.services.registerInstance(EntityStoreService, entityStore);
|
2025-10-14 23:42:06 +08:00
|
|
|
Core.services.registerInstance(ComponentRegistry, componentRegistry);
|
2025-10-15 00:23:19 +08:00
|
|
|
Core.services.registerInstance(ProjectService, projectService);
|
2025-10-15 00:40:27 +08:00
|
|
|
Core.services.registerInstance(ComponentDiscoveryService, componentDiscovery);
|
|
|
|
|
Core.services.registerInstance(ComponentLoaderService, componentLoader);
|
2025-10-15 17:15:05 +08:00
|
|
|
Core.services.registerInstance(PropertyMetadataService, propertyMetadata);
|
2025-10-15 17:21:59 +08:00
|
|
|
Core.services.registerInstance(LogService, logService);
|
2025-10-14 22:53:26 +08:00
|
|
|
|
|
|
|
|
const pluginMgr = new EditorPluginManager();
|
2025-10-15 00:15:12 +08:00
|
|
|
pluginMgr.initialize(coreInstance, Core.services);
|
2025-10-14 22:53:26 +08:00
|
|
|
|
|
|
|
|
await pluginMgr.installEditor(new SceneInspectorPlugin());
|
|
|
|
|
|
|
|
|
|
const greeting = await TauriAPI.greet('Developer');
|
|
|
|
|
console.log(greeting);
|
|
|
|
|
|
2025-10-14 23:31:09 +08:00
|
|
|
setInitialized(true);
|
2025-10-14 22:53:26 +08:00
|
|
|
setPluginManager(pluginMgr);
|
2025-10-14 23:31:09 +08:00
|
|
|
setEntityStore(entityStore);
|
|
|
|
|
setMessageHub(messageHub);
|
2025-10-15 17:21:59 +08:00
|
|
|
setLogService(logService);
|
2025-10-14 23:56:54 +08:00
|
|
|
setStatus(t('header.status.ready'));
|
2025-10-14 22:53:26 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to initialize editor:', error);
|
2025-10-14 23:56:54 +08:00
|
|
|
setStatus(t('header.status.failed'));
|
2025-10-14 22:53:26 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
initializeEditor();
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-10-15 00:23:19 +08:00
|
|
|
const handleOpenProject = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const projectPath = await TauriAPI.openProjectDialog();
|
2025-10-15 00:40:27 +08:00
|
|
|
if (!projectPath) return;
|
|
|
|
|
|
2025-10-15 18:29:48 +08:00
|
|
|
setIsLoading(true);
|
|
|
|
|
setLoadingMessage(locale === 'zh' ? '正在打开项目...' : 'Opening project...');
|
|
|
|
|
|
2025-10-15 00:40:27 +08:00
|
|
|
const projectService = Core.services.resolve(ProjectService);
|
|
|
|
|
const discoveryService = Core.services.resolve(ComponentDiscoveryService);
|
|
|
|
|
const loaderService = Core.services.resolve(ComponentLoaderService);
|
|
|
|
|
|
|
|
|
|
if (!projectService || !discoveryService || !loaderService) {
|
|
|
|
|
console.error('Required services not available');
|
2025-10-15 18:29:48 +08:00
|
|
|
setIsLoading(false);
|
2025-10-15 00:40:27 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await projectService.openProject(projectPath);
|
2025-10-15 17:15:05 +08:00
|
|
|
|
|
|
|
|
await fetch('/@user-project-set-path', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ path: projectPath })
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-15 18:29:48 +08:00
|
|
|
setLoadingMessage(locale === 'zh' ? '正在扫描组件...' : 'Scanning components...');
|
2025-10-15 00:40:27 +08:00
|
|
|
setStatus('Scanning components...');
|
|
|
|
|
|
|
|
|
|
const componentsPath = projectService.getComponentsPath();
|
|
|
|
|
if (componentsPath) {
|
|
|
|
|
const componentInfos = await discoveryService.scanComponents({
|
|
|
|
|
basePath: componentsPath,
|
2025-10-15 09:19:30 +08:00
|
|
|
pattern: '**/*.ts',
|
2025-10-15 00:40:27 +08:00
|
|
|
scanFunction: TauriAPI.scanDirectory,
|
|
|
|
|
readFunction: TauriAPI.readFileContent
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-15 18:29:48 +08:00
|
|
|
setLoadingMessage(locale === 'zh' ? `正在加载 ${componentInfos.length} 个组件...` : `Loading ${componentInfos.length} components...`);
|
2025-10-15 00:40:27 +08:00
|
|
|
setStatus(`Loading ${componentInfos.length} components...`);
|
|
|
|
|
|
2025-10-15 17:15:05 +08:00
|
|
|
const modulePathTransform = (filePath: string) => {
|
|
|
|
|
const relativePath = filePath.replace(projectPath, '').replace(/\\/g, '/');
|
|
|
|
|
return `/@user-project${relativePath}`;
|
|
|
|
|
};
|
2025-10-15 00:40:27 +08:00
|
|
|
|
2025-10-15 17:15:05 +08:00
|
|
|
await loaderService.loadComponents(componentInfos, modulePathTransform);
|
|
|
|
|
|
|
|
|
|
setStatus(t('header.status.projectOpened') + ` (${componentInfos.length} components registered)`);
|
2025-10-15 00:40:27 +08:00
|
|
|
} else {
|
|
|
|
|
setStatus(t('header.status.projectOpened'));
|
2025-10-15 00:23:19 +08:00
|
|
|
}
|
2025-10-15 09:34:44 +08:00
|
|
|
|
|
|
|
|
setCurrentProjectPath(projectPath);
|
|
|
|
|
setProjectLoaded(true);
|
2025-10-15 18:29:48 +08:00
|
|
|
setIsLoading(false);
|
2025-10-15 00:23:19 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to open project:', error);
|
|
|
|
|
setStatus(t('header.status.failed'));
|
2025-10-15 18:29:48 +08:00
|
|
|
setIsLoading(false);
|
2025-10-15 00:23:19 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-15 09:34:44 +08:00
|
|
|
const handleCreateProject = async () => {
|
|
|
|
|
console.log('Create project not implemented yet');
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-15 18:24:13 +08:00
|
|
|
const handleNewScene = () => {
|
|
|
|
|
console.log('New scene not implemented yet');
|
|
|
|
|
};
|
2025-10-15 09:34:44 +08:00
|
|
|
|
2025-10-15 18:24:13 +08:00
|
|
|
const handleOpenScene = () => {
|
|
|
|
|
console.log('Open scene not implemented yet');
|
2025-10-15 09:34:44 +08:00
|
|
|
};
|
|
|
|
|
|
2025-10-15 18:24:13 +08:00
|
|
|
const handleSaveScene = () => {
|
|
|
|
|
console.log('Save scene not implemented yet');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSaveSceneAs = () => {
|
|
|
|
|
console.log('Save scene as not implemented yet');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCloseProject = () => {
|
|
|
|
|
setProjectLoaded(false);
|
|
|
|
|
setCurrentProjectPath(null);
|
|
|
|
|
setStatus(t('header.status.ready'));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleExit = () => {
|
|
|
|
|
window.close();
|
2025-10-15 09:34:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleLocaleChange = () => {
|
|
|
|
|
const newLocale = locale === 'en' ? 'zh' : 'en';
|
|
|
|
|
changeLocale(newLocale);
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-15 09:58:45 +08:00
|
|
|
useEffect(() => {
|
2025-10-15 17:21:59 +08:00
|
|
|
if (projectLoaded && entityStore && messageHub && logService) {
|
2025-10-15 17:15:05 +08:00
|
|
|
setPanels([
|
2025-10-15 09:58:45 +08:00
|
|
|
{
|
|
|
|
|
id: 'scene-hierarchy',
|
|
|
|
|
title: locale === 'zh' ? '场景层级' : 'Scene Hierarchy',
|
|
|
|
|
position: 'left',
|
|
|
|
|
content: <SceneHierarchy entityStore={entityStore} messageHub={messageHub} />,
|
|
|
|
|
closable: false
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'inspector',
|
|
|
|
|
title: locale === 'zh' ? '检视器' : 'Inspector',
|
|
|
|
|
position: 'right',
|
|
|
|
|
content: <EntityInspector entityStore={entityStore} messageHub={messageHub} />,
|
|
|
|
|
closable: false
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'viewport',
|
|
|
|
|
title: locale === 'zh' ? '视口' : 'Viewport',
|
|
|
|
|
position: 'center',
|
2025-10-15 17:28:45 +08:00
|
|
|
content: <Viewport locale={locale} />,
|
2025-10-15 09:58:45 +08:00
|
|
|
closable: false
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'assets',
|
|
|
|
|
title: locale === 'zh' ? '资产' : 'Assets',
|
|
|
|
|
position: 'bottom',
|
|
|
|
|
content: <AssetBrowser projectPath={currentProjectPath} locale={locale} />,
|
|
|
|
|
closable: false
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'console',
|
|
|
|
|
title: locale === 'zh' ? '控制台' : 'Console',
|
|
|
|
|
position: 'bottom',
|
2025-10-15 17:21:59 +08:00
|
|
|
content: <ConsolePanel logService={logService} />,
|
2025-10-15 09:58:45 +08:00
|
|
|
closable: false
|
|
|
|
|
}
|
2025-10-15 17:15:05 +08:00
|
|
|
]);
|
2025-10-15 09:58:45 +08:00
|
|
|
}
|
2025-10-15 17:21:59 +08:00
|
|
|
}, [projectLoaded, entityStore, messageHub, logService, locale, currentProjectPath, t]);
|
2025-10-15 09:58:45 +08:00
|
|
|
|
2025-10-15 17:15:05 +08:00
|
|
|
const handlePanelMove = (panelId: string, newPosition: any) => {
|
|
|
|
|
setPanels(prevPanels =>
|
|
|
|
|
prevPanels.map(panel =>
|
|
|
|
|
panel.id === panelId ? { ...panel, position: newPosition } : panel
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-15 09:34:44 +08:00
|
|
|
if (!initialized) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="editor-loading">
|
2025-10-15 17:15:05 +08:00
|
|
|
<Loader2 size={32} className="animate-spin" />
|
2025-10-15 09:34:44 +08:00
|
|
|
<h2>Loading Editor...</h2>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!projectLoaded) {
|
|
|
|
|
return (
|
2025-10-15 18:29:48 +08:00
|
|
|
<>
|
|
|
|
|
<StartupPage
|
|
|
|
|
onOpenProject={handleOpenProject}
|
|
|
|
|
onCreateProject={handleCreateProject}
|
|
|
|
|
recentProjects={[]}
|
|
|
|
|
locale={locale}
|
|
|
|
|
/>
|
|
|
|
|
{isLoading && (
|
|
|
|
|
<div className="loading-overlay">
|
|
|
|
|
<div className="loading-content">
|
|
|
|
|
<Loader2 size={40} className="animate-spin" />
|
|
|
|
|
<p className="loading-message">{loadingMessage}</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2025-10-15 09:34:44 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-14 22:53:26 +08:00
|
|
|
return (
|
|
|
|
|
<div className="editor-container">
|
|
|
|
|
<div className="editor-header">
|
2025-10-15 18:24:13 +08:00
|
|
|
<MenuBar
|
|
|
|
|
locale={locale}
|
|
|
|
|
onNewScene={handleNewScene}
|
|
|
|
|
onOpenScene={handleOpenScene}
|
|
|
|
|
onSaveScene={handleSaveScene}
|
|
|
|
|
onSaveSceneAs={handleSaveSceneAs}
|
|
|
|
|
onOpenProject={handleOpenProject}
|
|
|
|
|
onCloseProject={handleCloseProject}
|
|
|
|
|
onExit={handleExit}
|
2025-10-15 20:10:52 +08:00
|
|
|
onOpenPluginManager={() => setShowPluginManager(true)}
|
2025-10-15 18:24:13 +08:00
|
|
|
/>
|
|
|
|
|
<div className="header-right">
|
2025-10-14 23:56:54 +08:00
|
|
|
<button onClick={handleLocaleChange} className="toolbar-btn locale-btn" title={locale === 'en' ? '切换到中文' : 'Switch to English'}>
|
2025-10-15 17:15:05 +08:00
|
|
|
<Globe size={14} />
|
2025-10-14 23:31:09 +08:00
|
|
|
</button>
|
2025-10-15 18:24:13 +08:00
|
|
|
<span className="status">{status}</span>
|
2025-10-14 23:31:09 +08:00
|
|
|
</div>
|
2025-10-14 22:53:26 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="editor-content">
|
2025-10-15 17:15:05 +08:00
|
|
|
<DockContainer panels={panels} onPanelMove={handlePanelMove} />
|
2025-10-14 22:53:26 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="editor-footer">
|
2025-10-14 23:56:54 +08:00
|
|
|
<span>{t('footer.plugins')}: {pluginManager?.getAllEditorPlugins().length ?? 0}</span>
|
|
|
|
|
<span>{t('footer.entities')}: {entityStore?.getAllEntities().length ?? 0}</span>
|
|
|
|
|
<span>{t('footer.core')}: {initialized ? t('footer.active') : t('footer.inactive')}</span>
|
2025-10-14 22:53:26 +08:00
|
|
|
</div>
|
2025-10-15 20:10:52 +08:00
|
|
|
|
|
|
|
|
{showPluginManager && pluginManager && (
|
|
|
|
|
<PluginManagerWindow
|
|
|
|
|
pluginManager={pluginManager}
|
|
|
|
|
onClose={() => setShowPluginManager(false)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-10-14 22:53:26 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|