2010-12-17 23:44:19 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
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"
|
2014-03-12 10:19:33 +08:00
|
|
|
#include "CCLabel.h"
|
|
|
|
#include "CCString.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
|
|
|
|
2014-03-28 14:18:01 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (push)
|
|
|
|
#pragma warning (disable: 4996)
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF::LabelTTF()
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel = Label::create();
|
2014-03-25 09:58:16 +08:00
|
|
|
_renderLabel->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
|
2014-03-12 10:19:33 +08:00
|
|
|
this->addChild(_renderLabel);
|
|
|
|
this->setAnchorPoint(Point::ANCHOR_MIDDLE);
|
|
|
|
|
|
|
|
_contentDirty = false;
|
|
|
|
_cascadeColorEnabled = true;
|
|
|
|
_cascadeOpacityEnabled = true;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2011-03-15 11:59:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF::~LabelTTF()
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF * LabelTTF::create()
|
2012-05-31 06:34:50 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
LabelTTF * ret = new LabelTTF();
|
2014-03-12 10:19:33 +08:00
|
|
|
if (ret)
|
2012-05-31 06:34:50 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
ret->autorelease();
|
2012-05-31 06:34:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-05-31 06:34:50 +08:00
|
|
|
}
|
2013-11-07 09:05:13 +08:00
|
|
|
return ret;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
LabelTTF* LabelTTF::create(const std::string& string, const std::string& fontName, float fontSize,
|
2013-07-27 07:04:21 +08:00
|
|
|
const Size &dimensions, TextHAlignment hAlignment,
|
|
|
|
TextVAlignment vAlignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
LabelTTF *ret = new LabelTTF();
|
|
|
|
if(ret && ret->initWithString(string, fontName, fontSize, dimensions, hAlignment, vAlignment))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-11-07 09:05:13 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
LabelTTF * LabelTTF::createWithFontDefinition(const std::string& string, FontDefinition &textDefinition)
|
2013-06-08 07:33:01 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
LabelTTF *ret = new LabelTTF();
|
|
|
|
if(ret && ret->initWithStringAndTextDefinition(string, textDefinition))
|
2013-06-08 07:33:01 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2013-06-08 07:33:01 +08:00
|
|
|
}
|
2013-11-07 09:05:13 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2013-06-08 07:33:01 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
bool LabelTTF::initWithString(const std::string& string, const std::string& fontName, float fontSize,
|
2013-07-27 07:04:21 +08:00
|
|
|
const cocos2d::Size &dimensions, TextHAlignment hAlignment,
|
|
|
|
TextVAlignment vAlignment)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setString(string);
|
|
|
|
_renderLabel->setFontSize(fontSize);
|
|
|
|
_renderLabel->setDimensions(dimensions.width,dimensions.height);
|
|
|
|
_renderLabel->setAlignment(hAlignment,vAlignment);
|
|
|
|
_renderLabel->setFontName(fontName);
|
2014-03-20 20:56:10 +08:00
|
|
|
_contentDirty = true;
|
2014-03-12 10:19:33 +08:00
|
|
|
|
|
|
|
return true;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
bool LabelTTF::initWithStringAndTextDefinition(const std::string& string, FontDefinition &textDefinition)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setFontDefinition(textDefinition);
|
|
|
|
_renderLabel->setString(string);
|
2014-03-20 20:56:10 +08:00
|
|
|
_contentDirty = true;
|
2014-03-12 10:19:33 +08:00
|
|
|
|
|
|
|
return true;
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
void LabelTTF::setString(const std::string &string)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setString(string);
|
|
|
|
_contentDirty = true;
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-11-08 04:42:16 +08:00
|
|
|
const std::string& LabelTTF::getString() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
return _renderLabel->getString();
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string LabelTTF::getDescription() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-20 15:03:48 +08:00
|
|
|
return StringUtils::format("<LabelTTF | FontName = %s, FontSize = %f, Label = '%s'>", _renderLabel->getFontName().c_str(), _renderLabel->getFontSize(), _renderLabel->getString().c_str());
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
TextHAlignment LabelTTF::getHorizontalAlignment() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
return _renderLabel->getHorizontalAlignment();
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
void LabelTTF::setHorizontalAlignment(TextHAlignment alignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setHorizontalAlignment(alignment);
|
|
|
|
_contentDirty = true;
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
TextVAlignment LabelTTF::getVerticalAlignment() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
return _renderLabel->getVerticalAlignment();
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
void LabelTTF::setVerticalAlignment(TextVAlignment verticalAlignment)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setVerticalAlignment(verticalAlignment);
|
|
|
|
_contentDirty = true;
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:44:41 +08:00
|
|
|
const Size& LabelTTF::getDimensions() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
return _renderLabel->getDimensions();
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::setDimensions(const Size &dim)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setDimensions(dim.width,dim.height);
|
|
|
|
_contentDirty = true;
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
2010-12-28 15:05:55 +08:00
|
|
|
|
2014-03-20 15:03:48 +08:00
|
|
|
float LabelTTF::getFontSize() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
return _renderLabel->getFontSize();
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 14:14:02 +08:00
|
|
|
void LabelTTF::setFontSize(float fontSize)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setFontSize(fontSize);
|
|
|
|
_contentDirty = true;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
const std::string& LabelTTF::getFontName() const
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
return _renderLabel->getFontName();
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
void LabelTTF::setFontName(const std::string& fontName)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setFontName(fontName);
|
|
|
|
_contentDirty = true;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::enableShadow(const Size &shadowOffset, float shadowOpacity, float shadowBlur, bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2014-03-20 20:56:10 +08:00
|
|
|
Color4B temp(Color3B::BLACK);
|
|
|
|
temp.a = 255 * shadowOpacity;
|
|
|
|
_renderLabel->enableShadow(temp,shadowOffset,shadowBlur);
|
2014-03-12 10:19:33 +08:00
|
|
|
_contentDirty = true;
|
2013-04-26 09:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::disableShadow(bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->disableEffect();
|
2014-03-20 20:56:10 +08:00
|
|
|
_contentDirty = true;
|
2013-04-26 09:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void LabelTTF::enableStroke(const Color3B &strokeColor, float strokeSize, bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->enableOutline(Color4B(strokeColor),strokeSize);
|
|
|
|
_contentDirty = true;
|
2013-04-26 09:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::disableStroke(bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->disableEffect();
|
2014-03-20 20:56:10 +08:00
|
|
|
_contentDirty = true;
|
2013-04-26 09:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void LabelTTF::setFontFillColor(const Color3B &tintColor, bool updateTexture)
|
2013-05-01 07:36:14 +08:00
|
|
|
{
|
2014-03-20 20:56:10 +08:00
|
|
|
_renderLabel->setTextColor(Color4B(tintColor));
|
2013-05-01 07:36:14 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 15:17:37 +08:00
|
|
|
void LabelTTF::setTextDefinition(const FontDefinition& theDefinition)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setFontDefinition(theDefinition);
|
|
|
|
_contentDirty = true;
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 10:19:33 +08:00
|
|
|
const FontDefinition& LabelTTF::getTextDefinition() const
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
return _renderLabel->getFontDefinition();
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 10:19:33 +08:00
|
|
|
void LabelTTF::setBlendFunc(const BlendFunc &blendFunc)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setBlendFunc(blendFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
const BlendFunc &LabelTTF::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _renderLabel->getBlendFunc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LabelTTF::setFlippedX(bool flippedX)
|
|
|
|
{
|
|
|
|
if (flippedX)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setScaleX(-1.0f);
|
|
|
|
}
|
|
|
|
else
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setScaleX(1.0f);
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 10:19:33 +08:00
|
|
|
void LabelTTF::setFlippedY(bool flippedY)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
if (flippedY)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setScaleY(-1.0f);
|
|
|
|
}
|
2013-05-04 01:06:08 +08:00
|
|
|
else
|
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
_renderLabel->setScaleY(1.0f);
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
2014-03-12 10:19:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LabelTTF::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated)
|
|
|
|
{
|
|
|
|
if (_contentDirty)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2014-03-12 10:19:33 +08:00
|
|
|
this->setContentSize(_renderLabel->getContentSize());
|
|
|
|
_contentDirty = false;
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
2014-03-12 10:19:33 +08:00
|
|
|
Node::visit(renderer,parentTransform,parentTransformUpdated);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Size& LabelTTF::getContentSize() const
|
|
|
|
{
|
|
|
|
const_cast<LabelTTF*>(this)->setContentSize(_renderLabel->getContentSize());
|
|
|
|
return _contentSize;
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2014-03-28 14:18:01 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (pop)
|
|
|
|
#endif
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_END
|