mirror of
https://github.com/geode-sdk/geode.git
synced 2025-04-10 12:04:36 -04:00
findFirstChildRecursive put inside utils because its very useful
This commit is contained in:
parent
446f550b04
commit
06d69b1db5
1 changed files with 26 additions and 0 deletions
|
@ -168,6 +168,32 @@ namespace geode::cocos {
|
|||
*/
|
||||
GEODE_DLL cocos2d::CCNode* getChildByTagRecursive(cocos2d::CCNode* node, int tag);
|
||||
|
||||
/**
|
||||
* Get first node that conforms to the predicate
|
||||
* by traversing children recursively
|
||||
*
|
||||
* @param node Parent node
|
||||
* @param predicate Predicate used to evaluate nodes
|
||||
* @return Child node if one is found, or null if
|
||||
* there is none
|
||||
*/
|
||||
template <class Type = cocos2d::CCNode>
|
||||
Type* findFirstChildRecursive(cocos2d::CCNode* node, std::function<bool(Type*)> predicate) {
|
||||
if (predicate(node))return node;
|
||||
|
||||
auto children = node->getChildren();
|
||||
if (!children) return nullptr;
|
||||
|
||||
for (int i = 0; i < children->count(); ++i) {
|
||||
auto newParent = reinterpret_cast<cocos2d::CCNode*>(children->objectAtIndex(i));
|
||||
auto child = findFirstChildRecursive(newParent, predicate);
|
||||
if (child)
|
||||
return child;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given file exists in CCFileUtils
|
||||
* search paths.
|
||||
|
|
Loading…
Add table
Reference in a new issue