issue #1310: Updated LabelTest. Added init method for CCLabelBMFont. Fixed a bug in CCLabelAtlas::initWithString.

In CCSpriteBatchNode::initWithTexture, when capacity is equal to 0, set it to default value.
This commit is contained in:
James Chen 2012-06-12 14:33:53 +08:00
parent ef8ec1a941
commit b770010c41
6 changed files with 355 additions and 27 deletions

View File

@ -87,12 +87,12 @@ bool CCLabelAtlas::initWithString(const char *theString, const char *fntFile)
{
CCDictionary *dict = CCDictionary::dictionaryWithContentsOfFile(CCFileUtils::sharedFileUtils()->sharedFileUtils()->fullPathFromRelativePath(fntFile));
CCAssert(((CCInteger*)dict->objectForKey("version"))->getValue() == 1, "Unsupported version. Upgrade cocos2d version");
CCAssert(((CCString*)dict->objectForKey("version"))->intValue() == 1, "Unsupported version. Upgrade cocos2d version");
CCString *textureFilename = (CCString*)dict->objectForKey("textureFilename");
unsigned int width = ((CCInteger*)dict->objectForKey("itemWidth"))->getValue() / CC_CONTENT_SCALE_FACTOR();
unsigned int height = ((CCInteger*)dict->objectForKey("itemHeight"))->getValue() / CC_CONTENT_SCALE_FACTOR();
unsigned int startChar = ((CCInteger*)dict->objectForKey("firstChar"))->getValue();
unsigned int width = ((CCString*)dict->objectForKey("itemWidth"))->intValue() / CC_CONTENT_SCALE_FACTOR();
unsigned int height = ((CCString*)dict->objectForKey("itemHeight"))->intValue() / CC_CONTENT_SCALE_FACTOR();
unsigned int startChar = ((CCString*)dict->objectForKey("firstChar"))->intValue();
this->initWithString(theString, textureFilename->getCString(), width, height, startChar);

View File

@ -752,6 +752,11 @@ CCLabelBMFont *CCLabelBMFont::labelWithString(const char *str, const char *fntFi
return NULL;
}
bool CCLabelBMFont::init()
{
return initWithString(NULL, NULL, kCCLabelAutomaticWidth, kCCTextAlignmentLeft, CCPointZero);
}
bool CCLabelBMFont::initWithString(const char *theString, const char *fntFile)
{
return initWithString(theString, fntFile, kCCLabelAutomaticWidth, kCCTextAlignmentLeft, CCPointZero);
@ -788,6 +793,11 @@ bool CCLabelBMFont::initWithString(const char *theString, const char *fntFile, f
texture->autorelease();
}
if (theString == NULL)
{
theString = "";
}
if (CCSpriteBatchNode::initWithTexture(texture, strlen(theString)))
{
m_pAlignment = alignment;
@ -1352,6 +1362,33 @@ float CCLabelBMFont::getLetterPosXRight( CCSprite* sp )
{
return sp->getPosition().x * m_fScaleX + (sp->getContentSize().width * m_fScaleX * sp->getAnchorPoint().x);
}
// LabelBMFont - FntFile
void CCLabelBMFont::setFntFile(const char* fntFile)
{
if (fntFile != NULL && strcmp(fntFile, m_sFntFile.c_str()) != 0 )
{
CCBMFontConfiguration *newConf = FNTConfigLoadFile(fntFile);
CCAssert( newConf, "CCLabelBMFont: Impossible to create font. Please check file");
m_sFntFile = fntFile;
CC_SAFE_RELEASE(m_pConfiguration);
CC_SAFE_RETAIN(newConf);
m_pConfiguration = newConf;
this->setTexture(CCTextureCache::sharedTextureCache()->addImage(m_pConfiguration->getAtlasName()));
this->createFontChars();
}
}
const char* CCLabelBMFont::getFntFile()
{
return m_sFntFile.c_str();
}
//LabelBMFont - Debug draw
#if CC_LABELBMFONT_DEBUG_DRAW
void CCLabelBMFont::draw()

View File

@ -192,6 +192,7 @@ public:
static CCLabelBMFont * labelWithString(const char *str, const char *fntFile, float width, CCTextAlignment alignment);
static CCLabelBMFont * labelWithString(const char *str, const char *fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset);
bool init();
/** init a bitmap font altas with an initial string and the FNT file */
bool initWithString(const char *str, const char *fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset);
bool initWithString(const char *str, const char *fntFile, float width, CCTextAlignment alignment);
@ -213,6 +214,8 @@ public:
virtual void setScaleX(float scaleX);
virtual void setScaleY(float scaleY);
void setFntFile(const char* fntFile);
const char* getFntFile();
#if CC_LABELBMFONT_DEBUG_DRAW
virtual void draw();
#endif // CC_LABELBMFONT_DEBUG_DRAW

View File

@ -93,6 +93,12 @@ bool CCSpriteBatchNode::initWithTexture(CCTexture2D *tex, unsigned int capacity)
m_blendFunc.src = CC_BLEND_SRC;
m_blendFunc.dst = CC_BLEND_DST;
m_pobTextureAtlas = new CCTextureAtlas();
if (0 == capacity)
{
capacity = defaultCapacity;
}
m_pobTextureAtlas->initWithTexture(tex, capacity);
updateBlendFunc();

View File

@ -40,7 +40,7 @@ CCLayer* restartAtlasAction();
static int sceneIdx = -1;
#define MAX_LAYER 22
#define MAX_LAYER 26
CCLayer* createAtlasLayer(int nIndex)
{
@ -70,6 +70,10 @@ CCLayer* createAtlasLayer(int nIndex)
case 19: return new LabelTTFA8Test();
case 20: return new BMFontOneAtlas();
case 21: return new BMFontUnicode();
case 22: return new BMFontInit();
case 23: return new TTFFontInit();
case 24: return new Issue1343();
case 25: return new LabelTTFAlignment();
}
return NULL;
@ -151,9 +155,9 @@ void AtlasDemo::onEnter()
CCMenu *menu = CCMenu::menuWithItems(item1, item2, item3, NULL);
menu->setPosition( CCPointZero );
item1->setPosition( ccp( s.width/2 - 100,30) );
item2->setPosition( ccp( s.width/2, 30) );
item3->setPosition( ccp( s.width/2 + 100,30) );
item1->setPosition( ccp( s.width/2 - item2->getContentSize().width*2, item2->getContentSize().height/2));
item2->setPosition( ccp( s.width/2, item2->getContentSize().height/2));
item3->setPosition( ccp( s.width/2 + item2->getContentSize().width*2, item2->getContentSize().height/2));
addChild(menu, 1);
}
@ -265,12 +269,12 @@ LabelAtlasTest::LabelAtlasTest()
{
m_time = 0;
CCLabelAtlas* label1 = CCLabelAtlas::labelWithString("123 Test", "fonts/tuffy_bold_italic-charmap.png", 48, 64, ' ');
CCLabelAtlas* label1 = CCLabelAtlas::labelWithString("123 Test", "fonts/tuffy_bold_italic-charmap.plist");
addChild(label1, 0, kTagSprite1);
label1->setPosition( ccp(10,100) );
label1->setOpacity( 200 );
CCLabelAtlas *label2 = CCLabelAtlas::labelWithString("0123456789", "fonts/tuffy_bold_italic-charmap.png", 48, 64, ' ');
CCLabelAtlas *label2 = CCLabelAtlas::labelWithString("0123456789", "fonts/tuffy_bold_italic-charmap.plist");
addChild(label2, 0, kTagSprite2);
label2->setPosition( ccp(10,200) );
label2->setOpacity( 32 );
@ -358,6 +362,40 @@ std::string LabelAtlasColorTest::subtitle()
}
//------------------------------------------------------------------
//
// LabelTTFAlignment
//
//------------------------------------------------------------------
LabelTTFAlignment::LabelTTFAlignment()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* ttf0 = CCLabelTTF::labelWithString("Alignment 0\nnew line", CCSizeMake(256, 32), kCCTextAlignmentLeft, "Helvetica", 12);
ttf0->setPosition(ccp(s.width/2,(s.height/6)*2));
ttf0->setAnchorPoint(ccp(0.5f,0.5f));
this->addChild(ttf0);
CCLabelTTF* ttf1 = CCLabelTTF::labelWithString("Alignment 1\nnew line", CCSizeMake(245, 32), kCCTextAlignmentCenter, "Helvetica", 12);
ttf1->setPosition(ccp(s.width/2,(s.height/6)*3));
ttf1->setAnchorPoint(ccp(0.5f,0.5f));
this->addChild(ttf1);
CCLabelTTF* ttf2 = CCLabelTTF::labelWithString("Alignment 2\nnew line", CCSizeMake(245, 32), kCCTextAlignmentRight, "Helvetica", 12);
ttf2->setPosition(ccp(s.width/2,(s.height/6)*4));
ttf2->setAnchorPoint(ccp(0.5f,0.5f));
this->addChild(ttf2);
}
std::string LabelTTFAlignment::title()
{
return "CCLabelTTF alignment";
}
std::string LabelTTFAlignment::subtitle()
{
return "Tests alignment values";
}
//------------------------------------------------------------------
//
// Atlas3
@ -849,7 +887,7 @@ LabelAtlasHD::LabelAtlasHD()
CCSize s = CCDirector::sharedDirector()->getWinSize();
// CCLabelBMFont
CCLabelAtlas *label1 = CCLabelAtlas::labelWithString("TESTING RETINA DISPLAY", "fonts/larabie-16.png", 10, 20, 'A');
CCLabelAtlas *label1 = CCLabelAtlas::labelWithString("TESTING RETINA DISPLAY", "fonts/larabie-16.plist");
label1->setAnchorPoint(ccp(0.5f, 0.5f));
addChild(label1);
@ -909,20 +947,129 @@ void AtlasTestScene::runThisTest()
//------------------------------------------------------------------
LabelTTFTest::LabelTTFTest()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCSize blockSize = CCSizeMake(200, 160);
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLayerColor *colorLayer = CCLayerColor::layerWithColor(ccc4(100, 100, 100, 255), blockSize.width, blockSize.height);
colorLayer->setAnchorPoint(ccp(0,0));
colorLayer->setPosition(ccp((s.width - blockSize.width) / 2, (s.height - blockSize.height) / 2));
this->addChild(colorLayer);
CCMenuItemFont::setFontSize(30);
CCMenu *menu = CCMenu::menuWithItems(
CCMenuItemFont::itemWithString("Left", this, menu_selector(LabelTTFTest::setAlignmentLeft)),
CCMenuItemFont::itemWithString("Center", this, menu_selector(LabelTTFTest::setAlignmentCenter)),
CCMenuItemFont::itemWithString("Right", this, menu_selector(LabelTTFTest::setAlignmentRight)),
NULL);
menu->alignItemsVerticallyWithPadding(4);
menu->setPosition(ccp(50, s.height / 2 - 20));
this->addChild(menu);
menu = CCMenu::menuWithItems(
CCMenuItemFont::itemWithString("Top", this, menu_selector(LabelTTFTest::setAlignmentTop)),
CCMenuItemFont::itemWithString("Middle", this, menu_selector(LabelTTFTest::setAlignmentMiddle)),
CCMenuItemFont::itemWithString("Bottom", this, menu_selector(LabelTTFTest::setAlignmentBottom)),
NULL);
menu->alignItemsVerticallyWithPadding(4);
menu->setPosition(ccp(s.width - 50, s.height / 2 - 20));
this->addChild(menu);
m_plabel = NULL;
m_eHorizAlign = kCCTextAlignmentLeft;
m_eVertAlign = kCCVerticalTextAlignmentTop;
this->updateAlignment();
}
// CCLabelBMFont
CCLabelTTF *left = CCLabelTTF::labelWithString("align left", CCSizeMake(s.width, 50), kCCTextAlignmentLeft, "Marker Felt", 32);
CCLabelTTF *center = CCLabelTTF::labelWithString("align center", CCSizeMake(s.width, 50), kCCTextAlignmentCenter, "Marker Felt", 32);
CCLabelTTF *right = CCLabelTTF::labelWithString("align right", CCSizeMake(s.width, 50), kCCTextAlignmentRight, "Marker Felt", 32);
LabelTTFTest::~LabelTTFTest()
{
CC_SAFE_RELEASE(m_plabel);
}
left->setPosition(ccp(s.width / 2, 200));
center->setPosition(ccp(s.width / 2, 150));
right->setPosition(ccp(s.width / 2, 100));
addChild(left);
addChild(center);
addChild(right);
void LabelTTFTest::updateAlignment()
{
CCSize blockSize = CCSizeMake(200, 160);
CCSize s = CCDirector::sharedDirector()->getWinSize();
if (m_plabel)
{
m_plabel->removeFromParentAndCleanup(true);
}
m_plabel = CCLabelTTF::labelWithString(this->getCurrentAlignment(), blockSize, m_eHorizAlign, m_eVertAlign, "Marker Felt", 32);
m_plabel->retain();
m_plabel->setAnchorPoint(ccp(0,0));
m_plabel->setPosition(ccp((s.width - blockSize.width) / 2, (s.height - blockSize.height)/2 ));
this->addChild(m_plabel);
}
void LabelTTFTest::setAlignmentLeft(CCObject* pSender)
{
m_eHorizAlign = kCCTextAlignmentLeft;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentCenter(CCObject* pSender)
{
m_eHorizAlign = kCCTextAlignmentCenter;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentRight(CCObject* pSender)
{
m_eHorizAlign = kCCTextAlignmentRight;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentTop(CCObject* pSender)
{
m_eVertAlign = kCCVerticalTextAlignmentTop;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentMiddle(CCObject* pSender)
{
m_eVertAlign = kCCVerticalTextAlignmentCenter;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentBottom(CCObject* pSender)
{
m_eVertAlign = kCCVerticalTextAlignmentBottom;
this->updateAlignment();
}
const char* LabelTTFTest::getCurrentAlignment()
{
const char* vertical = NULL;
const char* horizontal = NULL;
switch (m_eVertAlign) {
case kCCVerticalTextAlignmentTop:
vertical = "Top";
break;
case kCCVerticalTextAlignmentCenter:
vertical = "Middle";
break;
case kCCVerticalTextAlignmentBottom:
vertical = "Bottom";
break;
}
switch (m_eHorizAlign) {
case kCCTextAlignmentLeft:
horizontal = "Left";
break;
case kCCTextAlignmentCenter:
horizontal = "Center";
break;
case kCCTextAlignmentRight:
horizontal = "Right";
break;
}
return CCString::stringWithFormat("Alignment %s %s", vertical, horizontal)->getCString();
}
string LabelTTFTest::title()
@ -932,16 +1079,20 @@ string LabelTTFTest::title()
string LabelTTFTest::subtitle()
{
return "You should see 3 labels aligned left, center and right";
return "Select the buttons on the sides to change alignment";
}
LabelTTFMultiline::LabelTTFMultiline()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
// CCLabelBMFont
CCLabelTTF *center = CCLabelTTF::labelWithString("word wrap \"testing\" (bla0) bla1 'bla2' [bla3] (bla4) {bla5} {bla6} [bla7] (bla8) [bla9] 'bla0' \"bla1\"",
CCSizeMake(s.width / 2, 200), kCCTextAlignmentCenter, "MarkerFelt.ttc", 32);
CCSizeMake(s.width/2,200),
kCCTextAlignmentCenter,
kCCVerticalTextAlignmentTop,
"Paint Boy",
32);
center->setPosition(ccp(s.width / 2, 150));
addChild(center);
@ -954,7 +1105,7 @@ string LabelTTFMultiline::title()
string LabelTTFMultiline::subtitle()
{
return "Word wrap using CCLabelTTF";
return "Word wrap using CCLabelTTF and a custom TTF font";
}
LabelTTFChinese::LabelTTFChinese()
@ -1268,4 +1419,85 @@ std::string BMFontUnicode::title()
std::string BMFontUnicode::subtitle()
{
return "You should see 3 differnt labels: In Spanish, Chinese and Korean";
}
// BMFontInit
BMFontInit::BMFontInit()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelBMFont* bmFont = new CCLabelBMFont();
bmFont->init();
bmFont->autorelease();
//CCLabelBMFont* bmFont = [CCLabelBMFont labelWithString:@"Foo" fntFile:@"arial-unicode-26.fnt"];
bmFont->setFntFile("fonts/helvetica-32.fnt");
bmFont->setString("It is working!");
this->addChild(bmFont);
bmFont->setPosition(ccp(s.width/2,s.height/4*2));
}
std::string BMFontInit::title()
{
return "CCLabelBMFont init";
}
std::string BMFontInit::subtitle()
{
return "Test for support of init method without parameters.";
}
// TTFFontInit
TTFFontInit::TTFFontInit()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* font = new CCLabelTTF();
font->init();
font->autorelease();
font->setFontName("Marker Felt");
font->setFontSize(48);
font->setString("It is working!");
this->addChild(font);
font->setPosition(ccp(s.width/2,s.height/4*2));
}
std::string TTFFontInit::title()
{
return "CCLabelTTF init";
}
std::string TTFFontInit::subtitle()
{
return "Test for support of init method without parameters.";
}
// Issue1343
Issue1343::Issue1343()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelBMFont* bmFont = new CCLabelBMFont();
bmFont->init();
bmFont->setFntFile("fonts/font-issue1343.fnt");
bmFont->setString("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.,'");
this->addChild(bmFont);
bmFont->release();
bmFont->setScale(0.3f);
bmFont->setPosition(ccp(s.width/2,s.height/4*2));
}
std::string Issue1343::title()
{
return "Issue 1343";
}
std::string Issue1343::subtitle()
{
return "You should see: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.,'";
}

View File

@ -54,6 +54,15 @@ public:
virtual std::string subtitle();
};
class LabelTTFAlignment : public AtlasDemo
{
public:
LabelTTFAlignment();
virtual std::string title();
virtual std::string subtitle();
};
class Atlas3 : public AtlasDemo
{
float m_time;
@ -166,8 +175,22 @@ class LabelTTFTest : public AtlasDemo
{
public:
LabelTTFTest();
virtual ~LabelTTFTest();
virtual std::string title();
virtual std::string subtitle();
private:
void setAlignmentLeft(CCObject* pSender);
void setAlignmentCenter(CCObject* pSender);
void setAlignmentRight(CCObject* pSender);
void setAlignmentTop(CCObject* pSender);
void setAlignmentMiddle(CCObject* pSender);
void setAlignmentBottom(CCObject* pSender);
void updateAlignment();
const char* getCurrentAlignment();
private:
CCLabelTTF* m_plabel;
CCTextAlignment m_eHorizAlign;
CCVerticalTextAlignment m_eVertAlign;
};
class LabelTTFMultiline : public AtlasDemo
@ -241,6 +264,33 @@ public:
virtual std::string subtitle();
};
class BMFontInit : public AtlasDemo
{
public:
BMFontInit();
virtual std::string title();
virtual std::string subtitle();
};
class TTFFontInit : public AtlasDemo
{
public:
TTFFontInit();
virtual std::string title();
virtual std::string subtitle();
};
class Issue1343 : public AtlasDemo
{
public:
Issue1343();
virtual std::string title();
virtual std::string subtitle();
};
// we don't support linebreak mode
#endif