axmol/cocos2dx/label_nodes/CCLabelTTF.cpp

301 lines
7.7 KiB
C++

/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
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.
****************************************************************************/
#include "CCLabelTTF.h"
#include "CCDirector.h"
#include "CCGLProgram.h"
#include "CCShaderCache.h"
#include "CCApplication.h"
NS_CC_BEGIN
#if CC_USE_LA88_LABELS
#define SHADER_PROGRAM kCCShader_PositionTextureColor
#else
#define SHADER_PROGRAM kCCShader_PositionTextureA8Color
#endif
//
//CCLabelTTF
//
CCLabelTTF::CCLabelTTF()
: m_hAlignment(kCCTextAlignmentCenter)
, m_vAlignment(kCCVerticalTextAlignmentTop)
, m_pFontName(NULL)
, m_fFontSize(0.0)
, m_string("")
{
}
CCLabelTTF::~CCLabelTTF()
{
CC_SAFE_DELETE(m_pFontName);
}
CCLabelTTF * CCLabelTTF::node()
{
CCLabelTTF * pRet = new CCLabelTTF();
if (pRet && pRet->init())
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
CCLabelTTF * CCLabelTTF::labelWithString(const char *string, const char *fontName, float fontSize)
{
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);
}
CCLabelTTF* CCLabelTTF::labelWithString(const char *string, const cocos2d::CCSize &dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize)
{
CCLabelTTF *pRet = new CCLabelTTF();
if(pRet && pRet->initWithString(string, dimensions, hAlignment, vAlignment, fontName, fontSize))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool CCLabelTTF::init()
{
return this->initWithString("", "Helvetica", 12);
}
bool CCLabelTTF::initWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
{
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)
{
if (CCSprite::init())
{
// shader program
this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
m_tDimensions = CCSizeMake(dimensions.width, dimensions.height);
m_hAlignment = hAlignment;
m_vAlignment = vAlignment;
m_pFontName = new std::string(fontName);
m_fFontSize = fontSize;
this->setString(string);
return true;
}
return false;
}
void CCLabelTTF::setString(const char *string)
{
CCAssert(string != NULL, "Invalid string");
if (m_string.compare(string))
{
m_string = string;
this->updateTexture();
}
}
const char* CCLabelTTF::getString(void)
{
return m_string.c_str();
}
const char* CCLabelTTF::description()
{
return CCString::stringWithFormat("<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_pFontName->c_str(), m_fFontSize)->getCString();
}
CCTextAlignment CCLabelTTF::getHorizontalAlignment()
{
return m_hAlignment;
}
void CCLabelTTF::setHorizontalAlignment(CCTextAlignment alignment)
{
if (alignment != m_hAlignment)
{
m_hAlignment = alignment;
// Force update
if (m_string.size() > 0)
{
this->updateTexture();
}
}
}
CCVerticalTextAlignment CCLabelTTF::getVerticalAlignment()
{
return m_vAlignment;
}
void CCLabelTTF::setVerticalAlignment(CCVerticalTextAlignment verticalAlignment)
{
if (verticalAlignment != m_vAlignment)
{
m_vAlignment = verticalAlignment;
// Force update
if (m_string.size() > 0)
{
this->updateTexture();
}
}
}
CCSize CCLabelTTF::getDimensions()
{
return m_tDimensions;
}
void CCLabelTTF::setDimensions(CCSize &dim)
{
if (dim.width != m_tDimensions.width || dim.height != m_tDimensions.height)
{
m_tDimensions = dim;
// Force udpate
if (m_string.size() > 0)
{
this->updateTexture();
}
}
}
float CCLabelTTF::getFontSize()
{
return m_fFontSize;
}
void CCLabelTTF::setFontSize(float fontSize)
{
if (m_fFontSize != fontSize)
{
m_fFontSize = fontSize;
// Force update
if (m_string.size() > 0)
{
this->updateTexture();
}
}
}
const char* CCLabelTTF::getFontName()
{
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);
}
NS_CC_END