CCLabelTTF new interface for ccBuilder

This commit is contained in:
carlo morgantini 2013-05-03 10:06:08 -07:00
parent 9ba489bba4
commit d9a53b3eb1
3 changed files with 198 additions and 96 deletions

View File

@ -138,6 +138,30 @@ bool CCLabelTTF::initWithString(const char *string, const char *fontName, float
return false; return false;
} }
bool CCLabelTTF::initWithStringAndTextDefinition(const char *string, ccTextDefinition & textDefinition)
{
if (CCSprite::init())
{
// shader program
this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
// prepare everythin needed to render the label
_updateWithTextDefinition(textDefinition, false);
// set the string
this->setString(string);
//
return true;
}
else
{
return false;
}
}
void CCLabelTTF::setString(const char *string) void CCLabelTTF::setString(const char *string)
{ {
CCAssert(string != NULL, "Invalid string"); CCAssert(string != NULL, "Invalid string");
@ -259,97 +283,44 @@ void CCLabelTTF::setFontName(const char *fontName)
// Helper // Helper
bool CCLabelTTF::updateTexture() bool CCLabelTTF::updateTexture()
{ {
CCTexture2D *tex;
tex = new CCTexture2D();
assert(tex);
if (!tex)
return false;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
CCTexture2D *tex; ccTextDefinition texDef = _prepareTextDefinition();
// let system compute label's width or height when its value is 0
// refer to cocos2d-x issue #1430
tex = new CCTexture2D();
if (!tex)
{
return false;
}
ccTextDefinition texDef;
texDef.m_fontSize = m_fFontSize * CC_CONTENT_SCALE_FACTOR();
texDef.m_fontName = *m_pFontName;
texDef.m_alignment = m_hAlignment;
texDef.m_vertAlignment = m_vAlignment;
texDef.m_dimensions = CC_SIZE_POINTS_TO_PIXELS(m_tDimensions);
if ( m_strokeEnabled )
{
texDef.m_stroke.m_strokeEnabled = true;
texDef.m_stroke.m_strokeSize = m_strokeSize;
texDef.m_stroke.m_strokeColor = m_strokeColor;
}
else
{
texDef.m_stroke.m_strokeEnabled = false;
}
if ( m_shadowEnabled )
{
texDef.m_shadow.m_shadowEnabled = true;
texDef.m_shadow.m_shadowOffset = m_shadowOffset;
texDef.m_shadow.m_shadowBlur = m_shadowBlur;
texDef.m_shadow.m_shadowOpacity = m_shadowOpacity;
}
else
{
texDef.m_shadow.m_shadowEnabled = false;
}
// text tint
texDef.m_fontTint.m_tintColor = m_textTintColor;
// init the texture
tex->initWithStringShadowStroke( m_string.c_str(), texDef ); tex->initWithStringShadowStroke( m_string.c_str(), texDef );
this->setTexture(tex);
tex->release();
CCRect rect = CCRectZero;
rect.size = m_pobTexture->getContentSize();
this->setTextureRect(rect);
return true;
#else #else
CCTexture2D *tex;
// let system compute label's width or height when its value is 0
// refer to cocos2d-x issue #1430
tex = new CCTexture2D();
if (!tex)
{
return false;
}
tex->initWithString( m_string.c_str(), tex->initWithString( m_string.c_str(),
m_pFontName->c_str(), m_pFontName->c_str(),
m_fFontSize * CC_CONTENT_SCALE_FACTOR(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(),
CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions),
m_hAlignment, m_hAlignment,
m_vAlignment); m_vAlignment);
this->setTexture(tex);
tex->release();
CCRect rect = CCRectZero;
rect.size = m_pobTexture->getContentSize();
this->setTextureRect(rect);
return true;
#endif #endif
// set the texture
this->setTexture(tex);
// release it
tex->release();
// set the size in the sprite
CCRect rect =CCRectZero;
rect.size = m_pobTexture->getContentSize();
this->setTextureRect(rect);
//ok
return true;
} }
void CCLabelTTF::enableShadow(CCSize &shadowOffset, float shadowOpacity, float shadowBlur) void CCLabelTTF::enableShadow(CCSize &shadowOffset, float shadowOpacity, float shadowBlur, bool updateTexture)
{ {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
@ -380,7 +351,7 @@ void CCLabelTTF::enableShadow(CCSize &shadowOffset, float shadowOpacity, float s
} }
if ( valueChanged ) if ( valueChanged && updateTexture )
{ {
this->updateTexture(); this->updateTexture();
} }
@ -391,14 +362,17 @@ void CCLabelTTF::enableShadow(CCSize &shadowOffset, float shadowOpacity, float s
} }
void CCLabelTTF::disableShadow() void CCLabelTTF::disableShadow(bool updateTexture)
{ {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
if (m_shadowEnabled) if (m_shadowEnabled)
{ {
m_shadowEnabled = false; m_shadowEnabled = false;
this->updateTexture();
if (updateTexture)
this->updateTexture();
} }
#else #else
@ -406,7 +380,7 @@ void CCLabelTTF::disableShadow()
#endif #endif
} }
void CCLabelTTF::enableStroke(const ccColor3B &strokeColor, float strokeSize) void CCLabelTTF::enableStroke(const ccColor3B &strokeColor, float strokeSize, bool updateTexture)
{ {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
@ -430,7 +404,7 @@ void CCLabelTTF::enableStroke(const ccColor3B &strokeColor, float strokeSize)
valueChanged = true; valueChanged = true;
} }
if (valueChanged) if ( valueChanged && updateTexture )
{ {
this->updateTexture(); this->updateTexture();
} }
@ -441,14 +415,16 @@ void CCLabelTTF::enableStroke(const ccColor3B &strokeColor, float strokeSize)
} }
void CCLabelTTF::disableStroke() void CCLabelTTF::disableStroke(bool updateTexture)
{ {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
if (m_strokeEnabled) if (m_strokeEnabled)
{ {
m_strokeEnabled = false; m_strokeEnabled = false;
this->updateTexture();
if (updateTexture)
this->updateTexture();
} }
#else #else
@ -457,18 +433,105 @@ void CCLabelTTF::disableStroke()
} }
void CCLabelTTF::setFontFillColor(const ccColor3B &tintColor) void CCLabelTTF::setFontFillColor(const ccColor3B &tintColor, bool updateTexture)
{ {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
if (m_textTintColor.r != tintColor.r || m_textTintColor.g != tintColor.g || m_textTintColor.b != tintColor.b) if (m_textTintColor.r != tintColor.r || m_textTintColor.g != tintColor.g || m_textTintColor.b != tintColor.b)
{ {
m_textTintColor = tintColor; m_textTintColor = tintColor;
this->updateTexture();
if (updateTexture)
this->updateTexture();
} }
#else #else
CCAssert(false, "Currently only supported on iOS and Android!"); CCAssert(false, "Currently only supported on iOS and Android!");
#endif #endif
} }
void CCLabelTTF::setTextDefinition(ccTextDefinition *theDefinition)
{
if (theDefinition)
{
_updateWithTextDefinition(*theDefinition, true);
}
}
ccTextDefinition *CCLabelTTF::getTextDefinition()
{
ccTextDefinition *tempDefinition = new ccTextDefinition;
*tempDefinition = _prepareTextDefinition();
return tempDefinition;
}
void CCLabelTTF::_updateWithTextDefinition(ccTextDefinition & textDefinition, bool mustUpdateTexture)
{
m_tDimensions = CCSizeMake(textDefinition.m_dimensions.width, textDefinition.m_dimensions.height);
m_hAlignment = textDefinition.m_alignment;
m_vAlignment = textDefinition.m_vertAlignment;
m_pFontName = new std::string(textDefinition.m_fontName);
m_fFontSize = textDefinition.m_fontSize;
// shadow
if ( textDefinition.m_shadow.m_shadowEnabled )
{
enableShadow(textDefinition.m_shadow.m_shadowOffset, textDefinition.m_shadow.m_shadowOpacity, textDefinition.m_shadow.m_shadowBlur, false);
}
// stroke
if ( textDefinition.m_stroke.m_strokeEnabled )
{
enableStroke(textDefinition.m_stroke.m_strokeColor, textDefinition.m_stroke.m_strokeSize, false);
}
// fill color
if ( textDefinition.m_fontTint.m_tintEnabled )
{
setFontFillColor(textDefinition.m_fontTint.m_tintColor, false);
}
if (mustUpdateTexture)
updateTexture();
}
ccTextDefinition CCLabelTTF::_prepareTextDefinition()
{
ccTextDefinition texDef;
texDef.m_fontSize = m_fFontSize * CC_CONTENT_SCALE_FACTOR();
texDef.m_fontName = *m_pFontName;
texDef.m_alignment = m_hAlignment;
texDef.m_vertAlignment = m_vAlignment;
texDef.m_dimensions = CC_SIZE_POINTS_TO_PIXELS(m_tDimensions);
if ( m_strokeEnabled )
{
texDef.m_stroke.m_strokeEnabled = true;
texDef.m_stroke.m_strokeSize = m_strokeSize;
texDef.m_stroke.m_strokeColor = m_strokeColor;
}
else
{
texDef.m_stroke.m_strokeEnabled = false;
}
if ( m_shadowEnabled )
{
texDef.m_shadow.m_shadowEnabled = true;
texDef.m_shadow.m_shadowOffset = m_shadowOffset;
texDef.m_shadow.m_shadowBlur = m_shadowBlur;
texDef.m_shadow.m_shadowOpacity = m_shadowOpacity;
}
else
{
texDef.m_shadow.m_shadowEnabled = false;
}
// text tint
texDef.m_fontTint.m_tintColor = m_textTintColor;
return texDef;
}
NS_CC_END NS_CC_END

