2013-09-13 22:20:20 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "gui/UILabel.h"
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
namespace gui {
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Label::Label():
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchScaleChangeEnabled(false),
|
2013-11-13 20:01:57 +08:00
|
|
|
_normalScaleValueX(1.0f),
|
|
|
|
_normalScaleValueY(1.0f),
|
2013-09-16 20:54:13 +08:00
|
|
|
_fontName("Thonburi"),
|
|
|
|
_fontSize(10),
|
|
|
|
_onSelectedScaleOffset(0.5),
|
2013-11-14 11:37:46 +08:00
|
|
|
_labelRenderer(nullptr)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Label::~Label()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Label* Label::create()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Label* widget = new Label();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool Label::init()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
if (Widget::init())
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::initRenderer()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::initRenderer();
|
|
|
|
_labelRenderer = LabelTTF::create();
|
2013-09-16 20:54:13 +08:00
|
|
|
_renderer->addChild(_labelRenderer);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setText(const std::string& text)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-08 04:42:16 +08:00
|
|
|
if (text.size()==0)
|
2013-09-13 22:20:20 +08:00
|
|
|
return;
|
2013-11-08 04:42:16 +08:00
|
|
|
|
|
|
|
_labelRenderer->setString(text);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const std::string& Label::getStringValue()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer->getString();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
int Label::getStringLength()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-08 04:42:16 +08:00
|
|
|
return _labelRenderer->getString().size();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setFontSize(int size)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
_fontSize = size;
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setFontSize(size);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setFontName(const std::string& name)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
_fontName = name;
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setFontName(name);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setTextAreaSize(const Size &size)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setDimensions(size);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setTextHorizontalAlignment(TextHAlignment alignment)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setHorizontalAlignment(alignment);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setTextVerticalAlignment(TextVAlignment alignment)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setVerticalAlignment(alignment);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setTouchScaleChangeEnabled(bool enable)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchScaleChangeEnabled = enable;
|
2013-11-13 20:01:57 +08:00
|
|
|
_normalScaleValueX = getScaleX();
|
|
|
|
_normalScaleValueY = getScaleY();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setScale(float fScale)
|
2013-11-13 20:01:57 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::setScale(fScale);
|
2013-11-13 20:01:57 +08:00
|
|
|
_normalScaleValueX = _normalScaleValueY = fScale;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setScaleX(float fScaleX)
|
2013-11-13 20:01:57 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::setScaleX(fScaleX);
|
2013-11-13 20:01:57 +08:00
|
|
|
_normalScaleValueX = fScaleX;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setScaleY(float fScaleY)
|
2013-11-13 20:01:57 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::setScaleY(fScaleY);
|
2013-11-13 20:01:57 +08:00
|
|
|
_normalScaleValueY = fScaleY;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool Label::isTouchScaleChangeEnabled()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _touchScaleChangeEnabled;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::onPressStateChangedToNormal()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_touchScaleChangeEnabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-13 20:01:57 +08:00
|
|
|
clickScale(_normalScaleValueX, _normalScaleValueY);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::onPressStateChangedToPressed()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_touchScaleChangeEnabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-13 20:01:57 +08:00
|
|
|
clickScale(_normalScaleValueX + _onSelectedScaleOffset, _normalScaleValueY + _onSelectedScaleOffset);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::onPressStateChangedToDisabled()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::clickScale(float scaleX, float scaleY)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-13 20:01:57 +08:00
|
|
|
_renderer->setScaleX(scaleX);
|
|
|
|
_renderer->setScaleY(scaleY);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setFlipX(bool flipX)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setFlippedX(flipX);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setFlipY(bool flipY)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setFlippedY(flipY);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool Label::isFlipX()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer->isFlippedX();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool Label::isFlipY()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer->isFlippedY();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::setAnchorPoint(const Point &pt)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::setAnchorPoint(pt);
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setAnchorPoint(pt);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::onSizeChanged()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::onSizeChanged();
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const Size& Label::getContentSize() const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Node* Label::getVirtualRenderer()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::labelScaleChangedWithSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
if (_ignoreSize)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setScale(1.0f);
|
|
|
|
_size = _labelRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Size textureSize = _labelRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setScale(1.0f);
|
2013-09-13 22:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-16 15:32:52 +08:00
|
|
|
float scaleX = _size.width / textureSize.width;
|
|
|
|
float scaleY = _size.height / textureSize.height;
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setScaleX(scaleX);
|
|
|
|
_labelRenderer->setScaleY(scaleY);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
|
2013-09-17 17:59:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
std::string Label::getDescription() const
|
2013-09-17 17:59:20 +08:00
|
|
|
{
|
|
|
|
return "Label";
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* Label::createCloneInstance()
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return Label::create();
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Label::copySpecialProperties(Widget *widget)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Label* label = dynamic_cast<Label*>(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
if (label)
|
|
|
|
{
|
|
|
|
setFontName(label->_fontName.c_str());
|
|
|
|
setFontSize(label->_labelRenderer->getFontSize());
|
|
|
|
setText(label->getStringValue());
|
|
|
|
setTouchScaleChangeEnabled(label->_touchScaleChangeEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|