2025-01-04 04:12:51 +00:00
|
|
|
#include "entities/EntitiesManager.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "workspace/WorkSpace.h"
|
2025-01-11 14:17:15 +00:00
|
|
|
#include "workspace/WorkSpaceManager.h"
|
2025-01-04 04:12:51 +00:00
|
|
|
#include "common/SpdLogger.h"
|
|
|
|
|
#include "entities/Entity.h"
|
|
|
|
|
#include "entities/ComponentFactory.h"
|
2025-11-01 13:30:06 +00:00
|
|
|
#include "entities/EntityFactory.h"
|
|
|
|
|
#include "entities/EntityRegistration.h"
|
2025-01-04 04:12:51 +00:00
|
|
|
|
|
|
|
|
#include "xml/tinyxml2.h"
|
|
|
|
|
|
|
|
|
|
template<> EntitiesManager* Singleton<EntitiesManager>::instance_ = nullptr;
|
|
|
|
|
|
|
|
|
|
EntitiesManager::EntitiesManager(QObject* parent)
|
|
|
|
|
: QObject(parent) {
|
2025-11-01 13:30:06 +00:00
|
|
|
Initialize();
|
2025-01-04 04:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EntitiesManager::~EntitiesManager() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EntitiesManager::OnDestory() {
|
|
|
|
|
std::vector<Entity*> entities(entities_.size());
|
|
|
|
|
std::transform(entities_.begin(), entities_.end(), entities.begin(),
|
|
|
|
|
[](const std::pair<QString, Entity*>& pair) {
|
|
|
|
|
return pair.second;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
for (auto* entity : entities) {
|
|
|
|
|
RemoveEntity(entity);
|
2025-01-05 15:54:44 +00:00
|
|
|
entity->deleteLater();
|
2025-01-04 04:12:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2025-01-11 14:17:15 +00:00
|
|
|
if (name == "Entity") {
|
|
|
|
|
//WorkSpace* workspace = WorkSpaceManager::Get().GetCurrent();
|
|
|
|
|
return new Entity(name);
|
|
|
|
|
}
|
2025-01-04 04:12:51 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-05 15:54:44 +00:00
|
|
|
Entity* entity = new Entity(workspce);
|
2025-01-04 04:12:51 +00:00
|
|
|
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());
|
|
|
|
|
}
|
2025-11-01 13:30:06 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 16:02:20 +00:00
|
|
|
Entity* EntitiesManager::CreateEntityWithComponents(const QString& type, const QString& mesh, bool useLabel, WorkSpace* workspace) {
|
2025-11-01 13:30:06 +00:00
|
|
|
if (!initialized_) {
|
|
|
|
|
Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!workspace) {
|
|
|
|
|
workspace = WorkSpaceManager::Get().GetCurrent();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 16:02:20 +00:00
|
|
|
Entity* entity = EntityFactory::Get().CreateEntityWithComponents(type, mesh, useLabel, workspace);
|
2025-11-01 13:30:06 +00:00
|
|
|
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);
|
|
|
|
|
}
|