axmol/cocos2dx/label_nodes/CCLabelTTF.cpp

568 lines
15 KiB
C++
Raw Normal View History

2010-12-17 23:44:19 +08:00
/****************************************************************************
Copyright (c) 2010-2012 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.
****************************************************************************/
#include "CCLabelTTF.h"
#include "CCDirector.h"
#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
NS_CC_BEGIN
#if CC_USE_LA88_LABELS
#define SHADER_PROGRAM kShader_PositionTextureColor
#else
#define SHADER_PROGRAM kShader_PositionTextureA8Color
#endif
//
//CCLabelTTF
//
LabelTTF::LabelTTF()
: _alignment(kTextAlignmentCenter)
, _vAlignment(kVerticalTextAlignmentTop)
, _fontName(NULL)
, _fontSize(0.0)
, _string("")
, _shadowEnabled(false)
, _strokeEnabled(false)
, _textFillColor(ccWHITE)
{
}
LabelTTF::~LabelTTF()
{
CC_SAFE_DELETE(_fontName);
}
LabelTTF * LabelTTF::create()
{
LabelTTF * pRet = new LabelTTF();
if (pRet && pRet->init())
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
LabelTTF * LabelTTF::create(const char *string, const char *fontName, float fontSize)
{
return LabelTTF::create(string, fontName, fontSize,
SizeZero, kTextAlignmentCenter, kVerticalTextAlignmentTop);
2012-06-11 14:36:25 +08:00
}
LabelTTF * LabelTTF::create(const char *string, const char *fontName, float fontSize,
const Size& dimensions, TextAlignment hAlignment)
2012-06-11 14:36:25 +08:00
{
return LabelTTF::create(string, fontName, fontSize, dimensions, hAlignment, kVerticalTextAlignmentTop);
}
2012-06-11 14:36:25 +08:00
LabelTTF* LabelTTF::create(const char *string, const char *fontName, float fontSize,
const Size &dimensions, TextAlignment hAlignment,
VerticalTextAlignment vAlignment)
{
LabelTTF *pRet = new LabelTTF();
if(pRet && pRet->initWithString(string, fontName, fontSize, dimensions, hAlignment, vAlignment))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
LabelTTF * LabelTTF::createWithFontDefinition(const char *string, ccFontDefinition &textDefinition)
{
LabelTTF *pRet = new LabelTTF();
if(pRet && pRet->initWithStringAndTextDefinition(string, textDefinition))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool LabelTTF::init()
{
return this->initWithString("", "Helvetica", 12);
}
bool LabelTTF::initWithString(const char *label, const char *fontName, float fontSize,
const Size& dimensions, TextAlignment alignment)
{
return this->initWithString(label, fontName, fontSize, dimensions, alignment, kVerticalTextAlignmentTop);
2012-06-11 14:36:25 +08:00
}
bool LabelTTF::initWithString(const char *label, const char *fontName, float fontSize)
2012-06-11 14:36:25 +08:00
{
return this->initWithString(label, fontName, fontSize,
SizeZero, kTextAlignmentLeft, kVerticalTextAlignmentTop);
2012-06-11 14:36:25 +08:00
}
bool LabelTTF::initWithString(const char *string, const char *fontName, float fontSize,
const cocos2d::Size &dimensions, TextAlignment hAlignment,
VerticalTextAlignment vAlignment)
2012-06-11 14:36:25 +08:00
{
if (Sprite::init())
{
// shader program
this->setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
2012-06-11 14:36:25 +08:00
_dimensions = CCSizeMake(dimensions.width, dimensions.height);
_alignment = hAlignment;
_vAlignment = vAlignment;
_fontName = new std::string(fontName);
_fontSize = fontSize;
2012-06-11 14:36:25 +08:00
this->setString(string);
return true;
}
2012-06-11 14:36:25 +08:00
return false;
}
2012-06-11 14:36:25 +08:00
bool LabelTTF::initWithStringAndTextDefinition(const char *string, ccFontDefinition &textDefinition)
2013-05-04 01:06:08 +08:00
{
if (Sprite::init())
2013-05-04 01:06:08 +08:00
{
// shader program
this->setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
2013-05-04 01:06:08 +08:00
// prepare everythin needed to render the label
_updateWithTextDefinition(textDefinition, false);
2013-05-04 01:06:08 +08:00
// set the string
this->setString(string);
//
return true;
}
else
{
return false;
}
}
void LabelTTF::setString(const char *string)
{
2012-06-11 14:36:25 +08:00
CCAssert(string != NULL, "Invalid string");
if (_string.compare(string))
{
_string = string;
2012-06-11 14:36:25 +08:00
this->updateTexture();
}
}
const char* LabelTTF::getString(void) const
2012-06-11 14:36:25 +08:00
{
return _string.c_str();
2012-06-11 14:36:25 +08:00
}
const char* LabelTTF::description()
2012-06-11 14:36:25 +08:00
{
return String::createWithFormat("<LabelTTF | FontName = %s, FontSize = %.1f>", _fontName->c_str(), _fontSize)->getCString();
2012-06-11 14:36:25 +08:00
}
TextAlignment LabelTTF::getHorizontalAlignment() const
2012-06-11 14:36:25 +08:00
{
return _alignment;
}
2012-06-11 14:36:25 +08:00
void LabelTTF::setHorizontalAlignment(TextAlignment alignment)
{
if (alignment != _alignment)
{
_alignment = alignment;
2012-06-11 14:36:25 +08:00
// Force update
if (_string.size() > 0)
2012-06-11 14:36:25 +08:00
{
this->updateTexture();
}
}
2012-06-11 14:36:25 +08:00
}
VerticalTextAlignment LabelTTF::getVerticalAlignment() const
2012-06-11 14:36:25 +08:00
{
return _vAlignment;
2012-06-11 14:36:25 +08:00
}
void LabelTTF::setVerticalAlignment(VerticalTextAlignment verticalAlignment)
2012-06-11 14:36:25 +08:00
{
if (verticalAlignment != _vAlignment)
{
_vAlignment = verticalAlignment;
2012-06-11 14:36:25 +08:00
// Force update
if (_string.size() > 0)
2012-06-11 14:36:25 +08:00
{
this->updateTexture();
}
}
2012-06-11 14:36:25 +08:00
}
const Size& LabelTTF::getDimensions() const
2012-06-11 14:36:25 +08:00
{
return _dimensions;
2012-06-11 14:36:25 +08:00
}
void LabelTTF::setDimensions(const Size &dim)
2012-06-11 14:36:25 +08:00
{
if (dim.width != _dimensions.width || dim.height != _dimensions.height)
{
_dimensions = dim;
2012-06-11 14:36:25 +08:00
// Force update
if (_string.size() > 0)
2012-06-11 14:36:25 +08:00
{
this->updateTexture();
}
}
2012-06-11 14:36:25 +08:00
}
2010-12-28 15:05:55 +08:00
float LabelTTF::getFontSize() const
2012-06-11 14:36:25 +08:00
{
return _fontSize;
}
void LabelTTF::setFontSize(float fontSize)
{
if (_fontSize != fontSize)
{
_fontSize = fontSize;
2012-06-12 09:43:27 +08:00
2012-06-11 14:36:25 +08:00
// Force update
if (_string.size() > 0)
2012-06-11 14:36:25 +08:00
{
this->updateTexture();
}
}
}
const char* LabelTTF::getFontName() const
{
return _fontName->c_str();
2012-06-11 14:36:25 +08:00
}
void LabelTTF::setFontName(const char *fontName)
2012-06-11 14:36:25 +08:00
{
if (_fontName->compare(fontName))
2012-06-11 14:36:25 +08:00
{
delete _fontName;
_fontName = new std::string(fontName);
2012-06-11 14:36:25 +08:00
// Force update
if (_string.size() > 0)
2012-06-11 14:36:25 +08:00
{
this->updateTexture();
}
}
}
// Helper
bool LabelTTF::updateTexture()
2012-06-11 14:36:25 +08:00
{
Texture2D *tex;
tex = new Texture2D();
2013-05-04 01:06:08 +08:00
if (!tex)
return false;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
ccFontDefinition texDef = _prepareTextDefinition(true);
tex->initWithString( _string.c_str(), &texDef );
#else
tex->initWithString( _string.c_str(),
_fontName->c_str(),
_fontSize * CC_CONTENT_SCALE_FACTOR(),
CC_SIZE_POINTS_TO_PIXELS(_dimensions),
_alignment,
_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
Rect rect =RectZero;
rect.size = _texture->getContentSize();
2013-05-04 01:06:08 +08:00
this->setTextureRect(rect);
//ok
return true;
}
void LabelTTF::enableShadow(const Size &shadowOffset, float shadowOpacity, float shadowBlur, bool updateTexture)
2013-04-26 09:22:26 +08:00
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
2013-04-26 09:22:26 +08:00
bool valueChanged = false;
if (false == _shadowEnabled)
{
_shadowEnabled = true;
valueChanged = true;
}
if ( (_shadowOffset.width != shadowOffset.width) || (_shadowOffset.height!=shadowOffset.height) )
{
_shadowOffset.width = shadowOffset.width;
_shadowOffset.height = shadowOffset.height;
valueChanged = true;
}
if (_shadowOpacity != shadowOpacity )
{
_shadowOpacity = shadowOpacity;
valueChanged = true;
}
2013-04-26 09:22:26 +08:00
if (_shadowBlur != shadowBlur)
{
_shadowBlur = shadowBlur;
valueChanged = true;
}
2013-05-04 01:06:08 +08:00
if ( valueChanged && updateTexture )
{
this->updateTexture();
}
2013-04-26 09:22:26 +08:00
#else
CCLOGERROR("Currently only supported on iOS and Android!");
#endif
2013-04-26 09:22:26 +08:00
}
void LabelTTF::disableShadow(bool updateTexture)
2013-04-26 09:22:26 +08:00
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
if (_shadowEnabled)
{
_shadowEnabled = false;
2013-05-04 01:06:08 +08:00
if (updateTexture)
this->updateTexture();
}
#else
CCLOGERROR("Currently only supported on iOS and Android!");
#endif
2013-04-26 09:22:26 +08:00
}
void LabelTTF::enableStroke(const ccColor3B &strokeColor, float strokeSize, bool updateTexture)
2013-04-26 09:22:26 +08:00
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
2013-04-26 09:22:26 +08:00
bool valueChanged = false;
if(_strokeEnabled == false)
{
_strokeEnabled = true;
valueChanged = true;
}
if ( (_strokeColor.r != strokeColor.r) || (_strokeColor.g != strokeColor.g) || (_strokeColor.b != strokeColor.b) )
{
_strokeColor = strokeColor;
valueChanged = true;
}
if (_strokeSize!=strokeSize)
{
_strokeSize = strokeSize;
valueChanged = true;
}
2013-05-04 01:06:08 +08:00
if ( valueChanged && updateTexture )
{
this->updateTexture();
}
2013-04-26 09:22:26 +08:00
#else
CCLOGERROR("Currently only supported on iOS and Android!");
#endif
2013-04-26 09:22:26 +08:00
}
void LabelTTF::disableStroke(bool updateTexture)
2013-04-26 09:22:26 +08:00
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
if (_strokeEnabled)
{
_strokeEnabled = false;
2013-05-04 01:06:08 +08:00
if (updateTexture)
this->updateTexture();
}
#else
CCLOGERROR("Currently only supported on iOS and Android!");
#endif
2013-04-26 09:22:26 +08:00
}
void LabelTTF::setFontFillColor(const ccColor3B &tintColor, bool updateTexture)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
if (_textFillColor.r != tintColor.r || _textFillColor.g != tintColor.g || _textFillColor.b != tintColor.b)
{
_textFillColor = tintColor;
2013-05-04 01:06:08 +08:00
if (updateTexture)
this->updateTexture();
}
#else
CCLOGERROR("Currently only supported on iOS and Android!");
#endif
}
void LabelTTF::setTextDefinition(ccFontDefinition *theDefinition)
2013-05-04 01:06:08 +08:00
{
if (theDefinition)
{
_updateWithTextDefinition(*theDefinition, true);
}
}
ccFontDefinition *LabelTTF::getTextDefinition()
2013-05-04 01:06:08 +08:00
{
2013-05-07 05:27:54 +08:00
ccFontDefinition *tempDefinition = new ccFontDefinition;
*tempDefinition = _prepareTextDefinition(false);
2013-05-04 01:06:08 +08:00
return tempDefinition;
}
void LabelTTF::_updateWithTextDefinition(ccFontDefinition & textDefinition, bool mustUpdateTexture)
2013-05-04 01:06:08 +08:00
{
_dimensions = CCSizeMake(textDefinition._dimensions.width, textDefinition._dimensions.height);
_alignment = textDefinition._alignment;
_vAlignment = textDefinition._vertAlignment;
2013-05-04 01:06:08 +08:00
_fontName = new std::string(textDefinition._fontName);
_fontSize = textDefinition._fontSize;
2013-05-04 01:06:08 +08:00
// shadow
if ( textDefinition._shadow._shadowEnabled )
2013-05-04 01:06:08 +08:00
{
enableShadow(textDefinition._shadow._shadowOffset, textDefinition._shadow._shadowOpacity, textDefinition._shadow._shadowBlur, false);
2013-05-04 01:06:08 +08:00
}
// stroke
if ( textDefinition._stroke._strokeEnabled )
2013-05-04 01:06:08 +08:00
{
enableStroke(textDefinition._stroke._strokeColor, textDefinition._stroke._strokeSize, false);
2013-05-04 01:06:08 +08:00
}
// fill color
setFontFillColor(textDefinition._fontFillColor, false);
2013-05-04 01:06:08 +08:00
if (mustUpdateTexture)
updateTexture();
}
ccFontDefinition LabelTTF::_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
if (adjustForResolution)
texDef._fontSize = _fontSize * CC_CONTENT_SCALE_FACTOR();
else
texDef._fontSize = _fontSize;
texDef._fontName = *_fontName;
texDef._alignment = _alignment;
texDef._vertAlignment = _vAlignment;
2013-05-04 01:06:08 +08:00
if (adjustForResolution)
texDef._dimensions = CC_SIZE_POINTS_TO_PIXELS(_dimensions);
else
texDef._dimensions = _dimensions;
// stroke
if ( _strokeEnabled )
2013-05-04 01:06:08 +08:00
{
texDef._stroke._strokeEnabled = true;
texDef._stroke._strokeColor = _strokeColor;
if (adjustForResolution)
texDef._stroke._strokeSize = _strokeSize * CC_CONTENT_SCALE_FACTOR();
else
texDef._stroke._strokeSize = _strokeSize;
2013-05-04 01:06:08 +08:00
}
else
{
texDef._stroke._strokeEnabled = false;
2013-05-04 01:06:08 +08:00
}
// shadow
if ( _shadowEnabled )
2013-05-04 01:06:08 +08:00
{
texDef._shadow._shadowEnabled = true;
texDef._shadow._shadowBlur = _shadowBlur;
texDef._shadow._shadowOpacity = _shadowOpacity;
if (adjustForResolution)
texDef._shadow._shadowOffset = CC_SIZE_POINTS_TO_PIXELS(_shadowOffset);
else
texDef._shadow._shadowOffset = _shadowOffset;
2013-05-04 01:06:08 +08:00
}
else
{
texDef._shadow._shadowEnabled = false;
2013-05-04 01:06:08 +08:00
}
// text tint
texDef._fontFillColor = _textFillColor;
2013-05-04 01:06:08 +08:00
return texDef;
}
NS_CC_END