diff --git a/loader/include/Geode/utils/cocos.hpp b/loader/include/Geode/utils/cocos.hpp index 99a21e69..9f02a0e8 100644 --- a/loader/include/Geode/utils/cocos.hpp +++ b/loader/include/Geode/utils/cocos.hpp @@ -737,6 +737,42 @@ namespace geode::cocos { return nullptr; } + /** + * Get the first child that has the given sprite frame + * name either in the sprite or in the sprite inside + * the button. + * + * @param parent Parent node to search in + * @param name Name of the sprite frame to search for + * @returns Child with the given sprite frame name, or + * nullptr if there is none + */ + template + Type getChildBySpriteFrameName(cocos2d::CCNode* parent, const char* name) { + auto cache = cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name); + if (!cache) return nullptr; + + auto* texture = cache->getTexture(); + auto rect = cache->getRect(); + + for (int i = 0; i < parent->getChildrenCount(); ++i) { + auto* child = parent->getChildren()->objectAtIndex(i); + if (auto* spr = typeinfo_cast(child)) { + if (spr->getTexture() == texture && spr->getTextureRect() == rect) { + return static_cast(spr); + } + } else if (auto* btn = typeinfo_cast(child)) { + auto* img = btn->getNormalImage(); + if (auto* spr = typeinfo_cast(img)) { + if (spr->getTexture() == texture && spr->getTextureRect() == rect) { + return static_cast(btn); + } + } + } + } + return nullptr; + } + /** * Checks if a given file exists in CCFileUtils * search paths.