diff --git a/mod.json b/mod.json index fdc76f6..4707ab6 100644 --- a/mod.json +++ b/mod.json @@ -1,5 +1,5 @@ { - "geode": "2.0.0-beta.23", + "geode": "2.0.0-beta.24", "version": "v1.0.0-beta.7", "gd": { "win": "2.204", diff --git a/src/CCLabelBMFontExt.h b/src/CCLabelBMFontExt.h index 6d5115b..aa1094e 100644 --- a/src/CCLabelBMFontExt.h +++ b/src/CCLabelBMFontExt.h @@ -7,14 +7,13 @@ #include #include #include +#include using namespace geode::prelude; -std::pair _Emoji(std::string name) -{ +std::pair _Emoji(const std::string &name) { auto spr = std::string(""_spr); - - return std::pair(name, spr + (name + std::string(".png"))); + return {name, spr + (name + std::string(".png"))}; } std::vector> emojis = { @@ -122,8 +121,7 @@ std::vector> emojis = { #include "EmojiInfoLayer.h" -enum LabelPartType -{ +enum LabelPartType { Text, Emoji, Username, @@ -131,517 +129,505 @@ enum LabelPartType Level }; -struct LabelPart -{ +struct LabelPart { LabelPartType type = LabelPartType::Text; - std::string extra = ""; + std::string extra; - LabelPart(LabelPartType type, std::string ext) - { + LabelPart(LabelPartType type, std::string ext) { this->type = type; - this->extra = ext; + this->extra = std::move(ext); } - std::string fileNameForEmoji() - { + std::string fileNameForEmoji() const { if (type == LabelPartType::Text) return ""; - for (size_t i = 0; i < emojis.size(); i++) - { - if (geode::utils::string::toLower(emojis[i].first) == geode::utils::string::toLower(extra)) - return emojis[i].second; + for (auto &emoji: emojis) { + if (geode::utils::string::toLower(emoji.first) == geode::utils::string::toLower(extra)) + return emoji.second; } return ""; } - bool isValid() - { + bool isValid() const { return (CCSpriteFrameCache::get()->spriteFrameByName(fileNameForEmoji().c_str())); } }; -std::string trimEndSpaces(const std::string& str) { +std::string trimEndSpaces(const std::string &str) { size_t endpos = str.find_last_not_of(" \t"); return (endpos != std::string::npos) ? str.substr(0, endpos + 1) : ""; } -class CCLabelBMFontExt : public CCMenu -{ - public: - std::vector parts; - - std::string font = ""; - std::string text = ""; - ccColor3B colour = ccc3(255, 255, 255); +class CCLabelBMFontExt : public CCMenu { +public: + std::vector parts; - float maxX; + std::string font = ""; + std::string text = ""; + ccColor3B colour = ccc3(255, 255, 255); - virtual void setColor(const ccColor3B& color) - { - colour = color; - - updateLabel(); - } - - virtual const ccColor3B& getColor(void) - { - return colour; - } + float maxX{}; - bool isUrl(std::string str) { - std::regex pattern("^(https?:\\/\\/)?((?:[^\\/:@\\s]+\\.)+)((?:[a-z]{2,})|(?:xn--[a-z0-9\\-]{2,}))(?:\\/\\S*)?$"); - - return std::regex_match(str, pattern); - } + virtual void setColor(const ccColor3B &color) { + colour = color; - bool isLevel(std::string str) - { - std::regex pattern("^[0-9]+$"); - - return std::regex_match(str, pattern); - } + updateLabel(); + } - void onEmoji(CCObject* sender) - { - #ifndef GEODE_IS_MACOS - auto str = as(sender)->getID(); + virtual const ccColor3B &getColor(void) { + return colour; + } - EmojiInfoLayer::addToScene(str); - #endif - } + static bool isUrl(const std::string &str) { + static std::regex pattern( + R"(((http|https)://)(www.)?[a-zA-Z0-9@:%._\+~#?&//=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%._\+~#?&//=]*))"); + bool isUrl = std::regex_match(str, pattern); + return isUrl; + } - void onUsername(CCObject* sender) - { - auto str = as(sender)->getID(); + static bool isLevel(const std::string &str) { + static std::regex pattern("^[0-9]+$"); + return std::regex_match(str, pattern); + } - log::info("username: {}", str); + void onEmoji(CCObject *sender) { +#ifndef GEODE_IS_MACOS + auto str = as(sender)->getID(); - auto search = GJSearchObject::create(SearchType::Users, str.c_str()); - auto browser = LevelBrowserLayer::create(search); + EmojiInfoLayer::addToScene(str); +#endif + } - auto scene = CCScene::create(); - scene->addChild(browser); + void onUsername(CCObject *sender) { + auto str = as(sender)->getID(); - CCDirector::get()->pushScene(CCTransitionFade::create(0.5f, scene)); + auto search = GJSearchObject::create(SearchType::Users, str.c_str()); + auto browser = LevelBrowserLayer::create(search); - //usernameForUserID() - } + auto scene = CCScene::create(); + scene->addChild(browser); - void onLevel(CCObject* sender) - { - auto str = as(sender)->getID(); + CCDirector::get()->pushScene(CCTransitionFade::create(0.5f, scene)); - log::info("level: {}", str); + //usernameForUserID() + } - auto search = GJSearchObject::create(SearchType::Search, str.c_str()); - auto browser = LevelBrowserLayer::create(search); + void onLevel(CCObject *sender) { + auto str = as(sender)->getID(); - auto scene = CCScene::create(); - scene->addChild(browser); + auto search = GJSearchObject::create(SearchType::Search, str.c_str()); + auto browser = LevelBrowserLayer::create(search); - CCDirector::get()->pushScene(CCTransitionFade::create(0.5f, scene)); + auto scene = CCScene::create(); + scene->addChild(browser); - //usernameForUserID() - } + CCDirector::get()->pushScene(CCTransitionFade::create(0.5f, scene)); - void onUrl(CCObject* sender) - { - auto str = as(sender)->getID(); + //usernameForUserID() + } - log::info("url: {}", str); + void onUrl(CCObject *sender) { + auto str = as(sender)->getID(); - geode::createQuickPopup( + geode::createQuickPopup( "Hold Up!", "Links are spooky! Are you sure you want to go to\n" + str + "?", "Cancel", "Yes", - [str](FLAlertLayer*, bool btn2) { + [str](FLAlertLayer *, bool btn2) { if (btn2) { - CCApplication::get()->openURL(str.c_str()); - //geode::utils::web::openLinkInBrowser(str); + //CCApplication::get()->openURL(str.c_str()); + geode::utils::web::openLinkInBrowser(str); } } - ); + ); + } + + static bool tryGetEmoji(std::string str, size_t from, std::string &out) { + if (str[from] != ':') + return false; + + size_t end = str.find(':', from + 1); + if (end == std::string::npos) + return false; + + out = str.substr(from + 1, end - from - 1); + std::string lower = geode::utils::string::toLower(out); + for (auto &emoji: emojis) { + if (geode::utils::string::toLower(emoji.first) == lower) + return true; } - virtual void updateLabel() // copying types from normal label - { - parts.clear(); + return true; + } - std::string s; - bool inEmoji = false; + static bool tryGetUsername(std::string str, size_t from, std::string &out) { + if (str[from] != '@') + return false; - for (size_t i = 0; i < text.size(); i++) - { - if (text[i] == ' ') - { - s = s + " "; + size_t end = str.find_first_of(" .,;:", from + 1); + if (end == std::string::npos) + end = str.size(); - parts.push_back(LabelPart(isLevel(trimEndSpaces(s)) ? LabelPartType::Level : (isUrl(trimEndSpaces(s)) ? LabelPartType::Url : (s.starts_with("@") ? LabelPartType::Username : LabelPartType::Text)), s)); + out = str.substr(from, end - from); + return true; + } - inEmoji = false; + static bool tryGetUrl(const std::string &str, size_t from, std::string &out) { + size_t end = str.find(' ', from + 1); + if (end == std::string::npos) + end = str.size(); - s = ""; + out = str.substr(from, end - from); + return isUrl(out); + } - continue; - } + static bool tryGetLevel(const std::string &str, size_t from, std::string &out) { + size_t end = str.find(' ', from + 1); + if (end == std::string::npos) + end = str.size(); - if (text[i] == ':') - { - if (inEmoji) - { - auto part = LabelPart(LabelPartType::Emoji, s); - - if (!part.isValid()) - { - part.type = LabelPartType::Text; - s = ":" + s + ":"; - part.extra = s; - } - else - { - - } - - parts.push_back(part); - } - else - parts.push_back(LabelPart(inEmoji ? LabelPartType::Emoji : (isLevel(trimEndSpaces(s)) ? LabelPartType::Level : (isUrl(trimEndSpaces(s)) ? LabelPartType::Url : (s.starts_with("@") ? LabelPartType::Username : LabelPartType::Text))), s)); + out = str.substr(from, end - from); + return isLevel(out); + } - inEmoji = !inEmoji; + void pushLabelPart(const std::string &s) { + std::string output = s; + LabelPartType type; + + if (tryGetEmoji(s, 0, output)) { + type = LabelPartType::Emoji; + } else if (tryGetUsername(s, 0, output)) { + type = LabelPartType::Username; + } else if (tryGetUrl(s, 0, output)) { + type = LabelPartType::Url; + } else if (tryGetLevel(s, 0, output)) { + type = LabelPartType::Level; + } else { + output = s; + type = LabelPartType::Text; + } - s = ""; + parts.emplace_back(type, output); - continue; // press start reference no way - } + // Check if there is a stripped part + if (s.size() > output.size()) { + parts.emplace_back(LabelPartType::Text, s.substr(output.size())); + } + } - s = s + text[i]; + virtual void updateLabel() { + parts.clear(); + + std::string s; + bool inEmoji = false; + + for (char c: text) { + if (c == ' ') { + pushLabelPart(s + " "); + inEmoji = false; + s = ""; + continue; } - if (s != "") - { - if (inEmoji) - s = ":" + s; + if (c == ':') { + if (inEmoji) { + auto part = LabelPart(LabelPartType::Emoji, s); + + if (!part.isValid()) { + part.type = LabelPartType::Text; + s = fmt::format(":{}:", s); + part.extra = s; + } + + parts.push_back(part); + } else { + if (s.starts_with("http")) { + s += ":"; + continue; + } + pushLabelPart(s); + } - parts.push_back(LabelPart(isLevel(trimEndSpaces(s)) ? LabelPartType::Level : (isUrl(trimEndSpaces(s)) ? LabelPartType::Url : (s.starts_with("@") ? LabelPartType::Username : LabelPartType::Text)), s)); + inEmoji = !inEmoji; + s = ""; + continue; // press start reference no way } - float pos = 0; - float wid = 0; - int yPos = 0; - float height = 0; + s += c; + } - float commentHeight = CCLabelBMFont::create("l", font.c_str())->getContentHeight(); + if (!s.empty()) { + if (inEmoji) + s = ":" + s; + pushLabelPart(s); + } - this->removeAllChildren(); + float pos = 0; + float wid = 0; + int yPos = 0; + float height = 0; - for (size_t i = 0; i < parts.size(); i++) - { - auto seg = parts[i]; - //log::info("type: {}, extra: {}, filename: {}", seg.type == LabelPartType::Emoji ? "Emoji" : "Text", seg.extra, seg.fileNameForEmoji()); + float commentHeight = CCLabelBMFont::create("l", font.c_str())->getContentHeight(); - if (seg.type == LabelPartType::Text) - { - auto lbl = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); - lbl->setAnchorPoint(ccp(0, 0)); - lbl->setPosition(ccp(pos, -20 * yPos)); - lbl->setColor(colour); - this->addChild(lbl); - - height = std::max(height, lbl->getScaledContentSize().height); - pos += lbl->getScaledContentSize().width; + this->removeAllChildren(); + + for (auto seg: parts) { + //log::info("type: {}, extra: {}, filename: {}", seg.type == LabelPartType::Emoji ? "Emoji" : "Text", seg.extra, seg.fileNameForEmoji()); + + if (seg.type == LabelPartType::Text) { + auto lbl = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); + lbl->setAnchorPoint(ccp(0, 0)); + lbl->setPosition(ccp(pos, -20 * yPos)); + lbl->setColor(colour); + this->addChild(lbl); + + height = std::max(height, lbl->getScaledContentSize().height); + pos += lbl->getScaledContentSize().width; + wid = std::max(wid, pos); + + if (maxX != 0 && pos > maxX) { wid = std::max(wid, pos); + pos = 0; + yPos++; - if (maxX != 0) - { - if (pos > maxX) - { - wid = std::max(wid, pos); - pos = 0; - yPos++; - - lbl->setPosition(ccp(pos, -20 * yPos)); - pos += lbl->getScaledContentSize().width + 2 - 0.75f; - } - } + lbl->setPosition(ccp(pos, -20 * yPos)); + pos += lbl->getScaledContentSize().width + 2 - 0.75f; } - else if (seg.type == LabelPartType::Emoji) - { - auto emoji = CCSprite::createWithSpriteFrameName(seg.fileNameForEmoji().c_str()); - - if (!emoji) - continue; + } else if (seg.type == LabelPartType::Emoji) { + auto emoji = CCSprite::createWithSpriteFrameName(seg.fileNameForEmoji().c_str()); - emoji->setScale(commentHeight / emoji->getContentHeight()); + if (!emoji) + continue; - auto emojiBtn = CCMenuItemSpriteExtra::create(emoji, this, menu_selector(CCLabelBMFontExt::onEmoji)); - emojiBtn->setID(seg.extra); + emoji->setScale(commentHeight / emoji->getContentHeight()); - emojiBtn->setPosition(ccp(pos + 2, -20 * yPos) + (emojiBtn->getScaledContentSize() / 2)); - this->addChild(emojiBtn); + auto emojiBtn = CCMenuItemSpriteExtra::create(emoji, this, menu_selector(CCLabelBMFontExt::onEmoji)); + emojiBtn->setID(seg.extra); - #ifdef GEODE_IS_MACOS - emojiBtn->setEnabled(false); - #endif + emojiBtn->setPosition(ccp(pos + 2, -20 * yPos) + (emojiBtn->getScaledContentSize() / 2)); + this->addChild(emojiBtn); - height = std::max(height, emoji->getScaledContentSize().height); - pos += emojiBtn->getScaledContentSize().width + 2 - 0.75f; - wid = std::max(wid, pos); +#ifdef GEODE_IS_MACOS + emojiBtn->setEnabled(false); +#endif - if (maxX != 0) - { - if (pos > maxX) - { - wid = std::max(wid, pos); - pos = 0; - yPos++; - - emojiBtn->setPosition(ccp(pos + 2, -20 * yPos) + (emojiBtn->getScaledContentSize() / 2)); - pos += emojiBtn->getScaledContentSize().width + 2 - 0.75f; - } - } - } - else if (seg.type == LabelPartType::Username) - { - auto n = CCNode::create(); - - auto lbl = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); - lbl->setColor(ccc3(127, 244, 244)); - lbl->setAnchorPoint(ccp(0, 0)); - - n->addChild(lbl); - n->setContentSize(lbl->getContentSize()); - - auto n2 = CCNode::create(); - - auto lbl2 = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); - lbl2->setOpacity(150); - lbl2->setAnchorPoint(ccp(0, 0)); - - n2->addChild(lbl2); - n2->setContentSize(lbl2->getContentSize()); - - auto btn = CCMenuItemSprite::create(n, n2, this, menu_selector(CCLabelBMFontExt::onUsername)); - btn->setID(seg.extra.substr(1)); - btn->setPosition(ccp(pos, -20 * yPos)); - btn->setAnchorPoint(ccp(0, 0)); - - this->addChild(btn); - - height = std::max(height, lbl->getScaledContentSize().height); - pos += lbl->getScaledContentSize().width; + height = std::max(height, emoji->getScaledContentSize().height); + pos += emojiBtn->getScaledContentSize().width + 2 - 0.75f; + wid = std::max(wid, pos); + + if (maxX != 0 && pos > maxX) { wid = std::max(wid, pos); + pos = 0; + yPos++; - if (maxX != 0) - { - if (pos > maxX) - { - wid = std::max(wid, pos); - pos = 0; - yPos++; - - lbl->setPosition(ccp(pos, -20 * yPos)); - pos += lbl->getScaledContentSize().width + 2 - 0.75f; - } - } + emojiBtn->setPosition(ccp(pos + 2, -20 * yPos) + (emojiBtn->getScaledContentSize() / 2)); + pos += emojiBtn->getScaledContentSize().width + 2 - 0.75f; } - else if (seg.type == LabelPartType::Url) - { - auto n = CCNode::create(); - - auto lbl = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); - lbl->setColor(ccc3(127, 244, 244)); - lbl->setAnchorPoint(ccp(0, 0)); - - auto line = CCLayerColor::create(); - line->setColor(lbl->getColor()); - line->setOpacity(255); - line->ignoreAnchorPointForPosition(false); - line->setAnchorPoint(ccp(0, 0)); - line->setPosition(ccp(0, -1)); - line->setContentWidth(lbl->getContentWidth()); - line->setContentHeight(1); - - n->addChild(line); - n->addChild(lbl); - n->setContentSize(lbl->getContentSize()); - - auto n2 = CCNode::create(); - - auto lbl2 = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); - lbl2->setOpacity(150); - lbl2->setAnchorPoint(ccp(0, 0)); - - auto line2 = CCLayerColor::create(); - line2->setColor(lbl2->getColor()); - line2->setOpacity(150); - line2->ignoreAnchorPointForPosition(false); - line2->setAnchorPoint(ccp(0, 0)); - line2->setPosition(ccp(0, -1)); - line2->setContentWidth(lbl->getContentWidth()); - line2->setContentHeight(1); - - n2->addChild(line2); - n2->addChild(lbl2); - n2->setContentSize(lbl2->getContentSize()); - - auto btn = CCMenuItemSprite::create(n, n2, this, menu_selector(CCLabelBMFontExt::onUrl)); - btn->setID(seg.extra); - btn->setPosition(ccp(pos, -20 * yPos)); - btn->setAnchorPoint(ccp(0, 0)); - - this->addChild(btn); - - height = std::max(height, lbl->getScaledContentSize().height); - pos += lbl->getScaledContentSize().width; + } else if (seg.type == LabelPartType::Username) { + auto n = CCNode::create(); + + auto lbl = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); + lbl->setColor(ccc3(127, 244, 244)); + lbl->setAnchorPoint(ccp(0, 0)); + + n->addChild(lbl); + n->setContentSize(lbl->getContentSize()); + + auto n2 = CCNode::create(); + + auto lbl2 = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); + lbl2->setOpacity(150); + lbl2->setAnchorPoint(ccp(0, 0)); + + n2->addChild(lbl2); + n2->setContentSize(lbl2->getContentSize()); + + auto btn = CCMenuItemSprite::create(n, n2, this, menu_selector(CCLabelBMFontExt::onUsername)); + btn->setID(seg.extra.substr(1)); + btn->setPosition(ccp(pos, -20 * yPos)); + btn->setAnchorPoint(ccp(0, 0)); + + this->addChild(btn); + + height = std::max(height, lbl->getScaledContentSize().height); + pos += lbl->getScaledContentSize().width; + wid = std::max(wid, pos); + + if (maxX != 0 && pos > maxX) { wid = std::max(wid, pos); + pos = 0; + yPos++; - if (maxX != 0) - { - if (pos > maxX) - { - wid = std::max(wid, pos); - pos = 0; - yPos++; - - lbl->setPosition(ccp(pos, -20 * yPos)); - pos += lbl->getScaledContentSize().width + 2 - 0.75f; - } - } + lbl->setPosition(ccp(pos, -20 * yPos)); + pos += lbl->getScaledContentSize().width + 2 - 0.75f; } - else if (seg.type == LabelPartType::Level) - { - auto n = CCNode::create(); - - auto lbl = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); - lbl->setColor(ccc3(127, 244, 244)); - lbl->setAnchorPoint(ccp(0, 0)); - - n->addChild(lbl); - n->setContentSize(lbl->getContentSize()); - - auto n2 = CCNode::create(); - - auto lbl2 = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); - lbl2->setOpacity(150); - lbl2->setAnchorPoint(ccp(0, 0)); - - n2->addChild(lbl2); - n2->setContentSize(lbl2->getContentSize()); - - auto btn = CCMenuItemSprite::create(n, n2, this, menu_selector(CCLabelBMFontExt::onLevel)); - btn->setID(seg.extra); - btn->setPosition(ccp(pos, -20 * yPos)); - btn->setAnchorPoint(ccp(0, 0)); - - this->addChild(btn); - - height = std::max(height, lbl->getScaledContentSize().height); - pos += lbl->getScaledContentSize().width; + } else if (seg.type == LabelPartType::Url) { + auto n = CCNode::create(); + + auto lbl = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); + lbl->setColor(ccc3(127, 244, 244)); + lbl->setAnchorPoint(ccp(0, 0)); + + auto line = CCLayerColor::create(); + line->setColor(lbl->getColor()); + line->setOpacity(255); + line->ignoreAnchorPointForPosition(false); + line->setAnchorPoint(ccp(0, 0)); + line->setPosition(ccp(0, -1)); + line->setContentWidth(lbl->getContentWidth()); + line->setContentHeight(1); + + n->addChild(line); + n->addChild(lbl); + n->setContentSize(lbl->getContentSize()); + + auto n2 = CCNode::create(); + + auto lbl2 = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); + lbl2->setOpacity(150); + lbl2->setAnchorPoint(ccp(0, 0)); + + auto line2 = CCLayerColor::create(); + line2->setColor(lbl2->getColor()); + line2->setOpacity(150); + line2->ignoreAnchorPointForPosition(false); + line2->setAnchorPoint(ccp(0, 0)); + line2->setPosition(ccp(0, -1)); + line2->setContentWidth(lbl->getContentWidth()); + line2->setContentHeight(1); + + n2->addChild(line2); + n2->addChild(lbl2); + n2->setContentSize(lbl2->getContentSize()); + + auto btn = CCMenuItemSprite::create(n, n2, this, menu_selector(CCLabelBMFontExt::onUrl)); + btn->setID(seg.extra); + btn->setPosition(ccp(pos, -20 * yPos)); + btn->setAnchorPoint(ccp(0, 0)); + + this->addChild(btn); + + height = std::max(height, lbl->getScaledContentSize().height); + pos += lbl->getScaledContentSize().width; + wid = std::max(wid, pos); + + if (maxX != 0 && pos > maxX) { wid = std::max(wid, pos); + pos = 0; + yPos++; - if (maxX != 0) - { - if (pos > maxX) - { - wid = std::max(wid, pos); - pos = 0; - yPos++; - - lbl->setPosition(ccp(pos, -20 * yPos)); - pos += lbl->getScaledContentSize().width + 2 - 0.75f; - } - } + lbl->setPosition(ccp(pos, -20 * yPos)); + pos += lbl->getScaledContentSize().width + 2 - 0.75f; } - } + } else if (seg.type == LabelPartType::Level) { + auto n = CCNode::create(); - CCArrayExt objects = this->getChildren(); + auto lbl = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); + lbl->setColor(ccc3(127, 244, 244)); + lbl->setAnchorPoint(ccp(0, 0)); - for (auto* obj : objects) { - obj->setPositionY(obj->getPositionY() + (20 * yPos)); - } + n->addChild(lbl); + n->setContentSize(lbl->getContentSize()); - this->setContentSize(ccp(wid, /*height*/20 * (yPos + 1))); - } + auto n2 = CCNode::create(); - virtual void setFont(const char *newString, bool update = true) - { - font = std::string(newString); + auto lbl2 = CCLabelBMFont::create(seg.extra.c_str(), font.c_str(), 99999); + lbl2->setOpacity(150); + lbl2->setAnchorPoint(ccp(0, 0)); - if (update) - updateLabel(); - } + n2->addChild(lbl2); + n2->setContentSize(lbl2->getContentSize()); - virtual void setString(const char *newString, bool update = true) - { - text = std::string(newString); + auto btn = CCMenuItemSprite::create(n, n2, this, menu_selector(CCLabelBMFontExt::onLevel)); + btn->setID(seg.extra); + btn->setPosition(ccp(pos, -20 * yPos)); + btn->setAnchorPoint(ccp(0, 0)); - if (update) - updateLabel(); + this->addChild(btn); + + height = std::max(height, lbl->getScaledContentSize().height); + pos += lbl->getScaledContentSize().width; + wid = std::max(wid, pos); + + if (maxX != 0 && pos > maxX) { + wid = std::max(wid, pos); + pos = 0; + yPos++; + + lbl->setPosition(ccp(pos, -20 * yPos)); + pos += lbl->getScaledContentSize().width + 2 - 0.75f; + } + } } - virtual void setMaxX(float max, bool update = true) - { - maxX = max; + CCArrayExt objects = this->getChildren(); - if (update) - updateLabel(); + for (auto *obj: objects) { + obj->setPositionY(obj->getPositionY() + (20 * yPos)); } - float clampf(float v, float min, float max) - { - if (v < min) - v = min; + this->setContentSize(ccp(wid, /*height*/20 * (yPos + 1))); + } - if (v > max) - v = max; + virtual void setFont(const char *newString, bool update = true) { + font = std::string(newString); - return v; - } + if (update) + updateLabel(); + } - void limitLabelWidth(float width, float defaultScale, float minScale) - { - this->setScale(clampf(width / this->getContentSize().width, minScale, defaultScale)); - } + virtual void setString(const char *newString, bool update = true) { + text = std::string(newString); + + if (update) + updateLabel(); + } - void limitLabelHeight(float height, float defaultScale, float minScale, bool alignScale = true) - { - this->setScale(clampf(height / this->getContentSize().height, minScale, defaultScale)); + virtual void setMaxX(float max, bool update = true) { + maxX = max; - if (alignScale) - setMaxX(this->maxX / this->getScale()); + if (update) + updateLabel(); + } - log::info("f: {}", height / this->getContentSize().height); - } + void limitLabelWidth(float width, float defaultScale, float minScale) { + this->setScale(clampf(width / this->getContentSize().width, minScale, defaultScale)); + } - bool init(const char* text, const char* font) - { - if (!CCMenu::init()) - return false; + void limitLabelHeight(float height, float defaultScale, float minScale, bool alignScale = true) { + this->setScale(clampf(height / this->getContentSize().height, minScale, defaultScale)); - this->setAnchorPoint(ccp(0.5f, 0.5f)); - this->ignoreAnchorPointForPosition(false); + if (alignScale) + setMaxX(this->maxX / this->getScale()); + } - this->setFont(font, false); - this->setString(text, false); + bool init(const char *text, const char *font) { + if (!CCMenu::init()) + return false; - this->updateLabel(); + this->setAnchorPoint(ccp(0.5f, 0.5f)); + this->ignoreAnchorPointForPosition(false); - handleTouchPriority(this); + this->setFont(font, false); + this->setString(text, false); - return true; - } + this->updateLabel(); - static CCLabelBMFontExt* create(const char* text, const char* font) - { - CCLabelBMFontExt* pRet = new CCLabelBMFontExt(); - if (pRet && pRet->init(text, font)) { - pRet->autorelease(); - return pRet; - } else { - delete pRet; - return nullptr; - } + handleTouchPriority(this); + + return true; + } + + static CCLabelBMFontExt *create(const char *text, const char *font) { + auto *pRet = new CCLabelBMFontExt(); + if (pRet && pRet->init(text, font)) { + pRet->autorelease(); + return pRet; + } else { + delete pRet; + return nullptr; } + } }; diff --git a/src/EmojiInfoLayer.h b/src/EmojiInfoLayer.h index 38ee274..2e8c308 100644 --- a/src/EmojiInfoLayer.h +++ b/src/EmojiInfoLayer.h @@ -5,91 +5,85 @@ #include #include #include +#include using namespace geode::prelude; #include "CCLabelBMFontExt.h" -class EmojiInfoLayer : public CCLayerColor -{ - public: - - virtual void keyBackClicked() - { - onClose(nullptr); - } +class EmojiInfoLayer : public CCLayerColor { +public: - void onClose(CCObject*) - { - CCTouchDispatcher::get()->removeDelegate(this); + virtual void keyBackClicked() { + onClose(nullptr); + } - this->removeFromParent(); - } + void onClose(CCObject *) { + CCTouchDispatcher::get()->removeDelegate(this); - bool init(std::string emoji) - { - if (!CCLayerColor::init()) - return false; + this->removeFromParent(); + } - this->setKeypadEnabled(true); + bool init(const std::string &emoji) { + if (!CCLayerColor::init()) + return false; - auto panel = CCScale9Sprite::create("GJ_square01.png"); - panel->setContentSize(ccp(240, 250)); - this->addChildAtPosition(panel, Anchor::Center); + this->setKeypadEnabled(true); - auto title = CCLabelBMFont::create((std::string(":") + emoji + std::string(":")).c_str(), "bigFont.fnt"); - title->setScale(0.65f); - this->addChildAtPosition(title, Anchor::Center, ccp(0, 70 + 38)); + auto panel = CCScale9Sprite::create("GJ_square01.png"); + panel->setContentSize(ccp(240, 250)); + this->addChildAtPosition(panel, Anchor::Center); - std::string ss; + auto title = CCLabelBMFont::create((std::string(":") + emoji + std::string(":")).c_str(), "bigFont.fnt"); + title->setScale(0.65f); + this->addChildAtPosition(title, Anchor::Center, ccp(0, 70 + 38)); - for (size_t i = 0; i < emojis.size(); i++) - { - if (emojis[i].first == emoji) - { - ss = emojis[i].second; - break; - } + std::string ss; + + for (auto &i: emojis) { + if (i.first == emoji) { + ss = i.second; + break; } + } - auto spr = CCSprite::createWithSpriteFrameName(ss.c_str()); - spr->setScale(2); - this->addChildAtPosition(spr, Anchor::Center); + auto spr = CCSprite::createWithSpriteFrameName(ss.c_str()); + spr->setScale(2); + this->addChildAtPosition(spr, Anchor::Center); - auto menu = CCMenu::create(); + auto menu = CCMenu::create(); - auto ok = CCMenuItemSpriteExtra::create(ButtonSprite::create("OK"), this, menu_selector(EmojiInfoLayer::onClose)); - menu->addChildAtPosition(ok, Anchor::Center, ccp(0, -63 - 39)); + auto ok = CCMenuItemSpriteExtra::create(ButtonSprite::create("OK"), this, + menu_selector(EmojiInfoLayer::onClose)); + menu->addChildAtPosition(ok, Anchor::Center, ccp(0, -63 - 39)); - this->addChild(menu); + this->addChild(menu); - this->runAction(CCFadeTo::create(0.5f, 125)); - this->ignoreAnchorPointForPosition(true); + this->runAction(CCFadeTo::create(0.5f, 125)); + this->ignoreAnchorPointForPosition(true); - CCTouchDispatcher::get()->addTargetedDelegate(this, -169, true); - CCTouchDispatcher::get()->addTargetedDelegate(menu, -170, true); + CCTouchDispatcher::get()->addTargetedDelegate(this, -169, true); + CCTouchDispatcher::get()->addTargetedDelegate(menu, -170, true); - return true; - } + return true; + } - static EmojiInfoLayer* create(std::string emoji) - { - EmojiInfoLayer* pRet = new EmojiInfoLayer(); - if (pRet && pRet->init(emoji)) { - pRet->autorelease(); - return pRet; - } else { - delete pRet; - return nullptr; - } + static EmojiInfoLayer *create(std::string emoji) { + auto *pRet = new EmojiInfoLayer(); + if (pRet && pRet->init(std::move(emoji))) { + pRet->autorelease(); + return pRet; + } else { + delete pRet; + return nullptr; } + } - static void addToScene(std::string emoji) - { - auto pRet = EmojiInfoLayer::create(emoji); + static void addToScene(std::string emoji) { + auto pRet = EmojiInfoLayer::create(std::move(emoji)); - CCScene::get()->addChild(pRet, CCScene::get()->getHighestChildZ() + 1); - } + CCScene::get()->addChild(pRet, CCScene::get()->getHighestChildZ() + 1); + } }; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e57e230..b48d534 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,222 +10,206 @@ using namespace geode::prelude; #include "CCLabelBMFontExt.h" -class $modify (CommentCell) -{ - CCLabelBMFontExt* label = nullptr; +class $modify (CommentCell) { + CCLabelBMFontExt *label = nullptr; - void onUnhide(cocos2d::CCObject* sender) - { - #ifdef GEODE_IS_ANDROID - CommentCell::onUnhide(sender); - #endif + void onUnhide(cocos2d::CCObject *sender) { +#ifdef GEODE_IS_ANDROID + CommentCell::onUnhide(sender); +#endif - //m_fields->label->setString() - } + //m_fields->label->setString() + } - void loadFromComment(GJComment* p0) - { - CommentCell::loadFromComment(p0); - - if (auto txt = as(this->getChildren()->objectAtIndex(1))->getChildByID("comment-text-label")) - { - txt->setVisible(false); + void loadFromComment(GJComment *p0) { + CommentCell::loadFromComment(p0); - auto lbl2 = CCLabelBMFontExt::create(p0->m_commentString.c_str(), "chatFont.fnt"); + if (auto txt = as(this->getChildren()->objectAtIndex(1))->getChildByID("comment-text-label")) { + txt->setVisible(false); - if (p0->m_isSpam) - lbl2->setString("Comment flagged as spam"); + auto lbl2 = CCLabelBMFontExt::create(p0->m_commentString.c_str(), "chatFont.fnt"); - m_fields->label = lbl2; + if (p0->m_isSpam) + lbl2->setString("Comment flagged as spam"); - if (lbl2) - { - lbl2->setPosition(txt->getPosition()); - lbl2->setAnchorPoint(txt->getAnchorPoint()); - lbl2->limitLabelWidth(270, 0.7f, 0); - lbl2->setColor(p0->m_color); + m_fields->label = lbl2; - as(this->getChildren()->objectAtIndex(1))->addChild(lbl2); - } + if (lbl2) { + lbl2->setPosition(txt->getPosition()); + lbl2->setAnchorPoint(txt->getAnchorPoint()); + lbl2->limitLabelWidth(270, 0.7f, 0); + lbl2->setColor(p0->m_color); - return; - } + as(this->getChildren()->objectAtIndex(1))->addChild(lbl2); + } - if (auto txt = as(as(this->getChildren()->objectAtIndex(1))->getChildByID("comment-text-area"))) - { - txt->setVisible(false); + return; + } - auto lbl2 = CCLabelBMFontExt::create(p0->m_commentString.c_str(), "chatFont.fnt"); - lbl2->setColor(p0->m_color); + if (auto txt = as