mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-14 19:15:05 -05:00
add getChildOfType to CCNode
This commit is contained in:
parent
5f8d272816
commit
0089d13f7a
2 changed files with 35 additions and 29 deletions
34
loader/include/Geode/cocos/base_nodes/CCNode.h
vendored
34
loader/include/Geode/cocos/base_nodes/CCNode.h
vendored
|
@ -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>
|
||||
|
@ -1123,6 +1124,39 @@ public:
|
|||
GEODE_DLL void removeEventListener(std::string const& id);
|
||||
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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue