Skip to content

Commit f506da5

Browse files
committed
fix(main): Fix positionning for username-menu
1 parent 55f0155 commit f506da5

1 file changed

Lines changed: 22 additions & 120 deletions

File tree

src/main.cpp

Lines changed: 22 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515
static int getBadgePriority(CCNode* badge) {
1616
std::string id = geode::utils::string::toLower(badge->getID());
1717
if (!geode::utils::string::contains(id, "-badge:")) return 9999;
18-
// std::transform(id.begin(), id.end(), id.begin(), [](unsigned char c){ return std::tolower(c); });
18+
1919
size_t pos = id.find("-badge:");
2020
if (pos != std::string::npos) {
2121
size_t start = pos + 7;
2222
size_t end = id.find_first_not_of("0123456789", start);
2323
std::string num = id.substr(start, end == std::string::npos ? end : end - start);
24-
// try {
25-
// int val = std::stoi(num);
26-
// return val > 0 ? val : 9999;
27-
// } catch (...) {}
24+
2825
return geode::utils::numFromString<int>(num).unwrapOr(9999);
2926
}
3027
return 9999;
@@ -33,9 +30,7 @@ static int getBadgePriority(CCNode* badge) {
3330
// Helper: Sorts a CCArray* of badges by priority (lowest first)
3431
static CCArray* sortBadgesByPriority(CCArray* badges) {
3532
std::vector<CCNode*> badgeVec;
36-
// for (int i = 0; i < badges->count(); ++i) {
37-
// badgeVec.push_back(static_cast<CCNode*>(badges->objectAtIndex(i)));
38-
// }
33+
3934
for (CCNode* badge : CCArrayExt<CCNode*>(badges)) {
4035
if (!badge) continue;
4136
badgeVec.push_back(badge);
@@ -63,8 +58,6 @@ class $modify(CustomCommentCell, CommentCell) {
6358

6459
void updateBadges(CCArray* childsToRemove, CCMenu* username_menu, CCArray* badges) {
6560
// Remove all badges from the layer
66-
// CCObject* childObj;
67-
// CCARRAY_FOREACH(childsToRemove, childObj) static_cast<CCNode*>(childObj)->removeFromParent();
6861
CommentCell* cell = this;
6962
for (CCNode* childObj : CCArrayExt<CCNode*>(childsToRemove)) {
7063
if (!childObj) continue;
@@ -145,30 +138,12 @@ class $modify(CustomCommentCell, CommentCell) {
145138
int badge_count = m_fields->badgeCount;
146139

147140
CCMenu* username_menu = typeinfo_cast<CCMenu*>(layer->getChildByIDRecursive("username-menu"));
148-
if (!username_menu) {
149-
log::error("Could not find username-menu");
150-
return;
151-
}
141+
if (!username_menu) return;
152142

153-
if (!m_fields->loaded) {
154-
return;
155-
}
143+
if (!m_fields->loaded) return;
156144

157145
CCArray* childsToRemoveTemp = CCArray::create();
158146
CCArray* temp = CCArray::create();
159-
CCObject* childObj;
160-
// CCARRAY_FOREACH(username_menu->getChildren(), childObj) {
161-
// CCNode* child = static_cast<CCNode*>(childObj);
162-
// std::string find_str ("-badge");
163-
// std::string child_id = child->getID();
164-
// std::transform(child_id.begin(), child_id.end(), child_id.begin(), [](unsigned char c){ return std::tolower(c); });
165-
// if (child_id.find(find_str) != std::string::npos) {
166-
// if (child_id != "badgeapi-plus-badge") {
167-
// childsToRemoveTemp->addObject(child);
168-
// temp->addObject(child);
169-
// }
170-
// }
171-
// }
172147
for (CCNode* child : username_menu->getChildrenExt()) {
173148
if (!child) continue;
174149
std::string child_id = geode::utils::string::toLower(child->getID());
@@ -182,15 +157,6 @@ class $modify(CustomCommentCell, CommentCell) {
182157
if (badge_api_plus) {
183158
auto badge_api_plus_item = static_cast<CCArray*>(static_cast<CCNode*>(badge_api_plus)->getUserObject());
184159

185-
// Check if badges are already loaded on BadgeMenu and then remove those on the profile page
186-
// CCObject* childObj;
187-
// CCARRAY_FOREACH(badge_api_plus_item, childObj) {
188-
// CCNode* child = static_cast<CCNode*>(childObj);
189-
// if (auto user_child = username_menu->getChildByIDRecursive(child->getID())) {
190-
// user_child->removeFromParent();
191-
// username_menu->updateLayout();
192-
// }
193-
// }
194160
for (CCNode* child : CCArrayExt<CCNode*>(badge_api_plus_item)) {
195161
if (!child) continue;
196162
auto user_child = username_menu->getChildByIDRecursive(child->getID());
@@ -200,29 +166,11 @@ class $modify(CustomCommentCell, CommentCell) {
200166
username_menu->updateLayout(); // update layout once outside of the loop
201167

202168
CCArray* temp2 = CCArray::create();
203-
// CCARRAY_FOREACH(badge_api_plus_item, childObj) {
204-
// temp2->addObject(childObj);
205-
// }
206169
for (CCNode* child : CCArrayExt<CCNode*>(badge_api_plus_item)) {
207170
if (!child) continue;
208171
temp2->addObject(child);
209172
}
210173

211-
// CCARRAY_FOREACH(temp, childObj) {
212-
// CCNode* child = static_cast<CCNode*>(childObj);
213-
214-
// // check if ID is present on badge_api_plus_item objects
215-
// bool found = false;
216-
// CCObject* childObj2;
217-
// CCARRAY_FOREACH(badge_api_plus_item, childObj2) {
218-
// CCNode* child2 = static_cast<CCNode*>(childObj2);
219-
// if (child->getID() == child2->getID()) {
220-
// found = true;
221-
// break;
222-
// }
223-
// }
224-
// if (!found) temp2->addObject(child);
225-
// }
226174
for (CCNode* child : CCArrayExt<CCNode*>(temp)) {
227175
if (!child) continue;
228176
bool found = false;
@@ -278,19 +226,14 @@ class $modify(CustomCommentCell, CommentCell) {
278226

279227
CommentCell::loadFromComment(p0);
280228

281-
if (m_fields->loaded) {
282-
return;
283-
}
229+
if (m_fields->loaded) return;
284230

285231
m_fields->loaded = true;
286232

287233
auto layer = m_mainLayer;
288234

289235
CCMenu* username_menu = typeinfo_cast<CCMenu*>(layer->getChildByIDRecursive("username-menu"));
290-
if (!username_menu) {
291-
//log::error("Could not find username-menu");
292-
return;
293-
}
236+
if (!username_menu) return;
294237

295238
this->schedule(schedule_selector(CustomCommentCell::updateBadgesSchedule), 0.0f);
296239
}
@@ -311,7 +254,6 @@ class $modify(CustomProfilePage, ProfilePage) {
311254
void updateBadges(CCArray* childsToRemove, CCMenu* username_menu, CCLabelBMFont* label, CCArray* badges) {
312255
// Remove all badges from the layer
313256
CCObject* childObj;
314-
// CCARRAY_FOREACH(childsToRemove, childObj) static_cast<CCNode*>(childObj)->removeFromParent();
315257
for (CCNode* child : CCArrayExt<CCNode*>(childsToRemove)) {
316258
if (!child) continue;
317259
child->removeFromParent();
@@ -382,15 +324,10 @@ class $modify(CustomProfilePage, ProfilePage) {
382324
int badge_count = m_fields->badgeCount;
383325

384326
CCMenu* username_menu = typeinfo_cast<CCMenu*>(layer->getChildByIDRecursive("username-menu"));
385-
if (!username_menu) {
386-
log::error("Could not find username-menu");
387-
return;
388-
}
327+
if (!username_menu) return;
328+
389329
CCLabelBMFont* label = typeinfo_cast<CCLabelBMFont*>(layer->getChildByIDRecursive("username-label"));
390-
if (!label) {
391-
log::error("Could not find username-label");
392-
return;
393-
}
330+
if (!label) return;
394331

395332
if (!m_fields->loaded) {
396333
return;
@@ -399,18 +336,6 @@ class $modify(CustomProfilePage, ProfilePage) {
399336
CCArray* childsToRemoveTemp = CCArray::create();
400337
CCArray* temp = CCArray::create();
401338
CCObject* childObj;
402-
// CCARRAY_FOREACH(username_menu->getChildren(), childObj) {
403-
// CCNode* child = static_cast<CCNode*>(childObj);
404-
// std::string find_str ("-badge");
405-
// std::string child_id = child->getID();
406-
// std::transform(child_id.begin(), child_id.end(), child_id.begin(), [](unsigned char c){ return std::tolower(c); });
407-
// if (child_id.find(find_str) != std::string::npos) {
408-
// if (child_id != "badgeapi-plus-badge") {
409-
// childsToRemoveTemp->addObject(child);
410-
// temp->addObject(child);
411-
// }
412-
// }
413-
// }
414339
for (CCNode* child : username_menu->getChildrenExt()) {
415340
if (!child) continue;
416341
std::string child_id = geode::utils::string::toLower(child->getID());
@@ -424,15 +349,6 @@ class $modify(CustomProfilePage, ProfilePage) {
424349
if (badge_api_plus) {
425350
auto badge_api_plus_item = static_cast<CCArray*>(static_cast<CCNode*>(badge_api_plus)->getUserObject());
426351

427-
// Check if badges are already loaded on BadgeMenu and then remove those on the profile page
428-
// CCObject* childObj;
429-
// CCARRAY_FOREACH(badge_api_plus_item, childObj) {
430-
// CCNode* child = static_cast<CCNode*>(childObj);
431-
// if (auto user_child = username_menu->getChildByIDRecursive(child->getID())) {
432-
// user_child->removeFromParent();
433-
// username_menu->updateLayout();
434-
// }
435-
// }
436352
for (CCNode* child : CCArrayExt<CCNode*>(badge_api_plus_item)) {
437353
if (!child) continue;
438354
auto user_child = username_menu->getChildByIDRecursive(child->getID());
@@ -442,29 +358,11 @@ class $modify(CustomProfilePage, ProfilePage) {
442358
username_menu->updateLayout(); // update layout once after loop finishes
443359

444360
CCArray* temp2 = CCArray::create();
445-
// CCARRAY_FOREACH(badge_api_plus_item, childObj) {
446-
// temp2->addObject(childObj);
447-
// }
448361
for (CCNode* node : CCArrayExt<CCNode*>(badge_api_plus_item)) {
449362
if (!node) continue;
450363
temp2->addObject(node);
451364
}
452365

453-
// CCARRAY_FOREACH(temp, childObj) {
454-
// CCNode* child = static_cast<CCNode*>(childObj);
455-
456-
// // check if ID is present on badge_api_plus_item objects
457-
// bool found = false;
458-
// CCObject* childObj2;
459-
// CCARRAY_FOREACH(badge_api_plus_item, childObj2) {
460-
// CCNode* child2 = static_cast<CCNode*>(childObj2);
461-
// if (child->getID() == child2->getID()) {
462-
// found = true;
463-
// break;
464-
// }
465-
// }
466-
// if (!found) temp2->addObject(child);
467-
// }
468366
for (CCNode* child : CCArrayExt<CCNode*>(temp)) {
469367
if (!child) continue;
470368
bool found = false;
@@ -492,6 +390,14 @@ class $modify(CustomProfilePage, ProfilePage) {
492390
m_fields->badgeMenu = BadgeMenu::scene(sortedBadges);
493391
}
494392

393+
void usernameMenuFixer(float dt) {
394+
auto winSize = CCDirector::sharedDirector()->getWinSize();
395+
auto usernameMenu = m_mainLayer->getChildByID("username-menu");
396+
if (usernameMenu->getChildrenCount() > 1) {
397+
usernameMenu->setPositionX(winSize.width / 2.1);
398+
}
399+
}
400+
495401
void loadPageFromUserInfo(GJUserScore* a2) {
496402
if (this->getChildByIDRecursive("mod-badge")) this->getChildByIDRecursive("mod-badge")->removeFromParent();
497403

@@ -506,16 +412,12 @@ class $modify(CustomProfilePage, ProfilePage) {
506412
auto layer = m_mainLayer;
507413

508414
CCMenu* username_menu = typeinfo_cast<CCMenu*>(layer->getChildByIDRecursive("username-menu"));
509-
if (!username_menu) {
510-
//log::error("Could not find username-menu");
511-
return;
512-
}
415+
if (!username_menu) return;
416+
513417
CCLabelBMFont* label = typeinfo_cast<CCLabelBMFont*>(layer->getChildByIDRecursive("username-label"));
514-
if (!label) {
515-
//log::error("Could not find username-label");
516-
return;
517-
}
418+
if (!label) return;
518419

519420
this->schedule(schedule_selector(CustomProfilePage::updateBadgesSchedule), 0.0f);
421+
this->schedule(schedule_selector(CustomProfilePage::usernameMenuFixer), 0.0f);
520422
}
521423
};

0 commit comments

Comments
 (0)