diff --git a/Sources/Overload/OvCore/include/OvCore/ECS/Actor.h b/Sources/Overload/OvCore/include/OvCore/ECS/Actor.h index 249a25b7..6846a949 100644 --- a/Sources/Overload/OvCore/include/OvCore/ECS/Actor.h +++ b/Sources/Overload/OvCore/include/OvCore/ECS/Actor.h @@ -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 */ diff --git a/Sources/Overload/OvCore/src/OvCore/ECS/Actor.cpp b/Sources/Overload/OvCore/src/OvCore/ECS/Actor.cpp index 400fd2b7..ef4d072f 100644 --- a/Sources/Overload/OvCore/src/OvCore/ECS/Actor.cpp +++ b/Sources/Overload/OvCore/src/OvCore/ECS/Actor.cpp @@ -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; diff --git a/Sources/Overload/OvCore/src/OvCore/Scripting/LuaActorBinder.cpp b/Sources/Overload/OvCore/src/OvCore/Scripting/LuaActorBinder.cpp index 2b58b188..69929075 100644 --- a/Sources/Overload/OvCore/src/OvCore/Scripting/LuaActorBinder.cpp +++ b/Sources/Overload/OvCore/src/OvCore/Scripting/LuaActorBinder.cpp @@ -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 */ diff --git a/Sources/Overload/OvEditor/src/OvEditor/Panels/Hierarchy.cpp b/Sources/Overload/OvEditor/src/OvEditor/Panels/Hierarchy.cpp index f89c9255..e65a4691 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Panels/Hierarchy.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Panels/Hierarchy.cpp @@ -282,10 +282,11 @@ void OvEditor::Panels::Hierarchy::AddActorByInstance(OvCore::ECS::Actor & p_acto textSelectable.AddPlugin>>("Actor", "Attach to...", std::make_pair(&p_actor, &textSelectable)); textSelectable.AddPlugin>>("Actor").DataReceivedEvent += [&p_actor, &textSelectable](std::pair 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); };