2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "2d/CCLabel.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
static const int LABELBMFONT_RENDERER_Z = (-1);
|
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(TextBMFont)
|
|
|
|
|
|
|
|
TextBMFont::TextBMFont():
|
|
|
|
_labelBMFontRenderer(nullptr),
|
|
|
|
_fntFileHasInit(false),
|
|
|
|
_fntFileName(""),
|
2014-04-17 14:08:25 +08:00
|
|
|
_stringValue(""),
|
|
|
|
_labelBMFontRendererAdaptDirty(true)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextBMFont::~TextBMFont()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TextBMFont* TextBMFont::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TextBMFont* widget = new (std::nothrow) TextBMFont();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-04-02 18:16:17 +08:00
|
|
|
|
|
|
|
TextBMFont* TextBMFont::create(const std::string &text, const std::string &filename)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TextBMFont* widget = new (std::nothrow) TextBMFont();
|
2014-04-02 18:16:17 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->setFntFile(filename);
|
2014-05-14 16:02:31 +08:00
|
|
|
widget->setString(text);
|
2014-04-02 18:16:17 +08:00
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void TextBMFont::initRenderer()
|
|
|
|
{
|
2014-03-28 10:28:44 +08:00
|
|
|
_labelBMFontRenderer = cocos2d::Label::create();
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_labelBMFontRenderer, LABELBMFONT_RENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:16:17 +08:00
|
|
|
void TextBMFont::setFntFile(const std::string& fileName)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-02 18:16:17 +08:00
|
|
|
if (fileName.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_fntFileName = fileName;
|
2014-03-28 10:28:44 +08:00
|
|
|
_labelBMFontRenderer->setBMFontFilePath(fileName);
|
2014-06-06 16:09:59 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_fntFileHasInit = true;
|
2014-05-14 16:02:31 +08:00
|
|
|
setString(_stringValue);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 16:02:31 +08:00
|
|
|
void TextBMFont::setString(const std::string& value)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_stringValue = value;
|
|
|
|
if (!_fntFileHasInit)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_labelBMFontRenderer->setString(value);
|
2014-04-17 14:08:25 +08:00
|
|
|
updateContentSizeWithTextureSize(_labelBMFontRenderer->getContentSize());
|
|
|
|
_labelBMFontRendererAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 16:02:31 +08:00
|
|
|
const std::string& TextBMFont::getString()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-02 18:16:17 +08:00
|
|
|
return _stringValue;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-05-14 16:02:31 +08:00
|
|
|
|
2014-05-14 16:25:30 +08:00
|
|
|
ssize_t TextBMFont::getStringLength()const
|
2014-05-14 16:02:31 +08:00
|
|
|
{
|
|
|
|
return _labelBMFontRenderer->getStringLength();
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void TextBMFont::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
2014-04-17 14:08:25 +08:00
|
|
|
_labelBMFontRendererAdaptDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBMFont::adaptRenderers()
|
|
|
|
{
|
|
|
|
if (_labelBMFontRendererAdaptDirty)
|
|
|
|
{
|
|
|
|
labelBMFontScaleChangedWithSize();
|
|
|
|
_labelBMFontRendererAdaptDirty = false;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
const Size& TextBMFont::getVirtualRendererSize() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _labelBMFontRenderer->getContentSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* TextBMFont::getVirtualRenderer()
|
|
|
|
{
|
|
|
|
return _labelBMFontRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBMFont::labelBMFontScaleChangedWithSize()
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
_labelBMFontRenderer->setScale(1.0f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size textureSize = _labelBMFontRenderer->getContentSize();
|
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_labelBMFontRenderer->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
|
|
|
_labelBMFontRenderer->setScaleX(scaleX);
|
|
|
|
_labelBMFontRenderer->setScaleY(scaleY);
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_labelBMFontRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextBMFont::getDescription() const
|
|
|
|
{
|
|
|
|
return "TextBMFont";
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* TextBMFont::createCloneInstance()
|
|
|
|
{
|
|
|
|
return TextBMFont::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBMFont::copySpecialProperties(Widget *widget)
|
|
|
|
{
|
|
|
|
TextBMFont* labelBMFont = dynamic_cast<TextBMFont*>(widget);
|
|
|
|
if (labelBMFont)
|
|
|
|
{
|
2014-04-03 15:45:06 +08:00
|
|
|
setFntFile(labelBMFont->_fntFileName);
|
2014-05-14 16:02:31 +08:00
|
|
|
setString(labelBMFont->_stringValue);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|