Merge remote-tracking branch 'ricorigin/developNewLabel_create' into developNewLabelMerge

Conflicts:
	cocos2dx/label_nodes/CCFontDefinition.cpp
	cocos2dx/label_nodes/CCFontDefinition.h
	cocos2dx/label_nodes/CCStringTTF.cpp
	cocos2dx/label_nodes/CCStringTTF.h
	samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp
This commit is contained in:
carlo morgantini 2013-07-19 12:24:25 -07:00
commit c977e6c3fa
6 changed files with 105 additions and 28 deletions

View File

@ -32,6 +32,27 @@ FontDefinitionTTF::FontDefinitionTTF():_textImages(0), _commonLineHeight(0)
{
}
FontDefinitionTTF* FontDefinitionTTF::create(const char *fontName, int fontSize, const char *letters, int textureSize )
{
FontDefinitionTTF *ret = new FontDefinitionTTF;
if(!ret)
return 0;
if ( ret->initDefinition( fontName, fontSize, letters, textureSize ) )
{
ret->autorelease();
return ret;
}
else
{
delete ret;
return 0;
}
}
FontDefinitionTTF::~FontDefinitionTTF()
{
if (_textImages)
@ -117,17 +138,18 @@ bool FontDefinitionTTF::prepareLetterDefinitions(TextFontPagesDef *pageDefs)
// store the common line height
_commonLineHeight = maxLineHeight;
//
return true;
}
bool FontDefinitionTTF::createFontDefinition(char *fontName, int fontSize, char *letters, int textureSize, bool debugOutput)
bool FontDefinitionTTF::initDefinition(const char *fontName, int fontSize, const char *letters, int textureSize)
{
// preare texture/image stuff
_textImages = new TextImage();
if (!_textImages)
return false;
if (!_textImages->initWithString(letters, textureSize, textureSize, fontName, fontSize, !debugOutput))
if (!_textImages->initWithString(letters, textureSize, textureSize, fontName, fontSize, true))
{
delete _textImages;
_textImages = 0;

View File

@ -44,26 +44,28 @@ struct LetterDefinition
float commonLineHeight;
};
class FontDefinitionTTF
/**
*/
class FontDefinitionTTF : public Object
{
public:
FontDefinitionTTF();
~FontDefinitionTTF();
bool createFontDefinition(char *fontName, int fontSize, char *letters, int textureSize = 512, bool debugOutput = false);
static FontDefinitionTTF* create(const char *fontName, int fontSize, const char *letters, int textureSize = 512);
LetterDefinition & getLetterDefinition(unsigned short int theLetter);
Texture2D * getTexture(int index);
int getNumTextures();
int getNumTextures();
Font * getFont() { return _textImages->getFont(); }
float getCommonLineHeight() { return _commonLineHeight; }
Font * getFont() { return _textImages->getFont(); }
float getCommonLineHeight() { return _commonLineHeight; }
// carloX
// carloX new
FontAtlas * createFontAtlas();
private:
FontDefinitionTTF();
~FontDefinitionTTF();
bool initDefinition(const char *fontName, int fontSize, const char *letters, int textureSize);
bool prepareLetterDefinitions(TextFontPagesDef *pageDefs);
void addLetterDefinition(LetterDefinition &defToAdd);

View File

@ -35,6 +35,29 @@ StringTTF::StringTTF(FontDefinitionTTF *theDef, TextAlignment alignment) : _cu
_lineBreakWithoutSpaces(false),
_advances(0)
{
if (_fontDef)
_fontDef->retain();
}
StringTTF* StringTTF::create(FontDefinitionTTF *def, TextAlignment alignment)
{
StringTTF *ret = new StringTTF(def, alignment);
if (!ret)
return 0;
if( ret->init() )
{
ret->autorelease();
return ret;
}
else
{
delete ret;
return 0;
}
return ret;
}
StringTTF::~StringTTF()
@ -50,9 +73,17 @@ StringTTF::~StringTTF()
delete [] _advances;
_advances = 0;
}
if (_fontDef)
_fontDef->release();
}
bool StringTTF::setText(char *pStringToRender, float lineWidth, TextAlignment alignment, bool lineBreakWithoutSpaces)
bool StringTTF::init()
{
return true;
}
bool StringTTF::setText(const char *stringToRender, float lineWidth, TextAlignment alignment, bool lineBreakWithoutSpaces)
{
if (!_fontDef)
return false;
@ -75,7 +106,8 @@ bool StringTTF::setText(char *pStringToRender, float lineWidth, TextAlignment al
return false;
int numLetter = 0;
unsigned short int *pUTF8Text = pFont->getUTF16Text(pStringToRender, numLetter);
unsigned short int *pUTF8Text = pFont->getUTF16Text(stringToRender, numLetter);
setCurrentString(pUTF8Text);
// align text

View File

@ -34,12 +34,10 @@ class StringTTF : public Node , public LabelTextFormatProtocol, public RGBAProto
{
public:
StringTTF(FontDefinitionTTF *pDefinition, TextAlignment alignment = kTextAlignmentLeft);
~StringTTF();
bool setText(char *pStringToRender, float lineWidth, TextAlignment alignment = kTextAlignmentLeft, bool lineBreakWithoutSpaces = false);
static StringTTF* create(FontDefinitionTTF *pDefinition, TextAlignment alignment = kTextAlignmentLeft);
// main interface
bool setText(const char *pStringToRender, float lineWidth, TextAlignment alignment = kTextAlignmentLeft, bool lineBreakWithoutSpaces = false);
virtual void setAlignment(TextAlignment alignment);
virtual void setWidth(float width);
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace);
@ -99,6 +97,10 @@ public:
private:
StringTTF(FontDefinitionTTF *pDefinition, TextAlignment alignment = kTextAlignmentLeft);
~StringTTF();
bool init();
void alignText();
void hideAllLetters();

View File

@ -1658,16 +1658,30 @@ std::string LabelBMFontNewTest::subtitle()
LabelDyamicTest::LabelDyamicTest()
{
Size size = Director::sharedDirector()->getWinSize();
const char *pFontFileName = "fonts/arial.ttf";
const char *pFontGlyphs = "abcdefghilmnopqrstuvzxywABCDEFGHILMNOPQRSTUVZXYW0123456789,. ";
FontDefinitionTTF *pDef = new FontDefinitionTTF;
const char *fontGlyphs = "abcdefghilmnopqrstuvzxywABCDEFGHILMNOPQRSTUVZXYW0123456789,. ";
FontDefinitionTTF *def = FontDefinitionTTF::create(pFontFileName, 26, fontGlyphs);
pDef->createFontDefinition((char *)pFontFileName, 26, (char *) pFontGlyphs);
StringTTF *pTestString = new StringTTF(pDef);
pTestString->setText( (char *)LongSentencesExample, size.width, kTextAlignmentCenter, false);
addChild(pTestString);
pTestString->setPosition(Point(0, size.height/2));
if ( def )
{
label1 = StringTTF::create(def);
if (label1)
{
label1->setText( LongSentencesExample, size.width, kTextAlignmentCenter, false);
addChild(label1);
label1->setPosition(Point(0, size.height/2));
label1->retain();
}
}
}
LabelDyamicTest::~LabelDyamicTest()
{
if (label1)
{
label1->release();
}
}
void LabelDyamicTest::draw()

View File

@ -331,13 +331,18 @@ private:
class LabelDyamicTest : public AtlasDemo
{
public:
LabelDyamicTest();
LabelDyamicTest();
~LabelDyamicTest();
virtual void draw();
virtual std::string title();
virtual std::string subtitle();
private:
StringTTF *label1;
};