isSpriteFrameName

This commit is contained in:
altalk23 2024-02-16 23:58:40 +03:00
parent 07dd379611
commit eea35568fe
2 changed files with 31 additions and 13 deletions

View file

@ -737,6 +737,18 @@ namespace geode::cocos {
return nullptr; return nullptr;
} }
/**
* Checks if a node has the given sprite frame
* name either in the sprite or in the sprite inside
* the button.
*
* @param node Node to check
* @param name Name of the sprite frame to search for
* @returns True if the node has the given sprite frame
* name
*/
bool isSpriteFrameName(cocos2d::CCNode* node, const char* name);
/** /**
* Get the first child that has the given sprite frame * Get the first child that has the given sprite frame
* name either in the sprite or in the sprite inside * name either in the sprite or in the sprite inside

View file

@ -339,27 +339,33 @@ std::shared_ptr<WeakRefController> WeakRefPool::manage(CCObject* obj) {
return m_pool.at(obj); return m_pool.at(obj);
} }
CCNode* geode::cocos::getChildBySpriteFrameName(CCNode* parent, const char* name) { bool geode::cocos::isSpriteFrameName(CCNode* node, const char* name) {
auto cache = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name); auto cache = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name);
if (!cache) return nullptr; if (!cache) return false;
auto* texture = cache->getTexture(); auto* texture = cache->getTexture();
auto rect = cache->getRect(); auto rect = cache->getRect();
for (int i = 0; i < parent->getChildrenCount(); ++i) { if (auto* spr = typeinfo_cast<CCSprite*>(node)) {
auto* child = parent->getChildren()->objectAtIndex(i);
if (auto* spr = typeinfo_cast<CCSprite*>(child)) {
if (spr->getTexture() == texture && spr->getTextureRect() == rect) { if (spr->getTexture() == texture && spr->getTextureRect() == rect) {
return spr; return true;
} }
} else if (auto* btn = typeinfo_cast<CCMenuItemSprite*>(child)) { } else if (auto* btn = typeinfo_cast<CCMenuItemSprite*>(node)) {
auto* img = btn->getNormalImage(); auto* img = btn->getNormalImage();
if (auto* spr = typeinfo_cast<CCSprite*>(img)) { if (auto* spr = typeinfo_cast<CCSprite*>(img)) {
if (spr->getTexture() == texture && spr->getTextureRect() == rect) { if (spr->getTexture() == texture && spr->getTextureRect() == rect) {
return btn; return true;
} }
} }
} }
return false;
}
CCNode* geode::cocos::getChildBySpriteFrameName(CCNode* parent, const char* name) {
for (auto child : CCArrayExt<CCNode*>(parent->getChildren())) {
if (::isSpriteFrameName(static_cast<CCNode*>(child), name)) {
return child;
}
} }
return nullptr; return nullptr;
} }