2011-11-09 22:11:36 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-11-09 22:11:36 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2011-11-09 22:11:36 +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.
|
|
|
|
|
|
|
|
Use any of these editors to generate BMFonts:
|
|
|
|
http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
|
|
|
|
http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
|
|
|
|
http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
|
|
|
|
http://www.angelcode.com/products/bmfont/ (Free, Windows only)
|
|
|
|
|
|
|
|
****************************************************************************/
|
2014-08-27 13:39:50 +08:00
|
|
|
#include "2d/CCLabelBMFont.h"
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSprite.h"
|
2011-11-09 22:11:36 +08:00
|
|
|
|
2014-04-22 23:18:23 +08:00
|
|
|
#if CC_LABELBMFONT_DEBUG_DRAW
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "base/CCDirector.h"
|
2014-04-22 23:18:23 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-10 15:14:55 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2014-03-28 14:18:01 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (push)
|
|
|
|
#pragma warning (disable: 4996)
|
|
|
|
#endif
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_BEGIN
|
2013-03-20 13:55:43 +08:00
|
|
|
|
2014-03-28 14:22:51 +08:00
|
|
|
LabelBMFont * LabelBMFont::create()
|
2012-06-15 15:10:40 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LabelBMFont * pRet = new (std::nothrow) LabelBMFont();
|
2014-03-25 09:58:16 +08:00
|
|
|
if (pRet)
|
2012-06-15 15:10:40 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-06-15 15:10:40 +08:00
|
|
|
}
|
2012-06-14 18:32:44 +08:00
|
|
|
|
2012-03-22 09:46:07 +08:00
|
|
|
//LabelBMFont - Creation & Init
|
2014-05-15 01:07:09 +08:00
|
|
|
LabelBMFont *LabelBMFont::create(const std::string& str, const std::string& fntFile, float width /* = 0 */, TextHAlignment alignment /* = TextHAlignment::LEFT */,const Vec2& imageOffset /* = Vec2::ZERO */)
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LabelBMFont *ret = new (std::nothrow) LabelBMFont();
|
2014-02-20 22:33:52 +08:00
|
|
|
if(ret && ret->initWithString(str, fntFile, width, alignment,imageOffset))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool LabelBMFont::initWithString(const std::string& str, const std::string& fntFile, float width /* = 0 */, TextHAlignment alignment /* = TextHAlignment::LEFT */,const Vec2& imageOffset /* = Vec2::ZERO */)
|
2012-06-12 14:33:53 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
if (_label->setBMFontFilePath(fntFile,imageOffset))
|
2012-06-08 15:29:47 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_fntFile = fntFile;
|
2014-02-20 22:33:52 +08:00
|
|
|
_label->setMaxLineWidth(width);
|
|
|
|
_label->setAlignment(alignment);
|
|
|
|
_label->setString(str);
|
|
|
|
this->setContentSize(_label->getContentSize());
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-20 22:33:52 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelBMFont::LabelBMFont()
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
_label = Label::create();
|
2014-05-15 01:07:09 +08:00
|
|
|
_label->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2014-02-20 22:33:52 +08:00
|
|
|
this->addChild(_label);
|
2014-05-15 01:07:09 +08:00
|
|
|
this->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
2014-02-20 22:33:52 +08:00
|
|
|
_cascadeOpacityEnabled = true;
|
2014-09-11 15:39:56 +08:00
|
|
|
|
|
|
|
#if CC_LABELBMFONT_DEBUG_DRAW
|
|
|
|
_debugDrawNode = DrawNode::create();
|
|
|
|
addChild(_debugDrawNode);
|
|
|
|
#endif
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelBMFont::~LabelBMFont()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2011-11-09 22:11:36 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
//LabelBMFont - LabelProtocol protocol
|
2013-11-06 11:02:03 +08:00
|
|
|
void LabelBMFont::setString(const std::string &newString)
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
_label->setString(newString);
|
|
|
|
this->setContentSize(_label->getContentSize());
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 04:42:16 +08:00
|
|
|
const std::string& LabelBMFont::getString() const
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
return _label->getString();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
/** Override synthesized setOpacity to recurse items */
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelBMFont::setOpacityModifyRGB(bool var)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
_label->setOpacityModifyRGB(var);
|
2013-12-19 10:33:04 +08:00
|
|
|
for(const auto &child : _children) {
|
2014-02-20 22:33:52 +08:00
|
|
|
child->setOpacityModifyRGB(var);
|
2013-12-19 10:33:04 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-07-04 08:22:15 +08:00
|
|
|
bool LabelBMFont::isOpacityModifyRGB() const
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
return _label->isOpacityModifyRGB();
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// LabelBMFont - Alignment
|
2013-07-27 07:04:21 +08:00
|
|
|
void LabelBMFont::setAlignment(TextHAlignment alignment)
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
_label->setAlignment(alignment);
|
|
|
|
this->setContentSize(_label->getContentSize());
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelBMFont::setWidth(float width)
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
_label->setMaxLineWidth(width);
|
|
|
|
this->setContentSize(_label->getContentSize());
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelBMFont::setLineBreakWithoutSpace( bool breakWithoutSpace )
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
_label->setLineBreakWithoutSpace(breakWithoutSpace);
|
|
|
|
this->setContentSize(_label->getContentSize());
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2014-02-20 22:33:52 +08:00
|
|
|
// LabelBMFont - FntFile
|
2014-05-15 01:07:09 +08:00
|
|
|
void LabelBMFont::setFntFile(const std::string& fntFile, const Vec2& imageOffset /* = Vec2::ZERO */)
|
2012-04-25 18:08:23 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
if (_fntFile.compare(fntFile) != 0)
|
|
|
|
{
|
|
|
|
_fntFile = fntFile;
|
|
|
|
_label->setBMFontFilePath(fntFile,imageOffset);
|
|
|
|
}
|
2012-04-25 18:08:23 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 22:33:52 +08:00
|
|
|
const std::string& LabelBMFont::getFntFile() const
|
2012-04-25 18:08:23 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
return _fntFile;
|
2012-04-25 18:08:23 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 22:33:52 +08:00
|
|
|
std::string LabelBMFont::getDescription() const
|
2012-04-25 18:08:23 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
return StringUtils::format("<LabelBMFont | Tag = %d, Label = '%s'>", _tag, _label->getString().c_str());
|
2012-04-25 18:08:23 +08:00
|
|
|
}
|
2012-06-12 14:33:53 +08:00
|
|
|
|
2014-02-20 22:33:52 +08:00
|
|
|
void LabelBMFont::setBlendFunc(const BlendFunc &blendFunc)
|
2012-06-15 15:10:40 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
_label->setBlendFunc(blendFunc);
|
2012-06-15 15:10:40 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 22:33:52 +08:00
|
|
|
const BlendFunc &LabelBMFont::getBlendFunc() const
|
2012-06-15 15:10:40 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
return _label->getBlendFunc();
|
2012-06-15 15:10:40 +08:00
|
|
|
}
|
2012-06-12 14:33:53 +08:00
|
|
|
|
2014-05-17 15:40:36 +08:00
|
|
|
Node* LabelBMFont::getChildByTag(int tag) const
|
2013-12-13 06:30:22 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
return _label->getLetter(tag);
|
2013-12-13 06:30:22 +08:00
|
|
|
}
|
2012-06-12 14:33:53 +08:00
|
|
|
|
2014-03-28 14:18:01 +08:00
|
|
|
Sprite* LabelBMFont::getLetter(int ID)
|
|
|
|
{
|
|
|
|
return _label->getLetter(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LabelBMFont::setColor(const Color3B& color)
|
|
|
|
{
|
|
|
|
_label->setColor(color);
|
|
|
|
}
|
|
|
|
|
2014-03-28 15:58:28 +08:00
|
|
|
const Size& LabelBMFont::getContentSize() const
|
|
|
|
{
|
|
|
|
const_cast<LabelBMFont*>(this)->setContentSize(_label->getContentSize());
|
|
|
|
return _contentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect LabelBMFont::getBoundingBox() const
|
|
|
|
{
|
2014-08-02 08:04:29 +08:00
|
|
|
return Node::getBoundingBox();
|
2014-03-28 15:58:28 +08:00
|
|
|
}
|
2014-04-23 15:05:02 +08:00
|
|
|
#if CC_LABELBMFONT_DEBUG_DRAW
|
2014-05-31 07:42:05 +08:00
|
|
|
void LabelBMFont::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2014-04-22 23:18:23 +08:00
|
|
|
{
|
2014-11-24 17:21:14 +08:00
|
|
|
Node::draw(renderer, transform, _transformUpdated);
|
2014-04-23 15:05:02 +08:00
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
_debugDrawNode->clear();
|
2014-04-22 23:18:23 +08:00
|
|
|
auto size = getContentSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 vertices[4]=
|
2014-04-22 23:18:23 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2::ZERO,
|
|
|
|
Vec2(size.width, 0),
|
|
|
|
Vec2(size.width, size.height),
|
|
|
|
Vec2(0, size.height)
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
2014-09-11 15:39:56 +08:00
|
|
|
_debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
|
2014-04-22 23:18:23 +08:00
|
|
|
}
|
|
|
|
#endif
|
2011-11-09 22:11:36 +08:00
|
|
|
|
2014-03-28 14:18:01 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (pop)
|
|
|
|
#endif
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_END
|