change how forAll iterates over children forAll was currently applying the predicate multiple times per element: - calling super on each component applying the predicate once - applying it again per child when calling forAll in a container component. Diffs= 8fbe13788 change how forAll iterates over children (#7546) Co-authored-by: hernan <hernan@rive.app>
diff --git a/.rive_head b/.rive_head index 172e250..026d8df 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -93fb6eb8365dba5c803d3132f7526dc1c888293e +8fbe13788fa0bd1050ce0b044beb210e1b14ab6a
diff --git a/include/rive/component.hpp b/include/rive/component.hpp index ba06a69..7488dca 100644 --- a/include/rive/component.hpp +++ b/include/rive/component.hpp
@@ -57,8 +57,6 @@ { return (m_Dirt & ComponentDirt::Collapsed) == ComponentDirt::Collapsed; } - - virtual bool forAll(std::function<bool(Component*)> predicate); }; } // namespace rive
diff --git a/include/rive/container_component.hpp b/include/rive/container_component.hpp index e340a17..04cf246 100644 --- a/include/rive/container_component.hpp +++ b/include/rive/container_component.hpp
@@ -15,7 +15,8 @@ // Returns true if it searched through all of its children. predicate can // return false to stop searching. - bool forAll(std::function<bool(Component*)> predicate) override; + bool forAll(std::function<bool(Component*)> predicate); + bool forEachChild(std::function<bool(Component*)> predicate); private: std::vector<Component*> m_children;
diff --git a/src/animation/state_machine_instance.cpp b/src/animation/state_machine_instance.cpp index 51f01f1..c6f7316 100644 --- a/src/animation/state_machine_instance.cpp +++ b/src/animation/state_machine_instance.cpp
@@ -667,9 +667,9 @@ { auto listener = machine->listener(i); auto target = m_artboardInstance->resolve(listener->targetId()); - if (target != nullptr && target->is<Component>()) + if (target != nullptr && target->is<ContainerComponent>()) { - target->as<Component>()->forAll([&](Component* component) { + target->as<ContainerComponent>()->forAll([&](Component* component) { if (component->is<Shape>()) { HitShape* hitShape;
diff --git a/src/component.cpp b/src/component.cpp index b7fa038..8765b9e 100644 --- a/src/component.cpp +++ b/src/component.cpp
@@ -89,6 +89,4 @@ onDirty(m_Dirt); m_DependencyHelper.onComponentDirty(this); return true; -} - -bool Component::forAll(std::function<bool(Component*)> predicate) { return predicate(this); } \ No newline at end of file +} \ No newline at end of file
diff --git a/src/container_component.cpp b/src/container_component.cpp index af03348..2ed1bfe 100644 --- a/src/container_component.cpp +++ b/src/container_component.cpp
@@ -18,17 +18,24 @@ bool ContainerComponent::forAll(std::function<bool(Component*)> predicate) { - if (!Super::forAll(predicate)) + if (!predicate(this)) { return false; } + forEachChild(predicate); + return true; +} + +bool ContainerComponent::forEachChild(std::function<bool(Component*)> predicate) +{ for (Component* child : m_children) { if (!predicate(child)) { return false; } - if (child->is<ContainerComponent>() && !child->as<ContainerComponent>()->forAll(predicate)) + if (child->is<ContainerComponent>() && + !child->as<ContainerComponent>()->forEachChild(predicate)) { return false; }