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-10-15 18:00:03 +08:00
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
namespace gui {
|
2013-09-13 22:20:20 +08:00
|
|
|
|
|
|
|
UILabel::UILabel():
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchScaleChangeEnabled(false),
|
|
|
|
_normalScaleValue(1.0f),
|
|
|
|
_fontName("Thonburi"),
|
|
|
|
_fontSize(10),
|
|
|
|
_onSelectedScaleOffset(0.5),
|
|
|
|
_labelRenderer(NULL)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UILabel::~UILabel()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UILabel* UILabel::create()
|
|
|
|
{
|
|
|
|
UILabel* widget = new UILabel();
|
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UILabel::init()
|
|
|
|
{
|
|
|
|
if (UIWidget::init())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::initRenderer()
|
|
|
|
{
|
|
|
|
UIWidget::initRenderer();
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer = CCLabelTTF::create();
|
|
|
|
_renderer->addChild(_labelRenderer);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setText(const char* text)
|
|
|
|
{
|
|
|
|
if (!text)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string strText(text);
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setString(strText.c_str());
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* UILabel::getStringValue()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer->getString();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int UILabel::getStringLength()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
const char* str = _labelRenderer->getString();
|
2013-09-13 22:20:20 +08:00
|
|
|
return strlen(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setFontSize(int size)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setFontSize(size);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setFontName(const char* name)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setFontName(name);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setTextAreaSize(const Size &size)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setDimensions(size);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setTextHorizontalAlignment(TextHAlignment alignment)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setHorizontalAlignment(alignment);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setTextVerticalAlignment(TextVAlignment alignment)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setVerticalAlignment(alignment);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setTouchScaleChangeEnabled(bool enable)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchScaleChangeEnabled = enable;
|
|
|
|
_normalScaleValue = getScale();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UILabel::isTouchScaleChangeEnabled()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _touchScaleChangeEnabled;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::onPressStateChangedToNormal()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_touchScaleChangeEnabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
clickScale(_normalScaleValue);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::onPressStateChangedToPressed()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_touchScaleChangeEnabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
clickScale(_normalScaleValue + _onSelectedScaleOffset);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::onPressStateChangedToDisabled()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::clickScale(float scale)
|
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
_renderer->setScale(scale);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setFlipX(bool flipX)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setFlippedX(flipX);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setFlipY(bool flipY)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setFlippedY(flipY);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UILabel::isFlipX()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer->isFlippedX();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UILabel::isFlipY()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer->isFlippedY();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::setAnchorPoint(const Point &pt)
|
|
|
|
{
|
|
|
|
UIWidget::setAnchorPoint(pt);
|
2013-09-16 20:54:13 +08:00
|
|
|
_labelRenderer->setAnchorPoint(pt);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::onSizeChanged()
|
|
|
|
{
|
|
|
|
labelScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Size& UILabel::getContentSize() const
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* UILabel::getVirtualRenderer()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _labelRenderer;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILabel::labelScaleChangedWithSize()
|
|
|
|
{
|
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-09-16 20:54:13 +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-09-17 17:59:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* UILabel::getDescription() const
|
|
|
|
{
|
|
|
|
return "Label";
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|