issue#3630:Adjust some method for more easily create and change type.

This commit is contained in:
Dhilan007 2014-01-11 22:33:07 +08:00
parent af733ce9b5
commit 69c004108b
7 changed files with 276 additions and 168 deletions

View File

@ -32,74 +32,59 @@
NS_CC_BEGIN
Label* Label::createWithTTF(const std::string& label, const std::string& fontFilePath, int fontSize, int lineSize, TextHAlignment alignment, GlyphCollection glyphs, const char *customGlyphs, bool useDistanceField)
Label* Label::create()
{
FontAtlas *tmpAtlas = nullptr;
if(useDistanceField)
tmpAtlas = FontAtlasCache::getFontAtlasTTF(fontFilePath.c_str(), DISTANCEFIELD_ATLAS_FONTSIZE, glyphs, customGlyphs,true);
else
tmpAtlas = FontAtlasCache::getFontAtlasTTF(fontFilePath.c_str(), fontSize, glyphs, customGlyphs,false);
if (!tmpAtlas)
return nullptr;
// create the actual label
Label* templabel = Label::createWithAtlas(tmpAtlas, alignment, lineSize, useDistanceField,true);
if (templabel)
{
if(useDistanceField)
templabel->setFontSize(fontSize);
templabel->setText(label, lineSize, alignment, false);
return templabel;
}
return nullptr;
}
Label* Label::createWithBMFont(const std::string& label, const std::string& bmfontFilePath, TextHAlignment alignment, int lineSize)
{
FontAtlas *tmpAtlas = FontAtlasCache::getFontAtlasFNT(bmfontFilePath.c_str());
if (!tmpAtlas)
return 0;
Label* templabel = Label::createWithAtlas(tmpAtlas, alignment, lineSize);
if (templabel)
{
templabel->setText(label, lineSize, alignment, false);
return templabel;
}
else
{
return 0;
}
return 0;
}
Label* Label::createWithAtlas(FontAtlas *atlas, TextHAlignment alignment, int lineSize, bool useDistanceField,bool useA8Shader)
{
Label *ret = new Label(atlas, alignment, useDistanceField,useA8Shader);
Label *ret = new Label();
if (!ret)
return 0;
return nullptr;
if( ret->init() )
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();
if (!ret)
return nullptr;
if (ret->setTTFConfig(ttfConfig))
{
if(ttfConfig.distanceFieldEnable)
ret->setFontSize(ttfConfig.fontSize);
ret->setText(text,alignment,lineSize);
ret->autorelease();
return ret;
}
else
{
delete ret;
return 0;
return nullptr;
}
}
Label* Label::createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& alignment /* = TextHAlignment::CENTER */, int lineSize /* = 0 */)
{
Label *ret = new Label();
if (!ret)
return nullptr;
if (ret->setBMFontFilePath(bmfontFilePath))
{
ret->setText(text,alignment,lineSize);
ret->autorelease();
return ret;
}
else
{
delete ret;
return nullptr;
}
}
Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,bool useA8Shader)
: _reusedLetter(nullptr)
@ -108,8 +93,8 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,b
, _lineBreakWithoutSpaces(false)
, _width(0.0f)
, _alignment(alignment)
, _currentUTF16String(0)
, _originalUTF16String(0)
, _currentUTF16String(nullptr)
, _originalUTF16String(nullptr)
, _advances(nullptr)
, _fontAtlas(atlas)
, _isOpacityModifyRGB(true)
@ -118,6 +103,7 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,b
, _fontSize(0)
, _uniformEffectColor(0)
{
_cascadeColorEnabled = true;
}
Label::~Label()
@ -136,36 +122,134 @@ bool Label::init()
{
bool ret = true;
if(_fontAtlas)
{
if (_reusedLetter == nullptr)
{
_reusedLetter = Sprite::createWithTexture(&_fontAtlas->getTexture(0));
_reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB);
ret = SpriteBatchNode::initWithTexture(&_fontAtlas->getTexture(0), 30);
_reusedLetter->retain();
}
ret = SpriteBatchNode::initWithTexture(&_fontAtlas->getTexture(0), 30);
}
if (_useDistanceField)
setLabelEffect(LabelEffect::NORMAL,Color3B::BLACK);
else if(_useA8Shader)
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR));
else
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
return ret;
}
void Label::setString(const std::string &stringToRender)
bool Label::setTTFConfig(const TTFConfig& ttfConfig)
{
FontAtlas *newAtlas = nullptr;
if(ttfConfig.distanceFieldEnable)
newAtlas = FontAtlasCache::getFontAtlasTTF(ttfConfig.fontFilePath, DISTANCEFIELD_ATLAS_FONTSIZE, ttfConfig.glyphs, ttfConfig.customGlyphs,true);
else
newAtlas = FontAtlasCache::getFontAtlasTTF(ttfConfig.fontFilePath, ttfConfig.fontSize, ttfConfig.glyphs, ttfConfig.customGlyphs,false);
if (!newAtlas)
return false;
FontAtlas *oldAtlas = _fontAtlas;
bool oldDistanceFieldEnable = _useDistanceField;
bool oldA8ShaderEnabel = _useA8Shader;
_fontAtlas = newAtlas;
_useDistanceField = ttfConfig.distanceFieldEnable;
_useA8Shader = true;
bool ret = Label::init();
if (oldAtlas)
{
if (ret)
{
FontAtlasCache::releaseFontAtlas(oldAtlas);
}
else
{
_fontAtlas = oldAtlas;
_useDistanceField = oldDistanceFieldEnable;
_useA8Shader = oldA8ShaderEnabel;
Label::init();
FontAtlasCache::releaseFontAtlas(newAtlas);
}
}
if (_fontAtlas)
{
_commonLineHeight = _fontAtlas->getCommonLineHeight();
if (_currentUTF16String)
{
alignText();
}
}
return ret;
}
bool Label::setBMFontFilePath(const std::string& bmfontFilePath)
{
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasFNT(bmfontFilePath);
if (!newAtlas)
return false;
FontAtlas *oldAtlas = _fontAtlas;
bool oldDistanceFieldEnable = _useDistanceField;
bool oldA8ShaderEnabel = _useA8Shader;
_fontAtlas = newAtlas;
_useDistanceField = false;
_useA8Shader = false;
bool ret = Label::init();
if (oldAtlas)
{
if (ret)
{
FontAtlasCache::releaseFontAtlas(oldAtlas);
}
else
{
_fontAtlas = oldAtlas;
_useDistanceField = oldDistanceFieldEnable;
_useA8Shader = oldA8ShaderEnabel;
Label::init();
FontAtlasCache::releaseFontAtlas(newAtlas);
}
}
if (_fontAtlas)
{
_commonLineHeight = _fontAtlas->getCommonLineHeight();
if (_currentUTF16String)
{
alignText();
}
}
return ret;
}
void Label::setString(const std::string &text)
{
_multilineEnable = true;
setText(stringToRender, _width, TextHAlignment::CENTER, false);
setText(text, TextHAlignment::CENTER, _width, false);
}
void Label::setString(const std::string &stringToRender,bool multilineEnable)
void Label::setString(const std::string &text,bool multilineEnable)
{
_multilineEnable = multilineEnable;
setText(stringToRender, _width, TextHAlignment::CENTER, false);
setText(text, TextHAlignment::CENTER, _width, false);
}
bool Label::setText(const std::string& stringToRender, float lineWidth, TextHAlignment alignment, bool lineBreakWithoutSpaces)
bool Label::setText(const std::string& text, const TextHAlignment& alignment /* = TextHAlignment::LEFT */, float lineWidth /* = 0 */, bool lineBreakWithoutSpaces /* = false */)
{
if (!_fontAtlas)
if (!_fontAtlas || _commonLineHeight <= 0)
return false;
// carloX
@ -176,18 +260,10 @@ bool Label::setText(const std::string& stringToRender, float lineWidth, TextHAli
_alignment = alignment;
_lineBreakWithoutSpaces = lineBreakWithoutSpaces;
// store locally common line height
_commonLineHeight = _fontAtlas->getCommonLineHeight();
if (_commonLineHeight <= 0)
return false;
// int numLetter = 0;
unsigned short* utf16String = cc_utf8_to_utf16(stringToRender.c_str());
unsigned short* utf16String = cc_utf8_to_utf16(text.c_str());
if(!utf16String)
return false;
_cascadeColorEnabled = true;
setCurrentString(utf16String);
setOriginalString(utf16String);

View File

@ -54,23 +54,44 @@ enum class LabelEffect {
struct FontLetterDefinition;
class FontAtlas;
typedef struct _ttfConfig
{
std::string fontFilePath;
int fontSize;
GlyphCollection glyphs;
const char *customGlyphs;
bool distanceFieldEnable;
_ttfConfig(const char* filePath,int fontSize = 36, const GlyphCollection& glyphs = GlyphCollection::NEHE,
const char *customGlyphs = nullptr,bool useDistanceField = false)
:fontFilePath(filePath)
,fontSize(fontSize)
,glyphs(glyphs)
,customGlyphs(customGlyphs)
,distanceFieldEnable(useDistanceField)
{}
}TTFConfig;
class CC_DLL Label : public SpriteBatchNode, public LabelProtocol, public LabelTextFormatProtocol
{
public:
static Label* create();
// static create
static Label* createWithTTF(const std::string& label, const std::string& fontFilePath, int fontSize, int lineSize = 0, TextHAlignment alignment = TextHAlignment::CENTER, GlyphCollection glyphs = GlyphCollection::NEHE, const char *customGlyphs = 0, bool useDistanceField = false);
static Label* createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment = TextHAlignment::CENTER, int lineWidth = 0);
static Label* createWithBMFont(const std::string& label, const std::string& bmfontFilePath, TextHAlignment alignment = TextHAlignment::CENTER, int lineSize = 0);
static Label* createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& alignment = TextHAlignment::CENTER, int lineWidth = 0);
bool setText(const std::string& stringToRender, float lineWidth, TextHAlignment alignment = TextHAlignment::LEFT, bool lineBreakWithoutSpaces = false);
bool setTTFConfig(const TTFConfig& ttfConfig);
bool setBMFontFilePath(const std::string& bmfontFilePath);
bool setText(const std::string& text, const TextHAlignment& alignment = TextHAlignment::LEFT, float lineWidth = 0, bool lineBreakWithoutSpaces = false);
//only support for TTF
void setLabelEffect(LabelEffect effect,const Color3B& effectColor);
virtual void setString(const std::string &stringToRender) override;
void setString(const std::string &stringToRender,bool multilineEnable);
virtual void setString(const std::string &text) override;
void setString(const std::string &text,bool multilineEnable);
virtual void setAlignment(TextHAlignment alignment);
virtual void setWidth(float width);
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace);
@ -127,15 +148,13 @@ private:
/**
* @js NA
*/
Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField = false,bool useA8Shader = false);
Label(FontAtlas *atlas = nullptr, TextHAlignment alignment = TextHAlignment::CENTER, bool useDistanceField = false,bool useA8Shader = false);
/**
* @js NA
* @lua NA
*/
~Label();
static Label* createWithAtlas(FontAtlas *atlas, TextHAlignment alignment = TextHAlignment::LEFT, int lineSize = 0, bool useDistanceField = false,bool useA8Shader = false);
void setFontSize(int fontSize);
bool init();
@ -151,7 +170,6 @@ private:
virtual void updateColor() override;
//! used for optimization
Sprite *_reusedLetter;
std::vector<LetterInfo> _lettersInfo;

