Merge pull request #5657 from Dhilan007/develop_label

closed #4337:label support create by font name,compatible with old labelTTF
This commit is contained in:
James Chen 2014-03-11 16:29:29 +08:00
commit 2f877de428
4 changed files with 371 additions and 128 deletions

View File

@ -50,6 +50,54 @@ Label* Label::create()
return ret; return ret;
} }
Label* Label::createWithFontDefinition(const std::string& text, const FontDefinition &textDefinition)
{
auto ret = new Label();
if (ret)
{
ret->setFontDefinition(textDefinition);
ret->setString(text);
ret->autorelease();
}
return ret;
}
Label* Label::create(const std::string& text, const std::string& fontName, float fontSize, const Size& dimensions /* = Size::ZERO */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, TextVAlignment vAlignment /* = TextVAlignment::TOP */)
{
auto ret = new Label(nullptr,hAlignment);
if (ret)
{
do
{
if (fontName.find('.') != fontName.npos)
{
TTFConfig ttfConfig(fontName.c_str(),fontSize,GlyphCollection::DYNAMIC);
if (ret->setTTFConfig(ttfConfig))
{
break;
}
}
FontDefinition fontDef;
fontDef._fontName = fontName;
fontDef._fontSize = fontSize;
fontDef._dimensions = dimensions;
fontDef._alignment = hAlignment;
fontDef._vertAlignment = vAlignment;
ret->setFontDefinition(fontDef);
} while (0);
ret->setDimensions(dimensions.width,dimensions.height);
ret->setString(text);
ret->autorelease();
}
return ret;
}
Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment /* = TextHAlignment::CENTER */, int lineSize /* = 0 */) Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment /* = TextHAlignment::CENTER */, int lineSize /* = 0 */)
{ {
Label *ret = new Label(nullptr,alignment); Label *ret = new Label(nullptr,alignment);
@ -162,7 +210,13 @@ bool Label::setCharMap(const std::string& plistFile)
if (!newAtlas) if (!newAtlas)
return false; return false;
return initWithFontAtlas(newAtlas); if (initWithFontAtlas(newAtlas))
{
_currentLabelType = LabelType::CHARMAP;
return true;
}
return false;
} }
bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
@ -172,7 +226,13 @@ bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int st
if (!newAtlas) if (!newAtlas)
return false; return false;
return initWithFontAtlas(newAtlas); if (initWithFontAtlas(newAtlas))
{
_currentLabelType = LabelType::CHARMAP;
return true;
}
return false;
} }
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)
@ -182,7 +242,13 @@ bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHe
if (!newAtlas) if (!newAtlas)
return false; return false;
return initWithFontAtlas(newAtlas); if (initWithFontAtlas(newAtlas))
{
_currentLabelType = LabelType::CHARMAP;
return true;
}
return false;
} }
Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,bool useA8Shader) Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,bool useA8Shader)
@ -192,6 +258,7 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,b
, _maxLineWidth(0) , _maxLineWidth(0)
, _labelWidth(0) , _labelWidth(0)
, _labelHeight(0) , _labelHeight(0)
, _labelDimensions(Size::ZERO)
, _hAlignment(alignment) , _hAlignment(alignment)
, _currentUTF16String(nullptr) , _currentUTF16String(nullptr)
, _originalUTF16String(nullptr) , _originalUTF16String(nullptr)
@ -203,9 +270,16 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,b
, _fontScale(1.0f) , _fontScale(1.0f)
, _uniformEffectColor(0) , _uniformEffectColor(0)
,_currNumLines(-1) ,_currNumLines(-1)
,_textSprite(nullptr)
,_contentDirty(false)
{ {
_cascadeColorEnabled = true; _cascadeColorEnabled = true;
_batchNodes.push_back(this); _batchNodes.push_back(this);
_fontDefinition._fontName = "Helvetica";
_fontDefinition._fontSize = 32;
_fontDefinition._alignment = TextHAlignment::LEFT;
_fontDefinition._vertAlignment = TextVAlignment::TOP;
} }
Label::~Label() Label::~Label()
@ -301,11 +375,7 @@ bool Label::initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = fa
if (_fontAtlas) if (_fontAtlas)
{ {
_commonLineHeight = _fontAtlas->getCommonLineHeight(); _commonLineHeight = _fontAtlas->getCommonLineHeight();
if(_currentUTF16String) _contentDirty = true;
{
resetCurrentString();
alignText();
}
} }
return ret; return ret;
@ -333,6 +403,7 @@ bool Label::setTTFConfig(const TTFConfig& ttfConfig)
{ {
this->setFontScale(1.0f * ttfConfig.fontSize / DefultFontSize); this->setFontScale(1.0f * ttfConfig.fontSize / DefultFontSize);
} }
_currentLabelType = LabelType::TTF;
return true; return true;
} }
else else
@ -348,85 +419,65 @@ bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Point& im
if (!newAtlas) if (!newAtlas)
return false; return false;
return initWithFontAtlas(newAtlas); if (initWithFontAtlas(newAtlas))
{
_currentLabelType = LabelType::BMFONT;
return true;
}
return false;
}
void Label::setFontDefinition(const FontDefinition& textDefinition)
{
_fontDefinition = textDefinition;
_currentLabelType = LabelType::STRING_TEXTURE;
_contentDirty = true;
} }
void Label::setString(const std::string& text) void Label::setString(const std::string& text)
{ {
auto utf16String = cc_utf8_to_utf16(text.c_str()); _originalUTF8String = text;
if(utf16String) _contentDirty = true;
{
_originalUTF8String = text;
setCurrentString(utf16String);
setOriginalString(utf16String);
alignText();
}
} }
void Label::setAlignment(TextHAlignment hAlignment,bool aligntext /* = true */) void Label::setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment)
{
setAlignment(hAlignment,_vAlignment,aligntext);
}
inline void Label::setHorizontalAlignment(TextHAlignment hAlignment,bool aligntext /* = true */)
{
setAlignment(hAlignment,_vAlignment,aligntext);
}
inline void Label::setVerticalAlignment(TextVAlignment vAlignment,bool aligntext /* = true */)
{
setAlignment(_hAlignment,vAlignment,aligntext);
}
void Label::setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment,bool aligntext /* = true */)
{ {
if (hAlignment != _hAlignment || vAlignment != _vAlignment) if (hAlignment != _hAlignment || vAlignment != _vAlignment)
{ {
_fontDefinition._alignment = hAlignment;
_fontDefinition._vertAlignment = vAlignment;
_hAlignment = hAlignment; _hAlignment = hAlignment;
_vAlignment = vAlignment; _vAlignment = vAlignment;
if (_currentUTF16String && aligntext)
{ _contentDirty = true;
resetCurrentString();
alignText();
}
} }
} }
void Label::setMaxLineWidth(unsigned int maxLineWidth) void Label::setMaxLineWidth(unsigned int maxLineWidth)
{ {
if (_maxLineWidth != maxLineWidth) if (_labelWidth == 0 && _maxLineWidth != maxLineWidth)
{ {
_maxLineWidth = maxLineWidth; _maxLineWidth = maxLineWidth;
if (_currentUTF16String) _contentDirty = true;
{
resetCurrentString();
alignText();
}
} }
} }
inline void Label::setWidth(unsigned int width)
{
setDimensions(width,_labelHeight);
}
inline void Label::setHeight(unsigned int height)
{
setDimensions(_labelWidth,height);
}
void Label::setDimensions(unsigned int width,unsigned int height) void Label::setDimensions(unsigned int width,unsigned int height)
{ {
if (height != _labelHeight || width != _labelWidth) if (height != _labelHeight || width != _labelWidth)
{ {
_labelHeight = height; _fontDefinition._dimensions.width = width;
_fontDefinition._dimensions.height = height;
_labelWidth = width; _labelWidth = width;
_labelHeight = height;
_labelDimensions.width = width;
_labelDimensions.height = height;
_maxLineWidth = width; _maxLineWidth = width;
if (_currentUTF16String) _contentDirty = true;
{
resetCurrentString();
alignText();
}
} }
} }
@ -434,15 +485,8 @@ void Label::setLineBreakWithoutSpace(bool breakWithoutSpace)
{ {
if (breakWithoutSpace != _lineBreakWithoutSpaces) if (breakWithoutSpace != _lineBreakWithoutSpaces)
{ {
// store
_lineBreakWithoutSpaces = breakWithoutSpace; _lineBreakWithoutSpaces = breakWithoutSpace;
_contentDirty = true;
// need to align text again
if(_currentUTF16String)
{
resetCurrentString();
alignText();
}
} }
} }
@ -525,7 +569,7 @@ void Label::alignText()
if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) ) if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) )
LabelTextFormatter::createStringSprites(this); LabelTextFormatter::createStringSprites(this);
if(_labelWidth >0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT)) if(_labelWidth > 0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT))
LabelTextFormatter::alignText(this); LabelTextFormatter::alignText(this);
int strLen = cc_wcslen(_currentUTF16String); int strLen = cc_wcslen(_currentUTF16String);
@ -613,26 +657,11 @@ bool Label::setCurrentString(unsigned short *stringToSet)
computeStringNumLines(); computeStringNumLines();
// compute the advances // compute the advances
return computeHorizontalKernings(stringToSet); if (_fontAtlas)
}
void Label::resetCurrentString()
{
if ((!_currentUTF16String) && (!_originalUTF16String))
return;
// set the new string
if (_currentUTF16String)
{ {
delete [] _currentUTF16String; computeHorizontalKernings(stringToSet);
_currentUTF16String = 0;
} }
return true;
int stringLenght = cc_wcslen(_originalUTF16String);
_currentUTF16String = new unsigned short int [stringLenght + 1];
memcpy(_currentUTF16String, _originalUTF16String, stringLenght * 2);
_currentUTF16String[stringLenght] = 0;
} }
void Label::updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture) void Label::updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture)
@ -706,7 +735,7 @@ void Label::setLabelEffect(LabelEffect effect,const Color3B& effectColor)
void Label::enableGlow(const Color3B& glowColor) void Label::enableGlow(const Color3B& glowColor)
{ {
if(_useDistanceField == false) if(! _useDistanceField)
return; return;
_currLabelEffect = LabelEffect::GLOW; _currLabelEffect = LabelEffect::GLOW;
_effectColor = glowColor; _effectColor = glowColor;
@ -716,15 +745,25 @@ void Label::enableGlow(const Color3B& glowColor)
void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */) void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */)
{ {
_outlineColor = outlineColor; _outlineColor = outlineColor;
if (outlineSize > 0) if (outlineSize > 0)
{ {
_currLabelEffect = LabelEffect::OUTLINE; if (_currentLabelType == LabelType::TTF)
if (_fontConfig.outlineSize != outlineSize)
{ {
_fontConfig.outlineSize = outlineSize; if (_fontConfig.outlineSize != outlineSize)
setTTFConfig(_fontConfig); {
auto config = _fontConfig;
config.outlineSize = outlineSize;
setTTFConfig(config);
initProgram();
}
} }
initProgram(); _fontDefinition._stroke._strokeEnabled = true;
_fontDefinition._stroke._strokeSize = outlineSize;
_fontDefinition._stroke._strokeColor = Color3B(outlineColor.r,outlineColor.g,outlineColor.b);
_currLabelEffect = LabelEffect::OUTLINE;
_contentDirty = true;
} }
} }
@ -736,6 +775,13 @@ void Label::enableShadow(const Color3B& shadowColor /* = Color3B::BLACK */,const
//todo:support blur for shadow //todo:support blur for shadow
_shadowBlurRadius = 0; _shadowBlurRadius = 0;
_currLabelEffect = LabelEffect::SHADOW; _currLabelEffect = LabelEffect::SHADOW;
_fontDefinition._shadow._shadowEnabled = true;
_fontDefinition._shadow._shadowBlur = blurRadius;
_fontDefinition._shadow._shadowOffset = offset;
_fontDefinition._shadow._shadowOpacity = opacity;
_contentDirty = true;
} }
void Label::disableEffect() void Label::disableEffect()
@ -747,6 +793,7 @@ void Label::disableEffect()
} }
_currLabelEffect = LabelEffect::NORMAL; _currLabelEffect = LabelEffect::NORMAL;
initProgram(); initProgram();
_contentDirty = true;
} }
void Label::setFontScale(float fontScale) void Label::setFontScale(float fontScale)
@ -845,14 +892,61 @@ void Label::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpda
renderer->addCommand(&_customCommand); renderer->addCommand(&_customCommand);
} }
void Label::createSpriteWithFontDefinition()
{
_currentLabelType = LabelType::STRING_TEXTURE;
auto texture = new Texture2D;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
if (_fontDefinition._shadow._shadowEnabled || _fontDefinition._stroke._strokeEnabled)
{
CCLOGERROR("Currently only supported on iOS and Android!");
}
_fontDefinition._shadow._shadowEnabled = false;
_fontDefinition._stroke._strokeEnabled = false;
#endif
texture->initWithString(_originalUTF8String.c_str(),_fontDefinition);
_textSprite = Sprite::createWithTexture(texture);
_textSprite->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
this->setContentSize(_textSprite->getContentSize());
texture->release();
Node::addChild(_textSprite,0,Node::INVALID_TAG);
}
void Label::updateContent()
{
auto utf16String = cc_utf8_to_utf16(_originalUTF8String.c_str());
setCurrentString(utf16String);
setOriginalString(utf16String);
if (_textSprite)
{
Node::removeChild(_textSprite,true);
_textSprite = nullptr;
}
if (_fontAtlas)
{
alignText();
}
else
{
createSpriteWithFontDefinition();
}
_contentDirty = false;
}
void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated)
{ {
if (! _visible) if (! _visible)
{ {
return; return;
} }
if (_contentDirty)
{
updateContent();
}
if (_currLabelEffect == LabelEffect::SHADOW && _shadowBlurRadius <= 0) if (! _textSprite && _currLabelEffect == LabelEffect::SHADOW && _shadowBlurRadius <= 0)
{ {
_parentTransform = parentTransform; _parentTransform = parentTransform;
draw(renderer, _modelViewTransform, true); draw(renderer, _modelViewTransform, true);
@ -871,7 +965,14 @@ void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parent
kmGLPushMatrix(); kmGLPushMatrix();
kmGLLoadMatrix(&_modelViewTransform); kmGLLoadMatrix(&_modelViewTransform);
draw(renderer, _modelViewTransform, dirty); if (_textSprite)
{
_textSprite->visit();
}
else
{
draw(renderer, _modelViewTransform, dirty);
}
kmGLPopMatrix(); kmGLPopMatrix();
} }
@ -879,12 +980,53 @@ void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parent
setOrderOfArrival(0); setOrderOfArrival(0);
} }
void Label::setFontName(const std::string& fontName)
{
_fontDefinition._fontName = fontName;
if (fontName.find('.') != fontName.npos)
{
auto config = _fontConfig;
config.fontFilePath = fontName;
setTTFConfig(config);
}
_contentDirty = true;
}
void Label::setFontSize(int fontSize)
{
if (_currentLabelType == LabelType::TTF)
{
if (_fontConfig.distanceFieldEnabled)
{
_fontConfig.fontSize = fontSize;
this->setFontScale(1.0f * fontSize / DefultFontSize);
}
else
{
auto fontConfig = _fontConfig;
fontConfig.fontSize = fontSize;
setTTFConfig(fontConfig);
}
}
else
{
_fontDefinition._fontSize = fontSize;
_fontConfig.fontSize = fontSize;
_contentDirty = true;
}
}
///// PROTOCOL STUFF ///// PROTOCOL STUFF
Sprite * Label::getLetter(int lettetIndex) Sprite * Label::getLetter(int lettetIndex)
{ {
if (lettetIndex < _limitShowCount) if (_contentDirty)
{ {
if(_lettersInfo[lettetIndex].def.validDefinition == false) updateContent();
}
if (! _textSprite && lettetIndex < _limitShowCount)
{
if(! _lettersInfo[lettetIndex].def.validDefinition)
return nullptr; return nullptr;
Sprite* sp = static_cast<Sprite*>(this->getChildByTag(lettetIndex)); Sprite* sp = static_cast<Sprite*>(this->getChildByTag(lettetIndex));
@ -913,13 +1055,7 @@ Sprite * Label::getLetter(int lettetIndex)
int Label::getCommonLineHeight() const int Label::getCommonLineHeight() const
{ {
return _commonLineHeight; return _textSprite ? 0 : _commonLineHeight;
}
// string related stuff
int Label::getStringNumLines() const
{
return _currNumLines;
} }
void Label::computeStringNumLines() void Label::computeStringNumLines()
@ -969,6 +1105,11 @@ void Label::setOpacityModifyRGB(bool isOpacityModifyRGB)
void Label::setColor(const Color3B& color) void Label::setColor(const Color3B& color)
{ {
_fontDefinition._fontFillColor = color;
if (_textSprite)
{
updateContent();
}
_reusedLetter->setColor(color); _reusedLetter->setColor(color);
SpriteBatchNode::setColor(color); SpriteBatchNode::setColor(color);
} }
@ -1014,4 +1155,12 @@ std::string Label::getDescription() const
return StringUtils::format("<Label | Tag = %d, Label = '%s'>", _tag, cc_utf16_to_utf8(_currentUTF16String,-1,nullptr,nullptr)); return StringUtils::format("<Label | Tag = %d, Label = '%s'>", _tag, cc_utf16_to_utf8(_currentUTF16String,-1,nullptr,nullptr));
} }
const Size& Label::getContentSize() const
{
if (_contentDirty)
{
const_cast<Label*>(this)->updateContent();
}
return Node::getContentSize();
}
NS_CC_END NS_CC_END

View File

@ -82,6 +82,13 @@ public:
static Label* create(); static Label* create();
/** creates a Label from a font name, horizontal alignment, dimension in points, and font size in points.
* @warning It will generate texture by the platform-dependent code if [fontName] not a font file.
*/
static Label * create(const std::string& text, const std::string& fontName, float fontSize,
const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT,
TextVAlignment vAlignment = TextVAlignment::TOP);
CC_DEPRECATED_ATTRIBUTE static Label* createWithTTF(const std::string& label, const std::string& fontFilePath, CC_DEPRECATED_ATTRIBUTE static Label* createWithTTF(const std::string& label, const std::string& fontFilePath,
int fontSize, int lineSize = 0, TextHAlignment alignment = TextHAlignment::LEFT, int fontSize, int lineSize = 0, TextHAlignment alignment = TextHAlignment::LEFT,
GlyphCollection glyphs = GlyphCollection::NEHE, const char *customGlyphs = 0, bool useDistanceField = false); GlyphCollection glyphs = GlyphCollection::NEHE, const char *customGlyphs = 0, bool useDistanceField = false);
@ -99,6 +106,12 @@ public:
static Label * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); static Label * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
static Label * createWithCharMap(const std::string& plistFile); static Label * createWithCharMap(const std::string& plistFile);
/** create a lable with string and a font definition
* @warning It will generate texture by the platform-dependent code and create Sprite for show text.
* To obtain better performance use createWithTTF/createWithBMFont/createWithCharMap
*/
static Label * createWithFontDefinition(const std::string& text, const FontDefinition &textDefinition);
/** set TTF configuration for Label */ /** set TTF configuration for Label */
virtual bool setTTFConfig(const TTFConfig& ttfConfig); virtual bool setTTFConfig(const TTFConfig& ttfConfig);
@ -108,14 +121,21 @@ public:
virtual bool setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); virtual bool setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
virtual bool setCharMap(const std::string& plistFile); virtual bool setCharMap(const std::string& plistFile);
/** set the text definition used by this label
* It will create Sprite for show text if you haven't set up using TTF/BMFont/CharMap.
*/
virtual void setFontDefinition(const FontDefinition& textDefinition);
/** get the text definition used by this label */
const FontDefinition& getFontDefinition() const { return _fontDefinition; }
/** changes the string to render /** changes the string to render
* * @warning It is as expensive as changing the string if you haven't set up TTF/BMFont/CharMap for the label.
*/ */
virtual void setString(const std::string& text) override; virtual void setString(const std::string& text) override;
virtual const std::string& getString() const override { return _originalUTF8String; } virtual const std::string& getString() const override { return _originalUTF8String; }
CC_DEPRECATED_ATTRIBUTE void setLabelEffect(LabelEffect effect,const Color3B& effectColor); CC_DEPRECATED_ATTRIBUTE void setLabelEffect(LabelEffect effect,const Color3B& effectColor);
/** /**
@ -135,42 +155,52 @@ public:
virtual void disableEffect(); virtual void disableEffect();
virtual void setAlignment(TextHAlignment hAlignment,bool aligntext = true); void setAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment,_vAlignment);}
TextHAlignment getTextAlignment() const { return _hAlignment;} TextHAlignment getTextAlignment() const { return _hAlignment;}
virtual void setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment,bool aligntext = true); void setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment);
virtual void setHorizontalAlignment(TextHAlignment alignment,bool aligntext = true); void setHorizontalAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment,_vAlignment); }
TextHAlignment getHorizontalAlignment() const { return _hAlignment; } TextHAlignment getHorizontalAlignment() const { return _hAlignment; }
virtual void setVerticalAlignment(TextVAlignment verticalAlignment,bool aligntext = true); void setVerticalAlignment(TextVAlignment vAlignment) { setAlignment(_hAlignment,vAlignment); }
TextVAlignment getVerticalAlignment() const { return _vAlignment; } TextVAlignment getVerticalAlignment() const { return _vAlignment; }
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace); void setLineBreakWithoutSpace(bool breakWithoutSpace);
/** Sets the max line width of the label. /** Sets the max line width of the label.
* The label's max line width be used for force line breaks if the set value not equal zero. * The label's max line width be used for force line breaks if the set value not equal zero.
* The label's width and max line width has not always to be equal. * The label's width and max line width has not always to be equal.
*/ */
virtual void setMaxLineWidth(unsigned int maxLineWidth); void setMaxLineWidth(unsigned int maxLineWidth);
unsigned int getMaxLineWidth() { return _maxLineWidth;} unsigned int getMaxLineWidth() { return _maxLineWidth;}
/** Sets the untransformed size of the label. /** Sets the untransformed size of the label.
* The label's width be used for text align if the set value not equal zero. * The label's width be used for text align if the set value not equal zero.
* The label's max line width will be equal to the same value. * The label's max line width will be equal to the same value.
*/ */
virtual void setWidth(unsigned int width); void setWidth(unsigned int width) { setDimensions(width,_labelHeight);}
unsigned int getWidth() const { return _labelWidth; } unsigned int getWidth() const { return _labelWidth; }
/** Sets the untransformed size of the label. /** Sets the untransformed size of the label.
* The label's height be used for text align if the set value not equal zero. * The label's height be used for text align if the set value not equal zero.
* The text will display of incomplete when the size of label not enough to support display all text. * The text will display of incomplete when the size of label not enough to support display all text.
*/ */
virtual void setHeight(unsigned int height); void setHeight(unsigned int height){ setDimensions(_labelWidth,height);}
unsigned int getHeight() const { return _labelHeight;} unsigned int getHeight() const { return _labelHeight;}
/** Sets the untransformed size of the label in a more efficient way. */ /** Sets the untransformed size of the label in a more efficient way. */
virtual void setDimensions(unsigned int width,unsigned int height); void setDimensions(unsigned int width,unsigned int height);
const Size& getDimensions() const{ return _labelDimensions;}
/** update content immediately.*/
virtual void updateContent();
virtual void setFontName(const std::string& fontName);
virtual const std::string& getFontName() const { return _fontDefinition._fontName;}
virtual void setFontSize(int fontSize);
virtual int getFontSize() const { return _fontDefinition._fontSize;}
virtual bool isOpacityModifyRGB() const override; virtual bool isOpacityModifyRGB() const override;
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override; virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
@ -182,9 +212,9 @@ public:
int getCommonLineHeight() const; int getCommonLineHeight() const;
// string related stuff // string related stuff
int getStringNumLines() const; int getStringNumLines() const { return _currNumLines;}
CC_DEPRECATED_ATTRIBUTE int getStringLenght() const { return getStringLength(); }
int getStringLength() const; int getStringLength() const;
CC_DEPRECATED_ATTRIBUTE int getStringLenght() const { return getStringLength(); }
virtual void visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) override; virtual void visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) override;
virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) override; virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) override;
@ -198,6 +228,8 @@ public:
virtual void addChild(Node * child, int zOrder=0, int tag=0) override; virtual void addChild(Node * child, int zOrder=0, int tag=0) override;
virtual std::string getDescription() const override; virtual std::string getDescription() const override;
virtual const Size& getContentSize() const override;
protected: protected:
void onDraw(const kmMat4& transform, bool transformUpdated); void onDraw(const kmMat4& transform, bool transformUpdated);
@ -208,6 +240,14 @@ protected:
Point position; Point position;
Size contentSize; Size contentSize;
}; };
enum class LabelType {
TTF,
BMFONT,
CHARMAP,
STRING_TEXTURE
};
/** /**
* @js NA * @js NA
*/ */
@ -232,7 +272,6 @@ protected:
bool computeHorizontalKernings(unsigned short int *stringToRender); bool computeHorizontalKernings(unsigned short int *stringToRender);
bool setCurrentString(unsigned short *stringToSet); bool setCurrentString(unsigned short *stringToSet);
bool setOriginalString(unsigned short *stringToSet); bool setOriginalString(unsigned short *stringToSet);
void resetCurrentString();
void computeStringNumLines(); void computeStringNumLines();
void updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture); void updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture);
@ -243,7 +282,11 @@ protected:
void drawShadowWithoutBlur(); void drawShadowWithoutBlur();
void createSpriteWithFontDefinition();
bool _isOpacityModifyRGB; bool _isOpacityModifyRGB;
bool _contentDirty;
LabelType _currentLabelType;
std::vector<SpriteBatchNode*> _batchNodes; std::vector<SpriteBatchNode*> _batchNodes;
FontAtlas * _fontAtlas; FontAtlas * _fontAtlas;
@ -251,6 +294,10 @@ protected:
TTFConfig _fontConfig; TTFConfig _fontConfig;
//compatibility with older LabelTTF
Sprite* _textSprite;
FontDefinition _fontDefinition;
//! used for optimization //! used for optimization
Sprite *_reusedLetter; Sprite *_reusedLetter;
Rect _reusedRect; Rect _reusedRect;
@ -261,6 +308,7 @@ protected:
int * _horizontalKernings; int * _horizontalKernings;
unsigned int _maxLineWidth; unsigned int _maxLineWidth;
Size _labelDimensions;
unsigned int _labelWidth; unsigned int _labelWidth;
unsigned int _labelHeight; unsigned int _labelHeight;
TextHAlignment _hAlignment; TextHAlignment _hAlignment;

