#include "entities/EntitiesManager.h" #include #include "workspace/WorkSpace.h" #include "workspace/WorkSpaceManager.h" #include "common/SpdLogger.h" #include "entities/Entity.h" #include "entities/ComponentFactory.h" #include "entities/EntityFactory.h" #include "entities/EntityRegistration.h" #include "xml/tinyxml2.h" template<> EntitiesManager* Singleton::instance_ = nullptr; EntitiesManager::EntitiesManager(QObject* parent) : QObject(parent) { Initialize(); } EntitiesManager::~EntitiesManager() { } void EntitiesManager::OnDestory() { std::vector entities(entities_.size()); std::transform(entities_.begin(), entities_.end(), entities.begin(), [](const std::pair& pair) { return pair.second; } ); for (auto* entity : entities) { RemoveEntity(entity); entity->deleteLater(); } } bool EntitiesManager::Contains(const QString& name) const { const auto itor = entities_.find(name); if (entities_.end() != itor) { return true; } return false; } bool EntitiesManager::Parse(const tinyxml2::XMLElement* element, WorkSpace* workspce) { LOG_INFO("entitiesManager parse"); if (nullptr == element) { LOG_WARN("parse failed"); return false; } if (nullptr == workspce) { LOG_WARN("workspce is nullptr"); return false; } const tinyxml2::XMLElement* xmlElement = element->FirstChildElement(); Entity* parent = nullptr; while (nullptr != xmlElement) { const char* name = xmlElement->Name(); Entity* entity = Create(xmlElement, parent, workspce); if (nullptr == entity) { xmlElement = xmlElement->NextSiblingElement(); LOG_WARN("Create entity failed {}", name); continue; } xmlElement = xmlElement->NextSiblingElement(); } return true; } Entity* EntitiesManager::Create(const QString& name) { if (name == "Entity") { //WorkSpace* workspace = WorkSpaceManager::Get().GetCurrent(); return new Entity(name); } return nullptr; } Entity* EntitiesManager::Create(const tinyxml2::XMLElement* element, Entity* parent, WorkSpace* workspce) { const char* name = element->Name(); if (strcmp(name, "Entity") != 0) { return nullptr; } Entity* entity = new Entity(workspce); if (nullptr == entity) { LOG_WARN("create entity failed"); return nullptr; } entity->Serialize(element); AddEntity(entity); entity->SetParent(parent); if (nullptr == parent) { workspce->AddEntity(entity); } return entity; } Entity* EntitiesManager::CreateMesh(const QString& mesh) { Entity* entity = Create("Entity"); AddEntity(entity); SceneComponent* conponent = ComponentFactory::Create("MeshComponent", nullptr); conponent->SetAttribute("mesh", mesh.toStdString().c_str()); conponent->AttachEntity(entity); return entity; } bool EntitiesManager::DeleteEntity(Entity* entity) { if (nullptr == entity) { LOG_WARN("entity is nullptr"); return false; } RemoveEntity(entity); entity->deleteLater(); return true; } Entity* EntitiesManager::GetEntity(const QString& uuid) { Entity* entity = entities_[uuid]; if (nullptr == entity) { LOG_WARN("not contain entity: {}", uuid.toStdString()); } return entity; } void EntitiesManager::AddEntity(Entity* entity) { if (nullptr == entity) { LOG_WARN("entity is nullptr"); return; } entities_[entity->GetUUid()] = entity; } void EntitiesManager::RemoveEntity(Entity* entity) { if (nullptr == entity) { LOG_WARN("entity is nullptr"); return; } entities_.erase(entity->GetUUid()); } void EntitiesManager::Initialize() { if (!initialized_) { EntityRegistration::RegisterAllEntities(); initialized_ = true; LOG_INFO("EntitiesManager::Initialize - Entity factory initialized"); } } Entity* EntitiesManager::CreateEntity(const QString& type, WorkSpace* workspace) { if (!initialized_) { Initialize(); } if (!workspace) { workspace = WorkSpaceManager::Get().GetCurrent(); } Entity* entity = EntityFactory::Get().CreateEntity(type, workspace); if (entity) { AddEntity(entity); if (workspace) { workspace->AddEntity(entity); } LOG_INFO("EntitiesManager::CreateEntity - Created entity of type: {}", type.toStdString()); } else { LOG_ERROR("EntitiesManager::CreateEntity - Failed to create entity of type: {}", type.toStdString()); } return entity; } Entity* EntitiesManager::CreateEntityWithComponents(const QString& type, const QString& mesh, bool useLabel, WorkSpace* workspace) { if (!initialized_) { Initialize(); } if (!workspace) { workspace = WorkSpaceManager::Get().GetCurrent(); } Entity* entity = EntityFactory::Get().CreateEntityWithComponents(type, mesh, useLabel, workspace); if (entity) { AddEntity(entity); if (workspace) { workspace->AddEntity(entity); } LOG_INFO("EntitiesManager::CreateEntityWithComponents - Created entity with components of type: {}", type.toStdString()); } else { LOG_ERROR("EntitiesManager::CreateEntityWithComponents - Failed to create entity of type: {}", type.toStdString()); } return entity; } QStringList EntitiesManager::GetSupportedEntityTypes() const { if (!initialized_) { return QStringList(); } return EntityFactory::Get().GetRegisteredTypes(); } QString EntitiesManager::GetEntityDisplayName(const QString& type) const { if (!initialized_) { return type; } return EntityFactory::Get().GetDisplayName(type); } QString EntitiesManager::GetEntityDescription(const QString& type) const { if (!initialized_) { return QString(); } return EntityFactory::Get().GetDescription(type); }