geode/loader/include/Geode/ui/Popup.hpp

166 lines
6.3 KiB
C++
Raw Normal View History

#pragma once
#include <Geode/binding/CCMenuItemSpriteExtra.hpp>
2022-10-30 14:59:20 -04:00
#include <Geode/binding/FLAlertLayer.hpp>
2022-12-13 02:24:03 -05:00
#include <Geode/utils/MiniFunction.hpp>
2024-01-24 03:57:10 -05:00
#include <Geode/utils/cocos.hpp>
namespace geode {
2022-10-30 14:59:20 -04:00
template <typename... InitArgs>
class Popup : public FLAlertLayer {
protected:
cocos2d::CCSize m_size;
cocos2d::extension::CCScale9Sprite* m_bgSprite;
cocos2d::CCLabelBMFont* m_title = nullptr;
CCMenuItemSpriteExtra* m_closeBtn;
2024-01-31 16:11:43 -05:00
bool m_dynamic;
2024-01-24 03:57:10 -05:00
~Popup() override {
cocos2d::CCTouchDispatcher::get()->unregisterForcePrio(this);
}
void registerWithTouchDispatcher() override {
cocos2d::CCTouchDispatcher::get()->addTargetedDelegate(this, -500, true);
}
2024-01-31 16:11:43 -05:00
private:
bool initBase(
float width, float height, InitArgs... args, char const* bg,
cocos2d::CCRect bgRect, bool dynamic
) {
2024-01-31 16:11:43 -05:00
m_dynamic = dynamic;
auto winSize = cocos2d::CCDirector::get()->getWinSize();
m_size = cocos2d::CCSize { width, height };
cocos2d::CCTouchDispatcher::get()->registerForcePrio(this, 2);
if (!this->initWithColor({ 0, 0, 0, 105 })) return false;
m_mainLayer = cocos2d::CCLayer::create();
this->addChild(m_mainLayer);
m_bgSprite = cocos2d::extension::CCScale9Sprite::create(bg, bgRect);
m_bgSprite->setContentSize(m_size);
m_bgSprite->setPosition(winSize.width / 2, winSize.height / 2);
m_mainLayer->addChild(m_bgSprite);
m_buttonMenu = cocos2d::CCMenu::create();
2022-09-01 05:46:37 -04:00
m_buttonMenu->setZOrder(100);
m_mainLayer->addChild(m_buttonMenu);
2024-01-31 16:11:43 -05:00
if (dynamic) {
m_mainLayer->ignoreAnchorPointForPosition(false);
m_mainLayer->setPosition(winSize / 2);
m_mainLayer->setContentSize(m_size);
m_mainLayer->setLayout(
cocos2d::CopySizeLayout::create()
2024-01-31 16:11:43 -05:00
->add(m_buttonMenu)
->add(m_bgSprite)
);
}
this->setTouchEnabled(true);
auto closeSpr = cocos2d::CCSprite::createWithSpriteFrameName("GJ_closeBtn_001.png");
closeSpr->setScale(.8f);
m_closeBtn = CCMenuItemSpriteExtra::create(
closeSpr, this, (cocos2d::SEL_MenuHandler)(&Popup::onClose)
);
2024-01-31 16:11:43 -05:00
if (dynamic) {
m_buttonMenu->addChildAtPosition(m_closeBtn, cocos2d::Anchor::TopLeft, { 3.f, -3.f });
}
else {
m_closeBtn->setPosition(-m_size.width / 2 + 3.f, m_size.height / 2 - 3.f);
m_buttonMenu->addChild(m_closeBtn);
}
if (!setup(std::forward<InitArgs>(args)...)) {
return false;
}
this->setKeypadEnabled(true);
this->setTouchEnabled(true);
return true;
}
2024-01-31 16:11:43 -05:00
protected:
[[deprecated("Use Popup::initAnchored instead, as it has more reasonable menu and layer content sizes")]]
2024-01-31 16:11:43 -05:00
bool init(
float width, float height, InitArgs... args, char const* bg = "GJ_square01.png",
cocos2d::CCRect bgRect = { 0, 0, 80, 80 }
) {
return this->initBase(width, height, std::forward<InitArgs>(args)..., bg, bgRect, false);
}
/**
* Init with AnchorLayout and the content size of `m_buttonMenu` and
* `m_bgSprite` being tied to the size of `m_mainLayer` (rather than
* being the size of the window)
*/
bool initAnchored(
2024-01-31 16:11:43 -05:00
float width, float height, InitArgs... args, char const* bg = "GJ_square01.png",
cocos2d::CCRect bgRect = { 0, 0, 80, 80 }
) {
return this->initBase(width, height, std::forward<InitArgs>(args)..., bg, bgRect, true);
}
virtual bool setup(InitArgs... args) = 0;
void keyDown(cocos2d::enumKeyCodes key) {
if (key == cocos2d::enumKeyCodes::KEY_Escape) return this->onClose(nullptr);
if (key == cocos2d::enumKeyCodes::KEY_Space) return;
return FLAlertLayer::keyDown(key);
}
virtual void onClose(cocos2d::CCObject*) {
this->setKeyboardEnabled(false);
this->removeFromParentAndCleanup(true);
}
void setTitle(
2022-10-25 17:50:33 -04:00
std::string const& title,
const char* font = "goldFont.fnt",
float scale = .7f,
float offset = 20.f
) {
if (m_title) {
2022-10-25 17:50:33 -04:00
m_title->setString(title.c_str());
2024-01-31 16:11:43 -05:00
}
else {
2022-10-25 17:50:33 -04:00
m_title = cocos2d::CCLabelBMFont::create(title.c_str(), font);
2024-01-31 16:11:43 -05:00
m_title->setZOrder(2);
if (m_dynamic) {
m_mainLayer->addChildAtPosition(m_title, cocos2d::Anchor::Top, ccp(0, -offset));
}
else {
auto winSize = cocos2d::CCDirector::get()->getWinSize();
m_title->setPosition(winSize / 2 + ccp(0, m_size.height / 2 - offset));
}
}
m_title->limitLabelWidth(m_size.width - 20.f, scale, .1f);
}
};
GEODE_DLL FLAlertLayer* createQuickPopup(
2022-10-30 14:59:20 -04:00
char const* title, std::string const& content, char const* btn1, char const* btn2,
2022-12-13 02:24:03 -05:00
utils::MiniFunction<void(FLAlertLayer*, bool)> selected, bool doShow = true
);
GEODE_DLL FLAlertLayer* createQuickPopup(
2022-10-30 14:59:20 -04:00
char const* title, std::string const& content, char const* btn1, char const* btn2,
2022-12-13 02:24:03 -05:00
float width, utils::MiniFunction<void(FLAlertLayer*, bool)> selected, bool doShow = true
);
GEODE_DLL FLAlertLayer* createQuickPopup(
char const* title, std::string const& content, char const* btn1, char const* btn2,
utils::MiniFunction<void(FLAlertLayer*, bool)> selected, bool doShow, bool cancelledByEscape
);
GEODE_DLL FLAlertLayer* createQuickPopup(
char const* title, std::string const& content, char const* btn1, char const* btn2,
float width, utils::MiniFunction<void(FLAlertLayer*, bool)> selected, bool doShow, bool cancelledByEscape
);
}