From d4f62a8063b320cd87fa388ac9674e159d0e38b5 Mon Sep 17 00:00:00 2001 From: WenhaiLin Date: Tue, 14 Jul 2015 11:30:56 +0800 Subject: [PATCH] Label:improving the code readability. --- cocos/2d/CCLabel.cpp | 295 +++++++++++++++++++++------------------- cocos/2d/CCLabel.h | 141 +++++++++---------- cocos/2d/CCLabelTTF.cpp | 6 +- cocos/2d/CCLabelTTF.h | 3 +- 4 files changed, 228 insertions(+), 217 deletions(-) diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index a52080e421..182de4619a 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -162,7 +162,7 @@ Label* Label::create(const std::string& text, const std::string& font, float fon Label* Label::createWithSystemFont(const std::string& text, const std::string& font, float fontSize, const Size& dimensions /* = Size::ZERO */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, TextVAlignment vAlignment /* = TextVAlignment::TOP */) { - auto ret = new (std::nothrow) Label(nullptr,hAlignment,vAlignment); + auto ret = new (std::nothrow) Label(hAlignment,vAlignment); if (ret) { @@ -182,7 +182,7 @@ Label* Label::createWithSystemFont(const std::string& text, const std::string& f Label* Label::createWithTTF(const std::string& text, const std::string& fontFile, float fontSize, const Size& dimensions /* = Size::ZERO */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, TextVAlignment vAlignment /* = TextVAlignment::TOP */) { - auto ret = new (std::nothrow) Label(nullptr,hAlignment,vAlignment); + auto ret = new (std::nothrow) Label(hAlignment,vAlignment); if (ret && FileUtils::getInstance()->isFileExist(fontFile)) { @@ -202,9 +202,9 @@ Label* Label::createWithTTF(const std::string& text, const std::string& fontFile return nullptr; } -Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment /* = TextHAlignment::CENTER */, int maxLineWidth /* = 0 */) +Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment hAlignment /* = TextHAlignment::CENTER */, int maxLineWidth /* = 0 */) { - auto ret = new (std::nothrow) Label(nullptr,alignment); + auto ret = new (std::nothrow) Label(hAlignment); if (ret && FileUtils::getInstance()->isFileExist(ttfConfig.fontFilePath) && ret->setTTFConfig(ttfConfig)) { @@ -219,9 +219,9 @@ Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, return nullptr; } -Label* Label::createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& alignment /* = TextHAlignment::LEFT */, int maxLineWidth /* = 0 */, const Vec2& imageOffset /* = Vec2::ZERO */) +Label* Label::createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& hAlignment /* = TextHAlignment::LEFT */, int maxLineWidth /* = 0 */, const Vec2& imageOffset /* = Vec2::ZERO */) { - auto ret = new (std::nothrow) Label(nullptr,alignment); + auto ret = new (std::nothrow) Label(hAlignment); if (ret && ret->setBMFontFilePath(bmfontFilePath,imageOffset)) { @@ -326,36 +326,18 @@ bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHe return true; } -Label::Label(FontAtlas *atlas /* = nullptr */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, - TextVAlignment vAlignment /* = TextVAlignment::TOP */,bool useDistanceField /* = false */,bool useA8Shader /* = false */) -: _isOpacityModifyRGB(false) -, _contentDirty(false) -, _fontAtlas(atlas) -, _textSprite(nullptr) -, _compatibleMode(false) +Label::Label(TextHAlignment hAlignment /* = TextHAlignment::LEFT */, + TextVAlignment vAlignment /* = TextVAlignment::TOP */) +: _textSprite(nullptr) +, _shadowNode(nullptr) +, _fontAtlas(nullptr) , _reusedLetter(nullptr) -, _additionalKerning(0.0f) -, _commonLineHeight(0.0f) -, _lineBreakWithoutSpaces(false) , _horizontalKernings(nullptr) -, _maxLineWidth(0.0f) -, _labelDimensions(Size::ZERO) -, _labelWidth(0.0f) -, _labelHeight(0.0f) -, _hAlignment(hAlignment) -, _vAlignment(vAlignment) -, _currNumLines(-1) -, _fontScale(1.0f) -, _useDistanceField(useDistanceField) -, _useA8Shader(useA8Shader) -, _effectColorF(Color4F::BLACK) -, _uniformEffectColor(0) -, _shadowDirty(false) -, _blendFunc(BlendFunc::ALPHA_PREMULTIPLIED) -, _insideBounds(true) { setAnchorPoint(Vec2::ANCHOR_MIDDLE); reset(); + _hAlignment = hAlignment; + _vAlignment = vAlignment; _purgeTextureListener = EventListenerCustom::create(FontAtlas::CMD_PURGE_FONTATLAS, [this](EventCustom* event){ if (_fontAtlas && _currentLabelType == LabelType::TTF && event->getUserData() == _fontAtlas) @@ -394,27 +376,27 @@ Label::~Label() if (_fontAtlas) { + Node::removeAllChildrenWithCleanup(true); + CC_SAFE_RELEASE_NULL(_reusedLetter); + _batchNodes.clear(); FontAtlasCache::releaseFontAtlas(_fontAtlas); } _eventDispatcher->removeEventListener(_purgeTextureListener); _eventDispatcher->removeEventListener(_resetTextureListener); - CC_SAFE_RELEASE_NULL(_reusedLetter); CC_SAFE_RELEASE_NULL(_textSprite); CC_SAFE_RELEASE_NULL(_shadowNode); } void Label::reset() { - TTFConfig temp; - _fontConfig = temp; - - _systemFontDirty = false; - _systemFont = "Helvetica"; - _systemFontSize = 12; - + CC_SAFE_RELEASE_NULL(_textSprite); + CC_SAFE_RELEASE_NULL(_shadowNode); + Node::removeAllChildrenWithCleanup(true); + CC_SAFE_RELEASE_NULL(_reusedLetter); + _letters.clear(); _batchNodes.clear(); - + _lettersInfo.clear(); if (_fontAtlas) { FontAtlasCache::releaseFontAtlas(_fontAtlas); @@ -423,21 +405,53 @@ void Label::reset() _currentLabelType = LabelType::STRING_TEXTURE; _currLabelEffect = LabelEffect::NORMAL; - _shadowBlurRadius = 0; + _contentDirty = false; + _currNumLines = 0; + _limitShowCount = 0; + _currentUTF16String.clear(); + _originalUTF8String.clear(); - Node::removeAllChildrenWithCleanup(true); - _textSprite = nullptr; - _shadowNode = nullptr; + TTFConfig temp; + _fontConfig = temp; + _outlineSize = 0.f; + _bmFontPath = ""; + _systemFontDirty = false; + _systemFont = "Helvetica"; + _systemFontSize = 12; - CC_SAFE_RELEASE_NULL(_reusedLetter); + if (_horizontalKernings) + { + delete[] _horizontalKernings; + _horizontalKernings = nullptr; + } + _additionalKerning = 0.f; + _commonLineHeight = 0.f; + _maxLineWidth = 0.f; + _labelDimensions.width = 0.f; + _labelDimensions.height = 0.f; + _labelWidth = 0.f; + _labelHeight = 0.f; + _lineBreakWithoutSpaces = false; + _hAlignment = TextHAlignment::LEFT; + _vAlignment = TextVAlignment::TOP; + _effectColorF = Color4F::BLACK; _textColor = Color4B::WHITE; _textColorF = Color4F::WHITE; setColor(Color3B::WHITE); + _shadowDirty = false; _shadowEnabled = false; + _shadowBlurRadius = 0.f; + + _correctionScale = 1.f; + _useDistanceField = false; + _useA8Shader = false; _clipEnabled = false; _blendFuncDirty = false; + _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; + _isOpacityModifyRGB = false; + _insideBounds = true; } void Label::updateShaderProgram() @@ -539,7 +553,7 @@ bool Label::setTTFConfig(const TTFConfig& ttfConfig) updateShaderProgram(); if(ttfConfig.distanceFieldEnabled) { - this->setFontScale(1.0f * ttfConfig.fontSize / DistanceFieldFontSize); + this->setCorrectionScale(1.0f * ttfConfig.fontSize / DistanceFieldFontSize); } } @@ -624,7 +638,7 @@ void Label::setScale(float scale) { if (_useDistanceField) { - scale *= _fontScale; + scale *= _correctionScale; } Node::setScale(scale); } @@ -633,7 +647,7 @@ void Label::setScaleX(float scaleX) { if (_useDistanceField) { - scaleX *= _fontScale; + scaleX *= _correctionScale; } Node::setScaleX(scaleX); } @@ -642,7 +656,7 @@ void Label::setScaleY(float scaleY) { if (_useDistanceField) { - scaleY *= _fontScale; + scaleY *= _correctionScale; } Node::setScaleY(scaleY); } @@ -651,7 +665,7 @@ float Label::getScaleY() const { if (_useDistanceField) { - return _scaleY / _fontScale; + return _scaleY / _correctionScale; } else { @@ -663,7 +677,7 @@ float Label::getScaleX() const { if (_useDistanceField) { - return _scaleX / _fontScale; + return _scaleX / _correctionScale; } else { @@ -849,11 +863,10 @@ void Label::enableGlow(const Color4B& glowColor) _contentDirty = true; } _currLabelEffect = LabelEffect::GLOW; - _effectColor = glowColor; - _effectColorF.r = _effectColor.r / 255.0f; - _effectColorF.g = _effectColor.g / 255.0f; - _effectColorF.b = _effectColor.b / 255.0f; - _effectColorF.a = _effectColor.a / 255.0f; + _effectColorF.r = glowColor.r / 255.0f; + _effectColorF.g = glowColor.g / 255.0f; + _effectColorF.b = glowColor.b / 255.0f; + _effectColorF.a = glowColor.a / 255.0f; updateShaderProgram(); } } @@ -877,9 +890,12 @@ void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = -1 */ setTTFConfig(_fontConfig); } } - else if (_effectColor != outlineColor || _outlineSize != outlineSize) + else if (_effectColorF != outlineColor || _outlineSize != outlineSize) { - _effectColor = outlineColor; + _effectColorF.r = outlineColor.r / 255.f; + _effectColorF.g = outlineColor.g / 255.f; + _effectColorF.b = outlineColor.b / 255.f; + _effectColorF.a = outlineColor.a / 255.f; _outlineSize = outlineSize; _currLabelEffect = LabelEffect::OUTLINE; _contentDirty = true; @@ -896,7 +912,6 @@ void Label::enableShadow(const Color4B& shadowColor /* = Color4B::BLACK */,const _shadowOffset.width = offset.width * contentScaleFactor; _shadowOffset.height = offset.height * contentScaleFactor; //TODO: support blur for shadow - _shadowBlurRadius = 0; _shadowColor3B.r = shadowColor.r; _shadowColor3B.g = shadowColor.g; @@ -905,13 +920,14 @@ void Label::enableShadow(const Color4B& shadowColor /* = Color4B::BLACK */,const if (!_systemFontDirty && !_contentDirty && _textSprite) { + auto& fontDef = _getFontDefinition(); if (_shadowNode) { if (shadowColor != _shadowColor4F) { _shadowNode->release(); _shadowNode = nullptr; - createShadowSpriteForSystemFont(); + createShadowSpriteForSystemFont(fontDef); } else { @@ -920,7 +936,7 @@ void Label::enableShadow(const Color4B& shadowColor /* = Color4B::BLACK */,const } else { - createShadowSpriteForSystemFont(); + createShadowSpriteForSystemFont(fontDef); } } @@ -982,59 +998,18 @@ void Label::disableEffect(LabelEffect effect) } } -void Label::setFontScale(float fontScale) +void Label::setCorrectionScale(float correctionScale) { - _fontScale = fontScale * CC_CONTENT_SCALE_FACTOR(); - Node::setScale(_fontScale); + _correctionScale = correctionScale * CC_CONTENT_SCALE_FACTOR(); + Node::setScale(_correctionScale); } -void Label::createSpriteForSystemFont() +void Label::createSpriteForSystemFont(const FontDefinition& fontDef) { _currentLabelType = LabelType::STRING_TEXTURE; - if (!_compatibleMode) - { - _fontDefinition._fontName = _systemFont; - _fontDefinition._fontSize = _systemFontSize; - - _fontDefinition._alignment = _hAlignment; - _fontDefinition._vertAlignment = _vAlignment; - - _fontDefinition._dimensions.width = _labelWidth; - _fontDefinition._dimensions.height = _labelHeight; - - _fontDefinition._fontFillColor.r = _textColor.r; - _fontDefinition._fontFillColor.g = _textColor.g; - _fontDefinition._fontFillColor.b = _textColor.b; - _fontDefinition._fontAlpha = _textColor.a; - - _fontDefinition._shadow._shadowEnabled = false; - - if (_currLabelEffect == LabelEffect::OUTLINE && _outlineSize > 0) - { - _fontDefinition._stroke._strokeEnabled = true; - _fontDefinition._stroke._strokeSize = _outlineSize; - _fontDefinition._stroke._strokeColor.r = _effectColor.r; - _fontDefinition._stroke._strokeColor.g = _effectColor.g; - _fontDefinition._stroke._strokeColor.b = _effectColor.b; - _fontDefinition._stroke._strokeAlpha = _effectColor.a; - } - else - { - _fontDefinition._stroke._strokeEnabled = false; - } - -#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) - if (_fontDefinition._stroke._strokeEnabled) - { - CCLOGERROR("Currently only supported on iOS and Android!"); - } - _fontDefinition._stroke._strokeEnabled = false; -#endif - } - auto texture = new (std::nothrow) Texture2D; - texture->initWithString(_originalUTF8String.c_str(), _fontDefinition); + texture->initWithString(_originalUTF8String.c_str(), fontDef); _textSprite = Sprite::createWithTexture(texture); //set camera mask using label's camera mask, because _textSprite may be null when setting camera mask to label @@ -1052,16 +1027,16 @@ void Label::createSpriteForSystemFont() _textSprite->updateDisplayedOpacity(_displayedOpacity); } -void Label::createShadowSpriteForSystemFont() +void Label::createShadowSpriteForSystemFont(const FontDefinition& fontDef) { - if (!_fontDefinition._stroke._strokeEnabled && _fontDefinition._fontFillColor == _shadowColor3B - && (_fontDefinition._fontAlpha == _shadowOpacity)) + if (!fontDef._stroke._strokeEnabled && fontDef._fontFillColor == _shadowColor3B + && (fontDef._fontAlpha == _shadowOpacity)) { _shadowNode = Sprite::createWithTexture(_textSprite->getTexture()); } else { - auto shadowFontDefinition = _fontDefinition; + FontDefinition shadowFontDefinition = fontDef; shadowFontDefinition._fontFillColor.r = _shadowColor3B.r; shadowFontDefinition._fontFillColor.g = _shadowColor3B.g; shadowFontDefinition._fontFillColor.b = _shadowColor3B.b; @@ -1108,20 +1083,35 @@ void Label::setCameraMask(unsigned short mask, bool applyChildren) void Label::setFontDefinition(const FontDefinition& textDefinition) { - _fontDefinition = textDefinition; + _systemFont = textDefinition._fontName; + _systemFontSize = textDefinition._fontSize; + _hAlignment = textDefinition._alignment; + _vAlignment = textDefinition._vertAlignment; + setDimensions(textDefinition._dimensions.width, textDefinition._dimensions.height); + Color4B textColor = Color4B(textDefinition._fontFillColor); + textColor.a = textDefinition._fontAlpha; + setTextColor(textColor); + #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) - if (_fontDefinition._stroke._strokeEnabled) + if (textDefinition._stroke._strokeEnabled) { CCLOGERROR("Currently only supported on iOS and Android!"); } - _fontDefinition._stroke._strokeEnabled = false; -#endif - if (_fontDefinition._shadow._shadowEnabled) + _outlineSize = 0.f; +#else + if (textDefinition._stroke._strokeEnabled && textDefinition._stroke._strokeSize > 0.f) { - _fontDefinition._shadow._shadowEnabled = false; - enableShadow(Color4B(0,0,0,255 * _fontDefinition._shadow._shadowOpacity),_fontDefinition._shadow._shadowOffset,_fontDefinition._shadow._shadowBlur); + Color4B outlineColor = Color4B(textDefinition._stroke._strokeColor); + outlineColor.a = textDefinition._stroke._strokeAlpha; + enableOutline(outlineColor, textDefinition._stroke._strokeSize); + } +#endif + + if (textDefinition._shadow._shadowEnabled) + { + enableShadow(Color4B(0, 0, 0, 255 * textDefinition._shadow._shadowOpacity), + textDefinition._shadow._shadowOffset, textDefinition._shadow._shadowBlur); } - _compatibleMode = true; } void Label::updateContent() @@ -1156,10 +1146,11 @@ void Label::updateContent() } else { - createSpriteForSystemFont(); + auto& fontDef = _getFontDefinition(); + createSpriteForSystemFont(fontDef); if (_shadowEnabled) { - createShadowSpriteForSystemFont(); + createShadowSpriteForSystemFont(fontDef); } } _contentDirty = false; @@ -1297,8 +1288,7 @@ void Label::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t pare uint32_t flags = processParentFlags(parentTransform, parentFlags); - if (!_originalUTF8String.empty() && _shadowEnabled && _shadowBlurRadius <= 0 - && (_shadowDirty || (flags & FLAGS_DIRTY_MASK))) + if (!_originalUTF8String.empty() && _shadowEnabled && (_shadowDirty || (flags & FLAGS_DIRTY_MASK))) { _position.x += _shadowOffset.width; _position.y += _shadowOffset.height; @@ -1526,11 +1516,6 @@ int Label::getStringLength() const } // RGBA protocol -bool Label::isOpacityModifyRGB() const -{ - return _isOpacityModifyRGB; -} - void Label::setOpacityModifyRGB(bool isOpacityModifyRGB) { if (isOpacityModifyRGB != _isOpacityModifyRGB) @@ -1572,20 +1557,16 @@ void Label::setTextColor(const Color4B &color) { CCASSERT(_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::STRING_TEXTURE, "Only supported system font and ttf!"); + if (_currentLabelType == LabelType::STRING_TEXTURE && _textColor != color) + { + _contentDirty = true; + } + _textColor = color; _textColorF.r = _textColor.r / 255.0f; _textColorF.g = _textColor.g / 255.0f; _textColorF.b = _textColor.b / 255.0f; _textColorF.a = _textColor.a / 255.0f; - - if (_currentLabelType == LabelType::STRING_TEXTURE) - { - if (_fontDefinition._fontFillColor.r != _textColor.r || _fontDefinition._fontFillColor.g != _textColor.g - || _fontDefinition._fontFillColor.b != _textColor.b || _fontDefinition._fontAlpha != _textColor.a) - { - _contentDirty = true; - } - } } void Label::updateColor() @@ -1680,4 +1661,44 @@ void Label::removeChild(Node* child, bool cleanup /* = true */) } } +FontDefinition Label::_getFontDefinition() const +{ + FontDefinition systemFontDef; + systemFontDef._fontName = _systemFont; + systemFontDef._fontSize = _systemFontSize; + systemFontDef._alignment = _hAlignment; + systemFontDef._vertAlignment = _vAlignment; + systemFontDef._dimensions.width = _labelWidth; + systemFontDef._dimensions.height = _labelHeight; + systemFontDef._fontFillColor.r = _textColor.r; + systemFontDef._fontFillColor.g = _textColor.g; + systemFontDef._fontFillColor.b = _textColor.b; + systemFontDef._fontAlpha = _textColor.a; + systemFontDef._shadow._shadowEnabled = false; + + if (_currLabelEffect == LabelEffect::OUTLINE && _outlineSize > 0.f) + { + systemFontDef._stroke._strokeEnabled = true; + systemFontDef._stroke._strokeSize = _outlineSize; + systemFontDef._stroke._strokeColor.r = _effectColorF.r * 255; + systemFontDef._stroke._strokeColor.g = _effectColorF.g * 255; + systemFontDef._stroke._strokeColor.b = _effectColorF.b * 255; + systemFontDef._stroke._strokeAlpha = _effectColorF.a * 255; + } + else + { + systemFontDef._stroke._strokeEnabled = false; + } + +#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) + if (systemFontDef._stroke._strokeEnabled) + { + CCLOGERROR("Currently only supported on iOS and Android!"); + } + systemFontDef._stroke._strokeEnabled = false; +#endif + + return systemFontDef; +} + NS_CC_END diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index db9f19fafa..f7389f55ab 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -162,7 +162,8 @@ public: * @return An automatically released Label object. * @see TTFConfig setTTFConfig setMaxLineWidth */ - static Label* createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment hAlignment = TextHAlignment::LEFT, int maxLineWidth = 0); + static Label* createWithTTF(const TTFConfig& ttfConfig, const std::string& text, + TextHAlignment hAlignment = TextHAlignment::LEFT, int maxLineWidth = 0); /** * Allocates and initializes a Label, with a bitmap font file. @@ -441,7 +442,7 @@ public: virtual const BlendFunc& getBlendFunc() const override { return _blendFunc; } virtual void setBlendFunc(const BlendFunc &blendFunc) override; - virtual bool isOpacityModifyRGB() const override; + virtual bool isOpacityModifyRGB() const override { return _isOpacityModifyRGB; } virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override; virtual void updateDisplayedColor(const Color3B& parentColor) override; virtual void updateDisplayedOpacity(GLubyte parentOpacity) override; @@ -455,7 +456,6 @@ public: virtual std::string getDescription() const override; virtual const Size& getContentSize() const override; - virtual Rect getBoundingBox() const override; virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override; @@ -469,13 +469,8 @@ public: CC_DEPRECATED_ATTRIBUTE static Label* create(const std::string& text, const std::string& font, float fontSize, const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT, TextVAlignment vAlignment = TextVAlignment::TOP); - CC_DEPRECATED_ATTRIBUTE virtual void setFontDefinition(const FontDefinition& textDefinition); - CC_DEPRECATED_ATTRIBUTE const FontDefinition& getFontDefinition() const { return _fontDefinition; } - - /** - * @deprecated Use `getLineHeight` instead. - */ + CC_DEPRECATED_ATTRIBUTE FontDefinition getFontDefinition() const { return _getFontDefinition(); } CC_DEPRECATED_ATTRIBUTE int getCommonLineHeight() const { return (int)getLineHeight();} CC_CONSTRUCTOR_ACCESS: @@ -483,8 +478,8 @@ CC_CONSTRUCTOR_ACCESS: * Constructor of Label. * @js NA */ - Label(FontAtlas *atlas = nullptr, TextHAlignment hAlignment = TextHAlignment::LEFT, - TextVAlignment vAlignment = TextVAlignment::TOP,bool useDistanceField = false,bool useA8Shader = false); + Label(TextHAlignment hAlignment = TextHAlignment::LEFT, + TextVAlignment vAlignment = TextVAlignment::TOP); /** * Destructor of Label. @@ -494,132 +489,124 @@ CC_CONSTRUCTOR_ACCESS: virtual ~Label(); protected: - void onDraw(const Mat4& transform, bool transformUpdated); - void onDrawShadow(GLProgram* glProgram); - struct LetterInfo { FontLetterDefinition def; - Vec2 position; Size contentSize; int atlasIndex; }; - enum class LabelType { + enum class LabelType { TTF, BMFONT, CHARMAP, STRING_TEXTURE }; - virtual void setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled = false, bool useA8Shader = false); + virtual void setFontAtlas(FontAtlas* atlas, bool distanceFieldEnabled = false, bool useA8Shader = false); - bool recordLetterInfo(const cocos2d::Vec2& point,const FontLetterDefinition& letterDef, int spriteIndex); - bool recordPlaceholderInfo(int spriteIndex); - - void setFontScale(float fontScale); - - virtual void alignText(); - - bool computeHorizontalKernings(const std::u16string& stringToRender); + void setCorrectionScale(float fontScale); void computeStringNumLines(); + void onDraw(const Mat4& transform, bool transformUpdated); + void onDrawShadow(GLProgram* glProgram); + void drawSelf(Renderer* renderer, uint32_t flags); + + virtual void alignText(); + bool computeHorizontalKernings(const std::u16string& stringToRender); + bool recordLetterInfo(const cocos2d::Vec2& point,const FontLetterDefinition& letterDef, int spriteIndex); + bool recordPlaceholderInfo(int spriteIndex); + void updateQuads(); - virtual void updateColor() override; + void createSpriteForSystemFont(const FontDefinition& fontDef); + void createShadowSpriteForSystemFont(const FontDefinition& fontDef); virtual void updateShaderProgram(); - void createSpriteForSystemFont(); - - void createShadowSpriteForSystemFont(); - void reset(); - void drawSelf(Renderer* renderer, uint32_t flags); + FontDefinition _getFontDefinition() const; + + virtual void updateColor() override; + + LabelType _currentLabelType; + bool _contentDirty; + std::u16string _currentUTF16String; + std::string _originalUTF8String; + int _currNumLines; std::string _bmFontPath; - - bool _isOpacityModifyRGB; - bool _contentDirty; + TTFConfig _fontConfig; + float _outlineSize; bool _systemFontDirty; std::string _systemFont; - float _systemFontSize; - LabelType _currentLabelType; - - Vector _batchNodes; - FontAtlas * _fontAtlas; - std::vector _lettersInfo; - EventListenerCustom* _purgeTextureListener; - EventListenerCustom* _resetTextureListener; - - TTFConfig _fontConfig; - - //compatibility with older LabelTTF + float _systemFontSize; Sprite* _textSprite; - FontDefinition _fontDefinition; - bool _compatibleMode; + Sprite* _shadowNode; + + FontAtlas* _fontAtlas; + Vector _batchNodes; + std::vector _lettersInfo; //! used for optimization Sprite *_reusedLetter; Rect _reusedRect; int _limitShowCount; - float _additionalKerning; + //layout relevant properties. float _commonLineHeight; - bool _lineBreakWithoutSpaces; - int * _horizontalKernings; - + float _additionalKerning; + int* _horizontalKernings; + bool _lineBreakWithoutSpaces; float _maxLineWidth; - Size _labelDimensions; + Size _labelDimensions; float _labelWidth; float _labelHeight; TextHAlignment _hAlignment; TextVAlignment _vAlignment; - int _currNumLines; - std::u16string _currentUTF16String; - std::string _originalUTF8String; + //the correction scale for distance field. + float _correctionScale; - float _fontScale; + LabelEffect _currLabelEffect; + Color4F _effectColorF; + Color4B _textColor; + Color4F _textColorF; + CustomCommand _customCommand; + Mat4 _shadowTransform; + GLuint _uniformEffectColor; + GLuint _uniformTextColor; bool _useDistanceField; bool _useA8Shader; - LabelEffect _currLabelEffect; - Color4B _effectColor; - Color4F _effectColorF; - - GLuint _uniformEffectColor; - GLuint _uniformTextColor; - CustomCommand _customCommand; - - bool _shadowDirty; - bool _shadowEnabled; - Size _shadowOffset; - int _shadowBlurRadius; - Mat4 _shadowTransform; + bool _shadowDirty; + bool _shadowEnabled; + Size _shadowOffset; + Color4F _shadowColor4F; Color3B _shadowColor3B; GLubyte _shadowOpacity; - Sprite* _shadowNode; - - int _outlineSize; - - Color4B _textColor; - Color4F _textColorF; + float _shadowBlurRadius; bool _clipEnabled; bool _blendFuncDirty; BlendFunc _blendFunc; - /// whether or not the sprite was inside bounds the previous frame + + /// whether or not the label was inside bounds the previous frame bool _insideBounds; + bool _isOpacityModifyRGB; + std::unordered_map _letters; + EventListenerCustom* _purgeTextureListener; + EventListenerCustom* _resetTextureListener; + private: CC_DISALLOW_COPY_AND_ASSIGN(Label); diff --git a/cocos/2d/CCLabelTTF.cpp b/cocos/2d/CCLabelTTF.cpp index 205cce2c3e..c19c037fc5 100644 --- a/cocos/2d/CCLabelTTF.cpp +++ b/cocos/2d/CCLabelTTF.cpp @@ -223,9 +223,11 @@ void LabelTTF::setTextDefinition(const FontDefinition& theDefinition) _contentDirty = true; } -const FontDefinition& LabelTTF::getTextDefinition() const +const FontDefinition& LabelTTF::getTextDefinition() { - return _renderLabel->getFontDefinition(); + auto fontDef = _renderLabel->getFontDefinition(); + memcpy(&_fontDef, &fontDef, sizeof(FontDefinition)); + return _fontDef; } void LabelTTF::setBlendFunc(const BlendFunc &blendFunc) diff --git a/cocos/2d/CCLabelTTF.h b/cocos/2d/CCLabelTTF.h index c4313a961f..cae01b65c1 100644 --- a/cocos/2d/CCLabelTTF.h +++ b/cocos/2d/CCLabelTTF.h @@ -100,7 +100,7 @@ public: void setTextDefinition(const FontDefinition& theDefinition); /** get the text definition used by this label */ - const FontDefinition& getTextDefinition() const; + const FontDefinition& getTextDefinition(); @@ -163,6 +163,7 @@ public: protected: Label* _renderLabel; bool _contentDirty; + FontDefinition _fontDef; };