Add MenuItemSprite

This commit is contained in:
altalk23 2023-09-20 17:07:25 +03:00
parent 34398b86e9
commit 152dc7edcf
6 changed files with 220 additions and 1 deletions

View file

@ -264,6 +264,11 @@ class cocos2d::CCEaseOut {
static cocos2d::CCEaseOut* create(cocos2d::CCActionInterval*, float) = mac 0x2a1b70;
}
[[link(win)]]
class cocos2d::CCEaseBounceOut {
static cocos2d::CCEaseBounceOut* create(cocos2d::CCActionInterval*) = mac 0x2a3b40;
}
[[link(win)]]
class cocos2d::CCEGLView {
CCEGLView();

View file

@ -0,0 +1,47 @@
#pragma once
#include <cocos2d.h>
#include "../utils/MiniFunction.hpp"
#pragma warning(disable : 4275)
namespace geode {
class GEODE_DLL MenuItemSprite : public cocos2d::CCMenuItemSprite {
protected:
MenuItemSprite();
~MenuItemSprite() override;
public:
static MenuItemSprite* create(
cocos2d::CCNode* normalSprite, cocos2d::CCNode* disabledSprite,
cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callback
);
static MenuItemSprite* create(
cocos2d::CCNode* normalSprite,
cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callback
);
bool init(
cocos2d::CCNode* normalSprite, cocos2d::CCNode* disabledSprite,
cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callback
);
void runActionOnSprite(cocos2d::CCNode* sprite, cocos2d::CCAction* generator);
void runActionOnSprites(utils::MiniFunction<cocos2d::CCAction*()> generator);
utils::MiniFunction<cocos2d::CCAction*()> m_selectedAction;
utils::MiniFunction<cocos2d::CCAction*()> m_unselectedAction;
utils::MiniFunction<cocos2d::CCAction*()> m_activateAction;
void activate() override;
void selected() override;
void unselected() override;
void setImage(cocos2d::CCNode* image);
void setNormalImage(cocos2d::CCNode* image) override;
void setDisabledImage(cocos2d::CCNode* image) override;
void setSelectedImage(cocos2d::CCNode* image) override;
};
}

View file

@ -0,0 +1,43 @@
#pragma once
#include <cocos2d.h>
#include "MenuItemSprite.hpp"
#pragma warning(disable : 4275)
namespace geode {
class GEODE_DLL MenuItemToggle : public cocos2d::CCMenuItem {
protected:
MenuItemSprite* m_offButton;
MenuItemSprite* m_onButton;
bool m_toggled;
MenuItemToggle();
~MenuItemToggle() override;
public:
static MenuItemToggle* create(
cocos2d::CCNode* onSprite, cocos2d::CCNode* offSprite,
cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callback
);
bool init(
cocos2d::CCNode* onSprite, cocos2d::CCNode* offSprite,
cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callback
);
void setOffButton(MenuItemSprite* button);
void setOnButton(MenuItemSprite* button);
MenuItemSprite* getOffButton() const;
MenuItemSprite* getOnButton() const;
bool isToggled() const;
void toggle(bool enabled);
void setScale(float scale) override;
void activate() override;
void selected() override;
void unselected() override;
void setEnabled(bool enabled) override;
};
}

View file

@ -123,6 +123,30 @@ namespace geode::utils {
return *this;
}
template <class Callable>
requires(MiniFunctionCallable<Callable, Ret, Args...> && !std::is_same_v<std::decay_t<Callable>, MiniFunction<FunctionType>>)
MiniFunction& operator=(Callable&& func) {
delete m_state;
m_state = new MiniFunctionState<std::decay_t<Callable>, Ret, Args...>(std::forward<Callable>(func));
return *this;
}
template <class FunctionPointer>
requires(!MiniFunctionCallable<FunctionPointer, Ret, Args...> && std::is_pointer_v<FunctionPointer> && std::is_function_v<std::remove_pointer_t<FunctionPointer>>)
MiniFunction& operator=(FunctionPointer func) {
delete m_state;
m_state = new MiniFunctionStatePointer<FunctionPointer, Ret, Args...>(func);
return *this;
}
template <class MemberFunctionPointer>
requires(std::is_member_function_pointer_v<MemberFunctionPointer>)
MiniFunction& operator=(MemberFunctionPointer func) {
delete m_state;
m_state = new MiniFunctionStateMemberPointer<MemberFunctionPointer, Ret, Args...>(func);
return *this;
}
Ret operator()(Args... args) const {
if (!m_state) return Ret();
return m_state->call(args...);

View file

@ -16,6 +16,8 @@
#include <Geode/utils/string.hpp>
#include <Geode/utils/ranges.hpp>
#include <Geode/ui/MenuItemSprite.hpp>
#define FTS_FUZZY_MATCH_IMPLEMENTATION
#include <Geode/external/fts/fts_fuzzy_match.h>
@ -259,7 +261,7 @@ bool ModListLayer::init() {
m_topMenu = CCMenu::create();
// add back button
auto backBtn = CCMenuItemSpriteExtra::create(
auto backBtn = MenuItemSprite::create(
CCSprite::createWithSpriteFrameName("GJ_arrow_01_001.png"), this,
menu_selector(ModListLayer::onExit)
);

View file

@ -0,0 +1,98 @@
#include <Geode/ui/MenuItemSprite.hpp>
using namespace geode::prelude;
MenuItemSprite* MenuItemSprite::create(
cocos2d::CCNode* normalSprite,
cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callback
) {
return MenuItemSprite::create(normalSprite, nullptr, target, callback);
}
MenuItemSprite* MenuItemSprite::create(
cocos2d::CCNode* normalSprite, cocos2d::CCNode* disabledSprite,
cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callback
) {
auto ret = new MenuItemSprite();
if (ret && ret->init(normalSprite, disabledSprite, target, callback)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
MenuItemSprite::MenuItemSprite() {}
MenuItemSprite::~MenuItemSprite() {
}
void MenuItemSprite::setImage(cocos2d::CCNode* image) {
if (image) {
image->setAnchorPoint({ 0.5f, 0.5f });
image->setPosition(image->getContentSize() / 2);
}
}
// why are the members private like what
void MenuItemSprite::setNormalImage(cocos2d::CCNode* image) {
CCMenuItemSprite::setNormalImage(image);
this->setImage(image);
}
void MenuItemSprite::setDisabledImage(cocos2d::CCNode* image) {
CCMenuItemSprite::setDisabledImage(image);
this->setImage(image);
}
void MenuItemSprite::setSelectedImage(cocos2d::CCNode* image) {
CCMenuItemSprite::setSelectedImage(image);
this->setImage(image);
}
bool MenuItemSprite::init(
cocos2d::CCNode* normalSprite, cocos2d::CCNode* disabledSprite,
cocos2d::CCObject* target, cocos2d::SEL_MenuHandler callback
) {
if (!CCMenuItemSprite::initWithNormalSprite(normalSprite, disabledSprite, nullptr, target, callback)) return false;
m_selectedAction = []() -> cocos2d::CCAction* {
return cocos2d::CCEaseBounceOut::create(cocos2d::CCScaleTo::create(0.3f, 1.26f));
};
m_unselectedAction = []() -> cocos2d::CCAction* {
return cocos2d::CCEaseBounceOut::create(cocos2d::CCScaleTo::create(0.4f, 1.0f));
};
m_activateAction = []() -> cocos2d::CCAction* {
return cocos2d::CCScaleTo::create(0.f, 1.0f);
};
return true;
}
void MenuItemSprite::runActionOnSprite(cocos2d::CCNode* sprite, cocos2d::CCAction* generator) {
if (sprite) {
sprite->stopAllActions();
if (generator) sprite->runAction(generator);
}
}
void MenuItemSprite::runActionOnSprites(MiniFunction<cocos2d::CCAction*()> generator) {
this->runActionOnSprite(this->getNormalImage(), generator());
this->runActionOnSprite(this->getDisabledImage(), generator());
this->runActionOnSprite(this->getSelectedImage(), generator());
}
void MenuItemSprite::activate() {
this->runActionOnSprites(m_activateAction);
CCMenuItem::activate();
}
void MenuItemSprite::selected() {
if (!m_bEnabled) return;
CCMenuItem::selected();
this->runActionOnSprites(m_selectedAction);
}
void MenuItemSprite::unselected() {
if (!m_bEnabled) return;
CCMenuItem::unselected();
this->runActionOnSprites(m_unselectedAction);
}