2011-11-09 22:11:36 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-11-09 22:11:36 +08:00
|
|
|
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)
|
|
|
|
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCLabelBMFont.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCString.h"
|
2011-11-09 22:11:36 +08:00
|
|
|
#include "platform/platform.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCDictionary.h"
|
2011-11-09 22:11:36 +08:00
|
|
|
#include "CCConfiguration.h"
|
2012-11-16 14:23:14 +08:00
|
|
|
#include "draw_nodes/CCDrawingPrimitives.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "sprite_nodes/CCSprite.h"
|
|
|
|
#include "support/CCPointExtension.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2012-03-22 09:46:07 +08:00
|
|
|
#include "CCDirector.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "textures/CCTextureCache.h"
|
2013-02-27 15:16:49 +08:00
|
|
|
#include "support/ccUTF8.h"
|
2011-11-09 22:11:36 +08:00
|
|
|
|
2012-04-10 15:14:55 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_BEGIN
|
2013-03-20 13:55:43 +08:00
|
|
|
|
|
|
|
// The return value needs to be deleted by CC_SAFE_DELETE_ARRAY.
|
|
|
|
static unsigned short* copyUTF16StringN(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;
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
|
|
|
//FNTConfig Cache - free functions
|
|
|
|
//
|
2012-06-19 16:31:26 +08:00
|
|
|
static CCDictionary* s_pConfigurations = NULL;
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCBMFontConfiguration* FNTConfigLoadFile( const char *fntFile)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCBMFontConfiguration* pRet = NULL;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2012-06-19 16:31:26 +08:00
|
|
|
if( s_pConfigurations == NULL )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-19 16:31:26 +08:00
|
|
|
s_pConfigurations = new CCDictionary();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2012-06-19 16:31:26 +08:00
|
|
|
pRet = (CCBMFontConfiguration*)s_pConfigurations->objectForKey(fntFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
if( pRet == NULL )
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
pRet = CCBMFontConfiguration::create(fntFile);
|
2012-06-08 15:29:47 +08:00
|
|
|
if (pRet)
|
|
|
|
{
|
2012-06-19 16:31:26 +08:00
|
|
|
s_pConfigurations->setObject(pRet, fntFile);
|
2012-06-08 15:29:47 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-01-18 18:02:39 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pRet;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-01-18 18:02:39 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void FNTConfigRemoveCache( void )
|
|
|
|
{
|
2012-06-19 16:31:26 +08:00
|
|
|
if (s_pConfigurations)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-19 16:31:26 +08:00
|
|
|
s_pConfigurations->removeAllObjects();
|
|
|
|
CC_SAFE_RELEASE_NULL(s_pConfigurations);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
|
|
|
//BitmapFontConfiguration
|
|
|
|
//
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCBMFontConfiguration * CCBMFontConfiguration::create(const char *FNTfile)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCBMFontConfiguration * pRet = new CCBMFontConfiguration();
|
|
|
|
if (pRet->initWithFNTfile(FNTfile))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCBMFontConfiguration::initWithFNTfile(const char *FNTfile)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_kerningDictionary = NULL;
|
|
|
|
_fontDefDictionary = NULL;
|
2012-11-15 17:16:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_characterSet = this->parseConfigFile(FNTfile);
|
2012-11-15 17:16:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _characterSet)
|
2012-06-08 15:29:47 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
std::set<unsigned int>* CCBMFontConfiguration::getCharacterSet() const
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _characterSet;
|
2012-11-15 17:16:51 +08:00
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCBMFontConfiguration::CCBMFontConfiguration()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _fontDefDictionary(NULL)
|
|
|
|
, _commonHeight(0)
|
|
|
|
, _kerningDictionary(NULL)
|
|
|
|
, _characterSet(NULL)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCBMFontConfiguration::~CCBMFontConfiguration()
|
|
|
|
{
|
2013-06-06 09:21:05 +08:00
|
|
|
CCLOGINFO( "cocos2d: deallocing CCBMFontConfiguration %p", this );
|
2012-04-19 14:35:52 +08:00
|
|
|
this->purgeFontDefDictionary();
|
|
|
|
this->purgeKerningDictionary();
|
2013-06-15 14:03:30 +08:00
|
|
|
_atlasName.clear();
|
|
|
|
CC_SAFE_DELETE(_characterSet);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
|
|
|
const char* CCBMFontConfiguration::description(void)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-14 16:05:58 +08:00
|
|
|
return CCString::createWithFormat(
|
2013-04-19 17:56:27 +08:00
|
|
|
"<CCBMFontConfiguration = " CC_FORMAT_PRINTF_SIZE_T " | Glphys:%d Kernings:%d | Image = %s>",
|
|
|
|
(size_t)this,
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_COUNT(_fontDefDictionary),
|
|
|
|
HASH_COUNT(_kerningDictionary),
|
|
|
|
_atlasName.c_str()
|
2012-04-19 14:35:52 +08:00
|
|
|
)->getCString();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCBMFontConfiguration::purgeKerningDictionary()
|
|
|
|
{
|
2012-11-15 17:16:51 +08:00
|
|
|
tCCKerningHashElement *current;
|
2013-06-15 14:03:30 +08:00
|
|
|
while(_kerningDictionary)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
current = _kerningDictionary;
|
|
|
|
HASH_DEL(_kerningDictionary,current);
|
2012-04-19 14:35:52 +08:00
|
|
|
free(current);
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-04-06 23:33:42 +08:00
|
|
|
void CCBMFontConfiguration::purgeFontDefDictionary()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-11-15 17:16:51 +08:00
|
|
|
tCCFontDefHashElement *current, *tmp;
|
2012-04-06 23:33:42 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_ITER(hh, _fontDefDictionary, current, tmp) {
|
|
|
|
HASH_DEL(_fontDefDictionary, current);
|
2012-04-19 14:35:52 +08:00
|
|
|
free(current);
|
|
|
|
}
|
2012-04-06 23:33:42 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
std::set<unsigned int>* CCBMFontConfiguration::parseConfigFile(const char *controlFile)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-01-25 20:51:52 +08:00
|
|
|
std::string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename(controlFile);
|
2012-06-27 17:04:54 +08:00
|
|
|
CCString *contents = CCString::createWithContentsOfFile(fullpath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-08 15:29:47 +08:00
|
|
|
CCAssert(contents, "CCBMFontConfiguration::parseConfigFile | Open file error.");
|
2012-11-15 17:16:51 +08:00
|
|
|
|
|
|
|
set<unsigned int> *validCharsString = new set<unsigned int>();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-08 15:29:47 +08:00
|
|
|
if (!contents)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-08 15:29:47 +08:00
|
|
|
CCLOG("cocos2d: Error parsing FNTfile %s", controlFile);
|
2012-11-15 17:16:51 +08:00
|
|
|
return NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse spacing / padding
|
|
|
|
std::string line;
|
2012-06-08 15:29:47 +08:00
|
|
|
std::string strLeft = contents->getCString();
|
2012-04-19 14:35:52 +08:00
|
|
|
while (strLeft.length() > 0)
|
|
|
|
{
|
|
|
|
int pos = strLeft.find('\n');
|
|
|
|
|
|
|
|
if (pos != (int)std::string::npos)
|
|
|
|
{
|
|
|
|
// the data is more than a line.get one line
|
|
|
|
line = strLeft.substr(0, pos);
|
|
|
|
strLeft = strLeft.substr(pos + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get the left data
|
|
|
|
line = strLeft;
|
|
|
|
strLeft.erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(line.substr(0,strlen("info face")) == "info face")
|
|
|
|
{
|
|
|
|
// XXX: info parsing is incomplete
|
|
|
|
// Not needed for the Hiero editors, but needed for the AngelCode editor
|
|
|
|
// [self parseInfoArguments:line];
|
|
|
|
this->parseInfoArguments(line);
|
|
|
|
}
|
|
|
|
// Check to see if the start of the line is something we are interested in
|
|
|
|
else if(line.substr(0,strlen("common lineHeight")) == "common lineHeight")
|
|
|
|
{
|
|
|
|
this->parseCommonArguments(line);
|
|
|
|
}
|
|
|
|
else if(line.substr(0,strlen("page id")) == "page id")
|
|
|
|
{
|
|
|
|
this->parseImageFileName(line, controlFile);
|
|
|
|
}
|
|
|
|
else if(line.substr(0,strlen("chars c")) == "chars c")
|
|
|
|
{
|
|
|
|
// Ignore this line
|
|
|
|
}
|
|
|
|
else if(line.substr(0,strlen("char")) == "char")
|
|
|
|
{
|
|
|
|
// Parse the current line and create a new CharDef
|
2012-11-15 17:16:51 +08:00
|
|
|
tCCFontDefHashElement* element = (tCCFontDefHashElement*)malloc( sizeof(*element) );
|
2012-04-19 14:35:52 +08:00
|
|
|
this->parseCharacterDefinition(line, &element->fontDef);
|
|
|
|
|
|
|
|
element->key = element->fontDef.charID;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_ADD_INT(_fontDefDictionary, key, element);
|
2012-11-15 17:16:51 +08:00
|
|
|
|
|
|
|
validCharsString->insert(element->fontDef.charID);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-08 15:29:47 +08:00
|
|
|
// else if(line.substr(0,strlen("kernings count")) == "kernings count")
|
|
|
|
// {
|
|
|
|
// this->parseKerningCapacity(line);
|
|
|
|
// }
|
2012-04-19 14:35:52 +08:00
|
|
|
else if(line.substr(0,strlen("kerning first")) == "kerning first")
|
|
|
|
{
|
|
|
|
this->parseKerningEntry(line);
|
|
|
|
}
|
|
|
|
}
|
2012-06-08 15:29:47 +08:00
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
return validCharsString;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCBMFontConfiguration::parseImageFileName(std::string line, const char *fntFile)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// line to parse:
|
|
|
|
// page id=0 file="bitmapFontTest.png"
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// page ID. Sanity check
|
|
|
|
int index = line.find('=')+1;
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
|
|
|
CCAssert(atoi(value.c_str()) == 0, "LabelBMFont file could not be found");
|
|
|
|
// file
|
|
|
|
index = line.find('"')+1;
|
|
|
|
index2 = line.find('"', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_atlasName = CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile(value.c_str(), fntFile);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCBMFontConfiguration::parseInfoArguments(std::string line)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// possible lines to parse:
|
|
|
|
// info face="Script" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=1,4,3,2 spacing=0,0 outline=0
|
|
|
|
// info face="Cracked" size=36 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// padding
|
|
|
|
int index = line.find("padding=");
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
2013-06-15 14:03:30 +08:00
|
|
|
sscanf(value.c_str(), "padding=%d,%d,%d,%d", &_padding.top, &_padding.right, &_padding.bottom, &_padding.left);
|
|
|
|
CCLOG("cocos2d: padding: %d,%d,%d,%d", _padding.left, _padding.top, _padding.right, _padding.bottom);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCBMFontConfiguration::parseCommonArguments(std::string line)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// line to parse:
|
|
|
|
// common lineHeight=104 base=26 scaleW=1024 scaleH=512 pages=1 packed=0
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Height
|
|
|
|
int index = line.find("lineHeight=");
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
2013-06-15 14:03:30 +08:00
|
|
|
sscanf(value.c_str(), "lineHeight=%d", &_commonHeight);
|
2012-04-19 14:35:52 +08:00
|
|
|
// scaleW. sanity check
|
|
|
|
index = line.find("scaleW=") + strlen("scaleW=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
CCAssert(atoi(value.c_str()) <= CCConfiguration::sharedConfiguration()->getMaxTextureSize(), "CCLabelBMFont: page can't be larger than supported");
|
|
|
|
// scaleH. sanity check
|
|
|
|
index = line.find("scaleH=") + strlen("scaleH=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
CCAssert(atoi(value.c_str()) <= CCConfiguration::sharedConfiguration()->getMaxTextureSize(), "CCLabelBMFont: page can't be larger than supported");
|
|
|
|
// pages. sanity check
|
|
|
|
index = line.find("pages=") + strlen("pages=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
CCAssert(atoi(value.c_str()) == 1, "CCBitfontAtlas: only supports 1 page");
|
|
|
|
|
|
|
|
// packed (ignore) What does this mean ??
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCBMFontConfiguration::parseCharacterDefinition(std::string line, ccBMFontDef *characterDefinition)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// line to parse:
|
|
|
|
// char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=44 xadvance=14 page=0 chnl=0
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Character ID
|
|
|
|
int index = line.find("id=");
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "id=%u", &characterDefinition->charID);
|
|
|
|
|
|
|
|
// Character x
|
|
|
|
index = line.find("x=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "x=%f", &characterDefinition->rect.origin.x);
|
|
|
|
// Character y
|
|
|
|
index = line.find("y=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "y=%f", &characterDefinition->rect.origin.y);
|
|
|
|
// Character width
|
|
|
|
index = line.find("width=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "width=%f", &characterDefinition->rect.size.width);
|
|
|
|
// Character height
|
|
|
|
index = line.find("height=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "height=%f", &characterDefinition->rect.size.height);
|
|
|
|
// Character xoffset
|
|
|
|
index = line.find("xoffset=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
2012-08-16 18:09:01 +08:00
|
|
|
sscanf(value.c_str(), "xoffset=%hd", &characterDefinition->xOffset);
|
2012-04-19 14:35:52 +08:00
|
|
|
// Character yoffset
|
|
|
|
index = line.find("yoffset=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
2012-08-16 18:09:01 +08:00
|
|
|
sscanf(value.c_str(), "yoffset=%hd", &characterDefinition->yOffset);
|
2012-04-19 14:35:52 +08:00
|
|
|
// Character xadvance
|
|
|
|
index = line.find("xadvance=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
2012-08-16 18:09:01 +08:00
|
|
|
sscanf(value.c_str(), "xadvance=%hd", &characterDefinition->xAdvance);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCBMFontConfiguration::parseKerningEntry(std::string line)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// line to parse:
|
|
|
|
// kerning first=121 second=44 amount=-7
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// first
|
|
|
|
int first;
|
|
|
|
int index = line.find("first=");
|
|
|
|
int index2 = line.find(' ', index);
|
|
|
|
std::string value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "first=%d", &first);
|
|
|
|
|
|
|
|
// second
|
|
|
|
int second;
|
|
|
|
index = line.find("second=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "second=%d", &second);
|
|
|
|
|
|
|
|
// amount
|
|
|
|
int amount;
|
|
|
|
index = line.find("amount=");
|
|
|
|
index2 = line.find(' ', index);
|
|
|
|
value = line.substr(index, index2-index);
|
|
|
|
sscanf(value.c_str(), "amount=%d", &amount);
|
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
tCCKerningHashElement *element = (tCCKerningHashElement *)calloc( sizeof( *element ), 1 );
|
2012-04-19 14:35:52 +08:00
|
|
|
element->amount = amount;
|
|
|
|
element->key = (first<<16) | (second&0xffff);
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_ADD_INT(_kerningDictionary,key, element);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
//
|
|
|
|
//CCLabelBMFont
|
|
|
|
//
|
|
|
|
|
|
|
|
//LabelBMFont - Purge Cache
|
|
|
|
void CCLabelBMFont::purgeCachedData()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
FNTConfigRemoveCache();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
CCLabelBMFont * CCLabelBMFont::create()
|
|
|
|
{
|
|
|
|
CCLabelBMFont * pRet = new CCLabelBMFont();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-06-14 18:32:44 +08:00
|
|
|
|
2012-11-02 10:06:48 +08:00
|
|
|
CCLabelBMFont * CCLabelBMFont::create(const char *str, const char *fntFile, float width, CCTextAlignment alignment)
|
|
|
|
{
|
|
|
|
return CCLabelBMFont::create(str, fntFile, width, alignment, CCPointZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLabelBMFont * CCLabelBMFont::create(const char *str, const char *fntFile, float width)
|
|
|
|
{
|
|
|
|
return CCLabelBMFont::create(str, fntFile, width, kCCTextAlignmentLeft, CCPointZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLabelBMFont * CCLabelBMFont::create(const char *str, const char *fntFile)
|
|
|
|
{
|
|
|
|
return CCLabelBMFont::create(str, fntFile, kCCLabelAutomaticWidth, kCCTextAlignmentLeft, CCPointZero);
|
|
|
|
}
|
|
|
|
|
2012-03-22 09:46:07 +08:00
|
|
|
//LabelBMFont - Creation & Init
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelBMFont *CCLabelBMFont::create(const char *str, const char *fntFile, float width/* = kCCLabelAutomaticWidth*/, CCTextAlignment alignment/* = kCCTextAlignmentLeft*/, CCPoint imageOffset/* = CCPointZero*/)
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLabelBMFont *pRet = new CCLabelBMFont();
|
|
|
|
if(pRet && pRet->initWithString(str, fntFile, width, alignment, imageOffset))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
2012-06-12 14:33:53 +08:00
|
|
|
bool CCLabelBMFont::init()
|
|
|
|
{
|
|
|
|
return initWithString(NULL, NULL, kCCLabelAutomaticWidth, kCCTextAlignmentLeft, CCPointZero);
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
bool CCLabelBMFont::initWithString(const char *theString, const char *fntFile, float width/* = kCCLabelAutomaticWidth*/, CCTextAlignment alignment/* = kCCTextAlignmentLeft*/, CCPoint imageOffset/* = CCPointZero*/)
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCAssert(!_configuration, "re-init is no longer supported");
|
2012-06-08 15:29:47 +08:00
|
|
|
CCAssert( (theString && fntFile) || (theString==NULL && fntFile==NULL), "Invalid params for CCLabelBMFont");
|
|
|
|
|
|
|
|
CCTexture2D *texture = NULL;
|
|
|
|
|
|
|
|
if (fntFile)
|
|
|
|
{
|
|
|
|
CCBMFontConfiguration *newConf = FNTConfigLoadFile(fntFile);
|
2012-11-15 17:16:51 +08:00
|
|
|
if (!newConf)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: WARNING. CCLabelBMFont: Impossible to create font. Please check file: '%s'", fntFile);
|
|
|
|
release();
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-08 15:29:47 +08:00
|
|
|
|
|
|
|
newConf->retain();
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_configuration);
|
|
|
|
_configuration = newConf;
|
2012-06-08 15:29:47 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_fntFile = fntFile;
|
2012-06-08 15:29:47 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
texture = CCTextureCache::sharedTextureCache()->addImage(_configuration->getAtlasName());
|
2012-06-08 15:29:47 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texture = new CCTexture2D();
|
|
|
|
texture->autorelease();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-12 14:33:53 +08:00
|
|
|
if (theString == NULL)
|
|
|
|
{
|
|
|
|
theString = "";
|
|
|
|
}
|
|
|
|
|
2012-06-08 15:29:47 +08:00
|
|
|
if (CCSpriteBatchNode::initWithTexture(texture, strlen(theString)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_width = width;
|
|
|
|
_alignment = alignment;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_displayedOpacity = _realOpacity = 255;
|
|
|
|
_displayedColor = _realColor = ccWHITE;
|
|
|
|
_cascadeOpacityEnabled = true;
|
|
|
|
_cascadeColorEnabled = true;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_contentSize = CCSizeZero;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_isOpacityModifyRGB = _textureAtlas->getTexture()->hasPremultipliedAlpha();
|
|
|
|
_anchorPoint = ccp(0.5f, 0.5f);
|
2012-11-15 17:16:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_imageOffset = imageOffset;
|
2013-02-28 10:37:47 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_reusedChar = new CCSprite();
|
|
|
|
_reusedChar->initWithTexture(_textureAtlas->getTexture(), CCRectMake(0, 0, 0, 0), false);
|
|
|
|
_reusedChar->setBatchNode(this);
|
2012-11-15 17:16:51 +08:00
|
|
|
|
2013-02-28 10:37:47 +08:00
|
|
|
this->setString(theString, true);
|
2012-11-15 17:16:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
|
|
|
|
CCLabelBMFont::CCLabelBMFont()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _string(NULL)
|
|
|
|
, _initialString(NULL)
|
|
|
|
, _alignment(kCCTextAlignmentCenter)
|
|
|
|
, _width(-1.0f)
|
|
|
|
, _configuration(NULL)
|
|
|
|
, _lineBreakWithoutSpaces(false)
|
|
|
|
, _imageOffset(CCPointZero)
|
|
|
|
, _reusedChar(NULL)
|
|
|
|
, _displayedOpacity(255)
|
|
|
|
, _realOpacity(255)
|
|
|
|
, _displayedColor(ccWHITE)
|
|
|
|
, _realColor(ccWHITE)
|
|
|
|
, _cascadeColorEnabled(true)
|
|
|
|
, _cascadeOpacityEnabled(true)
|
|
|
|
, _isOpacityModifyRGB(false)
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCLabelBMFont::~CCLabelBMFont()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_reusedChar);
|
|
|
|
CC_SAFE_DELETE_ARRAY(_string);
|
|
|
|
CC_SAFE_DELETE_ARRAY(_initialString);
|
|
|
|
CC_SAFE_RELEASE(_configuration);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
// LabelBMFont - Atlas generation
|
|
|
|
int CCLabelBMFont::kerningAmountForFirst(unsigned short first, unsigned short second)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int ret = 0;
|
|
|
|
unsigned int key = (first<<16) | (second & 0xffff);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _configuration->_kerningDictionary ) {
|
2012-11-15 17:16:51 +08:00
|
|
|
tCCKerningHashElement *element = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_configuration->_kerningDictionary, &key, element);
|
2012-04-19 14:35:52 +08:00
|
|
|
if(element)
|
|
|
|
ret = element->amount;
|
|
|
|
}
|
|
|
|
return ret;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-01-18 18:02:39 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCLabelBMFont::createFontChars()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int nextFontPositionX = 0;
|
2012-03-20 15:04:53 +08:00
|
|
|
int nextFontPositionY = 0;
|
2013-02-27 09:38:30 +08:00
|
|
|
unsigned short prev = -1;
|
2012-04-19 14:35:52 +08:00
|
|
|
int kerningAmount = 0;
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize tmpSize = CCSizeZero;
|
2010-12-24 17:07:31 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
int longestLine = 0;
|
|
|
|
unsigned int totalHeight = 0;
|
2011-11-09 22:11:36 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
unsigned int quantityOfLines = 1;
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int stringLen = _string ? cc_wcslen(_string) : 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (stringLen == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
set<unsigned int> *charSet = _configuration->getCharacterSet();
|
2012-11-21 10:06:03 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
for (unsigned int i = 0; i < stringLen - 1; ++i)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned short c = _string[i];
|
2012-03-20 15:04:53 +08:00
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
quantityOfLines++;
|
2012-01-18 18:02:39 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
totalHeight = _configuration->_commonHeight * quantityOfLines;
|
|
|
|
nextFontPositionY = 0-(_configuration->_commonHeight - _configuration->_commonHeight * quantityOfLines);
|
2012-11-15 17:16:51 +08:00
|
|
|
|
|
|
|
CCRect rect;
|
|
|
|
ccBMFontDef fontDef;
|
2012-01-18 18:02:39 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
for (unsigned int i= 0; i < stringLen; i++)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned short c = _string[i];
|
2011-11-09 22:11:36 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
if (c == '\n')
|
2011-11-09 22:11:36 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
nextFontPositionX = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
nextFontPositionY -= _configuration->_commonHeight;
|
2012-03-20 15:04:53 +08:00
|
|
|
continue;
|
2011-11-09 22:11:36 +08:00
|
|
|
}
|
2012-11-15 17:16:51 +08:00
|
|
|
|
|
|
|
if (charSet->find(c) == charSet->end())
|
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
CCLOGWARN("cocos2d::CCLabelBMFont: Attempted to use character not defined in this bitmap: %d", c);
|
2012-11-15 17:16:51 +08:00
|
|
|
continue;
|
|
|
|
}
|
2011-11-09 22:11:36 +08:00
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
kerningAmount = this->kerningAmountForFirst(prev, c);
|
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
tCCFontDefHashElement *element = NULL;
|
2012-04-06 23:33:42 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// unichar is a short, and an int is needed on HASH_FIND_INT
|
|
|
|
unsigned int key = c;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_configuration->_fontDefDictionary, &key, element);
|
2012-11-15 17:16:51 +08:00
|
|
|
if (! element)
|
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
CCLOGWARN("cocos2d::CCLabelBMFont: characer not found %d", c);
|
2012-11-15 17:16:51 +08:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-06 23:33:42 +08:00
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
fontDef = element->fontDef;
|
2012-04-06 23:33:42 +08:00
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
rect = fontDef.rect;
|
2012-04-19 14:35:52 +08:00
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
2012-04-06 23:33:42 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
rect.origin.x += _imageOffset.x;
|
|
|
|
rect.origin.y += _imageOffset.y;
|
2012-01-18 18:43:41 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSprite *fontChar;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
bool hasSprite = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
fontChar = (CCSprite*)(this->getChildByTag(i));
|
2013-02-27 09:38:30 +08:00
|
|
|
if(fontChar )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
// Reusing previous Sprite
|
|
|
|
fontChar->setVisible(true);
|
|
|
|
}
|
|
|
|
else
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
// New Sprite ? Set correct color, opacity, etc...
|
2012-11-15 17:16:51 +08:00
|
|
|
if( 0 )
|
|
|
|
{
|
|
|
|
/* WIP: Doesn't support many features yet.
|
|
|
|
But this code is super fast. It doesn't create any sprite.
|
|
|
|
Ideal for big labels.
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
fontChar = _reusedChar;
|
2012-11-15 17:16:51 +08:00
|
|
|
fontChar->setBatchNode(NULL);
|
|
|
|
hasSprite = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-19 16:08:18 +08:00
|
|
|
fontChar = new CCSprite();
|
2013-06-15 14:03:30 +08:00
|
|
|
fontChar->initWithTexture(_textureAtlas->getTexture(), rect);
|
2012-11-15 17:16:51 +08:00
|
|
|
addChild(fontChar, i, i);
|
|
|
|
fontChar->release();
|
|
|
|
}
|
2013-02-27 09:38:30 +08:00
|
|
|
|
|
|
|
// Apply label properties
|
2013-06-15 14:03:30 +08:00
|
|
|
fontChar->setOpacityModifyRGB(_isOpacityModifyRGB);
|
2013-02-27 09:38:30 +08:00
|
|
|
|
|
|
|
// Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on
|
2013-06-15 14:03:30 +08:00
|
|
|
fontChar->updateDisplayedColor(_displayedColor);
|
|
|
|
fontChar->updateDisplayedOpacity(_displayedOpacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
// updating previous sprite
|
|
|
|
fontChar->setTextureRect(rect, false, rect.size);
|
2010-12-24 17:07:31 +08:00
|
|
|
|
2012-06-08 15:29:47 +08:00
|
|
|
// See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!)
|
2013-06-15 14:03:30 +08:00
|
|
|
int yOffset = _configuration->_commonHeight - fontDef.yOffset;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint fontPos = ccp( (float)nextFontPositionX + fontDef.xOffset + fontDef.rect.size.width*0.5f + kerningAmount,
|
|
|
|
(float)nextFontPositionY + yOffset - rect.size.height*0.5f * CC_CONTENT_SCALE_FACTOR() );
|
|
|
|
fontChar->setPosition(CC_POINT_PIXELS_TO_POINTS(fontPos));
|
|
|
|
|
|
|
|
// update kerning
|
|
|
|
nextFontPositionX += fontDef.xAdvance + kerningAmount;
|
2013-02-27 09:38:30 +08:00
|
|
|
prev = c;
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
if (longestLine < nextFontPositionX)
|
|
|
|
{
|
|
|
|
longestLine = nextFontPositionX;
|
|
|
|
}
|
2012-11-15 17:16:51 +08:00
|
|
|
|
|
|
|
if (! hasSprite)
|
|
|
|
{
|
|
|
|
updateQuadFromSprite(fontChar, i);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-28 15:05:55 +08:00
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
// If the last character processed has an xAdvance which is less that the width of the characters image, then we need
|
|
|
|
// to adjust the width of the string to take this into account, or the character will overlap the end of the bounding
|
|
|
|
// box
|
|
|
|
if (fontDef.xAdvance < fontDef.rect.size.width)
|
|
|
|
{
|
|
|
|
tmpSize.width = longestLine + fontDef.rect.size.width - fontDef.xAdvance;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmpSize.width = longestLine;
|
|
|
|
}
|
|
|
|
tmpSize.height = totalHeight;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setContentSize(CC_SIZE_PIXELS_TO_POINTS(tmpSize));
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//LabelBMFont - CCLabelProtocol protocol
|
|
|
|
void CCLabelBMFont::setString(const char *newString)
|
2012-03-22 09:46:07 +08:00
|
|
|
{
|
2013-03-20 13:55:43 +08:00
|
|
|
this->setString(newString, true);
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
2013-03-20 13:55:43 +08:00
|
|
|
void CCLabelBMFont::setString(const char *newString, bool needUpdateLabel)
|
|
|
|
{
|
|
|
|
if (newString == NULL) {
|
|
|
|
newString = "";
|
|
|
|
}
|
|
|
|
if (needUpdateLabel) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_initialStringUTF8 = newString;
|
2013-03-20 13:55:43 +08:00
|
|
|
}
|
|
|
|
unsigned short* utf16String = cc_utf8_to_utf16(newString);
|
|
|
|
setString(utf16String, needUpdateLabel);
|
|
|
|
CC_SAFE_DELETE_ARRAY(utf16String);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelBMFont::setString(unsigned short *newString, bool needUpdateLabel)
|
|
|
|
{
|
|
|
|
if (!needUpdateLabel)
|
2013-03-01 18:15:20 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned short* tmp = _string;
|
|
|
|
_string = copyUTF16StringN(newString);
|
2013-03-20 13:55:43 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(tmp);
|
2013-03-01 18:15:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned short* tmp = _initialString;
|
|
|
|
_initialString = copyUTF16StringN(newString);
|
2013-03-20 13:55:43 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(tmp);
|
2013-03-01 18:15:20 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() != 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCObject* child;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, child)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCNode* pNode = (CCNode*) child;
|
|
|
|
if (pNode)
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
pNode->setVisible(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->createFontChars();
|
2013-03-20 13:55:43 +08:00
|
|
|
|
|
|
|
if (needUpdateLabel) {
|
2012-04-19 14:35:52 +08:00
|
|
|
updateLabel();
|
2013-03-20 13:55:43 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCLabelBMFont::getString(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _initialStringUTF8.c_str();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelBMFont::setCString(const char *label)
|
|
|
|
{
|
|
|
|
setString(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
//LabelBMFont - CCRGBAProtocol protocol
|
2013-02-27 09:38:30 +08:00
|
|
|
const ccColor3B& CCLabelBMFont::getColor()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _realColor;
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ccColor3B& CCLabelBMFont::getDisplayedColor()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _displayedColor;
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelBMFont::setColor(const ccColor3B& color)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_displayedColor = _realColor = color;
|
2013-02-27 09:38:30 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _cascadeColorEnabled ) {
|
2013-02-27 09:38:30 +08:00
|
|
|
ccColor3B parentColor = ccWHITE;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCRGBAProtocol* pParent = dynamic_cast<CCRGBAProtocol*>(_parent);
|
2013-02-27 09:38:30 +08:00
|
|
|
if (pParent && pParent->isCascadeColorEnabled())
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
parentColor = pParent->getDisplayedColor();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-02-27 09:38:30 +08:00
|
|
|
this->updateDisplayedColor(parentColor);
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-11-20 16:34:55 +08:00
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
GLubyte CCLabelBMFont::getOpacity(void)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _realOpacity;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-11-20 16:34:55 +08:00
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
GLubyte CCLabelBMFont::getDisplayedOpacity(void)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _displayedOpacity;
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
/** Override synthesized setOpacity to recurse items */
|
2013-02-28 10:37:47 +08:00
|
|
|
void CCLabelBMFont::setOpacity(GLubyte opacity)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_displayedOpacity = _realOpacity = opacity;
|
2013-02-27 09:38:30 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _cascadeOpacityEnabled ) {
|
2013-02-27 09:38:30 +08:00
|
|
|
GLubyte parentOpacity = 255;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCRGBAProtocol* pParent = dynamic_cast<CCRGBAProtocol*>(_parent);
|
2013-02-27 09:38:30 +08:00
|
|
|
if (pParent && pParent->isCascadeOpacityEnabled())
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
parentOpacity = pParent->getDisplayedOpacity();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-02-27 09:38:30 +08:00
|
|
|
this->updateDisplayedOpacity(parentOpacity);
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-02-27 09:38:30 +08:00
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
void CCLabelBMFont::setOpacityModifyRGB(bool var)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isOpacityModifyRGB = var;
|
|
|
|
if (_children && _children->count() != 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCObject* child;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, child)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCNode* pNode = (CCNode*) child;
|
|
|
|
if (pNode)
|
2011-04-21 14:46:15 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(pNode);
|
|
|
|
if (pRGBAProtocol)
|
2011-11-09 22:11:36 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
pRGBAProtocol->setOpacityModifyRGB(_isOpacityModifyRGB);
|
2011-11-09 22:11:36 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCLabelBMFont::isOpacityModifyRGB()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _isOpacityModifyRGB;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
void CCLabelBMFont::updateDisplayedOpacity(GLubyte parentOpacity)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_displayedOpacity = _realOpacity * parentOpacity/255.0;
|
2013-02-27 09:38:30 +08:00
|
|
|
|
|
|
|
CCObject* pObj;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObj)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
CCSprite *item = (CCSprite*)pObj;
|
2013-06-15 14:03:30 +08:00
|
|
|
item->updateDisplayedOpacity(_displayedOpacity);
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 10:37:47 +08:00
|
|
|
void CCLabelBMFont::updateDisplayedColor(const ccColor3B& parentColor)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +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;
|
2013-02-27 09:38:30 +08:00
|
|
|
|
|
|
|
CCObject* pObj;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObj)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
CCSprite *item = (CCSprite*)pObj;
|
2013-06-15 14:03:30 +08:00
|
|
|
item->updateDisplayedColor(_displayedColor);
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 10:37:47 +08:00
|
|
|
bool CCLabelBMFont::isCascadeColorEnabled()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelBMFont::setCascadeColorEnabled(bool cascadeColorEnabled)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_cascadeColorEnabled = cascadeColorEnabled;
|
2013-02-28 10:37:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCLabelBMFont::isCascadeOpacityEnabled()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelBMFont::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_cascadeOpacityEnabled = cascadeOpacityEnabled;
|
2013-02-28 10:37:47 +08:00
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
// LabelBMFont - AnchorPoint
|
|
|
|
void CCLabelBMFont::setAnchorPoint(const CCPoint& point)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! point.equals(_anchorPoint))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCSpriteBatchNode::setAnchorPoint(point);
|
|
|
|
updateLabel();
|
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// LabelBMFont - Alignment
|
|
|
|
void CCLabelBMFont::updateLabel()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setString(_initialString, false);
|
2012-05-31 15:32:18 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_width > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Step 1: Make multiline
|
2013-06-15 14:03:30 +08:00
|
|
|
vector<unsigned short> str_whole = cc_utf16_vec_from_utf16_str(_string);
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int stringLength = str_whole.size();
|
|
|
|
vector<unsigned short> multiline_string;
|
2012-04-25 18:17:04 +08:00
|
|
|
multiline_string.reserve( stringLength );
|
2012-04-19 14:35:52 +08:00
|
|
|
vector<unsigned short> last_word;
|
2012-04-25 18:17:04 +08:00
|
|
|
last_word.reserve( stringLength );
|
2012-04-25 18:08:23 +08:00
|
|
|
|
2012-05-29 17:11:33 +08:00
|
|
|
unsigned int line = 1, i = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
bool start_line = false, start_word = false;
|
|
|
|
float startOfLine = -1, startOfWord = -1;
|
|
|
|
int skip = 0;
|
|
|
|
|
|
|
|
CCArray* children = getChildren();
|
|
|
|
for (unsigned int j = 0; j < children->count(); j++)
|
|
|
|
{
|
|
|
|
CCSprite* characterSprite;
|
2013-02-27 09:38:30 +08:00
|
|
|
unsigned int justSkipped = 0;
|
|
|
|
|
|
|
|
while (!(characterSprite = (CCSprite*)this->getChildByTag(j + skip + justSkipped)))
|
|
|
|
{
|
|
|
|
justSkipped++;
|
|
|
|
}
|
|
|
|
|
|
|
|
skip += justSkipped;
|
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
if (!characterSprite->isVisible())
|
|
|
|
continue;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-05-29 17:11:33 +08:00
|
|
|
if (i >= stringLength)
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
unsigned short character = str_whole[i];
|
|
|
|
|
|
|
|
if (!start_word)
|
|
|
|
{
|
2012-04-25 18:17:04 +08:00
|
|
|
startOfWord = getLetterPosXLeft( characterSprite );
|
2012-04-19 14:35:52 +08:00
|
|
|
start_word = true;
|
|
|
|
}
|
|
|
|
if (!start_line)
|
|
|
|
{
|
|
|
|
startOfLine = startOfWord;
|
|
|
|
start_line = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Newline.
|
|
|
|
if (character == '\n')
|
|
|
|
{
|
|
|
|
cc_utf8_trim_ws(&last_word);
|
|
|
|
|
2012-04-25 18:17:04 +08:00
|
|
|
last_word.push_back('\n');
|
|
|
|
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
|
|
|
|
last_word.clear();
|
|
|
|
start_word = false;
|
|
|
|
start_line = false;
|
|
|
|
startOfWord = -1;
|
|
|
|
startOfLine = -1;
|
2013-02-27 09:38:30 +08:00
|
|
|
i+=justSkipped;
|
2012-04-25 18:17:04 +08:00
|
|
|
line++;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-05-29 17:11:33 +08:00
|
|
|
if (i >= stringLength)
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
character = str_whole[i];
|
|
|
|
|
|
|
|
if (!startOfWord)
|
|
|
|
{
|
2012-04-25 18:17:04 +08:00
|
|
|
startOfWord = getLetterPosXLeft( characterSprite );
|
2012-04-19 14:35:52 +08:00
|
|
|
start_word = true;
|
|
|
|
}
|
|
|
|
if (!startOfLine)
|
|
|
|
{
|
|
|
|
startOfLine = startOfWord;
|
|
|
|
start_line = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whitespace.
|
|
|
|
if (isspace_unicode(character))
|
|
|
|
{
|
|
|
|
last_word.push_back(character);
|
|
|
|
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
|
|
|
|
last_word.clear();
|
|
|
|
start_word = false;
|
|
|
|
startOfWord = -1;
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out of bounds.
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( getLetterPosXRight( characterSprite ) - startOfLine > _width )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_lineBreakWithoutSpaces)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
last_word.push_back(character);
|
|
|
|
|
|
|
|
int found = cc_utf8_find_last_not_char(multiline_string, ' ');
|
|
|
|
if (found != -1)
|
|
|
|
cc_utf8_trim_ws(&multiline_string);
|
|
|
|
else
|
|
|
|
multiline_string.clear();
|
|
|
|
|
|
|
|
if (multiline_string.size() > 0)
|
|
|
|
multiline_string.push_back('\n');
|
|
|
|
|
|
|
|
line++;
|
|
|
|
start_line = false;
|
|
|
|
startOfLine = -1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cc_utf8_trim_ws(&last_word);
|
|
|
|
|
|
|
|
last_word.push_back('\n');
|
|
|
|
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
|
|
|
|
last_word.clear();
|
|
|
|
start_word = false;
|
|
|
|
start_line = false;
|
|
|
|
startOfWord = -1;
|
|
|
|
startOfLine = -1;
|
|
|
|
line++;
|
|
|
|
|
2012-05-29 17:11:33 +08:00
|
|
|
if (i >= stringLength)
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (!startOfWord)
|
|
|
|
{
|
2012-04-25 18:17:04 +08:00
|
|
|
startOfWord = getLetterPosXLeft( characterSprite );
|
2012-04-19 14:35:52 +08:00
|
|
|
start_word = true;
|
|
|
|
}
|
|
|
|
if (!startOfLine)
|
|
|
|
{
|
|
|
|
startOfLine = startOfWord;
|
|
|
|
start_line = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Character is normal.
|
|
|
|
last_word.push_back(character);
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end());
|
|
|
|
|
|
|
|
int size = multiline_string.size();
|
|
|
|
unsigned short* str_new = new unsigned short[size + 1];
|
|
|
|
|
|
|
|
for (int i = 0; i < size; ++i)
|
2012-05-29 17:11:33 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
str_new[i] = multiline_string[i];
|
2012-05-29 17:11:33 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-03-20 13:55:43 +08:00
|
|
|
str_new[size] = '\0';
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-03-20 13:55:43 +08:00
|
|
|
this->setString(str_new, false);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(str_new);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Make alignment
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_alignment != kCCTextAlignmentLeft)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
int lineNumber = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
int str_len = cc_wcslen(_string);
|
2012-04-19 14:35:52 +08:00
|
|
|
vector<unsigned short> last_line;
|
|
|
|
for (int ctr = 0; ctr <= str_len; ++ctr)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_string[ctr] == '\n' || _string[ctr] == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
float lineWidth = 0.0f;
|
2012-04-25 18:17:04 +08:00
|
|
|
unsigned int line_length = last_line.size();
|
2012-09-06 14:09:29 +08:00
|
|
|
// if last line is empty we must just increase lineNumber and work with next line
|
2012-09-05 15:36:34 +08:00
|
|
|
if (line_length == 0)
|
|
|
|
{
|
|
|
|
lineNumber++;
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
int index = i + line_length - 1 + lineNumber;
|
|
|
|
if (index < 0) continue;
|
|
|
|
|
|
|
|
CCSprite* lastChar = (CCSprite*)getChildByTag(index);
|
2012-04-25 18:17:04 +08:00
|
|
|
if ( lastChar == NULL )
|
|
|
|
continue;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
lineWidth = lastChar->getPosition().x + lastChar->getContentSize().width/2.0f;
|
|
|
|
|
|
|
|
float shift = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (_alignment)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-08 16:22:57 +08:00
|
|
|
case kCCTextAlignmentCenter:
|
2012-04-19 14:35:52 +08:00
|
|
|
shift = getContentSize().width/2.0f - lineWidth/2.0f;
|
|
|
|
break;
|
2012-06-08 16:22:57 +08:00
|
|
|
case kCCTextAlignmentRight:
|
2012-04-19 14:35:52 +08:00
|
|
|
shift = getContentSize().width - lineWidth;
|
2012-06-08 16:22:57 +08:00
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shift != 0)
|
|
|
|
{
|
|
|
|
for (unsigned j = 0; j < line_length; j++)
|
|
|
|
{
|
|
|
|
index = i + j + lineNumber;
|
|
|
|
if (index < 0) continue;
|
|
|
|
|
|
|
|
CCSprite* characterSprite = (CCSprite*)getChildByTag(index);
|
|
|
|
characterSprite->setPosition(ccpAdd(characterSprite->getPosition(), ccp(shift, 0.0f)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i += line_length;
|
|
|
|
lineNumber++;
|
|
|
|
|
|
|
|
last_line.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
last_line.push_back(_string[ctr]);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// LabelBMFont - Alignment
|
|
|
|
void CCLabelBMFont::setAlignment(CCTextAlignment alignment)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_alignment = alignment;
|
2012-04-19 14:35:52 +08:00
|
|
|
updateLabel();
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelBMFont::setWidth(float width)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_width = width;
|
2012-04-19 14:35:52 +08:00
|
|
|
updateLabel();
|
2012-03-22 09:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelBMFont::setLineBreakWithoutSpace( bool breakWithoutSpace )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_lineBreakWithoutSpaces = breakWithoutSpace;
|
2012-04-19 14:35:52 +08:00
|
|
|
updateLabel();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2012-04-25 18:08:23 +08:00
|
|
|
void CCLabelBMFont::setScale(float scale)
|
|
|
|
{
|
2012-04-25 18:17:04 +08:00
|
|
|
CCSpriteBatchNode::setScale(scale);
|
|
|
|
updateLabel();
|
2012-04-25 18:08:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelBMFont::setScaleX(float scaleX)
|
|
|
|
{
|
2012-04-25 18:17:04 +08:00
|
|
|
CCSpriteBatchNode::setScaleX(scaleX);
|
|
|
|
updateLabel();
|
2012-04-25 18:08:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLabelBMFont::setScaleY(float scaleY)
|
|
|
|
{
|
2012-04-25 18:17:04 +08:00
|
|
|
CCSpriteBatchNode::setScaleY(scaleY);
|
|
|
|
updateLabel();
|
2012-04-25 18:08:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float CCLabelBMFont::getLetterPosXLeft( CCSprite* sp )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return sp->getPosition().x * _scaleX - (sp->getContentSize().width * _scaleX * sp->getAnchorPoint().x);
|
2012-04-25 18:08:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float CCLabelBMFont::getLetterPosXRight( CCSprite* sp )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return sp->getPosition().x * _scaleX + (sp->getContentSize().width * _scaleX * sp->getAnchorPoint().x);
|
2012-04-25 18:08:23 +08:00
|
|
|
}
|
2012-06-12 14:33:53 +08:00
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
// LabelBMFont - FntFile
|
|
|
|
void CCLabelBMFont::setFntFile(const char* fntFile)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (fntFile != NULL && strcmp(fntFile, _fntFile.c_str()) != 0 )
|
2012-06-15 15:10:40 +08:00
|
|
|
{
|
|
|
|
CCBMFontConfiguration *newConf = FNTConfigLoadFile(fntFile);
|
|
|
|
|
|
|
|
CCAssert( newConf, "CCLabelBMFont: Impossible to create font. Please check file");
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_fntFile = fntFile;
|
2012-06-15 15:10:40 +08:00
|
|
|
|
|
|
|
CC_SAFE_RETAIN(newConf);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_configuration);
|
|
|
|
_configuration = newConf;
|
2012-06-15 15:10:40 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setTexture(CCTextureCache::sharedTextureCache()->addImage(_configuration->getAtlasName()));
|
2012-06-15 15:10:40 +08:00
|
|
|
this->createFontChars();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCLabelBMFont::getFntFile()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _fntFile.c_str();
|
2012-06-15 15:10:40 +08:00
|
|
|
}
|
2012-06-12 14:33:53 +08:00
|
|
|
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
//LabelBMFont - Debug draw
|
2011-07-04 10:59:35 +08:00
|
|
|
#if CC_LABELBMFONT_DEBUG_DRAW
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCLabelBMFont::draw()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSpriteBatchNode::draw();
|
2013-01-14 16:06:18 +08:00
|
|
|
const CCSize& s = this->getContentSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint vertices[4]={
|
|
|
|
ccp(0,0),ccp(s.width,0),
|
|
|
|
ccp(s.width,s.height),ccp(0,s.height),
|
|
|
|
};
|
|
|
|
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
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_END
|