add CCMenuItemExt for lambda callback util needs

This commit is contained in:
altalk23 2024-06-01 15:38:21 +03:00
parent 43f646164f
commit de733173c0
2 changed files with 155 additions and 0 deletions

View file

@ -2,6 +2,7 @@
#include <Geode/DefaultInclude.hpp>
#include <cocos2d.h>
#include <cocos-ext.h>
namespace geode {
/**

View file

@ -8,6 +8,8 @@
#include <functional>
#include <type_traits>
#include "../loader/Event.hpp"
#include <Geode/binding/CCMenuItemSpriteExtra.hpp>
#include <Geode/binding/CCMenuItemToggler.hpp>
#include "MiniFunction.hpp"
// support converting ccColor3B / ccColor4B to / from json
@ -1186,6 +1188,158 @@ namespace geode::cocos {
}
};
struct CCMenuItemExt {
private:
template <class Node>
class LambdaCallback : public cocos2d::CCObject {
public:
utils::MiniFunction<void(Node*)> m_callback;
static LambdaCallback* create(utils::MiniFunction<void(Node*)>&& callback) {
auto ret = new (std::nothrow) LambdaCallback();
if (ret && ret->init(std::forward<std::remove_reference_t<decltype(callback)>>(callback))) {
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
bool init(utils::MiniFunction<void(Node*)>&& callback) {
m_callback = std::forward<std::remove_reference_t<decltype(callback)>>(callback);
return true;
}
void execute(cocos2d::CCNode* node) {
m_callback(static_cast<Node*>(node));
}
};
public:
static cocos2d::CCMenuItem* create(
utils::MiniFunction<void(cocos2d::CCMenuItem*)>&& callback
) {
auto item = cocos2d::CCMenuItem::create();
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
return item;
}
static cocos2d::CCMenuItemSprite* createSprite(
cocos2d::CCNode* normalSprite,
cocos2d::CCNode* selectedSprite,
utils::MiniFunction<void(cocos2d::CCMenuItemSprite*)>&& callback
) {
auto item = cocos2d::CCMenuItemSprite::create(normalSprite, selectedSprite);
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
return item;
}
static cocos2d::CCMenuItemSprite* createSprite(
cocos2d::CCNode* normalSprite,
cocos2d::CCNode* selectedSprite,
cocos2d::CCNode* disabledSprite,
utils::MiniFunction<void(cocos2d::CCMenuItemSprite*)>&& callback
) {
auto item = cocos2d::CCMenuItemSprite::create(normalSprite, selectedSprite, disabledSprite);
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
return item;
}
static CCMenuItemSpriteExtra* createSpriteExtra(
cocos2d::CCNode* normalSprite,
utils::MiniFunction<void(CCMenuItemSpriteExtra*)>&& callback
) {
auto item = CCMenuItemSpriteExtra::create(normalSprite, nullptr, nullptr);
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
return item;
}
static CCMenuItemSpriteExtra* createSpriteExtraWithFilename(
std::string_view normalSpriteName,
float scale,
utils::MiniFunction<void(CCMenuItemSpriteExtra*)>&& callback
) {
auto sprite = cocos2d::CCSprite::create(normalSpriteName.data());
sprite->setScale(scale);
return createSpriteExtra(sprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
}
static CCMenuItemSpriteExtra* createSpriteExtraWithFrameName(
std::string_view normalSpriteName,
float scale,
utils::MiniFunction<void(CCMenuItemSpriteExtra*)>&& callback
) {
auto sprite = cocos2d::CCSprite::createWithSpriteFrameName(normalSpriteName.data());
sprite->setScale(scale);
return createSpriteExtra(sprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
}
static CCMenuItemToggler* createToggler(
cocos2d::CCNode* onSprite,
cocos2d::CCNode* offSprite,
utils::MiniFunction<void(CCMenuItemToggler*)>&& callback
) {
auto item = CCMenuItemToggler::create(onSprite, offSprite, nullptr, nullptr);
assignCallback(item, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
return item;
}
static CCMenuItemToggler* createTogglerWithStandardSprites(
float scale,
utils::MiniFunction<void(CCMenuItemToggler*)>&& callback
) {
auto offSprite = cocos2d::CCSprite::createWithSpriteFrameName("GJ_checkOff_001.png");
auto onSprite = cocos2d::CCSprite::createWithSpriteFrameName("GJ_checkOn_001.png");
offSprite->setScale(scale);
onSprite->setScale(scale);
return createToggler(offSprite, onSprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
}
static CCMenuItemToggler* createTogglerWithFilename(
std::string_view onSpriteName,
std::string_view offSpriteName,
float scale,
utils::MiniFunction<void(CCMenuItemToggler*)>&& callback
) {
auto offSprite = cocos2d::CCSprite::create(offSpriteName.data());
auto onSprite = cocos2d::CCSprite::create(onSpriteName.data());
offSprite->setScale(scale);
onSprite->setScale(scale);
return createToggler(offSprite, onSprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
}
static CCMenuItemToggler* createTogglerWithFrameName(
std::string_view onSpriteName,
std::string_view offSpriteName,
float scale,
utils::MiniFunction<void(CCMenuItemToggler*)>&& callback
) {
auto offSprite = cocos2d::CCSprite::createWithSpriteFrameName(offSpriteName.data());
auto onSprite = cocos2d::CCSprite::createWithSpriteFrameName(onSpriteName.data());
offSprite->setScale(scale);
onSprite->setScale(scale);
return createToggler(offSprite, onSprite, std::forward<std::remove_reference_t<decltype(callback)>>(callback));
}
template <class Node>
static void assignCallback(
cocos2d::CCMenuItem* item,
utils::MiniFunction<void(Node*)>&& callback
) {
auto lambda = LambdaCallback<Node>::create(std::forward<std::remove_reference_t<decltype(callback)>>(callback));
item->setTarget(lambda, menu_selector(LambdaCallback<Node>::execute));
item->setUserObject("lambda-callback", lambda);
}
};
void GEODE_DLL handleTouchPriorityWith(cocos2d::CCNode* node, int priority, bool force = false);
void GEODE_DLL handleTouchPriority(cocos2d::CCNode* node, bool force = false);
}