From ac379e3ef320fa8fbfe592e41500b13306a4424d Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 21 Sep 2022 18:23:01 +0800 Subject: [PATCH] Added method to scene graph to easily traverse and execute a unary function --- SHADE_Engine/src/Scene/SHSceneGraph.cpp | 14 ++++++++++++ SHADE_Engine/src/Scene/SHSceneGraph.h | 30 ++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/SHADE_Engine/src/Scene/SHSceneGraph.cpp b/SHADE_Engine/src/Scene/SHSceneGraph.cpp index 821388b0..f5df85d7 100644 --- a/SHADE_Engine/src/Scene/SHSceneGraph.cpp +++ b/SHADE_Engine/src/Scene/SHSceneGraph.cpp @@ -507,6 +507,11 @@ namespace SHADE ReleaseNode(node); } + void SHSceneGraph::Traverse (const UnaryPredicate& predicate) const + { + TraverseAndInvokePredicate(root, predicate); + } + /*-----------------------------------------------------------------------------------*/ /* Private Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -541,4 +546,13 @@ namespace SHADE delete node; } + void SHSceneGraph::TraverseAndInvokePredicate(const SHSceneNode* node, const UnaryPredicate& predicate) + { + for (auto* child : node->children) + { + predicate(child); + TraverseAndInvokePredicate(child, predicate); + } + } + } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Scene/SHSceneGraph.h b/SHADE_Engine/src/Scene/SHSceneGraph.h index 38fcc414..bdb8f52c 100644 --- a/SHADE_Engine/src/Scene/SHSceneGraph.h +++ b/SHADE_Engine/src/Scene/SHSceneGraph.h @@ -18,12 +18,25 @@ namespace SHADE { + /*-----------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-----------------------------------------------------------------------------------*/ + + class SHSceneGraph; + /*-----------------------------------------------------------------------------------*/ /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ class SH_API SHSceneNode { + private: + /*---------------------------------------------------------------------------------*/ + /* Friends */ + /*---------------------------------------------------------------------------------*/ + + friend class SHSceneGraph; + public: /*---------------------------------------------------------------------------------*/ /* Data Members */ @@ -71,6 +84,10 @@ namespace SHADE void RemoveAllChildren () noexcept; private: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + EntityID entityID; SHSceneNode* parent; std::vector children; @@ -83,7 +100,10 @@ namespace SHADE /* Type Definitions */ /*---------------------------------------------------------------------------------*/ - using EntityNodeMap = std::unordered_map; + using EntityNodeMap = std::unordered_map; + + using UnaryPredicate = void (*)(SHSceneNode*); + /*---------------------------------------------------------------------------------*/ /* Constructors & Destructor */ @@ -124,6 +144,9 @@ namespace SHADE bool RemoveNode (SHSceneNode* nodeToRemove) noexcept; void Reset () noexcept; + void Traverse (const UnaryPredicate& predicate) const; + + private: /*---------------------------------------------------------------------------------*/ /* Data Members */ @@ -136,8 +159,9 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - SHSceneNode* AllocateNode (EntityID entityID); - void ReleaseNode (SHSceneNode* node) noexcept; + SHSceneNode* AllocateNode (EntityID entityID); + void ReleaseNode (SHSceneNode* node) noexcept; + static void TraverseAndInvokePredicate (const SHSceneNode* node, const UnaryPredicate& predicate); };