Skip to content

Commit

Permalink
editor+runtime: fix slow hit-testing
Browse files Browse the repository at this point in the history
turns out forEachChild + recursion is a bad combo. Recurse on just the first level of the hierarchy. I also made sure that the `forEachChild` in both Dart and C++ would do the same thing, and have the same signature.

Diffs=
90da85fbb4 editor+runtime: fix slow hit-testing (#8907)

Co-authored-by: Susan Wang <[email protected]>
  • Loading branch information
susan101566 and susan101566 committed Jan 22, 2025
1 parent f87e58e commit 11e9c65
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b03d365c93d0d52f0f05ae598deacbf7dd330a53
90da85fbb4151a7a4a1c30cf2f6738929debe925
8 changes: 5 additions & 3 deletions include/rive/container_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class ContainerComponent : public ContainerComponentBase
virtual void addChild(Component* component);
bool collapse(bool value) override;

// Returns true if it searched through all of its children. predicate can
// return false to stop searching.
// Returns whether predicate returns true for the current Component.
bool forAll(std::function<bool(Component*)> predicate);
bool forEachChild(std::function<bool(Component*)> predicate);

// Recursively descend onto all the children in the hierarchy tree.
// If predicate returns false, it won't recurse down a particular branch.
void forEachChild(std::function<bool(Component*)> predicate);

private:
std::vector<Component*> m_children;
Expand Down
2 changes: 1 addition & 1 deletion src/animation/state_machine_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ void StateMachineInstance::addToHitLookup(
hitLookup,
listenerGroup,
isOpaque);
return true;
return false;
});
return;
}
Expand Down
10 changes: 4 additions & 6 deletions src/container_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,17 @@ bool ContainerComponent::forAll(std::function<bool(Component*)> predicate)
return true;
}

bool ContainerComponent::forEachChild(std::function<bool(Component*)> predicate)
void ContainerComponent::forEachChild(std::function<bool(Component*)> predicate)
{
for (Component* child : m_children)
{
if (!predicate(child))
{
return false;
continue;
}
if (child->is<ContainerComponent>() &&
!child->as<ContainerComponent>()->forEachChild(predicate))
if (child->is<ContainerComponent>())
{
return false;
child->as<ContainerComponent>()->forEachChild(predicate);
}
}
return true;
}

0 comments on commit 11e9c65

Please sign in to comment.