Skip to content

Commit 584bcfa

Browse files
committed
use callbacks in menu ui
1 parent 3bc1097 commit 584bcfa

11 files changed

Lines changed: 108 additions & 166 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ build-*/
6161

6262
# CLion
6363
.idea/
64-
/cmake-build-*/
64+
/cmake-build-*/
65+
66+
# uh
67+
*.ini

src/hooks/PlayerObject/RandomGamemode.cpp

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/menu/OptionCategoryItem.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
#include <Geode/Geode.hpp>
44

5-
using namespace geode::prelude;
6-
75
namespace horrible {
8-
// Event for option toggles
9-
struct CategoryEvent final : Event<CategoryEvent, bool(std::string_view, bool)> {
10-
using Event::Event;
11-
};
12-
13-
class OptionCategoryItem final : public CCMenu {
6+
class OptionCategoryItem final : public cocos2d::CCMenu {
147
private:
158
class Impl;
169
std::unique_ptr<Impl> m_impl;
1710

11+
using Callback = geode::Function<void(std::string_view, bool)>;
12+
1813
protected:
1914
OptionCategoryItem();
2015
~OptionCategoryItem();
2116

2217
void onToggle(CCObject* sender);
2318

24-
bool init(CCSize const& size, std::string category);
19+
bool init(cocos2d::CCSize const& size, std::string category);
2520

2621
public:
27-
static OptionCategoryItem* create(CCSize const& size, std::string category);
22+
static OptionCategoryItem* create(cocos2d::CCSize const& size, std::string category);
23+
24+
void setToggleCallback(Callback&& callback);
25+
void setToggled(bool on);
26+
27+
geode::ZStringView getCategory() const noexcept;
2828
};
2929
};

src/menu/OptionItem.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,27 @@
44

55
#include <Geode/Geode.hpp>
66

7-
using namespace geode::prelude;
8-
using namespace horrible;
9-
107
namespace horrible {
11-
// Event for pin toggles
12-
struct PinEvent final : Event<PinEvent, bool()> {
13-
using Event::Event;
14-
};
15-
16-
class OptionItem final : public CCMenu, private FLAlertLayerProtocol {
8+
class OptionItem final : public cocos2d::CCMenu, private FLAlertLayerProtocol {
179
private:
1810
class Impl;
1911
std::unique_ptr<Impl> m_impl;
2012

13+
using Callback = Function<void()>;
14+
2115
protected:
2216
OptionItem();
2317
~OptionItem();
2418

25-
void onToggle(CCObject*);
26-
void onPin(CCObject* sender);
19+
void onToggle(cocos2d::CCObject*);
20+
void onPin(cocos2d::CCObject* sender);
2721

28-
bool init(CCSize const& size, Option option, bool devMode);
22+
bool init(cocos2d::CCSize const& size, Option option, bool devMode);
2923

3024
public:
31-
static OptionItem* create(CCSize const& size, Option option, bool devMode);
25+
static OptionItem* create(cocos2d::CCSize const& size, Option option, bool devMode);
26+
27+
void setPinCallback(Callback&& callback);
3228

3329
Option const& getOption() const noexcept;
3430
bool isCompatible() const noexcept;

src/menu/OptionMenu.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
#include <Geode/Geode.hpp>
88

9-
using namespace geode::prelude;
10-
119
namespace horrible {
1210
class OptionMenu final : public Popup {
1311
private:

src/menu/OptionMenuButton.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
#include <Geode/Geode.hpp>
44

5-
using namespace geode::prelude;
6-
75
namespace horrible {
8-
class OptionMenuButton final : public CCLayer {
6+
class OptionMenuButton final : public cocos2d::CCLayer {
97
private:
108
class Impl;
119
std::unique_ptr<Impl> m_impl;
@@ -31,14 +29,14 @@ namespace horrible {
3129
void onEnter() override;
3230

3331
void setScale(float scale) override;
34-
void setPosition(CCPoint const& position) override;
32+
void setPosition(cocos2d::CCPoint const& position) override;
3533

3634
void setTheme(std::string theme);
3735

3836
// CCLayer targeted touch listener methods
39-
bool ccTouchBegan(CCTouch* touch, CCEvent* event) override;
40-
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event) override;
41-
virtual void ccTouchEnded(CCTouch* touch, CCEvent* event) override;
37+
bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) override;
38+
virtual void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) override;
39+
virtual void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) override;
4240

4341
int64_t getOpacitySetting() const noexcept;
4442
float getScaleSetting() const noexcept;

src/menu/SettingV3.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
#include <Geode/loader/SettingV3.hpp>
66

7-
using namespace geode::prelude;
8-
97
namespace horrible {
10-
class HorribleSettingV3 final : public SettingV3 {
8+
class HorribleSettingV3 final : public geode::SettingV3 {
119
public:
12-
static Result<std::shared_ptr<SettingV3>> parse(
10+
static geode::Result<std::shared_ptr<SettingV3>> parse(
1311
std::string key,
1412
std::string modID,
1513
matjson::Value const& json);
@@ -21,10 +19,10 @@ namespace horrible {
2119

2220
void reset() override;
2321

24-
SettingNodeV3* createNode(float width) override;
22+
geode::SettingNodeV3* createNode(float width) override;
2523
};
2624

27-
class HorribleSettingNodeV3 final : public SettingNodeV3 {
25+
class HorribleSettingNodeV3 final : public geode::SettingNodeV3 {
2826
private:
2927
class Impl;
3028
std::unique_ptr<Impl> m_impl;
@@ -33,7 +31,7 @@ namespace horrible {
3331
HorribleSettingNodeV3();
3432
~HorribleSettingNodeV3();
3533

36-
void updateState(CCNode* invoker) override;
34+
void updateState(cocos2d::CCNode* invoker) override;
3735
void onCommit() override;
3836
void onResetToDefault() override;
3937

src/menu/src/OptionCategoryItem.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class OptionCategoryItem::Impl final {
1111
std::string category = ""; // The category name
1212

1313
CCMenuItemToggler* toggler = nullptr; // The toggler for the option
14+
15+
Callback toggleCallback = nullptr; // Callback for when the category is toggled
1416
};
1517

1618
OptionCategoryItem::OptionCategoryItem() : m_impl(std::make_unique<Impl>()) {};
@@ -67,19 +69,25 @@ bool OptionCategoryItem::init(CCSize const& size, std::string category) {
6769

6870
addChild(nameLabel);
6971

70-
addEventListener(
71-
CategoryEvent(),
72-
[this](std::string_view category, bool enabled) {
73-
if (m_impl->toggler) {
74-
if (category != m_impl->category) m_impl->toggler->toggle(false);
75-
};
76-
});
77-
7872
return true;
7973
};
8074

8175
void OptionCategoryItem::onToggle(CCObject* sender) {
82-
if (m_impl->toggler) CategoryEvent().send(m_impl->category, !m_impl->toggler->isOn());
76+
if (m_impl->toggler) {
77+
if (m_impl->toggleCallback) m_impl->toggleCallback(m_impl->category, !m_impl->toggler->isOn());
78+
};
79+
};
80+
81+
void OptionCategoryItem::setToggleCallback(Callback&& callback) {
82+
m_impl->toggleCallback = std::move(callback);
83+
};
84+
85+
void OptionCategoryItem::setToggled(bool on) {
86+
if (m_impl->toggler) m_impl->toggler->toggle(on);
87+
};
88+
89+
ZStringView OptionCategoryItem::getCategory() const noexcept {
90+
return m_impl->category;
8391
};
8492

8593
OptionCategoryItem* OptionCategoryItem::create(CCSize const& size, std::string category) {

src/menu/src/OptionItem.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class OptionItem::Impl final {
1111
public:
1212
bool compatible = false; // If this option is compatible with the current platform
1313

14-
// The option
15-
Option option;
14+
Option option; // A copy of the option
1615

1716
CCMenuItemToggler* toggler = nullptr; // The toggler for the option
17+
CCNode* newContainer = nullptr; // Container for the "New!" label and icon
1818

19-
CCNode* newContainer = nullptr;
19+
Callback pinCallback = nullptr; // Callback for when the option is pinned or unpinned
2020

2121
// Save the current state of the toggler as the option state
2222
void saveTogglerState() {
@@ -294,12 +294,16 @@ void OptionItem::onToggle(CCObject*) {
294294
void OptionItem::onPin(CCObject* sender) {
295295
if (auto pinBtn = typeinfo_cast<CCMenuItemToggler*>(sender)) {
296296
options::set(m_impl->option.getID(), options::isEnabled(m_impl->option.getID()), !pinBtn->isToggled(), true);
297-
PinEvent().send();
298297

298+
if (m_impl->pinCallback) m_impl->pinCallback();
299299
m_impl->clearNewLabel();
300300
};
301301
};
302302

303+
void OptionItem::setPinCallback(Callback&& callback) {
304+
m_impl->pinCallback = std::move(callback);
305+
};
306+
303307
Option const& OptionItem::getOption() const noexcept {
304308
return m_impl->option;
305309
};

0 commit comments

Comments
 (0)