Skip to content

Commit 097de0c

Browse files
committed
make ColorPickPopup use a callback instead of a delegate
1 parent aba7ff0 commit 097de0c

File tree

4 files changed

+12
-24
lines changed

4 files changed

+12
-24
lines changed

loader/include/Geode/ui/ColorPickPopup.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
#include "TextInput.hpp"
55
#include "../loader/Event.hpp"
66
#include <Geode/binding/TextInputDelegate.hpp>
7+
#include <Geode/utils/function.hpp>
78

89
namespace geode {
910
class ColorPickPopup;
1011

11-
class GEODE_DLL ColorPickPopupDelegate {
12-
public:
13-
virtual void updateColor(cocos2d::ccColor4B const& color) {}
14-
};
15-
1612
class GEODE_DLL ColorPickPopup :
1713
public Popup<cocos2d::ccColor4B const&, bool>,
1814
public cocos2d::extension::ColorPickerDelegate,
@@ -48,6 +44,6 @@ namespace geode {
4844
static ColorPickPopup* create(cocos2d::ccColor4B const& color);
4945

5046
void setColorTarget(cocos2d::CCSprite* spr);
51-
void setDelegate(ColorPickPopupDelegate* delegate);
47+
void setCallback(geode::Function<void(cocos2d::ccColor4B const&)> callback);
5248
};
5349
}

loader/src/loader/SettingNodeV3.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,9 @@ void Color3BSettingNodeV3::updateState(CCNode* invoker) {
568568

569569
void Color3BSettingNodeV3::onSelectColor(CCObject*) {
570570
auto popup = ColorPickPopup::create(this->getValue());
571-
popup->setDelegate(this);
571+
popup->setCallback([this](ccColor4B const& color) { this->setValue(to3B(color), nullptr); });
572572
popup->show();
573573
}
574-
void Color3BSettingNodeV3::updateColor(ccColor4B const& color) {
575-
this->setValue(to3B(color), nullptr);
576-
}
577-
578574
Color3BSettingNodeV3* Color3BSettingNodeV3::create(std::shared_ptr<Color3BSettingV3> setting, float width) {
579575
auto ret = new Color3BSettingNodeV3();
580576
if (ret->init(setting, width)) {
@@ -616,12 +612,9 @@ void Color4BSettingNodeV3::updateState(CCNode* invoker) {
616612

617613
void Color4BSettingNodeV3::onSelectColor(CCObject*) {
618614
auto popup = ColorPickPopup::create(this->getValue());
619-
popup->setDelegate(this);
615+
popup->setCallback([this](ccColor4B const& color) { this->setValue(color, nullptr); });
620616
popup->show();
621617
}
622-
void Color4BSettingNodeV3::updateColor(ccColor4B const& color) {
623-
this->setValue(color, nullptr);
624-
}
625618

626619
Color4BSettingNodeV3* Color4BSettingNodeV3::create(std::shared_ptr<Color4BSettingV3> setting, float width) {
627620
auto ret = new Color4BSettingNodeV3();

loader/src/loader/SettingNodeV3.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,29 +274,27 @@ class FileSettingNodeV3 : public SettingValueNodeV3<FileSettingV3> {
274274
static FileSettingNodeV3* create(std::shared_ptr<FileSettingV3> setting, float width);
275275
};
276276

277-
class Color3BSettingNodeV3 : public SettingValueNodeV3<Color3BSettingV3>, public ColorPickPopupDelegate {
277+
class Color3BSettingNodeV3 : public SettingValueNodeV3<Color3BSettingV3> {
278278
protected:
279279
CCMenuItemSpriteExtra* m_colorBtn;
280280
ColorChannelSprite* m_colorSprite;
281281

282282
bool init(std::shared_ptr<Color3BSettingV3> setting, float width);
283283
void updateState(CCNode* invoker) override;
284284
void onSelectColor(CCObject*);
285-
void updateColor(ccColor4B const& color) override;
286285

287286
public:
288287
static Color3BSettingNodeV3* create(std::shared_ptr<Color3BSettingV3> setting, float width);
289288
};
290289

291-
class Color4BSettingNodeV3 : public SettingValueNodeV3<Color4BSettingV3>, public ColorPickPopupDelegate {
290+
class Color4BSettingNodeV3 : public SettingValueNodeV3<Color4BSettingV3> {
292291
protected:
293292
CCMenuItemSpriteExtra* m_colorBtn;
294293
ColorChannelSprite* m_colorSprite;
295294

296295
bool init(std::shared_ptr<Color4BSettingV3> setting, float width);
297296
void updateState(CCNode* invoker) override;
298297
void onSelectColor(CCObject*);
299-
void updateColor(ccColor4B const& color) override;
300298

301299
public:
302300
static Color4BSettingNodeV3* create(std::shared_ptr<Color4BSettingV3> setting, float width);

loader/src/ui/nodes/ColorPickPopup.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <Geode/binding/SliderThumb.hpp>
55
#include <Geode/ui/ColorPickPopup.hpp>
66
#include <Geode/utils/cocos.hpp>
7+
#include <Geode/utils/function.hpp>
78
#include <charconv>
89
#include <clocale>
910
#include <Geode/loader/Mod.hpp>
@@ -21,7 +22,7 @@ class ColorPickPopup::Impl final {
2122
TextInput* m_bInput;
2223
TextInput* m_hexInput;
2324
TextInput* m_opacityInput = nullptr;
24-
ColorPickPopupDelegate* m_delegate = nullptr;
25+
geode::Function<void(cocos2d::ccColor4B const&)> m_callback;
2526
cocos2d::CCSprite* m_newColorSpr;
2627
CCMenuItemSpriteExtra* m_resetBtn;
2728
};
@@ -370,8 +371,8 @@ void ColorPickPopup::onReset(CCObject*) {
370371
}
371372

372373
void ColorPickPopup::onClose(CCObject* sender) {
373-
if (m_impl->m_delegate) {
374-
m_impl->m_delegate->updateColor(m_impl->m_color);
374+
if (m_impl->m_callback) {
375+
m_impl->m_callback(m_impl->m_color);
375376
}
376377
Popup::onClose(sender);
377378
}
@@ -424,8 +425,8 @@ void ColorPickPopup::colorValueChanged(ccColor3B color) {
424425
this->updateState(m_impl->m_picker);
425426
}
426427

427-
void ColorPickPopup::setDelegate(ColorPickPopupDelegate* delegate) {
428-
m_impl->m_delegate = delegate;
428+
void ColorPickPopup::setCallback(geode::Function<void(cocos2d::ccColor4B const&)> callback) {
429+
m_impl->m_callback = std::move(callback);
429430
}
430431

431432
void ColorPickPopup::setColorTarget(cocos2d::CCSprite* spr) {

0 commit comments

Comments
 (0)