View File

@ -37,6 +37,8 @@ NS_CC_BEGIN
* @{ * @{
*/ */
/** @brief CCLabelTTF is a subclass of CCTextureNode that knows how to render text labels /** @brief CCLabelTTF is a subclass of CCTextureNode that knows how to render text labels
* *
* All features from CCTextureNode are valid in CCLabelTTF * All features from CCTextureNode are valid in CCLabelTTF
@ -80,22 +82,37 @@ public:
const CCSize& dimensions, CCTextAlignment hAlignment, const CCSize& dimensions, CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment); CCVerticalTextAlignment vAlignment);
/** initializes the CCLabelTTF with a font name, alignment, dimension and font size */
bool initWithStringAndTextDefinition(const char *string, ccTextDefinition & textDefinition);
/** set the text definition used by this label */
void setTextDefinition(ccTextDefinition *theDefinition);
/** get the text definition used by this label */
ccTextDefinition * getTextDefinition();
/** enable or disable shadow for the label */ /** enable or disable shadow for the label */
void enableShadow(CCSize &shadowOffset, float shadowOpacity, float shadowBlur); void enableShadow(CCSize &shadowOffset, float shadowOpacity, float shadowBlur, bool mustUpdateTexture = true);
/** disable shadow rendering */ /** disable shadow rendering */
void disableShadow(); void disableShadow(bool mustUpdateTexture = true);
/** enable or disable stroke */ /** enable or disable stroke */
void enableStroke(const ccColor3B &strokeColor, float strokeSize); void enableStroke(const ccColor3B &strokeColor, float strokeSize, bool mustUpdateTexture = true);
/** disable stroke */ /** disable stroke */
void disableStroke(); void disableStroke(bool mustUpdateTexture = true);
/** set text tinting */ /** set text tinting */
void setFontFillColor(const ccColor3B &tintColor); void setFontFillColor(const ccColor3B &tintColor, bool mustUpdateTexture = true);
/** initializes the CCLabelTTF */ /** initializes the CCLabelTTF */
bool init(); bool init();
@ -127,6 +144,11 @@ public:
private: private:
bool updateTexture(); bool updateTexture();
protected: protected:
/** set the text definition for this label */
void _updateWithTextDefinition(ccTextDefinition & textDefinition, bool mustUpdateTexture = true);
ccTextDefinition _prepareTextDefinition();
/** Dimensions of the label in Points */ /** Dimensions of the label in Points */
CCSize m_tDimensions; CCSize m_tDimensions;
/** The alignment of the label */ /** The alignment of the label */
@ -148,11 +170,10 @@ protected:
bool m_strokeEnabled; bool m_strokeEnabled;
ccColor3B m_strokeColor; ccColor3B m_strokeColor;
float m_strokeSize; float m_strokeSize;
/** font tint */ /** font tint */
ccColor3B m_textTintColor; ccColor3B m_textTintColor;
std::string m_string; std::string m_string;
}; };

