Merge branch 'main' of https://github.com/geode-sdk/geode into main

This commit is contained in:
HJfod 2024-02-16 23:11:27 +02:00
commit d0ed9844be
2 changed files with 55 additions and 0 deletions

View file

@ -737,6 +737,30 @@ namespace geode::cocos {
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
* 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
*/
cocos2d::CCNode* getChildBySpriteFrameName(cocos2d::CCNode* parent, const char* name);
/**
* Checks if a given file exists in CCFileUtils
* search paths.

View file

@ -339,6 +339,37 @@ std::shared_ptr<WeakRefController> WeakRefPool::manage(CCObject* obj) {
return m_pool.at(obj);
}
bool geode::cocos::isSpriteFrameName(CCNode* node, const char* name) {
auto cache = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name);
if (!cache) return false;
auto* texture = cache->getTexture();
auto rect = cache->getRect();
if (auto* spr = typeinfo_cast<CCSprite*>(node)) {
if (spr->getTexture() == texture && spr->getTextureRect() == rect) {
return true;
}
} else if (auto* btn = typeinfo_cast<CCMenuItemSprite*>(node)) {
auto* img = btn->getNormalImage();
if (auto* spr = typeinfo_cast<CCSprite*>(img)) {
if (spr->getTexture() == texture && spr->getTextureRect() == rect) {
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;
}
CCRect geode::cocos::calculateNodeCoverage(std::vector<CCNode*> const& nodes) {
CCRect coverage;
for (auto child : nodes) {