Merge pull request #13342 from WenhaiLin/v3-label-miss-letter

Label:Fixed display incorrect if label content is changed after invoking `getLetter(letterIndex)`
This commit is contained in:
子龙山人 2015-08-13 15:09:54 +08:00
commit 082caaf3e0
2 changed files with 61 additions and 58 deletions

View File

@ -646,6 +646,55 @@ void Label::setLineBreakWithoutSpace(bool breakWithoutSpace)
} }
} }
void Label::updateLabelLetters()
{
if (!_letters.empty())
{
Rect uvRect;
LabelLetter* letterSprite;
int letterIndex;
for (auto it = _letters.begin(); it != _letters.end();)
{
letterIndex = it->first;
letterSprite = (LabelLetter*)it->second;
if (letterIndex >= _lengthOfString)
{
Node::removeChild(letterSprite, true);
it = _letters.erase(it);
}
else
{
auto& letterInfo = _lettersInfo[letterIndex];
auto& letterDef = _fontAtlas->_letterDefinitions[letterInfo.utf16Char];
uvRect.size.height = letterDef.height;
uvRect.size.width = letterDef.width;
uvRect.origin.x = letterDef.U;
uvRect.origin.y = letterDef.V;
letterSprite->setTexture(_fontAtlas->getTexture(letterDef.textureID));
if (letterDef.width <= 0.f || letterDef.height <= 0.f)
{
letterSprite->setTextureAtlas(nullptr);
}
else
{
letterSprite->setTextureRect(uvRect, false, uvRect.size);
letterSprite->setTextureAtlas(_batchNodes.at(letterDef.textureID)->getTextureAtlas());
letterSprite->setAtlasIndex(_lettersInfo[letterIndex].atlasIndex);
}
auto px = letterInfo.positionX + letterDef.width / 2 + _linesOffsetX[letterInfo.lineIndex];
auto py = letterInfo.positionY - letterDef.height / 2 + _letterOffsetY;
letterSprite->setPosition(px, py);
++it;
}
}
}
}
void Label::alignText() void Label::alignText()
{ {
if (_fontAtlas == nullptr || _utf16Text.empty()) if (_fontAtlas == nullptr || _utf16Text.empty())
@ -690,44 +739,10 @@ void Label::alignText()
} }
computeAlignmentOffset(); computeAlignmentOffset();
if (!_letters.empty())
{
Rect uvRect;
Sprite* letterSprite;
int letterIndex;
for (auto it = _letters.begin(); it != _letters.end();)
{
letterIndex = it->first;
letterSprite = it->second;
if (letterIndex >= _lengthOfString)
{
Node::removeChild(letterSprite, true);
it = _letters.erase(it);
}
else
{
auto& letterInfo = _lettersInfo[letterIndex];
auto& letterDef = _fontAtlas->_letterDefinitions[letterInfo.utf16Char];
uvRect.size.height = letterDef.height;
uvRect.size.width = letterDef.width;
uvRect.origin.x = letterDef.U;
uvRect.origin.y = letterDef.V;
letterSprite->setBatchNode(_batchNodes.at(letterDef.textureID));
letterSprite->setTextureRect(uvRect, false, uvRect.size);
auto px = letterInfo.positionX + letterDef.width / 2 + _linesOffsetX[letterInfo.lineIndex];
auto py = letterInfo.positionY - letterDef.height / 2 + _letterOffsetY;
letterSprite->setPosition(px,py);
++it;
}
}
}
updateQuads(); updateQuads();
updateLabelLetters();
updateColor(); updateColor();
} }
@ -1372,7 +1387,7 @@ Sprite* Label::getLetter(int letterIndex)
if (_systemFontDirty || _currentLabelType == LabelType::STRING_TEXTURE) if (_systemFontDirty || _currentLabelType == LabelType::STRING_TEXTURE)
{ {
break; break;
} }
auto contentDirty = _contentDirty; auto contentDirty = _contentDirty;
if (contentDirty) if (contentDirty)
@ -1393,16 +1408,16 @@ Sprite* Label::getLetter(int letterIndex)
letter = _letters[letterIndex]; letter = _letters[letterIndex];
} }
auto& letterDef = _fontAtlas->_letterDefinitions[letterInfo.utf16Char];
auto textureID = letterDef.textureID;
Rect uvRect;
uvRect.size.height = letterDef.height;
uvRect.size.width = letterDef.width;
uvRect.origin.x = letterDef.U;
uvRect.origin.y = letterDef.V;
if (letter == nullptr) if (letter == nullptr)
{ {
auto& letterDef = _fontAtlas->_letterDefinitions[letterInfo.utf16Char];
auto textureID = letterDef.textureID;
Rect uvRect;
uvRect.size.height = letterDef.height;
uvRect.size.width = letterDef.width;
uvRect.origin.x = letterDef.U;
uvRect.origin.y = letterDef.V;
if (letterDef.width <= 0.f || letterDef.height <= 0.f) if (letterDef.width <= 0.f || letterDef.height <= 0.f)
{ {
letter = LabelLetter::create(); letter = LabelLetter::create();
@ -1421,19 +1436,6 @@ Sprite* Label::getLetter(int letterIndex)
addChild(letter); addChild(letter);
_letters[letterIndex] = letter; _letters[letterIndex] = letter;
} }
else if (contentDirty)
{
if (letterDef.width <= 0.f || letterDef.height <= 0.f)
{
letter->setTextureAtlas(nullptr);
}
else
{
letter->setTexture(_fontAtlas->getTexture(textureID));
letter->setTextureRect(uvRect, false, uvRect.size);
letter->setTextureAtlas(_batchNodes.at(textureID)->getTextureAtlas());
}
}
} }
} while (false); } while (false);

View File

@ -505,6 +505,7 @@ protected:
bool multilineTextWrapByChar(); bool multilineTextWrapByChar();
bool multilineTextWrapByWord(); bool multilineTextWrapByWord();
void updateLabelLetters();
virtual void alignText(); virtual void alignText();
void computeAlignmentOffset(); void computeAlignmentOffset();
bool computeHorizontalKernings(const std::u16string& stringToRender); bool computeHorizontalKernings(const std::u16string& stringToRender);