Skip to content

Commit 21456fc

Browse files
committed
make mdtextarea pimpl
1 parent c4a3aff commit 21456fc

File tree

2 files changed

+66
-59
lines changed

2 files changed

+66
-59
lines changed

loader/include/Geode/ui/MDTextArea.hpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,16 @@ namespace geode {
3535
public cocos2d::CCLabelProtocol,
3636
public FLAlertLayerProtocol {
3737
private:
38+
class Impl;
39+
std::unique_ptr<Impl> m_impl;
3840
/**
3941
* Converts single newlines to soft linebreaks.
4042
*/
4143
static std::string translateNewlines(std::string const& str);
4244

43-
// TODO in v5: this should be pimpl (or final)
44-
protected:
45-
std::string m_text;
46-
cocos2d::CCSize m_size;
47-
cocos2d::extension::CCScale9Sprite* m_bgSprite = nullptr;
48-
cocos2d::CCMenu* m_content = nullptr;
49-
CCScrollLayerExt* m_scrollLayer = nullptr;
50-
TextRenderer* m_renderer = nullptr;
51-
bool m_compatibilityMode = false;
52-
5345
bool init(std::string str, cocos2d::CCSize const& size);
5446

47+
MDTextArea();
5548
virtual ~MDTextArea();
5649

5750
void onLink(CCObject*);

loader/src/ui/nodes/MDTextArea.cpp

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <charconv>
1818
#include <Geode/loader/Log.hpp>
1919
#include <Geode/ui/GeodeUI.hpp>
20+
#include <memory>
2021
#include <server/Server.hpp>
2122
#include <regex>
2223

@@ -28,6 +29,19 @@ static constexpr float g_indent = 7.f;
2829
static constexpr float g_codeBlockIndent = 8.f;
2930
static constexpr ccColor3B g_linkColor = {0x7f, 0xf4, 0xf4};
3031

32+
class MDTextArea::Impl {
33+
public:
34+
std::string m_text;
35+
cocos2d::CCSize m_size;
36+
cocos2d::extension::CCScale9Sprite* m_bgSprite = nullptr;
37+
cocos2d::CCMenu* m_content = nullptr;
38+
CCScrollLayerExt* m_scrollLayer = nullptr;
39+
TextRenderer* m_renderer = nullptr;
40+
bool m_compatibilityMode = false;
41+
};
42+
43+
MDTextArea::MDTextArea() : m_impl(std::make_unique<Impl>()) {}
44+
3145
auto makeMdFont() -> TextRenderer::Font {
3246
return [](int style) -> TextRenderer::Label {
3347
if ((style & TextStyleBold) && (style & TextStyleItalic)) {
@@ -129,37 +143,37 @@ bool MDTextArea::init(std::string str, CCSize const& size) {
129143
this->ignoreAnchorPointForPosition(false);
130144
this->setAnchorPoint({ .5f, .5f });
131145

132-
m_text = std::move(str);
133-
m_size = size - CCSize { 15.f, 0.f };
134-
this->setContentSize(m_size);
135-
m_renderer = TextRenderer::create();
136-
CC_SAFE_RETAIN(m_renderer);
146+
m_impl->m_text = std::move(str);
147+
m_impl->m_size = size - CCSize { 15.f, 0.f };
148+
this->setContentSize(m_impl->m_size);
149+
m_impl->m_renderer = TextRenderer::create();
150+
CC_SAFE_RETAIN(m_impl->m_renderer);
137151

138-
m_bgSprite = CCScale9Sprite::create("square02b_001.png", { 0.0f, 0.0f, 80.0f, 80.0f });
139-
m_bgSprite->setScale(.5f);
140-
m_bgSprite->setColor({ 0, 0, 0 });
141-
m_bgSprite->setOpacity(75);
142-
m_bgSprite->setContentSize(size * 2);
143-
m_bgSprite->setPosition(m_size / 2);
144-
this->addChild(m_bgSprite);
152+
m_impl->m_bgSprite = CCScale9Sprite::create("square02b_001.png", { 0.0f, 0.0f, 80.0f, 80.0f });
153+
m_impl->m_bgSprite->setScale(.5f);
154+
m_impl->m_bgSprite->setColor({ 0, 0, 0 });
155+
m_impl->m_bgSprite->setOpacity(75);
156+
m_impl->m_bgSprite->setContentSize(size * 2);
157+
m_impl->m_bgSprite->setPosition(m_impl->m_size / 2);
158+
this->addChild(m_impl->m_bgSprite);
145159

146-
m_scrollLayer = ScrollLayer::create({ 0, 0, m_size.width, m_size.height }, true);
160+
m_impl->m_scrollLayer = ScrollLayer::create({ 0, 0, m_impl->m_size.width, m_impl->m_size.height }, true);
147161

148-
m_content = CCMenu::create();
149-
m_content->setZOrder(2);
150-
m_scrollLayer->m_contentLayer->addChild(m_content);
162+
m_impl->m_content = CCMenu::create();
163+
m_impl->m_content->setZOrder(2);
164+
m_impl->m_scrollLayer->m_contentLayer->addChild(m_impl->m_content);
151165

152-
m_scrollLayer->setTouchEnabled(true);
166+
m_impl->m_scrollLayer->setTouchEnabled(true);
153167

154-
this->addChild(m_scrollLayer);
168+
this->addChild(m_impl->m_scrollLayer);
155169

156170
this->updateLabel();
157171

158172
return true;
159173
}
160174

161175
MDTextArea::~MDTextArea() {
162-
CC_SAFE_RELEASE(m_renderer);
176+
CC_SAFE_RELEASE(m_impl->m_renderer);
163177
}
164178

165179
void MDTextArea::onLink(CCObject* pSender) {
@@ -236,8 +250,8 @@ struct MDParser {
236250

237251
static int parseText(MD_TEXTTYPE type, MD_CHAR const* rawText, MD_SIZE size, void* mdtextarea) {
238252
auto textarea = static_cast<MDTextArea*>(mdtextarea);
239-
auto renderer = textarea->m_renderer;
240-
auto compatibilityMode = textarea->m_compatibilityMode;
253+
auto renderer = textarea->m_impl->m_renderer;
254+
auto compatibilityMode = textarea->m_impl->m_compatibilityMode;
241255

242256
auto text = std::string(rawText, size);
243257
switch (type) {
@@ -412,7 +426,7 @@ struct MDParser {
412426

413427
static int enterBlock(MD_BLOCKTYPE type, void* detail, void* mdtextarea) {
414428
auto textarea = static_cast<MDTextArea*>(mdtextarea);
415-
auto renderer = textarea->m_renderer;
429+
auto renderer = textarea->m_impl->m_renderer;
416430
switch (type) {
417431
case MD_BLOCKTYPE::MD_BLOCK_DOC:
418432
{
@@ -459,7 +473,7 @@ struct MDParser {
459473
case MD_BLOCKTYPE::MD_BLOCK_HR:
460474
{
461475
renderer->breakLine(g_paragraphPadding / 2);
462-
renderer->renderNode(BreakLine::create(textarea->m_size.width));
476+
renderer->renderNode(BreakLine::create(textarea->m_impl->m_size.width));
463477
renderer->breakLine(g_paragraphPadding);
464478
}
465479
break;
@@ -505,7 +519,7 @@ struct MDParser {
505519

506520
static int leaveBlock(MD_BLOCKTYPE type, void* detail, void* mdtextarea) {
507521
auto textarea = static_cast<MDTextArea*>(mdtextarea);
508-
auto renderer = textarea->m_renderer;
522+
auto renderer = textarea->m_impl->m_renderer;
509523
switch (type) {
510524
case MD_BLOCKTYPE::MD_BLOCK_DOC:
511525
{
@@ -518,7 +532,7 @@ struct MDParser {
518532
renderer->breakLine();
519533
if (hdetail->level == 1) {
520534
renderer->breakLine(g_paragraphPadding / 2);
521-
renderer->renderNode(BreakLine::create(textarea->m_size.width));
535+
renderer->renderNode(BreakLine::create(textarea->m_impl->m_size.width));
522536
}
523537
renderer->breakLine(g_paragraphPadding);
524538
renderer->popScale();
@@ -556,7 +570,7 @@ struct MDParser {
556570

557571
auto pad = g_codeBlockIndent / 1.5f;
558572

559-
CCSize size { textarea->m_size.width - renderer->getCurrentIndent() -
573+
CCSize size { textarea->m_impl->m_size.width - renderer->getCurrentIndent() -
560574
renderer->getCurrentWrapOffset() + pad * 2,
561575
s_codeStart - codeEnd + pad * 2 };
562576

@@ -577,7 +591,7 @@ struct MDParser {
577591
);
578592
bg->setAnchorPoint({ .5f, .5f });
579593
bg->setZOrder(-1);
580-
textarea->m_content->addChild(bg);
594+
textarea->m_impl->m_content->addChild(bg);
581595

582596
renderer->popWrapOffset();
583597
renderer->popIndent();
@@ -607,7 +621,7 @@ struct MDParser {
607621
}
608622

609623
static int enterSpan(MD_SPANTYPE type, void* detail, void* mdtextarea) {
610-
auto renderer = static_cast<MDTextArea*>(mdtextarea)->m_renderer;
624+
auto renderer = static_cast<MDTextArea*>(mdtextarea)->m_impl->m_renderer;
611625
switch (type) {
612626
case MD_SPANTYPE::MD_SPAN_STRONG:
613627
{
@@ -666,7 +680,7 @@ struct MDParser {
666680
}
667681

668682
static int leaveSpan(MD_SPANTYPE type, void* detail, void* mdtextarea) {
669-
auto renderer = static_cast<MDTextArea*>(mdtextarea)->m_renderer;
683+
auto renderer = static_cast<MDTextArea*>(mdtextarea)->m_impl->m_renderer;
670684
switch (type) {
671685
case MD_SPANTYPE::MD_SPAN_STRONG:
672686
{
@@ -731,12 +745,12 @@ decltype(MDParser::s_codeSpans) MDParser::s_codeSpans = {};
731745
bool MDParser::s_breakListLine = false;
732746

733747
void MDTextArea::updateLabel() {
734-
m_renderer->begin(m_content, CCPointZero, m_size);
748+
m_impl->m_renderer->begin(m_impl->m_content, CCPointZero, m_impl->m_size);
735749

736-
m_renderer->pushFont(makeMdFont());
737-
m_renderer->pushScale(.5f);
738-
m_renderer->pushVerticalAlign(TextAlignment::End);
739-
m_renderer->pushHorizontalAlign(TextAlignment::Begin);
750+
m_impl->m_renderer->pushFont(makeMdFont());
751+
m_impl->m_renderer->pushScale(.5f);
752+
m_impl->m_renderer->pushVerticalAlign(TextAlignment::End);
753+
m_impl->m_renderer->pushHorizontalAlign(TextAlignment::Begin);
740754

741755
MD_PARSER parser;
742756

@@ -754,16 +768,16 @@ void MDTextArea::updateLabel() {
754768

755769
MDParser::s_codeSpans = {};
756770

757-
auto textContent = m_text;
758-
if (m_compatibilityMode) {
759-
textContent = MDTextArea::translateNewlines(m_text);
771+
auto textContent = m_impl->m_text;
772+
if (m_impl->m_compatibilityMode) {
773+
textContent = MDTextArea::translateNewlines(m_impl->m_text);
760774

761775
// ery proofing...
762776
utils::string::replaceIP(textContent, "<c_>", "<c->");
763777
}
764778

765779
if (md_parse(textContent.c_str(), textContent.size(), &parser, this)) {
766-
m_renderer->renderString("Error parsing Markdown");
780+
m_impl->m_renderer->renderString("Error parsing Markdown");
767781
}
768782

769783
for (auto& render : MDParser::s_codeSpans) {
@@ -778,7 +792,7 @@ void MDTextArea::updateLabel() {
778792
);
779793
bg->setAnchorPoint(render.m_node->getAnchorPoint());
780794
bg->setZOrder(-1);
781-
m_content->addChild(bg);
795+
m_impl->m_content->addChild(bg);
782796
// i know what you're thinking.
783797
// my brother in christ, what the hell is this?
784798
// where did this magical + 1.5f come from?
@@ -788,31 +802,31 @@ void MDTextArea::updateLabel() {
788802
render.m_node->setPositionY(render.m_node->getPositionY() + 1.5f);
789803
}
790804

791-
m_renderer->end();
805+
m_impl->m_renderer->end();
792806

793-
if (m_content->getContentSize().height > m_size.height) {
807+
if (m_impl->m_content->getContentSize().height > m_impl->m_size.height) {
794808
// Generate bottom padding
795-
m_scrollLayer->m_contentLayer->setContentSize(m_content->getContentSize() + CCSize { 0.f, 12.5 });
796-
m_content->setPositionY(10.f);
809+
m_impl->m_scrollLayer->m_contentLayer->setContentSize(m_impl->m_content->getContentSize() + CCSize { 0.f, 12.5 });
810+
m_impl->m_content->setPositionY(10.f);
797811
} else {
798-
m_scrollLayer->m_contentLayer->setContentSize(m_content->getContentSize());
799-
m_content->setPositionY(-2.5f);
812+
m_impl->m_scrollLayer->m_contentLayer->setContentSize(m_impl->m_content->getContentSize());
813+
m_impl->m_content->setPositionY(-2.5f);
800814
}
801815

802-
m_scrollLayer->moveToTop();
816+
m_impl->m_scrollLayer->moveToTop();
803817
}
804818

805819
CCScrollLayerExt* MDTextArea::getScrollLayer() const {
806-
return m_scrollLayer;
820+
return m_impl->m_scrollLayer;
807821
}
808822

809823
void MDTextArea::setString(char const* text) {
810-
m_text = text;
824+
m_impl->m_text = text;
811825
this->updateLabel();
812826
}
813827

814828
char const* MDTextArea::getString() {
815-
return m_text.c_str();
829+
return m_impl->m_text.c_str();
816830
}
817831

818832
MDTextArea* MDTextArea::create(std::string str, CCSize const& size) {

0 commit comments

Comments
 (0)