Skip to content

Commit

Permalink
scene::forEachTransformable takes node by reference
Browse files Browse the repository at this point in the history
Not sure why this was ever using a shared_ptr, since the INode was never
stored: the only thing the function ever did with it was immediately
dereference it. Switching to a simple reference avoids the need for several
shared_from_this() calls in Doom3GroupNode.
  • Loading branch information
Matthew Mott committed Nov 24, 2021
1 parent a64abd1 commit 3b80608
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
38 changes: 22 additions & 16 deletions libs/transformlib.h
Expand Up @@ -21,23 +21,29 @@ namespace scene
{

/**
* Visit each transformable that is child of the given node with the given functor.
* @brief Visit each Transformable child node
*
* @tparam Func
* Functor object accepting a single parameter consisting of an ITransformable&.
*
* @param node
* Node to start traversal from.
*
* @param functor
* Functor to invoke.
*/
inline void foreachTransformable(const scene::INodePtr& node, const std::function<void(ITransformable&)>& functor)
template<typename Func>
void forEachTransformable(const INode& node, Func functor)
{
if (!node) return;

node->foreachNode([&] (const scene::INodePtr& child)->bool
{
ITransformablePtr transformable = Node_getTransformable(child);

if (transformable)
{
functor(*transformable);
}

return true;
});
node.foreachNode(
[&](const scene::INodePtr& child) -> bool
{
ITransformablePtr transformable = Node_getTransformable(child);
if (transformable)
functor(*transformable);

return true;
}
);
}

}
10 changes: 5 additions & 5 deletions radiantcore/entity/doom3group/Doom3GroupNode.cpp
Expand Up @@ -370,7 +370,7 @@ void Doom3GroupNode::_onTransformationChanged()
// If this is a container, pass the call to the children and leave the entity unharmed
if (!isModel())
{
scene::foreachTransformable(shared_from_this(), [] (ITransformable& child)
scene::forEachTransformable(*this, [] (ITransformable& child)
{
child.revertTransform();
});
Expand Down Expand Up @@ -491,7 +491,7 @@ void Doom3GroupNode::rotate(const Quaternion& rotation)
if (!isModel())
{
// Rotate all child nodes too
scene::foreachTransformable(shared_from_this(), [&] (ITransformable& child)
scene::forEachTransformable(*this, [&] (ITransformable& child)
{
child.setType(TRANSFORM_PRIMITIVE);
child.setRotation(rotation);
Expand All @@ -512,7 +512,7 @@ void Doom3GroupNode::scale(const Vector3& scale)
if (!isModel())
{
// Scale all child nodes too
scene::foreachTransformable(shared_from_this(), [&] (ITransformable& child)
scene::forEachTransformable(*this, [&] (ITransformable& child)
{
child.setType(TRANSFORM_PRIMITIVE);
child.setScale(scale);
Expand Down Expand Up @@ -554,7 +554,7 @@ void Doom3GroupNode::freezeTransform()

if (!isModel())
{
scene::foreachTransformable(shared_from_this(), [] (ITransformable& child)
scene::forEachTransformable(*this, [] (ITransformable& child)
{
child.freezeTransform();
});
Expand Down Expand Up @@ -694,7 +694,7 @@ void Doom3GroupNode::translateChildren(const Vector3& childTranslation)
if (inScene())
{
// Translate all child nodes too
scene::foreachTransformable(shared_from_this(), [&] (ITransformable& child)
scene::forEachTransformable(*this, [&] (ITransformable& child)
{
child.setType(TRANSFORM_PRIMITIVE);
child.setTranslation(childTranslation);
Expand Down

0 comments on commit 3b80608

Please sign in to comment.