View File

@ -76,6 +76,7 @@ static std::function<Layer*()> createFunctions[] =
CL(LabelCharMapColorTest), CL(LabelCharMapColorTest),
CL(LabelCrashTest), CL(LabelCrashTest),
CL(LabelTTFOldNew), CL(LabelTTFOldNew),
CL(LabelFontNameTest),
CL(LabelAlignmentTest) CL(LabelAlignmentTest)
}; };
@ -1558,7 +1559,7 @@ void LabelTTFOldNew::onDraw(const kmMat4 &transform, bool transformUpdated)
kmGLPushMatrix(); kmGLPushMatrix();
kmGLLoadMatrix(&transform); kmGLLoadMatrix(&transform);
auto label1 = (Label*)getChildByTag(kTagBitmapAtlas1); auto label1 = (LabelTTF*)getChildByTag(kTagBitmapAtlas1);
auto labelSize = label1->getContentSize(); auto labelSize = label1->getContentSize();
auto origin = Director::getInstance()->getWinSize(); auto origin = Director::getInstance()->getWinSize();
@ -1612,6 +1613,40 @@ std::string LabelTTFOldNew::subtitle() const
return "Comparison between old(red) and new(white) TTF label"; return "Comparison between old(red) and new(white) TTF label";
} }
LabelFontNameTest::LabelFontNameTest()
{
auto size = Director::getInstance()->getWinSize();
auto label1 = Label::create();
label1->setString("Default Font");
label1->setPosition( Point(size.width/2, size.height * 0.7) );
label1->setAnchorPoint(Point::ANCHOR_MIDDLE);
addChild(label1);
FontDefinition fontDef;
fontDef._fontName = "Marker Felt";
fontDef._fontSize = 32;
auto label2 = Label::createWithFontDefinition("Create with FontDefinition",fontDef);
label2->setPosition( Point(size.width/2, size.height * 0.6) );
label2->setAnchorPoint(Point::ANCHOR_MIDDLE);
addChild(label2);
auto label3 = Label::create("Marker Felt","Marker Felt",32);
label3->setPosition( Point(size.width/2, size.height * 0.5) );
label3->setAnchorPoint(Point::ANCHOR_MIDDLE);
addChild(label3);
}
std::string LabelFontNameTest::title() const
{
return "New Label Test";
}
std::string LabelFontNameTest::subtitle() const
{
return "create label by font name,compatible with old labelTTF";
}
LabelAlignmentTest::LabelAlignmentTest() LabelAlignmentTest::LabelAlignmentTest()
{ {
auto blockSize = Size(200, 160); auto blockSize = Size(200, 160);
@ -1666,42 +1701,42 @@ LabelAlignmentTest::~LabelAlignmentTest()
void LabelAlignmentTest::setAlignmentLeft(Ref* sender) void LabelAlignmentTest::setAlignmentLeft(Ref* sender)
{ {
_horizAlign = TextHAlignment::LEFT; _horizAlign = TextHAlignment::LEFT;
_label->setHorizontalAlignment(_horizAlign,false); _label->setHorizontalAlignment(_horizAlign);
_label->setString(getCurrentAlignment()); _label->setString(getCurrentAlignment());
} }
void LabelAlignmentTest::setAlignmentCenter(Ref* sender) void LabelAlignmentTest::setAlignmentCenter(Ref* sender)
{ {
_horizAlign = TextHAlignment::CENTER; _horizAlign = TextHAlignment::CENTER;
_label->setHorizontalAlignment(_horizAlign,false); _label->setHorizontalAlignment(_horizAlign);
_label->setString(getCurrentAlignment()); _label->setString(getCurrentAlignment());
} }
void LabelAlignmentTest::setAlignmentRight(Ref* sender) void LabelAlignmentTest::setAlignmentRight(Ref* sender)
{ {
_horizAlign = TextHAlignment::RIGHT; _horizAlign = TextHAlignment::RIGHT;
_label->setHorizontalAlignment(_horizAlign,false); _label->setHorizontalAlignment(_horizAlign);
_label->setString(getCurrentAlignment()); _label->setString(getCurrentAlignment());
} }
void LabelAlignmentTest::setAlignmentTop(Ref* sender) void LabelAlignmentTest::setAlignmentTop(Ref* sender)
{ {
_vertAlign = TextVAlignment::TOP; _vertAlign = TextVAlignment::TOP;
_label->setVerticalAlignment(_vertAlign,false); _label->setVerticalAlignment(_vertAlign);
_label->setString(getCurrentAlignment()); _label->setString(getCurrentAlignment());
} }
void LabelAlignmentTest::setAlignmentMiddle(Ref* sender) void LabelAlignmentTest::setAlignmentMiddle(Ref* sender)
{ {
_vertAlign = TextVAlignment::CENTER; _vertAlign = TextVAlignment::CENTER;
_label->setVerticalAlignment(_vertAlign,false); _label->setVerticalAlignment(_vertAlign);
_label->setString(getCurrentAlignment()); _label->setString(getCurrentAlignment());
} }
void LabelAlignmentTest::setAlignmentBottom(Ref* sender) void LabelAlignmentTest::setAlignmentBottom(Ref* sender)
{ {
_vertAlign = TextVAlignment::BOTTOM; _vertAlign = TextVAlignment::BOTTOM;
_label->setVerticalAlignment(_vertAlign,false); _label->setVerticalAlignment(_vertAlign);
_label->setString(getCurrentAlignment()); _label->setString(getCurrentAlignment());
} }

View File

@ -450,6 +450,17 @@ public:
virtual std::string subtitle() const override; virtual std::string subtitle() const override;
}; };
class LabelFontNameTest : public AtlasDemoNew
{
public:
CREATE_FUNC(LabelFontNameTest);
LabelFontNameTest();
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
class LabelAlignmentTest : public AtlasDemoNew class LabelAlignmentTest : public AtlasDemoNew
{ {
public: public: