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 00:15:12 +08:00
|
|
|
import { EditorPluginManager, UIRegistry, MessageHub, SerializerRegistry, EntityStoreService, ComponentRegistry, LocaleService, PropertyMetadataService } from '@esengine/editor-core';
|
2025-10-14 22:53:26 +08:00
|
|
|
import { SceneInspectorPlugin } from './plugins/SceneInspectorPlugin';
|
2025-10-14 23:31:09 +08:00
|
|
|
import { SceneHierarchy } from './components/SceneHierarchy';
|
|
|
|
|
import { EntityInspector } from './components/EntityInspector';
|
2025-10-14 22:53:26 +08:00
|
|
|
import { TauriAPI } from './api/tauri';
|
2025-10-14 23:42:06 +08:00
|
|
|
import { TransformComponent } from './example-components/TransformComponent';
|
|
|
|
|
import { SpriteComponent } from './example-components/SpriteComponent';
|
|
|
|
|
import { RigidBodyComponent } from './example-components/RigidBodyComponent';
|
2025-10-14 23:56:54 +08:00
|
|
|
import { useLocale } from './hooks/useLocale';
|
|
|
|
|
import { en, zh } from './locales';
|
2025-10-14 22:53:26 +08:00
|
|
|
import './styles/App.css';
|
|
|
|
|
|
2025-10-14 23:56:54 +08:00
|
|
|
// 在 App 组件外部初始化 Core 和基础服务
|
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-15 00:15:12 +08:00
|
|
|
const propertyMetadata = new PropertyMetadataService();
|
|
|
|
|
Core.services.registerInstance(PropertyMetadataService, propertyMetadata);
|
|
|
|
|
|
|
|
|
|
propertyMetadata.register(TransformComponent, {
|
|
|
|
|
properties: {
|
|
|
|
|
x: { type: 'number', label: 'X Position' },
|
|
|
|
|
y: { type: 'number', label: 'Y Position' },
|
|
|
|
|
rotation: { type: 'number', label: 'Rotation', min: 0, max: 360 },
|
|
|
|
|
scaleX: { type: 'number', label: 'Scale X', min: 0, step: 0.1 },
|
|
|
|
|
scaleY: { type: 'number', label: 'Scale Y', min: 0, step: 0.1 }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
propertyMetadata.register(SpriteComponent, {
|
|
|
|
|
properties: {
|
|
|
|
|
texturePath: { type: 'string', label: 'Texture Path' },
|
|
|
|
|
color: { type: 'color', label: 'Tint Color' },
|
|
|
|
|
visible: { type: 'boolean', label: 'Visible' }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
propertyMetadata.register(RigidBodyComponent, {
|
|
|
|
|
properties: {
|
|
|
|
|
mass: { type: 'number', label: 'Mass', min: 0, step: 0.1 },
|
|
|
|
|
friction: { type: 'number', label: 'Friction', min: 0, max: 1, step: 0.01 },
|
|
|
|
|
restitution: { type: 'number', label: 'Restitution', min: 0, max: 1, step: 0.01 },
|
|
|
|
|
isDynamic: { type: 'boolean', label: 'Dynamic' }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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-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-14 23:56:54 +08:00
|
|
|
const { t, locale, changeLocale } = useLocale();
|
|
|
|
|
const [status, setStatus] = useState(t('header.status.initializing'));
|
2025-10-14 22:53:26 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const initializeEditor = async () => {
|
|
|
|
|
try {
|
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();
|
|
|
|
|
|
|
|
|
|
componentRegistry.register({
|
|
|
|
|
name: 'Transform',
|
|
|
|
|
type: TransformComponent,
|
|
|
|
|
category: 'Transform',
|
|
|
|
|
description: 'Position, rotation and scale'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
componentRegistry.register({
|
|
|
|
|
name: 'Sprite',
|
|
|
|
|
type: SpriteComponent,
|
|
|
|
|
category: 'Rendering',
|
|
|
|
|
description: 'Sprite renderer'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
componentRegistry.register({
|
|
|
|
|
name: 'RigidBody',
|
|
|
|
|
type: RigidBodyComponent,
|
|
|
|
|
category: 'Physics',
|
|
|
|
|
description: 'Physics body'
|
|
|
|
|
});
|
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-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-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-14 23:31:09 +08:00
|
|
|
const handleCreateEntity = () => {
|
|
|
|
|
if (!initialized || !entityStore) return;
|
|
|
|
|
const scene = Core.scene;
|
|
|
|
|
if (!scene) return;
|
|
|
|
|
|
|
|
|
|
const entity = scene.createEntity('Entity');
|
|
|
|
|
entityStore.addEntity(entity);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDeleteEntity = () => {
|
|
|
|
|
if (!entityStore) return;
|
|
|
|
|
const selected = entityStore.getSelectedEntity();
|
|
|
|
|
if (selected) {
|
|
|
|
|
selected.destroy();
|
|
|
|
|
entityStore.removeEntity(selected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-14 23:56:54 +08:00
|
|
|
const handleLocaleChange = () => {
|
|
|
|
|
const newLocale = locale === 'en' ? 'zh' : 'en';
|
|
|
|
|
changeLocale(newLocale);
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-14 22:53:26 +08:00
|
|
|
return (
|
|
|
|
|
<div className="editor-container">
|
|
|
|
|
<div className="editor-header">
|
2025-10-14 23:56:54 +08:00
|
|
|
<h1>{t('app.title')}</h1>
|
2025-10-14 23:31:09 +08:00
|
|
|
<div className="header-toolbar">
|
|
|
|
|
<button onClick={handleCreateEntity} disabled={!initialized} className="toolbar-btn">
|
2025-10-14 23:56:54 +08:00
|
|
|
{t('header.toolbar.createEntity')}
|
2025-10-14 23:31:09 +08:00
|
|
|
</button>
|
|
|
|
|
<button onClick={handleDeleteEntity} disabled={!entityStore?.getSelectedEntity()} className="toolbar-btn">
|
2025-10-14 23:56:54 +08:00
|
|
|
{t('header.toolbar.deleteEntity')}
|
|
|
|
|
</button>
|
|
|
|
|
<button onClick={handleLocaleChange} className="toolbar-btn locale-btn" title={locale === 'en' ? '切换到中文' : 'Switch to English'}>
|
|
|
|
|
{locale === 'en' ? '中' : 'EN'}
|
2025-10-14 23:31:09 +08:00
|
|
|
</button>
|
|
|
|
|
</div>
|
2025-10-14 22:53:26 +08:00
|
|
|
<span className="status">{status}</span>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="editor-content">
|
|
|
|
|
<div className="sidebar-left">
|
2025-10-14 23:31:09 +08:00
|
|
|
{entityStore && messageHub ? (
|
|
|
|
|
<SceneHierarchy entityStore={entityStore} messageHub={messageHub} />
|
|
|
|
|
) : (
|
|
|
|
|
<div className="loading">Loading...</div>
|
|
|
|
|
)}
|
2025-10-14 22:53:26 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="main-content">
|
|
|
|
|
<div className="viewport">
|
2025-10-14 23:56:54 +08:00
|
|
|
<h3>{t('viewport.title')}</h3>
|
|
|
|
|
<p>{t('viewport.placeholder')}</p>
|
2025-10-14 22:53:26 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="bottom-panel">
|
2025-10-14 23:56:54 +08:00
|
|
|
<h4>{t('console.title')}</h4>
|
|
|
|
|
<p>{t('console.placeholder')}</p>
|
2025-10-14 22:53:26 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="sidebar-right">
|
2025-10-14 23:31:09 +08:00
|
|
|
{entityStore && messageHub ? (
|
|
|
|
|
<EntityInspector entityStore={entityStore} messageHub={messageHub} />
|
|
|
|
|
) : (
|
|
|
|
|
<div className="loading">Loading...</div>
|
|
|
|
|
)}
|
2025-10-14 22:53:26 +08:00
|
|
|
</div>
|
|
|
|
|
</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>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|