add getChildOfType to CCNode

This commit is contained in:
dankmeme01 2024-10-13 13:11:57 +02:00
parent 5f8d272816
commit 0089d13f7a
2 changed files with 35 additions and 29 deletions

View file

@ -39,6 +39,7 @@
#include "../include/CCProtocols.h"
#include "Layout.hpp"
#include "../../loader/Event.hpp"
#include <Geode/utils/casts.hpp>
#ifndef GEODE_IS_MEMBER_TEST
#include <matjson.hpp>
@ -1124,6 +1125,39 @@ public:
GEODE_DLL geode::EventListenerProtocol* getEventListener(std::string const& id);
GEODE_DLL size_t getEventListenerCount();
template <class T = CCNode>
T* getChildOfType(int index) {
size_t indexCounter = 0;
if (this->getChildrenCount() == 0) return nullptr;
// start from end for negative index
if (index < 0) {
index = -index - 1;
for (size_t i = this->getChildrenCount() - 1; i >= 0; i--) {
auto obj = geode::cast::typeinfo_cast<T*>(this->getChildren()->objectAtIndex(i));
if (obj != nullptr) {
if (indexCounter == index) {
return obj;
}
++indexCounter;
}
if (i == 0) break;
}
}
else {
for (size_t i = 0; i < this->getChildrenCount(); i++) {
auto obj = geode::cast::typeinfo_cast<T*>(this->getChildren()->objectAtIndex(i));
if (obj != nullptr) {
if (indexCounter == index) {
return obj;
}
++indexCounter;
}
}
}
return nullptr;
}
/// @{
/// @name Shader Program
/**

View file

@ -622,35 +622,7 @@ namespace geode::cocos {
*/
template <class Type = cocos2d::CCNode>
static Type* getChildOfType(cocos2d::CCNode* node, int index) {
size_t indexCounter = 0;
if (node->getChildrenCount() == 0) return nullptr;
// start from end for negative index
if (index < 0) {
index = -index - 1;
for (size_t i = node->getChildrenCount() - 1; i >= 0; i--) {
auto obj = cast::typeinfo_cast<Type*>(node->getChildren()->objectAtIndex(i));
if (obj != nullptr) {
if (indexCounter == index) {
return obj;
}
++indexCounter;
}
if (i == 0) break;
}
}
else {
for (size_t i = 0; i < node->getChildrenCount(); i++) {
auto obj = cast::typeinfo_cast<Type*>(node->getChildren()->objectAtIndex(i));
if (obj != nullptr) {
if (indexCounter == index) {
return obj;
}
++indexCounter;
}
}
}
return nullptr;
return node->getChildOfType<Type>(index);
}
/**