Merge pull request #5768 from Dhilan007/develop_label2

Refactor setter of label's font name and font size.
This commit is contained in:
James Chen 2014-03-13 21:14:36 +08:00
commit 8743b00d9f
3 changed files with 148 additions and 140 deletions

View File

@ -46,7 +46,7 @@ FontAtlas * FontAtlasCache::getFontAtlasTTF(const TTFConfig & config)
int fontSize = config.fontSize; int fontSize = config.fontSize;
if (useDistanceField) if (useDistanceField)
{ {
fontSize = Label::DefultFontSize; fontSize = Label::DistanceFieldFontSize;
} }
std::string atlasName = generateFontName(config.fontFilePath, fontSize, GlyphCollection::DYNAMIC, useDistanceField); std::string atlasName = generateFontName(config.fontFilePath, fontSize, GlyphCollection::DYNAMIC, useDistanceField);

View File

@ -36,7 +36,7 @@
NS_CC_BEGIN NS_CC_BEGIN
const int Label::DefultFontSize = 50; const int Label::DistanceFieldFontSize = 50;
Label* Label::create() Label* Label::create()
{ {
@ -208,15 +208,15 @@ bool Label::setCharMap(const std::string& plistFile)
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(plistFile); FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(plistFile);
if (!newAtlas) if (!newAtlas)
return false;
if (initWithFontAtlas(newAtlas))
{ {
_currentLabelType = LabelType::CHARMAP; reset();
return true; return false;
} }
return false; setFontAtlas(newAtlas);
_currentLabelType = LabelType::CHARMAP;
return true;
} }
bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) 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); FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(texture,itemWidth,itemHeight,startCharMap);
if (!newAtlas) if (!newAtlas)
return false;
if (initWithFontAtlas(newAtlas))
{ {
_currentLabelType = LabelType::CHARMAP; reset();
return true; return false;
} }
return false; setFontAtlas(newAtlas);
_currentLabelType = LabelType::CHARMAP;
return true;
} }
bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) 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); FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(charMapFile,itemWidth,itemHeight,startCharMap);
if (!newAtlas) if (!newAtlas)
return false;
if (initWithFontAtlas(newAtlas))
{ {
_currentLabelType = LabelType::CHARMAP; reset();
return true; return false;
} }
return false; setFontAtlas(newAtlas);
_currentLabelType = LabelType::CHARMAP;
return true;
} }
Label::Label(FontAtlas *atlas /* = nullptr */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, Label::Label(FontAtlas *atlas /* = nullptr */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */,
@ -274,17 +274,10 @@ Label::Label(FontAtlas *atlas /* = nullptr */, TextHAlignment hAlignment /* = Te
, _currNumLines(-1) , _currNumLines(-1)
, _textSprite(nullptr) , _textSprite(nullptr)
, _contentDirty(false) , _contentDirty(false)
, _currentLabelType(LabelType::STRING_TEXTURE)
, _currLabelEffect(LabelEffect::NORMAL)
, _shadowBlurRadius(0)
{ {
_cascadeColorEnabled = true; _cascadeColorEnabled = true;
_batchNodes.push_back(this);
reset();
_fontDefinition._fontName = "Helvetica";
_fontDefinition._fontSize = 12;
_fontDefinition._alignment = TextHAlignment::LEFT;
_fontDefinition._vertAlignment = TextVAlignment::TOP;
} }
Label::~Label() Label::~Label()
@ -301,28 +294,40 @@ Label::~Label()
CC_SAFE_RELEASE_NULL(_reusedLetter); CC_SAFE_RELEASE_NULL(_reusedLetter);
} }
bool Label::init() void Label::reset()
{ {
bool ret = true; TTFConfig temp;
if(_fontAtlas) _fontConfig = temp;
{
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();
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() void Label::updateShaderProgram()
{ {
switch (_currLabelEffect) switch (_currLabelEffect)
{ {
@ -349,32 +354,33 @@ void Label::initProgram()
_uniformEffectColor = glGetUniformLocation(_shaderProgram->getProgram(), "v_effectColor"); _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 */)
{ {
FontAtlas *oldAtlas = _fontAtlas; if (atlas == _fontAtlas)
bool oldDistanceFieldEnable = _useDistanceField; {
bool oldA8ShaderEnabel = _useA8Shader; FontAtlasCache::releaseFontAtlas(atlas);
return;
}
if (_fontAtlas)
{
FontAtlasCache::releaseFontAtlas(_fontAtlas);
_fontAtlas = nullptr;
}
_fontAtlas = atlas; _fontAtlas = atlas;
_useDistanceField = distanceFieldEnabled; SpriteBatchNode::initWithTexture(_fontAtlas->getTexture(0), 30);
_useA8Shader = useA8Shader; if (_reusedLetter == nullptr)
bool ret = Label::init();
if (oldAtlas)
{ {
if (ret) _reusedLetter = Sprite::createWithTexture(_fontAtlas->getTexture(0));
{ _reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB);
FontAtlasCache::releaseFontAtlas(oldAtlas); _reusedLetter->retain();
} _reusedLetter->setAnchorPoint(Point::ANCHOR_TOP_LEFT);
else _reusedLetter->setBatchNode(this);
{ }
_fontAtlas = oldAtlas; else
_useDistanceField = oldDistanceFieldEnable; {
_useA8Shader = oldA8ShaderEnabel; _reusedLetter->setTexture(_fontAtlas->getTexture(0));
Label::init();
FontAtlasCache::releaseFontAtlas(atlas);
}
} }
if (_fontAtlas) if (_fontAtlas)
@ -382,8 +388,10 @@ bool Label::initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = fa
_commonLineHeight = _fontAtlas->getCommonLineHeight(); _commonLineHeight = _fontAtlas->getCommonLineHeight();
_contentDirty = true; _contentDirty = true;
} }
_useDistanceField = distanceFieldEnabled;
return ret; _useA8Shader = useA8Shader;
_currLabelEffect = LabelEffect::NORMAL;
updateShaderProgram();
} }
bool Label::setTTFConfig(const TTFConfig& ttfConfig) bool Label::setTTFConfig(const TTFConfig& ttfConfig)
@ -391,30 +399,30 @@ bool Label::setTTFConfig(const TTFConfig& ttfConfig)
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(ttfConfig); FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(ttfConfig);
if (!newAtlas) if (!newAtlas)
{
reset();
return false; return false;
}
if (initWithFontAtlas(newAtlas,ttfConfig.distanceFieldEnabled,true)) _fontConfig = ttfConfig;
if (_fontConfig.outlineSize > 0)
{ {
_fontConfig = ttfConfig; _fontConfig.distanceFieldEnabled = false;
if (ttfConfig.outlineSize > 0) _useDistanceField = false;
{ _useA8Shader = false;
_fontConfig.distanceFieldEnabled = false; _currLabelEffect = LabelEffect::OUTLINE;
_useDistanceField = false; updateShaderProgram();
_useA8Shader = false;
_currLabelEffect = LabelEffect::OUTLINE;
initProgram();
}
else if(ttfConfig.distanceFieldEnabled)
{
this->setFontScale(1.0f * ttfConfig.fontSize / DefultFontSize);
}
_currentLabelType = LabelType::TTF;
return true;
} }
else else if(ttfConfig.distanceFieldEnabled)
{ {
return false; this->setFontScale(1.0f * ttfConfig.fontSize / DistanceFieldFontSize);
} }
setFontAtlas(newAtlas,ttfConfig.distanceFieldEnabled,true);
_currentLabelType = LabelType::TTF;
return true;
} }
bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Point& imageOffset /* = Point::ZERO */) bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Point& imageOffset /* = Point::ZERO */)
@ -422,21 +430,23 @@ bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Point& im
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasFNT(bmfontFilePath,imageOffset); FontAtlas *newAtlas = FontAtlasCache::getFontAtlasFNT(bmfontFilePath,imageOffset);
if (!newAtlas) if (!newAtlas)
return false;
if (initWithFontAtlas(newAtlas))
{ {
_currentLabelType = LabelType::BMFONT; reset();
return true; return false;
} }
return false; setFontAtlas(newAtlas);
_currentLabelType = LabelType::BMFONT;
return true;
} }
void Label::setFontDefinition(const FontDefinition& textDefinition) void Label::setFontDefinition(const FontDefinition& textDefinition)
{ {
reset();
_fontDefinition = textDefinition; _fontDefinition = textDefinition;
_currentLabelType = LabelType::STRING_TEXTURE; _fontName = textDefinition._fontName;
_fontSize = textDefinition._fontSize;
_contentDirty = true; _contentDirty = true;
} }
@ -744,7 +754,7 @@ void Label::enableGlow(const Color3B& glowColor)
return; return;
_currLabelEffect = LabelEffect::GLOW; _currLabelEffect = LabelEffect::GLOW;
_effectColor = glowColor; _effectColor = glowColor;
initProgram(); updateShaderProgram();
} }
void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */) void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */)
@ -760,7 +770,7 @@ void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */)
auto config = _fontConfig; auto config = _fontConfig;
config.outlineSize = outlineSize; config.outlineSize = outlineSize;
setTTFConfig(config); setTTFConfig(config);
initProgram(); updateShaderProgram();
} }
} }
_fontDefinition._stroke._strokeEnabled = true; _fontDefinition._stroke._strokeEnabled = true;
@ -797,7 +807,7 @@ void Label::disableEffect()
setTTFConfig(_fontConfig); setTTFConfig(_fontConfig);
} }
_currLabelEffect = LabelEffect::NORMAL; _currLabelEffect = LabelEffect::NORMAL;
initProgram(); updateShaderProgram();
_contentDirty = true; _contentDirty = true;
} }
@ -940,12 +950,30 @@ void Label::updateContent()
_contentDirty = false; _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) void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated)
{ {
if (! _visible || _originalUTF8String.empty()) if (! _visible || _originalUTF8String.empty())
{ {
return; return;
} }
if (_fontDirty)
{
updateFont();
}
if (_contentDirty) if (_contentDirty)
{ {
updateContent(); updateContent();
@ -987,19 +1015,10 @@ void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parent
void Label::setFontName(const std::string& fontName) void Label::setFontName(const std::string& fontName)
{ {
if (fontName.find('.') != fontName.npos) if (fontName != _fontName)
{ {
auto config = _fontConfig; _fontName = fontName;
config.fontFilePath = fontName; _fontDirty = true;
if (setTTFConfig(config))
{
return;
}
}
if (_fontDefinition._fontName != fontName)
{
_fontDefinition._fontName = fontName;
_contentDirty = true;
} }
} }
@ -1016,29 +1035,10 @@ const std::string& Label::getFontName() const
void Label::setFontSize(int fontSize) void Label::setFontSize(int fontSize)
{ {
if (_currentLabelType == LabelType::TTF) if (_fontSize != fontSize)
{ {
if (_fontConfig.fontSize == fontSize) _fontSize = fontSize;
{ _fontDirty = true;
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;
} }
} }
@ -1204,6 +1204,10 @@ std::string Label::getDescription() const
const Size& Label::getContentSize() const const Size& Label::getContentSize() const
{ {
if (_fontDirty)
{
const_cast<Label*>(this)->updateFont();
}
if (_contentDirty) if (_contentDirty)
{ {
const_cast<Label*>(this)->updateContent(); const_cast<Label*>(this)->updateContent();

View File

@ -59,7 +59,7 @@ typedef struct _ttfConfig
bool distanceFieldEnabled; bool distanceFieldEnabled;
int outlineSize; 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) const char *customGlyphCollection = nullptr,bool useDistanceField = false,int outline = 0)
:fontFilePath(filePath) :fontFilePath(filePath)
,fontSize(size) ,fontSize(size)
@ -78,7 +78,7 @@ typedef struct _ttfConfig
class CC_DLL Label : public SpriteBatchNode, public LabelProtocol class CC_DLL Label : public SpriteBatchNode, public LabelProtocol
{ {
public: public:
static const int DefultFontSize; static const int DistanceFieldFontSize;
static Label* create(); static Label* create();
@ -259,14 +259,12 @@ protected:
*/ */
virtual ~Label(); 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 recordLetterInfo(const cocos2d::Point& point,const FontLetterDefinition& letterDef, int spriteIndex);
bool recordPlaceholderInfo(int spriteIndex); bool recordPlaceholderInfo(int spriteIndex);
void setFontScale(float fontScale); void setFontScale(float fontScale);
virtual bool init();
virtual void alignText(); virtual void alignText();
@ -279,14 +277,20 @@ protected:
virtual void updateColor() override; virtual void updateColor() override;
virtual void initProgram(); virtual void updateShaderProgram();
void drawShadowWithoutBlur(); void drawShadowWithoutBlur();
void createSpriteWithFontDefinition(); void createSpriteWithFontDefinition();
void updateFont();
void reset();
bool _isOpacityModifyRGB; bool _isOpacityModifyRGB;
bool _contentDirty; bool _contentDirty;
bool _fontDirty;
std::string _fontName;
int _fontSize;
LabelType _currentLabelType; LabelType _currentLabelType;
std::vector<SpriteBatchNode*> _batchNodes; std::vector<SpriteBatchNode*> _batchNodes;