View File

@ -1 +1 @@
16af4ad046f4db00af7a229d89c406054d8616c3
52164446e797e85d5d6d746eb85e915c8eb7df15

View File

@ -158,20 +158,19 @@ LabelTTFAlignmentNew::LabelTTFAlignmentNew()
{
auto s = Director::getInstance()->getWinSize();
auto ttf0 = Label::createWithTTF("Alignment 0\nnew line", "fonts/tahoma.ttf", 32);
ttf0->setAlignment(TextHAlignment::LEFT);
TTFConfig config("fonts/tahoma.ttf",32);
auto ttf0 = Label::createWithTTF(config,"Alignment 0\nnew line",TextHAlignment::LEFT);
ttf0->setPosition(Point(s.width/2,(s.height/6)*2 - 30));
ttf0->setAnchorPoint(Point(0.5f,0.5f));
this->addChild(ttf0);
auto ttf1 = Label::createWithTTF("Alignment 1\nnew line", "fonts/tahoma.ttf", 32);
ttf1->setAlignment(TextHAlignment::CENTER);
auto ttf1 = Label::createWithTTF(config,"Alignment 1\nnew line",TextHAlignment::CENTER);
ttf1->setPosition(Point(s.width/2,(s.height/6)*3 - 30));
ttf1->setAnchorPoint(Point(0.5f,0.5f));
this->addChild(ttf1);
auto ttf2 = Label::createWithTTF("Alignment 2\nnew line", "fonts/tahoma.ttf", 32);
ttf1->setAlignment(TextHAlignment::RIGHT);
auto ttf2 = Label::createWithTTF(config,"Alignment 2\nnew line",TextHAlignment::RIGHT);
ttf2->setPosition(Point(s.width/2,(s.height/6)*4 - 30));
ttf2->setAnchorPoint(Point(0.5f,0.5f));
this->addChild(ttf2);
@ -194,7 +193,7 @@ LabelFNTColorAndOpacity::LabelFNTColorAndOpacity()
auto col = LayerColor::create( Color4B(128,128,128,255) );
addChild(col, -10);
auto label1 = Label::createWithBMFont("Test", "fonts/bitmapFontTest2.fnt");
auto label1 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "Test");
label1->setAnchorPoint( Point(0,0) );
addChild(label1, 0, kTagBitmapAtlas1);
@ -204,13 +203,13 @@ LabelFNTColorAndOpacity::LabelFNTColorAndOpacity()
auto repeat = RepeatForever::create(seq);
label1->runAction(repeat);
auto label2 = Label::createWithBMFont("Test", "fonts/bitmapFontTest2.fnt");
auto label2 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "Test");
label2->setAnchorPoint( Point(0.5f, 0.5f) );
label2->setColor( Color3B::RED );
addChild(label2, 0, kTagBitmapAtlas2);
label2->runAction( repeat->clone() );
auto label3 = Label::createWithBMFont("Test", "fonts/bitmapFontTest2.fnt");
auto label3 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "Test");
label3->setAnchorPoint( Point(1,1) );
addChild(label3, 0, kTagBitmapAtlas3);
@ -252,7 +251,7 @@ LabelFNTSpriteActions::LabelFNTSpriteActions()
_time = 0;
// Upper Label
auto label = Label::createWithBMFont("Bitmap Font Atlas", "fonts/bitmapFontTest.fnt");
auto label = Label::createWithBMFont("fonts/bitmapFontTest.fnt", "Bitmap Font Atlas");
addChild(label);
auto s = Director::getInstance()->getWinSize();
@ -289,7 +288,7 @@ LabelFNTSpriteActions::LabelFNTSpriteActions()
// Bottom Label
auto label2 = Label::createWithBMFont("00.0", "fonts/bitmapFontTest.fnt");
auto label2 = Label::createWithBMFont("fonts/bitmapFontTest.fnt", "00.0");
addChild(label2, 0, kTagBitmapAtlas2);
label2->setPosition( Point(s.width/2.0f, 80) );
@ -341,7 +340,7 @@ std::string LabelFNTSpriteActions::subtitle() const
LabelFNTPadding::LabelFNTPadding()
{
auto label = Label::createWithBMFont("abcdefg", "fonts/bitmapFontTest4.fnt");
auto label = Label::createWithBMFont("fonts/bitmapFontTest4.fnt", "abcdefg");
addChild(label);
auto s = Director::getInstance()->getWinSize();
@ -365,17 +364,17 @@ LabelFNTOffset::LabelFNTOffset()
auto s = Director::getInstance()->getWinSize();
Label* label = NULL;
label = Label::createWithBMFont("FaFeFiFoFu", "fonts/bitmapFontTest5.fnt");
label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "FaFeFiFoFu");
addChild(label);
label->setPosition( Point(s.width/2, s.height/2+50) );
label->setAnchorPoint( Point(0.5f, 0.5f) ) ;
label = Label::createWithBMFont("fafefifofu", "fonts/bitmapFontTest5.fnt");
label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "fafefifofu");
addChild(label);
label->setPosition( Point(s.width/2, s.height/2) );
label->setAnchorPoint( Point(0.5f, 0.5f) );
label = Label::createWithBMFont("aeiou", "fonts/bitmapFontTest5.fnt");
label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "aeiou");
addChild(label);
label->setPosition( Point(s.width/2, s.height/2-50) );
label->setAnchorPoint( Point(0.5f, 0.5f) );
@ -396,19 +395,19 @@ LabelFNTColor::LabelFNTColor()
auto s = Director::getInstance()->getWinSize();
Label* label = NULL;
label = Label::createWithBMFont("Blue", "fonts/bitmapFontTest5.fnt");
label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "Blue");
label->setColor( Color3B::BLUE );
addChild(label);
label->setPosition( Point(s.width/2, s.height/4) );
label->setAnchorPoint( Point(0.5f, 0.5f) );
label = Label::createWithBMFont("Red", "fonts/bitmapFontTest5.fnt");
label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "Red");
addChild(label);
label->setPosition( Point(s.width/2, 2*s.height/4) );
label->setAnchorPoint( Point(0.5f, 0.5f) );
label->setColor( Color3B::RED );
label = Label::createWithBMFont("G", "fonts/bitmapFontTest5.fnt");
label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "Green");
addChild(label);
label->setPosition( Point(s.width/2, 3*s.height/4) );
label->setAnchorPoint( Point(0.5f, 0.5f) );
@ -433,7 +432,7 @@ LabelFNTHundredLabels::LabelFNTHundredLabels()
{
char str[6] = {0};
sprintf(str, "-%d-", i);
auto label = Label::createWithBMFont(str, "fonts/bitmapFontTest.fnt");
auto label = Label::createWithBMFont("fonts/bitmapFontTest.fnt", str);
addChild(label);
auto s = Director::getInstance()->getWinSize();
@ -459,7 +458,7 @@ LabelFNTMultiLine::LabelFNTMultiLine()
Size s;
// Left
auto label1 = Label::createWithBMFont(" Multi line\nLeft", "fonts/bitmapFontTest3.fnt");
auto label1 = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", " Multi line\nLeft");
label1->setAnchorPoint(Point(0,0));
addChild(label1, 0, kTagBitmapAtlas1);
@ -468,7 +467,7 @@ LabelFNTMultiLine::LabelFNTMultiLine()
// Center
auto label2 = Label::createWithBMFont("Multi line\nCenter", "fonts/bitmapFontTest3.fnt");
auto label2 = Label::createWithBMFont( "fonts/bitmapFontTest3.fnt", "Multi line\nCenter");
label2->setAnchorPoint(Point(0.5f, 0.5f));
addChild(label2, 0, kTagBitmapAtlas2);
@ -476,7 +475,7 @@ LabelFNTMultiLine::LabelFNTMultiLine()
CCLOG("content size: %.2fx%.2f", s.width, s.height);
// right
auto label3 = Label::createWithBMFont("Multi line\nRight\nThree lines Three", "fonts/bitmapFontTest3.fnt");
auto label3 = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", "Multi line\nRight\nThree lines Three");
label3->setAnchorPoint(Point(1, 1));
addChild(label3, 0, kTagBitmapAtlas3);
@ -504,19 +503,18 @@ LabelFNTandTTFEmpty::LabelFNTandTTFEmpty()
float delta = s.height/4;
// LabelBMFont
auto label1 = Label::createWithBMFont("", "fonts/bitmapFontTest3.fnt", TextHAlignment::CENTER, s.width);
auto label1 = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", "", TextHAlignment::CENTER, s.width);
addChild(label1, 0, kTagBitmapAtlas1);
label1->setAnchorPoint(Point(0.5f, 0.5f));
label1->setPosition(Point(s.width/2, delta));
// LabelTTF
auto label2 = Label::createWithTTF("", "fonts/arial.ttf", 48, s.width, TextHAlignment::CENTER,GlyphCollection::NEHE);
TTFConfig ttfConfig("fonts/arial.ttf",48);
auto label2 = Label::createWithTTF(ttfConfig,"", TextHAlignment::CENTER,s.width);
addChild(label2, 0, kTagBitmapAtlas2);
label2->setAnchorPoint(Point(0.5f, 0.5f));
label2->setPosition(Point(s.width/2, delta * 2));
schedule(schedule_selector(LabelFNTandTTFEmpty::updateStrings), 1.0f);
setEmpty = false;
@ -558,7 +556,7 @@ LabelFNTRetina::LabelFNTRetina()
auto s = Director::getInstance()->getWinSize();
// LabelBMFont
auto label1 = Label::createWithBMFont("TESTING RETINA DISPLAY", "fonts/konqa32.fnt");
auto label1 = Label::createWithBMFont("fonts/konqa32.fnt", "TESTING RETINA DISPLAY");
label1->setAnchorPoint(Point(0.5f, 0.5f));
addChild(label1);
label1->setPosition(Point(s.width/2, s.height/2));
@ -582,7 +580,7 @@ LabelFNTGlyphDesigner::LabelFNTGlyphDesigner()
addChild(layer, -10);
// LabelBMFont
auto label1 = Label::createWithBMFont("Testing Glyph Designer", "fonts/futura-48.fnt");
auto label1 = Label::createWithBMFont("fonts/futura-48.fnt", "Testing Glyph Designer");
label1->setAnchorPoint(Point(0.5f, 0.5f));
addChild(label1);
label1->setPosition(Point(s.width/2, s.height/2));
@ -603,7 +601,8 @@ LabelTTFUnicodeChinese::LabelTTFUnicodeChinese()
auto size = Director::getInstance()->getWinSize();
// Adding "啊" letter at the end of string to make VS2012 happy, otherwise VS will generate errors
// like "Error 3 error C2146: syntax error : missing ')' before identifier 'label'";
auto label = Label::createWithTTF("美好的一天啊", "fonts/wt021.ttf", 55, size.width, TextHAlignment::CENTER, GlyphCollection::CUSTOM, "美好的一天啊");
TTFConfig ttfConfig("fonts/wt021.ttf",55,GlyphCollection::CUSTOM, "美好的一天啊");
auto label = Label::createWithTTF(ttfConfig,"美好的一天啊", TextHAlignment::CENTER, size.width);
label->setAnchorPoint(Point(0.5f, 0.5f));
label->setPosition(Point(size.width / 2, size.height /2));
this->addChild(label);
@ -622,7 +621,7 @@ std::string LabelTTFUnicodeChinese::subtitle() const
LabelFNTUnicodeChinese::LabelFNTUnicodeChinese()
{
auto size = Director::getInstance()->getWinSize();
auto label = Label::createWithBMFont("中国", "fonts/bitmapFontChinese.fnt");
auto label = Label::createWithBMFont("fonts/bitmapFontChinese.fnt", "中国");
label->setAnchorPoint(Point(0.5f, 0.5f));
label->setPosition(Point(size.width / 2, size.height /2));
this->addChild(label);
@ -671,8 +670,7 @@ LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment()
auto size = Director::getInstance()->getWinSize();
// create and initialize a Label
this->_labelShouldRetain = Label::createWithBMFont(LongSentencesExample, "fonts/markerFelt.fnt", TextHAlignment::CENTER, size.width/1.5);
//this->_labelShouldRetain = Label::createWithBMFont(LongSentencesExample, "fonts/bitmapFontTest.fnt", TextHAlignment::CENTER, size.width/1.5);
this->_labelShouldRetain = Label::createWithBMFont("fonts/markerFelt.fnt", LongSentencesExample, TextHAlignment::CENTER, size.width/1.5);
this->_labelShouldRetain->setAnchorPoint(Point(0.5f, 0.5f));
this->_labelShouldRetain->retain();
@ -855,22 +853,22 @@ LabelFNTUNICODELanguages::LabelFNTUNICODELanguages()
auto s = Director::getInstance()->getWinSize();
auto label1 = Label::createWithBMFont(spanish, "fonts/arial-unicode-26.fnt", TextHAlignment::CENTER, 200);
auto label1 = Label::createWithBMFont("fonts/arial-unicode-26.fnt", spanish, TextHAlignment::CENTER, 200);
addChild(label1);
label1->setAnchorPoint(Point(0.5f, 0.5f));
label1->setPosition(Point(s.width/2, s.height/5*3));
auto label2 = Label::createWithBMFont(chinese, "fonts/arial-unicode-26.fnt");
auto label2 = Label::createWithBMFont("fonts/arial-unicode-26.fnt", chinese);
addChild(label2);
label2->setAnchorPoint(Point(0.5f, 0.5f));
label2->setPosition(Point(s.width/2, s.height/5*2.5));
auto label3 = Label::createWithBMFont(russian, "fonts/arial-26-en-ru.fnt");
auto label3 = Label::createWithBMFont("fonts/arial-26-en-ru.fnt", russian);
addChild(label3);
label3->setAnchorPoint(Point(0.5f, 0.5f));
label3->setPosition(Point(s.width/2, s.height/5*2));
auto label4 = Label::createWithBMFont(japanese, "fonts/arial-unicode-26.fnt");
auto label4 = Label::createWithBMFont("fonts/arial-unicode-26.fnt", japanese);
addChild(label4);
label4->setAnchorPoint(Point(0.5f, 0.5f));
label4->setPosition(Point(s.width/2, s.height/5*1.5));
@ -894,7 +892,7 @@ LabelFNTBounds::LabelFNTBounds()
addChild(layer, -10);
// LabelBMFont
label1 = Label::createWithBMFont("Testing Glyph Designer", "fonts/boundsTestFont.fnt", TextHAlignment::CENTER, s.width);
label1 = Label::createWithBMFont("fonts/boundsTestFont.fnt", "Testing Glyph Designer", TextHAlignment::CENTER, s.width);
label1->setAnchorPoint(Point(0.5f, 0.5f));
addChild(label1);
label1->setPosition(Point(s.width/2, s.height/2));
@ -946,7 +944,8 @@ LabelTTFLongLineWrapping::LabelTTFLongLineWrapping()
auto size = Director::getInstance()->getWinSize();
// Long sentence
auto label1 = Label::createWithTTF(LongSentencesExample, "fonts/arial.ttf", 28, size.width, TextHAlignment::CENTER, GlyphCollection::NEHE);
TTFConfig ttfConfig("fonts/arial.ttf", 28);
auto label1 = Label::createWithTTF(ttfConfig, LongSentencesExample, TextHAlignment::CENTER,size.width);
label1->setPosition( Point(size.width/2, size.height/2) );
label1->setAnchorPoint(Point(0.5, 1.0));
addChild(label1);
@ -966,22 +965,23 @@ LabelTTFColor::LabelTTFColor()
{
auto size = Director::getInstance()->getWinSize();
TTFConfig ttfConfig("fonts/arial.ttf", 35);
// Green
auto label1 = Label::createWithTTF("Green", "fonts/arial.ttf", 35, size.width, TextHAlignment::CENTER, GlyphCollection::NEHE);
auto label1 = Label::createWithTTF(ttfConfig,"Green", TextHAlignment::CENTER, size.width);
label1->setPosition( Point(size.width/2, size.height/5 * 1.5) );
label1->setColor( Color3B::GREEN );
label1->setAnchorPoint(Point(0.5, 0.5));
addChild(label1);
// Red
auto label2 = Label::createWithTTF("Red", "fonts/arial.ttf", 35, size.width, TextHAlignment::CENTER, GlyphCollection::NEHE);
auto label2 = Label::createWithTTF(ttfConfig,"Red", TextHAlignment::CENTER, size.width);
label2->setPosition( Point(size.width/2, size.height/5 * 2.0) );
label2->setColor( Color3B::RED );
label2->setAnchorPoint(Point(0.5, 0.5));
addChild(label2);
// Blue
auto label3 = Label::createWithTTF("Blue", "fonts/arial.ttf", 35, size.width, TextHAlignment::CENTER, GlyphCollection::NEHE);
auto label3 = Label::createWithTTF(ttfConfig,"Blue", TextHAlignment::CENTER, size.width);
label3->setPosition( Point(size.width/2, size.height/5 * 2.5) );
label3->setColor( Color3B::BLUE );
label3->setAnchorPoint(Point(0.5, 0.5));
@ -1001,13 +1001,11 @@ std::string LabelTTFColor::subtitle() const
LabelTTFDynamicAlignment::LabelTTFDynamicAlignment()
{
auto size = Director::getInstance()->getWinSize();
_label = Label::createWithTTF(LongSentencesExample, "fonts/arial.ttf", 45, size.width, TextHAlignment::CENTER, GlyphCollection::NEHE);
TTFConfig ttfConfig("fonts/arial.ttf", 45);
_label = Label::createWithTTF(ttfConfig,LongSentencesExample, TextHAlignment::CENTER, size.width);
_label->setPosition( Point(size.width/2, size.height/2) );
_label->setAnchorPoint(Point(0.5, 0.5));
auto menu = Menu::create(
MenuItemFont::create("Left", CC_CALLBACK_1(LabelTTFDynamicAlignment::setAlignmentLeft, this)),
MenuItemFont::create("Center", CC_CALLBACK_1(LabelTTFDynamicAlignment::setAlignmentCenter, this)),
@ -1074,21 +1072,24 @@ LabelTTFUnicodeNew::LabelTTFUnicodeNew()
float vStep = size.height/9;
float vSize = size.height;
TTFConfig ttfConfig("fonts/arial.ttf", 45,GlyphCollection::ASCII);
// Spanish
auto label1 = Label::createWithTTF("Buen día, ¿cómo te llamas?", "fonts/arial.ttf", 45, size.width, TextHAlignment::CENTER, GlyphCollection::ASCII);
auto label1 = Label::createWithTTF(ttfConfig,"Buen día, ¿cómo te llamas?", TextHAlignment::CENTER, size.width);
label1->setPosition( Point(size.width/2, vSize - (vStep * 4.5)) );
label1->setAnchorPoint(Point(0.5, 0.5));
addChild(label1);
// German
auto label2 = Label::createWithTTF("In welcher Straße haben Sie gelebt?", "fonts/arial.ttf", 45, size.width, TextHAlignment::CENTER, GlyphCollection::ASCII);
auto label2 = Label::createWithTTF(ttfConfig,"In welcher Straße haben Sie gelebt?", TextHAlignment::CENTER,size.width);
label2->setPosition( Point(size.width/2, vSize - (vStep * 5.5)) );
label2->setAnchorPoint(Point(0.5, 0.5));
addChild(label2);
// chinese
auto label3 = Label::createWithTTF(chinese, "fonts/wt021.ttf", 45, size.width, TextHAlignment::CENTER, GlyphCollection::CUSTOM, chinese.c_str());
ttfConfig.fontFilePath = "fonts/wt021.ttf";
ttfConfig.glyphs = GlyphCollection::CUSTOM;
ttfConfig.customGlyphs = chinese.c_str();
auto label3 = Label::createWithTTF(ttfConfig,chinese, TextHAlignment::CENTER,size.width);
label3->setPosition( Point(size.width/2, vSize - (vStep * 6.5)) );
label3->setAnchorPoint(Point(0.5, 0.5));
addChild(label3);
@ -1118,9 +1119,10 @@ LabelTTFFontsTestNew::LabelTTFFontsTestNew()
#define arraysize(ar) (sizeof(ar) / sizeof(ar[0]))
auto size = Director::getInstance()->getWinSize();
TTFConfig ttfConfig(ttfpaths[0],40, GlyphCollection::NEHE);
for(size_t i=0;i < arraysize(ttfpaths); ++i) {
auto label = Label::createWithTTF( ttfpaths[i], ttfpaths[i], 40, 0, TextHAlignment::CENTER, GlyphCollection::NEHE);
ttfConfig.fontFilePath = ttfpaths[i];
auto label = Label::createWithTTF(ttfConfig, ttfpaths[i], TextHAlignment::CENTER,0);
if( label ) {
label->setPosition( Point(size.width/2, ((size.height * 0.6)/arraysize(ttfpaths) * i) + (size.height/5)));
@ -1147,7 +1149,7 @@ LabelBMFontTestNew::LabelBMFontTestNew()
{
auto size = Director::getInstance()->getWinSize();
auto label1 = Label::createWithBMFont("Hello World, this is testing the new Label using fnt file", "fonts/bitmapFontTest2.fnt", TextHAlignment::CENTER, size.width);
auto label1 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "Hello World, this is testing the new Label using fnt file", TextHAlignment::CENTER, size.width);
label1->setPosition( Point(size.width/2, size.height/2) );
label1->setAnchorPoint(Point(0.5, 0.5));
addChild(label1);
@ -1166,8 +1168,9 @@ std::string LabelBMFontTestNew::subtitle() const
LabelTTFDistanceField::LabelTTFDistanceField()
{
auto size = Director::getInstance()->getWinSize();
TTFConfig ttfConfig("fonts/arial.ttf", 80, GlyphCollection::DYNAMIC,nullptr,true);
auto label1 = Label::createWithTTF("Distance Field", "fonts/arial.ttf", 80, size.width, TextHAlignment::CENTER, GlyphCollection::DYNAMIC,nullptr,true);
auto label1 = Label::createWithTTF(ttfConfig,"Distance Field",TextHAlignment::CENTER,size.width);
label1->setPosition( Point(size.width/2, size.height/2) );
label1->setColor( Color3B::GREEN );
label1->setAnchorPoint(Point(0.5, 0.5));
@ -1180,7 +1183,7 @@ LabelTTFDistanceField::LabelTTFDistanceField()
nullptr);
label1->runAction(RepeatForever::create(action));
auto label2 = Label::createWithTTF("Distance Field", "fonts/arial.ttf", 80, size.width, TextHAlignment::CENTER, GlyphCollection::DYNAMIC,nullptr,true);
auto label2 = Label::createWithTTF(ttfConfig,"Distance Field",TextHAlignment::CENTER,size.width);
label2->setPosition( Point(size.width/2, size.height/5) );
label2->setColor( Color3B::RED );
label2->setAnchorPoint(Point(0.5, 0.5));
@ -1205,21 +1208,23 @@ LabelTTFDistanceFieldEffect::LabelTTFDistanceFieldEffect()
auto bg = LayerColor::create(Color4B(200,191,231,255));
this->addChild(bg);
auto label1 = Label::createWithTTF("Glow", "fonts/arial.ttf", 80, size.width, TextHAlignment::CENTER, GlyphCollection::DYNAMIC,nullptr,true);
TTFConfig ttfConfig("fonts/arial.ttf", 80, GlyphCollection::DYNAMIC,nullptr,true);
auto label1 = Label::createWithTTF(ttfConfig,"Glow", TextHAlignment::CENTER, size.width);
label1->setPosition( Point(size.width/2, size.height*0.5) );
label1->setColor( Color3B::GREEN );
label1->setAnchorPoint(Point(0.5, 0.5));
label1->setLabelEffect(LabelEffect::GLOW,Color3B::YELLOW);
addChild(label1);
auto label2 = Label::createWithTTF("Outline", "fonts/arial.ttf", 80, size.width, TextHAlignment::CENTER, GlyphCollection::DYNAMIC,nullptr,true);
auto label2 = Label::createWithTTF(ttfConfig,"Outline", TextHAlignment::CENTER, size.width);
label2->setPosition( Point(size.width/2, size.height*0.375) );
label2->setColor( Color3B::RED );
label2->setAnchorPoint(Point(0.5, 0.5));
label2->setLabelEffect(LabelEffect::OUTLINE,Color3B::BLUE);
addChild(label2);
auto label3 = Label::createWithTTF("Shadow", "fonts/arial.ttf", 80, size.width, TextHAlignment::CENTER, GlyphCollection::DYNAMIC,nullptr,true);
auto label3 = Label::createWithTTF(ttfConfig,"Shadow", TextHAlignment::CENTER, size.width);
label3->setPosition( Point(size.width/2, size.height*0.25f) );
label3->setColor( Color3B::RED );
label3->setAnchorPoint(Point(0.5, 0.5));

View File

@ -764,20 +764,22 @@ void DirectorEventTest::onEnter()
Size s = Director::getInstance()->getWinSize();
_label1 = Label::createWithTTF("Update: 0", "fonts/arial.ttf", 20);
TTFConfig ttfConfig("fonts/arial.ttf", 20);
_label1 = Label::createWithTTF(ttfConfig, "Update: 0");
_label1->setPosition(30,s.height/2 + 60);
this->addChild(_label1);
_label2 = Label::createWithTTF("Visit: 0", "fonts/arial.ttf", 20);
_label2 = Label::createWithTTF(ttfConfig, "Visit: 0");
_label2->setPosition(30,s.height/2 + 20);
this->addChild(_label2);
_label3 = Label::createWithTTF("Draw: 0", "fonts/arial.ttf", 20);
_label3 = Label::createWithTTF(ttfConfig, "Draw: 0");
_label3->setPosition(30,30);
_label3->setPosition(30,s.height/2 - 20);
this->addChild(_label3);
_label4 = Label::createWithTTF("Projection: 0", "fonts/arial.ttf", 20);
_label4 = Label::createWithTTF(ttfConfig, "Projection: 0");
_label4->setPosition(30,30);
_label4->setPosition(30,s.height/2 - 60);
this->addChild(_label4);

View File

@ -86,6 +86,7 @@ void LabelMainScene::initWithSubTest(int nodes)
_lastRenderedCount = 0;
_quantityNodes = 0;
_accumulativeTime = 0.0f;
_labelContainer = Layer::create();
addChild(_labelContainer);
@ -212,15 +213,18 @@ void LabelMainScene::onIncrease(Object* sender)
}
break;
case kCaseLabelUpdate:
{
TTFConfig ttfConfig("fonts/arial.ttf", 60, GlyphCollection::DYNAMIC, nullptr, true);
for( int i=0;i< kNodesIncrease;i++)
{
auto label = Label::createWithTTF("Label", "fonts/arial.ttf", 60, size.width, TextHAlignment::CENTER, GlyphCollection::DYNAMIC,nullptr,true);
auto label = Label::createWithTTF(ttfConfig, "Label", TextHAlignment::CENTER, size.width);
label->setPosition(Point((size.width/2 + rand() % 50), ((int)size.height/2 + rand() % 50)));
_labelContainer->addChild(label, 1, _quantityNodes);
_quantityNodes++;
}
break;
}
case kCaseLabelBMFontBigLabels:
for( int i=0;i< kNodesIncrease;i++)
{
@ -232,15 +236,18 @@ void LabelMainScene::onIncrease(Object* sender)
}
break;
case kCaseLabelBigLabels:
{
TTFConfig ttfConfig("fonts/arial.ttf", 60, GlyphCollection::DYNAMIC);
for( int i=0;i< kNodesIncrease;i++)
{
auto label = Label::createWithTTF(LongSentencesExample, "fonts/arial.ttf", 60, size.width, TextHAlignment::CENTER, GlyphCollection::DYNAMIC,nullptr);
auto label = Label::createWithTTF(ttfConfig, LongSentencesExample, TextHAlignment::CENTER, size.width);
label->setPosition(Point((rand() % 50), rand()%((int)size.height/3)));
_labelContainer->addChild(label, 1, _quantityNodes);
_quantityNodes++;
}
break;
}
default:
break;
}

View File

@ -54,7 +54,7 @@ skip = Node::[^setPosition$ setGLServerState description getUserObject .*UserDat
Copying::[*],
LabelProtocol::[*],
LabelTextFormatProtocol::[*],
Label::[getLettersInfo],
Label::[getLettersInfo createWithTTF setTTFConfig],
.*Delegate::[*],
PoolManager::[*],
Texture2D::[initWithPVRTCData addPVRTCImage releaseData setTexParameters initWithData keepData getPixelFormatInfoMap],