mirror of https://github.com/axmolengine/axmol.git
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:
commit
2f877de428
|
@ -50,6 +50,54 @@ Label* Label::create()
|
|||
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 *ret = new Label(nullptr,alignment);
|
||||
|
@ -162,7 +210,13 @@ bool Label::setCharMap(const std::string& plistFile)
|
|||
if (!newAtlas)
|
||||
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)
|
||||
|
@ -172,7 +226,13 @@ bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int st
|
|||
if (!newAtlas)
|
||||
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)
|
||||
|
@ -182,7 +242,13 @@ bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHe
|
|||
if (!newAtlas)
|
||||
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)
|
||||
|
@ -192,6 +258,7 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,b
|
|||
, _maxLineWidth(0)
|
||||
, _labelWidth(0)
|
||||
, _labelHeight(0)
|
||||
, _labelDimensions(Size::ZERO)
|
||||
, _hAlignment(alignment)
|
||||
, _currentUTF16String(nullptr)
|
||||
, _originalUTF16String(nullptr)
|
||||
|
@ -203,9 +270,16 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,b
|
|||
, _fontScale(1.0f)
|
||||
, _uniformEffectColor(0)
|
||||
,_currNumLines(-1)
|
||||
,_textSprite(nullptr)
|
||||
,_contentDirty(false)
|
||||
{
|
||||
_cascadeColorEnabled = true;
|
||||
_batchNodes.push_back(this);
|
||||
|
||||
_fontDefinition._fontName = "Helvetica";
|
||||
_fontDefinition._fontSize = 32;
|
||||
_fontDefinition._alignment = TextHAlignment::LEFT;
|
||||
_fontDefinition._vertAlignment = TextVAlignment::TOP;
|
||||
}
|
||||
|
||||
Label::~Label()
|
||||
|
@ -301,11 +375,7 @@ bool Label::initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = fa
|
|||
if (_fontAtlas)
|
||||
{
|
||||
_commonLineHeight = _fontAtlas->getCommonLineHeight();
|
||||
if(_currentUTF16String)
|
||||
{
|
||||
resetCurrentString();
|
||||
alignText();
|
||||
}
|
||||
_contentDirty = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -333,6 +403,7 @@ bool Label::setTTFConfig(const TTFConfig& ttfConfig)
|
|||
{
|
||||
this->setFontScale(1.0f * ttfConfig.fontSize / DefultFontSize);
|
||||
}
|
||||
_currentLabelType = LabelType::TTF;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
|
@ -348,101 +419,74 @@ bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Point& im
|
|||
if (!newAtlas)
|
||||
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)
|
||||
{
|
||||
auto utf16String = cc_utf8_to_utf16(text.c_str());
|
||||
if(utf16String)
|
||||
{
|
||||
_originalUTF8String = text;
|
||||
setCurrentString(utf16String);
|
||||
setOriginalString(utf16String);
|
||||
alignText();
|
||||
}
|
||||
_originalUTF8String = text;
|
||||
_contentDirty = true;
|
||||
}
|
||||
|
||||
void Label::setAlignment(TextHAlignment hAlignment,bool aligntext /* = true */)
|
||||
{
|
||||
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 */)
|
||||
void Label::setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment)
|
||||
{
|
||||
if (hAlignment != _hAlignment || vAlignment != _vAlignment)
|
||||
{
|
||||
_fontDefinition._alignment = hAlignment;
|
||||
_fontDefinition._vertAlignment = vAlignment;
|
||||
|
||||
_hAlignment = hAlignment;
|
||||
_vAlignment = vAlignment;
|
||||
if (_currentUTF16String && aligntext)
|
||||
{
|
||||
resetCurrentString();
|
||||
alignText();
|
||||
}
|
||||
|
||||
_contentDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Label::setMaxLineWidth(unsigned int maxLineWidth)
|
||||
{
|
||||
if (_maxLineWidth != maxLineWidth)
|
||||
if (_labelWidth == 0 && _maxLineWidth != maxLineWidth)
|
||||
{
|
||||
_maxLineWidth = maxLineWidth;
|
||||
if (_currentUTF16String)
|
||||
{
|
||||
resetCurrentString();
|
||||
alignText();
|
||||
}
|
||||
_contentDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (height != _labelHeight || width != _labelWidth)
|
||||
{
|
||||
_labelHeight = height;
|
||||
_fontDefinition._dimensions.width = width;
|
||||
_fontDefinition._dimensions.height = height;
|
||||
|
||||
_labelWidth = width;
|
||||
_labelHeight = height;
|
||||
_labelDimensions.width = width;
|
||||
_labelDimensions.height = height;
|
||||
|
||||
_maxLineWidth = width;
|
||||
if (_currentUTF16String)
|
||||
{
|
||||
resetCurrentString();
|
||||
alignText();
|
||||
}
|
||||
}
|
||||
_contentDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Label::setLineBreakWithoutSpace(bool breakWithoutSpace)
|
||||
{
|
||||
if (breakWithoutSpace != _lineBreakWithoutSpaces)
|
||||
{
|
||||
// store
|
||||
_lineBreakWithoutSpaces = breakWithoutSpace;
|
||||
|
||||
// need to align text again
|
||||
if(_currentUTF16String)
|
||||
{
|
||||
resetCurrentString();
|
||||
alignText();
|
||||
}
|
||||
_contentDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -525,7 +569,7 @@ void Label::alignText()
|
|||
if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) )
|
||||
LabelTextFormatter::createStringSprites(this);
|
||||
|
||||
if(_labelWidth >0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT))
|
||||
if(_labelWidth > 0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT))
|
||||
LabelTextFormatter::alignText(this);
|
||||
|
||||
int strLen = cc_wcslen(_currentUTF16String);
|
||||
|
@ -613,26 +657,11 @@ bool Label::setCurrentString(unsigned short *stringToSet)
|
|||
computeStringNumLines();
|
||||
|
||||
// compute the advances
|
||||
return computeHorizontalKernings(stringToSet);
|
||||
}
|
||||
|
||||
void Label::resetCurrentString()
|
||||
{
|
||||
if ((!_currentUTF16String) && (!_originalUTF16String))
|
||||
return;
|
||||
|
||||
// set the new string
|
||||
if (_currentUTF16String)
|
||||
if (_fontAtlas)
|
||||
{
|
||||
delete [] _currentUTF16String;
|
||||
_currentUTF16String = 0;
|
||||
computeHorizontalKernings(stringToSet);
|
||||
}
|
||||
|
||||
int stringLenght = cc_wcslen(_originalUTF16String);
|
||||
_currentUTF16String = new unsigned short int [stringLenght + 1];
|
||||
memcpy(_currentUTF16String, _originalUTF16String, stringLenght * 2);
|
||||
_currentUTF16String[stringLenght] = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if(_useDistanceField == false)
|
||||
if(! _useDistanceField)
|
||||
return;
|
||||
_currLabelEffect = LabelEffect::GLOW;
|
||||
_effectColor = glowColor;
|
||||
|
@ -716,16 +745,26 @@ void Label::enableGlow(const Color3B& glowColor)
|
|||
void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */)
|
||||
{
|
||||
_outlineColor = outlineColor;
|
||||
|
||||
if (outlineSize > 0)
|
||||
{
|
||||
_currLabelEffect = LabelEffect::OUTLINE;
|
||||
if (_fontConfig.outlineSize != outlineSize)
|
||||
if (_currentLabelType == LabelType::TTF)
|
||||
{
|
||||
_fontConfig.outlineSize = outlineSize;
|
||||
setTTFConfig(_fontConfig);
|
||||
if (_fontConfig.outlineSize != outlineSize)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void Label::enableShadow(const Color3B& shadowColor /* = Color3B::BLACK */,const Size &offset /* = Size(2 ,-2)*/, float opacity /* = 0.75f */, int blurRadius /* = 0 */)
|
||||
|
@ -736,6 +775,13 @@ void Label::enableShadow(const Color3B& shadowColor /* = Color3B::BLACK */,const
|
|||
//todo:support blur for shadow
|
||||
_shadowBlurRadius = 0;
|
||||
_currLabelEffect = LabelEffect::SHADOW;
|
||||
|
||||
_fontDefinition._shadow._shadowEnabled = true;
|
||||
_fontDefinition._shadow._shadowBlur = blurRadius;
|
||||
_fontDefinition._shadow._shadowOffset = offset;
|
||||
_fontDefinition._shadow._shadowOpacity = opacity;
|
||||
|
||||
_contentDirty = true;
|
||||
}
|
||||
|
||||
void Label::disableEffect()
|
||||
|
@ -747,6 +793,7 @@ void Label::disableEffect()
|
|||
}
|
||||
_currLabelEffect = LabelEffect::NORMAL;
|
||||
initProgram();
|
||||
_contentDirty = true;
|
||||
}
|
||||
|
||||
void Label::setFontScale(float fontScale)
|
||||
|
@ -845,14 +892,61 @@ void Label::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpda
|
|||
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)
|
||||
{
|
||||
if (! _visible)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (_contentDirty)
|
||||
{
|
||||
updateContent();
|
||||
}
|
||||
|
||||
if (_currLabelEffect == LabelEffect::SHADOW && _shadowBlurRadius <= 0)
|
||||
if (! _textSprite && _currLabelEffect == LabelEffect::SHADOW && _shadowBlurRadius <= 0)
|
||||
{
|
||||
_parentTransform = parentTransform;
|
||||
draw(renderer, _modelViewTransform, true);
|
||||
|
@ -871,7 +965,14 @@ void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parent
|
|||
kmGLPushMatrix();
|
||||
kmGLLoadMatrix(&_modelViewTransform);
|
||||
|
||||
draw(renderer, _modelViewTransform, dirty);
|
||||
if (_textSprite)
|
||||
{
|
||||
_textSprite->visit();
|
||||
}
|
||||
else
|
||||
{
|
||||
draw(renderer, _modelViewTransform, dirty);
|
||||
}
|
||||
|
||||
kmGLPopMatrix();
|
||||
}
|
||||
|
@ -879,12 +980,53 @@ void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parent
|
|||
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
|
||||
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;
|
||||
|
||||
Sprite* sp = static_cast<Sprite*>(this->getChildByTag(lettetIndex));
|
||||
|
@ -913,13 +1055,7 @@ Sprite * Label::getLetter(int lettetIndex)
|
|||
|
||||
int Label::getCommonLineHeight() const
|
||||
{
|
||||
return _commonLineHeight;
|
||||
}
|
||||
|
||||
// string related stuff
|
||||
int Label::getStringNumLines() const
|
||||
{
|
||||
return _currNumLines;
|
||||
return _textSprite ? 0 : _commonLineHeight;
|
||||
}
|
||||
|
||||
void Label::computeStringNumLines()
|
||||
|
@ -969,6 +1105,11 @@ void Label::setOpacityModifyRGB(bool isOpacityModifyRGB)
|
|||
|
||||
void Label::setColor(const Color3B& color)
|
||||
{
|
||||
_fontDefinition._fontFillColor = color;
|
||||
if (_textSprite)
|
||||
{
|
||||
updateContent();
|
||||
}
|
||||
_reusedLetter->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));
|
||||
}
|
||||
|
||||
const Size& Label::getContentSize() const
|
||||
{
|
||||
if (_contentDirty)
|
||||
{
|
||||
const_cast<Label*>(this)->updateContent();
|
||||
}
|
||||
return Node::getContentSize();
|
||||
}
|
||||
NS_CC_END
|
||||
|
|
|
@ -82,6 +82,13 @@ public:
|
|||
|
||||
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,
|
||||
int fontSize, int lineSize = 0, TextHAlignment alignment = TextHAlignment::LEFT,
|
||||
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(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 */
|
||||
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(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
|
||||
*
|
||||
* @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 const std::string& getString() const override { return _originalUTF8String; }
|
||||
|
||||
|
||||
CC_DEPRECATED_ATTRIBUTE void setLabelEffect(LabelEffect effect,const Color3B& effectColor);
|
||||
|
||||
/**
|
||||
|
@ -135,43 +155,53 @@ public:
|
|||
virtual void disableEffect();
|
||||
|
||||
|
||||
virtual void setAlignment(TextHAlignment hAlignment,bool aligntext = true);
|
||||
void setAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment,_vAlignment);}
|
||||
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; }
|
||||
|
||||
virtual void setVerticalAlignment(TextVAlignment verticalAlignment,bool aligntext = true);
|
||||
void setVerticalAlignment(TextVAlignment vAlignment) { setAlignment(_hAlignment,vAlignment); }
|
||||
TextVAlignment getVerticalAlignment() const { return _vAlignment; }
|
||||
|
||||
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace);
|
||||
void setLineBreakWithoutSpace(bool breakWithoutSpace);
|
||||
|
||||
/** 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 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;}
|
||||
|
||||
/** 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 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; }
|
||||
|
||||
/** Sets the untransformed size of the label.
|
||||
* 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.
|
||||
*/
|
||||
virtual void setHeight(unsigned int height);
|
||||
void setHeight(unsigned int height){ setDimensions(_labelWidth,height);}
|
||||
unsigned int getHeight() const { return _labelHeight;}
|
||||
|
||||
/** 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 void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
|
||||
virtual void setColor(const Color3B& color) override;
|
||||
|
@ -182,9 +212,9 @@ public:
|
|||
int getCommonLineHeight() const;
|
||||
|
||||
// string related stuff
|
||||
int getStringNumLines() const;
|
||||
CC_DEPRECATED_ATTRIBUTE int getStringLenght() const { return getStringLength(); }
|
||||
int getStringNumLines() const { return _currNumLines;}
|
||||
int getStringLength() const;
|
||||
CC_DEPRECATED_ATTRIBUTE int getStringLenght() const { return getStringLength(); }
|
||||
|
||||
virtual void visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) 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 std::string getDescription() const override;
|
||||
|
||||
virtual const Size& getContentSize() const override;
|
||||
|
||||
protected:
|
||||
void onDraw(const kmMat4& transform, bool transformUpdated);
|
||||
|
||||
|
@ -208,6 +240,14 @@ protected:
|
|||
Point position;
|
||||
Size contentSize;
|
||||
};
|
||||
enum class LabelType {
|
||||
|
||||
TTF,
|
||||
BMFONT,
|
||||
CHARMAP,
|
||||
STRING_TEXTURE
|
||||
};
|
||||
|
||||
/**
|
||||
* @js NA
|
||||
*/
|
||||
|
@ -232,7 +272,6 @@ protected:
|
|||
bool computeHorizontalKernings(unsigned short int *stringToRender);
|
||||
bool setCurrentString(unsigned short *stringToSet);
|
||||
bool setOriginalString(unsigned short *stringToSet);
|
||||
void resetCurrentString();
|
||||
void computeStringNumLines();
|
||||
|
||||
void updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture);
|
||||
|
@ -243,7 +282,11 @@ protected:
|
|||
|
||||
void drawShadowWithoutBlur();
|
||||
|
||||
void createSpriteWithFontDefinition();
|
||||
|
||||
bool _isOpacityModifyRGB;
|
||||
bool _contentDirty;
|
||||
LabelType _currentLabelType;
|
||||
|
||||
std::vector<SpriteBatchNode*> _batchNodes;
|
||||
FontAtlas * _fontAtlas;
|
||||
|
@ -251,6 +294,10 @@ protected:
|
|||
|
||||
TTFConfig _fontConfig;
|
||||
|
||||
//compatibility with older LabelTTF
|
||||
Sprite* _textSprite;
|
||||
FontDefinition _fontDefinition;
|
||||
|
||||
//! used for optimization
|
||||
Sprite *_reusedLetter;
|
||||
Rect _reusedRect;
|
||||
|
@ -261,6 +308,7 @@ protected:
|
|||
int * _horizontalKernings;
|
||||
|
||||
unsigned int _maxLineWidth;
|
||||
Size _labelDimensions;
|
||||
unsigned int _labelWidth;
|
||||
unsigned int _labelHeight;
|
||||
TextHAlignment _hAlignment;
|
||||
|
|
|
@ -76,6 +76,7 @@ static std::function<Layer*()> createFunctions[] =
|
|||
CL(LabelCharMapColorTest),
|
||||
CL(LabelCrashTest),
|
||||
CL(LabelTTFOldNew),
|
||||
CL(LabelFontNameTest),
|
||||
CL(LabelAlignmentTest)
|
||||
};
|
||||
|
||||
|
@ -1558,7 +1559,7 @@ void LabelTTFOldNew::onDraw(const kmMat4 &transform, bool transformUpdated)
|
|||
kmGLPushMatrix();
|
||||
kmGLLoadMatrix(&transform);
|
||||
|
||||
auto label1 = (Label*)getChildByTag(kTagBitmapAtlas1);
|
||||
auto label1 = (LabelTTF*)getChildByTag(kTagBitmapAtlas1);
|
||||
auto labelSize = label1->getContentSize();
|
||||
auto origin = Director::getInstance()->getWinSize();
|
||||
|
||||
|
@ -1612,6 +1613,40 @@ std::string LabelTTFOldNew::subtitle() const
|
|||
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()
|
||||
{
|
||||
auto blockSize = Size(200, 160);
|
||||
|
@ -1666,42 +1701,42 @@ LabelAlignmentTest::~LabelAlignmentTest()
|
|||
void LabelAlignmentTest::setAlignmentLeft(Ref* sender)
|
||||
{
|
||||
_horizAlign = TextHAlignment::LEFT;
|
||||
_label->setHorizontalAlignment(_horizAlign,false);
|
||||
_label->setHorizontalAlignment(_horizAlign);
|
||||
_label->setString(getCurrentAlignment());
|
||||
}
|
||||
|
||||
void LabelAlignmentTest::setAlignmentCenter(Ref* sender)
|
||||
{
|
||||
_horizAlign = TextHAlignment::CENTER;
|
||||
_label->setHorizontalAlignment(_horizAlign,false);
|
||||
_label->setHorizontalAlignment(_horizAlign);
|
||||
_label->setString(getCurrentAlignment());
|
||||
}
|
||||
|
||||
void LabelAlignmentTest::setAlignmentRight(Ref* sender)
|
||||
{
|
||||
_horizAlign = TextHAlignment::RIGHT;
|
||||
_label->setHorizontalAlignment(_horizAlign,false);
|
||||
_label->setHorizontalAlignment(_horizAlign);
|
||||
_label->setString(getCurrentAlignment());
|
||||
}
|
||||
|
||||
void LabelAlignmentTest::setAlignmentTop(Ref* sender)
|
||||
{
|
||||
_vertAlign = TextVAlignment::TOP;
|
||||
_label->setVerticalAlignment(_vertAlign,false);
|
||||
_label->setVerticalAlignment(_vertAlign);
|
||||
_label->setString(getCurrentAlignment());
|
||||
}
|
||||
|
||||
void LabelAlignmentTest::setAlignmentMiddle(Ref* sender)
|
||||
{
|
||||
_vertAlign = TextVAlignment::CENTER;
|
||||
_label->setVerticalAlignment(_vertAlign,false);
|
||||
_label->setVerticalAlignment(_vertAlign);
|
||||
_label->setString(getCurrentAlignment());
|
||||
}
|
||||
|
||||
void LabelAlignmentTest::setAlignmentBottom(Ref* sender)
|
||||
{
|
||||
_vertAlign = TextVAlignment::BOTTOM;
|
||||
_label->setVerticalAlignment(_vertAlign,false);
|
||||
_label->setVerticalAlignment(_vertAlign);
|
||||
_label->setString(getCurrentAlignment());
|
||||
}
|
||||
|
||||
|
|
|
@ -450,6 +450,17 @@ public:
|
|||
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
|
||||
{
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue