2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
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"
|
|
|
|
#include "2d/CCLabel.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
static const int LABELATLAS_RENDERER_Z = (-1);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TextAtlas::~TextAtlas() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TextAtlas* TextAtlas::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
TextAtlas* widget = new TextAtlas();
|
|
|
|
if (widget->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextAtlas::initRenderer()
|
|
|
|
{
|
|
|
|
_labelAtlasRenderer = Label::create();
|
|
|
|
_labelAtlasRenderer->setAnchorPoint(Point::ANCHOR_MIDDLE);
|
|
|
|
addProtectedChild(_labelAtlasRenderer, LABELATLAS_RENDERER_Z, -1);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
TextAtlas* TextAtlas::create(const std::string& stringValue,
|
|
|
|
const std::string& charMapFile,
|
2019-11-23 20:27:39 +08:00
|
|
|
int itemWidth,
|
|
|
|
int itemHeight,
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::string& startCharMap)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
TextAtlas* widget = new TextAtlas();
|
|
|
|
if (widget->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
widget->setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap);
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TextAtlas::setProperty(const std::string& stringValue,
|
|
|
|
const std::string& charMapFile,
|
|
|
|
int itemWidth,
|
|
|
|
int itemHeight,
|
|
|
|
const std::string& startCharMap)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_stringValue = stringValue;
|
2019-11-23 20:27:39 +08:00
|
|
|
_charMapFileName = charMapFile;
|
2021-12-25 10:04:45 +08:00
|
|
|
_itemWidth = itemWidth;
|
|
|
|
_itemHeight = itemHeight;
|
|
|
|
_startCharMap = startCharMap;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_labelAtlasRenderer->setCharMap(_charMapFileName, _itemWidth, _itemHeight, (int)(_startCharMap[0]));
|
|
|
|
_labelAtlasRenderer->setString(stringValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 03:35:34 +08:00
|
|
|
void TextAtlas::setString(std::string_view value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (value == _labelAtlasRenderer->getString())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_stringValue = value;
|
|
|
|
_labelAtlasRenderer->setString(value);
|
|
|
|
updateContentSizeWithTextureSize(_labelAtlasRenderer->getContentSize());
|
|
|
|
_labelAtlasRendererAdaptDirty = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
// CCLOG("cssss w %f, h %f", _contentSize.width, _contentSize.height);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& TextAtlas::getString() const
|
|
|
|
{
|
|
|
|
return _labelAtlasRenderer->getString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
ssize_t TextAtlas::getStringLength() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _labelAtlasRenderer->getStringLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextAtlas::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
|
|
|
_labelAtlasRendererAdaptDirty = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void TextAtlas::adaptRenderers()
|
|
|
|
{
|
|
|
|
if (_labelAtlasRendererAdaptDirty)
|
|
|
|
{
|
|
|
|
labelAtlasScaleChangedWithSize();
|
|
|
|
_labelAtlasRendererAdaptDirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 TextAtlas::getVirtualRendererSize() const
|
2019-11-23 20:27:39 +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();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_labelAtlasRenderer->setScale(1.0f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
float scaleX = _contentSize.width / textureSize.width;
|
|
|
|
float scaleY = _contentSize.height / textureSize.height;
|
|
|
|
_labelAtlasRenderer->setScaleX(scaleX);
|
|
|
|
_labelAtlasRenderer->setScaleY(scaleY);
|
|
|
|
}
|
|
|
|
_labelAtlasRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-11-23 20:27:39 +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);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ResourceData TextAtlas::getRenderFile()
|
|
|
|
{
|
|
|
|
ResourceData rData;
|
|
|
|
rData.type = 0;
|
|
|
|
rData.file = _charMapFileName;
|
|
|
|
return rData;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace ui
|
2019-11-23 20:27:39 +08:00
|
|
|
NS_CC_END
|