fix simpletextarea alignment & dont update at draw

This commit is contained in:
altalk23 2024-02-04 19:23:53 +03:00
parent ba0e13f9b2
commit 9d92a7c11c
2 changed files with 32 additions and 36 deletions

View file

@ -50,21 +50,23 @@ namespace geode {
private: private:
static SimpleTextArea* create(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth); static SimpleTextArea* create(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth);
bool m_shouldUpdate; bool init(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth);
bool m_artificialWidth;
cocos2d::CCMenu* m_container; bool m_shouldUpdate = false;
bool m_artificialWidth = false;
cocos2d::CCMenu* m_container = nullptr;
std::string m_font; std::string m_font;
std::string m_text; std::string m_text;
std::vector<cocos2d::CCLabelBMFont*> m_lines; std::vector<cocos2d::CCLabelBMFont*> m_lines;
cocos2d::ccColor4B m_color; cocos2d::ccColor4B m_color = { 0xFF, 0xFF, 0xFF, 0xFF };
cocos2d::CCTextAlignment m_alignment; cocos2d::CCTextAlignment m_alignment = cocos2d::kCCTextAlignmentLeft;
WrappingMode m_wrappingMode; WrappingMode m_wrappingMode = WORD_WRAP;
size_t m_maxLines; size_t m_maxLines = 0;
float m_scale; float m_scale = 1.f;
float m_lineHeight; float m_lineHeight = 0.f;
float m_linePadding; 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); cocos2d::CCLabelBMFont* createLabel(const std::string& text, const float top);
float calculateOffset(cocos2d::CCLabelBMFont* label); float calculateOffset(cocos2d::CCLabelBMFont* label);
void charIteration(const std::function<cocos2d::CCLabelBMFont*(cocos2d::CCLabelBMFont* line, const char c, const float top)>& overflowHandling); void charIteration(const std::function<cocos2d::CCLabelBMFont*(cocos2d::CCLabelBMFont* line, const char c, const float top)>& overflowHandling);

View file

@ -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* 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(); instance->autorelease();
return instance; 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_font = font;
m_text = text; m_text = text;
m_maxLines = 0;
m_scale = scale; m_scale = scale;
m_linePadding = 0;
m_color = { 0xFF, 0xFF, 0xFF, 0xFF };
m_alignment = kCCTextAlignmentLeft;
m_wrappingMode = WORD_WRAP;
m_artificialWidth = artificialWidth; m_artificialWidth = artificialWidth;
m_container = CCMenu::create(); m_container = CCMenu::create();
m_shouldUpdate = true;
this->setAnchorPoint({ 0.5f, 0.5f }); this->setAnchorPoint({ 0.5f, 0.5f });
m_container->setPosition({ 0, 0 }); m_container->setPosition({ 0, 0 });
@ -43,11 +39,15 @@ SimpleTextArea::SimpleTextArea(const std::string& font, const std::string& text,
m_container->setContentSize({ width, 0 }); m_container->setContentSize({ width, 0 });
this->addChild(m_container); this->addChild(m_container);
this->updateContainer();
return true;
} }
void SimpleTextArea::setFont(const std::string& font) { void SimpleTextArea::setFont(const std::string& font) {
m_font = font; m_font = font;
m_shouldUpdate = true; this->updateContainer();
} }
std::string SimpleTextArea::getFont() { std::string SimpleTextArea::getFont() {
@ -56,7 +56,7 @@ std::string SimpleTextArea::getFont() {
void SimpleTextArea::setColor(const ccColor4B& color) { void SimpleTextArea::setColor(const ccColor4B& color) {
m_color = color; m_color = color;
m_shouldUpdate = true; this->updateContainer();
} }
ccColor4B SimpleTextArea::getColor() { ccColor4B SimpleTextArea::getColor() {
@ -65,7 +65,7 @@ ccColor4B SimpleTextArea::getColor() {
void SimpleTextArea::setAlignment(const CCTextAlignment alignment) { void SimpleTextArea::setAlignment(const CCTextAlignment alignment) {
m_alignment = alignment; m_alignment = alignment;
m_shouldUpdate = true; this->updateContainer();
} }
CCTextAlignment SimpleTextArea::getAlignment() { CCTextAlignment SimpleTextArea::getAlignment() {
@ -74,7 +74,7 @@ CCTextAlignment SimpleTextArea::getAlignment() {
void SimpleTextArea::setWrappingMode(const WrappingMode mode) { void SimpleTextArea::setWrappingMode(const WrappingMode mode) {
m_wrappingMode = mode; m_wrappingMode = mode;
m_shouldUpdate = true; this->updateContainer();
} }
WrappingMode SimpleTextArea::getWrappingMode() { WrappingMode SimpleTextArea::getWrappingMode() {
@ -83,7 +83,7 @@ WrappingMode SimpleTextArea::getWrappingMode() {
void SimpleTextArea::setText(const std::string& text) { void SimpleTextArea::setText(const std::string& text) {
m_text = text; m_text = text;
m_shouldUpdate = true; this->updateContainer();
} }
std::string SimpleTextArea::getText() { std::string SimpleTextArea::getText() {
@ -92,7 +92,7 @@ std::string SimpleTextArea::getText() {
void SimpleTextArea::setMaxLines(const size_t maxLines) { void SimpleTextArea::setMaxLines(const size_t maxLines) {
m_maxLines = maxLines; m_maxLines = maxLines;
m_shouldUpdate = true; this->updateContainer();
} }
size_t SimpleTextArea::getMaxLines() { size_t SimpleTextArea::getMaxLines() {
@ -101,7 +101,7 @@ size_t SimpleTextArea::getMaxLines() {
void SimpleTextArea::setWidth(const float width) { void SimpleTextArea::setWidth(const float width) {
m_artificialWidth = true; m_artificialWidth = true;
m_shouldUpdate = true; this->updateContainer();
this->setContentSize({ width, this->getContentSize().height }); this->setContentSize({ width, this->getContentSize().height });
m_container->setContentSize(this->getContentSize()); m_container->setContentSize(this->getContentSize());
@ -113,7 +113,7 @@ float SimpleTextArea::getWidth() {
void SimpleTextArea::setScale(const float scale) { void SimpleTextArea::setScale(const float scale) {
m_scale = scale; m_scale = scale;
m_shouldUpdate = true; this->updateContainer();
} }
float SimpleTextArea::getScale() { float SimpleTextArea::getScale() {
@ -122,7 +122,7 @@ float SimpleTextArea::getScale() {
void SimpleTextArea::setLinePadding(const float padding) { void SimpleTextArea::setLinePadding(const float padding) {
m_linePadding = padding; m_linePadding = padding;
m_shouldUpdate = true; this->updateContainer();
} }
float SimpleTextArea::getLinePadding() { float SimpleTextArea::getLinePadding() {
@ -276,7 +276,7 @@ void SimpleTextArea::updateContainer() {
line->setPosition({ 0, y }); line->setPosition({ 0, y });
} break; } break;
case kCCTextAlignmentCenter: { case kCCTextAlignmentCenter: {
line->setAnchorPoint({ 0, 1 }); line->setAnchorPoint({ 0.5f, 1 });
line->setPosition({ width / 2, y }); line->setPosition({ width / 2, y });
} break; } break;
case kCCTextAlignmentRight: { case kCCTextAlignmentRight: {
@ -291,10 +291,4 @@ void SimpleTextArea::updateContainer() {
void SimpleTextArea::draw() { void SimpleTextArea::draw() {
CCNode::draw(); CCNode::draw();
if (m_shouldUpdate) {
this->updateContainer();
m_shouldUpdate = false;
}
} }