CCLabelTTF have now a tint, plus bug fixing and code clean up

This commit is contained in:
carlo morgantini 2013-04-30 16:36:14 -07:00
parent 221884f6d4
commit c727fa33c0
9 changed files with 244 additions and 203 deletions

View File

@ -48,6 +48,9 @@ CCLabelTTF::CCLabelTTF()
, m_shadowEnabled(false) , m_shadowEnabled(false)
, m_strokeEnabled(false) , m_strokeEnabled(false)
{ {
m_textTintColor.r = 255;
m_textTintColor.g = 255;
m_textTintColor.b = 255;
} }
CCLabelTTF::~CCLabelTTF() CCLabelTTF::~CCLabelTTF()
@ -260,66 +263,58 @@ bool CCLabelTTF::updateTexture()
// let system compute label's width or height when its value is 0 // let system compute label's width or height when its value is 0
// refer to cocos2d-x issue #1430 // refer to cocos2d-x issue #1430
tex = new CCTexture2D(); tex = new CCTexture2D();
if (!tex) if (!tex)
{ {
return false; return false;
} }
ccTextShadow *textShadow = 0; ccTextDefinition texDef;
ccTextStroke *textStroke = 0;
if (m_strokeEnabled) 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 )
{ {
textStroke = new ccTextStroke; texDef.m_stroke.m_strokeEnabled = true;
if (!textStroke) texDef.m_stroke.m_strokeSize = m_strokeSize;
return false; texDef.m_stroke.m_strokeColor = m_strokeColor;
textStroke->m_strokeSize = m_strokeSize;
textStroke->m_strokeColor = m_strokeColor;
} }
else
if (m_shadowEnabled)
{ {
textShadow = new ccTextShadow; texDef.m_stroke.m_strokeEnabled = false;
if (!textShadow)
{
if(textStroke)
delete textStroke;
return false;
}
textShadow->m_shadowOffset = m_shadowOffset;
textShadow->m_shadowBlur = m_shadowBlur;
textShadow->m_shadowOpacity = m_shadowOpacity;
} }
tex->initWithStringShadowStroke( m_string.c_str(), if ( m_shadowEnabled )
m_pFontName->c_str(), {
m_fFontSize * CC_CONTENT_SCALE_FACTOR(), texDef.m_shadow.m_shadowEnabled = true;
CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), texDef.m_shadow.m_shadowOffset = m_shadowOffset;
m_hAlignment, texDef.m_shadow.m_shadowBlur = m_shadowBlur;
m_vAlignment, texDef.m_shadow.m_shadowOpacity = m_shadowOpacity;
textShadow, }
textStroke); 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 );
this->setTexture(tex); this->setTexture(tex);
tex->release(); tex->release();
CCRect rect = CCRectZero; CCRect rect = CCRectZero;
rect.size = m_pobTexture->getContentSize(); rect.size = m_pobTexture->getContentSize();
this->setTextureRect(rect); this->setTextureRect(rect);
if (textShadow)
delete textShadow;
if (textStroke)
delete textStroke;
return true; return true;
} }
@ -404,4 +399,14 @@ void CCLabelTTF::disableStroke()
} }
} }
void CCLabelTTF::setFontTintColor(ccColor3B &tintColor)
{
if (m_textTintColor.r != tintColor.r || m_textTintColor.g != tintColor.g || m_textTintColor.b != tintColor.b)
{
m_textTintColor = tintColor;
this->updateTexture();
}
}
NS_CC_END NS_CC_END

View File

@ -93,6 +93,9 @@ public:
/** disable stroke */ /** disable stroke */
void disableStroke(); void disableStroke();
/** set text tinting */
void setFontTintColor(ccColor3B &tintColor);
/** initializes the CCLabelTTF */ /** initializes the CCLabelTTF */
bool init(); bool init();
@ -146,6 +149,9 @@ protected:
ccColor3B m_strokeColor; ccColor3B m_strokeColor;
float m_strokeSize; float m_strokeSize;
/** font tint */
ccColor3B m_textTintColor;
std::string m_string; std::string m_string;
}; };

View File

