axmol/tests/cpp-tests/Classes/LabelTest/LabelTest.cpp

1756 lines
51 KiB
C++
Raw Normal View History

2013-12-25 14:52:49 +08:00
#include "LabelTest.h"
2012-03-22 11:48:37 +08:00
#include "../testResource.h"
2014-04-30 08:37:36 +08:00
#include "renderer/CCRenderer.h"
#include "renderer/CCCustomCommand.h"
2012-03-22 11:48:37 +08:00
2014-03-31 17:21:56 +08:00
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif _MSC_VER >= 1400 //vs 2005 or higher
#pragma warning (push)
#pragma warning (disable: 4996)
#endif
2010-09-07 11:47:54 +08:00
enum {
kTagTileMap = 1,
kTagSpriteManager = 1,
kTagAnimation1 = 1,
kTagBitmapAtlas1 = 1,
kTagBitmapAtlas2 = 2,
kTagBitmapAtlas3 = 3,
2010-09-07 11:47:54 +08:00
};
enum {
kTagSprite1,
kTagSprite2,
kTagSprite3,
kTagSprite4,
kTagSprite5,
kTagSprite6,
kTagSprite7,
kTagSprite8,
2012-03-22 11:48:37 +08:00
};
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// AtlasDemo
//
//------------------------------------------------------------------
2012-03-22 11:48:37 +08:00
2010-09-07 11:47:54 +08:00
enum
{
IDC_NEXT = 100,
IDC_BACK,
IDC_RESTART
2010-09-07 11:47:54 +08:00
};
2011-07-08 15:57:46 +08:00
Layer* nextAtlasAction();
Layer* backAtlasAction();
Layer* restartAtlasAction();
2012-03-22 11:48:37 +08:00
static int sceneIdx = -1;
static std::function<Layer*()> createFunctions[] =
{
CL(LabelAtlasTest),
CL(LabelAtlasColorTest),
CL(Atlas3),
CL(Atlas4),
CL(Atlas5),
CL(Atlas6),
CL(AtlasBitmapColor),
CL(AtlasFastBitmap),
CL(BitmapFontMultiLine),
CL(LabelsEmpty),
CL(LabelBMFontHD),
CL(LabelAtlasHD),
CL(LabelGlyphDesigner),
CL(LabelTTFTest),
CL(LabelTTFMultiline),
CL(LabelTTFChinese),
CL(LabelBMFontChinese),
CL(BitmapFontMultiLineAlignment),
CL(LabelTTFA8Test),
CL(BMFontOneAtlas),
CL(BMFontUnicode),
CL(BMFontInit),
CL(TTFFontInit),
CL(Issue1343),
CL(LabelTTFAlignment),
CL(LabelBMFontBounds),
CL(TTFFontShadowAndStroke),
// should be moved to another test
CL(Atlas1),
CL(LabelBMFontCrashTest),
CL(LabelBMFontBinaryFormat),
};
2010-09-07 11:47:54 +08:00
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
2010-09-07 11:47:54 +08:00
Layer* nextAtlasAction()
2010-09-07 11:47:54 +08:00
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;
2010-09-07 11:47:54 +08:00
auto layer = (createFunctions[sceneIdx])();
return layer;
2010-09-07 11:47:54 +08:00
}
Layer* backAtlasAction()
2010-09-07 11:47:54 +08:00
{
sceneIdx--;
int total = MAX_LAYER;
if( sceneIdx < 0 )
sceneIdx += total;
auto layer = (createFunctions[sceneIdx])();
return layer;
2010-09-07 11:47:54 +08:00
}
Layer* restartAtlasAction()
2010-09-07 11:47:54 +08:00
{
auto layer = (createFunctions[sceneIdx])();
return layer;
2012-03-22 11:48:37 +08:00
}
AtlasDemo::AtlasDemo(void)
{
}
AtlasDemo::~AtlasDemo(void)
{
}
std::string AtlasDemo::title() const
2010-09-07 11:47:54 +08:00
{
return "No title";
2010-09-07 11:47:54 +08:00
}
std::string AtlasDemo::subtitle() const
2010-09-07 11:47:54 +08:00
{
return "";
2010-09-07 11:47:54 +08:00
}
void AtlasDemo::onEnter()
{
BaseTest::onEnter();
2010-09-07 11:47:54 +08:00
}
void AtlasDemo::restartCallback(Ref* sender)
2010-09-07 11:47:54 +08:00
{
auto s = new AtlasTestScene();
s->addChild(restartAtlasAction());
2010-09-07 11:47:54 +08:00
Director::getInstance()->replaceScene(s);
2010-09-07 11:47:54 +08:00
s->release();
}
void AtlasDemo::nextCallback(Ref* sender)
2010-09-07 11:47:54 +08:00
{
auto s = new AtlasTestScene();
s->addChild( nextAtlasAction() );
Director::getInstance()->replaceScene(s);
2010-09-07 11:47:54 +08:00
s->release();
}
void AtlasDemo::backCallback(Ref* sender)
2010-09-07 11:47:54 +08:00
{
auto s = new AtlasTestScene();
s->addChild( backAtlasAction() );
Director::getInstance()->replaceScene(s);
2010-09-07 11:47:54 +08:00
s->release();
}
2012-03-22 11:48:37 +08:00
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// Atlas1
//
//------------------------------------------------------------------
Atlas1::Atlas1()
{
2014-05-10 09:39:25 +08:00
setGLProgram(GLProgramCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE));
_textureAtlas = TextureAtlas::create(s_AtlasTest, 3); _textureAtlas->retain();
auto s = Director::getInstance()->getWinSize();
//
// Notice: u,v tex coordinates are inverted
//
V3F_C4B_T2F_Quad quads[] =
{
{
2014-04-02 15:25:19 +08:00
{Vector3(0,0,0),Color4B(0,0,255,255),Tex2F(0.0f,1.0f),}, // bottom left
{Vector3(s.width,0,0),Color4B(0,0,255,0),Tex2F(1.0f,1.0f),}, // bottom right
{Vector3(0,s.height,0),Color4B(0,0,255,0),Tex2F(0.0f,0.0f),}, // top left
{Vector3(s.width,s.height,0),Color4B(0,0,255,255),Tex2F(1.0f,0.0f),}, // top right
},
{
2014-04-02 15:25:19 +08:00
{Vector3(40,40,0),Color4B(255,255,255,255),Tex2F(0.0f,0.2f),}, // bottom left
{Vector3(120,80,0),Color4B(255,0,0,255),Tex2F(0.5f,0.2f),}, // bottom right
{Vector3(40,160,0),Color4B(255,255,255,255),Tex2F(0.0f,0.0f),}, // top left
{Vector3(160,160,0),Color4B(0,255,0,255),Tex2F(0.5f,0.0f),}, // top right
},
{
2014-04-02 15:25:19 +08:00
{Vector3(s.width/2,40,0),Color4B(255,0,0,255),Tex2F(0.0f,1.0f),}, // bottom left
{Vector3(s.width,40,0),Color4B(0,255,0,255),Tex2F(1.0f,1.0f),}, // bottom right
{Vector3(s.width/2-50,200,0),Color4B(0,0,255,255),Tex2F(0.0f,0.0f),}, // top left
{Vector3(s.width,100,0),Color4B(255,255,0,255),Tex2F(1.0f,0.0f),}, // top right
},
};
for( int i=0;i<3;i++)
{
_textureAtlas->updateQuad(&quads[i], i);
}
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
Atlas1::~Atlas1()
{
_textureAtlas->release();
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
2014-04-08 22:07:35 +08:00
void Atlas1::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
2010-09-07 11:47:54 +08:00
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(Atlas1::onDraw, this, transform, transformUpdated);
renderer->addCommand(&_customCommand);
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
2014-04-10 10:16:16 +08:00
void Atlas1::onDraw(const Matrix &transform, bool transformUpdated)
2013-12-25 14:52:49 +08:00
{
2014-05-09 07:42:36 +08:00
getGLProgram()->use();
getGLProgram()->setUniformsForBuiltins(transform);
2013-12-25 14:52:49 +08:00
_textureAtlas->drawQuads();
}
std::string Atlas1::title() const
2010-09-07 11:47:54 +08:00
{
return "CCTextureAtlas";
2012-03-22 11:48:37 +08:00
}
std::string Atlas1::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Manual creation of TextureAtlas";
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// LabelAtlasTest
//
//------------------------------------------------------------------
LabelAtlasTest::LabelAtlasTest()
{
_time = 0;
2010-09-07 11:47:54 +08:00
auto label1 = LabelAtlas::create("123 Test", "fonts/tuffy_bold_italic-charmap.plist");
addChild(label1, 0, kTagSprite1);
label1->setPosition( Vector2(10,100) );
label1->setOpacity( 200 );
2010-09-07 11:47:54 +08:00
auto label2 = LabelAtlas::create("0123456789", "fonts/tuffy_bold_italic-charmap.plist");
addChild(label2, 0, kTagSprite2);
label2->setPosition( Vector2(10,200) );
label2->setOpacity( 32 );
2010-09-07 11:47:54 +08:00
schedule(schedule_selector(LabelAtlasTest::step));
2010-09-07 11:47:54 +08:00
}
2012-06-08 13:55:28 +08:00
void LabelAtlasTest::step(float dt)
2010-09-07 11:47:54 +08:00
{
_time += dt;
2010-09-07 11:47:54 +08:00
char string[12] = {0};
sprintf(string, "%2.2f Test", _time);
//string.format("%2.2f Test", _time);
2010-09-07 11:47:54 +08:00
auto label1 = (LabelAtlas*)getChildByTag(kTagSprite1);
label1->setString(string);
2010-09-07 11:47:54 +08:00
auto label2 = (LabelAtlas*)getChildByTag(kTagSprite2);
sprintf(string, "%d", (int)_time);
//string.format("%d", (int)_time);
label2->setString(string);
2010-09-07 11:47:54 +08:00
}
std::string LabelAtlasTest::title() const
2010-09-07 11:47:54 +08:00
{
return "LabelAtlas";
2012-03-22 11:48:37 +08:00
}
std::string LabelAtlasTest::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Updating label should be fast";
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// LabelAtlasColorTest
//
//------------------------------------------------------------------
2012-03-22 11:48:37 +08:00
LabelAtlasColorTest::LabelAtlasColorTest()
{
auto label1 = LabelAtlas::create("123 Test", "fonts/tuffy_bold_italic-charmap.png", 48, 64, ' ');
addChild(label1, 0, kTagSprite1);
label1->setPosition( Vector2(10,100) );
label1->setOpacity( 200 );
auto label2 = LabelAtlas::create("0123456789", "fonts/tuffy_bold_italic-charmap.png", 48, 64, ' ');
addChild(label2, 0, kTagSprite2);
label2->setPosition( Vector2(10,200) );
label2->setColor( Color3B::RED );
2012-03-22 11:48:37 +08:00
auto fade = FadeOut::create(1.0f);
auto fade_in = fade->reverse();
auto cb = CallFunc::create(CC_CALLBACK_0(LabelAtlasColorTest::actionFinishCallback, this));
auto seq = Sequence::create(fade, fade_in, cb, NULL);
auto repeat = RepeatForever::create( seq );
label2->runAction( repeat );
2012-03-22 11:48:37 +08:00
_time = 0;
schedule( schedule_selector(LabelAtlasColorTest::step) ); //:@selector(step:)];
2012-03-22 11:48:37 +08:00
}
2013-02-28 15:57:15 +08:00
void LabelAtlasColorTest::actionFinishCallback()
{
CCLOG("Action finished");
}
2012-06-08 13:55:28 +08:00
void LabelAtlasColorTest::step(float dt)
2012-03-22 11:48:37 +08:00
{
_time += dt;
2012-03-22 11:48:37 +08:00
char string[12] = {0};
sprintf(string, "%2.2f Test", _time);
//std::string string = std::string::createWithFormat("%2.2f Test", _time);
auto label1 = (LabelAtlas*)getChildByTag(kTagSprite1);
label1->setString(string);
auto label2 = (LabelAtlas*)getChildByTag(kTagSprite2);
sprintf(string, "%d", (int)_time);
label2->setString( string );
2012-03-22 11:48:37 +08:00
}
std::string LabelAtlasColorTest::title() const
2012-03-22 11:48:37 +08:00
{
return "CCLabelAtlas";
2012-03-22 11:48:37 +08:00
}
std::string LabelAtlasColorTest::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Opacity + Color should work at the same time";
2012-03-22 11:48:37 +08:00
}
//------------------------------------------------------------------
//
// LabelTTFAlignment
//
//------------------------------------------------------------------
LabelTTFAlignment::LabelTTFAlignment()
{
auto s = Director::getInstance()->getWinSize();
auto ttf0 = LabelTTF::create("Alignment 0\nnew line", "Helvetica", 12,
Size(256, 32), TextHAlignment::LEFT);
2013-08-14 01:12:02 +08:00
ttf0->setPosition(Vector2(s.width/2,(s.height/6)*2));
ttf0->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
this->addChild(ttf0);
2013-08-14 01:12:02 +08:00
auto ttf1 = LabelTTF::create("Alignment 1\nnew line", "Helvetica", 12,
2013-08-14 01:12:02 +08:00
Size(245, 32), TextHAlignment::CENTER);
ttf1->setPosition(Vector2(s.width/2,(s.height/6)*3));
ttf1->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
this->addChild(ttf1);
auto ttf2 = LabelTTF::create("Alignment 2\nnew line", "Helvetica", 12,
Size(245, 32), TextHAlignment::RIGHT);
ttf2->setPosition(Vector2(s.width/2,(s.height/6)*4));
ttf2->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
this->addChild(ttf2);
}
std::string LabelTTFAlignment::title() const
{
return "CCLabelTTF alignment";
}
std::string LabelTTFAlignment::subtitle() const
{
return "Tests alignment values";
}
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// Atlas3
2012-03-22 11:48:37 +08:00
//
// Use any of these editors to generate BMFonts:
// http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
// http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
// http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
// http://www.angelcode.com/products/bmfont/ (Free, Windows only)
2010-09-07 11:47:54 +08:00
//
//------------------------------------------------------------------
Atlas3::Atlas3()
{
_time = 0;
auto col = LayerColor::create( Color4B(128,128,128,255) );
addChild(col, -10);
auto label1 = LabelBMFont::create("Test", "fonts/bitmapFontTest2.fnt");
// testing anchors
label1->setAnchorPoint( Vector2::ANCHOR_BOTTOM_LEFT );
addChild(label1, 0, kTagBitmapAtlas1);
auto fade = FadeOut::create(1.0f);
auto fade_in = fade->reverse();
2014-03-31 17:38:54 +08:00
auto seq = Sequence::create(fade, fade_in, nullptr);
auto repeat = RepeatForever::create(seq);
label1->runAction(repeat);
// VERY IMPORTANT
// color and opacity work OK because bitmapFontAltas2 loads a BMP image (not a PNG image)
// If you want to use both opacity and color, it is recommended to use NON premultiplied images like BMP images
// Of course, you can also tell XCode not to compress PNG images, but I think it doesn't work as expected
auto label2 = LabelBMFont::create("Test", "fonts/bitmapFontTest2.fnt");
// testing anchors
label2->setAnchorPoint( Vector2::ANCHOR_MIDDLE );
label2->setColor( Color3B::RED );
addChild(label2, 0, kTagBitmapAtlas2);
2014-01-24 15:02:42 +08:00
auto tint = Sequence::create(TintTo::create(1, 255, 0, 0),
TintTo::create(1, 0, 255, 0),
TintTo::create(1, 0, 0, 255),
NULL);
label2->runAction( RepeatForever::create(tint) );
auto label3 = LabelBMFont::create("Test", "fonts/bitmapFontTest2.fnt");
// testing anchors
label3->setAnchorPoint( Vector2::ANCHOR_TOP_RIGHT );
addChild(label3, 0, kTagBitmapAtlas3);
2013-01-28 20:34:52 +08:00
label1->setPosition( VisibleRect::leftBottom() );
label2->setPosition( VisibleRect::center() );
label3->setPosition( VisibleRect::rightTop() );
schedule( schedule_selector(Atlas3::step) );//:@selector(step:)];
2010-09-07 11:47:54 +08:00
}
2012-06-08 13:55:28 +08:00
void Atlas3::step(float dt)
2010-09-07 11:47:54 +08:00
{
_time += dt;
//std::string string;
2010-09-07 11:47:54 +08:00
char string[15] = {0};
sprintf(string, "%2.2f Test j", _time);
//string.format("%2.2f Test j", _time);
auto label1 = (LabelBMFont*) getChildByTag(kTagBitmapAtlas1);
label1->setString(string);
auto label2 = (LabelBMFont*) getChildByTag(kTagBitmapAtlas2);
label2->setString(string);
auto label3 = (LabelBMFont*) getChildByTag(kTagBitmapAtlas3);
label3->setString(string);
2010-09-07 11:47:54 +08:00
}
std::string Atlas3::title() const
2010-09-07 11:47:54 +08:00
{
return "CCLabelBMFont";
2012-03-22 11:48:37 +08:00
}
std::string Atlas3::subtitle() const
2010-09-07 11:47:54 +08:00
{
return "Testing alignment. Testing opacity + tint";
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// Atlas4
2012-03-22 11:48:37 +08:00
//
// Use any of these editors to generate BMFonts:
// http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
// http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
// http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
2011-07-08 15:57:46 +08:00
// http://www.angelcode.com/products/bmfont/ (Free, Windows only)
2010-09-07 11:47:54 +08:00
//
//------------------------------------------------------------------
Atlas4::Atlas4()
{
_time = 0;
// Upper Label
auto label = LabelBMFont::create("Bitmap Font Atlas", "fonts/bitmapFontTest.fnt");
addChild(label);
auto s = Director::getInstance()->getWinSize();
label->setPosition( Vector2(s.width/2, s.height/2) );
label->setAnchorPoint( Vector2::ANCHOR_MIDDLE );
auto BChar = (Sprite*) label->getChildByTag(0);
auto FChar = (Sprite*) label->getChildByTag(7);
auto AChar = (Sprite*) label->getChildByTag(12);
auto rotate = RotateBy::create(2, 360);
auto rot_4ever = RepeatForever::create(rotate);
auto scale = ScaleBy::create(2, 1.5f);
auto scale_back = scale->reverse();
auto scale_seq = Sequence::create(scale, scale_back,NULL);
auto scale_4ever = RepeatForever::create(scale_seq);
auto jump = JumpBy::create(0.5f, Vector2::ZERO, 60, 1);
auto jump_4ever = RepeatForever::create(jump);
auto fade_out = FadeOut::create(1);
auto fade_in = FadeIn::create(1);
auto seq = Sequence::create(fade_out, fade_in, NULL);
auto fade_4ever = RepeatForever::create(seq);
BChar->runAction(rot_4ever);
BChar->runAction(scale_4ever);
FChar->runAction(jump_4ever);
AChar->runAction(fade_4ever);
// Bottom Label
auto label2 = LabelBMFont::create("00.0", "fonts/bitmapFontTest.fnt");
addChild(label2, 0, kTagBitmapAtlas2);
label2->setPosition( Vector2(s.width/2.0f, 80) );
auto lastChar = (Sprite*) label2->getChildByTag(3);
lastChar->runAction( rot_4ever->clone() );
schedule( schedule_selector(Atlas4::step), 0.1f);
2010-09-07 11:47:54 +08:00
}
2014-04-08 22:07:35 +08:00
void Atlas4::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
2013-12-25 15:05:49 +08:00
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(Atlas4::onDraw, this, transform, transformUpdated);
renderer->addCommand(&_customCommand);
2013-12-25 15:05:49 +08:00
}
2014-04-10 10:16:16 +08:00
void Atlas4::onDraw(const Matrix &transform, bool transformUpdated)
2010-09-07 11:47:54 +08:00
{
Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
auto s = Director::getInstance()->getWinSize();
DrawPrimitives::drawLine( Vector2(0, s.height/2), Vector2(s.width, s.height/2) );
DrawPrimitives::drawLine( Vector2(s.width/2, 0), Vector2(s.width/2, s.height) );
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2010-09-07 11:47:54 +08:00
}
2012-06-08 13:55:28 +08:00
void Atlas4::step(float dt)
2010-09-07 11:47:54 +08:00
{
_time += dt;
2010-09-07 11:47:54 +08:00
char string[10] = {0};
sprintf(string, "%04.1f", _time);
// std::string string;
// string.format("%04.1f", _time);
auto label1 = (LabelBMFont*) getChildByTag(kTagBitmapAtlas2);
label1->setString(string);
2010-09-07 11:47:54 +08:00
}
std::string Atlas4::title() const
2010-09-07 11:47:54 +08:00
{
return "CCLabelBMFont";
2012-03-22 11:48:37 +08:00
}
std::string Atlas4::subtitle() const
2010-09-07 11:47:54 +08:00
{
return "Using fonts as Sprite objects. Some characters should rotate.";
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// Atlas5
2012-03-22 11:48:37 +08:00
//
// Use any of these editors to generate BMFonts:
// http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
// http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
// http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
2011-07-08 15:57:46 +08:00
// http://www.angelcode.com/products/bmfont/ (Free, Windows only)
2010-09-07 11:47:54 +08:00
//
//------------------------------------------------------------------
Atlas5::Atlas5()
{
auto label = LabelBMFont::create("abcdefg", "fonts/bitmapFontTest4.fnt");
addChild(label);
auto s = Director::getInstance()->getWinSize();
label->setPosition( Vector2(s.width/2, s.height/2) );
label->setAnchorPoint( Vector2::ANCHOR_MIDDLE );
2010-09-07 11:47:54 +08:00
}
std::string Atlas5::title() const
2010-09-07 11:47:54 +08:00
{
return "CCLabelBMFont";
2012-03-22 11:48:37 +08:00
}
std::string Atlas5::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Testing padding";
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// Atlas6
2012-03-22 11:48:37 +08:00
//
// Use any of these editors to generate BMFonts:
// http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
// http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
// http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
2011-07-08 15:57:46 +08:00
// http://www.angelcode.com/products/bmfont/ (Free, Windows only)
2010-09-07 11:47:54 +08:00
//
//------------------------------------------------------------------
Atlas6::Atlas6()
{
auto s = Director::getInstance()->getWinSize();
2010-09-07 11:47:54 +08:00
LabelBMFont* label = NULL;
label = LabelBMFont::create("FaFeFiFoFu", "fonts/bitmapFontTest5.fnt");
addChild(label);
label->setPosition( Vector2(s.width/2, s.height/2+50) );
label->setAnchorPoint( Vector2::ANCHOR_MIDDLE ) ;
label = LabelBMFont::create("fafefifofu", "fonts/bitmapFontTest5.fnt");
addChild(label);
label->setPosition( Vector2(s.width/2, s.height/2) );
label->setAnchorPoint( Vector2::ANCHOR_MIDDLE );
2010-09-07 11:47:54 +08:00
label = LabelBMFont::create("aeiou", "fonts/bitmapFontTest5.fnt");
addChild(label);
label->setPosition( Vector2(s.width/2, s.height/2-50) );
label->setAnchorPoint( Vector2::ANCHOR_MIDDLE );
2010-09-07 11:47:54 +08:00
}
std::string Atlas6::title() const
2010-09-07 11:47:54 +08:00
{
return "CCLabelBMFont";
2012-03-22 11:48:37 +08:00
}
std::string Atlas6::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Rendering should be OK. Testing offset";
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// AtlasBitmapColor
2012-03-22 11:48:37 +08:00
//
// Use any of these editors to generate BMFonts:
// http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
// http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
// http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
2011-07-08 15:57:46 +08:00
// http://www.angelcode.com/products/bmfont/ (Free, Windows only)
2010-09-07 11:47:54 +08:00
//
//------------------------------------------------------------------
2012-03-22 11:48:37 +08:00
AtlasBitmapColor::AtlasBitmapColor()
{
auto s = Director::getInstance()->getWinSize();
2014-03-31 17:38:54 +08:00
LabelBMFont* label = nullptr;
label = LabelBMFont::create("Blue", "fonts/bitmapFontTest5.fnt");
label->setColor( Color3B::BLUE );
addChild(label);
label->setPosition( Vector2(s.width/2, s.height/4) );
label->setAnchorPoint( Vector2::ANCHOR_MIDDLE );
label = LabelBMFont::create("Red", "fonts/bitmapFontTest5.fnt");
addChild(label);
label->setPosition( Vector2(s.width/2, 2*s.height/4) );
label->setAnchorPoint( Vector2::ANCHOR_MIDDLE );
label->setColor( Color3B::RED );
label = LabelBMFont::create("G", "fonts/bitmapFontTest5.fnt");
addChild(label);
label->setPosition( Vector2(s.width/2, 3*s.height/4) );
label->setAnchorPoint( Vector2::ANCHOR_MIDDLE );
label->setColor( Color3B::GREEN );
label->setString("Green");
2012-03-22 11:48:37 +08:00
}
std::string AtlasBitmapColor::title() const
2012-03-22 11:48:37 +08:00
{
return "CCLabelBMFont";
2012-03-22 11:48:37 +08:00
}
std::string AtlasBitmapColor::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Testing color";
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
//------------------------------------------------------------------
//
// AtlasFastBitmap
2012-03-22 11:48:37 +08:00
//
// Use any of these editors to generate BMFonts:
// http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
// http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
// http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
2011-07-08 15:57:46 +08:00
// http://www.angelcode.com/products/bmfont/ (Free, Windows only)
2010-09-07 11:47:54 +08:00
//
//------------------------------------------------------------------
AtlasFastBitmap::AtlasFastBitmap()
{
// Upper Label
for( int i=0 ; i < 100;i ++ )
{
2010-09-07 11:47:54 +08:00
char str[6] = {0};
sprintf(str, "-%d-", i);
auto label = LabelBMFont::create(str, "fonts/bitmapFontTest.fnt");
addChild(label);
auto s = Director::getInstance()->getWinSize();
2010-09-07 11:47:54 +08:00
auto p = Vector2( CCRANDOM_0_1() * s.width, CCRANDOM_0_1() * s.height);
label->setPosition( p );
label->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
}
2010-09-07 11:47:54 +08:00
}
std::string AtlasFastBitmap::title() const
2010-09-07 11:47:54 +08:00
{
return "CCLabelBMFont";
2012-03-22 11:48:37 +08:00
}
2010-09-07 11:47:54 +08:00
std::string AtlasFastBitmap::subtitle() const
2010-09-07 11:47:54 +08:00
{
return "Creating several LabelBMFont with the same .fnt file should be fast";
2012-03-22 11:48:37 +08:00
}
2011-02-23 16:47:25 +08:00
//------------------------------------------------------------------
//
// BitmapFontMultiLine
2012-03-22 11:48:37 +08:00
//
// Use any of these editors to generate BMFonts:
// http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
// http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
// http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
2011-07-08 15:57:46 +08:00
// http://www.angelcode.com/products/bmfont/ (Free, Windows only)
2011-02-23 16:47:25 +08:00
//
2012-03-22 11:48:37 +08:00
//------------------------------------------------------------------
BitmapFontMultiLine::BitmapFontMultiLine()
{
Size s;
2012-03-22 11:48:37 +08:00
// Left
auto label1 = LabelBMFont::create(" Multi line\nLeft", "fonts/bitmapFontTest3.fnt");
label1->setAnchorPoint(Vector2::ANCHOR_BOTTOM_LEFT);
2012-03-22 11:48:37 +08:00
addChild(label1, 0, kTagBitmapAtlas1);
s = label1->getContentSize();
CCLOG("content size: %.2fx%.2f", s.width, s.height);
// Center
auto label2 = LabelBMFont::create("Multi line\nCenter", "fonts/bitmapFontTest3.fnt");
label2->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
2012-03-22 11:48:37 +08:00
addChild(label2, 0, kTagBitmapAtlas2);
s= label2->getContentSize();
CCLOG("content size: %.2fx%.2f", s.width, s.height);
// right
auto label3 = LabelBMFont::create("Multi line\nRight\nThree lines Three", "fonts/bitmapFontTest3.fnt");
label3->setAnchorPoint(Vector2::ANCHOR_TOP_RIGHT);
2012-03-22 11:48:37 +08:00
addChild(label3, 0, kTagBitmapAtlas3);
s = label3->getContentSize();
CCLOG("content size: %.2fx%.2f", s.width, s.height);
label1->setPosition(VisibleRect::leftBottom());
label2->setPosition(VisibleRect::center());
label3->setPosition(VisibleRect::rightTop());
2012-03-22 11:48:37 +08:00
}
std::string BitmapFontMultiLine::title() const
2012-03-22 11:48:37 +08:00
{
return "CCLabelBMFont";
}
std::string BitmapFontMultiLine::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Multiline + anchor point";
}
2011-02-23 16:47:25 +08:00
//------------------------------------------------------------------
//
// LabelsEmpty
//
2012-03-22 11:48:37 +08:00
//------------------------------------------------------------------
LabelsEmpty::LabelsEmpty()
{
auto s = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
// LabelBMFont
auto label1 = LabelBMFont::create("", "fonts/bitmapFontTest3.fnt");
2012-03-22 11:48:37 +08:00
addChild(label1, 0, kTagBitmapAtlas1);
label1->setPosition(Vector2(s.width/2, s.height-100));
2012-03-22 11:48:37 +08:00
// LabelTTF
auto label2 = LabelTTF::create("", "Arial", 24);
2012-03-22 11:48:37 +08:00
addChild(label2, 0, kTagBitmapAtlas2);
label2->setPosition(Vector2(s.width/2, s.height/2));
2012-03-22 11:48:37 +08:00
// LabelAtlas
auto label3 = LabelAtlas::create("", "fonts/tuffy_bold_italic-charmap.png", 48, 64, ' ');
2012-03-22 11:48:37 +08:00
addChild(label3, 0, kTagBitmapAtlas3);
label3->setPosition(Vector2(s.width/2, 0+100));
2012-03-22 11:48:37 +08:00
schedule(schedule_selector(LabelsEmpty::updateStrings), 1.0f);
setEmpty = false;
}
2012-06-08 13:55:28 +08:00
void LabelsEmpty::updateStrings(float dt)
2012-03-22 11:48:37 +08:00
{
auto label1 = static_cast<LabelBMFont*>( getChildByTag(kTagBitmapAtlas1) );
auto label2 = static_cast<LabelTTF*>( getChildByTag(kTagBitmapAtlas2) );
auto label3 = static_cast<LabelAtlas*>( getChildByTag(kTagBitmapAtlas3) );
2012-03-22 11:48:37 +08:00
if( ! setEmpty )
{
label1->setString("not empty");
label2->setString("not empty");
label3->setString("hi");
setEmpty = true;
}
else
{
label1->setString("");
label2->setString("");
label3->setString("");
setEmpty = false;
}
}
std::string LabelsEmpty::title() const
2012-03-22 11:48:37 +08:00
{
return "Testing empty labels";
}
std::string LabelsEmpty::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "3 empty labels: LabelAtlas, LabelTTF and LabelBMFont";
}
2011-02-23 16:47:25 +08:00
//------------------------------------------------------------------
//
// LabelBMFontHD
//
2012-03-22 11:48:37 +08:00
//------------------------------------------------------------------
LabelBMFontHD::LabelBMFontHD()
{
auto s = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
// LabelBMFont
auto label1 = LabelBMFont::create("TESTING RETINA DISPLAY", "fonts/konqa32.fnt");
2012-03-22 11:48:37 +08:00
addChild(label1);
label1->setPosition(Vector2(s.width/2, s.height/2));
2012-03-22 11:48:37 +08:00
}
std::string LabelBMFontHD::title() const
2012-03-22 11:48:37 +08:00
{
return "Testing Retina Display BMFont";
}
std::string LabelBMFontHD::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "loading arista16 or arista16-hd";
}
2011-02-23 16:47:25 +08:00
//------------------------------------------------------------------
//
// LabelAtlasHD
//
2012-03-22 11:48:37 +08:00
//------------------------------------------------------------------
LabelAtlasHD::LabelAtlasHD()
{
auto s = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
// LabelBMFont
auto label1 = LabelAtlas::create("TESTING RETINA DISPLAY", "fonts/larabie-16.plist");
label1->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
2012-03-22 11:48:37 +08:00
addChild(label1);
label1->setPosition(Vector2(s.width/2, s.height/2));
2012-03-22 11:48:37 +08:00
}
std::string LabelAtlasHD::title() const
2012-03-22 11:48:37 +08:00
{
return "LabelAtlas with Retina Display";
}
std::string LabelAtlasHD::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "loading larabie-16 / larabie-16-hd";
}
2011-02-23 16:47:25 +08:00
//------------------------------------------------------------------
//
// LabelGlyphDesigner
//
2012-03-22 11:48:37 +08:00
//------------------------------------------------------------------
LabelGlyphDesigner::LabelGlyphDesigner()
{
auto s = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
auto layer = LayerColor::create(Color4B(128,128,128,255));
2012-03-22 11:48:37 +08:00
addChild(layer, -10);
// LabelBMFont
auto label1 = LabelBMFont::create("Testing Glyph Designer", "fonts/futura-48.fnt");
2012-03-22 11:48:37 +08:00
addChild(label1);
label1->setPosition(Vector2(s.width/2, s.height/2));
2012-03-22 11:48:37 +08:00
}
std::string LabelGlyphDesigner::title() const
2012-03-22 11:48:37 +08:00
{
return "Testing Glyph Designer";
}
std::string LabelGlyphDesigner::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "You should see a font with shawdows and outline";
}
void AtlasTestScene::runThisTest()
{
2013-02-28 15:57:15 +08:00
sceneIdx = -1;
auto layer = nextAtlasAction();
addChild(layer);
2012-03-22 11:48:37 +08:00
Director::getInstance()->replaceScene(this);
2012-03-22 11:48:37 +08:00
}
2011-07-06 14:56:05 +08:00
//------------------------------------------------------------------
//
// LabelTTFTest
//
2012-03-22 11:48:37 +08:00
//------------------------------------------------------------------
LabelTTFTest::LabelTTFTest()
{
auto blockSize = Size(200, 160);
auto s = Director::getInstance()->getWinSize();
auto colorLayer = LayerColor::create(Color4B(100, 100, 100, 255), blockSize.width, blockSize.height);
colorLayer->setAnchorPoint(Vector2::ANCHOR_BOTTOM_LEFT);
colorLayer->setPosition(Vector2((s.width - blockSize.width) / 2, (s.height - blockSize.height) / 2));
this->addChild(colorLayer);
MenuItemFont::setFontSize(30);
auto menu = Menu::create(
MenuItemFont::create("Left", CC_CALLBACK_1(LabelTTFTest::setAlignmentLeft, this)),
MenuItemFont::create("Center", CC_CALLBACK_1(LabelTTFTest::setAlignmentCenter, this)),
MenuItemFont::create("Right", CC_CALLBACK_1(LabelTTFTest::setAlignmentRight, this)),
NULL);
menu->alignItemsVerticallyWithPadding(4);
menu->setPosition(Vector2(50, s.height / 2 - 20));
this->addChild(menu);
menu = Menu::create(
MenuItemFont::create("Top", CC_CALLBACK_1(LabelTTFTest::setAlignmentTop, this)),
MenuItemFont::create("Middle", CC_CALLBACK_1(LabelTTFTest::setAlignmentMiddle, this)),
MenuItemFont::create("Bottom", CC_CALLBACK_1(LabelTTFTest::setAlignmentBottom, this)),
NULL);
menu->alignItemsVerticallyWithPadding(4);
menu->setPosition(Vector2(s.width - 50, s.height / 2 - 20));
this->addChild(menu);
2014-03-31 17:21:56 +08:00
_label = nullptr;
_horizAlign = TextHAlignment::LEFT;
_vertAlign = TextVAlignment::TOP;
this->updateAlignment();
}
LabelTTFTest::~LabelTTFTest()
{
2014-03-31 17:21:56 +08:00
CC_SAFE_RELEASE(_label);
}
void LabelTTFTest::updateAlignment()
{
auto blockSize = Size(200, 160);
auto s = Director::getInstance()->getWinSize();
2014-03-31 17:21:56 +08:00
if (_label)
{
2014-03-31 17:21:56 +08:00
_label->removeFromParentAndCleanup(true);
}
2013-03-21 09:00:55 +08:00
2014-03-31 17:21:56 +08:00
CC_SAFE_RELEASE(_label);
2013-03-21 09:00:55 +08:00
_label = LabelTTF::create(this->getCurrentAlignment(), "Marker Felt", 32,
blockSize, _horizAlign, _vertAlign);
2014-03-31 17:21:56 +08:00
_label->retain();
_label->setAnchorPoint(Vector2::ANCHOR_BOTTOM_LEFT);
_label->setPosition(Vector2((s.width - blockSize.width) / 2, (s.height - blockSize.height)/2 ));
2014-03-31 17:21:56 +08:00
this->addChild(_label);
}
void LabelTTFTest::setAlignmentLeft(Ref* sender)
{
_horizAlign = TextHAlignment::LEFT;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentCenter(Ref* sender)
{
_horizAlign = TextHAlignment::CENTER;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentRight(Ref* sender)
{
_horizAlign = TextHAlignment::RIGHT;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentTop(Ref* sender)
{
_vertAlign = TextVAlignment::TOP;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentMiddle(Ref* sender)
{
_vertAlign = TextVAlignment::CENTER;
this->updateAlignment();
}
void LabelTTFTest::setAlignmentBottom(Ref* sender)
{
_vertAlign = TextVAlignment::BOTTOM;
this->updateAlignment();
}
const char* LabelTTFTest::getCurrentAlignment()
{
const char* vertical = NULL;
const char* horizontal = NULL;
switch (_vertAlign) {
case TextVAlignment::TOP:
vertical = "Top";
break;
case TextVAlignment::CENTER:
vertical = "Middle";
break;
case TextVAlignment::BOTTOM:
vertical = "Bottom";
break;
}
switch (_horizAlign) {
case TextHAlignment::LEFT:
horizontal = "Left";
break;
case TextHAlignment::CENTER:
horizontal = "Center";
break;
case TextHAlignment::RIGHT:
horizontal = "Right";
break;
}
return String::createWithFormat("Alignment %s %s", vertical, horizontal)->getCString();
2012-03-22 11:48:37 +08:00
}
std::string LabelTTFTest::title() const
2012-03-22 11:48:37 +08:00
{
return "Testing LabelTTF";
2012-03-22 11:48:37 +08:00
}
std::string LabelTTFTest::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Select the buttons on the sides to change alignment";
2012-03-22 11:48:37 +08:00
}
LabelTTFMultiline::LabelTTFMultiline()
{
auto s = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
auto center = LabelTTF::create("word wrap \"testing\" (bla0) bla1 'bla2' [bla3] (bla4) {bla5} {bla6} [bla7] (bla8) [bla9] 'bla0' \"bla1\"",
"Paint Boy",
32,
Size(s.width/2,200),
TextHAlignment::CENTER,
TextVAlignment::TOP);
center->setPosition(Vector2(s.width / 2, 150));
2012-03-22 11:48:37 +08:00
addChild(center);
2012-03-22 11:48:37 +08:00
}
std::string LabelTTFMultiline::title() const
2012-03-22 11:48:37 +08:00
{
return "Testing LabelTTF Word Wrap";
2012-03-22 11:48:37 +08:00
}
std::string LabelTTFMultiline::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Word wrap using LabelTTF and a custom TTF font";
2012-03-22 11:48:37 +08:00
}
LabelTTFChinese::LabelTTFChinese()
{
auto size = Director::getInstance()->getWinSize();
auto label = LabelTTF::create("中国", "Marker Felt", 30);
label->setPosition(Vector2(size.width / 2, size.height /2));
this->addChild(label);
2012-03-22 11:48:37 +08:00
}
std::string LabelTTFChinese::title() const
2012-03-22 11:48:37 +08:00
{
return "Testing LabelTTF with Chinese character";
2012-03-22 11:48:37 +08:00
}
LabelBMFontChinese::LabelBMFontChinese()
{
auto size = Director::getInstance()->getWinSize();
auto label = LabelBMFont::create("中国", "fonts/bitmapFontChinese.fnt");
label->setPosition(Vector2(size.width / 2, size.height /2));
this->addChild(label);
}
std::string LabelBMFontChinese::title() const
2012-03-22 11:48:37 +08:00
{
return "Testing LabelBMFont with Chinese character";
2012-03-22 11:48:37 +08:00
}
/// BitmapFontMultiLineAlignment
#define LongSentencesExample "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
#define LineBreaksExample "Lorem ipsum dolor\nsit amet\nconsectetur adipisicing elit\nblah\nblah"
#define MixedExample "ABC\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt\nDEF"
#define ArrowsMax 0.95
#define ArrowsMin 0.7
#define LeftAlign 0
#define CenterAlign 1
#define RightAlign 2
#define LongSentences 0
#define LineBreaks 1
#define Mixed 2
static float alignmentItemPadding = 50;
static float menuItemPaddingCenter = 50;
BitmapFontMultiLineAlignment::BitmapFontMultiLineAlignment()
{
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(BitmapFontMultiLineAlignment::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(BitmapFontMultiLineAlignment::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(BitmapFontMultiLineAlignment::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// ask director the the window size
auto size = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
// create and initialize a Label
2014-03-31 17:21:56 +08:00
_labelShouldRetain = LabelBMFont::create(LongSentencesExample, "fonts/markerFelt.fnt", size.width/1.5, TextHAlignment::CENTER);
_labelShouldRetain->retain();
2012-03-22 11:48:37 +08:00
2014-03-31 17:21:56 +08:00
_arrowsBarShouldRetain = Sprite::create("Images/arrowsBar.png");
_arrowsBarShouldRetain->retain();
_arrowsShouldRetain = Sprite::create("Images/arrows.png");
_arrowsShouldRetain->retain();
2012-03-22 11:48:37 +08:00
MenuItemFont::setFontSize(20);
auto longSentences = MenuItemFont::create("Long Flowing Sentences", CC_CALLBACK_1(BitmapFontMultiLineAlignment::stringChanged, this));
auto lineBreaks = MenuItemFont::create("Short Sentences With Intentional Line Breaks", CC_CALLBACK_1(BitmapFontMultiLineAlignment::stringChanged, this));
auto mixed = MenuItemFont::create("Long Sentences Mixed With Intentional Line Breaks", CC_CALLBACK_1(BitmapFontMultiLineAlignment::stringChanged, this));
auto stringMenu = Menu::create(longSentences, lineBreaks, mixed, NULL);
stringMenu->alignItemsVertically();
2012-03-22 11:48:37 +08:00
longSentences->setColor(Color3B::RED);
_lastSentenceItem = longSentences;
longSentences->setTag(LongSentences);
lineBreaks->setTag(LineBreaks);
mixed->setTag(Mixed);
2012-03-22 11:48:37 +08:00
MenuItemFont::setFontSize(30);
2012-03-22 11:48:37 +08:00
auto left = MenuItemFont::create("Left", CC_CALLBACK_1(BitmapFontMultiLineAlignment::alignmentChanged, this));
auto center = MenuItemFont::create("Center", CC_CALLBACK_1(BitmapFontMultiLineAlignment::alignmentChanged, this));
auto right = MenuItemFont::create("Right", CC_CALLBACK_1(BitmapFontMultiLineAlignment::alignmentChanged, this));
auto alignmentMenu = Menu::create(left, center, right, NULL);
alignmentMenu->alignItemsHorizontallyWithPadding(alignmentItemPadding);
2012-03-22 11:48:37 +08:00
center->setColor(Color3B::RED);
_lastAlignmentItem = center;
left->setTag(LeftAlign);
center->setTag(CenterAlign);
right->setTag(RightAlign);
2012-03-22 11:48:37 +08:00
// position the label on the center of the screen
_labelShouldRetain->setPosition(Vector2(size.width/2, size.height/2));
2012-03-22 11:48:37 +08:00
2014-03-31 17:21:56 +08:00
_arrowsBarShouldRetain->setVisible(false);
2012-03-22 11:48:37 +08:00
float arrowsWidth = (ArrowsMax - ArrowsMin) * size.width;
2014-03-31 17:21:56 +08:00
_arrowsBarShouldRetain->setScaleX(arrowsWidth / this->_arrowsBarShouldRetain->getContentSize().width);
_arrowsBarShouldRetain->setPosition(Vector2(((ArrowsMax + ArrowsMin) / 2) * size.width, this->_labelShouldRetain->getPosition().y));
2012-03-22 11:48:37 +08:00
this->snapArrowsToEdge();
2012-03-22 11:48:37 +08:00
stringMenu->setPosition(Vector2(size.width/2, size.height - menuItemPaddingCenter));
alignmentMenu->setPosition(Vector2(size.width/2, menuItemPaddingCenter+15));
2012-03-22 11:48:37 +08:00
2014-03-31 17:21:56 +08:00
addChild(_labelShouldRetain);
addChild(_arrowsBarShouldRetain);
addChild(_arrowsShouldRetain);
addChild(stringMenu);
addChild(alignmentMenu);
2012-03-22 11:48:37 +08:00
}
BitmapFontMultiLineAlignment::~BitmapFontMultiLineAlignment()
{
this->_labelShouldRetain->release();
this->_arrowsBarShouldRetain->release();
this->_arrowsShouldRetain->release();
2012-03-22 11:48:37 +08:00
}
std::string BitmapFontMultiLineAlignment::title() const
2012-03-22 11:48:37 +08:00
{
return "";
2012-03-22 11:48:37 +08:00
}
std::string BitmapFontMultiLineAlignment::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "";
2012-03-22 11:48:37 +08:00
}
void BitmapFontMultiLineAlignment::stringChanged(cocos2d::Ref *sender)
2012-03-22 11:48:37 +08:00
{
auto item = (MenuItemFont*)sender;
item->setColor(Color3B::RED);
this->_lastAlignmentItem->setColor(Color3B::WHITE);
this->_lastAlignmentItem = item;
2012-03-22 11:48:37 +08:00
switch(item->getTag())
{
case LongSentences:
2014-03-31 17:31:04 +08:00
_labelShouldRetain->setString(LongSentencesExample);
break;
case LineBreaks:
2014-03-31 17:31:04 +08:00
_labelShouldRetain->setString(LineBreaksExample);
break;
case Mixed:
2014-03-31 17:31:04 +08:00
_labelShouldRetain->setString(MixedExample);
break;
default:
break;
}
2012-03-22 11:48:37 +08:00
this->snapArrowsToEdge();
2012-03-22 11:48:37 +08:00
}
void BitmapFontMultiLineAlignment::alignmentChanged(cocos2d::Ref *sender)
2012-03-22 11:48:37 +08:00
{
auto item = static_cast<MenuItemFont*>(sender);
item->setColor(Color3B::RED);
this->_lastAlignmentItem->setColor(Color3B::WHITE);
this->_lastAlignmentItem = item;
2012-03-22 11:48:37 +08:00
switch(item->getTag())
{
case LeftAlign:
2014-03-31 17:31:04 +08:00
_labelShouldRetain->setAlignment(TextHAlignment::LEFT);
break;
case CenterAlign:
2014-03-31 17:31:04 +08:00
_labelShouldRetain->setAlignment(TextHAlignment::CENTER);
break;
case RightAlign:
2014-03-31 17:31:04 +08:00
_labelShouldRetain->setAlignment(TextHAlignment::RIGHT);
break;
default:
break;
}
2012-03-22 11:48:37 +08:00
this->snapArrowsToEdge();
2012-03-22 11:48:37 +08:00
}
void BitmapFontMultiLineAlignment::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event *event)
2012-03-22 11:48:37 +08:00
{
auto touch = touches[0];
auto location = touch->getLocationInView();
2012-03-22 11:48:37 +08:00
if (this->_arrowsShouldRetain->getBoundingBox().containsPoint(location))
{
_drag = true;
this->_arrowsBarShouldRetain->setVisible(true);
}
2012-03-22 11:48:37 +08:00
}
void BitmapFontMultiLineAlignment::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event)
2012-03-22 11:48:37 +08:00
{
_drag = false;
this->snapArrowsToEdge();
2012-03-22 11:48:37 +08:00
this->_arrowsBarShouldRetain->setVisible(false);
2012-03-22 11:48:37 +08:00
}
void BitmapFontMultiLineAlignment::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
2012-03-22 11:48:37 +08:00
{
if (! _drag)
{
return;
}
2012-03-22 11:48:37 +08:00
auto touch = touches[0];
auto location = touch->getLocationInView();
2012-03-22 11:48:37 +08:00
auto winSize = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
this->_arrowsShouldRetain->setPosition(Vector2(MAX(MIN(location.x, ArrowsMax*winSize.width), ArrowsMin*winSize.width),
this->_arrowsShouldRetain->getPosition().y));
2012-03-22 11:48:37 +08:00
float labelWidth = fabs(this->_arrowsShouldRetain->getPosition().x - this->_labelShouldRetain->getPosition().x) * 2;
2012-03-22 11:48:37 +08:00
2014-03-31 17:38:54 +08:00
_labelShouldRetain->setWidth(labelWidth);
2012-03-22 11:48:37 +08:00
}
void BitmapFontMultiLineAlignment::snapArrowsToEdge()
{
this->_arrowsShouldRetain->setPosition(Vector2(this->_labelShouldRetain->getPosition().x + this->_labelShouldRetain->getContentSize().width/2,
this->_labelShouldRetain->getPosition().y));
2012-03-22 11:48:37 +08:00
}
/// LabelTTFA8Test
LabelTTFA8Test::LabelTTFA8Test()
{
auto s = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
auto layer = LayerColor::create(Color4B(128, 128, 128, 255));
addChild(layer, -10);
2012-03-22 11:48:37 +08:00
// LabelBMFont
auto label1 = LabelTTF::create("Testing A8 Format", "Marker Felt", 48);
addChild(label1);
label1->setColor(Color3B::RED);
label1->setPosition(Vector2(s.width/2, s.height/2));
2012-03-22 11:48:37 +08:00
auto fadeOut = FadeOut::create(2);
auto fadeIn = FadeIn::create(2);
auto seq = Sequence::create(fadeOut, fadeIn, NULL);
auto forever = RepeatForever::create(seq);
label1->runAction(forever);
2012-03-22 11:48:37 +08:00
}
std::string LabelTTFA8Test::title() const
2012-03-22 11:48:37 +08:00
{
return "Testing A8 Format";
2012-03-22 11:48:37 +08:00
}
std::string LabelTTFA8Test::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "RED label, fading In and Out in the center of the screen";
2012-03-22 11:48:37 +08:00
}
/// BMFontOneAtlas
BMFontOneAtlas::BMFontOneAtlas()
{
auto s = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
auto label1 = LabelBMFont::create("This is Helvetica", "fonts/helvetica-32.fnt");
2012-03-22 11:48:37 +08:00
addChild(label1);
label1->setPosition(Vector2(s.width/2, s.height/3*2));
2012-03-22 11:48:37 +08:00
auto label2 = LabelBMFont::create("And this is Geneva", "fonts/geneva-32.fnt", 0, TextHAlignment::LEFT, Vector2(0, 128));
addChild(label2);
label2->setPosition(Vector2(s.width/2, s.height/3*1));
2012-03-22 11:48:37 +08:00
}
std::string BMFontOneAtlas::title() const
2012-03-22 11:48:37 +08:00
{
return "CCLabelBMFont with one texture";
2012-03-22 11:48:37 +08:00
}
std::string BMFontOneAtlas::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "Using 2 .fnt definitions that share the same texture atlas.";
2012-03-22 11:48:37 +08:00
}
/// BMFontUnicode
BMFontUnicode::BMFontUnicode()
{
2013-12-18 16:03:07 +08:00
auto strings = FileUtils::getInstance()->getValueMapFromFile("fonts/strings.xml");
std::string chinese = strings["chinese1"].asString();
std::string russian = strings["russian"].asString();
std::string spanish = strings["spanish"].asString();
std::string japanese = strings["japanese"].asString();
2012-03-22 11:48:37 +08:00
auto s = Director::getInstance()->getWinSize();
2012-03-22 11:48:37 +08:00
auto label1 = LabelBMFont::create(spanish, "fonts/arial-unicode-26.fnt", 200, TextHAlignment::LEFT);
2012-03-22 11:48:37 +08:00
addChild(label1);
label1->setPosition(Vector2(s.width/2, s.height/5*4));
2012-03-22 11:48:37 +08:00
auto label2 = LabelBMFont::create(chinese, "fonts/arial-unicode-26.fnt");
addChild(label2);
label2->setPosition(Vector2(s.width/2, s.height/5*3));
2012-03-22 11:48:37 +08:00
auto label3 = LabelBMFont::create(russian, "fonts/arial-26-en-ru.fnt");
addChild(label3);
label3->setPosition(Vector2(s.width/2, s.height/5*2));
auto label4 = LabelBMFont::create(japanese, "fonts/arial-unicode-26.fnt");
addChild(label4);
label4->setPosition(Vector2(s.width/2, s.height/5*1));
2012-03-22 11:48:37 +08:00
}
std::string BMFontUnicode::title() const
2012-03-22 11:48:37 +08:00
{
return "CCLabelBMFont with Unicode support";
2012-03-22 11:48:37 +08:00
}
std::string BMFontUnicode::subtitle() const
2012-03-22 11:48:37 +08:00
{
return "You should see 4 differnt labels:\nIn Spanish, Chinese, Russian and Korean";
}
// BMFontInit
BMFontInit::BMFontInit()
{
auto s = Director::getInstance()->getWinSize();
auto bmFont = LabelBMFont::create();
bmFont->setFntFile("fonts/helvetica-32.fnt");
bmFont->setString("It is working!");
this->addChild(bmFont);
bmFont->setPosition(Vector2(s.width/2,s.height/4*2));
}
std::string BMFontInit::title() const
{
return "LabelBMFont create()";
}
std::string BMFontInit::subtitle() const
{
return "Testing LabelBMFont::create() wihtout params";
}
// TTFFontInit
TTFFontInit::TTFFontInit()
{
auto s = Director::getInstance()->getWinSize();
auto font = LabelTTF::create();
font->setFontName("Marker Felt");
font->setFontSize(48);
font->setString("It is working!");
this->addChild(font);
font->setPosition(Vector2(s.width/2,s.height/4*2));
}
std::string TTFFontInit::title() const
{
return "LabelTTF create()";
}
std::string TTFFontInit::subtitle() const
{
return "Testing LabelTTF::create() wihtout params";
}
2013-04-26 09:22:26 +08:00
TTFFontShadowAndStroke::TTFFontShadowAndStroke()
{
auto layer = LayerColor::create(Color4B(0,190,0,255));
2013-04-26 09:22:26 +08:00
addChild(layer, -10);
auto s = Director::getInstance()->getWinSize();
Color3B tintColorRed( 255, 0, 0 );
Color3B tintColorYellow( 255, 255, 0 );
Color3B tintColorBlue( 0, 0, 255 );
Color3B strokeColor( 0, 0, 255 );
Color3B strokeShadowColor( 255, 0, 0 );
2013-04-26 09:22:26 +08:00
Size shadowOffset(12.0, 12.0);
FontDefinition shadowTextDef;
shadowTextDef._fontSize = 20;
shadowTextDef._fontName = std::string("Marker Felt");
shadowTextDef._shadow._shadowEnabled = true;
shadowTextDef._shadow._shadowOffset = shadowOffset;
shadowTextDef._shadow._shadowOpacity = 1.0;
shadowTextDef._shadow._shadowBlur = 1.0;
shadowTextDef._fontFillColor = tintColorRed;
// shadow only label
auto fontShadow = LabelTTF::createWithFontDefinition("Shadow Only Red Text", shadowTextDef);
// add label to the scene
2013-04-26 09:22:26 +08:00
this->addChild(fontShadow);
fontShadow->setPosition(Vector2(s.width/2,s.height/4*2.5));
// create the stroke only label
FontDefinition strokeTextDef;
strokeTextDef._fontSize = 20;
strokeTextDef._fontName = std::string("Marker Felt");
strokeTextDef._stroke._strokeEnabled = true;
strokeTextDef._stroke._strokeColor = strokeColor;
strokeTextDef._stroke._strokeSize = 1.5;
strokeTextDef._fontFillColor = tintColorYellow;
// stroke only label
auto fontStroke = LabelTTF::createWithFontDefinition("Stroke Only Yellow Text", strokeTextDef);
// add label to the scene
2013-04-26 09:22:26 +08:00
this->addChild(fontStroke);
fontStroke->setPosition(Vector2(s.width/2,s.height/4*1.8));
2013-04-26 09:22:26 +08:00
// create the label stroke and shadow
FontDefinition strokeShaodwTextDef;
strokeShaodwTextDef._fontSize = 20;
strokeShaodwTextDef._fontName = std::string("Marker Felt");
strokeShaodwTextDef._stroke._strokeEnabled = true;
strokeShaodwTextDef._stroke._strokeColor = strokeShadowColor;
strokeShaodwTextDef._stroke._strokeSize = 1.5;
strokeShaodwTextDef._shadow._shadowEnabled = true;
strokeShaodwTextDef._shadow._shadowOffset = shadowOffset;
strokeShaodwTextDef._shadow._shadowOpacity = 1.0;
strokeShaodwTextDef._shadow._shadowBlur = 1.0;
2013-05-09 01:22:04 +08:00
strokeShaodwTextDef._fontFillColor = tintColorBlue;
// shadow + stroke label
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
auto fontStrokeAndShadow = LabelTTF::createWithFontDefinition("Stroke && Shadow Blue Text", strokeShaodwTextDef);
#else
auto fontStrokeAndShadow = LabelTTF::createWithFontDefinition("Stroke &Shadow Blue Text", strokeShaodwTextDef);
#endif
// add label to the scene
2013-04-26 09:22:26 +08:00
this->addChild(fontStrokeAndShadow);
fontStrokeAndShadow->setPosition(Vector2(s.width/2,s.height/4*1.1));
2014-03-24 17:39:23 +08:00
auto buttonBG = MenuItemImage::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
buttonBG->setAnchorPoint(Vector2::ANCHOR_MIDDLE_LEFT);
2014-03-24 17:39:23 +08:00
buttonBG->setPosition(VisibleRect::left());
// create the label stroke and shadow
strokeShaodwTextDef._fontSize = 18;
strokeShaodwTextDef._fontName = "Marker Felt";
strokeShaodwTextDef._stroke._strokeEnabled = true;
strokeShaodwTextDef._stroke._strokeColor = Color3B::BLACK;
strokeShaodwTextDef._stroke._strokeSize = 3.0f;
strokeShaodwTextDef._shadow._shadowEnabled = false;
strokeShaodwTextDef._shadow._shadowOffset = Size(1, 1);
strokeShaodwTextDef._shadow._shadowOpacity = 1.0;
strokeShaodwTextDef._shadow._shadowBlur = 0.5f;
strokeShaodwTextDef._fontFillColor = Color3B::WHITE;
// shadow + stroke label
fontStrokeAndShadow = LabelTTF::createWithFontDefinition("Test", strokeShaodwTextDef);
// add label to the scene
buttonBG->addChild(fontStrokeAndShadow);
fontStrokeAndShadow->setPosition(Vector2(buttonBG->getContentSize().width/2, buttonBG->getContentSize().height/2));
2014-03-24 17:39:23 +08:00
auto menu = Menu::create(buttonBG, nullptr);
menu->setAnchorPoint(Vector2::ANCHOR_BOTTOM_LEFT);
menu->setPosition(Vector2::ZERO);
2014-03-24 17:39:23 +08:00
addChild(menu);
2013-04-26 09:22:26 +08:00
}
std::string TTFFontShadowAndStroke::title() const
2013-04-26 09:22:26 +08:00
{
return "CCLabelTTF shadows + stroke";
}
std::string TTFFontShadowAndStroke::subtitle() const
2013-04-26 09:22:26 +08:00
{
return "Test for support of TTF label with stroke and shadow";
}
// Issue1343
Issue1343::Issue1343()
{
auto s = Director::getInstance()->getWinSize();
auto bmFont = LabelBMFont::create();
bmFont->setFntFile("fonts/font-issue1343.fnt");
bmFont->setString("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.,'");
this->addChild(bmFont);
bmFont->setScale(0.3f);
bmFont->setPosition(Vector2(s.width/2,s.height/4*2));
}
std::string Issue1343::title() const
{
return "Issue 1343";
}
std::string Issue1343::subtitle() const
{
return "You should see: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.,'";
2012-03-22 11:48:37 +08:00
}
2012-11-20 18:17:47 +08:00
LabelBMFontBounds::LabelBMFontBounds()
{
auto s = Director::getInstance()->getWinSize();
2012-11-20 18:17:47 +08:00
auto layer = LayerColor::create(Color4B(128,128,128,255));
2012-11-20 18:17:47 +08:00
addChild(layer, -10);
// LabelBMFont
label1 = LabelBMFont::create("Testing Glyph Designer", "fonts/boundsTestFont.fnt");
2012-11-20 18:17:47 +08:00
addChild(label1);
label1->setPosition(Vector2(s.width/2, s.height/2));
2012-11-20 18:17:47 +08:00
}
std::string LabelBMFontBounds::title() const
2012-11-20 18:17:47 +08:00
{
return "Testing LabelBMFont Bounds";
}
std::string LabelBMFontBounds::subtitle() const
2012-11-20 18:17:47 +08:00
{
return "You should see string enclosed by a box";
}
2014-04-08 22:07:35 +08:00
void LabelBMFontBounds::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
2013-12-25 15:13:11 +08:00
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(LabelBMFontBounds::onDraw, this, transform, transformUpdated);
renderer->addCommand(&_customCommand);
2013-12-25 15:13:11 +08:00
}
2014-04-10 10:16:16 +08:00
void LabelBMFontBounds::onDraw(const Matrix &transform, bool transformUpdated)
2012-11-20 18:17:47 +08:00
{
Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
auto labelSize = label1->getContentSize();
auto origin = Director::getInstance()->getWinSize();
2012-11-20 18:17:47 +08:00
origin.width = origin.width / 2 - (labelSize.width / 2);
origin.height = origin.height / 2 - (labelSize.height / 2);
2014-04-15 17:29:23 +08:00
Vector2 vertices[4]=
2012-11-20 18:17:47 +08:00
{
2014-04-15 17:29:23 +08:00
Vector2(origin.width, origin.height),
Vector2(labelSize.width + origin.width, origin.height),
Vector2(labelSize.width + origin.width, labelSize.height + origin.height),
Vector2(origin.width, labelSize.height + origin.height)
2012-11-20 18:17:47 +08:00
};
DrawPrimitives::drawPoly(vertices, 4, true);
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2012-11-20 18:17:47 +08:00
}
// LabelBMFontCrashTest
void LabelBMFontCrashTest::onEnter()
{
AtlasDemo::onEnter();
auto winSize = Director::getInstance()->getWinSize();
//Create a label and add it
auto label1 = new LabelBMFont();
label1->initWithString("test", "fonts/bitmapFontTest2.fnt");
this->addChild(label1);
// Visit will call draw where the function "ccGLBindVAO(m_uVAOname);" will be invoked.
label1->visit();
// Remove this label
label1->removeFromParentAndCleanup(true);
label1->release();
// Create a new label and add it (then crashes)
auto label2 = LabelBMFont::create("test 2", "fonts/bitmapFontTest.fnt");
label2->setPosition(Vector2(winSize.width/2, winSize.height/2));
this->addChild(label2);
}
std::string LabelBMFontCrashTest::title() const
{
return "LabelBMFont Crash Test";
}
std::string LabelBMFontCrashTest::subtitle() const
{
return "Should not crash.";
}
// LabelBMFontBinaryFormat
LabelBMFontBinaryFormat::LabelBMFontBinaryFormat()
{
auto s = Director::getInstance()->getWinSize();
auto bmFont = LabelBMFont::create();
bmFont->setFntFile("fonts/Roboto.bmf.fnt");
bmFont->setString("It is working!");
this->addChild(bmFont);
bmFont->setPosition(Vector2(s.width/2,s.height/4*2));
}
std::string LabelBMFontBinaryFormat::title() const
{
return "LabelBMFont Binary FNT File";
}
std::string LabelBMFontBinaryFormat::subtitle() const
{
return "This label uses font file in AngelCode binary format";
}
2014-03-31 17:21:56 +08:00
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
#elif _MSC_VER >= 1400 //vs 2005 or higher
#pragma warning (pop)
#endif