Skip to content

Commit

Permalink
change how forAll iterates over children
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bodymovin and bodymovin committed Jul 9, 2024
1 parent 85a28fb commit 6600bc3
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
93fb6eb8365dba5c803d3132f7526dc1c888293e
8fbe13788fa0bd1050ce0b044beb210e1b14ab6a
2 changes: 0 additions & 2 deletions include/rive/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class Component : public ComponentBase
{
return (m_Dirt & ComponentDirt::Collapsed) == ComponentDirt::Collapsed;
}

virtual bool forAll(std::function<bool(Component*)> predicate);
};
} // namespace rive

Expand Down
3 changes: 2 additions & 1 deletion include/rive/container_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ContainerComponent : public ContainerComponentBase

// 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;
Expand Down
4 changes: 2 additions & 2 deletions src/animation/state_machine_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,9 @@ StateMachineInstance::StateMachineInstance(const StateMachine* machine,
{
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;
Expand Down
4 changes: 1 addition & 3 deletions src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,4 @@ bool Component::collapse(bool value)
onDirty(m_Dirt);
m_DependencyHelper.onComponentDirty(this);
return true;
}

bool Component::forAll(std::function<bool(Component*)> predicate) { return predicate(this); }
}
11 changes: 9 additions & 2 deletions src/container_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ bool ContainerComponent::collapse(bool value)

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;
}
Expand Down

0 comments on commit 6600bc3

Please sign in to comment.