@ -121,6 +121,9 @@ public:
ETextAlign eAlignMask = kAlignCenter, ETextAlign eAlignMask = kAlignCenter,
const char * pFontName = 0, const char * pFontName = 0,
int nSize = 0, int nSize = 0,
float textTintR = 1,
float textTintG = 1,
float textTintB = 1,
bool shadow = false, bool shadow = false,
float shadowOffsetX = 0.0, float shadowOffsetX = 0.0,
float shadowOffsetY = 0.0, float shadowOffsetY = 0.0,
@ -136,10 +139,12 @@ public:
unsigned char * getData() { return m_pData; } unsigned char * getData() { return m_pData; }
int getDataLen() { return m_nWidth * m_nHeight; } int getDataLen() { return m_nWidth * m_nHeight; }
bool hasAlpha() { return m_bHasAlpha; }
bool isPremultipliedAlpha() { return m_bPreMulti; }
bool hasAlpha() { return m_bHasAlpha; }
bool isPremultipliedAlpha() { return m_bPreMulti; }
/** /**
@brief Save CCImage data to the specified file, with specified format. @brief Save CCImage data to the specified file, with specified format.
@ -167,6 +172,7 @@ protected:
bool m_bHasAlpha; bool m_bHasAlpha;
bool m_bPreMulti; bool m_bPreMulti;
private: private:
// noncopyable // noncopyable
CCImage(const CCImage& rImg); CCImage(const CCImage& rImg);

View File

@ -35,12 +35,15 @@ THE SOFTWARE.
#include <string.h> #include <string.h>
#include <jni.h> #include <jni.h>
// prototype
void swapAlphaChannel(unsigned int *pImageMemory, unsigned int numPixels);
NS_CC_BEGIN NS_CC_BEGIN
class BitmapDC class BitmapDC
{ {
public: public:
BitmapDC() BitmapDC()
: m_pData(NULL) : m_pData(NULL)
, m_nWidth(0) , m_nWidth(0)
@ -62,6 +65,9 @@ public:
CCImage::ETextAlign eAlignMask, CCImage::ETextAlign eAlignMask,
const char * pFontName, const char * pFontName,
float fontSize, float fontSize,
float textTintR = 1.0,
float textTintG = 1.0,
float textTintB = 1.0,
bool shadow = false, bool shadow = false,
float shadowDeltaX = 0.0, float shadowDeltaX = 0.0,
float shadowDeltaY = 0.0, float shadowDeltaY = 0.0,
@ -75,7 +81,7 @@ public:
{ {
JniMethodInfo methodInfo; JniMethodInfo methodInfo;
if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmapShadowStroke", if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmapShadowStroke",
"(Ljava/lang/String;Ljava/lang/String;IIIIZFFFZFFFF)V")) "(Ljava/lang/String;Ljava/lang/String;IFFFIIIZFFFZFFFF)V"))
{ {
CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__); CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
return false; return false;
@ -104,7 +110,7 @@ public:
jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str()); jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str());
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, jstrText, methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, jstrText,
jstrFont, (int)fontSize, eAlignMask, nWidth, nHeight, shadow, shadowDeltaX, -shadowDeltaY, shadowBlur, stroke, strokeColorR, strokeColorG, strokeColorB, strokeSize); jstrFont, (int)fontSize, textTintR, textTintG, textTintB, eAlignMask, nWidth, nHeight, shadow, shadowDeltaX, -shadowDeltaY, shadowBlur, stroke, strokeColorR, strokeColorG, strokeColorB, strokeSize);
methodInfo.env->DeleteLocalRef(jstrText); methodInfo.env->DeleteLocalRef(jstrText);
methodInfo.env->DeleteLocalRef(jstrFont); methodInfo.env->DeleteLocalRef(jstrFont);
@ -119,7 +125,6 @@ public:
return getBitmapFromJavaShadowStroke( text, nWidth, nHeight, eAlignMask, pFontName, fontSize ); return getBitmapFromJavaShadowStroke( text, nWidth, nHeight, eAlignMask, pFontName, fontSize );
} }
// ARGB -> RGBA // ARGB -> RGBA
inline unsigned int swapAlpha(unsigned int value) inline unsigned int swapAlpha(unsigned int value)
{ {
@ -179,7 +184,10 @@ bool CCImage::initWithStringShadowStroke(
int nHeight , int nHeight ,
ETextAlign eAlignMask , ETextAlign eAlignMask ,
const char * pFontName , const char * pFontName ,
int nSize , int nSize ,
float textTintR,
float textTintG,
float textTintB,
bool shadow, bool shadow,
float shadowOffsetX, float shadowOffsetX,
float shadowOffsetY, float shadowOffsetY,
@ -198,25 +206,16 @@ bool CCImage::initWithStringShadowStroke(
BitmapDC &dc = sharedBitmapDC(); BitmapDC &dc = sharedBitmapDC();
CC_BREAK_IF(! dc.getBitmapFromJavaShadowStroke(pText,
nWidth, CC_BREAK_IF(! dc.getBitmapFromJavaShadowStroke(pText, nWidth, nHeight, eAlignMask, pFontName,
nHeight, nSize, textTintR, textTintG, textTintB, shadow,
eAlignMask, shadowOffsetX, shadowOffsetY, shadowBlur, shadowOpacity,
pFontName, stroke, strokeR, strokeG, strokeB, strokeSize ));
nSize,
shadow,
shadowOffsetX,
shadowOffsetY,
shadowBlur,
shadowOpacity,
stroke,
strokeR,
strokeG,
strokeB,
strokeSize ));
// assign the dc.m_pData to m_pData in order to save time // assign the dc.m_pData to m_pData in order to save time
m_pData = dc.m_pData; m_pData = dc.m_pData;
CC_BREAK_IF(! m_pData); CC_BREAK_IF(! m_pData);
m_nWidth = (short)dc.m_nWidth; m_nWidth = (short)dc.m_nWidth;
@ -225,7 +224,12 @@ bool CCImage::initWithStringShadowStroke(
m_bPreMulti = true; m_bPreMulti = true;
m_nBitsPerComponent = 8; m_nBitsPerComponent = 8;
// swap the alpha channel (ARGB to RGBA)
swapAlphaChannel((unsigned int *)m_pData, (m_nWidth * m_nHeight) );
// ok
bRet = true; bRet = true;
} while (0); } while (0);
return bRet; return bRet;
@ -233,6 +237,19 @@ bool CCImage::initWithStringShadowStroke(
NS_CC_END NS_CC_END
// swap the alpha channel in an 32 bit image (from ARGB to RGBA)
void swapAlphaChannel(unsigned int *pImageMemory, unsigned int numPixels)
{
for(int c = 0; c < numPixels; ++c, ++pImageMemory)
{
// copy the current pixel
unsigned int currenPixel = (*pImageMemory);
// swap channels and store back
char *pSource = (char *) &currenPixel;
*pImageMemory = (pSource[0] << 24) | (pSource[3]<<16) | (pSource[2]<<8) | pSource[1];
}
}
// this method is called by Cocos2dxBitmap // this method is called by Cocos2dxBitmap
extern "C" extern "C"
{ {

View File

@ -125,12 +125,11 @@ public class Cocos2dxBitmap {
Cocos2dxBitmap.initNativeObject(bitmap); Cocos2dxBitmap.initNativeObject(bitmap);
} }
public static void createTextBitmapShadowStroke(String pString, public static void createTextBitmapShadowStroke(String pString, final String pFontName, final int pFontSize,
final String pFontName, final int pFontSize, final int pAlignment, final float fontTintR, final float fontTintG, final float fontTintB,
final int pWidth, final int pHeight, final boolean shadow, final int pAlignment, final int pWidth, final int pHeight, final boolean shadow,
final float shadowDX, final float shadowDY, final float shadowBlur, final float shadowDX, final float shadowDY, final float shadowBlur, final boolean stroke,
final boolean stroke, final float strokeR, final float strokeG, final float strokeR, final float strokeG, final float strokeB, final float strokeSize) {
final float strokeB, final float strokeSize) {
final int horizontalAlignment = pAlignment & 0x0F; final int horizontalAlignment = pAlignment & 0x0F;
final int verticalAlignment = (pAlignment >> 4) & 0x0F; final int verticalAlignment = (pAlignment >> 4) & 0x0F;
@ -139,6 +138,9 @@ public class Cocos2dxBitmap {
final Paint paint = Cocos2dxBitmap.newPaint(pFontName, pFontSize, final Paint paint = Cocos2dxBitmap.newPaint(pFontName, pFontSize,
horizontalAlignment); horizontalAlignment);
// set the paint color
paint.setARGB(255, (int)(255.0 * fontTintR), (int)(255.0 * fontTintG), (int)(255.0 * fontTintB));
final TextProperty textProperty = Cocos2dxBitmap.computeTextProperty( final TextProperty textProperty = Cocos2dxBitmap.computeTextProperty(
pString, pWidth, pHeight, paint); pString, pWidth, pHeight, paint);
@ -153,14 +155,13 @@ public class Cocos2dxBitmap {
if ( shadow ) { if ( shadow ) {
int shadowColor = 0xff7d7d7d; int shadowColor = 0xff7d7d7d;
paint.setShadowLayer(shadowBlur, shadowDX, shadowDY, Color.BLACK); paint.setShadowLayer(shadowBlur, shadowDX, shadowDY, shadowColor);
bitmapPaddingX = Math.abs(shadowDX) * 4; bitmapPaddingX = Math.abs(shadowDX) * 4;
bitmapPaddingY = Math.abs(shadowDY) * 4; bitmapPaddingY = Math.abs(shadowDY) * 4;
} }
final Bitmap bitmap = Bitmap.createBitmap(textProperty.mMaxWidth + (int)bitmapPaddingX, final Bitmap bitmap = Bitmap.createBitmap(textProperty.mMaxWidth + (int)bitmapPaddingX,
bitmapTotalHeight + (int)bitmapPaddingY, Bitmap.Config.ARGB_8888); bitmapTotalHeight + (int)bitmapPaddingY, Bitmap.Config.ARGB_8888);
@ -175,34 +176,30 @@ public class Cocos2dxBitmap {
final String[] lines = textProperty.mLines; final String[] lines = textProperty.mLines;
for (final String line : lines) { for (final String line : lines) {
x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment);
horizontalAlignment); canvas.drawText(line, x + (bitmapPaddingX/2) , y + (bitmapPaddingY/2) , paint);
canvas.drawText(line, x + (bitmapPaddingX/2) , y + ( bitmapPaddingY / 2 ) , paint);
y += textProperty.mHeightPerLine; y += textProperty.mHeightPerLine;
} }
// draw again with stroke on if needed // draw again with stroke on if needed
if ( stroke ) { if ( stroke ) {
final Paint paintStroke = Cocos2dxBitmap.newPaintNoColor(pFontName, pFontSize, final Paint paintStroke = Cocos2dxBitmap.newPaint(pFontName, pFontSize, horizontalAlignment);
horizontalAlignment); paintStroke.setStyle(Paint.Style.STROKE);
paint.setStyle(Paint.Style.STROKE); paintStroke.setStrokeWidth(strokeSize);
paint.setStrokeWidth(strokeSize); paintStroke.setARGB(255, (int)strokeR * 255, (int)strokeG * 255, (int)strokeB * 255);
//paint.setARGB(255, (int)strokeR * 255, (int)strokeG * 255, (int)strokeB * 255);
paint.setARGB(0, 0, 0,255);
//paint.clearShadowLayer();
x = 0; x = 0;
y = Cocos2dxBitmap.computeY(fontMetricsInt, pHeight, y = Cocos2dxBitmap.computeY(fontMetricsInt, pHeight,
textProperty.mTotalHeight, verticalAlignment); textProperty.mTotalHeight, verticalAlignment);
final String[] lines2 = textProperty.mLines; final String[] lines2 = textProperty.mLines;
for (final String line : lines2) { for (final String line : lines2) {
x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment);
horizontalAlignment); canvas.drawText(line, x + (bitmapPaddingX/2), y + (bitmapPaddingY/2) , paintStroke);
canvas.drawText(line, x + (bitmapPaddingX/2), y + ( bitmapPaddingY / 2 ) , paintStroke);
y += textProperty.mHeightPerLine; y += textProperty.mHeightPerLine;
} }
} }
Cocos2dxBitmap.initNativeObject(bitmap); Cocos2dxBitmap.initNativeObject(bitmap);
@ -212,7 +209,7 @@ public class Cocos2dxBitmap {
final int pHorizontalAlignment) { final int pHorizontalAlignment) {
final Paint paint = new Paint(); final Paint paint = new Paint();
paint.setColor(Color.WHITE); paint.setColor(Color.WHITE);
paint.setTextSize(pFontSize); paint.setTextSize(pFontSize);
paint.setAntiAlias(true); paint.setAntiAlias(true);
/* Set type face for paint, now it support .ttf file. */ /* Set type face for paint, now it support .ttf file. */
@ -248,46 +245,6 @@ public class Cocos2dxBitmap {
return paint; return paint;
} }
private static Paint newPaintNoColor(final String pFontName, final int pFontSize,
final int pHorizontalAlignment) {
final Paint paint = new Paint();
//paint.setColor(Color.WHITE);
paint.setTextSize(pFontSize);
paint.setAntiAlias(true);
/* Set type face for paint, now it support .ttf file. */
if (pFontName.endsWith(".ttf")) {
try {
final Typeface typeFace = Cocos2dxTypefaces.get(
Cocos2dxBitmap.sContext, pFontName);
paint.setTypeface(typeFace);
} catch (final Exception e) {
Log.e("Cocos2dxBitmap", "error to create ttf type face: "
+ pFontName);
/* The file may not find, use system font. */
paint.setTypeface(Typeface.create(pFontName, Typeface.NORMAL));
}
} else {
paint.setTypeface(Typeface.create(pFontName, Typeface.NORMAL));
}
switch (pHorizontalAlignment) {
case HORIZONTALALIGN_CENTER:
paint.setTextAlign(Align.CENTER);
break;
case HORIZONTALALIGN_RIGHT:
paint.setTextAlign(Align.RIGHT);
break;
case HORIZONTALALIGN_LEFT:
default:
paint.setTextAlign(Align.LEFT);
break;
}
return paint;
}
private static TextProperty computeTextProperty(final String pString, private static TextProperty computeTextProperty(final String pString,
final int pWidth, final int pHeight, final Paint pPaint) { final int pWidth, final int pHeight, final Paint pPaint) {
final FontMetricsInt fm = pPaint.getFontMetricsInt(); final FontMetricsInt fm = pPaint.getFontMetricsInt();

View File

@ -46,6 +46,9 @@ typedef struct
float strokeColorG; float strokeColorG;
float strokeColorB; float strokeColorB;
float strokeSize; float strokeSize;
float tintColorR;
float tintColorG;
float tintColorB;
unsigned char* data; unsigned char* data;
@ -295,8 +298,9 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
break; break;
} }
// text color
CGContextSetRGBFillColor(context, pInfo->tintColorR, pInfo->tintColorG, pInfo->tintColorB, 1);
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextTranslateCTM(context, 0.0f, dim.height); CGContextTranslateCTM(context, 0.0f, dim.height);
CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
UIGraphicsPushContext(context); UIGraphicsPushContext(context);
@ -517,6 +521,9 @@ bool CCImage::initWithStringShadowStroke(
ETextAlign eAlignMask , ETextAlign eAlignMask ,
const char * pFontName , const char * pFontName ,
int nSize , int nSize ,
float textTintR,
float textTintG,
float textTintB,
bool shadow, bool shadow,
float shadowOffsetX, float shadowOffsetX,
float shadowOffsetY, float shadowOffsetY,
@ -544,6 +551,9 @@ bool CCImage::initWithStringShadowStroke(
info.strokeColorG = strokeG; info.strokeColorG = strokeG;
info.strokeColorB = strokeB; info.strokeColorB = strokeB;
info.strokeSize = strokeSize; info.strokeSize = strokeSize;
info.tintColorR = textTintR;
info.tintColorG = textTintG;
info.tintColorB = textTintB;
if (! _initWithString(pText, eAlignMask, pFontName, nSize, &info)) if (! _initWithString(pText, eAlignMask, pFontName, nSize, &info))

View File

@ -284,7 +284,7 @@ bool CCTexture2D::initWithImage(CCImage *uiImage)
bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int width, unsigned int height) bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int width, unsigned int height)
{ {
unsigned char* tempData = image->getData(); unsigned char* tempData = image->getData();
unsigned int* inPixel32 = NULL; unsigned int* inPixel32 = NULL;
unsigned char* inPixel8 = NULL; unsigned char* inPixel8 = NULL;
unsigned short* outPixel16 = NULL; unsigned short* outPixel16 = NULL;
bool hasAlpha = image->hasAlpha(); bool hasAlpha = image->hasAlpha();
@ -293,9 +293,9 @@ bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned in
size_t bpp = image->getBitsPerComponent(); size_t bpp = image->getBitsPerComponent();
// compute pixel format // compute pixel format
if(hasAlpha) if (hasAlpha)
{ {
pixelFormat = g_defaultAlphaPixelFormat; pixelFormat = g_defaultAlphaPixelFormat;
} }
else else
{ {
@ -312,7 +312,7 @@ bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned in
// Repack the pixel data into the right format // Repack the pixel data into the right format
unsigned int length = width * height; unsigned int length = width * height;
if (pixelFormat == kCCTexture2DPixelFormat_RGB565) if (pixelFormat == kCCTexture2DPixelFormat_RGB565)
{ {
if (hasAlpha) if (hasAlpha)
@ -428,18 +428,28 @@ bool CCTexture2D::initWithString(const char *text, const char *fontName, float f
bool CCTexture2D::initWithString(const char *text, const char *fontName, float fontSize, const CCSize& dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment) bool CCTexture2D::initWithString(const char *text, const char *fontName, float fontSize, const CCSize& dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
{ {
return initWithStringShadowStroke(text, fontName, fontSize, dimensions, hAlignment, vAlignment, 0, 0); ccTextDefinition tempDef;
tempDef.m_shadow.m_shadowEnabled = false;
tempDef.m_stroke.m_strokeEnabled = false;
tempDef.m_fontTint.m_tintEnabled = false;
tempDef.m_fontName = std::string(fontName);
tempDef.m_fontSize = fontSize;
tempDef.m_dimensions = dimensions;
tempDef.m_alignment = hAlignment;
tempDef.m_vertAlignment = vAlignment;
tempDef.m_fontTint.m_tintColor.r = 255;
tempDef.m_fontTint.m_tintColor.g = 255;
tempDef.m_fontTint.m_tintColor.b = 255;
return initWithStringShadowStroke(text, tempDef);
} }
bool CCTexture2D::initWithStringShadowStroke(const char *text, bool CCTexture2D::initWithStringShadowStroke(const char *text, ccTextDefinition &textDefinition)
const char *fontName,
float fontSize,
const CCSize& dimensions,
CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment,
ccTextShadow *pShadowParams,
ccTextStroke *pStrokeParams)
{ {
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
// cache the texture data // cache the texture data
VolatileTexture::addStringTexture(this, text, dimensions, hAlignment, vAlignment, fontName, fontSize); VolatileTexture::addStringTexture(this, text, dimensions, hAlignment, vAlignment, fontName, fontSize);
@ -448,28 +458,26 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text,
bool bRet = false; bool bRet = false;
CCImage::ETextAlign eAlign; CCImage::ETextAlign eAlign;
if (kCCVerticalTextAlignmentTop == vAlignment) if (kCCVerticalTextAlignmentTop == textDefinition.m_vertAlignment)
{ {
eAlign = (kCCTextAlignmentCenter == hAlignment) ? CCImage::kAlignTop eAlign = (kCCTextAlignmentCenter == textDefinition.m_alignment) ? CCImage::kAlignTop
: (kCCTextAlignmentLeft == hAlignment) ? CCImage::kAlignTopLeft : CCImage::kAlignTopRight; : (kCCTextAlignmentLeft == textDefinition.m_alignment) ? CCImage::kAlignTopLeft : CCImage::kAlignTopRight;
} }
else if (kCCVerticalTextAlignmentCenter == vAlignment) else if (kCCVerticalTextAlignmentCenter == textDefinition.m_vertAlignment)
{ {
eAlign = (kCCTextAlignmentCenter == hAlignment) ? CCImage::kAlignCenter eAlign = (kCCTextAlignmentCenter == textDefinition.m_alignment) ? CCImage::kAlignCenter
: (kCCTextAlignmentLeft == hAlignment) ? CCImage::kAlignLeft : CCImage::kAlignRight; : (kCCTextAlignmentLeft == textDefinition.m_alignment) ? CCImage::kAlignLeft : CCImage::kAlignRight;
} }
else if (kCCVerticalTextAlignmentBottom == vAlignment) else if (kCCVerticalTextAlignmentBottom == textDefinition.m_vertAlignment)
{ {
eAlign = (kCCTextAlignmentCenter == hAlignment) ? CCImage::kAlignBottom eAlign = (kCCTextAlignmentCenter == textDefinition.m_alignment) ? CCImage::kAlignBottom
: (kCCTextAlignmentLeft == hAlignment) ? CCImage::kAlignBottomLeft : CCImage::kAlignBottomRight; : (kCCTextAlignmentLeft == textDefinition.m_alignment) ? CCImage::kAlignBottomLeft : CCImage::kAlignBottomRight;
} }
else else
{ {
CCAssert(false, "Not supported alignment format!"); CCAssert(false, "Not supported alignment format!");
return false; return false;
} }
// handle shadow parameters // handle shadow parameters
bool shadowEnabled = false; bool shadowEnabled = false;
@ -478,13 +486,13 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text,
float shadowBlur = 0.0; float shadowBlur = 0.0;
float shadowOpacity = 0.0; float shadowOpacity = 0.0;
if (pShadowParams!=0) if ( textDefinition.m_shadow.m_shadowEnabled )
{ {
shadowEnabled = true; shadowEnabled = true;
shadowDX = pShadowParams->m_shadowOffset.width; shadowDX = textDefinition.m_shadow.m_shadowOffset.width;
shadowDY = pShadowParams->m_shadowOffset.height; shadowDY = textDefinition.m_shadow.m_shadowOffset.height;
shadowBlur = pShadowParams->m_shadowBlur; shadowBlur = textDefinition.m_shadow.m_shadowBlur;
shadowOpacity = pShadowParams->m_shadowOpacity; shadowOpacity = textDefinition.m_shadow.m_shadowOpacity;
} }
// handle stroke parameters // handle stroke parameters
@ -494,13 +502,13 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text,
float strokeColorB = 0.0; float strokeColorB = 0.0;
float strokeSize = 0.0; float strokeSize = 0.0;
if (pStrokeParams!=0) if ( textDefinition.m_stroke.m_strokeEnabled )
{ {
strokeEnabled = true; strokeEnabled = true;
strokeColorR = pStrokeParams->m_strokeColor.r / 255; strokeColorR = textDefinition.m_stroke.m_strokeColor.r / 255;
strokeColorG = pStrokeParams->m_strokeColor.g / 255; strokeColorG = textDefinition.m_stroke.m_strokeColor.g / 255;
strokeColorB = pStrokeParams->m_strokeColor.b / 255; strokeColorB = textDefinition.m_stroke.m_strokeColor.b / 255;
strokeSize = pStrokeParams->m_strokeSize; strokeSize = textDefinition.m_stroke.m_strokeSize;
} }
CCImage* pImage = new CCImage(); CCImage* pImage = new CCImage();
@ -509,11 +517,14 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text,
CC_BREAK_IF(NULL == pImage); CC_BREAK_IF(NULL == pImage);
bRet = pImage->initWithStringShadowStroke(text, bRet = pImage->initWithStringShadowStroke(text,
(int)dimensions.width, (int)textDefinition.m_dimensions.width,
(int)dimensions.height, (int)textDefinition.m_dimensions.height,
eAlign, eAlign,
fontName, textDefinition.m_fontName.c_str(),
(int)fontSize, textDefinition.m_fontSize,
textDefinition.m_fontTint.m_tintColor.r / 255,
textDefinition.m_fontTint.m_tintColor.g / 255,
textDefinition.m_fontTint.m_tintColor.b / 255,
shadowEnabled, shadowEnabled,
shadowDX, shadowDX,
shadowDY, shadowDY,
@ -537,22 +548,6 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text,
} }
// implementation CCTexture2D (Drawing) // implementation CCTexture2D (Drawing)
void CCTexture2D::drawAtPoint(const CCPoint& point) void CCTexture2D::drawAtPoint(const CCPoint& point)

View File

@ -71,6 +71,7 @@ typedef enum {
//! 2-bit PVRTC-compressed texture: PVRTC2 //! 2-bit PVRTC-compressed texture: PVRTC2
kCCTexture2DPixelFormat_PVRTC2, kCCTexture2DPixelFormat_PVRTC2,
//! Default texture format: RGBA8888 //! Default texture format: RGBA8888
kCCTexture2DPixelFormat_Default = kCCTexture2DPixelFormat_RGBA8888, kCCTexture2DPixelFormat_Default = kCCTexture2DPixelFormat_RGBA8888,
@ -101,21 +102,47 @@ typedef struct _ccTexParams {
Extension used for requesting text with stroke or shadow Extension used for requesting text with stroke or shadow
*/ */
// text shadow attributes
typedef struct _ccTextShadow typedef struct _ccTextShadow
{ {
bool m_shadowEnabled;
CCSize m_shadowOffset; CCSize m_shadowOffset;
float m_shadowBlur; float m_shadowBlur;
float m_shadowOpacity; float m_shadowOpacity;
} ccTextShadow; } ccTextShadow;
// text stroke attributes
typedef struct _ccTextStroke typedef struct _ccTextStroke
{ {
bool m_strokeEnabled;
ccColor3B m_strokeColor; ccColor3B m_strokeColor;
float m_strokeSize; float m_strokeSize;
} ccTextStroke; } ccTextStroke;
// text tinting attributes
typedef struct _ccTextTint
{
bool m_tintEnabled;
ccColor3B m_tintColor;
} ccTextTint;
// text attributes
typedef struct _ccTextDefinition
{
std::string m_fontName;
int m_fontSize;
CCTextAlignment m_alignment;
CCVerticalTextAlignment m_vertAlignment;
CCSize m_dimensions;
ccTextTint m_fontTint;
ccTextShadow m_shadow;
ccTextStroke m_stroke;
} ccTextDefinition;
//CLASS INTERFACES: //CLASS INTERFACES:
@ -165,18 +192,7 @@ public:
/** Initializes a texture from a string with font name and font size */ /** Initializes a texture from a string with font name and font size */
bool initWithString(const char *text, const char *fontName, float fontSize); bool initWithString(const char *text, const char *fontName, float fontSize);
/** Initializes a texture from a string with dimensions, alignment, font name and font size shadow and stroke*/ /** Initializes a texture from a string with dimensions, alignment, font name and font size shadow and stroke*/
bool initWithStringShadowStroke(const char *text, bool initWithStringShadowStroke(const char *text, ccTextDefinition &textDefinition);
const char *fontName,
float fontSize,
const CCSize& dimensions,
CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment,
ccTextShadow *pShadowParams,
ccTextStroke *pStrokeParams);
/** Initializes a texture from a PVR file */ /** Initializes a texture from a PVR file */
bool initWithPVRFile(const char* file); bool initWithPVRFile(const char* file);

View File

@ -1,4 +1,4 @@
#include "LabelTest.h" #include "LabelTest.h"
#include "../testResource.h" #include "../testResource.h"
enum { enum {
@ -1498,38 +1498,67 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke()
CCSize s = CCDirector::sharedDirector()->getWinSize(); CCSize s = CCDirector::sharedDirector()->getWinSize();
ccColor3B tintColorRed = { 255, 0, 0 };
ccColor3B tintColorYellow = { 255, 255, 0 };
ccColor3B tintColorBlue = { 0, 0, 255 };
// create the label
CCLabelTTF* fontShadow = new CCLabelTTF(); CCLabelTTF* fontShadow = new CCLabelTTF();
fontShadow->initWithString("Shadow Only", "Marker Felt", 20); fontShadow->initWithString("Shadow Only Red Text", "Marker Felt", 20);
// enable shadow
CCSize shadowOffset(12.0, 12.0); CCSize shadowOffset(12.0, 12.0);
fontShadow->enableShadow(shadowOffset, 1.0, 1.0); fontShadow->enableShadow(shadowOffset, 1.0, 1.0);
// set the text tint color
fontShadow->setFontTintColor(tintColorRed);
// add label to the scene
this->addChild(fontShadow); this->addChild(fontShadow);
fontShadow->setPosition(ccp(s.width/2,s.height/4*2.5)); fontShadow->setPosition(ccp(s.width/2,s.height/4*2.5));
// create the label
CCLabelTTF* fontStroke = new CCLabelTTF(); CCLabelTTF* fontStroke = new CCLabelTTF();
fontStroke->initWithString("Stroke Only", "Marker Felt", 20); fontStroke->initWithString("Stroke Only Yellow Text", "Marker Felt", 20);
// enable stroke
ccColor3B strokeColor; ccColor3B strokeColor;
strokeColor.r = 0; strokeColor.r = 0;
strokeColor.g = 10; strokeColor.g = 10;
strokeColor.b = 255; strokeColor.b = 255;
fontStroke->enableStroke(strokeColor, 1.5f); fontStroke->enableStroke(strokeColor, 1.5f);
// set text tint color
fontStroke->setFontTintColor(tintColorYellow);
// add label to the scene
this->addChild(fontStroke); this->addChild(fontStroke);
fontStroke->setPosition(ccp(s.width/2,s.height/4*1.8)); fontStroke->setPosition(ccp(s.width/2,s.height/4*1.8));
// create the label
CCLabelTTF* fontStrokeAndShadow = new CCLabelTTF(); CCLabelTTF* fontStrokeAndShadow = new CCLabelTTF();
fontStrokeAndShadow->initWithString("Stroke & Shadow", "Marker Felt", 20); fontStrokeAndShadow->initWithString("Stroke & Shadow Blue Text", "Marker Felt", 20);
// enable stroke
ccColor3B strokeShadowColor; ccColor3B strokeShadowColor;
strokeShadowColor.r = 255;
strokeShadowColor.r = 0; strokeShadowColor.g = 0;
strokeShadowColor.g = 10; strokeShadowColor.b = 0;
strokeShadowColor.b = 255;
fontStrokeAndShadow->enableStroke(strokeShadowColor, 1.5f); fontStrokeAndShadow->enableStroke(strokeShadowColor, 1.5f);
// enable shadow
fontStrokeAndShadow->enableShadow(shadowOffset, 1.0, 1.0); fontStrokeAndShadow->enableShadow(shadowOffset, 1.0, 1.0);
// set text tint color
fontStrokeAndShadow->setFontTintColor(tintColorBlue);
// add label to the scene
this->addChild(fontStrokeAndShadow); this->addChild(fontStrokeAndShadow);
fontStrokeAndShadow->setPosition(ccp(s.width/2,s.height/4*1.1)); fontStrokeAndShadow->setPosition(ccp(s.width/2,s.height/4*1.1));