Skip to content

Commit

Permalink
Merge pull request #257 from adriengivry/fix/hierarchy_parent_node_at…
Browse files Browse the repository at this point in the history
…tach

Fixing Hierarchy stack overflow due to parent node manipulation
  • Loading branch information
maxbrundev authored Oct 17, 2023
2 parents 151972a + 5547c31 commit 86b701b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Sources/Overload/OvCore/include/OvCore/ECS/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ namespace OvCore::ECS
*/
void DetachFromParent();

/**
* Returns true if this actor transform is descendant of the actor
* @param p_actor
*/
bool IsDescendantOf(const Actor* p_actor) const;

/**
* Returns true if the actor has a parent
*/
Expand Down
16 changes: 16 additions & 0 deletions Sources/Overload/OvCore/src/OvCore/ECS/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ void OvCore::ECS::Actor::DetachFromParent()
transform.RemoveParent();
}

bool OvCore::ECS::Actor::IsDescendantOf(const Actor* p_actor) const
{
const Actor* currentParentActor = m_parent;

while (currentParentActor != nullptr)
{
if (currentParentActor == p_actor)
{
return true;
}
currentParentActor = currentParentActor->GetParent();
}

return false;
}

bool OvCore::ECS::Actor::HasParent() const
{
return m_parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void OvCore::Scripting::LuaActorBinder::BindActor(sol::state & p_luaState)
"IsSelfActive", &Actor::IsSelfActive, // TODO: Add to doc
"IsActive", &Actor::IsActive,
"SetActive", &Actor::SetActive,
"IsDescendantOf", &Actor::IsDescendantOf,


/* Components Getters */
Expand Down
9 changes: 5 additions & 4 deletions Sources/Overload/OvEditor/src/OvEditor/Panels/Hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,11 @@ void OvEditor::Panels::Hierarchy::AddActorByInstance(OvCore::ECS::Actor & p_acto
textSelectable.AddPlugin<OvUI::Plugins::DDSource<std::pair<OvCore::ECS::Actor*, OvUI::Widgets::Layout::TreeNode*>>>("Actor", "Attach to...", std::make_pair(&p_actor, &textSelectable));
textSelectable.AddPlugin<OvUI::Plugins::DDTarget<std::pair<OvCore::ECS::Actor*, OvUI::Widgets::Layout::TreeNode*>>>("Actor").DataReceivedEvent += [&p_actor, &textSelectable](std::pair<OvCore::ECS::Actor*, OvUI::Widgets::Layout::TreeNode*> p_element)
{
if (p_element.second->HasParent())
p_element.second->GetParent()->UnconsiderWidget(*p_element.second);

textSelectable.ConsiderWidget(*p_element.second);
if (p_actor.IsDescendantOf(p_element.first))
{
OVLOG_WARNING("Cannot attach \"" + p_element.first->GetName() + "\" to \"" + p_actor.GetName() + "\" because it is a descendant of the latter.");
return;
}

p_element.first->SetParent(p_actor);
};
Expand Down

0 comments on commit 86b701b

Please sign in to comment.