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"
|
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("")
|
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-11 14:36:25 +08:00
|
|
|
CC_SAFE_DELETE(m_pFontName);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
CCLabelTTF * CCLabelTTF::labelWithString(const char *string, const char *fontName, float fontSize)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
return labelWithString(string, CCSizeZero, kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop, fontName, fontSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLabelTTF * CCLabelTTF::labelWithString(const char *string, const CCSize& dimensions, CCTextAlignment hAlignment, const char *fontName, float fontSize)
|
|
|
|
{
|
|
|
|
return labelWithString(string, dimensions, hAlignment, kCCVerticalTextAlignmentTop, fontName, fontSize);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
|
|
|
CCLabelTTF* CCLabelTTF::labelWithString(const char *string, const cocos2d::CCSize &dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLabelTTF *pRet = new CCLabelTTF();
|
2012-06-11 14:36:25 +08:00
|
|
|
if(pRet && pRet->initWithString(string, dimensions, hAlignment, vAlignment, fontName, fontSize))
|
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-03-16 13:42:53 +08:00
|
|
|
bool CCLabelTTF::initWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
|
|
|
|
{
|
2012-06-11 14:36:25 +08:00
|
|
|
return this->initWithString(label, dimensions, alignment, kCCVerticalTextAlignmentTop, fontName, fontSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCLabelTTF::initWithString(const char *label, const char *fontName, float fontSize)
|
|
|
|
{
|
|
|
|
return this->initWithString(label, CCSizeZero, kCCTextAlignmentLeft, kCCVerticalTextAlignmentTop, fontName, fontSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCLabelTTF::initWithString(const char *string, const cocos2d::CCSize &dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize)
|
|
|
|
{
|
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);
|
|
|
|
m_hAlignment = hAlignment;
|
|
|
|
m_vAlignment = vAlignment;
|
2012-03-16 13:42:53 +08:00
|
|
|
m_pFontName = new std::string(fontName);
|
2012-06-11 14:36:25 +08:00
|
|
|
m_fFontSize = fontSize;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return CCString::stringWithFormat("<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_pFontName->c_str(), m_fFontSize)->getCString();
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelTTF::setDimensions(CCSize &dim)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Force udpate
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
void CCLabelTTF::updateTexture()
|
|
|
|
{
|
|
|
|
CCTexture2D *tex;
|
|
|
|
if (m_tDimensions.width == 0 || m_tDimensions.height == 0)
|
|
|
|
{
|
|
|
|
tex = new CCTexture2D();
|
|
|
|
tex->initWithString(m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tex = new CCTexture2D();
|
|
|
|
tex->initWithString(m_string.c_str(),
|
|
|
|
CC_SIZE_POINTS_TO_PIXELS(m_tDimensions),
|
|
|
|
m_hAlignment,
|
|
|
|
m_vAlignment,
|
|
|
|
m_pFontName->c_str(),
|
|
|
|
m_fFontSize * CC_CONTENT_SCALE_FACTOR());
|
|
|
|
}
|
|
|
|
|
|
|
|
// iPad ?
|
|
|
|
//if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
|
|
|
|
if (CCApplication::sharedApplication().isIpad())
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->setTexture(tex);
|
|
|
|
tex->release();
|
|
|
|
|
|
|
|
CCRect rect = CCRectZero;
|
|
|
|
rect.size = m_pobTexture->getContentSize();
|
|
|
|
this->setTextureRect(rect);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|