From 6270e1c73b3358d8769e5ade414a8eab4889cdae Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Fri, 20 Sep 2024 23:05:50 +0300 Subject: [PATCH 1/2] add Popup::CloseEvent --- loader/include/Geode/ui/Popup.hpp | 61 ++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/loader/include/Geode/ui/Popup.hpp b/loader/include/Geode/ui/Popup.hpp index 3d1ee99d5..c9e6f3be8 100644 --- a/loader/include/Geode/ui/Popup.hpp +++ b/loader/include/Geode/ui/Popup.hpp @@ -6,8 +6,59 @@ #include namespace geode { - template + template class Popup : public FLAlertLayer { + public: + /** + * Event posted when this popup is being closed + */ + class CloseEvent final : public ::geode::Event { + private: + class Impl final { + private: + Popup* popup; + friend class CloseEvent; + }; + std::shared_ptr m_impl; + + friend class Popup; + + CloseEvent(Popup* popup) : m_impl(std::make_shared()) { + m_impl->popup = popup; + } + + public: + Popup* getPopup() const { + return m_impl->popup; + } + }; + class CloseEventFilter final : public ::geode::EventFilter { + public: + using Callback = void(CloseEvent*); + + private: + class Impl final { + private: + Popup* popup; + friend class CloseEventFilter; + }; + std::shared_ptr m_impl; + + friend class Popup; + + CloseEventFilter(Popup* popup) : m_impl(std::make_shared()) { + m_impl->popup = popup; + } + + public: + ListenerResult handle(utils::MiniFunction fn, CloseEvent* event) { + if (event->getPopup() == m_impl->popup) { + fn(event); + } + return ListenerResult::Propagate; + } + }; + protected: cocos2d::CCSize m_size; cocos2d::extension::CCScale9Sprite* m_bgSprite; @@ -115,6 +166,7 @@ namespace geode { } virtual void onClose(cocos2d::CCObject*) { + CloseEvent(this).post(); this->setKeypadEnabled(false); this->setTouchEnabled(false); this->removeFromParentAndCleanup(true); @@ -158,6 +210,13 @@ namespace geode { spr->setAnchorPoint(orig->getAnchorPoint()); m_closeBtn->setContentSize(origSize); } + + /** + * Returns an event filter that listens for when this popup is closed + */ + CloseEventFilter listenForClose() { + return CloseEventFilter(this); + } }; GEODE_DLL FLAlertLayer* createQuickPopup( From dc8d271ff1e9a87c23916b1f29cde7b80c0bbe4f Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Sat, 21 Sep 2024 15:33:59 +0300 Subject: [PATCH 2/2] add openSettingsPopup overload that returns the created popup --- loader/include/Geode/ui/GeodeUI.hpp | 10 ++++++++++ loader/src/ui/GeodeUI.cpp | 8 +++++++- loader/src/ui/mods/settings/ModSettingsPopup.cpp | 4 ++-- loader/src/ui/mods/settings/ModSettingsPopup.hpp | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/loader/include/Geode/ui/GeodeUI.hpp b/loader/include/Geode/ui/GeodeUI.hpp index 024173185..2d9ba1adb 100644 --- a/loader/include/Geode/ui/GeodeUI.hpp +++ b/loader/include/Geode/ui/GeodeUI.hpp @@ -141,6 +141,16 @@ namespace geode { * Open the settings popup for a mod (if it has any settings) */ GEODE_DLL void openSettingsPopup(Mod* mod); + /** + * Open the settings popup for a mod (if it has any settings) + * @param mod Mod the open the popup for + * @param disableGeodeTheme If false, the popup follows the user's chosen + * theme options. If true, the popup is always in the GD theme (not Geode's + * dark purple colors) + * @returns A pointer to the created Popup, or null if the mod has no + * settings + */ + GEODE_DLL Popup* openSettingsPopup(Mod* mod, bool disableGeodeTheme); /** * Create a default logo sprite */ diff --git a/loader/src/ui/GeodeUI.cpp b/loader/src/ui/GeodeUI.cpp index a15881520..9c1dce655 100644 --- a/loader/src/ui/GeodeUI.cpp +++ b/loader/src/ui/GeodeUI.cpp @@ -159,9 +159,15 @@ void geode::openChangelogPopup(Mod* mod) { } void geode::openSettingsPopup(Mod* mod) { + openSettingsPopup(mod, true); +} +Popup* geode::openSettingsPopup(Mod* mod, bool disableGeodeTheme) { if (mod->hasSettings()) { - ModSettingsPopup::create(mod)->show(); + auto popup = ModSettingsPopup::create(mod, disableGeodeTheme); + popup->show(); + return popup; } + return nullptr; } class ModLogoSprite : public CCNode { diff --git a/loader/src/ui/mods/settings/ModSettingsPopup.cpp b/loader/src/ui/mods/settings/ModSettingsPopup.cpp index e5ff34119..5048f8b07 100644 --- a/loader/src/ui/mods/settings/ModSettingsPopup.cpp +++ b/loader/src/ui/mods/settings/ModSettingsPopup.cpp @@ -345,9 +345,9 @@ void ModSettingsPopup::onClose(CCObject* sender) { GeodePopup::onClose(sender); } -ModSettingsPopup* ModSettingsPopup::create(Mod* mod) { +ModSettingsPopup* ModSettingsPopup::create(Mod* mod, bool forceDisableTheme) { auto ret = new ModSettingsPopup(); - if (ret->init(440, 280, mod)) { + if (ret->init(440, 280, mod, GeodePopupStyle::Default, forceDisableTheme)) { ret->autorelease(); return ret; } diff --git a/loader/src/ui/mods/settings/ModSettingsPopup.hpp b/loader/src/ui/mods/settings/ModSettingsPopup.hpp index 60662e2a3..c8cd14b5e 100644 --- a/loader/src/ui/mods/settings/ModSettingsPopup.hpp +++ b/loader/src/ui/mods/settings/ModSettingsPopup.hpp @@ -33,5 +33,5 @@ class ModSettingsPopup : public GeodePopup { void onClearSearch(CCObject*); public: - static ModSettingsPopup* create(Mod* mod); + static ModSettingsPopup* create(Mod* mod, bool forceDisableTheme = false); };