fix getLetter crash issue.

1. make vertical clamp works in non-wrap mode.

2. make the lineHeight and letterSprite scale work correct.
This commit is contained in:
zilongshanren 2015-11-20 16:39:48 +08:00
parent 8c25749366
commit 7ee82f20fc
4 changed files with 37 additions and 16 deletions

View File

@ -699,6 +699,8 @@ void Label::updateLabelLetters()
uvRect.origin.x = letterDef.U;
uvRect.origin.y = letterDef.V;
auto batchNode = _batchNodes.at(letterDef.textureID);
letterSprite->setTextureAtlas(batchNode->getTextureAtlas());
letterSprite->setTexture(_fontAtlas->getTexture(letterDef.textureID));
if (letterDef.width <= 0.f || letterDef.height <= 0.f)
{
@ -715,7 +717,7 @@ void Label::updateLabelLetters()
auto py = letterInfo.positionY - letterDef.height / 2 + _letterOffsetY;
letterSprite->setPosition(px, py);
this->updateReusedLetterScale();
this->updateLetterSpriteScale(letterSprite);
++it;
}
}
@ -768,7 +770,7 @@ bool Label::alignText()
}
computeAlignmentOffset();
if(_enableWrap && _overflow == Overflow::SHRINK){
if(_overflow == Overflow::SHRINK){
float fontSize = this->getRenderingFontSize();
if(fontSize > 0 && isVerticalClamp()){
@ -874,7 +876,7 @@ bool Label::updateQuads()
auto index = static_cast<int>(_batchNodes.at(letterDef.textureID)->getTextureAtlas()->getTotalQuads());
_lettersInfo[ctr].atlasIndex = index;
this->updateReusedLetterScale();
this->updateLetterSpriteScale(_reusedLetter);
_batchNodes.at(letterDef.textureID)->insertQuadFromSprite(_reusedLetter, index);
}
@ -1611,7 +1613,7 @@ void Label::setLineHeight(float height)
float Label::getLineHeight() const
{
CCASSERT(_currentLabelType != LabelType::STRING_TEXTURE, "Not supported system font!");
return _textSprite ? 0.0f : _lineHeight;
return _textSprite ? 0.0f : _lineHeight * _bmfontScale;
}
void Label::setLineSpacing(float height)
@ -1924,10 +1926,8 @@ void Label::enableWrap(bool enable)
}
this->_enableWrap = enable;
auto renderingFontSize = this->getRenderingFontSize();
if (fabs(_originalFontSize - renderingFontSize) >= 1) {
this->scaleFontSizeDown(_originalFontSize);
}
this->rescaleWithOriginalFontSize();
_contentDirty = true;
}
@ -1961,11 +1961,17 @@ void Label::setOverflow(Overflow overflow)
}
_overflow = overflow;
this->rescaleWithOriginalFontSize();
_contentDirty = true;
}
void Label::rescaleWithOriginalFontSize()
{
auto renderingFontSize = this->getRenderingFontSize();
if (fabs(_originalFontSize - renderingFontSize) >= 1) {
if (_originalFontSize - renderingFontSize >= 1) {
this->scaleFontSizeDown(_originalFontSize);
}
_contentDirty = true;
}
Label::Overflow Label::getOverflow()const
@ -1973,21 +1979,21 @@ Label::Overflow Label::getOverflow()const
return _overflow;
}
void Label::updateReusedLetterScale()
void Label::updateLetterSpriteScale(Sprite* sprite)
{
if (_currentLabelType == LabelType::BMFONT && _bmFontSize > 0)
{
_reusedLetter->setScale(_bmfontScale);
sprite->setScale(_bmfontScale);
}
else
{
if(fabs(_bmFontSize)<FLT_EPSILON)
{
_reusedLetter->setScale(0);
sprite->setScale(0);
}
else
{
_reusedLetter->setScale(1.0);
sprite->setScale(1.0);
}
}
}

View File

@ -572,6 +572,7 @@ protected:
bool isHorizontalClamp();
bool isVerticalClamp();
float getRenderingFontSize()const;
void rescaleWithOriginalFontSize();
void updateLabelLetters();
virtual bool alignText();
@ -592,7 +593,7 @@ protected:
bool setTTFConfigInternal(const TTFConfig& ttfConfig);
void setBMFontSizeInternal(float fontSize);
void restoreFontSize();
void updateReusedLetterScale();
void updateLetterSpriteScale(Sprite* sprite);
void reset();

View File

@ -371,7 +371,7 @@ void Label::shrinkLabelToContentSize(std::function<bool(void)> lambda)
int i = 0;
auto letterDefinition = _fontAtlas->_letterDefinitions;
auto tempLetterDefinition = letterDefinition;
float originalLineHeight = this->getLineHeight();
float originalLineHeight = _lineHeight;
bool flag = true;
while (lambda()) {
++i;

View File

@ -2264,6 +2264,14 @@ void LabelLayoutBaseTest::valueChanged(cocos2d::Ref *sender, cocos2d::extension:
_label->setBMFontSize(fontSize);
}
this->updateDrawNodeSize(_label->getContentSize());
//FIXME::When calling getLetter, the label Overflow feature will be invalid.
// auto letterSprite = _label->getLetter(1);
// auto moveBy = ScaleBy::create(1.0,2.0);
// letterSprite->stopAllActions();
// letterSprite->runAction(Sequence::create(moveBy, moveBy->clone()->reverse(), nullptr ));
//
// CCLOG("label line height = %f", _label->getLineHeight());
}
void LabelLayoutBaseTest::updateDrawNodeSize(const cocos2d::Size &drawNodeSize)
@ -2578,6 +2586,12 @@ LabelSystemFontTest::LabelSystemFontTest()
_label->setVerticalAlignment(TextVAlignment::CENTER);
_label->setOverflow(Label::Overflow::NORMAL);
_label->setSystemFontName("Hiragino Sans GB");
auto stepper = (ControlStepper*)this->getChildByName("stepper");
stepper->setEnabled(false);
auto checkbox = (CheckBox*)(this->getChildByName("toggleType"));
checkbox->setEnabled(false);
this->updateDrawNodeSize(_label->getContentSize());