axmol/cocos/ui/UIText.cpp

461 lines
11 KiB
C++
Raw Normal View History

2014-03-11 17:13:54 +08:00
/****************************************************************************
Copyright (c) 2013-2017 Chukong Technologies Inc.
2014-03-11 17:13:54 +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 "ui/UIText.h"
2014-06-04 14:26:21 +08:00
#include "2d/CCLabel.h"
#include "platform/CCFileUtils.h"
2014-03-11 17:13:54 +08:00
NS_CC_BEGIN
namespace ui {
static const int LABEL_RENDERER_Z = (-1);
2015-04-10 14:18:16 +08:00
2014-03-11 17:13:54 +08:00
IMPLEMENT_CLASS_GUI_INFO(Text)
Text::Text():
_touchScaleChangeEnabled(false),
_normalScaleValueX(1.0f),
_normalScaleValueY(1.0f),
_fontName("Thonburi"),
_fontSize(10.f),
2014-03-11 17:13:54 +08:00
_onSelectedScaleOffset(0.5),
2014-04-17 14:08:25 +08:00
_labelRenderer(nullptr),
2014-05-13 17:45:15 +08:00
_labelRendererAdaptDirty(true),
_type(Type::SYSTEM)
2014-03-11 17:13:54 +08:00
{
}
Text::~Text()
{
2015-04-10 14:18:16 +08:00
2014-03-11 17:13:54 +08:00
}
Text* Text::create()
{
Text* widget = new (std::nothrow) Text();
2014-03-11 17:13:54 +08:00
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
bool Text::init()
{
if (Widget::init())
{
return true;
}
return false;
}
2015-04-10 14:18:16 +08:00
Text* Text::create(const std::string &textContent, const std::string &fontName, float fontSize)
{
Text *text = new (std::nothrow) Text;
if (text && text->init(textContent, fontName, fontSize))
{
text->autorelease();
return text;
}
CC_SAFE_DELETE(text);
2014-04-03 17:13:24 +08:00
return nullptr;
}
2015-04-10 14:18:16 +08:00
bool Text::init(const std::string &textContent, const std::string &fontName, float fontSize)
{
2014-04-04 18:26:53 +08:00
bool ret = true;
do
{
if (!Widget::init())
{
2014-04-04 18:26:53 +08:00
ret = false;
break;
}
this->setFontName(fontName);
this->setFontSize(fontSize);
this->setString(textContent);
} while (0);
2014-04-04 18:26:53 +08:00
return ret;
}
2014-03-11 17:13:54 +08:00
void Text::initRenderer()
{
_labelRenderer = Label::create();
2014-03-24 15:25:44 +08:00
addProtectedChild(_labelRenderer, LABEL_RENDERER_Z, -1);
2014-03-11 17:13:54 +08:00
}
2015-04-10 14:18:16 +08:00
2014-05-14 15:26:14 +08:00
void Text::setString(const std::string &text)
2014-03-11 17:13:54 +08:00
{
2014-11-25 16:50:46 +08:00
if (text == _labelRenderer->getString())
{
return;
}
2014-03-11 17:13:54 +08:00
_labelRenderer->setString(text);
2014-04-17 14:08:25 +08:00
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
2014-03-11 17:13:54 +08:00
}
2015-04-10 14:18:16 +08:00
2014-05-14 15:26:14 +08:00
const std::string& Text::getString() const
2014-03-11 17:13:54 +08:00
{
return _labelRenderer->getString();
}
ssize_t Text::getStringLength()const
2014-03-11 17:13:54 +08:00
{
2014-05-14 15:26:14 +08:00
return _labelRenderer->getStringLength();
2014-03-11 17:13:54 +08:00
}
void Text::setFontSize(float size)
2014-03-11 17:13:54 +08:00
{
if (_type == Type::SYSTEM)
{
2014-05-13 17:45:15 +08:00
_labelRenderer->setSystemFontSize(size);
}
else
{
2014-05-13 17:45:15 +08:00
TTFConfig config = _labelRenderer->getTTFConfig();
config.fontSize = size;
_labelRenderer->setTTFConfig(config);
}
2014-03-11 17:13:54 +08:00
_fontSize = size;
2014-04-17 14:08:25 +08:00
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
2014-03-11 17:13:54 +08:00
}
2015-04-10 14:18:16 +08:00
float Text::getFontSize()const
2014-03-11 17:13:54 +08:00
{
return _fontSize;
}
void Text::setFontName(const std::string& name)
{
2014-05-13 17:45:15 +08:00
if(FileUtils::getInstance()->isFileExist(name))
{
TTFConfig config = _labelRenderer->getTTFConfig();
config.fontFilePath = name;
config.fontSize = _fontSize;
2014-05-13 17:45:15 +08:00
_labelRenderer->setTTFConfig(config);
_type = Type::TTF;
}
else
{
2014-05-13 17:45:15 +08:00
_labelRenderer->setSystemFontName(name);
2014-10-09 18:28:09 +08:00
if (_type == Type::TTF)
{
_labelRenderer->requestSystemFontRefresh();
}
2014-05-13 17:45:15 +08:00
_type = Type::SYSTEM;
}
2014-03-11 17:13:54 +08:00
_fontName = name;
2014-04-17 14:08:25 +08:00
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
2014-03-11 17:13:54 +08:00
}
2015-04-10 14:18:16 +08:00
2014-05-27 10:54:47 +08:00
const std::string& Text::getFontName()const
2014-03-11 17:13:54 +08:00
{
return _fontName;
}
2015-04-10 14:18:16 +08:00
2014-05-13 17:45:15 +08:00
Text::Type Text::getType() const
{
return _type;
}
2014-03-11 17:13:54 +08:00
void Text::setTextAreaSize(const Size &size)
{
_labelRenderer->setDimensions(size.width,size.height);
if (!_ignoreSize)
{
_customSize=size;
}
2014-04-17 14:08:25 +08:00
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
2014-03-11 17:13:54 +08:00
}
2015-04-10 14:18:16 +08:00
2014-05-27 10:54:47 +08:00
const Size& Text::getTextAreaSize()const
2014-03-11 17:13:54 +08:00
{
return _labelRenderer->getDimensions();
}
void Text::setTextHorizontalAlignment(TextHAlignment alignment)
{
_labelRenderer->setHorizontalAlignment(alignment);
}
2015-04-10 14:18:16 +08:00
2014-05-27 10:54:47 +08:00
TextHAlignment Text::getTextHorizontalAlignment()const
2014-03-11 17:13:54 +08:00
{
return _labelRenderer->getHorizontalAlignment();
}
void Text::setTextVerticalAlignment(TextVAlignment alignment)
{
_labelRenderer->setVerticalAlignment(alignment);
}
2015-04-10 14:18:16 +08:00
2014-05-27 10:54:47 +08:00
TextVAlignment Text::getTextVerticalAlignment()const
2014-03-11 17:13:54 +08:00
{
return _labelRenderer->getVerticalAlignment();
}
2015-04-10 14:18:16 +08:00
void Text::setTextColor(const Color4B color)
{
_labelRenderer->setTextColor(color);
}
2015-04-10 14:18:16 +08:00
const Color4B& Text::getTextColor() const
{
return _labelRenderer->getTextColor();
}
2014-03-11 17:13:54 +08:00
void Text::setTouchScaleChangeEnabled(bool enable)
{
_touchScaleChangeEnabled = enable;
}
2015-04-10 14:18:16 +08:00
2014-05-27 10:54:47 +08:00
bool Text::isTouchScaleChangeEnabled()const
2014-03-11 17:13:54 +08:00
{
return _touchScaleChangeEnabled;
}
void Text::onPressStateChangedToNormal()
{
if (!_touchScaleChangeEnabled)
{
return;
}
_labelRenderer->setScaleX(_normalScaleValueX);
_labelRenderer->setScaleY(_normalScaleValueY);
2014-03-11 17:13:54 +08:00
}
void Text::onPressStateChangedToPressed()
{
if (!_touchScaleChangeEnabled)
{
return;
}
_labelRenderer->setScaleX(_normalScaleValueX + _onSelectedScaleOffset);
_labelRenderer->setScaleY(_normalScaleValueY + _onSelectedScaleOffset);
2014-03-11 17:13:54 +08:00
}
void Text::onPressStateChangedToDisabled()
{
2015-04-10 14:18:16 +08:00
2014-03-11 17:13:54 +08:00
}
void Text::onSizeChanged()
{
Widget::onSizeChanged();
2014-04-17 14:08:25 +08:00
_labelRendererAdaptDirty = true;
}
2015-04-10 14:18:16 +08:00
2014-04-17 14:08:25 +08:00
void Text::adaptRenderers()
{
if (_labelRendererAdaptDirty)
{
labelScaleChangedWithSize();
_labelRendererAdaptDirty = false;
}
2014-03-11 17:13:54 +08:00
}
2014-09-12 14:39:22 +08:00
Size Text::getVirtualRendererSize() const
{
return _labelRenderer->getContentSize();
}
Size Text::getAutoRenderSize()
2014-03-11 17:13:54 +08:00
{
2015-01-19 17:02:28 +08:00
Size virtualSize = _labelRenderer->getContentSize();
if (!_ignoreSize)
{
_labelRenderer->setDimensions(0, 0);
virtualSize = _labelRenderer->getContentSize();
_labelRenderer->setDimensions(_contentSize.width, _contentSize.height);
}
return virtualSize;
2014-03-11 17:13:54 +08:00
}
Node* Text::getVirtualRenderer()
{
return _labelRenderer;
}
void Text::labelScaleChangedWithSize()
{
if (_ignoreSize)
{
_labelRenderer->setScale(1.0f);
_normalScaleValueX = _normalScaleValueY = 1.0f;
}
else
{
_labelRenderer->setDimensions(_contentSize.width,_contentSize.height);
2014-03-11 17:13:54 +08:00
Size textureSize = _labelRenderer->getContentSize();
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
_labelRenderer->setScale(1.0f);
return;
}
float scaleX = _contentSize.width / textureSize.width;
float scaleY = _contentSize.height / textureSize.height;
2014-03-11 17:13:54 +08:00
_labelRenderer->setScaleX(scaleX);
_labelRenderer->setScaleY(scaleY);
_normalScaleValueX = scaleX;
_normalScaleValueY = scaleY;
}
2014-04-17 14:08:25 +08:00
_labelRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
2014-03-11 17:13:54 +08:00
}
std::string Text::getDescription() const
{
return "Label";
}
2015-04-10 14:18:16 +08:00
void Text::enableShadow(const Color4B& shadowColor,const Size &offset, int blurRadius)
{
_labelRenderer->enableShadow(shadowColor, offset, blurRadius);
}
void Text::enableOutline(const Color4B& outlineColor,int outlineSize)
{
_labelRenderer->enableOutline(outlineColor, outlineSize);
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
}
2015-04-10 14:18:16 +08:00
void Text::enableGlow(const Color4B& glowColor)
{
2014-06-06 16:50:25 +08:00
if (_type == Type::TTF)
_labelRenderer->enableGlow(glowColor);
}
void Text::disableEffect()
{
_labelRenderer->disableEffect();
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
}
2014-03-11 17:13:54 +08:00
2015-04-10 14:18:16 +08:00
void Text::disableEffect(LabelEffect effect)
{
_labelRenderer->disableEffect(effect);
//only outline effect will affect the content size of label
if(LabelEffect::OUTLINE == effect)
{
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
}
2015-04-10 14:18:16 +08:00
}
2015-12-10 13:17:31 +08:00
bool Text::isShadowEnabled() const
{
return _labelRenderer->isShadowEnabled();
}
Size Text::getShadowOffset() const
{
return _labelRenderer->getShadowOffset();
}
float Text::getShadowBlurRadius() const
{
return _labelRenderer->getShadowBlurRadius();
}
Color4B Text::getShadowColor() const
{
Color4F effect = _labelRenderer->getShadowColor();
return Color4B(effect.r * 255, effect.g * 255, effect.b * 255, effect.a * 255);
}
int Text::getOutlineSize() const
{
return _labelRenderer->getOutlineSize();
}
LabelEffect Text::getLabelEffectType() const
{
return _labelRenderer->getLabelEffectType();
}
Color4B Text::getEffectColor() const
{
Color4F effect = _labelRenderer->getEffectColor();
return Color4B(effect.r * 255, effect.g * 255, effect.b * 255, effect.a * 255);
}
2017-01-13 18:06:01 +08:00
Sprite * Text::getLetter(int lettetIndex)
{
return _labelRenderer->getLetter(lettetIndex);
}
2018-01-19 09:19:36 +08:00
void Text::setBlendFunc(const BlendFunc &blendFunc)
{
_labelRenderer->setBlendFunc(blendFunc);
}
const BlendFunc& Text::getBlendFunc() const
{
return _labelRenderer->getBlendFunc();
}
2014-03-11 17:13:54 +08:00
Widget* Text::createCloneInstance()
{
return Text::create();
}
void Text::copySpecialProperties(Widget *widget)
{
Text* label = dynamic_cast<Text*>(widget);
if (label)
{
2014-04-03 15:56:45 +08:00
setFontName(label->_fontName);
2014-06-26 17:29:07 +08:00
setFontSize(label->getFontSize());
setTextColor(label->getTextColor());
2014-05-14 15:26:14 +08:00
setString(label->getString());
2014-03-11 17:13:54 +08:00
setTouchScaleChangeEnabled(label->_touchScaleChangeEnabled);
setTextHorizontalAlignment(label->_labelRenderer->getHorizontalAlignment());
setTextVerticalAlignment(label->_labelRenderer->getVerticalAlignment());
setTextAreaSize(label->_labelRenderer->getDimensions());
setContentSize(label->getContentSize());
2015-12-10 13:17:31 +08:00
LabelEffect effectType = label->getLabelEffectType();
if (effectType == LabelEffect::GLOW)
{
enableGlow(label->getEffectColor());
}
else if (effectType == LabelEffect::OUTLINE)
{
enableOutline(label->getEffectColor(),label->getOutlineSize());
}
if (label->isShadowEnabled())
{
enableShadow(label->getShadowColor(),label->getShadowOffset(),label->getShadowBlurRadius());
}
2014-03-11 17:13:54 +08:00
}
}
}
NS_CC_END