diff --git a/cocos2dx/label_nodes/CCLabelTTF.cpp b/cocos2dx/label_nodes/CCLabelTTF.cpp index a7aaeee42e..43991dcb16 100644 --- a/cocos2dx/label_nodes/CCLabelTTF.cpp +++ b/cocos2dx/label_nodes/CCLabelTTF.cpp @@ -48,6 +48,9 @@ CCLabelTTF::CCLabelTTF() , m_shadowEnabled(false) , m_strokeEnabled(false) { + m_textTintColor.r = 255; + m_textTintColor.g = 255; + m_textTintColor.b = 255; } CCLabelTTF::~CCLabelTTF() @@ -260,66 +263,58 @@ bool CCLabelTTF::updateTexture() // 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; } - ccTextShadow *textShadow = 0; - ccTextStroke *textStroke = 0; + ccTextDefinition texDef; - 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; - if (!textStroke) - return false; - - textStroke->m_strokeSize = m_strokeSize; - textStroke->m_strokeColor = m_strokeColor; + texDef.m_stroke.m_strokeEnabled = true; + texDef.m_stroke.m_strokeSize = m_strokeSize; + texDef.m_stroke.m_strokeColor = m_strokeColor; } - - if (m_shadowEnabled) + else { - textShadow = new ccTextShadow; - - if (!textShadow) - { - if(textStroke) - delete textStroke; - return false; - } - - textShadow->m_shadowOffset = m_shadowOffset; - textShadow->m_shadowBlur = m_shadowBlur; - textShadow->m_shadowOpacity = m_shadowOpacity; + texDef.m_stroke.m_strokeEnabled = false; } - tex->initWithStringShadowStroke( m_string.c_str(), - m_pFontName->c_str(), - m_fFontSize * CC_CONTENT_SCALE_FACTOR(), - CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), - m_hAlignment, - m_vAlignment, - textShadow, - textStroke); + 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 ); this->setTexture(tex); tex->release(); CCRect rect = CCRectZero; - rect.size = m_pobTexture->getContentSize(); + rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); - - if (textShadow) - delete textShadow; - - if (textStroke) - delete textStroke; - - 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 diff --git a/cocos2dx/label_nodes/CCLabelTTF.h b/cocos2dx/label_nodes/CCLabelTTF.h index 3096163ab2..6c30fe076a 100644 --- a/cocos2dx/label_nodes/CCLabelTTF.h +++ b/cocos2dx/label_nodes/CCLabelTTF.h @@ -93,6 +93,9 @@ public: /** disable stroke */ void disableStroke(); + /** set text tinting */ + void setFontTintColor(ccColor3B &tintColor); + /** initializes the CCLabelTTF */ bool init(); @@ -146,6 +149,9 @@ protected: ccColor3B m_strokeColor; float m_strokeSize; + /** font tint */ + ccColor3B m_textTintColor; + std::string m_string; }; diff --git a/cocos2dx/platform/CCImage.h b/cocos2dx/platform/CCImage.h index f9b3bda61b..43a901db8c 100644 --- a/cocos2dx/platform/CCImage.h +++ b/cocos2dx/platform/CCImage.h @@ -121,6 +121,9 @@ public: ETextAlign eAlignMask = kAlignCenter, const char * pFontName = 0, int nSize = 0, + float textTintR = 1, + float textTintG = 1, + float textTintB = 1, bool shadow = false, float shadowOffsetX = 0.0, float shadowOffsetY = 0.0, @@ -136,10 +139,12 @@ public: 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. @@ -167,6 +172,7 @@ protected: bool m_bHasAlpha; bool m_bPreMulti; + private: // noncopyable CCImage(const CCImage& rImg); diff --git a/cocos2dx/platform/android/CCImage.cpp b/cocos2dx/platform/android/CCImage.cpp index c4ffb961ee..85ef335620 100644 --- a/cocos2dx/platform/android/CCImage.cpp +++ b/cocos2dx/platform/android/CCImage.cpp @@ -35,12 +35,15 @@ THE SOFTWARE. #include #include +// prototype +void swapAlphaChannel(unsigned int *pImageMemory, unsigned int numPixels); NS_CC_BEGIN class BitmapDC { public: + BitmapDC() : m_pData(NULL) , m_nWidth(0) @@ -62,6 +65,9 @@ public: CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize, + float textTintR = 1.0, + float textTintG = 1.0, + float textTintB = 1.0, bool shadow = false, float shadowDeltaX = 0.0, float shadowDeltaY = 0.0, @@ -75,7 +81,7 @@ public: { JniMethodInfo methodInfo; 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__); return false; @@ -104,7 +110,7 @@ public: jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str()); 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(jstrFont); @@ -119,7 +125,6 @@ public: return getBitmapFromJavaShadowStroke( text, nWidth, nHeight, eAlignMask, pFontName, fontSize ); } - // ARGB -> RGBA inline unsigned int swapAlpha(unsigned int value) { @@ -179,7 +184,10 @@ bool CCImage::initWithStringShadowStroke( int nHeight , ETextAlign eAlignMask , const char * pFontName , - int nSize , + int nSize , + float textTintR, + float textTintG, + float textTintB, bool shadow, float shadowOffsetX, float shadowOffsetY, @@ -198,25 +206,16 @@ bool CCImage::initWithStringShadowStroke( BitmapDC &dc = sharedBitmapDC(); - CC_BREAK_IF(! dc.getBitmapFromJavaShadowStroke(pText, - nWidth, - nHeight, - eAlignMask, - pFontName, - nSize, - shadow, - shadowOffsetX, - shadowOffsetY, - shadowBlur, - shadowOpacity, - stroke, - strokeR, - strokeG, - strokeB, - strokeSize )); + + CC_BREAK_IF(! dc.getBitmapFromJavaShadowStroke(pText, nWidth, nHeight, eAlignMask, pFontName, + nSize, textTintR, textTintG, textTintB, shadow, + shadowOffsetX, shadowOffsetY, shadowBlur, shadowOpacity, + stroke, strokeR, strokeG, strokeB, strokeSize )); + // assign the dc.m_pData to m_pData in order to save time m_pData = dc.m_pData; + CC_BREAK_IF(! m_pData); m_nWidth = (short)dc.m_nWidth; @@ -225,7 +224,12 @@ bool CCImage::initWithStringShadowStroke( m_bPreMulti = true; m_nBitsPerComponent = 8; + // swap the alpha channel (ARGB to RGBA) + swapAlphaChannel((unsigned int *)m_pData, (m_nWidth * m_nHeight) ); + + // ok bRet = true; + } while (0); return bRet; @@ -233,6 +237,19 @@ bool CCImage::initWithStringShadowStroke( 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 *) ¤Pixel; + *pImageMemory = (pSource[0] << 24) | (pSource[3]<<16) | (pSource[2]<<8) | pSource[1]; + } +} + // this method is called by Cocos2dxBitmap extern "C" { diff --git a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java index b2b1a20bf3..1adbd25bd1 100644 --- a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -125,12 +125,11 @@ public class Cocos2dxBitmap { Cocos2dxBitmap.initNativeObject(bitmap); } - public static void createTextBitmapShadowStroke(String pString, - final String pFontName, final int pFontSize, final int pAlignment, - final int pWidth, final int pHeight, final boolean shadow, - final float shadowDX, final float shadowDY, final float shadowBlur, - final boolean stroke, final float strokeR, final float strokeG, - final float strokeB, final float strokeSize) { + public static void createTextBitmapShadowStroke(String pString, final String pFontName, final int pFontSize, + final float fontTintR, final float fontTintG, final float fontTintB, + final int pAlignment, final int pWidth, final int pHeight, final boolean shadow, + final float shadowDX, final float shadowDY, final float shadowBlur, final boolean stroke, + final float strokeR, final float strokeG, final float strokeB, final float strokeSize) { final int horizontalAlignment = pAlignment & 0x0F; final int verticalAlignment = (pAlignment >> 4) & 0x0F; @@ -139,6 +138,9 @@ public class Cocos2dxBitmap { final Paint paint = Cocos2dxBitmap.newPaint(pFontName, pFontSize, 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( pString, pWidth, pHeight, paint); @@ -153,14 +155,13 @@ public class Cocos2dxBitmap { if ( shadow ) { int shadowColor = 0xff7d7d7d; - paint.setShadowLayer(shadowBlur, shadowDX, shadowDY, Color.BLACK); - + paint.setShadowLayer(shadowBlur, shadowDX, shadowDY, shadowColor); + bitmapPaddingX = Math.abs(shadowDX) * 4; bitmapPaddingY = Math.abs(shadowDY) * 4; } - final Bitmap bitmap = Bitmap.createBitmap(textProperty.mMaxWidth + (int)bitmapPaddingX, bitmapTotalHeight + (int)bitmapPaddingY, Bitmap.Config.ARGB_8888); @@ -175,34 +176,30 @@ public class Cocos2dxBitmap { final String[] lines = textProperty.mLines; for (final String line : lines) { - x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, - horizontalAlignment); - canvas.drawText(line, x + (bitmapPaddingX/2) , y + ( bitmapPaddingY / 2 ) , paint); + x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment); + canvas.drawText(line, x + (bitmapPaddingX/2) , y + (bitmapPaddingY/2) , paint); y += textProperty.mHeightPerLine; } // draw again with stroke on if needed if ( stroke ) { - final Paint paintStroke = Cocos2dxBitmap.newPaintNoColor(pFontName, pFontSize, - horizontalAlignment); - paint.setStyle(Paint.Style.STROKE); - paint.setStrokeWidth(strokeSize); - //paint.setARGB(255, (int)strokeR * 255, (int)strokeG * 255, (int)strokeB * 255); - paint.setARGB(0, 0, 0,255); - //paint.clearShadowLayer(); - + final Paint paintStroke = Cocos2dxBitmap.newPaint(pFontName, pFontSize, horizontalAlignment); + paintStroke.setStyle(Paint.Style.STROKE); + paintStroke.setStrokeWidth(strokeSize); + paintStroke.setARGB(255, (int)strokeR * 255, (int)strokeG * 255, (int)strokeB * 255); + x = 0; y = Cocos2dxBitmap.computeY(fontMetricsInt, pHeight, textProperty.mTotalHeight, verticalAlignment); final String[] lines2 = textProperty.mLines; for (final String line : lines2) { - x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, - horizontalAlignment); - canvas.drawText(line, x + (bitmapPaddingX/2), y + ( bitmapPaddingY / 2 ) , paintStroke); + x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment); + canvas.drawText(line, x + (bitmapPaddingX/2), y + (bitmapPaddingY/2) , paintStroke); y += textProperty.mHeightPerLine; } + } Cocos2dxBitmap.initNativeObject(bitmap); @@ -212,7 +209,7 @@ public class Cocos2dxBitmap { final int pHorizontalAlignment) { final Paint paint = new Paint(); paint.setColor(Color.WHITE); - paint.setTextSize(pFontSize); + paint.setTextSize(pFontSize); paint.setAntiAlias(true); /* Set type face for paint, now it support .ttf file. */ @@ -248,46 +245,6 @@ public class Cocos2dxBitmap { 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, final int pWidth, final int pHeight, final Paint pPaint) { final FontMetricsInt fm = pPaint.getFontMetricsInt(); diff --git a/cocos2dx/platform/ios/CCImage.mm b/cocos2dx/platform/ios/CCImage.mm index 956b1ad28e..ecc187c779 100644 --- a/cocos2dx/platform/ios/CCImage.mm +++ b/cocos2dx/platform/ios/CCImage.mm @@ -46,6 +46,9 @@ typedef struct float strokeColorG; float strokeColorB; float strokeSize; + float tintColorR; + float tintColorG; + float tintColorB; unsigned char* data; @@ -295,8 +298,9 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl break; } + // text color + CGContextSetRGBFillColor(context, pInfo->tintColorR, pInfo->tintColorG, pInfo->tintColorB, 1); - CGContextSetRGBFillColor(context, 1, 1, 1, 1); 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 UIGraphicsPushContext(context); @@ -517,6 +521,9 @@ bool CCImage::initWithStringShadowStroke( ETextAlign eAlignMask , const char * pFontName , int nSize , + float textTintR, + float textTintG, + float textTintB, bool shadow, float shadowOffsetX, float shadowOffsetY, @@ -544,6 +551,9 @@ bool CCImage::initWithStringShadowStroke( info.strokeColorG = strokeG; info.strokeColorB = strokeB; info.strokeSize = strokeSize; + info.tintColorR = textTintR; + info.tintColorG = textTintG; + info.tintColorB = textTintB; if (! _initWithString(pText, eAlignMask, pFontName, nSize, &info)) diff --git a/cocos2dx/textures/CCTexture2D.cpp b/cocos2dx/textures/CCTexture2D.cpp index 55c85e79e1..68d596bc04 100644 --- a/cocos2dx/textures/CCTexture2D.cpp +++ b/cocos2dx/textures/CCTexture2D.cpp @@ -284,7 +284,7 @@ bool CCTexture2D::initWithImage(CCImage *uiImage) bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int width, unsigned int height) { unsigned char* tempData = image->getData(); - unsigned int* inPixel32 = NULL; + unsigned int* inPixel32 = NULL; unsigned char* inPixel8 = NULL; unsigned short* outPixel16 = NULL; bool hasAlpha = image->hasAlpha(); @@ -293,9 +293,9 @@ bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned in size_t bpp = image->getBitsPerComponent(); // compute pixel format - if(hasAlpha) + if (hasAlpha) { - pixelFormat = g_defaultAlphaPixelFormat; + pixelFormat = g_defaultAlphaPixelFormat; } else { @@ -312,7 +312,7 @@ bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned in // Repack the pixel data into the right format unsigned int length = width * height; - + if (pixelFormat == kCCTexture2DPixelFormat_RGB565) { 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) { - 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, - const char *fontName, - float fontSize, - const CCSize& dimensions, - CCTextAlignment hAlignment, - CCVerticalTextAlignment vAlignment, - ccTextShadow *pShadowParams, - ccTextStroke *pStrokeParams) +bool CCTexture2D::initWithStringShadowStroke(const char *text, ccTextDefinition &textDefinition) { + #if CC_ENABLE_CACHE_TEXTURE_DATA // cache the texture data VolatileTexture::addStringTexture(this, text, dimensions, hAlignment, vAlignment, fontName, fontSize); @@ -448,28 +458,26 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text, bool bRet = false; CCImage::ETextAlign eAlign; - if (kCCVerticalTextAlignmentTop == vAlignment) + if (kCCVerticalTextAlignmentTop == textDefinition.m_vertAlignment) { - eAlign = (kCCTextAlignmentCenter == hAlignment) ? CCImage::kAlignTop - : (kCCTextAlignmentLeft == hAlignment) ? CCImage::kAlignTopLeft : CCImage::kAlignTopRight; + eAlign = (kCCTextAlignmentCenter == textDefinition.m_alignment) ? CCImage::kAlignTop + : (kCCTextAlignmentLeft == textDefinition.m_alignment) ? CCImage::kAlignTopLeft : CCImage::kAlignTopRight; } - else if (kCCVerticalTextAlignmentCenter == vAlignment) + else if (kCCVerticalTextAlignmentCenter == textDefinition.m_vertAlignment) { - eAlign = (kCCTextAlignmentCenter == hAlignment) ? CCImage::kAlignCenter - : (kCCTextAlignmentLeft == hAlignment) ? CCImage::kAlignLeft : CCImage::kAlignRight; + eAlign = (kCCTextAlignmentCenter == textDefinition.m_alignment) ? CCImage::kAlignCenter + : (kCCTextAlignmentLeft == textDefinition.m_alignment) ? CCImage::kAlignLeft : CCImage::kAlignRight; } - else if (kCCVerticalTextAlignmentBottom == vAlignment) + else if (kCCVerticalTextAlignmentBottom == textDefinition.m_vertAlignment) { - eAlign = (kCCTextAlignmentCenter == hAlignment) ? CCImage::kAlignBottom - : (kCCTextAlignmentLeft == hAlignment) ? CCImage::kAlignBottomLeft : CCImage::kAlignBottomRight; + eAlign = (kCCTextAlignmentCenter == textDefinition.m_alignment) ? CCImage::kAlignBottom + : (kCCTextAlignmentLeft == textDefinition.m_alignment) ? CCImage::kAlignBottomLeft : CCImage::kAlignBottomRight; } else { CCAssert(false, "Not supported alignment format!"); return false; } - - // handle shadow parameters bool shadowEnabled = false; @@ -478,13 +486,13 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text, float shadowBlur = 0.0; float shadowOpacity = 0.0; - if (pShadowParams!=0) + if ( textDefinition.m_shadow.m_shadowEnabled ) { shadowEnabled = true; - shadowDX = pShadowParams->m_shadowOffset.width; - shadowDY = pShadowParams->m_shadowOffset.height; - shadowBlur = pShadowParams->m_shadowBlur; - shadowOpacity = pShadowParams->m_shadowOpacity; + shadowDX = textDefinition.m_shadow.m_shadowOffset.width; + shadowDY = textDefinition.m_shadow.m_shadowOffset.height; + shadowBlur = textDefinition.m_shadow.m_shadowBlur; + shadowOpacity = textDefinition.m_shadow.m_shadowOpacity; } // handle stroke parameters @@ -494,13 +502,13 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text, float strokeColorB = 0.0; float strokeSize = 0.0; - if (pStrokeParams!=0) + if ( textDefinition.m_stroke.m_strokeEnabled ) { strokeEnabled = true; - strokeColorR = pStrokeParams->m_strokeColor.r / 255; - strokeColorG = pStrokeParams->m_strokeColor.g / 255; - strokeColorB = pStrokeParams->m_strokeColor.b / 255; - strokeSize = pStrokeParams->m_strokeSize; + strokeColorR = textDefinition.m_stroke.m_strokeColor.r / 255; + strokeColorG = textDefinition.m_stroke.m_strokeColor.g / 255; + strokeColorB = textDefinition.m_stroke.m_strokeColor.b / 255; + strokeSize = textDefinition.m_stroke.m_strokeSize; } CCImage* pImage = new CCImage(); @@ -509,11 +517,14 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text, CC_BREAK_IF(NULL == pImage); bRet = pImage->initWithStringShadowStroke(text, - (int)dimensions.width, - (int)dimensions.height, + (int)textDefinition.m_dimensions.width, + (int)textDefinition.m_dimensions.height, eAlign, - fontName, - (int)fontSize, + textDefinition.m_fontName.c_str(), + 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, shadowDX, shadowDY, @@ -537,22 +548,6 @@ bool CCTexture2D::initWithStringShadowStroke(const char *text, } - - - - - - - - - - - - - - - - // implementation CCTexture2D (Drawing) void CCTexture2D::drawAtPoint(const CCPoint& point) diff --git a/cocos2dx/textures/CCTexture2D.h b/cocos2dx/textures/CCTexture2D.h index 409843f200..b6a069452f 100644 --- a/cocos2dx/textures/CCTexture2D.h +++ b/cocos2dx/textures/CCTexture2D.h @@ -71,6 +71,7 @@ typedef enum { //! 2-bit PVRTC-compressed texture: PVRTC2 kCCTexture2DPixelFormat_PVRTC2, + //! Default texture format: RGBA8888 kCCTexture2DPixelFormat_Default = kCCTexture2DPixelFormat_RGBA8888, @@ -101,21 +102,47 @@ typedef struct _ccTexParams { Extension used for requesting text with stroke or shadow */ +// text shadow attributes typedef struct _ccTextShadow { + bool m_shadowEnabled; CCSize m_shadowOffset; float m_shadowBlur; float m_shadowOpacity; } ccTextShadow; +// text stroke attributes typedef struct _ccTextStroke { + bool m_strokeEnabled; ccColor3B m_strokeColor; float m_strokeSize; } 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: @@ -165,18 +192,7 @@ public: /** Initializes a texture from a string with font name and font size */ 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*/ - bool initWithStringShadowStroke(const char *text, - const char *fontName, - float fontSize, - const CCSize& dimensions, - CCTextAlignment hAlignment, - CCVerticalTextAlignment vAlignment, - ccTextShadow *pShadowParams, - ccTextStroke *pStrokeParams); - - - - + bool initWithStringShadowStroke(const char *text, ccTextDefinition &textDefinition); /** Initializes a texture from a PVR file */ bool initWithPVRFile(const char* file); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp index 4baa4a4be9..99fec88950 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp @@ -1,4 +1,4 @@ -#include "LabelTest.h" + #include "LabelTest.h" #include "../testResource.h" enum { @@ -1498,38 +1498,67 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() 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(); - fontShadow->initWithString("Shadow Only", "Marker Felt", 20); + fontShadow->initWithString("Shadow Only Red Text", "Marker Felt", 20); + + // enable shadow CCSize shadowOffset(12.0, 12.0); fontShadow->enableShadow(shadowOffset, 1.0, 1.0); + + // set the text tint color + fontShadow->setFontTintColor(tintColorRed); + + // add label to the scene this->addChild(fontShadow); fontShadow->setPosition(ccp(s.width/2,s.height/4*2.5)); + + // create the label CCLabelTTF* fontStroke = new CCLabelTTF(); - fontStroke->initWithString("Stroke Only", "Marker Felt", 20); + fontStroke->initWithString("Stroke Only Yellow Text", "Marker Felt", 20); + // enable stroke ccColor3B strokeColor; strokeColor.r = 0; strokeColor.g = 10; strokeColor.b = 255; fontStroke->enableStroke(strokeColor, 1.5f); + + // set text tint color + fontStroke->setFontTintColor(tintColorYellow); + + // add label to the scene this->addChild(fontStroke); fontStroke->setPosition(ccp(s.width/2,s.height/4*1.8)); + + + + // create the label CCLabelTTF* fontStrokeAndShadow = new CCLabelTTF(); - fontStrokeAndShadow->initWithString("Stroke & Shadow", "Marker Felt", 20); + fontStrokeAndShadow->initWithString("Stroke & Shadow Blue Text", "Marker Felt", 20); + // enable stroke ccColor3B strokeShadowColor; - - strokeShadowColor.r = 0; - strokeShadowColor.g = 10; - strokeShadowColor.b = 255; - + strokeShadowColor.r = 255; + strokeShadowColor.g = 0; + strokeShadowColor.b = 0; fontStrokeAndShadow->enableStroke(strokeShadowColor, 1.5f); + + // enable shadow fontStrokeAndShadow->enableShadow(shadowOffset, 1.0, 1.0); + + // set text tint color + fontStrokeAndShadow->setFontTintColor(tintColorBlue); + + // add label to the scene this->addChild(fontStrokeAndShadow); fontStrokeAndShadow->setPosition(ccp(s.width/2,s.height/4*1.1));