2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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),
|
|
|
|
_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()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
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
|
|
|
|
2014-04-02 17:40:09 +08:00
|
|
|
Text* Text::create(const std::string &textContent, const std::string &fontName, int fontSize)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Text *text = new (std::nothrow) Text;
|
2014-11-25 18:16:34 +08:00
|
|
|
if (text && text->init(textContent, fontName, fontSize))
|
|
|
|
{
|
2014-04-02 17:40:09 +08:00
|
|
|
text->autorelease();
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(text);
|
2014-04-03 17:13:24 +08:00
|
|
|
return nullptr;
|
2014-04-02 17:40:09 +08:00
|
|
|
}
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2014-04-02 17:40:09 +08:00
|
|
|
bool Text::init(const std::string &textContent, const std::string &fontName, int fontSize)
|
|
|
|
{
|
2014-04-04 18:26:53 +08:00
|
|
|
bool ret = true;
|
2014-11-25 18:16:34 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!Widget::init())
|
|
|
|
{
|
2014-04-04 18:26:53 +08:00
|
|
|
ret = false;
|
2014-04-02 17:40:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
this->setFontName(fontName);
|
|
|
|
this->setFontSize(fontSize);
|
2015-01-07 17:21:41 +08:00
|
|
|
this->setString(textContent);
|
2014-04-02 17:40:09 +08:00
|
|
|
} while (0);
|
2014-04-04 18:26:53 +08:00
|
|
|
return ret;
|
2014-04-02 17:40:09 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void Text::initRenderer()
|
|
|
|
{
|
2014-03-14 21:06:40 +08:00
|
|
|
_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();
|
|
|
|
}
|
|
|
|
|
2014-05-14 16:25:30 +08:00
|
|
|
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(int size)
|
|
|
|
{
|
2014-11-25 18:16:34 +08:00
|
|
|
if (_type == Type::SYSTEM)
|
|
|
|
{
|
2014-05-13 17:45:15 +08:00
|
|
|
_labelRenderer->setSystemFontSize(size);
|
|
|
|
}
|
2014-11-25 18:16:34 +08:00
|
|
|
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
|
|
|
|
2014-05-27 10:54:47 +08:00
|
|
|
int 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;
|
2014-05-19 11:38:45 +08:00
|
|
|
config.fontSize = _fontSize;
|
2014-05-13 17:45:15 +08:00
|
|
|
_labelRenderer->setTTFConfig(config);
|
|
|
|
_type = Type::TTF;
|
|
|
|
}
|
2014-11-25 18:16:34 +08:00
|
|
|
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)
|
|
|
|
{
|
2014-03-14 21:06:40 +08:00
|
|
|
_labelRenderer->setDimensions(size.width,size.height);
|
2014-11-25 18:16:34 +08:00
|
|
|
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
|
|
|
|
2014-07-29 12:47:21 +08:00
|
|
|
void Text::setTextColor(const Color4B color)
|
2014-07-25 13:46:25 +08:00
|
|
|
{
|
|
|
|
_labelRenderer->setTextColor(color);
|
|
|
|
}
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2014-07-27 07:23:16 +08:00
|
|
|
const Color4B& Text::getTextColor() const
|
2014-07-25 13:46:25 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2014-03-14 21:06:40 +08:00
|
|
|
_labelRenderer->setScaleX(_normalScaleValueX);
|
|
|
|
_labelRenderer->setScaleY(_normalScaleValueY);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Text::onPressStateChangedToPressed()
|
|
|
|
{
|
|
|
|
if (!_touchScaleChangeEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-03-14 21:06:40 +08:00
|
|
|
_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
|
2015-01-20 18:40:25 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2014-10-09 18:28:09 +08:00
|
|
|
_labelRenderer->setDimensions(0,0);
|
2014-03-11 17:13:54 +08:00
|
|
|
_labelRenderer->setScale(1.0f);
|
|
|
|
_normalScaleValueX = _normalScaleValueY = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
_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;
|
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
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
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
void Text::enableShadow(const Color4B& shadowColor,const Size &offset, int blurRadius)
|
|
|
|
{
|
2014-06-05 19:20:16 +08:00
|
|
|
_labelRenderer->enableShadow(shadowColor, offset, blurRadius);
|
|
|
|
}
|
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
void Text::enableOutline(const Color4B& outlineColor,int outlineSize)
|
|
|
|
{
|
2014-06-06 16:41:45 +08:00
|
|
|
_labelRenderer->enableOutline(outlineColor, outlineSize);
|
2015-04-10 15:30:18 +08:00
|
|
|
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
|
|
|
|
_labelRendererAdaptDirty = true;
|
2014-06-05 19:20:16 +08:00
|
|
|
}
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
void Text::enableGlow(const Color4B& glowColor)
|
|
|
|
{
|
2014-06-06 16:50:25 +08:00
|
|
|
if (_type == Type::TTF)
|
|
|
|
_labelRenderer->enableGlow(glowColor);
|
2014-06-05 19:20:16 +08:00
|
|
|
}
|
2014-06-09 14:17:57 +08:00
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
void Text::disableEffect()
|
|
|
|
{
|
2014-06-05 19:20:16 +08:00
|
|
|
_labelRenderer->disableEffect();
|
2015-04-10 15:30:18 +08:00
|
|
|
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
|
|
|
|
_labelRendererAdaptDirty = true;
|
2014-06-05 19:20:16 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-04-10 14:18:16 +08:00
|
|
|
void Text::disableEffect(LabelEffect effect)
|
|
|
|
{
|
|
|
|
_labelRenderer->disableEffect(effect);
|
2015-04-10 15:30:18 +08:00
|
|
|
//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
|
|
|
}
|
|
|
|
|
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());
|
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());
|
2014-12-11 13:33:28 +08:00
|
|
|
setContentSize(label->getContentSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|