Added Event for Scene Graph Change Parent

This commit is contained in:
Diren D Bharwani 2022-10-20 17:47:32 +08:00
parent 6172ff35b6
commit 5a0b34ceea
3 changed files with 34 additions and 13 deletions

View File

@ -5,8 +5,9 @@ typedef uint32_t SHEventIdentifier;
typedef uint32_t SHEventHandle; typedef uint32_t SHEventHandle;
//Add your event identifiers here: //Add your event identifiers here:
constexpr SHEventIdentifier SH_EXAMPLE_EVENT{0}; constexpr SHEventIdentifier SH_EXAMPLE_EVENT { 0 };
constexpr SHEventIdentifier SH_ENTITY_DESTROYED_EVENT{ 1 }; constexpr SHEventIdentifier SH_ENTITY_DESTROYED_EVENT { 1 };
constexpr SHEventIdentifier SH_ENTITY_CREATION_EVENT{ 2 }; constexpr SHEventIdentifier SH_ENTITY_CREATION_EVENT { 2 };
constexpr SHEventIdentifier SH_COMPONENT_ADDED_EVENT{ 3 }; constexpr SHEventIdentifier SH_COMPONENT_ADDED_EVENT { 3 };
constexpr SHEventIdentifier SH_COMPONENT_REMOVED_EVENT{ 4 }; constexpr SHEventIdentifier SH_COMPONENT_REMOVED_EVENT { 4 };
constexpr SHEventIdentifier SH_SCENEGRAPH_CHANGE_PARENT_EVENT { 5 };

View File

@ -15,6 +15,7 @@
// Project Headers // Project Headers
#include "ECS_Base/Managers/SHEntityManager.h" #include "ECS_Base/Managers/SHEntityManager.h"
#include "Events/SHEventManager.hpp"
#include "Tools/SHLogger.h" #include "Tools/SHLogger.h"
#include "Tools/SHException.h" #include "Tools/SHException.h"
@ -364,10 +365,18 @@ namespace SHADE
} }
//////////////////////////////////////// ////////////////////////////////////////
const SHSceneGraphChangeParentEvent EVENT_DATA
{
.oldParentID = NODE_ITER->second->GetParent()->GetEntityID(),
.newParentID = parent->GetEntityID()
};
if (parent == nullptr) if (parent == nullptr)
parent = root; parent = root;
NODE_ITER->second->SetParent(parent); NODE_ITER->second->SetParent(parent);
SHEventManager::BroadcastEvent<SHSceneGraphChangeParentEvent>(EVENT_DATA, SH_SCENEGRAPH_CHANGE_PARENT_EVENT);
} }
void SHSceneGraph::SetParent(EntityID entityID, EntityID parent) const noexcept void SHSceneGraph::SetParent(EntityID entityID, EntityID parent) const noexcept
@ -401,8 +410,16 @@ namespace SHADE
} }
//////////////////////////////////////// ////////////////////////////////////////
const SHSceneGraphChangeParentEvent EVENT_DATA
{
.oldParentID = NODE_ITER->second->GetParent()->GetEntityID(),
.newParentID = parent
};
SHSceneNode* currentNode = NODE_ITER->second; SHSceneNode* currentNode = NODE_ITER->second;
currentNode->SetParent(PARENT_ITER->second); currentNode->SetParent(PARENT_ITER->second);
SHEventManager::BroadcastEvent<SHSceneGraphChangeParentEvent>(EVENT_DATA, SH_SCENEGRAPH_CHANGE_PARENT_EVENT);
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -563,7 +580,7 @@ namespace SHADE
ReleaseNode(node); ReleaseNode(node);
} }
void SHSceneGraph::Traverse (const UnaryPredicate& predicate) const void SHSceneGraph::Traverse (const UnaryFunction& predicate) const
{ {
TraverseAndInvokePredicate(root, predicate); TraverseAndInvokePredicate(root, predicate);
} }
@ -602,7 +619,7 @@ namespace SHADE
delete node; delete node;
} }
void SHSceneGraph::TraverseAndInvokePredicate(const SHSceneNode* node, const UnaryPredicate& predicate) void SHSceneGraph::TraverseAndInvokePredicate(const SHSceneNode* node, const UnaryFunction& predicate)
{ {
for (auto* child : node->children) for (auto* child : node->children)
{ {

View File

@ -97,9 +97,8 @@ namespace SHADE
/* Type Definitions */ /* Type Definitions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
using EntityNodeMap = std::unordered_map<EntityID, SHSceneNode*>; using EntityNodeMap = std::unordered_map<EntityID, SHSceneNode*>;
using UnaryFunction = std::function<void(SHSceneNode*)>;
using UnaryPredicate = std::function<void(SHSceneNode*)>;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -143,8 +142,7 @@ namespace SHADE
bool RemoveNode (SHSceneNode* nodeToRemove) noexcept; bool RemoveNode (SHSceneNode* nodeToRemove) noexcept;
void Reset () noexcept; void Reset () noexcept;
void Traverse (const UnaryPredicate& predicate) const; void Traverse (const UnaryFunction& predicate) const;
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -160,8 +158,13 @@ namespace SHADE
SHSceneNode* AllocateNode (EntityID entityID); SHSceneNode* AllocateNode (EntityID entityID);
void ReleaseNode (SHSceneNode* node) noexcept; void ReleaseNode (SHSceneNode* node) noexcept;
static void TraverseAndInvokePredicate (const SHSceneNode* node, const UnaryPredicate& predicate); static void TraverseAndInvokePredicate (const SHSceneNode* node, const UnaryFunction& predicate);
}; };
struct SHSceneGraphChangeParentEvent
{
EntityID oldParentID;
EntityID newParentID;
};
} // namespace SHADE } // namespace SHADE