Added active in hierarchy property to scene nodes

This commit is contained in:
Diren D Bharwani 2022-12-12 17:07:18 +08:00
parent 970b9e7ec0
commit 9b17c62b1d
4 changed files with 82 additions and 39 deletions

View File

@ -186,6 +186,27 @@ namespace SHADE
return NODE_ITER->second->GetChildren(); return NODE_ITER->second->GetChildren();
} }
bool SHSceneGraph::IsActive(EntityID entityID) const noexcept
{
////////////////////////////////////////
// Error handling
if (!SHEntityManager::IsValidEID(entityID))
{
SHLOG_ERROR("Entity {} is invalid!", entityID)
return false;
}
const auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_ERROR("Entity {} cannot be found in the scene!", entityID)
return false;
}
////////////////////////////////////////
return NODE_ITER->second->IsActive();
}
bool SHSceneGraph::IsActiveInHierarchy(EntityID entityID) const noexcept bool SHSceneGraph::IsActiveInHierarchy(EntityID entityID) const noexcept
{ {
//////////////////////////////////////// ////////////////////////////////////////
@ -204,24 +225,7 @@ namespace SHADE
} }
//////////////////////////////////////// ////////////////////////////////////////
// Recurse up the tree until the root. If any parent is inactive, this node is inactive in the hierarchy. return NODE_ITER->second->IsActiveInHierarchy();
const SHSceneNode* PARENT_NODE = NODE_ITER->second->parent;
while (PARENT_NODE->GetEntityID() != root->GetEntityID())
{
if (!PARENT_NODE->IsActive())
return false;
if (!PARENT_NODE->parent)
{
SHLOGV_ERROR("Entity {}'s node that is not the root has no parent!", PARENT_NODE->GetEntityID())
return false;
}
PARENT_NODE = PARENT_NODE->parent;
}
return true;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -58,6 +58,7 @@ namespace SHADE
[[nodiscard]] SHSceneNode* GetChild (EntityID entityID, EntityID childEntityID) const noexcept; [[nodiscard]] SHSceneNode* GetChild (EntityID entityID, EntityID childEntityID) const noexcept;
[[nodiscard]] const std::vector<SHSceneNode*>& GetChildren (EntityID entityID) const noexcept; [[nodiscard]] const std::vector<SHSceneNode*>& GetChildren (EntityID entityID) const noexcept;
[[nodiscard]] bool IsActive (EntityID entityID) const noexcept;
[[nodiscard]] bool IsActiveInHierarchy (EntityID entityID) const noexcept; [[nodiscard]] bool IsActiveInHierarchy (EntityID entityID) const noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -23,24 +23,27 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHSceneNode::SHSceneNode(EntityID eid, SHSceneNode* parent) noexcept SHSceneNode::SHSceneNode(EntityID eid, SHSceneNode* parent) noexcept
: active { true } : active { true }
, entityID { eid } , isActiveInHierarchy { true }
, parent { parent } , entityID { eid }
, parent { parent }
{} {}
SHSceneNode::SHSceneNode(const SHSceneNode& rhs) noexcept SHSceneNode::SHSceneNode(const SHSceneNode& rhs) noexcept
: active { rhs.active } : active { rhs.active }
, entityID { rhs.entityID } , isActiveInHierarchy { rhs.isActiveInHierarchy }
, parent { rhs.parent } , entityID { rhs.entityID }
, parent { rhs.parent }
{ {
std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children)); std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children));
} }
SHSceneNode::SHSceneNode(SHSceneNode&& rhs) noexcept SHSceneNode::SHSceneNode(SHSceneNode&& rhs) noexcept
: active { rhs.active } : active { rhs.active }
, entityID { rhs.entityID } , isActiveInHierarchy { rhs.isActiveInHierarchy }
, parent { rhs.parent } , entityID { rhs.entityID }
, parent { rhs.parent }
{ {
std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children)); std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children));
} }
@ -50,9 +53,10 @@ namespace SHADE
if (this == &rhs) if (this == &rhs)
return *this; return *this;
active = rhs.active; active = rhs.active;
entityID = rhs.entityID; isActiveInHierarchy = rhs.isActiveInHierarchy;
parent = rhs.parent; entityID = rhs.entityID;
parent = rhs.parent;
children.clear(); children.clear();
std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children)); std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children));
@ -62,9 +66,10 @@ namespace SHADE
SHSceneNode& SHSceneNode::operator=(SHSceneNode&& rhs) noexcept SHSceneNode& SHSceneNode::operator=(SHSceneNode&& rhs) noexcept
{ {
active = rhs.active; active = rhs.active;
entityID = rhs.entityID; isActiveInHierarchy = rhs.isActiveInHierarchy;
parent = rhs.parent; entityID = rhs.entityID;
parent = rhs.parent;
children.clear(); children.clear();
std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children)); std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children));
@ -81,6 +86,11 @@ namespace SHADE
return active; return active;
} }
bool SHSceneNode::IsActiveInHierarchy() const noexcept
{
return isActiveInHierarchy;
}
EntityID SHSceneNode::GetEntityID() const noexcept EntityID SHSceneNode::GetEntityID() const noexcept
{ {
return entityID; return entityID;
@ -132,7 +142,27 @@ namespace SHADE
void SHSceneNode::SetActive(bool newActiveState) noexcept void SHSceneNode::SetActive(bool newActiveState) noexcept
{ {
active = newActiveState; active = newActiveState;
isActiveInHierarchy = newActiveState;
// Recurse down the children to set the active in hierarchy state
recursiveSetActiveInHierarchy(active, children);
}
/*-----------------------------------------------------------------------------------*/
/* Private Member Function Definitions */
/*-----------------------------------------------------------------------------------*/
void SHSceneNode::recursiveSetActiveInHierarchy(bool activeInHierarchy, const std::vector<SHSceneNode*>& childrenToSet) noexcept
{
if (childrenToSet.empty())
return;
for (auto* child : childrenToSet)
{
child->isActiveInHierarchy = activeInHierarchy;
recursiveSetActiveInHierarchy(activeInHierarchy, child->children);
}
} }
} // namespace SHADE } // namespace SHADE

View File

@ -43,7 +43,7 @@ namespace SHADE
/* Constructors & Destructor */ /* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
~SHSceneNode () = default; ~SHSceneNode () noexcept = default;
SHSceneNode (EntityID eid, SHSceneNode* parent = nullptr) noexcept; SHSceneNode (EntityID eid, SHSceneNode* parent = nullptr) noexcept;
SHSceneNode (const SHSceneNode& rhs) noexcept; SHSceneNode (const SHSceneNode& rhs) noexcept;
@ -55,10 +55,11 @@ namespace SHADE
/* Getter Functions */ /* Getter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] bool IsActive () const noexcept; [[nodiscard]] bool IsActive () const noexcept;
[[nodiscard]] EntityID GetEntityID () const noexcept; [[nodiscard]] bool IsActiveInHierarchy () const noexcept;
[[nodiscard]] SHSceneNode* GetParent () const noexcept; [[nodiscard]] EntityID GetEntityID () const noexcept;
[[nodiscard]] const std::vector<SHSceneNode*>& GetChildren () const noexcept; [[nodiscard]] SHSceneNode* GetParent () const noexcept;
[[nodiscard]] const std::vector<SHSceneNode*>& GetChildren () const noexcept;
[[nodiscard]] SHSceneNode* GetChild (EntityID childID) const noexcept; [[nodiscard]] SHSceneNode* GetChild (EntityID childID) const noexcept;
@ -74,9 +75,16 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
bool active; bool active;
bool isActiveInHierarchy;
EntityID entityID; EntityID entityID;
SHSceneNode* parent; SHSceneNode* parent;
std::vector<SHSceneNode*> children; std::vector<SHSceneNode*> children;
/*---------------------------------------------------------------------------------*/
/* Member Functions */
/*---------------------------------------------------------------------------------*/
static void recursiveSetActiveInHierarchy(bool activeInHierarchy, const std::vector<SHSceneNode*>& childrenToSet) noexcept;
}; };
} // namespace SHADE } // namespace SHADE