import { useState, useEffect } from 'react'; import { Entity, Core } from '@esengine/ecs-framework'; import { EntityStoreService, MessageHub, ComponentRegistry } from '@esengine/editor-core'; import { AddComponent } from './AddComponent'; import { PropertyInspector } from './PropertyInspector'; import '../styles/EntityInspector.css'; interface EntityInspectorProps { entityStore: EntityStoreService; messageHub: MessageHub; } export function EntityInspector({ entityStore, messageHub }: EntityInspectorProps) { const [selectedEntity, setSelectedEntity] = useState(null); const [showAddComponent, setShowAddComponent] = useState(false); const [expandedComponents, setExpandedComponents] = useState>(new Set()); useEffect(() => { const handleSelection = (data: { entity: Entity | null }) => { setSelectedEntity(data.entity); setShowAddComponent(false); }; const unsubSelect = messageHub.subscribe('entity:selected', handleSelection); return () => { unsubSelect(); }; }, [messageHub]); const handleAddComponent = (componentName: string) => { if (!selectedEntity) return; const componentRegistry = Core.services.resolve(ComponentRegistry); if (!componentRegistry) { console.error('ComponentRegistry not found'); return; } const component = componentRegistry.createInstance(componentName); if (component) { selectedEntity.addComponent(component); messageHub.publish('component:added', { entity: selectedEntity, component }); setShowAddComponent(false); } }; const handleRemoveComponent = (index: number) => { if (!selectedEntity) return; const component = selectedEntity.components[index]; if (component) { selectedEntity.removeComponent(component); messageHub.publish('component:removed', { entity: selectedEntity, component }); } }; const toggleComponentExpanded = (index: number) => { setExpandedComponents(prev => { const newSet = new Set(prev); if (newSet.has(index)) { newSet.delete(index); } else { newSet.add(index); } return newSet; }); }; const handlePropertyChange = (component: any, propertyName: string, value: any) => { if (!selectedEntity) return; messageHub.publish('component:property:changed', { entity: selectedEntity, component, propertyName, value }); }; if (!selectedEntity) { return (

Inspector

No entity selected
); } const components = selectedEntity.components; return (

Inspector

Entity Info
ID: {selectedEntity.id}
Name: Entity {selectedEntity.id}
Enabled: {selectedEntity.enabled ? 'Yes' : 'No'}
Components ({components.length})
{components.length === 0 ? (
No components
) : (
    {components.map((component, index) => { const isExpanded = expandedComponents.has(index); return (
  • 🔧 {component.constructor.name}
    {isExpanded && (
    handlePropertyChange(component, propertyName, value)} />
    )}
  • ); })}
)}
{showAddComponent && selectedEntity && ( setShowAddComponent(false)} /> )}
); }