2013-07-12 05:41:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga 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.
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
****************************************************************************/
|
2013-07-25 01:22:46 +08:00
|
|
|
#include "CCStringBMFont.h"
|
2013-07-12 05:41:03 +08:00
|
|
|
#include "cocoa/CCString.h"
|
|
|
|
#include "cocoa/CCDictionary.h"
|
|
|
|
#include "CCConfiguration.h"
|
|
|
|
#include "CCLabelTextFormatter.h"
|
|
|
|
#include "draw_nodes/CCDrawingPrimitives.h"
|
|
|
|
#include "sprite_nodes/CCSprite.h"
|
2013-07-20 07:04:23 +08:00
|
|
|
//#include "support/CCPointExtension.h"
|
2013-07-12 05:41:03 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "textures/CCTextureCache.h"
|
|
|
|
#include "support/ccUTF8.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
// The return value needs to be deleted by CC_SAFE_DELETE_ARRAY.
|
|
|
|
static unsigned short* copyUTF16StringNN(unsigned short* str)
|
|
|
|
{
|
|
|
|
int length = str ? cc_wcslen(str) : 0;
|
|
|
|
unsigned short* ret = new unsigned short[length+1];
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
ret[i] = str[i];
|
|
|
|
}
|
|
|
|
ret[length] = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//CCLabelBMFont
|
|
|
|
//
|
|
|
|
|
|
|
|
//LabelBMFont - Purge Cache
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::purgeCachedData()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
FNTConfigRemoveCache();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
StringBMFont * StringBMFont::create()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
2013-07-25 01:22:46 +08:00
|
|
|
StringBMFont * pRet = new StringBMFont();
|
2013-07-12 05:41:03 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
StringBMFont * StringBMFont::create(const char *str, const char *fntFile, float width, TextHAlignment alignment)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
2013-07-25 01:22:46 +08:00
|
|
|
return StringBMFont::create(str, fntFile, width, alignment, Point::ZERO);
|
2013-07-12 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
StringBMFont * StringBMFont::create(const char *str, const char *fntFile, float width)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
2013-07-27 07:04:21 +08:00
|
|
|
return StringBMFont::create(str, fntFile, width, TextHAlignment::LEFT, Point::ZERO);
|
2013-07-12 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
StringBMFont * StringBMFont::create(const char *str, const char *fntFile)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
2013-07-27 07:04:21 +08:00
|
|
|
return StringBMFont::create(str, fntFile, kLabelAutomaticWidth, TextHAlignment::LEFT, Point::ZERO);
|
2013-07-12 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//LabelBMFont - Creation & Init
|
2013-07-27 07:04:21 +08:00
|
|
|
StringBMFont *StringBMFont::create(const char *str, const char *fntFile, float width/* = kLabelAutomaticWidth*/, TextHAlignment alignment/* = kTextAlignmentLeft*/, Point imageOffset/* = Point::ZERO*/)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
2013-07-25 01:22:46 +08:00
|
|
|
StringBMFont *pRet = new StringBMFont();
|
2013-07-12 05:41:03 +08:00
|
|
|
if(pRet && pRet->initWithString(str, fntFile, width, alignment, imageOffset))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
bool StringBMFont::init()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
2013-07-27 07:04:21 +08:00
|
|
|
return initWithString(NULL, NULL, kLabelAutomaticWidth, TextHAlignment::LEFT, Point::ZERO);
|
2013-07-12 05:41:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
bool StringBMFont::initWithString(const char *theString, const char *fntFile, float width/* = kLabelAutomaticWidth*/, TextHAlignment alignment/* = kTextAlignmentLeft*/, Point imageOffset/* = Point::ZERO*/)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
CCAssert(!_configuration, "re-init is no longer supported");
|
2013-07-25 01:22:46 +08:00
|
|
|
CCAssert( (theString && fntFile) || (theString==NULL && fntFile==NULL), "Invalid params for StringBMFont");
|
2013-07-12 05:41:03 +08:00
|
|
|
|
|
|
|
Texture2D *texture = NULL;
|
|
|
|
|
|
|
|
if (fntFile)
|
|
|
|
{
|
|
|
|
CCBMFontConfiguration *newConf = FNTConfigLoadFile(fntFile);
|
|
|
|
if (!newConf)
|
|
|
|
{
|
2013-07-25 01:22:46 +08:00
|
|
|
CCLOG("cocos2d: WARNING. StringBMFont: Impossible to create font. Please check file: '%s'", fntFile);
|
2013-07-12 05:41:03 +08:00
|
|
|
release();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
newConf->retain();
|
|
|
|
CC_SAFE_RELEASE(_configuration);
|
|
|
|
_configuration = newConf;
|
|
|
|
|
|
|
|
_fntFile = fntFile;
|
|
|
|
|
2013-07-20 07:04:23 +08:00
|
|
|
texture = TextureCache::getInstance()->addImage(_configuration->getAtlasName());
|
2013-07-12 05:41:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texture = new Texture2D();
|
|
|
|
texture->autorelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (theString == NULL)
|
|
|
|
{
|
|
|
|
theString = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SpriteBatchNode::initWithTexture(texture, strlen(theString)))
|
|
|
|
{
|
|
|
|
_width = width;
|
|
|
|
_alignment = alignment;
|
|
|
|
|
|
|
|
_displayedOpacity = _realOpacity = 255;
|
|
|
|
_displayedColor = _realColor = Color3B::WHITE;
|
|
|
|
_cascadeOpacityEnabled = true;
|
|
|
|
_cascadeColorEnabled = true;
|
|
|
|
|
2013-07-20 07:04:23 +08:00
|
|
|
_contentSize = Size::ZERO;
|
2013-07-12 05:41:03 +08:00
|
|
|
|
|
|
|
_isOpacityModifyRGB = _textureAtlas->getTexture()->hasPremultipliedAlpha();
|
2013-07-20 07:04:23 +08:00
|
|
|
_anchorPoint = Point(0.5f, 0.5f);
|
2013-07-12 05:41:03 +08:00
|
|
|
|
|
|
|
_imageOffset = imageOffset;
|
|
|
|
|
|
|
|
_reusedChar = new Sprite();
|
2013-07-20 07:04:23 +08:00
|
|
|
_reusedChar->initWithTexture(_textureAtlas->getTexture(), Rect(0, 0, 0, 0), false);
|
2013-07-12 05:41:03 +08:00
|
|
|
_reusedChar->setBatchNode(this);
|
|
|
|
|
|
|
|
this->setString(theString);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
StringBMFont::StringBMFont()
|
2013-07-12 05:41:03 +08:00
|
|
|
: _string(NULL)
|
|
|
|
, _initialString(NULL)
|
2013-07-27 07:04:21 +08:00
|
|
|
, _alignment(TextHAlignment::CENTER)
|
2013-07-12 05:41:03 +08:00
|
|
|
, _width(-1.0f)
|
|
|
|
, _configuration(NULL)
|
|
|
|
, _lineBreakWithoutSpaces(false)
|
2013-07-20 07:04:23 +08:00
|
|
|
, _imageOffset(Point::ZERO)
|
2013-07-12 05:41:03 +08:00
|
|
|
, _reusedChar(NULL)
|
|
|
|
, _displayedOpacity(255)
|
|
|
|
, _realOpacity(255)
|
|
|
|
, _displayedColor(Color3B::WHITE)
|
|
|
|
, _realColor(Color3B::WHITE)
|
|
|
|
, _cascadeColorEnabled(true)
|
|
|
|
, _cascadeOpacityEnabled(true)
|
|
|
|
, _isOpacityModifyRGB(false)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
StringBMFont::~StringBMFont()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_reusedChar);
|
|
|
|
CC_SAFE_DELETE_ARRAY(_string);
|
|
|
|
CC_SAFE_DELETE_ARRAY(_initialString);
|
|
|
|
CC_SAFE_RELEASE(_configuration);
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
// StringBMFont - Atlas generation
|
|
|
|
int StringBMFont::kerningAmountForFirst(unsigned short first, unsigned short second)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned int key = (first<<16) | (second & 0xffff);
|
|
|
|
|
|
|
|
if( _configuration->_kerningDictionary ) {
|
|
|
|
tKerningHashElement *element = NULL;
|
|
|
|
HASH_FIND_INT(_configuration->_kerningDictionary, &key, element);
|
|
|
|
if(element)
|
|
|
|
ret = element->amount;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
const char* StringBMFont::getString(void) const
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _initialStringUTF8.c_str();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
//StringBMFont - RGBAProtocol protocol
|
|
|
|
const Color3B& StringBMFont::getColor() const
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _realColor;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
const Color3B& StringBMFont::getDisplayedColor() const
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _displayedColor;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setColor(const Color3B& color)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
_displayedColor = _realColor = color;
|
|
|
|
|
|
|
|
if( _cascadeColorEnabled ) {
|
|
|
|
Color3B parentColor = Color3B::WHITE;
|
|
|
|
RGBAProtocol* pParent = dynamic_cast<RGBAProtocol*>(_parent);
|
|
|
|
if (pParent && pParent->isCascadeColorEnabled())
|
|
|
|
{
|
|
|
|
parentColor = pParent->getDisplayedColor();
|
|
|
|
}
|
|
|
|
this->updateDisplayedColor(parentColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
GLubyte StringBMFont::getOpacity(void) const
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _realOpacity;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
GLubyte StringBMFont::getDisplayedOpacity(void) const
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _displayedOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Override synthesized setOpacity to recurse items */
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setOpacity(GLubyte opacity)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity = opacity;
|
|
|
|
|
|
|
|
if( _cascadeOpacityEnabled ) {
|
|
|
|
GLubyte parentOpacity = 255;
|
|
|
|
RGBAProtocol* pParent = dynamic_cast<RGBAProtocol*>(_parent);
|
|
|
|
if (pParent && pParent->isCascadeOpacityEnabled())
|
|
|
|
{
|
|
|
|
parentOpacity = pParent->getDisplayedOpacity();
|
|
|
|
}
|
|
|
|
this->updateDisplayedOpacity(parentOpacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setOpacityModifyRGB(bool var)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
_isOpacityModifyRGB = var;
|
|
|
|
if (_children && _children->count() != 0)
|
|
|
|
{
|
|
|
|
Object* child;
|
|
|
|
CCARRAY_FOREACH(_children, child)
|
|
|
|
{
|
|
|
|
Node* pNode = static_cast<Node*>( child );
|
|
|
|
if (pNode)
|
|
|
|
{
|
|
|
|
RGBAProtocol *pRGBAProtocol = dynamic_cast<RGBAProtocol*>(pNode);
|
|
|
|
if (pRGBAProtocol)
|
|
|
|
{
|
|
|
|
pRGBAProtocol->setOpacityModifyRGB(_isOpacityModifyRGB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
bool StringBMFont::isOpacityModifyRGB() const
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _isOpacityModifyRGB;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::updateDisplayedOpacity(GLubyte parentOpacity)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity * parentOpacity/255.0;
|
|
|
|
|
|
|
|
Object* pObj;
|
|
|
|
CCARRAY_FOREACH(_children, pObj)
|
|
|
|
{
|
|
|
|
Sprite *item = static_cast<Sprite*>( pObj );
|
|
|
|
item->updateDisplayedOpacity(_displayedOpacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::updateDisplayedColor(const Color3B& parentColor)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
_displayedColor.r = _realColor.r * parentColor.r/255.0;
|
|
|
|
_displayedColor.g = _realColor.g * parentColor.g/255.0;
|
|
|
|
_displayedColor.b = _realColor.b * parentColor.b/255.0;
|
|
|
|
|
|
|
|
Object* pObj;
|
|
|
|
CCARRAY_FOREACH(_children, pObj)
|
|
|
|
{
|
|
|
|
Sprite *item = static_cast<Sprite*>( pObj );
|
|
|
|
item->updateDisplayedColor(_displayedColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
bool StringBMFont::isCascadeColorEnabled() const
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setCascadeColorEnabled(bool cascadeColorEnabled)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
_cascadeColorEnabled = cascadeColorEnabled;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
bool StringBMFont::isCascadeOpacityEnabled() const
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
_cascadeOpacityEnabled = cascadeOpacityEnabled;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
// StringBMFont - AnchorPoint
|
|
|
|
void StringBMFont::setAnchorPoint(const Point& point)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
if( ! point.equals(_anchorPoint))
|
|
|
|
{
|
|
|
|
SpriteBatchNode::setAnchorPoint(point);
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
// StringBMFont - Alignment
|
2013-07-27 07:04:21 +08:00
|
|
|
void StringBMFont::setAlignment(TextHAlignment alignment)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
this->_alignment = alignment;
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setWidth(float width)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
this->_width = width;
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setLineBreakWithoutSpace( bool breakWithoutSpace )
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
_lineBreakWithoutSpaces = breakWithoutSpace;
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setScale(float scale)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
SpriteBatchNode::setScale(scale);
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setScaleX(float scaleX)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
SpriteBatchNode::setScaleX(scaleX);
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setScaleY(float scaleY)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
SpriteBatchNode::setScaleY(scaleY);
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
float StringBMFont::getLetterPosXLeft( Sprite* sp )
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return sp->getPosition().x * _scaleX - (sp->getContentSize().width * _scaleX * sp->getAnchorPoint().x);
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
float StringBMFont::getLetterPosXRight( Sprite* sp )
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return sp->getPosition().x * _scaleX + (sp->getContentSize().width * _scaleX * sp->getAnchorPoint().x);
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
// StringBMFont - FntFile
|
|
|
|
void StringBMFont::setFntFile(const char* fntFile)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
if (fntFile != NULL && strcmp(fntFile, _fntFile.c_str()) != 0 )
|
|
|
|
{
|
|
|
|
CCBMFontConfiguration *newConf = FNTConfigLoadFile(fntFile);
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
CCAssert( newConf, "CCStringBMFont: Impossible to create font. Please check file");
|
2013-07-12 05:41:03 +08:00
|
|
|
|
|
|
|
_fntFile = fntFile;
|
|
|
|
|
|
|
|
CC_SAFE_RETAIN(newConf);
|
|
|
|
CC_SAFE_RELEASE(_configuration);
|
|
|
|
_configuration = newConf;
|
|
|
|
|
2013-07-20 07:04:23 +08:00
|
|
|
this->setTexture(TextureCache::getInstance()->addImage(_configuration->getAtlasName()));
|
2013-07-12 05:41:03 +08:00
|
|
|
LabelTextFormatter::createStringSprites(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
const char* StringBMFont::getFntFile()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _fntFile.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
//StringBMFont - Debug draw
|
2013-07-12 05:41:03 +08:00
|
|
|
#if CC_LabelBMFontNew_DEBUG_DRAW
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::draw()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
SpriteBatchNode::draw();
|
|
|
|
const Size& s = this->getContentSize();
|
|
|
|
Point vertices[4]={
|
|
|
|
ccp(0,0),ccp(s.width,0),
|
|
|
|
ccp(s.width,s.height),ccp(0,s.height),
|
|
|
|
};
|
|
|
|
ccDrawPoly(vertices, 4, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CC_LABELBMFONT_DEBUG_DRAW
|
|
|
|
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
int StringBMFont::getCommonLineHeight()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
if (_configuration)
|
|
|
|
{
|
|
|
|
return _configuration->_commonHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
int StringBMFont::getKerningForCharsPair(unsigned short first, unsigned short second)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return this->kerningAmountForFirst(first, second);
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
ccBMFontDef StringBMFont::getFontDefForChar(unsigned short int theChar)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
ccBMFontDef fontDef;
|
|
|
|
tFontDefHashElement *element = NULL;
|
|
|
|
unsigned int key = theChar;
|
|
|
|
|
|
|
|
HASH_FIND_INT(_configuration->_fontDefDictionary, &key, element);
|
|
|
|
|
|
|
|
if (element)
|
|
|
|
{
|
|
|
|
fontDef = element->fontDef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fontDef;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a sprite for rendering one letter
|
2013-07-25 01:22:46 +08:00
|
|
|
Sprite * StringBMFont::getSpriteForChar(unsigned short int theChar, int spriteIndexHint)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
Rect rect;
|
|
|
|
ccBMFontDef fontDef;
|
|
|
|
Sprite *pRetSprite = 0;
|
|
|
|
|
|
|
|
// unichar is a short, and an int is needed on HASH_FIND_INT
|
|
|
|
tFontDefHashElement *element = NULL;
|
|
|
|
unsigned int key = theChar;
|
|
|
|
|
|
|
|
HASH_FIND_INT(_configuration->_fontDefDictionary, &key, element);
|
|
|
|
|
|
|
|
if (! element)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fontDef = element->fontDef;
|
|
|
|
|
|
|
|
rect = fontDef.rect;
|
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
|
|
|
|
|
|
|
rect.origin.x += _imageOffset.x;
|
|
|
|
rect.origin.y += _imageOffset.y;
|
|
|
|
|
|
|
|
//bool hasSprite = true;
|
|
|
|
pRetSprite = (Sprite*)(this->getChildByTag(spriteIndexHint));
|
|
|
|
|
|
|
|
if(pRetSprite )
|
|
|
|
{
|
|
|
|
// Reusing previous Sprite
|
|
|
|
pRetSprite->setVisible(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pRetSprite = new Sprite();
|
|
|
|
pRetSprite->initWithTexture(_textureAtlas->getTexture(), rect);
|
|
|
|
addChild(pRetSprite, spriteIndexHint, spriteIndexHint);
|
|
|
|
pRetSprite->release();
|
|
|
|
|
|
|
|
// Apply label properties
|
|
|
|
pRetSprite->setOpacityModifyRGB(_isOpacityModifyRGB);
|
|
|
|
|
|
|
|
// Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on
|
|
|
|
pRetSprite->updateDisplayedColor(_displayedColor);
|
|
|
|
pRetSprite->updateDisplayedOpacity(_displayedOpacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// updating previous sprite
|
|
|
|
pRetSprite->setTextureRect(rect, false, rect.size);
|
|
|
|
return pRetSprite;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
int StringBMFont::getStringNumLines()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
int quantityOfLines = 1;
|
|
|
|
|
|
|
|
unsigned int stringLen = _string ? cc_wcslen(_string) : 0;
|
|
|
|
if (stringLen == 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
// count number of lines
|
|
|
|
for (unsigned int i = 0; i < stringLen - 1; ++i)
|
|
|
|
{
|
|
|
|
unsigned short c = _string[i];
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
quantityOfLines++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return quantityOfLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need cross implementation
|
2013-07-25 01:22:46 +08:00
|
|
|
int StringBMFont::getStringLenght()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _string ? cc_wcslen(_string) : 0;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
unsigned short StringBMFont::getCharAtStringPosition(int position)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _string[position];
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
int StringBMFont::getXOffsetForChar(unsigned short c)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
ccBMFontDef fontDef = getFontDefForChar(c);
|
|
|
|
return fontDef.xOffset;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
int StringBMFont::getYOffsetForChar(unsigned short c)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
ccBMFontDef fontDef = getFontDefForChar(c);
|
|
|
|
return fontDef.yOffset;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
Rect StringBMFont::getRectForChar(unsigned short c)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
ccBMFontDef fontDef = getFontDefForChar(c);
|
|
|
|
return fontDef.rect;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
int StringBMFont::getAdvanceForChar(unsigned short c, int hintPositionInString)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
ccBMFontDef fontDef = getFontDefForChar(c);
|
|
|
|
return fontDef.xAdvance;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setLabelContentSize(const Size &newSize)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
setContentSize(newSize);
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::createStringSprites()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
LabelTextFormatter::createStringSprites(this);
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setString(const char *newString)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
// store initial string in char8 format
|
|
|
|
_initialStringUTF8 = newString;
|
|
|
|
|
|
|
|
// update the initial string if needed
|
|
|
|
unsigned short* utf16String = cc_utf8_to_utf16(newString);
|
|
|
|
unsigned short* tmp = _initialString;
|
|
|
|
_initialString = copyUTF16StringNN(utf16String);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(tmp);
|
|
|
|
CC_SAFE_DELETE_ARRAY(utf16String);
|
|
|
|
|
|
|
|
// do the rest of the josb
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::setCString(const char *label)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
setString(label);
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::updateLabel()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
if ( _initialString!=0 )
|
|
|
|
{
|
|
|
|
// set the new string
|
|
|
|
CC_SAFE_DELETE_ARRAY(_string);
|
|
|
|
_string = copyUTF16StringNN(_initialString);
|
|
|
|
|
|
|
|
// hide all the letters and create or recicle sprites for the new letters
|
|
|
|
updateLetterSprites();
|
|
|
|
|
|
|
|
// format the text on more than line
|
|
|
|
multilineText();
|
|
|
|
|
|
|
|
// align the text (left - center - right)
|
|
|
|
alignText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::updateLetterSprites()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
// hide all the letters
|
|
|
|
hideStringSprites();
|
|
|
|
|
|
|
|
// create new letters sprites
|
|
|
|
createStringSprites();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::hideStringSprites()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
if (_children && _children->count() != 0)
|
|
|
|
{
|
|
|
|
Object* child;
|
|
|
|
CCARRAY_FOREACH(_children, child)
|
|
|
|
{
|
|
|
|
Node* pNode = (Node*) child;
|
|
|
|
if (pNode)
|
|
|
|
{
|
|
|
|
pNode->setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::multilineText()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
if (_width > 0)
|
|
|
|
{
|
|
|
|
// format on more than one line
|
|
|
|
LabelTextFormatter::multilineText(this);
|
|
|
|
|
|
|
|
// hide all the letter sprites and create/reclaim letters sprite with new position
|
|
|
|
updateLetterSprites();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::alignText()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
2013-07-27 07:04:21 +08:00
|
|
|
if (_alignment != TextHAlignment::LEFT)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
LabelTextFormatter::alignText(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
unsigned short * StringBMFont::getUTF8String()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _string;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
Sprite * StringBMFont::getSpriteChild(int ID)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return (Sprite*)this->getChildByTag(ID);
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
float StringBMFont::getMaxLineWidth()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _width;
|
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
TextHAlignment StringBMFont::getTextAlignment()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _alignment;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
Array* StringBMFont::getChildrenLetters()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _children;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
void StringBMFont::assignNewUTF8String(unsigned short *newString)
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_DELETE_ARRAY(_string);
|
|
|
|
_string = newString;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
Size StringBMFont::getLabelContentSize()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return getContentSize();
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
bool StringBMFont::breakLineWithoutSpace()
|
2013-07-12 05:41:03 +08:00
|
|
|
{
|
|
|
|
return _lineBreakWithoutSpaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|