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)
|
|
|
|
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCLabelBMFont.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCDrawingPrimitives.h"
|
2014-02-20 22:33:52 +08:00
|
|
|
#include "CCString.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCSprite.h"
|
2011-11-09 22:11:36 +08:00
|
|
|
|
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
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelBMFont * pRet = new 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-02-20 22:33:52 +08:00
|
|
|
LabelBMFont *LabelBMFont::create(const std::string& str, const std::string& fntFile, float width /* = 0 */, TextHAlignment alignment /* = TextHAlignment::LEFT */,const Point& imageOffset /* = Point::ZERO */)
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
LabelBMFont *ret = new 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-02-20 22:33:52 +08:00
|
|
|
bool LabelBMFont::initWithString(const std::string& str, const std::string& fntFile, float width /* = 0 */, TextHAlignment alignment /* = TextHAlignment::LEFT */,const Point& imageOffset /* = Point::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-03-25 09:58:16 +08:00
|
|
|
_label->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
|
2014-02-20 22:33:52 +08:00
|
|
|
this->addChild(_label);
|
|
|
|
this->setAnchorPoint(Point::ANCHOR_MIDDLE);
|
|
|
|
_cascadeOpacityEnabled = true;
|
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
|
|
|
|
void LabelBMFont::setFntFile(const std::string& fntFile, const Point& imageOffset /* = Point::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-02-20 22:33:52 +08:00
|
|
|
Node* LabelBMFont::getChildByTag(int tag)
|
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
|
|
|
|
{
|
|
|
|
return _label->getBoundingBox();
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
//LabelBMFont - Debug draw
|
2011-07-04 10:59:35 +08:00
|
|
|
#if CC_LABELBMFONT_DEBUG_DRAW
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelBMFont::draw()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
const Size& s = this->getContentSize();
|
|
|
|
Point vertices[4]={
|
2013-07-12 18:04:32 +08:00
|
|
|
Point(0,0),Point(s.width,0),
|
|
|
|
Point(s.width,s.height),Point(0,s.height),
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
ccDrawPoly(vertices, 4, true);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2011-07-04 10:59:35 +08:00
|
|
|
#endif // CC_LABELBMFONT_DEBUG_DRAW
|
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
|