diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 66bd184470..f853ab05e0 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -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() { if (_fontAtlas == nullptr || _utf16Text.empty()) @@ -690,44 +739,10 @@ void Label::alignText() } 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(); + updateLabelLetters(); + updateColor(); } @@ -1372,7 +1387,7 @@ Sprite* Label::getLetter(int letterIndex) if (_systemFontDirty || _currentLabelType == LabelType::STRING_TEXTURE) { break; - } + } auto contentDirty = _contentDirty; if (contentDirty) @@ -1393,16 +1408,16 @@ Sprite* Label::getLetter(int 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) { + 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) { letter = LabelLetter::create(); @@ -1421,19 +1436,6 @@ Sprite* Label::getLetter(int letterIndex) addChild(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); diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index 0b29f3ffa8..931e0f577d 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -505,6 +505,7 @@ protected: bool multilineTextWrapByChar(); bool multilineTextWrapByWord(); + void updateLabelLetters(); virtual void alignText(); void computeAlignmentOffset(); bool computeHorizontalKernings(const std::u16string& stringToRender);