diff --git a/include/Engine/Core/EntityFile.h b/include/Engine/Core/EntityFile.h index 962e5c4e..27115264 100644 --- a/include/Engine/Core/EntityFile.h +++ b/include/Engine/Core/EntityFile.h @@ -36,23 +36,23 @@ class EntityFileHandler public: // @param EntityID The entity found // @param EntityID The parent of the entity - typedef std::function OnStartEntityCallback; + typedef std::function OnStartEntityCallback; void SetStartEntityCallback(OnStartEntityCallback c) { m_OnStartEntityCallback = c; } // @param EntityID The entity the component corresponds to // @param std::string Type name of the component - typedef std::function OnStartComponentCallback; + typedef std::function OnStartComponentCallback; void SetStartComponentCallback(OnStartComponentCallback c) { m_OnStartComponentCallback = c; } // @param EntityID Entity // @param std::string Component name // @param std::string Field name // @param std::map Field attribute names and values - typedef std::function)> OnStartFieldCallback; + typedef std::function&)> OnStartFieldCallback; void SetStartFieldCallback(OnStartFieldCallback c) { m_OnStartFieldCallback = c; } // @param EntityID Entity // @param std::string Component name // @param std::string Field name // @param char* Field data - typedef std::function OnStartFieldDataCallback; + typedef std::function OnStartFieldDataCallback; void SetStartFieldDataCallback(OnStartFieldDataCallback c) { m_OnStartFieldDataCallback = c; } private: @@ -172,13 +172,13 @@ private: { EntityID parent = m_EntityStack.top(); - // TODO: Create entity here - auto xName = attrs.getValue(XS::ToXMLCh("name")); - std::string name = XS::ToString(xName); - //LOG_DEBUG("Entity %i (%i): %s", m_NextEntityID, parent, name.c_str()); - if (m_Handler->m_OnStartEntityCallback) { - m_Handler->m_OnStartEntityCallback(m_NextEntityID, parent); + std::string name; + auto xName = attrs.getValue(XS::ToXMLCh("name")); + if (xName != nullptr) { + name = XS::ToString(xName); + } + m_Handler->m_OnStartEntityCallback(m_NextEntityID, parent, name); } m_EntityStack.push(m_NextEntityID); @@ -198,7 +198,7 @@ private: parser->parse(path.c_str()); delete parser; } - void onStartComponent(std::string name) + void onStartComponent(const std::string& name) { //LOG_DEBUG(" Component: %s", name.c_str()); m_CurrentComponent = name; @@ -206,8 +206,8 @@ private: m_Handler->m_OnStartComponentCallback(m_EntityStack.top(), name); } } - void onEndComponent(std::string name) { } - void onStartComponentField(std::string field, const xercesc::Attributes& attrs) + void onEndComponent(const std::string& name) { } + void onStartComponentField(const std::string& field, const xercesc::Attributes& attrs) { //LOG_DEBUG(" Field: %s", field.c_str()); m_CurrentField = field; @@ -223,7 +223,7 @@ private: m_Handler->m_OnStartFieldCallback(m_EntityStack.top(), m_CurrentComponent, field, m_CurrentAttributes); } } - void onEndComponentField(std::string field) { } + void onEndComponentField(const std::string& field) { } void onFieldData(char* data) { diff --git a/include/Engine/Core/EntityFileParser.h b/include/Engine/Core/EntityFileParser.h index c4c8c221..d46b508b 100644 --- a/include/Engine/Core/EntityFileParser.h +++ b/include/Engine/Core/EntityFileParser.h @@ -19,10 +19,10 @@ private: // created in order to resolve parent-child relationships. std::map m_EntityIDMapper; - void onStartEntity(EntityID entity, EntityID parent); - void onStartComponent(EntityID entity, std::string component); - void onStartComponentField(EntityID entity, std::string componentType, std::string fieldName, std::map attributes); - void onFieldData(EntityID entity, std::string componentType, std::string fieldName, const char* fieldData); + void onStartEntity(EntityID entity, EntityID parent, const std::string& name); + void onStartComponent(EntityID entity, const std::string& component); + void onStartComponentField(EntityID entity, const std::string& componentType, const std::string& fieldName, const std::map& attributes); + void onFieldData(EntityID entity, const std::string& componentType, const std::string& fieldName, const char* fieldData); }; #endif \ No newline at end of file diff --git a/include/Engine/Core/World.h b/include/Engine/Core/World.h index 22e684b0..1ea17a7b 100644 --- a/include/Engine/Core/World.h +++ b/include/Engine/Core/World.h @@ -37,6 +37,10 @@ public: const std::unordered_map& GetComponentPools() const { return m_ComponentPools; } // Get the entity children map const std::unordered_multimap& GetEntityChildren() const { return m_EntityChildren; } + // Set the textual name of an entity + void SetName(EntityID entity, const std::string& name); + // Get the textual name of an entity + std::string GetName(EntityID entity) const; private: EntityID m_CurrentEntityID = 0; @@ -45,6 +49,7 @@ private: // TODO: This should be a more effective structure std::unordered_multimap m_EntityChildren; std::unordered_map m_ComponentPools; + std::unordered_map m_EntityNames; EntityID generateEntityID(); }; diff --git a/src/Engine/Core/EntityFileParser.cpp b/src/Engine/Core/EntityFileParser.cpp index b3310cd4..541da01d 100644 --- a/src/Engine/Core/EntityFileParser.cpp +++ b/src/Engine/Core/EntityFileParser.cpp @@ -3,7 +3,7 @@ EntityFileParser::EntityFileParser(const EntityFile* entityFile) : m_EntityFile(entityFile) { - m_Handler.SetStartEntityCallback(std::bind(&EntityFileParser::onStartEntity, this, std::placeholders::_1, std::placeholders::_2)); + m_Handler.SetStartEntityCallback(std::bind(&EntityFileParser::onStartEntity, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); m_Handler.SetStartComponentCallback(std::bind(&EntityFileParser::onStartComponent, this, std::placeholders::_1, std::placeholders::_2)); m_Handler.SetStartFieldCallback(std::bind(&EntityFileParser::onStartComponentField, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); m_Handler.SetStartFieldDataCallback(std::bind(&EntityFileParser::onFieldData, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); @@ -16,22 +16,25 @@ void EntityFileParser::MergeEntities(World* world) m_EntityFile->Parse(&m_Handler); } -void EntityFileParser::onStartEntity(EntityID entity, EntityID parent) +void EntityFileParser::onStartEntity(EntityID entity, EntityID parent, const std::string& name) { EntityID realParent = m_EntityIDMapper.at(parent); EntityID realEntity = m_World->CreateEntity(realParent); + if (!name.empty()) { + m_World->SetName(realEntity, name); + } m_EntityIDMapper[entity] = realEntity; LOG_DEBUG("Created entity #%i (%i) with parent %i (%i)", entity, realEntity, parent, realParent); } -void EntityFileParser::onStartComponent(EntityID entity, std::string component) +void EntityFileParser::onStartComponent(EntityID entity, const std::string& component) { EntityID realEntity = m_EntityIDMapper.at(entity); m_World->AttachComponent(realEntity, component); LOG_DEBUG("Attached component of type \"%s\" to entity #%i (%i)", component.c_str(), entity, realEntity); } -void EntityFileParser::onStartComponentField(EntityID entity, std::string componentType, std::string fieldName, std::map attributes) +void EntityFileParser::onStartComponentField(EntityID entity, const std::string& componentType, const std::string& fieldName, const std::map& attributes) { EntityID realEntity = m_EntityIDMapper.at(entity); ComponentWrapper component = m_World->GetComponent(realEntity, componentType); @@ -47,7 +50,7 @@ void EntityFileParser::onStartComponentField(EntityID entity, std::string compon EntityFile::WriteAttributeData(data, field, attributes); } -void EntityFileParser::onFieldData(EntityID entity, std::string componentType, std::string fieldName, const char* fieldData) +void EntityFileParser::onFieldData(EntityID entity, const std::string& componentType, const std::string& fieldName, const char* fieldData) { EntityID realEntity = m_EntityIDMapper.at(entity); ComponentWrapper component = m_World->GetComponent(realEntity, componentType); diff --git a/src/Engine/Core/EntityFileWriter.cpp b/src/Engine/Core/EntityFileWriter.cpp index 161c3169..5b6c466d 100644 --- a/src/Engine/Core/EntityFileWriter.cpp +++ b/src/Engine/Core/EntityFileWriter.cpp @@ -15,6 +15,10 @@ void EntityFileWriter::WriteEntity(World* world, EntityID entity) root->setAttribute(X("xmlns:xsi"), X("http://www.w3.org/2001/XMLSchema-instance")); root->setAttribute(X("xsi:noNamespaceSchemaLocation"), X("../Types/Entity.xsd")); root->setAttribute(X("xmlns:c"), X("components")); + const std::string& name = world->GetName(entity); + if (!name.empty()) { + root->setAttribute(X("name"), X(name)); + } DOMElement* componentsElement = doc->createElement(X("Components")); root->appendChild(componentsElement); @@ -47,6 +51,10 @@ void EntityFileWriter::appendEntityChildren(xercesc::DOMElement* parentElement, EntityID childEntity = it->second; DOMElement* entityElement = doc->createElement(X("Entity")); + const std::string& name = world->GetName(childEntity); + if (!name.empty()) { + entityElement->setAttribute(X("name"), X(name)); + } parentElement->appendChild(entityElement); DOMElement* componentsElement = doc->createElement(X("Components")); diff --git a/src/Engine/Core/World.cpp b/src/Engine/Core/World.cpp index d947d1fd..9202be12 100644 --- a/src/Engine/Core/World.cpp +++ b/src/Engine/Core/World.cpp @@ -49,6 +49,9 @@ void World::DeleteEntity(EntityID entity) break; } } + + // Erase potential name + m_EntityNames.erase(entity); } void World::RegisterComponent(ComponentInfo& ci) @@ -121,6 +124,25 @@ void World::SetParent(EntityID entity, EntityID parent) m_EntityChildren.insert(std::make_pair(parent, entity)); } +void World::SetName(EntityID entity, const std::string& name) +{ + m_EntityNames[entity] = name; +} + +std::string World::GetName(EntityID entity) const +{ + if (entity == EntityID_Invalid) { + return "EntityID_Invalid"; + } + + auto it = m_EntityNames.find(entity); + if (it != m_EntityNames.end()) { + return it->second; + } else { + return std::string(); + } +} + EntityID World::generateEntityID() { // TODO: Make EntityID generation smarter diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 20ad2dbd..4ceba0fd 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -279,7 +279,6 @@ bool EditorSystem::OnFileDropped(const Events::FileDropped& e) return true; } - void EditorSystem::createWidget() { if (m_Widget == EntityID_Invalid) { @@ -600,7 +599,14 @@ bool EditorSystem::createEntityNode(World* world, EntityID entity) } ImGui::SetNextTreeNodeOpened(true, ImGuiSetCond_Once); - if (ImGui::TreeNode((std::string("#") + std::to_string(entity)).c_str())) { + std::string nodeTitle; + const std::string& entityName = world->GetName(entity); + if (!entityName.empty()) { + nodeTitle = entityName; + } else { + nodeTitle = std::string("#") + std::to_string(entity); + } + if (ImGui::TreeNode(nodeTitle.c_str())) { if (m_UIDraggingEntity != EntityID_Invalid && ImGui::IsItemHoveredRect() && ImGui::IsMouseReleased(0)) { LOG_DEBUG("Changed parent of %i to %i", m_UIDraggingEntity, entity); changeParent(m_UIDraggingEntity, entity);