2010-12-17 23:44:19 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2010-12-17 23:44:19 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2010-12-24 14:00:49 +08:00
|
|
|
#include "CCLabelTTF.h"
|
2011-11-28 17:28:43 +08:00
|
|
|
#include "CCDirector.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "shaders/CCGLProgram.h"
|
|
|
|
#include "shaders/CCShaderCache.h"
|
2012-06-11 14:36:25 +08:00
|
|
|
#include "CCApplication.h"
|
2010-12-17 23:44:19 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_BEGIN
|
2011-03-15 11:59:45 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
#if CC_USE_LA88_LABELS
|
|
|
|
#define SHADER_PROGRAM kCCShader_PositionTextureColor
|
|
|
|
#else
|
|
|
|
#define SHADER_PROGRAM kCCShader_PositionTextureA8Color
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
//CCLabelTTF
|
|
|
|
//
|
|
|
|
CCLabelTTF::CCLabelTTF()
|
2012-06-11 14:36:25 +08:00
|
|
|
: m_hAlignment(kCCTextAlignmentCenter)
|
|
|
|
, m_vAlignment(kCCVerticalTextAlignmentTop)
|
|
|
|
, m_pFontName(NULL)
|
|
|
|
, m_fFontSize(0.0)
|
|
|
|
, m_string("")
|
2013-04-26 09:22:26 +08:00
|
|
|
, m_shadowEnabled(false)
|
|
|
|
, m_strokeEnabled(false)
|
2013-05-07 05:27:54 +08:00
|
|
|
, m_textFillColor(ccWHITE)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
|
|
|
}
|
2011-03-15 11:59:45 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CCLabelTTF::~CCLabelTTF()
|
|
|
|
{
|
2012-06-14 06:56:52 +08:00
|
|
|
CC_SAFE_DELETE(m_pFontName);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 18:32:44 +08:00
|
|
|
CCLabelTTF * CCLabelTTF::create()
|
2012-05-31 06:34:50 +08:00
|
|
|
{
|
|
|
|
CCLabelTTF * pRet = new CCLabelTTF();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF * CCLabelTTF::create(const char *string, const char *fontName, float fontSize)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-08-01 13:24:23 +08:00
|
|
|
return CCLabelTTF::create(string, fontName, fontSize,
|
|
|
|
CCSizeZero, kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop);
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:24:23 +08:00
|
|
|
CCLabelTTF * CCLabelTTF::create(const char *string, const char *fontName, float fontSize,
|
|
|
|
const CCSize& dimensions, CCTextAlignment hAlignment)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2012-08-01 13:24:23 +08:00
|
|
|
return CCLabelTTF::create(string, fontName, fontSize, dimensions, hAlignment, kCCVerticalTextAlignmentTop);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2012-08-01 13:24:23 +08:00
|
|
|
CCLabelTTF* CCLabelTTF::create(const char *string, const char *fontName, float fontSize,
|
|
|
|
const CCSize &dimensions, CCTextAlignment hAlignment,
|
|
|
|
CCVerticalTextAlignment vAlignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLabelTTF *pRet = new CCLabelTTF();
|
2012-08-01 13:24:23 +08:00
|
|
|
if(pRet && pRet->initWithString(string, fontName, fontSize, dimensions, hAlignment, vAlignment))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCLabelTTF::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return this->initWithString("", "Helvetica", 12);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2012-08-01 13:24:23 +08:00
|
|
|
bool CCLabelTTF::initWithString(const char *label, const char *fontName, float fontSize,
|
|
|
|
const CCSize& dimensions, CCTextAlignment alignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-08-01 13:24:23 +08:00
|
|
|
return this->initWithString(label, fontName, fontSize, dimensions, alignment, kCCVerticalTextAlignmentTop);
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCLabelTTF::initWithString(const char *label, const char *fontName, float fontSize)
|
|
|
|
{
|
2012-08-01 13:24:23 +08:00
|
|
|
return this->initWithString(label, fontName, fontSize,
|
|
|
|
CCSizeZero, kCCTextAlignmentLeft, kCCVerticalTextAlignmentTop);
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:24:23 +08:00
|
|
|
bool CCLabelTTF::initWithString(const char *string, const char *fontName, float fontSize,
|
|
|
|
const cocos2d::CCSize &dimensions, CCTextAlignment hAlignment,
|
|
|
|
CCVerticalTextAlignment vAlignment)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (CCSprite::init())
|
|
|
|
{
|
|
|
|
// shader program
|
2012-06-11 14:36:25 +08:00
|
|
|
this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
|
|
|
|
|
|
|
|
m_tDimensions = CCSizeMake(dimensions.width, dimensions.height);
|
2013-04-26 09:22:26 +08:00
|
|
|
m_hAlignment = hAlignment;
|
|
|
|
m_vAlignment = vAlignment;
|
|
|
|
m_pFontName = new std::string(fontName);
|
|
|
|
m_fFontSize = fontSize;
|
2012-06-11 14:36:25 +08:00
|
|
|
|
|
|
|
this->setString(string);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2013-05-08 07:59:45 +08:00
|
|
|
bool CCLabelTTF::initWithStringAndTextDefinition(const char *string, ccFontDefinition &textDefinition)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
|
|
|
if (CCSprite::init())
|
|
|
|
{
|
|
|
|
// shader program
|
|
|
|
this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
|
|
|
|
|
|
|
|
// prepare everythin needed to render the label
|
2013-05-08 07:59:45 +08:00
|
|
|
_updateWithTextDefinition(textDefinition, false);
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
// set the string
|
|
|
|
this->setString(string);
|
|
|
|
|
|
|
|
//
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
void CCLabelTTF::setString(const char *string)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
CCAssert(string != NULL, "Invalid string");
|
|
|
|
|
|
|
|
if (m_string.compare(string))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
m_string = string;
|
|
|
|
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
const char* CCLabelTTF::getString(void)
|
|
|
|
{
|
|
|
|
return m_string.c_str();
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
const char* CCLabelTTF::description()
|
|
|
|
{
|
2012-06-14 16:05:58 +08:00
|
|
|
return CCString::createWithFormat("<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_pFontName->c_str(), m_fFontSize)->getCString();
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
CCTextAlignment CCLabelTTF::getHorizontalAlignment()
|
|
|
|
{
|
|
|
|
return m_hAlignment;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
|
|
|
void CCLabelTTF::setHorizontalAlignment(CCTextAlignment alignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
if (alignment != m_hAlignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
m_hAlignment = alignment;
|
|
|
|
|
|
|
|
// Force update
|
|
|
|
if (m_string.size() > 0)
|
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCVerticalTextAlignment CCLabelTTF::getVerticalAlignment()
|
|
|
|
{
|
|
|
|
return m_vAlignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelTTF::setVerticalAlignment(CCVerticalTextAlignment verticalAlignment)
|
|
|
|
{
|
|
|
|
if (verticalAlignment != m_vAlignment)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
m_vAlignment = verticalAlignment;
|
|
|
|
|
|
|
|
// Force update
|
|
|
|
if (m_string.size() > 0)
|
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCSize CCLabelTTF::getDimensions()
|
|
|
|
{
|
|
|
|
return m_tDimensions;
|
|
|
|
}
|
|
|
|
|
2012-08-16 11:43:44 +08:00
|
|
|
void CCLabelTTF::setDimensions(const CCSize &dim)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
|
|
|
if (dim.width != m_tDimensions.width || dim.height != m_tDimensions.height)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
m_tDimensions = dim;
|
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// Force update
|
2012-06-11 14:36:25 +08:00
|
|
|
if (m_string.size() > 0)
|
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
2010-12-28 15:05:55 +08:00
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
float CCLabelTTF::getFontSize()
|
|
|
|
{
|
|
|
|
return m_fFontSize;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
void CCLabelTTF::setFontSize(float fontSize)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
if (m_fFontSize != fontSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-12 09:43:27 +08:00
|
|
|
m_fFontSize = fontSize;
|
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
// Force update
|
|
|
|
if (m_string.size() > 0)
|
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
const char* CCLabelTTF::getFontName()
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
return m_pFontName->c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelTTF::setFontName(const char *fontName)
|
|
|
|
{
|
|
|
|
if (m_pFontName->compare(fontName))
|
|
|
|
{
|
|
|
|
delete m_pFontName;
|
|
|
|
m_pFontName = new std::string(fontName);
|
|
|
|
|
|
|
|
// Force update
|
|
|
|
if (m_string.size() > 0)
|
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper
|
2012-11-14 18:05:15 +08:00
|
|
|
bool CCLabelTTF::updateTexture()
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-05-04 01:06:08 +08:00
|
|
|
CCTexture2D *tex;
|
|
|
|
tex = new CCTexture2D();
|
|
|
|
|
|
|
|
if (!tex)
|
|
|
|
return false;
|
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2012-08-16 11:43:44 +08:00
|
|
|
|
2013-05-11 05:15:02 +08:00
|
|
|
ccFontDefinition texDef = _prepareTextDefinition(true);
|
2013-05-14 02:35:52 +08:00
|
|
|
tex->initWithString( m_string.c_str(), &texDef );
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#else
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
tex->initWithString( 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);
|
|
|
|
|
|
|
|
#endif
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
// 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;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
void CCLabelTTF::enableShadow(CCSize &shadowOffset, float shadowOpacity, float shadowBlur, bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
bool valueChanged = false;
|
|
|
|
|
|
|
|
if (false == m_shadowEnabled)
|
|
|
|
{
|
|
|
|
m_shadowEnabled = true;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (m_shadowOffset.width != shadowOffset.width) || (m_shadowOffset.height!=shadowOffset.height) )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
m_shadowOffset.width = shadowOffset.width;
|
|
|
|
m_shadowOffset.height = shadowOffset.height;
|
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_shadowOpacity != shadowOpacity )
|
|
|
|
{
|
|
|
|
m_shadowOpacity = shadowOpacity;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
if (m_shadowBlur != shadowBlur)
|
|
|
|
{
|
|
|
|
m_shadowBlur = shadowBlur;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
if ( valueChanged && updateTexture )
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#else
|
2013-05-02 08:32:16 +08:00
|
|
|
CCAssert(false, "Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2013-04-26 09:22:26 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
void CCLabelTTF::disableShadow(bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
|
|
|
if (m_shadowEnabled)
|
|
|
|
{
|
|
|
|
m_shadowEnabled = false;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
if (updateTexture)
|
|
|
|
this->updateTexture();
|
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2013-05-02 08:32:16 +08:00
|
|
|
CCAssert(false, "Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2013-04-26 09:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
void CCLabelTTF::enableStroke(const ccColor3B &strokeColor, float strokeSize, bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
bool valueChanged = false;
|
|
|
|
|
|
|
|
if(m_strokeEnabled == false)
|
|
|
|
{
|
|
|
|
m_strokeEnabled = true;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (m_strokeColor.r != strokeColor.r) || (m_strokeColor.g != strokeColor.g) || (m_strokeColor.b != strokeColor.b) )
|
|
|
|
{
|
|
|
|
m_strokeColor = strokeColor;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_strokeSize!=strokeSize)
|
|
|
|
{
|
|
|
|
m_strokeSize = strokeSize;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
if ( valueChanged && updateTexture )
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#else
|
2013-05-02 08:32:16 +08:00
|
|
|
CCAssert(false, "Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2013-04-26 09:22:26 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
void CCLabelTTF::disableStroke(bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
|
|
|
if (m_strokeEnabled)
|
|
|
|
{
|
|
|
|
m_strokeEnabled = false;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
if (updateTexture)
|
|
|
|
this->updateTexture();
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2013-05-02 08:32:16 +08:00
|
|
|
CCAssert(false, "Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
|
|
|
|
2013-04-26 09:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
void CCLabelTTF::setFontFillColor(const ccColor3B &tintColor, bool updateTexture)
|
2013-05-01 07:36:14 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2013-05-07 05:27:54 +08:00
|
|
|
if (m_textFillColor.r != tintColor.r || m_textFillColor.g != tintColor.g || m_textFillColor.b != tintColor.b)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-05-07 05:27:54 +08:00
|
|
|
m_textFillColor = tintColor;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
if (updateTexture)
|
|
|
|
this->updateTexture();
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
#else
|
2013-05-02 08:32:16 +08:00
|
|
|
CCAssert(false, "Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2013-05-01 07:36:14 +08:00
|
|
|
}
|
|
|
|
|
2013-05-07 05:27:54 +08:00
|
|
|
void CCLabelTTF::setTextDefinition(ccFontDefinition *theDefinition)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
|
|
|
if (theDefinition)
|
|
|
|
{
|
|
|
|
_updateWithTextDefinition(*theDefinition, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 05:27:54 +08:00
|
|
|
ccFontDefinition *CCLabelTTF::getTextDefinition()
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-05-07 05:27:54 +08:00
|
|
|
ccFontDefinition *tempDefinition = new ccFontDefinition;
|
2013-05-11 05:15:02 +08:00
|
|
|
*tempDefinition = _prepareTextDefinition(false);
|
2013-05-04 01:06:08 +08:00
|
|
|
return tempDefinition;
|
|
|
|
}
|
|
|
|
|
2013-05-07 05:27:54 +08:00
|
|
|
void CCLabelTTF::_updateWithTextDefinition(ccFontDefinition & textDefinition, bool mustUpdateTexture)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
|
|
|
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
|
2013-05-07 05:27:54 +08:00
|
|
|
setFontFillColor(textDefinition.m_fontFillColor, false);
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
if (mustUpdateTexture)
|
|
|
|
updateTexture();
|
|
|
|
}
|
|
|
|
|
2013-05-11 05:15:02 +08:00
|
|
|
ccFontDefinition CCLabelTTF::_prepareTextDefinition(bool adjustForResolution)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-05-07 05:27:54 +08:00
|
|
|
ccFontDefinition texDef;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
2013-05-11 05:15:02 +08:00
|
|
|
if (adjustForResolution)
|
|
|
|
texDef.m_fontSize = m_fFontSize * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
else
|
|
|
|
texDef.m_fontSize = m_fFontSize;
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
texDef.m_fontName = *m_pFontName;
|
|
|
|
texDef.m_alignment = m_hAlignment;
|
|
|
|
texDef.m_vertAlignment = m_vAlignment;
|
|
|
|
|
2013-05-11 05:15:02 +08:00
|
|
|
|
|
|
|
if (adjustForResolution)
|
|
|
|
texDef.m_dimensions = CC_SIZE_POINTS_TO_PIXELS(m_tDimensions);
|
|
|
|
else
|
|
|
|
texDef.m_dimensions = m_tDimensions;
|
|
|
|
|
|
|
|
|
|
|
|
// stroke
|
2013-05-04 01:06:08 +08:00
|
|
|
if ( m_strokeEnabled )
|
|
|
|
{
|
|
|
|
texDef.m_stroke.m_strokeEnabled = true;
|
|
|
|
texDef.m_stroke.m_strokeColor = m_strokeColor;
|
2013-05-11 05:15:02 +08:00
|
|
|
|
|
|
|
if (adjustForResolution)
|
|
|
|
texDef.m_stroke.m_strokeSize = m_strokeSize * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
else
|
|
|
|
texDef.m_stroke.m_strokeSize = m_strokeSize;
|
|
|
|
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texDef.m_stroke.m_strokeEnabled = false;
|
|
|
|
}
|
|
|
|
|
2013-05-11 05:15:02 +08:00
|
|
|
|
|
|
|
// shadow
|
2013-05-04 01:06:08 +08:00
|
|
|
if ( m_shadowEnabled )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
texDef.m_shadow.m_shadowEnabled = true;
|
|
|
|
texDef.m_shadow.m_shadowBlur = m_shadowBlur;
|
|
|
|
texDef.m_shadow.m_shadowOpacity = m_shadowOpacity;
|
|
|
|
|
|
|
|
if (adjustForResolution)
|
|
|
|
texDef.m_shadow.m_shadowOffset = CC_SIZE_POINTS_TO_PIXELS(m_shadowOffset);
|
|
|
|
else
|
|
|
|
texDef.m_shadow.m_shadowOffset = m_shadowOffset;
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texDef.m_shadow.m_shadowEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// text tint
|
2013-05-07 05:27:54 +08:00
|
|
|
texDef.m_fontFillColor = m_textFillColor;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
return texDef;
|
|
|
|
}
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_END
|