2013-07-19 04:13:41 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-07-12 05:41:03 +08:00
|
|
|
#include "cocos2d.h"
|
2013-07-11 02:59:05 +08:00
|
|
|
#include "CCStringTTF.h"
|
|
|
|
#include "CCFont.h"
|
|
|
|
#include "CCLabelTextFormatter.h"
|
2013-07-25 01:22:46 +08:00
|
|
|
#include "CCFontAtlasCache.h"
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-07-27 07:30:36 +08:00
|
|
|
StringTTF::StringTTF(FontAtlas *pAtlas, TextHAlignment alignment): _currentUTF8String(0),
|
2013-07-31 01:27:22 +08:00
|
|
|
_originalUTF8String(0),
|
2013-07-27 07:04:21 +08:00
|
|
|
_fontAtlas(pAtlas),
|
|
|
|
_alignment(alignment),
|
|
|
|
_lineBreakWithoutSpaces(false),
|
2013-07-27 07:30:36 +08:00
|
|
|
_advances(0),
|
|
|
|
_displayedColor(Color3B::WHITE),
|
|
|
|
_realColor(Color3B::WHITE),
|
|
|
|
_cascadeColorEnabled(true)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2013-07-16 05:35:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
StringTTF* StringTTF::create(FontAtlas *pAtlas, TextHAlignment alignment, int lineSize)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2013-07-25 01:22:46 +08:00
|
|
|
StringTTF *ret = new StringTTF(pAtlas, alignment);
|
2013-07-20 03:24:25 +08:00
|
|
|
|
|
|
|
if (!ret)
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
2013-07-25 01:22:46 +08:00
|
|
|
|
2013-07-20 03:24:25 +08:00
|
|
|
if( ret->init() )
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete ret;
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
2013-07-20 03:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StringTTF::~StringTTF()
|
|
|
|
{
|
|
|
|
if (_currentUTF8String)
|
|
|
|
{
|
|
|
|
delete [] _currentUTF8String;
|
|
|
|
_currentUTF8String = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_advances)
|
|
|
|
{
|
|
|
|
delete [] _advances;
|
|
|
|
_advances = 0;
|
|
|
|
}
|
2013-07-20 03:24:25 +08:00
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
if (_fontAtlas)
|
|
|
|
{
|
|
|
|
FontAtlasCache::releaseFontAtlas(_fontAtlas);
|
|
|
|
}
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
2013-07-16 05:35:25 +08:00
|
|
|
bool StringTTF::init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-20 07:49:51 +08:00
|
|
|
void StringTTF::setString(const char *stringToRender)
|
|
|
|
{
|
2013-07-27 07:04:21 +08:00
|
|
|
setText(stringToRender, 0, TextHAlignment::CENTER, false);
|
2013-07-20 07:49:51 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
bool StringTTF::setText(const char *stringToRender, float lineWidth, TextHAlignment alignment, bool lineBreakWithoutSpaces)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2013-07-25 01:22:46 +08:00
|
|
|
if (!_fontAtlas)
|
2013-07-11 02:59:05 +08:00
|
|
|
return false;
|
|
|
|
|
2013-07-19 04:13:41 +08:00
|
|
|
_width = lineWidth;
|
2013-07-11 02:59:05 +08:00
|
|
|
_alignment = alignment;
|
|
|
|
_lineBreakWithoutSpaces = lineBreakWithoutSpaces;
|
|
|
|
|
|
|
|
// release all the sprites
|
|
|
|
moveAllSpritesToCache();
|
|
|
|
|
|
|
|
// store locally common line height
|
2013-07-25 01:22:46 +08:00
|
|
|
_commonLineHeight = _fontAtlas->getCommonLineHeight();
|
2013-07-11 02:59:05 +08:00
|
|
|
if (_commonLineHeight <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int numLetter = 0;
|
2013-07-25 01:22:46 +08:00
|
|
|
unsigned short* utf16String = cc_utf8_to_utf16(stringToRender);
|
|
|
|
if(!utf16String)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
numLetter = cc_wcslen(utf16String);
|
2013-08-02 08:49:51 +08:00
|
|
|
SpriteBatchNode::initWithTexture(&_fontAtlas->getTexture(0), numLetter);
|
2013-07-27 07:30:36 +08:00
|
|
|
_cascadeColorEnabled = true;
|
2013-07-20 03:24:25 +08:00
|
|
|
|
2013-07-23 02:40:39 +08:00
|
|
|
//
|
2013-07-25 01:22:46 +08:00
|
|
|
setCurrentString(utf16String);
|
2013-07-31 01:27:22 +08:00
|
|
|
setOriginalString(utf16String);
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
// align text
|
|
|
|
alignText();
|
|
|
|
|
|
|
|
// done here
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
void StringTTF::setAlignment(TextHAlignment alignment)
|
2013-07-12 08:17:29 +08:00
|
|
|
{
|
|
|
|
// store the new alignment
|
|
|
|
if (alignment != _alignment)
|
|
|
|
{
|
|
|
|
// store
|
|
|
|
_alignment = alignment;
|
2013-07-31 01:27:22 +08:00
|
|
|
|
|
|
|
// reset the string
|
|
|
|
resetCurrentString();
|
2013-07-12 08:17:29 +08:00
|
|
|
|
|
|
|
// need to align text again
|
|
|
|
alignText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setWidth(float width)
|
|
|
|
{
|
|
|
|
if (width != _width)
|
|
|
|
{
|
|
|
|
// store
|
|
|
|
_width = width;
|
|
|
|
|
|
|
|
// need to align text again
|
|
|
|
alignText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setLineBreakWithoutSpace(bool breakWithoutSpace)
|
|
|
|
{
|
|
|
|
if (breakWithoutSpace != _lineBreakWithoutSpaces)
|
|
|
|
{
|
|
|
|
// store
|
|
|
|
_lineBreakWithoutSpaces = breakWithoutSpace;
|
|
|
|
|
|
|
|
// need to align text again
|
|
|
|
alignText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setScale(float scale)
|
|
|
|
{
|
|
|
|
Node::setScale(scale);
|
2013-07-13 03:39:47 +08:00
|
|
|
alignText();
|
2013-07-12 08:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setScaleX(float scaleX)
|
|
|
|
{
|
|
|
|
Node::setScaleX(scaleX);
|
2013-07-13 03:39:47 +08:00
|
|
|
alignText();
|
2013-07-12 08:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setScaleY(float scaleY)
|
|
|
|
{
|
|
|
|
Node::setScaleY(scaleY);
|
2013-07-13 03:39:47 +08:00
|
|
|
alignText();
|
2013-07-12 08:17:29 +08:00
|
|
|
}
|
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
void StringTTF::alignText()
|
|
|
|
{
|
|
|
|
hideAllLetters();
|
|
|
|
LabelTextFormatter::createStringSprites(this);
|
2013-07-23 04:08:37 +08:00
|
|
|
|
2013-07-23 02:40:39 +08:00
|
|
|
if( LabelTextFormatter::multilineText(this) )
|
|
|
|
{
|
|
|
|
hideAllLetters();
|
|
|
|
LabelTextFormatter::createStringSprites(this);
|
|
|
|
}
|
2013-07-23 04:08:37 +08:00
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
LabelTextFormatter::alignText(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::hideAllLetters()
|
|
|
|
{
|
2013-07-23 02:40:39 +08:00
|
|
|
Object* Obj = NULL;
|
|
|
|
CCARRAY_FOREACH(&_spriteArray, Obj)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2013-07-23 02:40:39 +08:00
|
|
|
((Sprite *)Obj)->setVisible(false);
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 02:40:39 +08:00
|
|
|
CCARRAY_FOREACH(&_spriteArrayCache, Obj)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2013-07-23 02:40:39 +08:00
|
|
|
((Sprite *)Obj)->setVisible(false);
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StringTTF::computeAdvancesForString(unsigned short int *stringToRender)
|
|
|
|
{
|
|
|
|
if (_advances)
|
|
|
|
{
|
|
|
|
delete [] _advances;
|
|
|
|
_advances = 0;
|
|
|
|
}
|
|
|
|
|
2013-08-02 08:08:57 +08:00
|
|
|
Font &theFont = _fontAtlas->getFont();
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
int letterCount = 0;
|
2013-08-02 08:08:57 +08:00
|
|
|
_advances = theFont.getAdvancesForTextUTF16(stringToRender, letterCount);
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
if(!_advances)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-31 01:27:22 +08:00
|
|
|
bool StringTTF::setOriginalString(unsigned short *stringToSet)
|
|
|
|
{
|
|
|
|
if (_originalUTF8String)
|
|
|
|
{
|
|
|
|
delete [] _originalUTF8String;
|
|
|
|
_originalUTF8String = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int newStringLenght = cc_wcslen(stringToSet);
|
|
|
|
_originalUTF8String = new unsigned short int [newStringLenght + 1];
|
|
|
|
memset(_originalUTF8String, 0, (newStringLenght + 1) * 2);
|
|
|
|
memcpy(_originalUTF8String, stringToSet, (newStringLenght * 2));
|
|
|
|
_originalUTF8String[newStringLenght] = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
bool StringTTF::setCurrentString(unsigned short *stringToSet)
|
|
|
|
{
|
|
|
|
// set the new string
|
|
|
|
if (_currentUTF8String)
|
|
|
|
{
|
|
|
|
delete [] _currentUTF8String;
|
|
|
|
_currentUTF8String = 0;
|
|
|
|
}
|
|
|
|
//
|
2013-07-31 01:27:22 +08:00
|
|
|
_currentUTF8String = stringToSet;
|
2013-07-11 02:59:05 +08:00
|
|
|
// compute the advances
|
|
|
|
return computeAdvancesForString(stringToSet);
|
|
|
|
}
|
|
|
|
|
2013-07-31 01:27:22 +08:00
|
|
|
void StringTTF::resetCurrentString()
|
|
|
|
{
|
|
|
|
// set the new string
|
|
|
|
if (_currentUTF8String)
|
|
|
|
{
|
|
|
|
delete [] _currentUTF8String;
|
|
|
|
_currentUTF8String = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stringLenght = cc_wcslen(_originalUTF8String);
|
|
|
|
_currentUTF8String = new unsigned short int [stringLenght + 1];
|
|
|
|
memcpy(_currentUTF8String, _originalUTF8String, stringLenght * 2);
|
|
|
|
_currentUTF8String[stringLenght] = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
Sprite * StringTTF::createNewSpriteFromLetterDefinition(FontLetterDefinition &theDefinition, Texture2D *theTexture)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
Rect uvRect;
|
|
|
|
uvRect.size.height = theDefinition.height;
|
|
|
|
uvRect.size.width = theDefinition.width;
|
|
|
|
uvRect.origin.x = theDefinition.U;
|
|
|
|
uvRect.origin.y = theDefinition.V;
|
|
|
|
|
|
|
|
SpriteFrame *pFrame = SpriteFrame::createWithTexture(theTexture, uvRect);
|
2013-07-25 01:22:46 +08:00
|
|
|
Sprite *tempSprite = getSprite();
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
if (!tempSprite)
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
tempSprite->initWithSpriteFrame(pFrame);
|
2013-07-26 08:58:13 +08:00
|
|
|
tempSprite->setAnchorPoint(Point(theDefinition.anchorX, theDefinition.anchorY));
|
2013-07-23 02:40:39 +08:00
|
|
|
tempSprite->setBatchNode(this);
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
return tempSprite;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
Sprite * StringTTF::updateSpriteWithLetterDefinition(Sprite *spriteToUpdate, FontLetterDefinition &theDefinition, Texture2D *theTexture)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
if (!spriteToUpdate)
|
|
|
|
{
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Rect uvRect;
|
|
|
|
uvRect.size.height = theDefinition.height;
|
|
|
|
uvRect.size.width = theDefinition.width;
|
|
|
|
uvRect.origin.x = theDefinition.U;
|
|
|
|
uvRect.origin.y = theDefinition.V;
|
|
|
|
|
2013-07-23 02:40:39 +08:00
|
|
|
SpriteFrame *frame = SpriteFrame::createWithTexture(theTexture, uvRect);
|
|
|
|
if (frame)
|
|
|
|
{
|
|
|
|
spriteToUpdate->setTexture(theTexture);
|
|
|
|
spriteToUpdate->setDisplayFrame(frame);
|
2013-07-26 08:58:13 +08:00
|
|
|
spriteToUpdate->setAnchorPoint(Point(theDefinition.anchorX, theDefinition.anchorY));
|
2013-07-23 02:40:39 +08:00
|
|
|
spriteToUpdate->setBatchNode(this);
|
|
|
|
}
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
return spriteToUpdate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite * StringTTF::getSpriteForLetter(unsigned short int newLetter)
|
|
|
|
{
|
2013-07-25 01:22:46 +08:00
|
|
|
if (!_fontAtlas)
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
2013-07-25 01:22:46 +08:00
|
|
|
|
2013-08-02 07:29:39 +08:00
|
|
|
FontLetterDefinition tempDefinition;
|
|
|
|
bool validDefinition = _fontAtlas->getLetterDefinitionForChar(newLetter, tempDefinition);
|
|
|
|
if (validDefinition)
|
2013-07-30 04:43:23 +08:00
|
|
|
{
|
2013-08-02 08:49:51 +08:00
|
|
|
Sprite *newSprite = createNewSpriteFromLetterDefinition(tempDefinition, &_fontAtlas->getTexture(tempDefinition.textureID) );
|
2013-07-30 04:43:23 +08:00
|
|
|
this->addChild(newSprite);
|
|
|
|
return newSprite;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
2013-07-30 04:43:23 +08:00
|
|
|
}
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Sprite * StringTTF::updateSpriteForLetter(Sprite *spriteToUpdate, unsigned short int newLetter)
|
|
|
|
{
|
2013-07-25 01:22:46 +08:00
|
|
|
if (!spriteToUpdate || !_fontAtlas)
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
FontLetterDefinition tempDefinition;
|
|
|
|
bool validDefinition = _fontAtlas->getLetterDefinitionForChar(newLetter, tempDefinition);
|
|
|
|
if (validDefinition)
|
|
|
|
{
|
2013-08-02 08:49:51 +08:00
|
|
|
Sprite *pNewSprite = updateSpriteWithLetterDefinition(spriteToUpdate, tempDefinition, &_fontAtlas->getTexture(tempDefinition.textureID) );
|
2013-08-02 07:29:39 +08:00
|
|
|
return pNewSprite;
|
|
|
|
}
|
2013-07-11 02:59:05 +08:00
|
|
|
else
|
|
|
|
{
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
2013-08-02 07:29:39 +08:00
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::moveAllSpritesToCache()
|
|
|
|
{
|
|
|
|
Object* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(&_spriteArray, pObj)
|
|
|
|
{
|
|
|
|
((Sprite *)pObj)->removeFromParent();
|
|
|
|
_spriteArrayCache.addObject(pObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
_spriteArray.removeAllObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite * StringTTF::getSprite()
|
|
|
|
{
|
|
|
|
if (_spriteArrayCache.count())
|
|
|
|
{
|
|
|
|
Sprite *retSprite = (Sprite *) _spriteArrayCache.lastObject();
|
|
|
|
_spriteArrayCache.removeLastObject();
|
|
|
|
return retSprite;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Sprite *retSprite = new Sprite;
|
|
|
|
return retSprite;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///// PROTOCOL STUFF
|
|
|
|
|
|
|
|
Sprite * StringTTF::getSpriteChild(int ID)
|
|
|
|
{
|
|
|
|
Object* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(&_spriteArray, pObj)
|
|
|
|
{
|
|
|
|
Sprite *pSprite = (Sprite *)pObj;
|
|
|
|
if ( pSprite->getTag() == ID)
|
|
|
|
{
|
|
|
|
return pSprite;
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Array * StringTTF::getChildrenLetters()
|
|
|
|
{
|
|
|
|
return &_spriteArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite * StringTTF::getSpriteForChar(unsigned short int theChar, int spriteIndexHint)
|
|
|
|
{
|
|
|
|
// ret sprite
|
|
|
|
Sprite *retSprite = 0;
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
// look for already existing sprites
|
2013-07-11 02:59:05 +08:00
|
|
|
retSprite = getSpriteChild(spriteIndexHint);
|
2013-07-25 01:22:46 +08:00
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
if (!retSprite)
|
|
|
|
{
|
|
|
|
retSprite = getSpriteForLetter(theChar);
|
|
|
|
if (!retSprite)
|
2013-08-02 07:29:39 +08:00
|
|
|
return nullptr;
|
2013-07-25 01:22:46 +08:00
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
if (retSprite)
|
|
|
|
retSprite->setTag(spriteIndexHint);
|
|
|
|
|
|
|
|
_spriteArray.addObject(retSprite);
|
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
// the sprite is now visible
|
|
|
|
retSprite->setVisible(true);
|
2013-07-25 01:22:46 +08:00
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
// set the right texture letter to the sprite
|
|
|
|
updateSpriteForLetter(retSprite, theChar);
|
|
|
|
|
|
|
|
// we are done here
|
|
|
|
return retSprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
float StringTTF::getLetterPosXLeft( Sprite* sp )
|
|
|
|
{
|
2013-07-13 03:39:47 +08:00
|
|
|
float scaleX = _scaleX;
|
2013-07-11 02:59:05 +08:00
|
|
|
return sp->getPosition().x * scaleX - (sp->getContentSize().width * scaleX * sp->getAnchorPoint().x);
|
|
|
|
}
|
|
|
|
|
|
|
|
float StringTTF::getLetterPosXRight( Sprite* sp )
|
|
|
|
{
|
2013-07-13 03:39:47 +08:00
|
|
|
float scaleX = _scaleX;
|
|
|
|
return sp->getPosition().x * scaleX + (sp->getContentSize().width * scaleX * sp->getAnchorPoint().x);
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int StringTTF::getCommonLineHeight()
|
|
|
|
{
|
2013-07-26 08:58:13 +08:00
|
|
|
return _commonLineHeight;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int StringTTF::getKerningForCharsPair(unsigned short first, unsigned short second)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StringTTF::getXOffsetForChar(unsigned short c)
|
|
|
|
{
|
2013-08-02 07:29:39 +08:00
|
|
|
FontLetterDefinition tempDefinition;
|
|
|
|
bool validDefinition = _fontAtlas->getLetterDefinitionForChar(c, tempDefinition);
|
|
|
|
if (!validDefinition)
|
2013-07-30 04:43:23 +08:00
|
|
|
return -1;
|
|
|
|
|
2013-08-02 07:29:39 +08:00
|
|
|
return (tempDefinition.offsetX);
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int StringTTF::getYOffsetForChar(unsigned short c)
|
|
|
|
{
|
2013-08-02 07:29:39 +08:00
|
|
|
FontLetterDefinition tempDefinition;
|
|
|
|
bool validDefinition = _fontAtlas->getLetterDefinitionForChar(c, tempDefinition);
|
|
|
|
if (!validDefinition)
|
2013-07-30 04:43:23 +08:00
|
|
|
return -1;
|
|
|
|
|
2013-08-02 07:29:39 +08:00
|
|
|
return (tempDefinition.offsetY);
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int StringTTF::getAdvanceForChar(unsigned short c, int hintPositionInString)
|
|
|
|
{
|
|
|
|
if (_advances)
|
|
|
|
{
|
|
|
|
// not that advance contains the X offset already
|
2013-08-02 07:29:39 +08:00
|
|
|
FontLetterDefinition tempDefinition;
|
|
|
|
bool validDefinition = _fontAtlas->getLetterDefinitionForChar(c, tempDefinition);
|
|
|
|
if (!validDefinition)
|
2013-07-30 04:43:23 +08:00
|
|
|
return -1;
|
|
|
|
|
2013-08-02 07:29:39 +08:00
|
|
|
return (_advances[hintPositionInString].width - tempDefinition.offsetX);
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect StringTTF::getRectForChar(unsigned short c)
|
|
|
|
{
|
2013-08-02 08:08:57 +08:00
|
|
|
return _fontAtlas->getFont().getRectForChar(c);
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// string related stuff
|
|
|
|
int StringTTF::getStringNumLines()
|
|
|
|
{
|
|
|
|
int quantityOfLines = 1;
|
|
|
|
|
|
|
|
unsigned int stringLen = _currentUTF8String ? cc_wcslen(_currentUTF8String) : 0;
|
|
|
|
if (stringLen == 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
// count number of lines
|
|
|
|
for (unsigned int i = 0; i < stringLen - 1; ++i)
|
|
|
|
{
|
|
|
|
unsigned short c = _currentUTF8String[i];
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
quantityOfLines++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return quantityOfLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StringTTF::getStringLenght()
|
|
|
|
{
|
|
|
|
return _currentUTF8String ? cc_wcslen(_currentUTF8String) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short StringTTF::getCharAtStringPosition(int position)
|
|
|
|
{
|
|
|
|
return _currentUTF8String[position];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short * StringTTF::getUTF8String()
|
|
|
|
{
|
|
|
|
return _currentUTF8String;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::assignNewUTF8String(unsigned short *newString)
|
|
|
|
{
|
|
|
|
setCurrentString(newString);
|
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
TextHAlignment StringTTF::getTextAlignment()
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
return _alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
// label related stuff
|
|
|
|
float StringTTF::getMaxLineWidth()
|
|
|
|
{
|
|
|
|
return _width;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StringTTF::breakLineWithoutSpace()
|
|
|
|
{
|
|
|
|
return _lineBreakWithoutSpaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size StringTTF::getLabelContentSize()
|
|
|
|
{
|
|
|
|
return getContentSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setLabelContentSize(const Size &newSize)
|
|
|
|
{
|
|
|
|
setContentSize(newSize);
|
|
|
|
}
|
|
|
|
|
2013-07-13 03:39:47 +08:00
|
|
|
|
|
|
|
// RGBA protocol
|
|
|
|
|
|
|
|
|
|
|
|
bool StringTTF::isOpacityModifyRGB() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setOpacityModifyRGB(bool isOpacityModifyRGB)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char StringTTF::getOpacity() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char StringTTF::getDisplayedOpacity() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void StringTTF::updateDisplayedOpacity(GLubyte parentOpacity)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StringTTF::isCascadeOpacityEnabled() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const Color3B& StringTTF::getColor(void) const
|
|
|
|
{
|
2013-07-27 07:30:36 +08:00
|
|
|
return _realColor;
|
2013-07-13 03:39:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Color3B& StringTTF::getDisplayedColor() const
|
|
|
|
{
|
2013-07-27 07:30:36 +08:00
|
|
|
return _displayedColor;
|
2013-07-13 03:39:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setColor(const Color3B& color)
|
|
|
|
{
|
2013-07-27 07:30:36 +08:00
|
|
|
_displayedColor = _realColor = color;
|
|
|
|
|
|
|
|
if( _cascadeColorEnabled )
|
|
|
|
{
|
|
|
|
Color3B parentColor = Color3B::WHITE;
|
|
|
|
RGBAProtocol* pParent = dynamic_cast<RGBAProtocol*>(_parent);
|
|
|
|
|
|
|
|
if (pParent && pParent->isCascadeColorEnabled())
|
|
|
|
parentColor = pParent->getDisplayedColor();
|
|
|
|
|
|
|
|
updateDisplayedColor(parentColor);
|
|
|
|
}
|
2013-07-13 03:39:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::updateDisplayedColor(const Color3B& parentColor)
|
|
|
|
{
|
2013-07-27 07:30:36 +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-13 03:39:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StringTTF::isCascadeColorEnabled() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTTF::setCascadeColorEnabled(bool cascadeColorEnabled)
|
|
|
|
{
|
2013-07-27 07:30:36 +08:00
|
|
|
_cascadeColorEnabled = cascadeColorEnabled;
|
2013-07-13 03:39:47 +08:00
|
|
|
}
|
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
NS_CC_END
|