From 0089d13f7af782893288ab19d5362c1f34c5cc90 Mon Sep 17 00:00:00 2001 From: dankmeme01 <42031238+dankmeme01@users.noreply.github.com> Date: Sun, 13 Oct 2024 13:11:57 +0200 Subject: [PATCH] add getChildOfType to CCNode --- .../include/Geode/cocos/base_nodes/CCNode.h | 34 +++++++++++++++++++ loader/include/Geode/utils/cocos.hpp | 30 +--------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/loader/include/Geode/cocos/base_nodes/CCNode.h b/loader/include/Geode/cocos/base_nodes/CCNode.h index 647a57e7..45c919a7 100644 --- a/loader/include/Geode/cocos/base_nodes/CCNode.h +++ b/loader/include/Geode/cocos/base_nodes/CCNode.h @@ -39,6 +39,7 @@ #include "../include/CCProtocols.h" #include "Layout.hpp" #include "../../loader/Event.hpp" +#include #ifndef GEODE_IS_MEMBER_TEST #include @@ -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 + 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(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(this->getChildren()->objectAtIndex(i)); + if (obj != nullptr) { + if (indexCounter == index) { + return obj; + } + ++indexCounter; + } + } + } + + return nullptr; + } /// @{ /// @name Shader Program diff --git a/loader/include/Geode/utils/cocos.hpp b/loader/include/Geode/utils/cocos.hpp index 7d0cef73..5412f112 100644 --- a/loader/include/Geode/utils/cocos.hpp +++ b/loader/include/Geode/utils/cocos.hpp @@ -622,35 +622,7 @@ namespace geode::cocos { */ template 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(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(node->getChildren()->objectAtIndex(i)); - if (obj != nullptr) { - if (indexCounter == index) { - return obj; - } - ++indexCounter; - } - } - } - - return nullptr; + return node->getChildOfType(index); } /**