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"
|
2010-12-17 23:44:19 +08:00
|
|
|
|
|
|
|
namespace cocos2d{
|
2010-08-11 18:09:10 +08:00
|
|
|
//
|
2010-12-24 17:07:31 +08:00
|
|
|
//CCLabelTTF
|
2010-08-11 18:09:10 +08:00
|
|
|
//
|
2011-03-15 11:59:45 +08:00
|
|
|
CCLabelTTF::CCLabelTTF()
|
|
|
|
: m_pFontName(NULL)
|
|
|
|
, m_pString(NULL)
|
2011-04-01 16:06:53 +08:00
|
|
|
, m_fFontSize(0.0)
|
|
|
|
, m_eAlignment(CCTextAlignmentCenter)
|
2011-03-15 11:59:45 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLabelTTF::~CCLabelTTF()
|
|
|
|
{
|
2011-04-01 16:06:53 +08:00
|
|
|
CC_SAFE_DELETE(m_pFontName);
|
|
|
|
CC_SAFE_DELETE(m_pString);
|
2011-03-15 11:59:45 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
CCLabelTTF * CCLabelTTF::labelWithString(const char *label, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
2010-12-24 17:07:31 +08:00
|
|
|
CCLabelTTF *pRet = new CCLabelTTF();
|
2010-08-19 14:09:40 +08:00
|
|
|
if(pRet && pRet->initWithString(label, dimensions, alignment, fontName, fontSize))
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2011-04-19 09:32:33 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
2010-08-11 18:09:10 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
CCLabelTTF * CCLabelTTF::labelWithString(const char *label, const char *fontName, float fontSize)
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
2010-12-24 17:07:31 +08:00
|
|
|
CCLabelTTF *pRet = new CCLabelTTF();
|
2010-08-19 14:09:40 +08:00
|
|
|
if(pRet && pRet->initWithString(label, fontName, fontSize))
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2011-04-19 09:32:33 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
2010-08-11 18:09:10 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
bool CCLabelTTF::initWithString(const char *label, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
assert(label != NULL);
|
2010-12-17 23:44:19 +08:00
|
|
|
if (CCSprite::init())
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
m_tDimensions = CCSizeMake( dimensions.width * CC_CONTENT_SCALE_FACTOR(), dimensions.height * CC_CONTENT_SCALE_FACTOR() );
|
2010-12-17 23:44:19 +08:00
|
|
|
m_eAlignment = alignment;
|
2011-03-15 11:59:45 +08:00
|
|
|
|
|
|
|
if (m_pFontName)
|
|
|
|
{
|
|
|
|
delete m_pFontName;
|
|
|
|
m_pFontName = NULL;
|
|
|
|
}
|
|
|
|
m_pFontName = new std::string(fontName);
|
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR();
|
2010-12-17 23:44:19 +08:00
|
|
|
this->setString(label);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
bool CCLabelTTF::initWithString(const char *label, const char *fontName, float fontSize)
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
assert(label != NULL);
|
2010-12-17 23:44:19 +08:00
|
|
|
if (CCSprite::init())
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
m_tDimensions = CCSizeZero;
|
2011-03-15 11:59:45 +08:00
|
|
|
|
|
|
|
if (m_pFontName)
|
|
|
|
{
|
|
|
|
delete m_pFontName;
|
|
|
|
m_pFontName = NULL;
|
|
|
|
}
|
|
|
|
m_pFontName = new std::string(fontName);
|
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR();
|
2010-12-17 23:44:19 +08:00
|
|
|
this->setString(label);
|
|
|
|
return true;
|
|
|
|
}
|
2010-09-04 12:02:52 +08:00
|
|
|
return false;
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
void CCLabelTTF::setString(const char *label)
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
2011-03-15 11:59:45 +08:00
|
|
|
if (m_pString)
|
|
|
|
{
|
|
|
|
delete m_pString;
|
|
|
|
m_pString = NULL;
|
|
|
|
}
|
|
|
|
m_pString = new std::string(label);
|
2010-09-14 10:26:19 +08:00
|
|
|
|
2010-08-11 18:09:10 +08:00
|
|
|
CCTexture2D *texture;
|
2011-03-07 17:11:57 +08:00
|
|
|
if( CCSize::CCSizeEqualToSize( m_tDimensions, CCSizeZero ) )
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
|
|
|
texture = new CCTexture2D();
|
2011-03-14 18:29:15 +08:00
|
|
|
texture->initWithString(label, m_pFontName->c_str(), m_fFontSize);
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texture = new CCTexture2D();
|
2011-03-14 18:29:15 +08:00
|
|
|
texture->initWithString(label, m_tDimensions, m_eAlignment, m_pFontName->c_str(), m_fFontSize);
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
|
|
|
this->setTexture(texture);
|
|
|
|
texture->release();
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCRect rect = CCRectZero;
|
2010-12-24 17:07:31 +08:00
|
|
|
rect.size = m_pobTexture->getContentSize();
|
|
|
|
this->setTextureRect(rect);
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
|
|
|
|
2010-12-28 15:05:55 +08:00
|
|
|
const char* CCLabelTTF::getString(void)
|
|
|
|
{
|
2011-03-14 18:29:15 +08:00
|
|
|
return m_pString->c_str();
|
2010-12-28 15:05:55 +08:00
|
|
|
}
|
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
char * CCLabelTTF::description()
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
2010-12-17 23:44:19 +08:00
|
|
|
char *ret = new char[100] ;
|
2011-03-14 18:29:15 +08:00
|
|
|
sprintf(ret, "<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_pFontName->c_str(), m_fFontSize);
|
2010-08-11 18:09:10 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2010-12-17 23:44:19 +08:00
|
|
|
}// namespace cocos2d
|