From 3cd538562cbb6a5152678d869317a07bd8dfeb1c Mon Sep 17 00:00:00 2001 From: Cocoa Date: Tue, 20 Sep 2022 19:32:28 +0800 Subject: [PATCH] More Scene Graph Fixes --- SHADE_Engine/src/Scene/SHSceneGraph.cpp | 42 +++++++++++++++---------- SHADE_Engine/src/Scene/SHSceneManager.h | 2 +- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/SHADE_Engine/src/Scene/SHSceneGraph.cpp b/SHADE_Engine/src/Scene/SHSceneGraph.cpp index 305c3fc0..821388b0 100644 --- a/SHADE_Engine/src/Scene/SHSceneGraph.cpp +++ b/SHADE_Engine/src/Scene/SHSceneGraph.cpp @@ -155,7 +155,7 @@ namespace SHADE return root; SHLOG_WARNING("Scene has no root object!") - return nullptr; + return nullptr; } SHSceneNode* SHSceneGraph::GetNode(EntityID entityID) const noexcept @@ -284,12 +284,17 @@ namespace SHADE void SHSceneNode::SetParent(SHSceneNode* parentNode) noexcept { if (parentNode == nullptr) + { SHLOG_WARNING("Removing Entity {}'s parent", entityID) + } // Handle self assignment if (parentNode == parent) return; + if (parent) + parent->RemoveChild(this); + parent = parentNode; // Update parent's children parent->AddChild(this); @@ -339,7 +344,7 @@ namespace SHADE return; } - auto PARENT_ITER = entityNodeMap.find(entityID); + auto PARENT_ITER = entityNodeMap.find(parent); if (PARENT_ITER == entityNodeMap.end()) { SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to parent to Entity {}", parent, entityID) @@ -384,24 +389,21 @@ namespace SHADE } //////////////////////////////////////// - SHSceneNode* removedChild = nullptr; - const auto ENTITY_MATCH = [&](SHSceneNode* node) + auto childIter = std::find_if(children.begin(), children.end(), [&](SHSceneNode* node) { - if (node->GetEntityID() == childID) - { - removedChild = node; - return true; - } + return node->GetEntityID() == childID; + }); + if (childIter == children.end()) + { + SHLOG_WARNING("Unable to remove Entity {} from Entity {} since it is not it's child!", childID, entityID) return false; - }; + } - children.end() = std::remove_if(children.begin(), children.end(), ENTITY_MATCH); + (*childIter)->parent = nullptr; + childIter = children.erase(childIter); - if (removedChild) - removedChild->parent = nullptr; - - return removedChild != nullptr; + return true; } bool SHSceneNode::RemoveChild(SHSceneNode* childToRemove) noexcept @@ -415,7 +417,14 @@ namespace SHADE } //////////////////////////////////////// - children.end() = std::remove(children.begin(), children.end(), childToRemove); + auto childIter = std::find(children.begin(), children.end(), childToRemove); + if (childIter == children.end()) + { + SHLOG_WARNING("Unable to remove Entity {} from Entity {} since it is not it's child!", childToRemove->entityID, entityID) + return false; + } + + childIter = children.erase(childIter); childToRemove->parent = nullptr; return true; @@ -429,7 +438,6 @@ namespace SHADE children.clear(); } - SHSceneNode* SHSceneGraph::AddNode(EntityID entityID, SHSceneNode* parent) { //////////////////////////////////////// diff --git a/SHADE_Engine/src/Scene/SHSceneManager.h b/SHADE_Engine/src/Scene/SHSceneManager.h index bdd8f596..72db1274 100644 --- a/SHADE_Engine/src/Scene/SHSceneManager.h +++ b/SHADE_Engine/src/Scene/SHSceneManager.h @@ -20,7 +20,7 @@ namespace SHADE { - class SHSceneManager + class SH_API SHSceneManager { private: //boolean to check if the scene has been changed