axmol/cocos/ui/UIText.cpp

365 lines
8.5 KiB
C++
Raw Normal View History

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);
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()
{
}
Text* Text::create()
{
Text* widget = new Text();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
bool Text::init()
{
if (Widget::init())
{
return true;
}
return false;
}
Text* Text::create(const std::string &textContent, const std::string &fontName, int fontSize)
{
Text *text = new 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;
}
bool Text::init(const std::string &textContent, const std::string &fontName, int 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;
}
2014-05-14 15:26:14 +08:00
this->setString(textContent);
this->setFontName(fontName);
this->setFontSize(fontSize);
} 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
}
2014-05-14 15:26:14 +08:00
void Text::setString(const std::string &text)
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
}
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(int size)
{
2014-05-13 17:45:15 +08:00
if (_type == Type::SYSTEM) {
_labelRenderer->setSystemFontSize(size);
}
else{
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
}
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;
config.fontSize = _fontSize;
2014-05-13 17:45:15 +08:00
_labelRenderer->setTTFConfig(config);
_type = Type::TTF;
}
else{
_labelRenderer->setSystemFontName(name);
_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
}
2014-05-27 10:54:47 +08:00
const std::string& Text::getFontName()const
2014-03-11 17:13:54 +08:00
{
return _fontName;
}
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);
2014-04-17 14:08:25 +08:00
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
2014-03-11 17:13:54 +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);
2014-04-17 14:08:25 +08:00
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
2014-03-11 17:13:54 +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);
2014-04-17 14:08:25 +08:00
updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
_labelRendererAdaptDirty = true;
2014-03-11 17:13:54 +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();
}
void Text::setTouchScaleChangeEnabled(bool enable)
{
_touchScaleChangeEnabled = enable;
}
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()
{
}
void Text::updateFlippedX()
{
if (_flippedX)
{
_labelRenderer->setScaleX(-1.0f);
}
else
{
_labelRenderer->setScaleX(1.0f);
}
2014-03-11 17:13:54 +08:00
}
void Text::updateFlippedY()
{
if (_flippedY)
{
_labelRenderer->setScaleY(-1.0f);
}
else
{
_labelRenderer->setScaleY(1.0f);
}
2014-03-11 17:13:54 +08:00
}
void Text::onSizeChanged()
{
Widget::onSizeChanged();
2014-04-17 14:08:25 +08:00
_labelRendererAdaptDirty = true;
}
void Text::adaptRenderers()
{
if (_labelRendererAdaptDirty)
{
labelScaleChangedWithSize();
_labelRendererAdaptDirty = false;
}
2014-03-11 17:13:54 +08:00
}
2014-04-17 14:08:25 +08:00
const Size& Text::getVirtualRendererSize() const
2014-03-11 17:13:54 +08:00
{
return _labelRenderer->getContentSize();
}
Node* Text::getVirtualRenderer()
{
return _labelRenderer;
}
void Text::labelScaleChangedWithSize()
{
if (_ignoreSize)
{
_labelRenderer->setDimensions(0,0);
2014-03-11 17:13:54 +08:00
_labelRenderer->setScale(1.0f);
_normalScaleValueX = _normalScaleValueY = 1.0f;
}
else
{
_labelRenderer->setDimensions(_size.width,_size.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 = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
_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";
}
void Text::updateTextureColor()
{
updateColorToRenderer(_labelRenderer);
}
void Text::updateTextureOpacity()
{
updateOpacityToRenderer(_labelRenderer);
}
void Text::updateTextureRGBA()
{
updateRGBAToRenderer(_labelRenderer);
}
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);
setFontSize(label->_labelRenderer->getSystemFontSize());
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());
}
}
}
NS_CC_END