Fixed alignment issues caused by unallocated memory and bad anchor points

Simply put, alignment had no default assignment, causing undefined behavior, this also revealed some bad anchor points which could be optimized
This commit is contained in:
SMJS 2023-10-03 22:28:15 +02:00 committed by GitHub
parent 7f277a77e1
commit 0e8865546c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,6 +30,7 @@ SimpleTextArea::SimpleTextArea(const std::string& font, const std::string& text,
m_maxLines = 0;
m_scale = scale;
m_linePadding = 0;
m_alignment = kCCTextAlignmentLeft;
m_artificialWidth = artificialWidth;
m_container = CCMenu::create();
@ -129,7 +130,6 @@ CCLabelBMFont* SimpleTextArea::createLabel(const std::string& text, const float
CCLabelBMFont* label = CCLabelBMFont::create(text.c_str(), m_font.c_str());
label->setScale(m_scale);
label->setAnchorPoint({ 0, 0 });
label->setPosition({ 0, top });
return label;
@ -203,26 +203,24 @@ void SimpleTextArea::updateContents() {
m_container->setContentSize(this->getContentSize());
m_container->removeAllChildren();
height -= m_lineHeight;
for (CCLabelBMFont* line : m_lines) {
const float y = height + line->getPositionY();
switch (m_alignment) {
case kCCTextAlignmentLeft: {
line->setAnchorPoint({ 0, 0 });
line->setAnchorPoint({ 0, 1 });
line->setPosition({ 0, y });
} break;
case kCCTextAlignmentCenter: {
line->setAnchorPoint({ 0.5f, 0 });
line->setAnchorPoint({ 0.5f, 1 });
line->setPosition({ width / 2, y });
} break;
case kCCTextAlignmentRight: {
line->setAnchorPoint({ 1, 0 });
line->setAnchorPoint({ 1, 1 });
line->setPosition({ width, y });
} break;
}
m_container->addChild(line);
}
}
}