Added fixes to scene graph. Read description for details.
1. Scene graph creates a root on construction. EntityID is set to MaxEID. The root is not intended to be modified. 2. When adding a node, if the 2nd arg (parent) is omitted, the node will be a child of the root.
This commit is contained in:
parent
624c8fa4d6
commit
c0290b7185
|
@ -76,7 +76,10 @@ namespace SHADE
|
|||
|
||||
SHSceneGraph::SHSceneGraph() noexcept
|
||||
: root { nullptr }
|
||||
{}
|
||||
{
|
||||
// The root is set to the maximum entity. It should not be interfaced with.
|
||||
root = AllocateNode(MAX_EID);
|
||||
}
|
||||
|
||||
SHSceneGraph::~SHSceneGraph() noexcept
|
||||
{
|
||||
|
@ -90,6 +93,8 @@ namespace SHADE
|
|||
for (auto* node : entityNodeMap | std::views::values)
|
||||
ReleaseNode(node);
|
||||
|
||||
delete root;
|
||||
|
||||
#ifdef _DEBUG
|
||||
SHLOG_INFO("Scene Graph Destroyed Successfully!")
|
||||
#endif
|
||||
|
@ -99,10 +104,25 @@ namespace SHADE
|
|||
/* Getter Function Definitions */
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
EntityID SHSceneNode::GetEntityID() const noexcept
|
||||
{
|
||||
return entityID;
|
||||
}
|
||||
|
||||
SHSceneNode* SHSceneNode::GetParent() const noexcept
|
||||
{
|
||||
return parent;
|
||||
}
|
||||
|
||||
std::vector<SHSceneNode*>& SHSceneNode::GetChildren() noexcept
|
||||
{
|
||||
return children;
|
||||
}
|
||||
|
||||
SHSceneNode* SHSceneNode::GetChild(EntityID childID) const noexcept
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error handling
|
||||
{
|
||||
if (!SHEntityManager::IsValidEID(childID))
|
||||
{
|
||||
SHLOG_ERROR("Child Entity {} is invalid! Unable to get child from Entity {}", childID, entityID)
|
||||
|
@ -114,7 +134,7 @@ namespace SHADE
|
|||
SHLOG_WARNING("Entity {} has no children!", entityID)
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
// Find child
|
||||
const auto ENTITY_MATCH = [&](const SHSceneNode* node) { return node->GetEntityID() == childID; };
|
||||
|
@ -129,7 +149,7 @@ namespace SHADE
|
|||
return *CHILD_ITER;
|
||||
}
|
||||
|
||||
SHSceneNode* SHSceneGraph::GetRoot() const noexcept
|
||||
const SHSceneNode* SHSceneGraph::GetRoot() const noexcept
|
||||
{
|
||||
if (root != nullptr)
|
||||
return root;
|
||||
|
@ -140,6 +160,8 @@ namespace SHADE
|
|||
|
||||
SHSceneNode* SHSceneGraph::GetNode(EntityID entityID) const noexcept
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error handling
|
||||
if (!SHEntityManager::IsValidEID(entityID))
|
||||
{
|
||||
SHLOG_ERROR("Entity {} is invalid! Unable to Get Scene node!", entityID)
|
||||
|
@ -152,12 +174,15 @@ namespace SHADE
|
|||
SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to Get Scene node!", entityID)
|
||||
return nullptr;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
return NODE_ITER->second;
|
||||
}
|
||||
|
||||
SHSceneNode* SHSceneGraph::GetParent(EntityID entityID) const noexcept
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error handling
|
||||
if (!SHEntityManager::IsValidEID(entityID))
|
||||
{
|
||||
SHLOG_ERROR("Entity {} is invalid! Unable to get Parent node!", entityID)
|
||||
|
@ -170,13 +195,15 @@ namespace SHADE
|
|||
SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to get Parent node!", entityID)
|
||||
return nullptr;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
return NODE_ITER->second->GetParent();
|
||||
}
|
||||
|
||||
SHSceneNode* SHSceneGraph::GetChild(EntityID entityID, SHSceneNode* childNode) const noexcept
|
||||
{
|
||||
// Error Handling
|
||||
////////////////////////////////////////
|
||||
// Error handling
|
||||
if (!SHEntityManager::IsValidEID(entityID))
|
||||
{
|
||||
SHLOG_ERROR("Entity {} is invalid!", entityID)
|
||||
|
@ -203,12 +230,15 @@ namespace SHADE
|
|||
SHLOG_WARNING("Entity {} is not a child of Entity {}!", childNode->GetEntityID(), entityID)
|
||||
return nullptr;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
return *CHILD_ITER;
|
||||
}
|
||||
|
||||
SHSceneNode* SHSceneGraph::GetChild(EntityID entityID, EntityID childEntityID) const noexcept
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error handling
|
||||
if (!SHEntityManager::IsValidEID(entityID))
|
||||
{
|
||||
SHLOG_ERROR("Entity {} is invalid!", entityID)
|
||||
|
@ -221,14 +251,15 @@ namespace SHADE
|
|||
SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID)
|
||||
return nullptr;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
return NODE_ITER->second->GetChild(childEntityID);
|
||||
}
|
||||
|
||||
const std::vector<SHSceneNode*>& SHSceneGraph::GetChildren(EntityID entityID) const noexcept
|
||||
{
|
||||
// TODO(Diren): Discuss with team best way to handle this
|
||||
|
||||
////////////////////////////////////////
|
||||
// Error handling
|
||||
if (!SHEntityManager::IsValidEID(entityID))
|
||||
{
|
||||
SHLOG_ERROR("Entity {} is invalid!", entityID)
|
||||
|
@ -241,6 +272,7 @@ namespace SHADE
|
|||
SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID)
|
||||
return root->GetChildren();
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
return NODE_ITER->second->GetChildren();
|
||||
}
|
||||
|
@ -254,6 +286,7 @@ namespace SHADE
|
|||
if (parentNode == nullptr)
|
||||
SHLOG_WARNING("Removing Entity {}'s parent", entityID)
|
||||
|
||||
// Handle self assignment
|
||||
if (parentNode == parent)
|
||||
return;
|
||||
|
||||
|
@ -264,6 +297,8 @@ namespace SHADE
|
|||
|
||||
void SHSceneGraph::SetParent(EntityID entityID, SHSceneNode* parent) const noexcept
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error Handling
|
||||
if (!SHEntityManager::IsValidEID(entityID))
|
||||
{
|
||||
SHLOG_ERROR("Entity {} is invalid!", entityID)
|
||||
|
@ -276,12 +311,15 @@ namespace SHADE
|
|||
SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID)
|
||||
return;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
NODE_ITER->second->SetParent(parent);
|
||||
}
|
||||
|
||||
void SHSceneGraph::SetParent(EntityID entityID, EntityID parent) const noexcept
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error Handling
|
||||
if (!SHEntityManager::IsValidEID(entityID))
|
||||
{
|
||||
SHLOG_ERROR("Entity {} is invalid! Unable to set parent of an invalid entity!", entityID)
|
||||
|
@ -307,6 +345,7 @@ namespace SHADE
|
|||
SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to parent to Entity {}", parent, entityID)
|
||||
return;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
SHSceneNode* currentNode = NODE_ITER->second;
|
||||
currentNode->SetParent(PARENT_ITER->second);
|
||||
|
@ -318,22 +357,32 @@ namespace SHADE
|
|||
|
||||
void SHSceneNode::AddChild(SHSceneNode* newChild) noexcept
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error Handling
|
||||
if (newChild == nullptr)
|
||||
{
|
||||
SHLOG_WARNING("Attempting to add a non-existent child to an entity!")
|
||||
return;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
if (newChild->parent)
|
||||
newChild->parent->RemoveChild(newChild);
|
||||
|
||||
newChild->parent = this;
|
||||
children.emplace_back(newChild);
|
||||
}
|
||||
|
||||
bool SHSceneNode::RemoveChild(EntityID childID) noexcept
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error Handling
|
||||
if (!SHEntityManager::IsValidEID(childID))
|
||||
{
|
||||
SHLOG_ERROR("Entity {} is invalid!", childID)
|
||||
return false;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
SHSceneNode* removedChild = nullptr;
|
||||
const auto ENTITY_MATCH = [&](SHSceneNode* node)
|
||||
|
@ -348,18 +397,23 @@ namespace SHADE
|
|||
};
|
||||
|
||||
children.end() = std::remove_if(children.begin(), children.end(), ENTITY_MATCH);
|
||||
|
||||
if (removedChild)
|
||||
removedChild->parent = nullptr;
|
||||
|
||||
return removedChild == nullptr;
|
||||
return removedChild != nullptr;
|
||||
}
|
||||
|
||||
bool SHSceneNode::RemoveChild(SHSceneNode* childToRemove) noexcept
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error Handling
|
||||
if (childToRemove == nullptr)
|
||||
{
|
||||
SHLOG_WARNING("Attempting to remove non-existent child from Entity {}", entityID)
|
||||
return false;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
children.end() = std::remove(children.begin(), children.end(), childToRemove);
|
||||
childToRemove->parent = nullptr;
|
||||
|
@ -378,11 +432,14 @@ namespace SHADE
|
|||
|
||||
SHSceneNode* SHSceneGraph::AddNode(EntityID entityID, SHSceneNode* parent)
|
||||
{
|
||||
////////////////////////////////////////
|
||||
// Error Handling
|
||||
if (!SHEntityManager::IsValidEID(entityID))
|
||||
{
|
||||
SHLOG_ERROR("Entity {} is invalid!", entityID)
|
||||
return nullptr;
|
||||
}
|
||||
////////////////////////////////////////
|
||||
|
||||
if (auto NODE_ITER = entityNodeMap.find(entityID); NODE_ITER != entityNodeMap.end())
|
||||
{
|
||||
|
@ -391,6 +448,10 @@ namespace SHADE
|
|||
}
|
||||
|
||||
SHSceneNode* newNode = AllocateNode(entityID);
|
||||
|
||||
if (parent == nullptr)
|
||||
parent = root;
|
||||
|
||||
newNode->SetParent(parent);
|
||||
|
||||
return newNode;
|
||||
|
|
|
@ -47,9 +47,9 @@ namespace SHADE
|
|||
/* Getter Functions */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
||||
[[nodiscard]] EntityID GetEntityID () const noexcept { return entityID ;}
|
||||
[[nodiscard]] SHSceneNode* GetParent () const noexcept { return parent; }
|
||||
[[nodiscard]] std::vector<SHSceneNode*>& GetChildren () noexcept { return children; }
|
||||
[[nodiscard]] EntityID GetEntityID () const noexcept;
|
||||
[[nodiscard]] SHSceneNode* GetParent () const noexcept;
|
||||
[[nodiscard]] std::vector<SHSceneNode*>& GetChildren () noexcept;
|
||||
|
||||
[[nodiscard]] SHSceneNode* GetChild (EntityID childID) const noexcept;
|
||||
|
||||
|
@ -101,7 +101,7 @@ namespace SHADE
|
|||
/* Getter Functions */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
||||
[[nodiscard]] SHSceneNode* GetRoot () const noexcept;
|
||||
[[nodiscard]] const SHSceneNode* GetRoot () const noexcept;
|
||||
[[nodiscard]] SHSceneNode* GetNode (EntityID entityID) const noexcept;
|
||||
[[nodiscard]] SHSceneNode* GetParent (EntityID entityID) const noexcept;
|
||||
[[nodiscard]] SHSceneNode* GetChild (EntityID entityID, SHSceneNode* childNode) const noexcept;
|
||||
|
|
Loading…
Reference in New Issue