View File

@ -103,9 +103,13 @@ Extension used for requesting text with stroke or shadow
*/ */
// text shadow attributes // text shadow attributes
typedef struct _ccTextShadow typedef class _ccTextShadow
{ {
bool m_shadowEnabled; public:
_ccTextShadow(): m_shadowEnabled(false) {}
bool m_shadowEnabled;
CCSize m_shadowOffset; CCSize m_shadowOffset;
float m_shadowBlur; float m_shadowBlur;
float m_shadowOpacity; float m_shadowOpacity;
@ -113,8 +117,12 @@ typedef struct _ccTextShadow
} ccTextShadow; } ccTextShadow;
// text stroke attributes // text stroke attributes
typedef struct _ccTextStroke typedef class _ccTextStroke
{ {
public:
_ccTextStroke(): m_strokeEnabled(false) {}
bool m_strokeEnabled; bool m_strokeEnabled;
ccColor3B m_strokeColor; ccColor3B m_strokeColor;
float m_strokeSize; float m_strokeSize;
@ -122,16 +130,26 @@ typedef struct _ccTextStroke
} ccTextStroke; } ccTextStroke;
// text tinting attributes // text tinting attributes
typedef struct _ccTextTint typedef class _ccTextTint
{ {
public:
_ccTextTint(): m_tintEnabled(false) {}
bool m_tintEnabled; bool m_tintEnabled;
ccColor3B m_tintColor; ccColor3B m_tintColor;
} ccTextTint; } ccTextTint;
// text attributes // text attributes
typedef struct _ccTextDefinition typedef class _ccTextDefinition : public CCObject
{ {
public:
_ccTextDefinition(): m_alignment(kCCTextAlignmentCenter),
m_vertAlignment(kCCVerticalTextAlignmentTop)
{ m_dimensions = CCSizeMake(0,0); }
std::string m_fontName; std::string m_fontName;
int m_fontSize; int m_fontSize;
CCTextAlignment m_alignment; CCTextAlignment m_alignment;