From 9d92a7c11ce2b47f039eda20f3e0a3ae621df613 Mon Sep 17 00:00:00 2001 From: altalk23 <45172705+altalk23@users.noreply.github.com> Date: Sun, 4 Feb 2024 19:23:53 +0300 Subject: [PATCH] fix simpletextarea alignment & dont update at draw --- loader/include/Geode/ui/TextArea.hpp | 24 ++++++++------- loader/src/ui/nodes/TextArea.cpp | 44 ++++++++++++---------------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/loader/include/Geode/ui/TextArea.hpp b/loader/include/Geode/ui/TextArea.hpp index c19e2304..a861d0af 100644 --- a/loader/include/Geode/ui/TextArea.hpp +++ b/loader/include/Geode/ui/TextArea.hpp @@ -50,21 +50,23 @@ namespace geode { private: static SimpleTextArea* create(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth); - bool m_shouldUpdate; - bool m_artificialWidth; - cocos2d::CCMenu* m_container; + bool init(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth); + + bool m_shouldUpdate = false; + bool m_artificialWidth = false; + cocos2d::CCMenu* m_container = nullptr; std::string m_font; std::string m_text; std::vector m_lines; - cocos2d::ccColor4B m_color; - cocos2d::CCTextAlignment m_alignment; - WrappingMode m_wrappingMode; - size_t m_maxLines; - float m_scale; - float m_lineHeight; - float m_linePadding; + cocos2d::ccColor4B m_color = { 0xFF, 0xFF, 0xFF, 0xFF }; + cocos2d::CCTextAlignment m_alignment = cocos2d::kCCTextAlignmentLeft; + WrappingMode m_wrappingMode = WORD_WRAP; + size_t m_maxLines = 0; + float m_scale = 1.f; + float m_lineHeight = 0.f; + float m_linePadding = 0.f; - SimpleTextArea(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth); + SimpleTextArea(); cocos2d::CCLabelBMFont* createLabel(const std::string& text, const float top); float calculateOffset(cocos2d::CCLabelBMFont* label); void charIteration(const std::function& overflowHandling); diff --git a/loader/src/ui/nodes/TextArea.cpp b/loader/src/ui/nodes/TextArea.cpp index a2b74717..a12f2c6d 100644 --- a/loader/src/ui/nodes/TextArea.cpp +++ b/loader/src/ui/nodes/TextArea.cpp @@ -11,9 +11,9 @@ SimpleTextArea* SimpleTextArea::create(const std::string& text, const std::strin } SimpleTextArea* SimpleTextArea::create(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth) { - SimpleTextArea* instance = new SimpleTextArea(font, text, scale, width, artificialWidth); + SimpleTextArea* instance = new SimpleTextArea(); - if (instance && instance->init()) { + if (instance && instance->init(font, text, scale, width, artificialWidth)) { instance->autorelease(); return instance; @@ -24,18 +24,14 @@ SimpleTextArea* SimpleTextArea::create(const std::string& font, const std::strin } } -SimpleTextArea::SimpleTextArea(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth) { +SimpleTextArea::SimpleTextArea() {} + +bool SimpleTextArea::init(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth) { m_font = font; m_text = text; - m_maxLines = 0; m_scale = scale; - m_linePadding = 0; - m_color = { 0xFF, 0xFF, 0xFF, 0xFF }; - m_alignment = kCCTextAlignmentLeft; - m_wrappingMode = WORD_WRAP; m_artificialWidth = artificialWidth; m_container = CCMenu::create(); - m_shouldUpdate = true; this->setAnchorPoint({ 0.5f, 0.5f }); m_container->setPosition({ 0, 0 }); @@ -43,11 +39,15 @@ SimpleTextArea::SimpleTextArea(const std::string& font, const std::string& text, m_container->setContentSize({ width, 0 }); this->addChild(m_container); + + this->updateContainer(); + + return true; } void SimpleTextArea::setFont(const std::string& font) { m_font = font; - m_shouldUpdate = true; + this->updateContainer(); } std::string SimpleTextArea::getFont() { @@ -56,7 +56,7 @@ std::string SimpleTextArea::getFont() { void SimpleTextArea::setColor(const ccColor4B& color) { m_color = color; - m_shouldUpdate = true; + this->updateContainer(); } ccColor4B SimpleTextArea::getColor() { @@ -65,7 +65,7 @@ ccColor4B SimpleTextArea::getColor() { void SimpleTextArea::setAlignment(const CCTextAlignment alignment) { m_alignment = alignment; - m_shouldUpdate = true; + this->updateContainer(); } CCTextAlignment SimpleTextArea::getAlignment() { @@ -74,7 +74,7 @@ CCTextAlignment SimpleTextArea::getAlignment() { void SimpleTextArea::setWrappingMode(const WrappingMode mode) { m_wrappingMode = mode; - m_shouldUpdate = true; + this->updateContainer(); } WrappingMode SimpleTextArea::getWrappingMode() { @@ -83,7 +83,7 @@ WrappingMode SimpleTextArea::getWrappingMode() { void SimpleTextArea::setText(const std::string& text) { m_text = text; - m_shouldUpdate = true; + this->updateContainer(); } std::string SimpleTextArea::getText() { @@ -92,7 +92,7 @@ std::string SimpleTextArea::getText() { void SimpleTextArea::setMaxLines(const size_t maxLines) { m_maxLines = maxLines; - m_shouldUpdate = true; + this->updateContainer(); } size_t SimpleTextArea::getMaxLines() { @@ -101,7 +101,7 @@ size_t SimpleTextArea::getMaxLines() { void SimpleTextArea::setWidth(const float width) { m_artificialWidth = true; - m_shouldUpdate = true; + this->updateContainer(); this->setContentSize({ width, this->getContentSize().height }); m_container->setContentSize(this->getContentSize()); @@ -113,7 +113,7 @@ float SimpleTextArea::getWidth() { void SimpleTextArea::setScale(const float scale) { m_scale = scale; - m_shouldUpdate = true; + this->updateContainer(); } float SimpleTextArea::getScale() { @@ -122,7 +122,7 @@ float SimpleTextArea::getScale() { void SimpleTextArea::setLinePadding(const float padding) { m_linePadding = padding; - m_shouldUpdate = true; + this->updateContainer(); } float SimpleTextArea::getLinePadding() { @@ -276,7 +276,7 @@ void SimpleTextArea::updateContainer() { line->setPosition({ 0, y }); } break; case kCCTextAlignmentCenter: { - line->setAnchorPoint({ 0, 1 }); + line->setAnchorPoint({ 0.5f, 1 }); line->setPosition({ width / 2, y }); } break; case kCCTextAlignmentRight: { @@ -291,10 +291,4 @@ void SimpleTextArea::updateContainer() { void SimpleTextArea::draw() { CCNode::draw(); - - if (m_shouldUpdate) { - this->updateContainer(); - - m_shouldUpdate = false; - } } \ No newline at end of file