axmol/cocos/ui/UITextBMFont.cpp

194 lines
4.9 KiB
C++
Raw Normal View History

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/UITextBMFont.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 LABELBMFONT_RENDERER_Z = (-1);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
IMPLEMENT_CLASS_GUI_INFO(TextBMFont)
2021-12-25 10:04:45 +08:00
TextBMFont::TextBMFont()
: _labelBMFontRenderer(nullptr), _fntFileName(""), _stringValue(""), _labelBMFontRendererAdaptDirty(true)
{}
TextBMFont::~TextBMFont() {}
2019-11-23 20:27:39 +08:00
TextBMFont* TextBMFont::create()
{
2021-12-08 00:11:53 +08:00
TextBMFont* widget = new TextBMFont();
if (widget->init())
2019-11-23 20:27:39 +08:00
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
2021-12-25 10:04:45 +08:00
2021-12-26 23:26:34 +08:00
TextBMFont* TextBMFont::create(std::string_view text, std::string_view filename)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
TextBMFont* widget = new TextBMFont();
if (widget->init())
2019-11-23 20:27:39 +08:00
{
widget->setFntFile(filename);
widget->setString(text);
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
void TextBMFont::initRenderer()
{
_labelBMFontRenderer = cocos2d::Label::create();
addProtectedChild(_labelBMFontRenderer, LABELBMFONT_RENDERER_Z, -1);
}
2021-12-26 23:26:34 +08:00
void TextBMFont::setFntFile(std::string_view fileName)
2019-11-23 20:27:39 +08:00
{
if (fileName.empty())
{
return;
}
_fntFileName = fileName;
_labelBMFontRenderer->setBMFontFilePath(fileName);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
updateContentSizeWithTextureSize(_labelBMFontRenderer->getContentSize());
_labelBMFontRendererAdaptDirty = true;
}
2021-12-26 23:26:34 +08:00
void TextBMFont::setString(std::string_view value)
2019-11-23 20:27:39 +08:00
{
if (value == _labelBMFontRenderer->getString())
{
return;
}
_stringValue = value;
_labelBMFontRenderer->setString(value);
updateContentSizeWithTextureSize(_labelBMFontRenderer->getContentSize());
_labelBMFontRendererAdaptDirty = true;
}
2021-12-26 23:26:34 +08:00
std::string_view TextBMFont::getString() const
2019-11-23 20:27:39 +08:00
{
return _stringValue;
}
2021-12-25 10:04:45 +08:00
ssize_t TextBMFont::getStringLength() const
2019-11-23 20:27:39 +08:00
{
return _labelBMFontRenderer->getStringLength();
}
void TextBMFont::onSizeChanged()
{
Widget::onSizeChanged();
_labelBMFontRendererAdaptDirty = true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void TextBMFont::adaptRenderers()
{
if (_labelBMFontRendererAdaptDirty)
{
labelBMFontScaleChangedWithSize();
_labelBMFontRendererAdaptDirty = false;
}
}
2021-10-23 23:27:14 +08:00
Vec2 TextBMFont::getVirtualRendererSize() const
2019-11-23 20:27:39 +08:00
{
return _labelBMFontRenderer->getContentSize();
}
Node* TextBMFont::getVirtualRenderer()
{
return _labelBMFontRenderer;
}
void TextBMFont::labelBMFontScaleChangedWithSize()
{
if (_ignoreSize)
{
_labelBMFontRenderer->setScale(1.0f);
}
else
{
2021-10-23 23:27:14 +08:00
Vec2 textureSize = _labelBMFontRenderer->getContentSize();
2019-11-23 20:27:39 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
_labelBMFontRenderer->setScale(1.0f);
return;
}
float scaleX = _contentSize.width / textureSize.width;
float scaleY = _contentSize.height / textureSize.height;
_labelBMFontRenderer->setScaleX(scaleX);
_labelBMFontRenderer->setScaleY(scaleY);
}
_labelBMFontRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
}
std::string TextBMFont::getDescription() const
{
return "TextBMFont";
}
Widget* TextBMFont::createCloneInstance()
{
return TextBMFont::create();
}
2021-12-25 10:04:45 +08:00
void TextBMFont::copySpecialProperties(Widget* widget)
2019-11-23 20:27:39 +08:00
{
TextBMFont* labelBMFont = dynamic_cast<TextBMFont*>(widget);
if (labelBMFont)
{
setFntFile(labelBMFont->_fntFileName);
setString(labelBMFont->_stringValue);
}
}
ResourceData TextBMFont::getRenderFile()
{
ResourceData rData;
rData.type = 0;
rData.file = _fntFileName;
return rData;
}
void TextBMFont::resetRender()
{
this->removeProtectedChild(_labelBMFontRenderer);
this->initRenderer();
}
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_CC_END