2010-12-17 23:44:19 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
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-03-16 13:42:53 +08:00
|
|
|
#include "CCGLProgram.h"
|
|
|
|
#include "CCShaderCache.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()
|
|
|
|
: m_eAlignment(CCTextAlignmentCenter)
|
|
|
|
, m_fFontSize(0.0)
|
|
|
|
{
|
|
|
|
}
|
2011-03-15 11:59:45 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CCLabelTTF::~CCLabelTTF()
|
|
|
|
{
|
2012-06-06 08:12:41 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2012-05-31 06:34:50 +08:00
|
|
|
CCLabelTTF * CCLabelTTF::node()
|
|
|
|
{
|
|
|
|
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
|
|
|
CCLabelTTF * CCLabelTTF::labelWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLabelTTF *pRet = new CCLabelTTF();
|
|
|
|
if(pRet && pRet->initWithString(label, dimensions, alignment, fontName, fontSize))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
CCLabelTTF * CCLabelTTF::labelWithString(const char *label, const char *fontName, float fontSize)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLabelTTF *pRet = new CCLabelTTF();
|
|
|
|
if(pRet && pRet->initWithString(label, fontName, fontSize))
|
|
|
|
{
|
|
|
|
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-03-16 13:42:53 +08:00
|
|
|
bool CCLabelTTF::initWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(label != NULL, "");
|
|
|
|
if (CCSprite::init())
|
|
|
|
{
|
|
|
|
// shader program
|
|
|
|
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tDimensions = CCSizeMake( dimensions.width * CC_CONTENT_SCALE_FACTOR(), dimensions.height * CC_CONTENT_SCALE_FACTOR() );
|
|
|
|
m_eAlignment = alignment;
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-06-06 08:12:41 +08:00
|
|
|
m_sFontName = fontName;
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
this->setString(label);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
bool CCLabelTTF::initWithString(const char *label, const char *fontName, float fontSize)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(label != NULL, "");
|
|
|
|
if (CCSprite::init())
|
|
|
|
{
|
|
|
|
// shader program
|
|
|
|
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tDimensions = CCSizeZero;
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-06-06 08:12:41 +08:00
|
|
|
m_sFontName = fontName;
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
this->setString(label);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-02 06:11:16 +08:00
|
|
|
const char * CCLabelTTF::getFontName()
|
|
|
|
{
|
2012-06-06 08:12:41 +08:00
|
|
|
return this->m_sFontName.c_str();
|
2012-06-02 06:11:16 +08:00
|
|
|
}
|
2012-05-31 06:34:50 +08:00
|
|
|
void CCLabelTTF::setFontName(const char *fontName)
|
|
|
|
{
|
2012-06-06 08:12:41 +08:00
|
|
|
if(strcmp(this->m_sFontName.c_str(), fontName) != 0)
|
2012-05-31 06:34:50 +08:00
|
|
|
{
|
2012-06-06 08:12:41 +08:00
|
|
|
this->m_sFontName = fontName;
|
2012-05-31 06:34:50 +08:00
|
|
|
|
2012-06-06 08:12:41 +08:00
|
|
|
this->setString(this->m_sString.c_str());
|
2012-05-31 06:34:50 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-02 06:11:16 +08:00
|
|
|
float CCLabelTTF::getFontSize()
|
|
|
|
{
|
|
|
|
return this->m_fFontSize;
|
|
|
|
}
|
2012-05-31 06:34:50 +08:00
|
|
|
void CCLabelTTF::setFontSize(float fontSize)
|
|
|
|
{
|
|
|
|
if(this->m_fFontSize != fontSize)
|
|
|
|
{
|
|
|
|
this->m_fFontSize = fontSize;
|
|
|
|
|
2012-06-06 08:12:41 +08:00
|
|
|
this->setString(this->m_sString.c_str());
|
2012-05-31 06:34:50 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-31 07:49:51 +08:00
|
|
|
void CCLabelTTF::setDimensions(CCSize dimensions)
|
|
|
|
{
|
|
|
|
if(this->m_tDimensions.width != dimensions.width || this->m_tDimensions.height != dimensions.height)
|
|
|
|
{
|
|
|
|
this->m_tDimensions = dimensions;
|
|
|
|
|
2012-06-06 08:12:41 +08:00
|
|
|
this->setString(this->m_sString.c_str());
|
2012-05-31 07:49:51 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-31 06:34:50 +08:00
|
|
|
void CCLabelTTF::setHorizontalAlignment(CCTextAlignment pCCTextAlignment)
|
|
|
|
{
|
|
|
|
if(this->m_eAlignment != pCCTextAlignment)
|
|
|
|
{
|
|
|
|
this->m_eAlignment = pCCTextAlignment;
|
|
|
|
|
2012-06-06 08:12:41 +08:00
|
|
|
this->setString(this->m_sString.c_str());
|
2012-05-31 06:34:50 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
void CCLabelTTF::setString(const char *label)
|
|
|
|
{
|
2012-06-06 08:12:41 +08:00
|
|
|
m_sString = label;
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTexture2D *texture;
|
|
|
|
if( CCSize::CCSizeEqualToSize( m_tDimensions, CCSizeZero ) )
|
|
|
|
{
|
|
|
|
texture = new CCTexture2D();
|
2012-06-06 08:12:41 +08:00
|
|
|
texture->initWithString(label, m_sFontName.c_str(), m_fFontSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texture = new CCTexture2D();
|
2012-06-06 08:12:41 +08:00
|
|
|
texture->initWithString(label, m_tDimensions, m_eAlignment, m_sFontName.c_str(), m_fFontSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-28 15:05:55 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
// TODO
|
|
|
|
// #ifdef __CC_PLATFORM_IOS
|
2012-04-19 14:35:52 +08:00
|
|
|
// // iPad ?
|
|
|
|
// if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
|
|
|
|
// if( CC_CONTENT_SCALE_FACTOR() == 2 )
|
|
|
|
// [tex setResolutionType:kCCResolutioniPadRetinaDisplay];
|
|
|
|
// else
|
|
|
|
// [tex setResolutionType:kCCResolutioniPad];
|
|
|
|
// }
|
|
|
|
// // iPhone ?
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// if( CC_CONTENT_SCALE_FACTOR() == 2 )
|
|
|
|
// [tex setResolutionType:kCCResolutioniPhoneRetinaDisplay];
|
|
|
|
// else
|
|
|
|
// [tex setResolutionType:kCCResolutioniPhone];
|
|
|
|
// }
|
2012-03-16 13:42:53 +08:00
|
|
|
// #end
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setTexture(texture);
|
|
|
|
texture->release();
|
2012-03-16 13:42:53 +08:00
|
|
|
|
|
|
|
CCRect rect = CCRectZero;
|
2012-04-19 14:35:52 +08:00
|
|
|
rect.size = m_pobTexture->getContentSize();
|
|
|
|
this->setTextureRect(rect);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCLabelTTF::getString(void)
|
|
|
|
{
|
2012-06-06 08:12:41 +08:00
|
|
|
return m_sString.c_str();
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
const char* CCLabelTTF::description()
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-06-06 08:12:41 +08:00
|
|
|
return CCString::stringWithFormat("<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_sFontName.c_str(), m_fFontSize)->getCString();
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|