From bc68c6b62940672423396091b6802d2426a95983 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Thu, 13 Mar 2014 18:46:35 +0800 Subject: [PATCH 1/3] Refactor setter of label's font name and font size. --- cocos/2d/CCLabel.cpp | 258 ++++++++++++++++++++++--------------------- cocos/2d/CCLabel.h | 10 +- 2 files changed, 139 insertions(+), 129 deletions(-) diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 574a3f33ec..1f2ced4179 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -208,15 +208,15 @@ bool Label::setCharMap(const std::string& plistFile) FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(plistFile); if (!newAtlas) - return false; - - if (initWithFontAtlas(newAtlas)) { - _currentLabelType = LabelType::CHARMAP; - return true; + reset(); + return false; } - return false; + initWithFontAtlas(newAtlas); + _currentLabelType = LabelType::CHARMAP; + + return true; } bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) @@ -224,15 +224,15 @@ bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int st FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(texture,itemWidth,itemHeight,startCharMap); if (!newAtlas) - return false; - - if (initWithFontAtlas(newAtlas)) { - _currentLabelType = LabelType::CHARMAP; - return true; + reset(); + return false; } - return false; + initWithFontAtlas(newAtlas); + _currentLabelType = LabelType::CHARMAP; + + return true; } bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) @@ -240,15 +240,15 @@ bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHe FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(charMapFile,itemWidth,itemHeight,startCharMap); if (!newAtlas) - return false; - - if (initWithFontAtlas(newAtlas)) { - _currentLabelType = LabelType::CHARMAP; - return true; + reset(); + return false; } - return false; + initWithFontAtlas(newAtlas); + _currentLabelType = LabelType::CHARMAP; + + return true; } Label::Label(FontAtlas *atlas /* = nullptr */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, @@ -274,17 +274,10 @@ Label::Label(FontAtlas *atlas /* = nullptr */, TextHAlignment hAlignment /* = Te , _currNumLines(-1) , _textSprite(nullptr) , _contentDirty(false) -, _currentLabelType(LabelType::STRING_TEXTURE) -, _currLabelEffect(LabelEffect::NORMAL) -, _shadowBlurRadius(0) { _cascadeColorEnabled = true; - _batchNodes.push_back(this); - - _fontDefinition._fontName = "Helvetica"; - _fontDefinition._fontSize = 12; - _fontDefinition._alignment = TextHAlignment::LEFT; - _fontDefinition._vertAlignment = TextVAlignment::TOP; + + reset(); } Label::~Label() @@ -301,25 +294,37 @@ Label::~Label() CC_SAFE_RELEASE_NULL(_reusedLetter); } -bool Label::init() -{ - bool ret = true; - if(_fontAtlas) - { - ret = SpriteBatchNode::initWithTexture(_fontAtlas->getTexture(0), 30); - if (_reusedLetter == nullptr) - { - _reusedLetter = Sprite::createWithTexture(_fontAtlas->getTexture(0)); - _reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB); - _reusedLetter->retain(); - _reusedLetter->setAnchorPoint(Point::ANCHOR_TOP_LEFT); - _reusedLetter->setBatchNode(this); - } - } - _currLabelEffect = LabelEffect::NORMAL; - initProgram(); +void Label::reset() +{ + TTFConfig temp; + _fontConfig = temp; - return ret; + _fontDefinition._fontName = "Helvetica"; + _fontDefinition._fontSize = 12; + _fontDefinition._alignment = _hAlignment; + _fontDefinition._vertAlignment = _vAlignment; + + _fontDirty = false; + _fontName = "Helvetica"; + _fontSize = 12; + + _batchNodes.clear(); + _batchNodes.push_back(this); + + if (_fontAtlas) + { + FontAtlasCache::releaseFontAtlas(_fontAtlas); + _fontAtlas = nullptr; + } + + _currentLabelType = LabelType::STRING_TEXTURE; + _currLabelEffect = LabelEffect::NORMAL; + _shadowBlurRadius = 0; + + Node::removeAllChildrenWithCleanup(true); + _textSprite = nullptr; + + CC_SAFE_RELEASE_NULL(_reusedLetter); } void Label::initProgram() @@ -351,30 +356,31 @@ void Label::initProgram() bool Label::initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false */, bool useA8Shader /* = false */) { - FontAtlas *oldAtlas = _fontAtlas; - bool oldDistanceFieldEnable = _useDistanceField; - bool oldA8ShaderEnabel = _useA8Shader; + if (atlas == _fontAtlas) + { + FontAtlasCache::releaseFontAtlas(atlas); + return true; + } + + if (_fontAtlas) + { + FontAtlasCache::releaseFontAtlas(_fontAtlas); + _fontAtlas = nullptr; + } _fontAtlas = atlas; - _useDistanceField = distanceFieldEnabled; - _useA8Shader = useA8Shader; - - bool ret = Label::init(); - if (oldAtlas) + SpriteBatchNode::initWithTexture(_fontAtlas->getTexture(0), 30); + if (_reusedLetter == nullptr) { - if (ret) - { - FontAtlasCache::releaseFontAtlas(oldAtlas); - } - else - { - _fontAtlas = oldAtlas; - _useDistanceField = oldDistanceFieldEnable; - _useA8Shader = oldA8ShaderEnabel; - Label::init(); - - FontAtlasCache::releaseFontAtlas(atlas); - } + _reusedLetter = Sprite::createWithTexture(_fontAtlas->getTexture(0)); + _reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB); + _reusedLetter->retain(); + _reusedLetter->setAnchorPoint(Point::ANCHOR_TOP_LEFT); + _reusedLetter->setBatchNode(this); + } + else + { + _reusedLetter->setTexture(_fontAtlas->getTexture(0)); } if (_fontAtlas) @@ -382,8 +388,12 @@ bool Label::initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = fa _commonLineHeight = _fontAtlas->getCommonLineHeight(); _contentDirty = true; } + _useDistanceField = distanceFieldEnabled; + _useA8Shader = useA8Shader; + _currLabelEffect = LabelEffect::NORMAL; + initProgram(); - return ret; + return true; } bool Label::setTTFConfig(const TTFConfig& ttfConfig) @@ -391,30 +401,30 @@ bool Label::setTTFConfig(const TTFConfig& ttfConfig) FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(ttfConfig); if (!newAtlas) + { + reset(); return false; + } - if (initWithFontAtlas(newAtlas,ttfConfig.distanceFieldEnabled,true)) + _fontConfig = ttfConfig; + if (_fontConfig.outlineSize > 0) { - _fontConfig = ttfConfig; - if (ttfConfig.outlineSize > 0) - { - _fontConfig.distanceFieldEnabled = false; - _useDistanceField = false; - _useA8Shader = false; - _currLabelEffect = LabelEffect::OUTLINE; - initProgram(); - } - else if(ttfConfig.distanceFieldEnabled) - { - this->setFontScale(1.0f * ttfConfig.fontSize / DefultFontSize); - } - _currentLabelType = LabelType::TTF; - return true; + _fontConfig.distanceFieldEnabled = false; + _useDistanceField = false; + _useA8Shader = false; + _currLabelEffect = LabelEffect::OUTLINE; + initProgram(); } - else + else if(ttfConfig.distanceFieldEnabled) { - return false; + this->setFontScale(1.0f * ttfConfig.fontSize / DefultFontSize); } + + initWithFontAtlas(newAtlas,ttfConfig.distanceFieldEnabled,true); + + _currentLabelType = LabelType::TTF; + + return true; } bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Point& imageOffset /* = Point::ZERO */) @@ -422,21 +432,23 @@ bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Point& im FontAtlas *newAtlas = FontAtlasCache::getFontAtlasFNT(bmfontFilePath,imageOffset); if (!newAtlas) - return false; - - if (initWithFontAtlas(newAtlas)) { - _currentLabelType = LabelType::BMFONT; - return true; + reset(); + return false; } - return false; + initWithFontAtlas(newAtlas); + _currentLabelType = LabelType::BMFONT; + + return true; } void Label::setFontDefinition(const FontDefinition& textDefinition) { + reset(); _fontDefinition = textDefinition; - _currentLabelType = LabelType::STRING_TEXTURE; + _fontName = textDefinition._fontName; + _fontSize = textDefinition._fontSize; _contentDirty = true; } @@ -940,12 +952,30 @@ void Label::updateContent() _contentDirty = false; } +void Label::updateFont() +{ + if (_fontName.find('.') != _fontName.npos) + { + _fontConfig.fontFilePath = _fontName; + _fontConfig.fontSize = _fontSize; + setTTFConfig(_fontConfig); + } + _fontDefinition._fontName = _fontName; + _fontDefinition._fontSize = _fontSize; + _contentDirty = true; + _fontDirty = false; +} + void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { if (! _visible || _originalUTF8String.empty()) { return; } + if (_fontDirty) + { + updateFont(); + } if (_contentDirty) { updateContent(); @@ -987,19 +1017,10 @@ void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parent void Label::setFontName(const std::string& fontName) { - if (fontName.find('.') != fontName.npos) + if (fontName != _fontName) { - auto config = _fontConfig; - config.fontFilePath = fontName; - if (setTTFConfig(config)) - { - return; - } - } - if (_fontDefinition._fontName != fontName) - { - _fontDefinition._fontName = fontName; - _contentDirty = true; + _fontName = fontName; + _fontDirty = true; } } @@ -1016,29 +1037,10 @@ const std::string& Label::getFontName() const void Label::setFontSize(int fontSize) { - if (_currentLabelType == LabelType::TTF) + if (_fontSize != fontSize) { - if (_fontConfig.fontSize == fontSize) - { - return; - } - if (_fontConfig.distanceFieldEnabled) - { - _fontConfig.fontSize = fontSize; - this->setFontScale(1.0f * fontSize / DefultFontSize); - } - else - { - auto fontConfig = _fontConfig; - fontConfig.fontSize = fontSize; - setTTFConfig(fontConfig); - } - } - else if(_fontDefinition._fontSize != fontSize) - { - _fontDefinition._fontSize = fontSize; - _fontConfig.fontSize = fontSize; - _contentDirty = true; + _fontSize = fontSize; + _fontDirty = true; } } @@ -1204,6 +1206,10 @@ std::string Label::getDescription() const const Size& Label::getContentSize() const { + if (_fontDirty) + { + const_cast(this)->updateFont(); + } if (_contentDirty) { const_cast(this)->updateContent(); diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index cbfcacf985..4045438ee8 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -59,7 +59,7 @@ typedef struct _ttfConfig bool distanceFieldEnabled; int outlineSize; - _ttfConfig(const char* filePath = "",int size = 36, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC, + _ttfConfig(const char* filePath = "",int size = 12, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC, const char *customGlyphCollection = nullptr,bool useDistanceField = false,int outline = 0) :fontFilePath(filePath) ,fontSize(size) @@ -265,8 +265,6 @@ protected: bool recordPlaceholderInfo(int spriteIndex); void setFontScale(float fontScale); - - virtual bool init(); virtual void alignText(); @@ -285,8 +283,14 @@ protected: void createSpriteWithFontDefinition(); + void updateFont(); + void reset(); + bool _isOpacityModifyRGB; bool _contentDirty; + bool _fontDirty; + std::string _fontName; + int _fontSize; LabelType _currentLabelType; std::vector _batchNodes; From 62d0fa8564f264ee6377f5fd957530eb7c76f40c Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Thu, 13 Mar 2014 20:52:33 +0800 Subject: [PATCH 2/3] label:rename some API for clearly. --- cocos/2d/CCFontAtlasCache.cpp | 2 +- cocos/2d/CCLabel.cpp | 32 +++++++++++++++----------------- cocos/2d/CCLabel.h | 6 +++--- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/cocos/2d/CCFontAtlasCache.cpp b/cocos/2d/CCFontAtlasCache.cpp index ab76eeb28a..60494a0222 100644 --- a/cocos/2d/CCFontAtlasCache.cpp +++ b/cocos/2d/CCFontAtlasCache.cpp @@ -46,7 +46,7 @@ FontAtlas * FontAtlasCache::getFontAtlasTTF(const TTFConfig & config) int fontSize = config.fontSize; if (useDistanceField) { - fontSize = Label::DefultFontSize; + fontSize = Label::DistanceFieldFontSize; } std::string atlasName = generateFontName(config.fontFilePath, fontSize, GlyphCollection::DYNAMIC, useDistanceField); diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 1f2ced4179..06b2e8f52e 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -36,7 +36,7 @@ NS_CC_BEGIN -const int Label::DefultFontSize = 50; +const int Label::DistanceFieldFontSize = 50; Label* Label::create() { @@ -213,7 +213,7 @@ bool Label::setCharMap(const std::string& plistFile) return false; } - initWithFontAtlas(newAtlas); + setFontAtlas(newAtlas); _currentLabelType = LabelType::CHARMAP; return true; @@ -229,7 +229,7 @@ bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int st return false; } - initWithFontAtlas(newAtlas); + setFontAtlas(newAtlas); _currentLabelType = LabelType::CHARMAP; return true; @@ -245,7 +245,7 @@ bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHe return false; } - initWithFontAtlas(newAtlas); + setFontAtlas(newAtlas); _currentLabelType = LabelType::CHARMAP; return true; @@ -327,7 +327,7 @@ void Label::reset() CC_SAFE_RELEASE_NULL(_reusedLetter); } -void Label::initProgram() +void Label::configureShaderProgram() { switch (_currLabelEffect) { @@ -354,12 +354,12 @@ void Label::initProgram() _uniformEffectColor = glGetUniformLocation(_shaderProgram->getProgram(), "v_effectColor"); } -bool Label::initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false */, bool useA8Shader /* = false */) +void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false */, bool useA8Shader /* = false */) { if (atlas == _fontAtlas) { FontAtlasCache::releaseFontAtlas(atlas); - return true; + return; } if (_fontAtlas) @@ -391,9 +391,7 @@ bool Label::initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = fa _useDistanceField = distanceFieldEnabled; _useA8Shader = useA8Shader; _currLabelEffect = LabelEffect::NORMAL; - initProgram(); - - return true; + configureShaderProgram(); } bool Label::setTTFConfig(const TTFConfig& ttfConfig) @@ -413,14 +411,14 @@ bool Label::setTTFConfig(const TTFConfig& ttfConfig) _useDistanceField = false; _useA8Shader = false; _currLabelEffect = LabelEffect::OUTLINE; - initProgram(); + configureShaderProgram(); } else if(ttfConfig.distanceFieldEnabled) { - this->setFontScale(1.0f * ttfConfig.fontSize / DefultFontSize); + this->setFontScale(1.0f * ttfConfig.fontSize / DistanceFieldFontSize); } - initWithFontAtlas(newAtlas,ttfConfig.distanceFieldEnabled,true); + setFontAtlas(newAtlas,ttfConfig.distanceFieldEnabled,true); _currentLabelType = LabelType::TTF; @@ -437,7 +435,7 @@ bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Point& im return false; } - initWithFontAtlas(newAtlas); + setFontAtlas(newAtlas); _currentLabelType = LabelType::BMFONT; return true; @@ -756,7 +754,7 @@ void Label::enableGlow(const Color3B& glowColor) return; _currLabelEffect = LabelEffect::GLOW; _effectColor = glowColor; - initProgram(); + configureShaderProgram(); } void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */) @@ -772,7 +770,7 @@ void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */) auto config = _fontConfig; config.outlineSize = outlineSize; setTTFConfig(config); - initProgram(); + configureShaderProgram(); } } _fontDefinition._stroke._strokeEnabled = true; @@ -809,7 +807,7 @@ void Label::disableEffect() setTTFConfig(_fontConfig); } _currLabelEffect = LabelEffect::NORMAL; - initProgram(); + configureShaderProgram(); _contentDirty = true; } diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index 4045438ee8..d92745e366 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -78,7 +78,7 @@ typedef struct _ttfConfig class CC_DLL Label : public SpriteBatchNode, public LabelProtocol { public: - static const int DefultFontSize; + static const int DistanceFieldFontSize; static Label* create(); @@ -259,7 +259,7 @@ protected: */ virtual ~Label(); - virtual bool initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled = false, bool useA8Shader = false); + virtual void setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled = false, bool useA8Shader = false); bool recordLetterInfo(const cocos2d::Point& point,const FontLetterDefinition& letterDef, int spriteIndex); bool recordPlaceholderInfo(int spriteIndex); @@ -277,7 +277,7 @@ protected: virtual void updateColor() override; - virtual void initProgram(); + virtual void configureShaderProgram(); void drawShadowWithoutBlur(); From fe6f485a6b526bbeeaade0270c8eefb4cea4392a Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Thu, 13 Mar 2014 21:00:36 +0800 Subject: [PATCH 3/3] label:rename some API for clearly. --- cocos/2d/CCLabel.cpp | 12 ++++++------ cocos/2d/CCLabel.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 06b2e8f52e..3cede4756a 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -327,7 +327,7 @@ void Label::reset() CC_SAFE_RELEASE_NULL(_reusedLetter); } -void Label::configureShaderProgram() +void Label::updateShaderProgram() { switch (_currLabelEffect) { @@ -391,7 +391,7 @@ void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false * _useDistanceField = distanceFieldEnabled; _useA8Shader = useA8Shader; _currLabelEffect = LabelEffect::NORMAL; - configureShaderProgram(); + updateShaderProgram(); } bool Label::setTTFConfig(const TTFConfig& ttfConfig) @@ -411,7 +411,7 @@ bool Label::setTTFConfig(const TTFConfig& ttfConfig) _useDistanceField = false; _useA8Shader = false; _currLabelEffect = LabelEffect::OUTLINE; - configureShaderProgram(); + updateShaderProgram(); } else if(ttfConfig.distanceFieldEnabled) { @@ -754,7 +754,7 @@ void Label::enableGlow(const Color3B& glowColor) return; _currLabelEffect = LabelEffect::GLOW; _effectColor = glowColor; - configureShaderProgram(); + updateShaderProgram(); } void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */) @@ -770,7 +770,7 @@ void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */) auto config = _fontConfig; config.outlineSize = outlineSize; setTTFConfig(config); - configureShaderProgram(); + updateShaderProgram(); } } _fontDefinition._stroke._strokeEnabled = true; @@ -807,7 +807,7 @@ void Label::disableEffect() setTTFConfig(_fontConfig); } _currLabelEffect = LabelEffect::NORMAL; - configureShaderProgram(); + updateShaderProgram(); _contentDirty = true; } diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index d92745e366..89f7252d43 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -277,7 +277,7 @@ protected: virtual void updateColor() override; - virtual void configureShaderProgram(); + virtual void updateShaderProgram(); void drawShadowWithoutBlur();