2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
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/UITextAtlas.h"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "2d/CCLabel.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
static const int LABELATLAS_RENDERER_Z = (-1);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
IMPLEMENT_CLASS_GUI_INFO(TextAtlas)
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TextAtlas::TextAtlas()
|
|
|
|
: _labelAtlasRenderer(nullptr)
|
|
|
|
, _stringValue("")
|
|
|
|
, _charMapFileName("")
|
|
|
|
, _itemWidth(0)
|
|
|
|
, _itemHeight(0)
|
|
|
|
, _startCharMap("")
|
|
|
|
, _labelAtlasRendererAdaptDirty(true)
|
|
|
|
{}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TextAtlas::~TextAtlas() {}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
TextAtlas* TextAtlas::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
TextAtlas* widget = new TextAtlas();
|
|
|
|
if (widget->init())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextAtlas::initRenderer()
|
|
|
|
{
|
2014-05-14 15:51:50 +08:00
|
|
|
_labelAtlasRenderer = Label::create();
|
2014-04-17 14:08:25 +08:00
|
|
|
_labelAtlasRenderer->setAnchorPoint(Point::ANCHOR_MIDDLE);
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_labelAtlasRenderer, LABELATLAS_RENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
TextAtlas* TextAtlas::create(std::string_view stringValue,
|
|
|
|
std::string_view charMapFile,
|
2014-04-02 18:06:03 +08:00
|
|
|
int itemWidth,
|
|
|
|
int itemHeight,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view startCharMap)
|
2014-04-02 18:06:03 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
TextAtlas* widget = new TextAtlas();
|
|
|
|
if (widget->init())
|
2014-04-02 18:06:03 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
widget->setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap);
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void TextAtlas::setProperty(std::string_view stringValue,
|
|
|
|
std::string_view charMapFile,
|
2021-12-25 10:04:45 +08:00
|
|
|
int itemWidth,
|
|
|
|
int itemHeight,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view startCharMap)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_stringValue = stringValue;
|
2014-03-11 17:13:54 +08:00
|
|
|
_charMapFileName = charMapFile;
|
2021-12-25 10:04:45 +08:00
|
|
|
_itemWidth = itemWidth;
|
|
|
|
_itemHeight = itemHeight;
|
|
|
|
_startCharMap = startCharMap;
|
|
|
|
|
2014-05-14 15:51:50 +08:00
|
|
|
_labelAtlasRenderer->setCharMap(_charMapFileName, _itemWidth, _itemHeight, (int)(_startCharMap[0]));
|
|
|
|
_labelAtlasRenderer->setString(stringValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
updateContentSizeWithTextureSize(_labelAtlasRenderer->getContentSize());
|
|
|
|
_labelAtlasRendererAdaptDirty = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
// CCLOG("cs w %f, h %f", _contentSize.width, _contentSize.height);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 03:35:34 +08:00
|
|
|
void TextAtlas::setString(std::string_view value)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-11-25 19:42:03 +08:00
|
|
|
if (value == _labelAtlasRenderer->getString())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
_stringValue = value;
|
|
|
|
_labelAtlasRenderer->setString(value);
|
2014-04-17 14:08:25 +08:00
|
|
|
updateContentSizeWithTextureSize(_labelAtlasRenderer->getContentSize());
|
|
|
|
_labelAtlasRendererAdaptDirty = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
// CCLOG("cssss w %f, h %f", _contentSize.width, _contentSize.height);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view TextAtlas::getString() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _labelAtlasRenderer->getString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
ssize_t TextAtlas::getStringLength() const
|
2014-05-14 15:51:50 +08:00
|
|
|
{
|
|
|
|
return _labelAtlasRenderer->getStringLength();
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void TextAtlas::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
2014-04-17 14:08:25 +08:00
|
|
|
_labelAtlasRendererAdaptDirty = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
void TextAtlas::adaptRenderers()
|
|
|
|
{
|
|
|
|
if (_labelAtlasRendererAdaptDirty)
|
|
|
|
{
|
|
|
|
labelAtlasScaleChangedWithSize();
|
|
|
|
_labelAtlasRendererAdaptDirty = false;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 TextAtlas::getVirtualRendererSize() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _labelAtlasRenderer->getContentSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* TextAtlas::getVirtualRenderer()
|
|
|
|
{
|
|
|
|
return _labelAtlasRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextAtlas::labelAtlasScaleChangedWithSize()
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
_labelAtlasRenderer->setScale(1.0f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 textureSize = _labelAtlasRenderer->getContentSize();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_labelAtlasRenderer->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
|
|
|
_labelAtlasRenderer->setScaleX(scaleX);
|
|
|
|
_labelAtlasRenderer->setScaleY(scaleY);
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_labelAtlasRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextAtlas::getDescription() const
|
|
|
|
{
|
|
|
|
return "TextAtlas";
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* TextAtlas::createCloneInstance()
|
|
|
|
{
|
|
|
|
return TextAtlas::create();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TextAtlas::copySpecialProperties(Widget* widget)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
TextAtlas* labelAtlas = dynamic_cast<TextAtlas*>(widget);
|
|
|
|
if (labelAtlas)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
setProperty(labelAtlas->_stringValue, labelAtlas->_charMapFileName, labelAtlas->_itemWidth,
|
|
|
|
labelAtlas->_itemHeight, labelAtlas->_startCharMap);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData TextAtlas::getRenderFile()
|
2015-11-18 13:38:29 +08:00
|
|
|
{
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData rData;
|
2015-11-18 13:38:29 +08:00
|
|
|
rData.type = 0;
|
|
|
|
rData.file = _charMapFileName;
|
|
|
|
return rData;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace ui
|
2014-03-11 17:13:54 +08:00
|
|
|
NS_CC_END
|