Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
EntityInspectorManager.cpp
Go to the documentation of this file.
1/*
2 * Olympe Blueprint Editor - Entity Inspector Manager Implementation
3 */
4
6#include "../World.h"
7#include "../ECS_Components.h"
8#include <iostream>
9#include <algorithm>
10
11namespace Olympe
12{
18
22
27
29 {
30 if (m_Initialized)
31 return;
32
33 std::cout << "[EntityInspectorManager] Initializing...\n";
34
35 // Initial sync with World
37
38 m_Initialized = true;
39 std::cout << "[EntityInspectorManager] Initialized with " << m_EntityList.size() << " entities\n";
40 }
41
43 {
44 if (!m_Initialized)
45 return;
46
47 std::cout << "[EntityInspectorManager] Shutting down...\n";
48
49 m_EntityList.clear();
50 m_EntityInfoCache.clear();
52
53 m_Initialized = false;
54 }
55
57 {
58 if (!m_Initialized)
59 return;
60
61 // Event-driven tracking via OnEntityCreated/Destroyed hooks
62 // No polling needed - sync only on explicit request via ForceSyncWithWorld()
63 }
64
66 {
67 if (!m_Initialized)
68 return;
69
70 // Check if already tracked
71 if (std::find(m_EntityList.begin(), m_EntityList.end(), entity) != m_EntityList.end())
72 return;
73
74 m_EntityList.push_back(entity);
75
76 string name = "Entity_" + std::to_string(entity);
78 {
80 name = identity.name;
81 }
82
83 EntityInfo info(entity);
84 info.name = name;
85 info.isActive = true;
86 m_EntityInfoCache[entity] = info;
87
88 std::cout << "[EntityInspectorManager] Entity created: " << entity << "\n";
89 }
90
92 {
93 if (!m_Initialized)
94 return;
95
96 auto it = std::find(m_EntityList.begin(), m_EntityList.end(), entity);
97 if (it != m_EntityList.end())
98 {
99 m_EntityList.erase(it);
100 m_EntityInfoCache.erase(entity);
101
102 if (m_SelectedEntity == entity)
104
105 std::cout << "[EntityInspectorManager] Entity destroyed: " << entity << "\n";
106 }
107 }
108
109 void EntityInspectorManager::OnComponentAdded(EntityID entity, const std::string& componentType)
110 {
111 if (!m_Initialized)
112 return;
113
114 auto it = m_EntityInfoCache.find(entity);
115 if (it != m_EntityInfoCache.end())
116 {
117 auto& components = it->second.componentTypes;
118 if (std::find(components.begin(), components.end(), componentType) == components.end())
119 {
120 components.push_back(componentType);
121 }
122 }
123 }
124
125 void EntityInspectorManager::OnComponentRemoved(EntityID entity, const std::string& componentType)
126 {
127 if (!m_Initialized)
128 return;
129
130 auto it = m_EntityInfoCache.find(entity);
131 if (it != m_EntityInfoCache.end())
132 {
133 auto& components = it->second.componentTypes;
134 auto compIt = std::find(components.begin(), components.end(), componentType);
135 if (compIt != components.end())
136 {
137 components.erase(compIt);
138 }
139 }
140 }
141
142 std::vector<EntityID> EntityInspectorManager::GetAllEntities() const
143 {
144 return m_EntityList;
145 }
146
147 std::vector<EntityInfo> EntityInspectorManager::GetAllEntityInfo() const
148 {
149 std::vector<EntityInfo> result;
150 for (EntityID entity : m_EntityList)
151 {
152 auto it = m_EntityInfoCache.find(entity);
153 if (it != m_EntityInfoCache.end())
154 result.push_back(it->second);
155 }
156 return result;
157 }
158
160 {
161 auto it = m_EntityInfoCache.find(entity);
162 if (it != m_EntityInfoCache.end())
163 return it->second;
164
165 return EntityInfo();
166 }
167
169 {
170 return std::find(m_EntityList.begin(), m_EntityList.end(), entity) != m_EntityList.end();
171 }
172
173 std::vector<std::string> EntityInspectorManager::GetEntityComponents(EntityID entity) const
174 {
175 auto it = m_EntityInfoCache.find(entity);
176 if (it != m_EntityInfoCache.end())
177 return it->second.componentTypes;
178
179 return {};
180 }
181
182 bool EntityInspectorManager::HasComponent(EntityID entity, const std::string& componentType) const
183 {
184 auto components = GetEntityComponents(entity);
185 return std::find(components.begin(), components.end(), componentType) != components.end();
186 }
187
188 std::vector<ComponentPropertyInfo> EntityInspectorManager::GetComponentProperties(EntityID entity, const std::string& componentType)
189 {
190 std::vector<ComponentPropertyInfo> properties;
191
192 // Access World to get actual component data
193 World& world = World::Get();
194
195 if (!world.IsEntityValid(entity))
196 return properties;
197
198 // Based on component type, extract properties
199 // This is a simplified version - in a real implementation, you'd use reflection or type traits
200
201 if (componentType == "Position_data")
202 {
203 if (world.HasComponent<Position_data>(entity))
204 {
205 auto& comp = world.GetComponent<Position_data>(entity);
206
208 prop.name = "x";
209 prop.type = "float";
210 prop.value = std::to_string(comp.position.x);
211 prop.dataPtr = &comp.position.x;
212 properties.push_back(prop);
213
214 prop.name = "y";
215 prop.value = std::to_string(comp.position.y);
216 prop.dataPtr = &comp.position.y;
217 properties.push_back(prop);
218
219 prop.name = "z";
220 prop.value = std::to_string(comp.position.z);
221 prop.dataPtr = &comp.position.z;
222 properties.push_back(prop);
223 }
224 }
225 else if (componentType == "Velocity_data")
226 {
227 if (world.HasComponent<Movement_data>(entity))
228 {
229 auto& comp = world.GetComponent<Movement_data>(entity);
230
232 prop.name = "dx";
233 prop.type = "float";
234 prop.value = std::to_string(comp.velocity.x);
235 prop.dataPtr = &comp.velocity.x;
236 properties.push_back(prop);
237
238 prop.name = "dy";
239 prop.value = std::to_string(comp.velocity.y);
240 prop.dataPtr = &comp.velocity.y;
241 properties.push_back(prop);
242
243 prop.name = "dz";
244 prop.value = std::to_string(comp.velocity.z);
245 prop.dataPtr = &comp.velocity.z;
246 properties.push_back(prop);
247 }
248 }
249 // Add more component types as needed...
250
251 return properties;
252 }
253
254 bool EntityInspectorManager::SetComponentProperty(EntityID entity, const std::string& componentType,
255 const std::string& propertyName, const std::string& value)
256 {
257 World& world = World::Get();
258
259 if (!world.IsEntityValid(entity))
260 return false;
261
262 // Based on component type and property name, set the value
263 // This is simplified - a real implementation would use reflection
264
265 if (componentType == "Position_data" && world.HasComponent<Position_data>(entity))
266 {
267 auto& comp = world.GetComponent<Position_data>(entity);
268
269 try
270 {
271 float floatValue = std::stof(value);
272
273 if (propertyName == "x")
274 comp.position.x = floatValue;
275 else if (propertyName == "y")
276 comp.position.y = floatValue;
277 else if (propertyName == "z")
278 comp.position.z = floatValue;
279 else
280 return false;
281
282 return true;
283 }
284 catch (...)
285 {
286 return false;
287 }
288 }
289 else if (componentType == "Movement_data" && world.HasComponent<Movement_data>(entity))
290 {
291 try
292 {
293 auto& comp = world.GetComponent<Movement_data>(entity);
294
295 float floatValue = std::stof(value);
296
297 if (propertyName == "dx")
298 comp.velocity.x = floatValue;
299 else if (propertyName == "dy")
300 comp.velocity.y = floatValue;
301 else if (propertyName == "dz")
302 comp.velocity.z = floatValue;
303 else
304 return false;
305
306 return true;
307 }
308 catch (...)
309 {
310 return false;
311 }
312 }
313
314 return false;
315 }
316
317 std::vector<EntityID> EntityInspectorManager::FilterByName(const std::string& nameFilter) const
318 {
319 std::vector<EntityID> result;
320
321 if (nameFilter.empty())
322 return m_EntityList;
323
324 for (EntityID entity : m_EntityList)
325 {
326 auto it = m_EntityInfoCache.find(entity);
327 if (it != m_EntityInfoCache.end())
328 {
329 if (it->second.name.find(nameFilter) != std::string::npos)
330 result.push_back(entity);
331 }
332 }
333
334 return result;
335 }
336
337 std::vector<EntityID> EntityInspectorManager::FilterByComponent(const std::string& componentType) const
338 {
339 std::vector<EntityID> result;
340
341 for (EntityID entity : m_EntityList)
342 {
343 if (HasComponent(entity, componentType))
344 result.push_back(entity);
345 }
346
347 return result;
348 }
349
351 {
352 if (IsEntityValid(entity) || entity == INVALID_ENTITY_ID)
353 m_SelectedEntity = entity;
354 }
355
360
362 {
363 World& world = World::Get();
364
365 // Get all entities from World
366 const auto& entitySignatures = world.m_entitySignatures;
367
368 // Add any entities we don't have
369 for (const auto& pair : entitySignatures)
370 {
371 EntityID entity = pair.first;
372
373 if (std::find(m_EntityList.begin(), m_EntityList.end(), entity) == m_EntityList.end())
374 {
375 // New entity found
376 OnEntityCreated(entity);
377 }
378
379 // Update component list
380 auto& info = m_EntityInfoCache[entity];
381 info.componentTypes.clear();
382
383 // Extract component types from signature
384 const ComponentSignature& sig = pair.second;
385
386 // Check each component type
387 // This is a simplified approach - ideally you'd have a component type registry
389 info.componentTypes.push_back("Position_data");
391 info.componentTypes.push_back("Velocity_data");
393 info.componentTypes.push_back("Sprite_data");
395 info.componentTypes.push_back("Health_data");
397 info.componentTypes.push_back("Controller_data");
399 info.componentTypes.push_back("PlayerBinding_data");
401 info.componentTypes.push_back("PlayerController_data");
402 // Add more component types as needed...
403 }
404
405 // Remove entities that no longer exist in World
406 m_EntityList.erase(
407 std::remove_if(m_EntityList.begin(), m_EntityList.end(),
408 [&entitySignatures](EntityID entity)
409 {
410 return entitySignatures.find(entity) == entitySignatures.end();
411 }),
412 m_EntityList.end()
413 );
414 }
415
417 {
418 // Map type IDs to names
419 // This is a simplified approach
420 return "UnknownComponent";
421 }
422
424 {
425 // Map names to type IDs
426 return 0;
427 }
428}
std::bitset< MAX_COMPONENTS > ComponentSignature
Definition ECS_Entity.h:31
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::uint64_t EntityID
Definition ECS_Entity.h:21
std::uint64_t ComponentTypeID
Definition ECS_Entity.h:27
const EntityID INVALID_ENTITY_ID
Definition ECS_Entity.h:23
EntityInspectorManager - Manages runtime entity tracking and inspection Singleton that maintains a sy...
std::vector< EntityID > GetAllEntities() const
std::vector< std::string > GetEntityComponents(EntityID entity) const
ComponentTypeID GetComponentTypeId(const std::string &name) const
static EntityInspectorManager & Instance()
void OnComponentAdded(EntityID entity, const std::string &componentType)
EntityInfo GetEntityInfo(EntityID entity) const
std::vector< ComponentPropertyInfo > GetComponentProperties(EntityID entity, const std::string &componentType)
std::map< EntityID, EntityInfo > m_EntityInfoCache
bool HasComponent(EntityID entity, const std::string &componentType) const
bool SetComponentProperty(EntityID entity, const std::string &componentType, const std::string &propertyName, const std::string &value)
void OnComponentRemoved(EntityID entity, const std::string &componentType)
std::vector< EntityID > FilterByName(const std::string &nameFilter) const
std::string GetComponentName(ComponentTypeID typeId) const
bool IsEntityValid(EntityID entity) const
std::vector< EntityID > FilterByComponent(const std::string &componentType) const
std::vector< EntityInfo > GetAllEntityInfo() const
Core ECS manager and world coordinator.
Definition World.h:210
static World & Get()
Get singleton instance (short form)
Definition World.h:232
bool HasComponent(EntityID entity) const
Definition World.h:451
bool IsEntityValid(EntityID entity) const
Definition World.h:376
T & GetComponent(EntityID entity)
Definition World.h:438
std::unordered_map< EntityID, ComponentSignature > m_entitySignatures
Definition World.h:636
Identity component for entity identification.
std::string name
Entity name identifier.
Position component for spatial location.