2025-10-14 23:31:09 +08:00
|
|
|
import { useState, useEffect } from 'react';
|
2025-10-17 18:13:31 +08:00
|
|
|
import { Entity, Core } from '@esengine/ecs-framework';
|
2025-11-25 22:23:19 +08:00
|
|
|
import { EntityStoreService, MessageHub, SceneManagerService, CommandManager, EntityCreationRegistry, EntityCreationTemplate } from '@esengine/editor-core';
|
2025-10-14 23:56:54 +08:00
|
|
|
import { useLocale } from '../hooks/useLocale';
|
2025-11-23 14:49:37 +08:00
|
|
|
import { Box, Layers, Wifi, Search, Plus, Trash2, Monitor, Globe, Image, Camera, Film } from 'lucide-react';
|
2025-10-15 23:24:13 +08:00
|
|
|
import { ProfilerService, RemoteEntity } from '../services/ProfilerService';
|
2025-10-17 18:13:31 +08:00
|
|
|
import { confirm } from '@tauri-apps/plugin-dialog';
|
2025-11-23 14:49:37 +08:00
|
|
|
import { CreateEntityCommand, CreateSpriteEntityCommand, CreateAnimatedSpriteEntityCommand, CreateCameraEntityCommand, DeleteEntityCommand } from '../application/commands/entity';
|
2025-10-14 23:31:09 +08:00
|
|
|
import '../styles/SceneHierarchy.css';
|
|
|
|
|
|
2025-11-21 10:03:18 +08:00
|
|
|
type ViewMode = 'local' | 'remote';
|
|
|
|
|
|
2025-10-14 23:31:09 +08:00
|
|
|
interface SceneHierarchyProps {
|
|
|
|
|
entityStore: EntityStoreService;
|
|
|
|
|
messageHub: MessageHub;
|
2025-11-21 10:03:18 +08:00
|
|
|
commandManager: CommandManager;
|
2025-11-23 21:45:10 +08:00
|
|
|
isProfilerMode?: boolean;
|
2025-10-14 23:31:09 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-23 21:45:10 +08:00
|
|
|
export function SceneHierarchy({ entityStore, messageHub, commandManager, isProfilerMode = false }: SceneHierarchyProps) {
|
2025-11-02 23:50:41 +08:00
|
|
|
const [entities, setEntities] = useState<Entity[]>([]);
|
|
|
|
|
const [remoteEntities, setRemoteEntities] = useState<RemoteEntity[]>([]);
|
|
|
|
|
const [isRemoteConnected, setIsRemoteConnected] = useState(false);
|
2025-11-23 21:45:10 +08:00
|
|
|
const [viewMode, setViewMode] = useState<ViewMode>(isProfilerMode ? 'remote' : 'local');
|
2025-11-02 23:50:41 +08:00
|
|
|
const [selectedId, setSelectedId] = useState<number | null>(null);
|
|
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
|
|
|
const [sceneName, setSceneName] = useState<string>('Untitled');
|
|
|
|
|
const [remoteSceneName, setRemoteSceneName] = useState<string | null>(null);
|
|
|
|
|
const [sceneFilePath, setSceneFilePath] = useState<string | null>(null);
|
|
|
|
|
const [isSceneModified, setIsSceneModified] = useState<boolean>(false);
|
2025-11-21 10:03:18 +08:00
|
|
|
const [contextMenu, setContextMenu] = useState<{ x: number; y: number; entityId: number | null } | null>(null);
|
2025-11-23 21:45:10 +08:00
|
|
|
const [draggedEntityId, setDraggedEntityId] = useState<number | null>(null);
|
|
|
|
|
const [dropTargetIndex, setDropTargetIndex] = useState<number | null>(null);
|
2025-11-25 22:23:19 +08:00
|
|
|
const [pluginTemplates, setPluginTemplates] = useState<EntityCreationTemplate[]>([]);
|
2025-11-02 23:50:41 +08:00
|
|
|
const { t, locale } = useLocale();
|
|
|
|
|
|
2025-11-21 10:03:18 +08:00
|
|
|
const isShowingRemote = viewMode === 'remote' && isRemoteConnected;
|
|
|
|
|
|
2025-11-25 22:23:19 +08:00
|
|
|
// Get entity creation templates from plugins
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const updateTemplates = () => {
|
|
|
|
|
const registry = Core.services.resolve(EntityCreationRegistry);
|
|
|
|
|
if (registry) {
|
|
|
|
|
setPluginTemplates(registry.getAll());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateTemplates();
|
|
|
|
|
|
|
|
|
|
// Update when plugins are installed
|
|
|
|
|
const unsubInstalled = messageHub.subscribe('plugin:installed', updateTemplates);
|
|
|
|
|
const unsubUninstalled = messageHub.subscribe('plugin:uninstalled', updateTemplates);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
unsubInstalled();
|
|
|
|
|
unsubUninstalled();
|
|
|
|
|
};
|
|
|
|
|
}, [messageHub]);
|
|
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
// Subscribe to scene changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const sceneManager = Core.services.resolve(SceneManagerService);
|
|
|
|
|
|
|
|
|
|
const updateSceneInfo = () => {
|
|
|
|
|
if (sceneManager) {
|
|
|
|
|
const state = sceneManager.getSceneState();
|
|
|
|
|
setSceneName(state.sceneName);
|
|
|
|
|
setIsSceneModified(state.isModified);
|
2025-11-23 21:45:10 +08:00
|
|
|
setSceneFilePath(state.currentScenePath || null);
|
2025-11-02 23:50:41 +08:00
|
|
|
}
|
|
|
|
|
};
|
2025-10-17 18:13:31 +08:00
|
|
|
|
|
|
|
|
updateSceneInfo();
|
2025-10-14 23:31:09 +08:00
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
const unsubLoaded = messageHub.subscribe('scene:loaded', (data: any) => {
|
|
|
|
|
if (data.sceneName) {
|
|
|
|
|
setSceneName(data.sceneName);
|
|
|
|
|
setSceneFilePath(data.path || null);
|
|
|
|
|
setIsSceneModified(data.isModified || false);
|
|
|
|
|
} else {
|
|
|
|
|
updateSceneInfo();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const unsubNew = messageHub.subscribe('scene:new', () => {
|
|
|
|
|
updateSceneInfo();
|
|
|
|
|
});
|
|
|
|
|
const unsubSaved = messageHub.subscribe('scene:saved', () => {
|
|
|
|
|
updateSceneInfo();
|
|
|
|
|
});
|
2025-11-23 21:45:10 +08:00
|
|
|
const unsubModified = messageHub.subscribe('scene:modified', updateSceneInfo);
|
2025-10-14 23:31:09 +08:00
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
return () => {
|
|
|
|
|
unsubLoaded();
|
|
|
|
|
unsubNew();
|
|
|
|
|
unsubSaved();
|
|
|
|
|
unsubModified();
|
|
|
|
|
};
|
|
|
|
|
}, [messageHub]);
|
|
|
|
|
|
|
|
|
|
// Subscribe to local entity changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const updateEntities = () => {
|
2025-11-23 21:45:10 +08:00
|
|
|
setEntities([...entityStore.getRootEntities()]);
|
2025-11-02 23:50:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSelection = (data: { entity: Entity | null }) => {
|
|
|
|
|
setSelectedId(data.entity?.id ?? null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateEntities();
|
|
|
|
|
|
|
|
|
|
const unsubAdd = messageHub.subscribe('entity:added', updateEntities);
|
|
|
|
|
const unsubRemove = messageHub.subscribe('entity:removed', updateEntities);
|
|
|
|
|
const unsubClear = messageHub.subscribe('entities:cleared', updateEntities);
|
|
|
|
|
const unsubSelect = messageHub.subscribe('entity:selected', handleSelection);
|
2025-11-23 21:45:10 +08:00
|
|
|
const unsubSceneLoaded = messageHub.subscribe('scene:loaded', updateEntities);
|
|
|
|
|
const unsubSceneNew = messageHub.subscribe('scene:new', updateEntities);
|
|
|
|
|
const unsubReordered = messageHub.subscribe('entity:reordered', updateEntities);
|
2025-11-02 23:50:41 +08:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
unsubAdd();
|
|
|
|
|
unsubRemove();
|
|
|
|
|
unsubClear();
|
|
|
|
|
unsubSelect();
|
2025-11-23 21:45:10 +08:00
|
|
|
unsubSceneLoaded();
|
|
|
|
|
unsubSceneNew();
|
|
|
|
|
unsubReordered();
|
2025-11-02 23:50:41 +08:00
|
|
|
};
|
|
|
|
|
}, [entityStore, messageHub]);
|
|
|
|
|
|
|
|
|
|
// Subscribe to remote entity data from ProfilerService
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const profilerService = (window as any).__PROFILER_SERVICE__ as ProfilerService | undefined;
|
|
|
|
|
|
|
|
|
|
if (!profilerService) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-10-14 23:31:09 +08:00
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
const initiallyConnected = profilerService.isConnected();
|
|
|
|
|
setIsRemoteConnected(initiallyConnected);
|
2025-10-14 23:31:09 +08:00
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
const unsubscribe = profilerService.subscribe((data) => {
|
|
|
|
|
const connected = profilerService.isConnected();
|
|
|
|
|
setIsRemoteConnected(connected);
|
2025-10-14 23:31:09 +08:00
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
if (connected && data.entities && data.entities.length > 0) {
|
|
|
|
|
// 只在实体列表发生实质性变化时才更新
|
|
|
|
|
setRemoteEntities((prev) => {
|
|
|
|
|
if (prev.length !== data.entities!.length) {
|
|
|
|
|
return data.entities!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查实体ID和名称是否变化
|
|
|
|
|
const hasChanged = data.entities!.some((entity, index) => {
|
|
|
|
|
const prevEntity = prev[index];
|
|
|
|
|
return !prevEntity ||
|
2025-10-16 17:10:22 +08:00
|
|
|
prevEntity.id !== entity.id ||
|
|
|
|
|
prevEntity.name !== entity.name ||
|
|
|
|
|
prevEntity.componentCount !== entity.componentCount;
|
2025-11-02 23:50:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return hasChanged ? data.entities! : prev;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 请求第一个实体的详情以获取场景名称
|
|
|
|
|
if (!remoteSceneName && data.entities.length > 0 && data.entities[0]) {
|
|
|
|
|
profilerService.requestEntityDetails(data.entities[0].id);
|
|
|
|
|
}
|
|
|
|
|
} else if (!connected) {
|
|
|
|
|
setRemoteEntities([]);
|
|
|
|
|
setRemoteSceneName(null);
|
|
|
|
|
}
|
2025-10-16 17:10:22 +08:00
|
|
|
});
|
2025-10-18 20:21:43 +08:00
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
return () => unsubscribe();
|
|
|
|
|
}, [remoteSceneName]);
|
|
|
|
|
|
|
|
|
|
// Listen for entity details to get remote scene name
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleEntityDetails = ((event: CustomEvent) => {
|
|
|
|
|
const details = event.detail;
|
|
|
|
|
if (details && details.sceneName) {
|
|
|
|
|
setRemoteSceneName(details.sceneName);
|
|
|
|
|
}
|
|
|
|
|
}) as EventListener;
|
|
|
|
|
|
|
|
|
|
window.addEventListener('profiler:entity-details', handleEntityDetails);
|
|
|
|
|
return () => window.removeEventListener('profiler:entity-details', handleEntityDetails);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleEntityClick = (entity: Entity) => {
|
|
|
|
|
entityStore.selectEntity(entity);
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-23 21:45:10 +08:00
|
|
|
const handleDragStart = (e: React.DragEvent, entityId: number) => {
|
|
|
|
|
setDraggedEntityId(entityId);
|
|
|
|
|
e.dataTransfer.effectAllowed = 'move';
|
|
|
|
|
e.dataTransfer.setData('text/plain', entityId.toString());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDragOver = (e: React.DragEvent, index: number) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.dataTransfer.dropEffect = 'move';
|
|
|
|
|
setDropTargetIndex(index);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDragLeave = () => {
|
|
|
|
|
setDropTargetIndex(null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDrop = (e: React.DragEvent, targetIndex: number) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
if (draggedEntityId !== null) {
|
|
|
|
|
entityStore.reorderEntity(draggedEntityId, targetIndex);
|
|
|
|
|
}
|
|
|
|
|
setDraggedEntityId(null);
|
|
|
|
|
setDropTargetIndex(null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDragEnd = () => {
|
|
|
|
|
setDraggedEntityId(null);
|
|
|
|
|
setDropTargetIndex(null);
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
const handleRemoteEntityClick = (entity: RemoteEntity) => {
|
|
|
|
|
setSelectedId(entity.id);
|
|
|
|
|
|
|
|
|
|
// 请求完整的实体详情(包含组件属性)
|
|
|
|
|
const profilerService = (window as any).__PROFILER_SERVICE__ as ProfilerService | undefined;
|
|
|
|
|
if (profilerService) {
|
|
|
|
|
profilerService.requestEntityDetails(entity.id);
|
2025-10-18 20:21:43 +08:00
|
|
|
}
|
2025-10-17 18:13:31 +08:00
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
// 先发布基本信息,详细信息稍后通过 ProfilerService 异步返回
|
|
|
|
|
messageHub.publish('remote-entity:selected', {
|
|
|
|
|
entity: {
|
|
|
|
|
id: entity.id,
|
|
|
|
|
name: entity.name,
|
|
|
|
|
enabled: entity.enabled,
|
|
|
|
|
componentCount: entity.componentCount,
|
|
|
|
|
componentTypes: entity.componentTypes
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSceneNameClick = () => {
|
|
|
|
|
if (sceneFilePath) {
|
|
|
|
|
messageHub.publish('asset:reveal', { path: sceneFilePath });
|
|
|
|
|
}
|
2025-10-17 18:13:31 +08:00
|
|
|
};
|
|
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
const handleCreateEntity = () => {
|
|
|
|
|
const entityCount = entityStore.getAllEntities().length;
|
|
|
|
|
const entityName = `Entity ${entityCount + 1}`;
|
2025-11-21 10:03:18 +08:00
|
|
|
|
|
|
|
|
const command = new CreateEntityCommand(
|
|
|
|
|
entityStore,
|
|
|
|
|
messageHub,
|
|
|
|
|
entityName
|
|
|
|
|
);
|
|
|
|
|
commandManager.execute(command);
|
2025-11-02 23:50:41 +08:00
|
|
|
};
|
2025-10-16 17:33:43 +08:00
|
|
|
|
2025-11-23 14:49:37 +08:00
|
|
|
const handleCreateSpriteEntity = () => {
|
|
|
|
|
// Count only Sprite entities for naming
|
|
|
|
|
const spriteCount = entityStore.getAllEntities().filter((e) => e.name.startsWith('Sprite ')).length;
|
|
|
|
|
const entityName = `Sprite ${spriteCount + 1}`;
|
|
|
|
|
|
|
|
|
|
const command = new CreateSpriteEntityCommand(
|
|
|
|
|
entityStore,
|
|
|
|
|
messageHub,
|
|
|
|
|
entityName
|
|
|
|
|
);
|
|
|
|
|
commandManager.execute(command);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCreateAnimatedSpriteEntity = () => {
|
|
|
|
|
const animCount = entityStore.getAllEntities().filter((e) => e.name.startsWith('AnimatedSprite ')).length;
|
|
|
|
|
const entityName = `AnimatedSprite ${animCount + 1}`;
|
|
|
|
|
|
|
|
|
|
const command = new CreateAnimatedSpriteEntityCommand(
|
|
|
|
|
entityStore,
|
|
|
|
|
messageHub,
|
|
|
|
|
entityName
|
|
|
|
|
);
|
|
|
|
|
commandManager.execute(command);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCreateCameraEntity = () => {
|
|
|
|
|
const entityCount = entityStore.getAllEntities().length;
|
|
|
|
|
const entityName = `Camera ${entityCount + 1}`;
|
|
|
|
|
|
|
|
|
|
const command = new CreateCameraEntityCommand(
|
|
|
|
|
entityStore,
|
|
|
|
|
messageHub,
|
|
|
|
|
entityName
|
|
|
|
|
);
|
|
|
|
|
commandManager.execute(command);
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
const handleDeleteEntity = async () => {
|
|
|
|
|
if (!selectedId) return;
|
2025-10-16 17:33:43 +08:00
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
const entity = entityStore.getEntity(selectedId);
|
|
|
|
|
if (!entity) return;
|
2025-10-16 17:33:43 +08:00
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
const confirmed = await confirm(
|
|
|
|
|
locale === 'zh'
|
2025-11-21 10:03:18 +08:00
|
|
|
? `确定要删除实体 "${entity.name}" 吗?`
|
|
|
|
|
: `Are you sure you want to delete entity "${entity.name}"?`,
|
2025-11-02 23:50:41 +08:00
|
|
|
{
|
|
|
|
|
title: locale === 'zh' ? '删除实体' : 'Delete Entity',
|
|
|
|
|
kind: 'warning'
|
|
|
|
|
}
|
2025-10-16 17:33:43 +08:00
|
|
|
);
|
2025-11-02 23:50:41 +08:00
|
|
|
|
|
|
|
|
if (confirmed) {
|
2025-11-21 10:03:18 +08:00
|
|
|
const command = new DeleteEntityCommand(
|
|
|
|
|
entityStore,
|
|
|
|
|
messageHub,
|
|
|
|
|
entity
|
|
|
|
|
);
|
|
|
|
|
commandManager.execute(command);
|
2025-11-02 23:50:41 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-21 10:03:18 +08:00
|
|
|
const handleContextMenu = (e: React.MouseEvent, entityId: number | null) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
setContextMenu({ x: e.clientX, y: e.clientY, entityId });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const closeContextMenu = () => {
|
|
|
|
|
setContextMenu(null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Close context menu on click outside
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleClick = () => closeContextMenu();
|
|
|
|
|
if (contextMenu) {
|
|
|
|
|
window.addEventListener('click', handleClick);
|
|
|
|
|
return () => window.removeEventListener('click', handleClick);
|
|
|
|
|
}
|
|
|
|
|
}, [contextMenu]);
|
|
|
|
|
|
2025-11-02 23:50:41 +08:00
|
|
|
// Listen for Delete key
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
2025-11-21 10:03:18 +08:00
|
|
|
if (e.key === 'Delete' && selectedId && !isShowingRemote) {
|
2025-11-02 23:50:41 +08:00
|
|
|
handleDeleteEntity();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
2025-11-21 10:03:18 +08:00
|
|
|
}, [selectedId, isShowingRemote]);
|
2025-11-02 23:50:41 +08:00
|
|
|
|
|
|
|
|
// Filter entities based on search query
|
|
|
|
|
const filterRemoteEntities = (entityList: RemoteEntity[]): RemoteEntity[] => {
|
|
|
|
|
if (!searchQuery.trim()) return entityList;
|
|
|
|
|
|
|
|
|
|
const query = searchQuery.toLowerCase();
|
|
|
|
|
return entityList.filter((entity) => {
|
|
|
|
|
const name = entity.name;
|
|
|
|
|
const id = entity.id.toString();
|
|
|
|
|
|
|
|
|
|
// Search by name or ID
|
|
|
|
|
if (name.toLowerCase().includes(query) || id.includes(query)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search by component types
|
|
|
|
|
if (Array.isArray(entity.componentTypes)) {
|
|
|
|
|
return entity.componentTypes.some((type) =>
|
|
|
|
|
type.toLowerCase().includes(query)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const filterLocalEntities = (entityList: Entity[]): Entity[] => {
|
|
|
|
|
if (!searchQuery.trim()) return entityList;
|
|
|
|
|
|
|
|
|
|
const query = searchQuery.toLowerCase();
|
|
|
|
|
return entityList.filter((entity) => {
|
|
|
|
|
const id = entity.id.toString();
|
|
|
|
|
return id.includes(query);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Determine which entities to display
|
2025-11-21 10:03:18 +08:00
|
|
|
const displayEntities = isShowingRemote
|
2025-11-02 23:50:41 +08:00
|
|
|
? filterRemoteEntities(remoteEntities)
|
|
|
|
|
: filterLocalEntities(entities);
|
2025-11-21 10:03:18 +08:00
|
|
|
const showRemoteIndicator = isShowingRemote && remoteEntities.length > 0;
|
|
|
|
|
const displaySceneName = isShowingRemote && remoteSceneName ? remoteSceneName : sceneName;
|
2025-11-02 23:50:41 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="scene-hierarchy">
|
|
|
|
|
<div className="hierarchy-header">
|
|
|
|
|
<Layers size={16} className="hierarchy-header-icon" />
|
|
|
|
|
<h3>{t('hierarchy.title')}</h3>
|
|
|
|
|
<div
|
2025-11-23 21:45:10 +08:00
|
|
|
className={[
|
|
|
|
|
'scene-name-container',
|
|
|
|
|
!isRemoteConnected && sceneFilePath && 'clickable',
|
|
|
|
|
isSceneModified && 'modified'
|
|
|
|
|
].filter(Boolean).join(' ')}
|
2025-11-02 23:50:41 +08:00
|
|
|
onClick={!isRemoteConnected ? handleSceneNameClick : undefined}
|
2025-11-23 21:45:10 +08:00
|
|
|
title={!isRemoteConnected && sceneFilePath
|
|
|
|
|
? `${displaySceneName}${isSceneModified ? (locale === 'zh' ? ' (未保存 - Ctrl+S 保存)' : ' (Unsaved - Ctrl+S to save)') : ''} - ${locale === 'zh' ? '点击跳转到文件' : 'Click to reveal file'}`
|
|
|
|
|
: displaySceneName}
|
2025-11-02 23:50:41 +08:00
|
|
|
>
|
|
|
|
|
<span className="scene-name">
|
2025-11-23 21:45:10 +08:00
|
|
|
{displaySceneName}
|
2025-11-02 23:50:41 +08:00
|
|
|
</span>
|
2025-11-23 21:45:10 +08:00
|
|
|
{!isRemoteConnected && isSceneModified && (
|
|
|
|
|
<span className="modified-indicator">●</span>
|
|
|
|
|
)}
|
2025-11-02 23:50:41 +08:00
|
|
|
</div>
|
2025-11-23 21:45:10 +08:00
|
|
|
{isRemoteConnected && !isProfilerMode && (
|
2025-11-21 10:03:18 +08:00
|
|
|
<div className="view-mode-toggle">
|
|
|
|
|
<button
|
|
|
|
|
className={`mode-btn ${viewMode === 'local' ? 'active' : ''}`}
|
|
|
|
|
onClick={() => setViewMode('local')}
|
|
|
|
|
title={locale === 'zh' ? '本地场景' : 'Local Scene'}
|
|
|
|
|
>
|
|
|
|
|
<Monitor size={14} />
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
className={`mode-btn ${viewMode === 'remote' ? 'active' : ''}`}
|
|
|
|
|
onClick={() => setViewMode('remote')}
|
|
|
|
|
title={locale === 'zh' ? '远程实体' : 'Remote Entities'}
|
|
|
|
|
>
|
|
|
|
|
<Globe size={14} />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-11-02 23:50:41 +08:00
|
|
|
{showRemoteIndicator && (
|
|
|
|
|
<div className="remote-indicator" title="Showing remote entities">
|
|
|
|
|
<Wifi size={12} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-10-15 23:24:13 +08:00
|
|
|
</div>
|
2025-11-02 23:50:41 +08:00
|
|
|
<div className="hierarchy-search">
|
|
|
|
|
<Search size={14} />
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
placeholder={t('hierarchy.search') || 'Search entities...'}
|
|
|
|
|
value={searchQuery}
|
|
|
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-11-21 10:03:18 +08:00
|
|
|
{!isShowingRemote && (
|
2025-11-02 23:50:41 +08:00
|
|
|
<div className="hierarchy-toolbar">
|
|
|
|
|
<button
|
2025-11-21 10:03:18 +08:00
|
|
|
className="toolbar-btn icon-only"
|
2025-11-02 23:50:41 +08:00
|
|
|
onClick={handleCreateEntity}
|
|
|
|
|
title={locale === 'zh' ? '创建实体' : 'Create Entity'}
|
|
|
|
|
>
|
|
|
|
|
<Plus size={14} />
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
2025-11-21 10:03:18 +08:00
|
|
|
className="toolbar-btn icon-only"
|
2025-11-02 23:50:41 +08:00
|
|
|
onClick={handleDeleteEntity}
|
|
|
|
|
disabled={!selectedId}
|
|
|
|
|
title={locale === 'zh' ? '删除实体' : 'Delete Entity'}
|
|
|
|
|
>
|
|
|
|
|
<Trash2 size={14} />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-11-21 10:03:18 +08:00
|
|
|
<div className="hierarchy-content scrollable" onContextMenu={(e) => !isShowingRemote && handleContextMenu(e, null)}>
|
2025-11-02 23:50:41 +08:00
|
|
|
{displayEntities.length === 0 ? (
|
|
|
|
|
<div className="empty-state">
|
|
|
|
|
<Box size={48} strokeWidth={1.5} className="empty-icon" />
|
|
|
|
|
<div className="empty-title">{t('hierarchy.empty')}</div>
|
|
|
|
|
<div className="empty-hint">
|
2025-11-21 10:03:18 +08:00
|
|
|
{isShowingRemote
|
2025-11-02 23:50:41 +08:00
|
|
|
? 'No entities in remote game'
|
|
|
|
|
: 'Create an entity to get started'}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-11-21 10:03:18 +08:00
|
|
|
) : isShowingRemote ? (
|
2025-11-02 23:50:41 +08:00
|
|
|
<ul className="entity-list">
|
|
|
|
|
{(displayEntities as RemoteEntity[]).map((entity) => (
|
|
|
|
|
<li
|
|
|
|
|
key={entity.id}
|
|
|
|
|
className={`entity-item remote-entity ${selectedId === entity.id ? 'selected' : ''} ${!entity.enabled ? 'disabled' : ''}`}
|
|
|
|
|
title={`${entity.name} - ${entity.componentTypes.join(', ')}`}
|
|
|
|
|
onClick={() => handleRemoteEntityClick(entity)}
|
|
|
|
|
>
|
|
|
|
|
<Box size={14} className="entity-icon" />
|
|
|
|
|
<span className="entity-name">{entity.name}</span>
|
|
|
|
|
{entity.tag !== 0 && (
|
|
|
|
|
<span className="entity-tag" title={`Tag: ${entity.tag}`}>
|
2025-10-16 17:33:43 +08:00
|
|
|
#{entity.tag}
|
2025-11-02 23:50:41 +08:00
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
{entity.componentCount > 0 && (
|
|
|
|
|
<span className="component-count">{entity.componentCount}</span>
|
|
|
|
|
)}
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
) : (
|
|
|
|
|
<ul className="entity-list">
|
2025-11-23 21:45:10 +08:00
|
|
|
{entities.map((entity, index) => (
|
2025-11-02 23:50:41 +08:00
|
|
|
<li
|
|
|
|
|
key={entity.id}
|
2025-11-23 21:45:10 +08:00
|
|
|
className={`entity-item ${selectedId === entity.id ? 'selected' : ''} ${draggedEntityId === entity.id ? 'dragging' : ''} ${dropTargetIndex === index ? 'drop-target' : ''}`}
|
|
|
|
|
draggable
|
2025-11-02 23:50:41 +08:00
|
|
|
onClick={() => handleEntityClick(entity)}
|
2025-11-23 21:45:10 +08:00
|
|
|
onDragStart={(e) => handleDragStart(e, entity.id)}
|
|
|
|
|
onDragOver={(e) => handleDragOver(e, index)}
|
|
|
|
|
onDragLeave={handleDragLeave}
|
|
|
|
|
onDrop={(e) => handleDrop(e, index)}
|
|
|
|
|
onDragEnd={handleDragEnd}
|
2025-11-21 10:03:18 +08:00
|
|
|
onContextMenu={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
handleEntityClick(entity);
|
|
|
|
|
handleContextMenu(e, entity.id);
|
|
|
|
|
}}
|
2025-11-02 23:50:41 +08:00
|
|
|
>
|
|
|
|
|
<Box size={14} className="entity-icon" />
|
2025-11-21 10:03:18 +08:00
|
|
|
<span className="entity-name">{entity.name || `Entity ${entity.id}`}</span>
|
2025-11-02 23:50:41 +08:00
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
2025-10-15 23:24:13 +08:00
|
|
|
)}
|
2025-11-02 23:50:41 +08:00
|
|
|
</div>
|
2025-11-21 10:03:18 +08:00
|
|
|
|
|
|
|
|
{contextMenu && !isShowingRemote && (
|
|
|
|
|
<div
|
|
|
|
|
className="context-menu"
|
|
|
|
|
style={{
|
|
|
|
|
position: 'fixed',
|
|
|
|
|
left: contextMenu.x,
|
|
|
|
|
top: contextMenu.y,
|
|
|
|
|
zIndex: 1000
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<button onClick={() => { handleCreateEntity(); closeContextMenu(); }}>
|
|
|
|
|
<Plus size={12} />
|
2025-11-23 14:49:37 +08:00
|
|
|
<span>{locale === 'zh' ? '创建空实体' : 'Create Empty Entity'}</span>
|
|
|
|
|
</button>
|
|
|
|
|
<button onClick={() => { handleCreateSpriteEntity(); closeContextMenu(); }}>
|
|
|
|
|
<Image size={12} />
|
|
|
|
|
<span>{locale === 'zh' ? '创建 Sprite' : 'Create Sprite'}</span>
|
|
|
|
|
</button>
|
|
|
|
|
<button onClick={() => { handleCreateAnimatedSpriteEntity(); closeContextMenu(); }}>
|
|
|
|
|
<Film size={12} />
|
|
|
|
|
<span>{locale === 'zh' ? '创建动画 Sprite' : 'Create Animated Sprite'}</span>
|
|
|
|
|
</button>
|
|
|
|
|
<button onClick={() => { handleCreateCameraEntity(); closeContextMenu(); }}>
|
|
|
|
|
<Camera size={12} />
|
|
|
|
|
<span>{locale === 'zh' ? '创建相机' : 'Create Camera'}</span>
|
2025-11-21 10:03:18 +08:00
|
|
|
</button>
|
2025-11-25 22:23:19 +08:00
|
|
|
{pluginTemplates.length > 0 && (
|
|
|
|
|
<>
|
|
|
|
|
<div className="context-menu-divider" />
|
|
|
|
|
{pluginTemplates.map((template) => (
|
|
|
|
|
<button
|
|
|
|
|
key={template.id}
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
await template.create(contextMenu.entityId ?? undefined);
|
|
|
|
|
closeContextMenu();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{template.icon || <Plus size={12} />}
|
|
|
|
|
<span>{template.label}</span>
|
|
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2025-11-21 10:03:18 +08:00
|
|
|
{contextMenu.entityId && (
|
|
|
|
|
<>
|
|
|
|
|
<div className="context-menu-divider" />
|
|
|
|
|
<button onClick={() => { handleDeleteEntity(); closeContextMenu(); }}>
|
|
|
|
|
<Trash2 size={12} />
|
|
|
|
|
<span>{locale === 'zh' ? '删除实体' : 'Delete Entity'}</span>
|
|
|
|
|
</button>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-11-02 23:50:41 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
2025-10-14 23:31:09 +08:00
|
|
|
}
|