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/UILabelAtlas.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-12-24 20:22:14 +08:00
|
|
|
|
|
|
|
#define LABELATLASRENDERERZ (-1)
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
|
2013-09-13 22:20:20 +08:00
|
|
|
UICCLabelAtlas::UICCLabelAtlas()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UICCLabelAtlas::~UICCLabelAtlas()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UICCLabelAtlas* UICCLabelAtlas::create()
|
|
|
|
{
|
|
|
|
UICCLabelAtlas *pRet = new UICCLabelAtlas();
|
|
|
|
if(pRet)
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 04:42:16 +08:00
|
|
|
void UICCLabelAtlas::setProperty(const std::string& string, const std::string& charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
initWithString(string, charMapFile, itemWidth, itemHeight, startCharMap);
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void UICCLabelAtlas::setProperty(const std::string& string, Texture2D *texture, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
initWithString(string, texture, itemWidth, itemHeight, startCharMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCLabelAtlas::draw()
|
|
|
|
{
|
|
|
|
if (!_textureAtlas)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
AtlasNode::draw();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
LabelAtlas::LabelAtlas():
|
2013-12-24 20:22:14 +08:00
|
|
|
_labelAtlasRenderer(nullptr),
|
2013-11-06 16:04:06 +08:00
|
|
|
_stringValue(""),
|
|
|
|
_charMapFileName(""),
|
|
|
|
_itemWidth(0),
|
|
|
|
_itemHeight(0),
|
|
|
|
_startCharMap("")
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
LabelAtlas::~LabelAtlas()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
LabelAtlas* LabelAtlas::create()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
LabelAtlas* widget = new LabelAtlas();
|
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
|
|
|
void LabelAtlas::initRenderer()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-24 20:22:14 +08:00
|
|
|
_labelAtlasRenderer = UICCLabelAtlas::create();
|
|
|
|
Node::addChild(_labelAtlasRenderer, LABELATLASRENDERERZ, -1);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void LabelAtlas::setProperty(const std::string& stringValue, const std::string& charMapFile, int itemWidth, int itemHeight, const std::string& startCharMap)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
_stringValue = stringValue;
|
|
|
|
_charMapFileName = charMapFile;
|
|
|
|
_itemWidth = itemWidth;
|
|
|
|
_itemHeight = itemHeight;
|
|
|
|
_startCharMap = startCharMap;
|
2013-12-24 20:22:14 +08:00
|
|
|
_labelAtlasRenderer->setProperty(stringValue, charMapFile, itemWidth, itemHeight, (int)(startCharMap[0]));
|
2013-09-13 22:20:20 +08:00
|
|
|
updateAnchorPoint();
|
|
|
|
labelAtlasScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void LabelAtlas::setStringValue(const std::string& value)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
_stringValue = value;
|
2013-12-24 20:22:14 +08:00
|
|
|
_labelAtlasRenderer->setString(value);
|
2013-09-13 22:20:20 +08:00
|
|
|
labelAtlasScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const std::string& LabelAtlas::getStringValue() const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-24 20:22:14 +08:00
|
|
|
return _labelAtlasRenderer->getString();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void LabelAtlas::setAnchorPoint(const Point &pt)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::setAnchorPoint(pt);
|
2013-12-24 20:22:14 +08:00
|
|
|
_labelAtlasRenderer->setAnchorPoint(Point(pt.x, pt.y));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void LabelAtlas::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
|
|
|
labelAtlasScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const Size& LabelAtlas::getContentSize() const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-24 20:22:14 +08:00
|
|
|
return _labelAtlasRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Node* LabelAtlas::getVirtualRenderer()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-24 20:22:14 +08:00
|
|
|
return _labelAtlasRenderer;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void LabelAtlas::labelAtlasScaleChangedWithSize()
|
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-12-24 20:22:14 +08:00
|
|
|
_labelAtlasRenderer->setScale(1.0f);
|
|
|
|
_size = _labelAtlasRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-24 20:22:14 +08:00
|
|
|
Size textureSize = _labelAtlasRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
2013-12-24 20:22:14 +08:00
|
|
|
_labelAtlasRenderer->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-12-24 20:22:14 +08:00
|
|
|
_labelAtlasRenderer->setScaleX(scaleX);
|
|
|
|
_labelAtlasRenderer->setScaleY(scaleY);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
std::string LabelAtlas::getDescription() const
|
2013-09-17 17:59:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
return "LabelAtlas";
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* LabelAtlas::createCloneInstance()
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return LabelAtlas::create();
|
2013-09-17 17:59:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void LabelAtlas::copySpecialProperties(Widget *widget)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
LabelAtlas* labelAtlas = dynamic_cast<LabelAtlas*>(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
if (labelAtlas)
|
|
|
|
{
|
2013-11-08 04:42:16 +08:00
|
|
|
setProperty(labelAtlas->_stringValue, labelAtlas->_charMapFileName, labelAtlas->_itemWidth, labelAtlas->_itemHeight, labelAtlas->_startCharMap);
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
|
|
|
NS_CC_END
|