Skip to content

Commit 3c254d7

Browse files
committed
add mod name to KeybindEditPopup
1 parent 375d5eb commit 3c254d7

File tree

3 files changed

+107
-66
lines changed

3 files changed

+107
-66
lines changed

loader/src/ui/mods/settings/KeybindEditPopup.cpp

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,27 @@ CCNode* createKeybindButton(Keybind const& keybind) {
77
return createGeodeButton(nullptr, keybind.toString(), true);
88
}
99

10-
KeybindEditPopup* KeybindEditPopup::create(ZStringView name, Keybind const& keybind, Function<void(Keybind const&)> callback) {
11-
auto ret = new KeybindEditPopup();
12-
if (ret->init(name, keybind, std::move(callback))) {
13-
ret->autorelease();
14-
return ret;
15-
}
16-
delete ret;
17-
return nullptr;
18-
}
19-
20-
bool KeybindEditPopup::init(ZStringView name, Keybind const& keybind, Function<void(Keybind const&)> callback) {
10+
bool KeybindEditPopup::init(
11+
std::shared_ptr<KeybindSettingV3> setting,
12+
Keybind const& keybind,
13+
Function<void(Keybind const&)> callback
14+
) {
2115
if (!GeodePopup::init(220, 170))
2216
return false;
2317

24-
this->setTitle(name);
18+
this->setTitle(setting->getDisplayName());
2519
m_noElasticity = true;
2620

21+
if (auto mod = setting->getMod()) {
22+
auto fromModLabel = CCLabelBMFont::create(mod->getName().c_str(), "bigFont.fnt");
23+
fromModLabel->setScale(.4f);
24+
fromModLabel->setColor(ccc3(55, 155, 255));
25+
m_mainLayer->addChildAtPosition(fromModLabel, Anchor::Top, ccp(0, -40));
26+
}
27+
28+
m_setting = setting;
2729
m_callback = std::move(callback);
2830
m_currentKeybind = keybind;
29-
3031
auto bottomMenu = CCMenu::create();
3132
bottomMenu->setContentWidth(220.f);
3233

@@ -81,7 +82,7 @@ void KeybindEditPopup::updateLabel() {
8182
if (*m_originalKeybind != m_currentKeybind) {
8283
m_originalKeybindContainer = CCNode::create();
8384
m_originalKeybindContainer->setContentWidth(200);
84-
m_originalKeybindContainer->setScale(.6f);
85+
m_originalKeybindContainer->setScale(.4f);
8586
m_originalKeybindContainer->setAnchorPoint(ccp(.5f, .5f));
8687

8788
auto originalKeybindInfoStart = CCLabelBMFont::create("(Previous: ", "bigFont.fnt");
@@ -116,7 +117,8 @@ void KeybindEditPopup::updateLabel() {
116117
limitNodeWidth(m_keybindNode, m_mainLayer->getContentWidth() - 10, .75f, .1f);
117118
}
118119
}
119-
m_mainLayer->addChildAtPosition(m_keybindNode, Anchor::Center, ccp(0, 7));
120+
float keybindOffset = m_originalKeybindContainer ? 10 : 0;
121+
m_mainLayer->addChildAtPosition(m_keybindNode, Anchor::Center, ccp(0, keybindOffset));
120122
}
121123

122124
void KeybindEditPopup::onSet(CCObject*) {
@@ -132,24 +134,33 @@ void KeybindEditPopup::onRemove(CCObject*) {
132134
this->onClose(nullptr);
133135
}
134136

135-
KeybindListPopup* KeybindListPopup::create(ZStringView name, std::vector<Keybind> const& keybinds, Function<void(std::vector<Keybind>)> callback) {
136-
auto ret = new KeybindListPopup();
137-
if (ret->init(name, keybinds, std::move(callback))) {
137+
KeybindEditPopup* KeybindEditPopup::create(
138+
std::shared_ptr<KeybindSettingV3> setting,
139+
Keybind const& keybind,
140+
Function<void(Keybind const&)> callback
141+
) {
142+
auto ret = new KeybindEditPopup();
143+
if (ret->init(setting, keybind, std::move(callback))) {
138144
ret->autorelease();
139145
return ret;
140146
}
141147
delete ret;
142148
return nullptr;
143149
}
144150

145-
bool KeybindListPopup::init(ZStringView name, std::vector<Keybind> const& keybinds, Function<void(std::vector<Keybind>)> callback) {
151+
bool KeybindListPopup::init(
152+
std::shared_ptr<KeybindSettingV3> setting,
153+
std::vector<Keybind> const& keybinds,
154+
Function<void(std::vector<Keybind>)> callback
155+
) {
146156
if (!GeodePopup::init(220.f, 250.f))
147157
return false;
148158

149-
this->setTitle(name);
159+
this->setTitle(setting->getDisplayName());
150160
m_noElasticity = true;
151161

152162
m_callback = std::move(callback);
163+
m_setting = setting;
153164
m_currentKeybinds = keybinds;
154165
m_hasChanged = false;
155166

@@ -189,35 +200,23 @@ bool KeybindListPopup::init(ZStringView name, std::vector<Keybind> const& keybin
189200

190201
void KeybindListPopup::updateKeybinds() {
191202
m_scrollLayer->m_contentLayer->removeAllChildren();
192-
193-
if (m_currentKeybinds.size() > 1) {
194-
m_scrollLayer->m_contentLayer->addChild(CCNode::create());
195-
for (size_t i = 1; i < m_currentKeybinds.size(); i++) {
196-
auto& keybind = m_currentKeybinds[i];
197-
CCNode* buttonSprite;
198-
// If this is a keyboard keybind, show the key name, otherwise show the controller input icon
199-
if (keybind.key < 1000 || keybind.key > 2000) {
200-
buttonSprite = createGeodeButton(keybind.toString(), true);
201-
} else {
202-
buttonSprite = createGeodeButton(keybind.createNode(), "");
203-
}
204-
auto button = CCMenuItemSpriteExtra::create(buttonSprite, this, menu_selector(KeybindListPopup::onKeybind));
205-
button->setTag(i);
206-
limitNodeWidth(button, 185.f, 1.f, .1f);
207-
auto menu = CCMenu::createWithItem(button);
208-
menu->setContentSize({ 190.f, button->getScaledContentHeight() });
209-
button->setPosition({ 95.f, button->getScaledContentHeight() / 2 });
210-
m_scrollLayer->m_contentLayer->addChild(menu);
211-
}
212-
m_scrollLayer->m_contentLayer->addChild(CCNode::create());
203+
size_t index = 0;
204+
for (auto& keybind : m_currentKeybinds) {
205+
auto bspr = createKeybindButton(keybind);
206+
auto button = CCMenuItemSpriteExtra::create(bspr, this, menu_selector(KeybindListPopup::onKeybind));
207+
button->setTag(index);
208+
auto menu = CCMenu::createWithItem(button);
209+
menu->setContentSize({ 190.f, button->getScaledContentHeight() });
210+
button->setPosition({ 95.f, button->getScaledContentHeight() / 2 });
211+
m_scrollLayer->m_contentLayer->addChild(menu);
212+
index += 1;
213213
}
214-
215214
m_scrollLayer->m_contentLayer->updateLayout();
216215
m_scrollLayer->scrollToTop();
217216
}
218217

219218
void KeybindListPopup::onAdd(CCObject*) {
220-
KeybindEditPopup::create(m_title->getString(), Keybind(), [this](Keybind const& newKeybind) {
219+
KeybindEditPopup::create(m_setting, Keybind(), [this](Keybind const& newKeybind) {
221220
if (std::ranges::contains(m_currentKeybinds, newKeybind)) return;
222221
m_hasChanged = true;
223222
m_currentKeybinds.push_back(newKeybind);
@@ -232,7 +231,7 @@ void KeybindListPopup::onSave(CCObject*) {
232231

233232
void KeybindListPopup::onKeybind(CCObject* sender) {
234233
auto index = sender->getTag();
235-
KeybindEditPopup::create(m_title->getString(), m_currentKeybinds[index], [this, index](Keybind const& newKeybind) {
234+
KeybindEditPopup::create(m_setting, m_currentKeybinds[index], [this, index](Keybind const& newKeybind) {
236235
if (m_currentKeybinds[index] == newKeybind) return;
237236
m_hasChanged = true;
238237
if (newKeybind.key == KEY_None || std::ranges::contains(m_currentKeybinds, newKeybind)) {
@@ -263,3 +262,16 @@ void KeybindListPopup::onClose(CCObject*) {
263262
}
264263
}
265264

265+
KeybindListPopup* KeybindListPopup::create(
266+
std::shared_ptr<KeybindSettingV3> setting,
267+
std::vector<Keybind> const& keybinds,
268+
Function<void(std::vector<Keybind>)> callback
269+
) {
270+
auto ret = new KeybindListPopup();
271+
if (ret->init(setting, keybinds, std::move(callback))) {
272+
ret->autorelease();
273+
return ret;
274+
}
275+
delete ret;
276+
return nullptr;
277+
}

loader/src/ui/mods/settings/KeybindEditPopup.hpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,60 @@ using namespace geode::prelude;
66

77
CCNode* createKeybindButton(Keybind const& keybind);
88

9+
// If these are ever exposed then they probably need to be rewritten to not
10+
// contain the KeybindSettingV3 directly and instead take a title, subtitle,
11+
// etc.
12+
913
class KeybindEditPopup : public GeodePopup {
1014
protected:
15+
std::shared_ptr<KeybindSettingV3> m_setting;
1116
CCNode* m_keybindNode = nullptr;
1217
CCNode* m_originalKeybindContainer = nullptr;
1318
Keybind m_currentKeybind;
1419
std::optional<Keybind> m_originalKeybind;
1520
Function<void(Keybind const&)> m_callback;
1621

17-
bool init(ZStringView name, Keybind const& keybind, Function<void(Keybind const&)> callback);
22+
bool init(
23+
std::shared_ptr<KeybindSettingV3> setting,
24+
Keybind const& keybind,
25+
Function<void(Keybind const&)> callback
26+
);
1827
void onSet(CCObject*);
1928
void onRemove(CCObject*);
2029

2130
void updateLabel();
2231

2332
public:
24-
static KeybindEditPopup* create(ZStringView name, Keybind const& keybind, Function<void(Keybind const&)> callback);
33+
static KeybindEditPopup* create(
34+
std::shared_ptr<KeybindSettingV3> setting,
35+
Keybind const& keybind,
36+
Function<void(Keybind const&)> callback
37+
);
2538
};
2639

2740
class KeybindListPopup : public GeodePopup {
2841
protected:
42+
std::shared_ptr<KeybindSettingV3> m_setting;
2943
std::vector<Keybind> m_currentKeybinds;
3044
Function<void(std::vector<Keybind>)> m_callback;
3145
ScrollLayer* m_scrollLayer;
3246
bool m_hasChanged;
3347

34-
bool init(ZStringView name, std::vector<Keybind> const& keybinds, Function<void(std::vector<Keybind>)> callback);
48+
bool init(
49+
std::shared_ptr<KeybindSettingV3> setting,
50+
std::vector<Keybind> const& keybinds,
51+
Function<void(std::vector<Keybind>)> callback
52+
);
3553
void updateKeybinds();
3654
void onAdd(CCObject*);
3755
void onSave(CCObject*);
3856
void onKeybind(CCObject*);
3957
void onClose(CCObject*) override;
58+
4059
public:
41-
static KeybindListPopup* create(ZStringView name, std::vector<Keybind> const& keybinds, Function<void(std::vector<Keybind>)> callback);
60+
static KeybindListPopup* create(
61+
std::shared_ptr<KeybindSettingV3> setting,
62+
std::vector<Keybind> const& keybinds,
63+
Function<void(std::vector<Keybind>)> callback
64+
);
4265
};

loader/src/ui/mods/settings/SettingNodeV3.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -704,32 +704,38 @@ void KeybindSettingNodeV3::updateState(CCNode* invoker) {
704704
}
705705

706706
void KeybindSettingNodeV3::onExtra(CCObject* sender) {
707-
auto setting = getSetting();
708-
KeybindListPopup::create(setting->getName().value_or(setting->getKey()), m_currentValue, [this](std::vector<Keybind> newKeybinds) {
709-
if (m_currentValue == newKeybinds) return;
710-
m_currentValue = std::move(newKeybinds);
711-
this->markChanged(nullptr);
712-
})->show();
707+
KeybindListPopup::create(
708+
getSetting(),
709+
m_currentValue,
710+
[this](std::vector<Keybind> newKeybinds) {
711+
if (m_currentValue == newKeybinds) return;
712+
m_currentValue = std::move(newKeybinds);
713+
this->markChanged(nullptr);
714+
}
715+
)->show();
713716
}
714717

715718
void KeybindSettingNodeV3::onKeybind(CCObject* sender) {
716719
auto index = sender->getTag();
717-
auto setting = getSetting();
718-
KeybindEditPopup::create(setting->getName().value_or(setting->getKey()), index >= 0 ? m_currentValue[index] : Keybind(), [this, index](Keybind const& newKeybind) {
719-
if (index >= 0) {
720-
if (newKeybind.key == KEY_None || std::ranges::contains(m_currentValue, newKeybind)) {
721-
m_currentValue.erase(m_currentValue.begin() + index);
720+
KeybindEditPopup::create(
721+
getSetting(),
722+
index >= 0 ? m_currentValue[index] : Keybind(),
723+
[this, index](Keybind const& newKeybind) {
724+
if (index >= 0) {
725+
if (newKeybind.key == KEY_None || std::ranges::contains(m_currentValue, newKeybind)) {
726+
m_currentValue.erase(m_currentValue.begin() + index);
727+
}
728+
else {
729+
m_currentValue[index] = newKeybind;
730+
}
722731
}
723732
else {
724-
m_currentValue[index] = newKeybind;
733+
if (std::ranges::contains(m_currentValue, newKeybind)) return;
734+
m_currentValue.push_back(newKeybind);
725735
}
736+
this->markChanged(nullptr);
726737
}
727-
else {
728-
if (std::ranges::contains(m_currentValue, newKeybind)) return;
729-
m_currentValue.push_back(newKeybind);
730-
}
731-
this->markChanged(nullptr);
732-
})->show();
738+
)->show();
733739
}
734740

735741
void KeybindSettingNodeV3::onCommit() {

0 commit comments

Comments
 (0)