Skip to content

Commit a1ccd5e

Browse files
authored
Refactor ConfigureLevelFileDataPopup methods and logic
1 parent 0c14ee9 commit a1ccd5e

1 file changed

Lines changed: 37 additions & 32 deletions

File tree

src/_Main.hpp

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ namespace MLE {
6060
int start = *it, end = start;
6161
while (++it != list.end() && *it == end + 1) end++;
6262

63-
if (end - start < 2) { // 1-2 IDs
63+
if (end - start < 2) {
6464
new_listing += fmt::format("{}{}", start, end > start ? fmt::format(",{}", end) : "");
6565
}
66-
else { // 3+
66+
else {
6767
new_listing += fmt::format("{}:{}", start, end);
6868
}
6969
new_listing += ",";
7070
}
71-
if (!new_listing.empty()) new_listing.pop_back(); // remove last comma)
71+
if (!new_listing.empty()) new_listing.pop_back();
7272
return new_listing;
7373
}
7474

@@ -96,22 +96,17 @@ namespace MLE {
9696
);
9797

9898
for (auto entry : string::split(list, ",")) {
99-
//sequence
10099
if (string::contains(entry.c_str(), ":")) {
101100
auto seq = string::split(entry.c_str(), ":");
102101
auto start = utils::numFromString<int>(seq[0].c_str()).unwrapOr(0);
103102
auto end = utils::numFromString<int>(seq[1].c_str()).unwrapOr(0);
104-
bool ew = start > end;//1:-22
103+
bool ew = start > end;
105104
for (int q = start; ew ? q != (end - 1) : q != (end + 1); ew ? --q : ++q) {
106-
auto id = q;
107-
//log::debug("{} (\"{}\")", id, entry);
108-
rtn.push_back(id);
105+
rtn.push_back(q);
109106
}
110107
}
111-
//single id
112108
else {
113109
auto id = utils::numFromString<int>(entry).unwrapOr(0);
114-
//log::debug("{} (\"{}\")", id, entry);
115110
rtn.push_back(id);
116111
}
117112
}
@@ -123,15 +118,24 @@ namespace MLE {
123118

124119
}
125120

126-
// genius name
127-
class ConfigureLevelFileDataPopup : public geode::Popup<LevelEditorLayer*, std::filesystem::path> {
121+
namespace MLE {
122+
inline bool containsAny(std::string_view str, std::initializer_list<std::string_view> subs) {
123+
for (auto& s : subs) {
124+
if (string::contains(str, s)) return true;
125+
}
126+
return false;
127+
}
128+
}
129+
130+
class ConfigureLevelFileDataPopup : public geode::Popup {
128131
protected:
129-
bool setup(LevelEditorLayer* editor, std::filesystem::path related_File) override {
132+
bool init(LevelEditorLayer* editor, std::filesystem::path related_File) {
133+
if (!Popup::init(410.000f, 262.000f)) return false;
130134

131135
auto scroll = ScrollLayer::create({
132136
this->m_buttonMenu->getContentSize().width * 0.86f,
133137
this->m_buttonMenu->getContentSize().height - 10.5f,
134-
});
138+
});
135139
scroll->ignoreAnchorPointForPosition(0);
136140
this->m_buttonMenu->addChildAtPosition(scroll, Anchor::Center, { 0.f, 0.0f });
137141

@@ -140,13 +144,14 @@ class ConfigureLevelFileDataPopup : public geode::Popup<LevelEditorLayer*, std::
140144
);
141145
for (auto asd : *json.get()) {
142146
auto key = asd.getKey().value_or("unnamed obj");
143-
if (string::containsAny(key, { "levelString" })) continue;
147+
if (MLE::containsAny(key, { "levelString" })) continue;
144148

145149
auto layer = CCLayerColor::create({ 0,0,0,42 });
146150
layer->setContentWidth(scroll->getContentWidth());
147151
layer->setContentHeight(34.000f);
148152

149-
if (string::containsAny(key, { "difficulty","stars","requiredCoins","Name","song","sfx","Track" })) layer->setOpacity(90);
153+
if (MLE::containsAny(key, { "difficulty","stars","requiredCoins","Name","song","sfx","Track" }))
154+
layer->setOpacity(90);
150155

151156
auto keyLabel = SimpleTextArea::create(key);
152157
keyLabel->setAnchorPoint({ 0.f, 0.5f });
@@ -159,15 +164,12 @@ class ConfigureLevelFileDataPopup : public geode::Popup<LevelEditorLayer*, std::
159164
layer->addChildAtPosition(keyInputErr, Anchor::BottomLeft, { 6.f, 12.f });
160165

161166
auto keyValInput = TextInput::create(132.f, key, keyLabel->getFont());
162-
163-
keyValInput->setFilter(" !\"#$ % &'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
164-
keyValInput->getInputNode()->m_allowedChars = " !\"#$ % &'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
167+
keyValInput->setFilter(" !\"#$ % &'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
168+
keyValInput->getInputNode()->m_allowedChars = " !\"#$ % &'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
165169
keyValInput->setString(asd.dump());
166170
keyValInput->setCallback(
167-
[=](auto str) mutable { //INPUT CALLBACK
168-
171+
[=](auto str) mutable {
169172
keyInputErr->setString("");
170-
171173
auto parse = matjson::parse(str);
172174
if (parse.isOk()) {
173175
(*json.get())[key] = parse.unwrapOrDefault();
@@ -179,11 +181,14 @@ class ConfigureLevelFileDataPopup : public geode::Popup<LevelEditorLayer*, std::
179181
);
180182
};
181183
}
182-
); //INPUT CALLBACK
184+
);
183185

184-
auto bgSize = keyValInput->getBGSprite()->getContentSize();
185-
keyValInput->getBGSprite()->setSpriteFrame(CCSprite::create("groundSquare_18_001.png")->displayFrame());
186-
keyValInput->getBGSprite()->setContentSize(bgSize);
186+
auto bgSprite = keyValInput->getBGSprite();
187+
auto bgSize = bgSprite->getContentSize();
188+
if (auto* tex = CCTextureCache::get()->addImage("groundSquare_18_001.png", false)) {
189+
bgSprite->setTexture(tex);
190+
}
191+
bgSprite->setContentSize(bgSize);
187192

188193
layer->addChildAtPosition(keyValInput, Anchor::Right, { -72.f, 0 });
189194

@@ -206,7 +211,7 @@ class ConfigureLevelFileDataPopup : public geode::Popup<LevelEditorLayer*, std::
206211
if (!editor) return;
207212
if (!editor->isRunning()) return;
208213
if (item->getTag() == "DontSaveLevel"_h) void();
209-
else EditorPauseLayer::create(editor)->saveLevel();
214+
else EditorPauseLayer::create(editor)->saveLevel();
210215
if (auto err = level::exportLevelFile(editor->m_level, related_File).err())
211216
Notification::create(
212217
" Failed to export level: \n " + err.value_or("unknown error"),
@@ -232,7 +237,7 @@ class ConfigureLevelFileDataPopup : public geode::Popup<LevelEditorLayer*, std::
232237
findFirstChildRecursive<CCLayerColor>(scroll->m_contentLayer, [](auto me) {
233238
if (me->getOpacity() == 90) me->setZOrder(me->getZOrder() == me->getTag() ? -1 : me->getTag());
234239
return false;
235-
});
240+
});
236241
scroll->m_contentLayer->updateLayout();
237242
scroll->scrollToTop();
238243
}
@@ -248,9 +253,9 @@ class ConfigureLevelFileDataPopup : public geode::Popup<LevelEditorLayer*, std::
248253
bottomMenuBG->setOpacity(190);
249254
bottomMenuBG->setID("bottomMenuBG"_spr);
250255
bottomMenuBG->setContentSize({
251-
this->m_buttonMenu->getContentSize().width,
252-
22.000f
253-
});
256+
this->m_buttonMenu->getContentSize().width,
257+
22.000f
258+
});
254259
bottomMenuBG->setZOrder(-1);
255260
this->m_buttonMenu->addChildAtPosition(bottomMenuBG, Anchor::Bottom, { 0.f, bottomMenuY });
256261

@@ -262,7 +267,7 @@ class ConfigureLevelFileDataPopup : public geode::Popup<LevelEditorLayer*, std::
262267
public:
263268
static ConfigureLevelFileDataPopup* create(LevelEditorLayer* editor, std::filesystem::path related_File) {
264269
auto ret = new ConfigureLevelFileDataPopup();
265-
if (ret->initAnchored(410.000f, 262.000f, editor, related_File)) {
270+
if (ret->init(editor, related_File)) {
266271
ret->autorelease();
267272
return ret;
268273
}

0 commit comments

Comments
 (0)