From d1053b168826b4c04326e78bb87ea07f7678cc5c Mon Sep 17 00:00:00 2001 From: dankmeme01 <42031238+dankmeme01@users.noreply.github.com> Date: Sat, 19 Oct 2024 21:29:24 +0200 Subject: [PATCH] add CallFuncExt util --- loader/include/Geode/utils/cocos.hpp | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/loader/include/Geode/utils/cocos.hpp b/loader/include/Geode/utils/cocos.hpp index 6eb5a13e..c103c571 100644 --- a/loader/include/Geode/utils/cocos.hpp +++ b/loader/include/Geode/utils/cocos.hpp @@ -1363,6 +1363,40 @@ namespace geode::cocos { } }; + // CCCallFunc alternative that accepts a lambda (or any function object) + template + class CallFuncExtImpl : public cocos2d::CCActionInstant { + public: + static CallFuncExtImpl* create(const F& func) { + auto ret = new CallFuncExtImpl; + ret->m_func = func; + ret->autorelease(); + return ret; + } + + static CallFuncExtImpl* create(F&& func) { + auto ret = new CallFuncExtImpl; + ret->m_func = std::move(func); + ret->autorelease(); + return ret; + } + + private: + F m_func; + + void update(float) override { + if (m_func) this->m_func(); + } + }; + + // small hack to allow template deduction + struct CallFuncExt { + template + static CallFuncExtImpl* create(F&& func) { + return CallFuncExtImpl::create(std::forward(func)); + } + }; + void GEODE_DLL handleTouchPriorityWith(cocos2d::CCNode* node, int priority, bool force = false); void GEODE_DLL handleTouchPriority(cocos2d::CCNode* node, bool force = false); }