axmol/cocos/2d/CCLabelTTF.cpp

293 lines
7.7 KiB
C++
Raw Normal View History

2010-12-17 23:44:19 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
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.
****************************************************************************/
#include "CCLabelTTF.h"
#include "CCLabel.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
2010-12-17 23:44:19 +08:00
NS_CC_BEGIN
#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
LabelTTF::LabelTTF()
{
_renderLabel = Label::create();
_renderLabel->setAnchorPoint(Vector2::ANCHOR_BOTTOM_LEFT);
this->addChild(_renderLabel);
this->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
_contentDirty = false;
_cascadeColorEnabled = true;
_cascadeOpacityEnabled = true;
}
LabelTTF::~LabelTTF()
{
}
LabelTTF * LabelTTF::create()
{
LabelTTF * ret = new LabelTTF();
if (ret)
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
LabelTTF* LabelTTF::create(const std::string& string, const std::string& fontName, float fontSize,
const Size &dimensions, TextHAlignment hAlignment,
TextVAlignment vAlignment)
{
LabelTTF *ret = new LabelTTF();
if(ret && ret->initWithString(string, fontName, fontSize, dimensions, hAlignment, vAlignment))
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
LabelTTF * LabelTTF::createWithFontDefinition(const std::string& string, FontDefinition &textDefinition)
{
LabelTTF *ret = new LabelTTF();
if(ret && ret->initWithStringAndTextDefinition(string, textDefinition))
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
bool LabelTTF::initWithString(const std::string& string, const std::string& fontName, float fontSize,
const cocos2d::Size &dimensions, TextHAlignment hAlignment,
TextVAlignment vAlignment)
2012-06-11 14:36:25 +08:00
{
_renderLabel->setString(string);
_renderLabel->setSystemFontSize(fontSize);
_renderLabel->setDimensions(dimensions.width,dimensions.height);
_renderLabel->setAlignment(hAlignment,vAlignment);
_renderLabel->setSystemFontName(fontName);
_contentDirty = true;
return true;
}
2012-06-11 14:36:25 +08:00
bool LabelTTF::initWithStringAndTextDefinition(const std::string& string, FontDefinition &textDefinition)
2013-05-04 01:06:08 +08:00
{
_renderLabel->setFontDefinition(textDefinition);
_renderLabel->setString(string);
_contentDirty = true;
return true;
2013-05-04 01:06:08 +08:00
}
void LabelTTF::setString(const std::string &string)
{
_renderLabel->setString(string);
_contentDirty = true;
2012-06-11 14:36:25 +08:00
}
const std::string& LabelTTF::getString() const
2012-06-11 14:36:25 +08:00
{
return _renderLabel->getString();
2012-06-11 14:36:25 +08:00
}
std::string LabelTTF::getDescription() const
2012-06-11 14:36:25 +08:00
{
return StringUtils::format("<LabelTTF | FontName = %s, FontSize = %f, Label = '%s'>", _renderLabel->getSystemFontName().c_str(), _renderLabel->getSystemFontSize(), _renderLabel->getString().c_str());
2012-06-11 14:36:25 +08:00
}
TextHAlignment LabelTTF::getHorizontalAlignment() const
2012-06-11 14:36:25 +08:00
{
return _renderLabel->getHorizontalAlignment();
}
2012-06-11 14:36:25 +08:00
void LabelTTF::setHorizontalAlignment(TextHAlignment alignment)
{
_renderLabel->setHorizontalAlignment(alignment);
_contentDirty = true;
2012-06-11 14:36:25 +08:00
}
TextVAlignment LabelTTF::getVerticalAlignment() const
2012-06-11 14:36:25 +08:00
{
return _renderLabel->getVerticalAlignment();
2012-06-11 14:36:25 +08:00
}
void LabelTTF::setVerticalAlignment(TextVAlignment verticalAlignment)
2012-06-11 14:36:25 +08:00
{
_renderLabel->setVerticalAlignment(verticalAlignment);
_contentDirty = true;
2012-06-11 14:36:25 +08:00
}
const Size& LabelTTF::getDimensions() const
2012-06-11 14:36:25 +08:00
{
return _renderLabel->getDimensions();
2012-06-11 14:36:25 +08:00
}
void LabelTTF::setDimensions(const Size &dim)
2012-06-11 14:36:25 +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
float LabelTTF::getFontSize() const
2012-06-11 14:36:25 +08:00
{
return _renderLabel->getSystemFontSize();
}
2014-03-20 14:14:02 +08:00
void LabelTTF::setFontSize(float fontSize)
{
_renderLabel->setSystemFontSize(fontSize);
_contentDirty = true;
}
const std::string& LabelTTF::getFontName() const
{
return _renderLabel->getSystemFontName();
2012-06-11 14:36:25 +08:00
}
void LabelTTF::setFontName(const std::string& fontName)
2012-06-11 14:36:25 +08:00
{
_renderLabel->setSystemFontName(fontName);
_contentDirty = true;
}
void LabelTTF::enableShadow(const Size &shadowOffset, float shadowOpacity, float shadowBlur, bool updateTexture)
2013-04-26 09:22:26 +08:00
{
Color4B temp(Color3B::BLACK);
temp.a = 255 * shadowOpacity;
_renderLabel->enableShadow(temp,shadowOffset,shadowBlur);
_contentDirty = true;
2013-04-26 09:22:26 +08:00
}
void LabelTTF::disableShadow(bool updateTexture)
2013-04-26 09:22:26 +08:00
{
_renderLabel->disableEffect();
_contentDirty = true;
2013-04-26 09:22:26 +08:00
}
void LabelTTF::enableStroke(const Color3B &strokeColor, float strokeSize, bool updateTexture)
2013-04-26 09:22:26 +08:00
{
_renderLabel->enableOutline(Color4B(strokeColor),strokeSize);
_contentDirty = true;
2013-04-26 09:22:26 +08:00
}
void LabelTTF::disableStroke(bool updateTexture)
2013-04-26 09:22:26 +08:00
{
_renderLabel->disableEffect();
_contentDirty = true;
2013-04-26 09:22:26 +08:00
}
void LabelTTF::setFontFillColor(const Color3B &tintColor, bool updateTexture)
{
_renderLabel->setTextColor(Color4B(tintColor));
}
void LabelTTF::setTextDefinition(const FontDefinition& theDefinition)
2013-05-04 01:06:08 +08:00
{
_renderLabel->setFontDefinition(theDefinition);
_contentDirty = true;
2013-05-04 01:06:08 +08:00
}
const FontDefinition& LabelTTF::getTextDefinition() const
2013-05-04 01:06:08 +08:00
{
return _renderLabel->getFontDefinition();
2013-05-04 01:06:08 +08:00
}
void LabelTTF::setBlendFunc(const BlendFunc &blendFunc)
2013-05-04 01:06:08 +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
{
_renderLabel->setScaleX(-1.0f);
}
else
2013-05-04 01:06:08 +08:00
{
_renderLabel->setScaleX(1.0f);
2013-05-04 01:06:08 +08:00
}
}
void LabelTTF::setFlippedY(bool flippedY)
2013-05-04 01:06:08 +08:00
{
if (flippedY)
2013-05-04 01:06:08 +08:00
{
_renderLabel->setScaleY(-1.0f);
}
2013-05-04 01:06:08 +08:00
else
{
_renderLabel->setScaleY(1.0f);
2013-05-04 01:06:08 +08:00
}
}
2014-04-08 21:45:54 +08:00
void LabelTTF::visit(Renderer *renderer, const Matrix &parentTransform, bool parentTransformUpdated)
{
if (_contentDirty)
2013-05-04 01:06:08 +08:00
{
this->setContentSize(_renderLabel->getContentSize());
_contentDirty = false;
2013-05-04 01:06:08 +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
}
Rect LabelTTF::getBoundingBox() const
{
return _renderLabel->getBoundingBox();
}
#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
NS_CC_END