findFirstChildRecursive put inside utils because its very useful

This commit is contained in:
camila314 2022-11-18 23:04:14 -06:00
parent 446f550b04
commit 06d69b1db5

View file

@ -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.