Merge branch 'develop' of https://github.com/cocos2d/cocos2d-x into developEventTrigger

This commit is contained in:
samuele3hu 2014-02-20 15:11:56 +08:00
commit 7b85ffcb82
13 changed files with 301 additions and 192 deletions

View File

@ -1 +1 @@
654ea57201d78ad72b48cd198ab0e9d937435c1b
2b6e0f0359921c6f1328c545b48ff94e6575e332

View File

@ -174,7 +174,7 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
{
_currentPageOrigY += _currentPageLineHeight;
_currentPageOrigX = 0;
if(_currentPageOrigY >= PAGE_HEIGHT)
if(_currentPageOrigY + _currentPageLineHeight >= PAGE_HEIGHT)
{
_atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, PAGE_WIDTH, PAGE_HEIGHT, _pageContentSize );
_currentPageOrigY = 0;

View File

@ -67,6 +67,7 @@ public:
bool prepareLetterDefinitions(unsigned short *utf16String);
inline const std::unordered_map<int, Texture2D*>& getTextures() const{ return _atlasTextures;}
void addTexture(Texture2D &texture, int slot);
float getCommonLineHeight() const;
void setCommonLineHeight(float newHeight);

View File

@ -60,8 +60,8 @@ Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text,
if (ret->setTTFConfig(ttfConfig))
{
if(ttfConfig.distanceFieldEnabled)
ret->setFontSize(ttfConfig.fontSize);
ret->setWidth(lineSize);
ret->setFontScale(1.0f * ttfConfig.fontSize / DISTANCEFIELD_ATLAS_FONTSIZE);
ret->setMaxLineWidth(lineSize);
ret->setString(text);
ret->autorelease();
return ret;
@ -88,7 +88,7 @@ Label* Label::createWithBMFont(const std::string& bmfontFilePath, const std::str
if (ret->setBMFontFilePath(bmfontFilePath))
{
ret->setWidth(lineSize);
ret->setMaxLineWidth(lineSize);
ret->setString(text);
ret->autorelease();
return ret;
@ -191,7 +191,7 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,b
: _reusedLetter(nullptr)
, _commonLineHeight(0.0f)
, _lineBreakWithoutSpaces(false)
, _width(0.0f)
, _maxLineWidth(0.0f)
, _alignment(alignment)
, _currentUTF16String(nullptr)
, _originalUTF16String(nullptr)
@ -200,11 +200,12 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,b
, _isOpacityModifyRGB(false)
, _useDistanceField(useDistanceField)
, _useA8Shader(useA8Shader)
, _fontSize(0)
, _fontScale(1.0f)
, _uniformEffectColor(0)
,_currNumLines(-1)
{
_cascadeColorEnabled = true;
_batchNodes.push_back(this);
}
Label::~Label()
@ -343,12 +344,12 @@ void Label::setAlignment(TextHAlignment alignment)
}
}
void Label::setWidth(float width)
void Label::setMaxLineWidth(float width)
{
if (width != _width)
if (width != _maxLineWidth)
{
// store
_width = width;
_maxLineWidth = width;
if (_currentUTF16String)
{
@ -381,7 +382,7 @@ void Label::setScale(float scale)
{
if (_useDistanceField)
{
scale *= 1.0f * _fontSize / DISTANCEFIELD_ATLAS_FONTSIZE;
scale *= _fontScale;
}
Node::setScale(scale);
}
@ -390,7 +391,7 @@ void Label::setScaleX(float scaleX)
{
if (_useDistanceField)
{
scaleX *= 1.0f * _fontSize / DISTANCEFIELD_ATLAS_FONTSIZE;
scaleX *= _fontScale;
}
Node::setScaleX(scaleX);
}
@ -399,7 +400,7 @@ void Label::setScaleY(float scaleY)
{
if (_useDistanceField)
{
scaleY *= 1.0f * _fontSize / DISTANCEFIELD_ATLAS_FONTSIZE;
scaleY *= _fontScale;
}
Node::setScaleY(scaleY);
}
@ -408,7 +409,7 @@ float Label::getScaleY() const
{
if (_useDistanceField)
{
return _scaleY / (1.0f * _fontSize / DISTANCEFIELD_ATLAS_FONTSIZE);
return _scaleY / _fontScale;
}
else
{
@ -420,7 +421,7 @@ float Label::getScaleX() const
{
if (_useDistanceField)
{
return _scaleX / (1.0f * _fontSize / DISTANCEFIELD_ATLAS_FONTSIZE);
return _scaleX / _fontScale;
}
else
{
@ -430,47 +431,69 @@ float Label::getScaleX() const
void Label::alignText()
{
if(_textureAtlas)
_textureAtlas->removeAllQuads();
if (_reusedLetter == nullptr)
{
return;
}
for (const auto& batchNode:_batchNodes)
{
batchNode->getTextureAtlas()->removeAllQuads();
}
_fontAtlas->prepareLetterDefinitions(_currentUTF16String);
auto textures = _fontAtlas->getTextures();
if (textures.size() > _batchNodes.size())
{
for (auto index = _batchNodes.size(); index < textures.size(); ++index)
{
auto batchNode = SpriteBatchNode::createWithTexture(textures[index]);
batchNode->setAnchorPoint(Point::ANCHOR_TOP_LEFT);
batchNode->setPosition(Point::ZERO);
Node::addChild(batchNode,0,Node::INVALID_TAG);
_batchNodes.push_back(batchNode);
}
}
LabelTextFormatter::createStringSprites(this);
if(_width > 0 && _contentSize.width > _width && LabelTextFormatter::multilineText(this) )
if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) )
LabelTextFormatter::createStringSprites(this);
if(_currNumLines > 0)
if(_currNumLines > 1 && _alignment != TextHAlignment::LEFT)
LabelTextFormatter::alignText(this);
int strLen = cc_wcslen(_currentUTF16String);
Rect uvRect;
Sprite* letterSprite;
for(const auto &child : _children) {
int tag = child->getTag();
if(tag < 0 || tag >= strLen)
if(tag >= strLen)
{
SpriteBatchNode::removeChild(child, true);
}
}
else if(tag >= 0)
{
letterSprite = dynamic_cast<Sprite*>(child);
if (letterSprite)
{
uvRect.size.height = _lettersInfo[tag].def.height;
uvRect.size.width = _lettersInfo[tag].def.width;
uvRect.origin.x = _lettersInfo[tag].def.U;
uvRect.origin.y = _lettersInfo[tag].def.V;
_reusedLetter->setBatchNode(nullptr);
int vaildIndex = 0;
Sprite* child = nullptr;
Rect uvRect;
letterSprite->setTexture(textures[_lettersInfo[tag].def.textureID]);
letterSprite->setTextureRect(uvRect);
}
}
}
int index;
for (int ctr = 0; ctr < strLen; ++ctr)
{
if (_lettersInfo[ctr].def.validDefinition)
{
child = static_cast<Sprite*>( this->getChildByTag(ctr) );
if (child)
{
uvRect.size.height = _lettersInfo[ctr].def.height;
uvRect.size.width = _lettersInfo[ctr].def.width;
uvRect.origin.x = _lettersInfo[ctr].def.U;
uvRect.origin.y = _lettersInfo[ctr].def.V;
child->setTexture(&_fontAtlas->getTexture(_lettersInfo[ctr].def.textureID));
child->setTextureRect(uvRect);
}
updateSpriteWithLetterDefinition(_reusedLetter,_lettersInfo[ctr].def,&_fontAtlas->getTexture(_lettersInfo[ctr].def.textureID));
updateSpriteWithLetterDefinition(_lettersInfo[ctr].def,textures[_lettersInfo[ctr].def.textureID]);
_reusedLetter->setPosition(_lettersInfo[ctr].position);
insertQuadFromSprite(_reusedLetter,vaildIndex++);
index = _batchNodes[_lettersInfo[ctr].def.textureID]->getTextureAtlas()->getTotalQuads();
_batchNodes[_lettersInfo[ctr].def.textureID]->insertQuadFromSprite(_reusedLetter,index);
}
}
@ -499,7 +522,6 @@ bool Label::setOriginalString(unsigned short *stringToSet)
if (_originalUTF16String)
{
delete [] _originalUTF16String;
_originalUTF16String = 0;
}
int newStringLenght = cc_wcslen(stringToSet);
@ -517,9 +539,8 @@ bool Label::setCurrentString(unsigned short *stringToSet)
if (_currentUTF16String)
{
delete [] _currentUTF16String;
_currentUTF16String = 0;
}
//
_currentUTF16String = stringToSet;
computeStringNumLines();
// compute the advances
@ -545,40 +566,27 @@ void Label::resetCurrentString()
}
Sprite * Label::updateSpriteWithLetterDefinition(Sprite *spriteToUpdate, const FontLetterDefinition &theDefinition, Texture2D *theTexture)
void Label::updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture)
{
if (!spriteToUpdate)
{
return 0;
}
else
{
Rect uvRect;
uvRect.size.height = theDefinition.height;
uvRect.size.width = theDefinition.width;
uvRect.origin.x = theDefinition.U;
uvRect.origin.y = theDefinition.V;
SpriteFrame *frame = SpriteFrame::createWithTexture(theTexture, uvRect);
if (frame)
{
spriteToUpdate->setBatchNode(this);
spriteToUpdate->setSpriteFrame(frame);
}
return spriteToUpdate;
}
_reusedRect.size.height = theDefinition.height;
_reusedRect.size.width = theDefinition.width;
_reusedRect.origin.x = theDefinition.U;
_reusedRect.origin.y = theDefinition.V;
if(_reusedLetter->getBatchNode() != _batchNodes[theDefinition.textureID])
_reusedLetter->setBatchNode(_batchNodes[theDefinition.textureID]);
_reusedLetter->setTextureRect(_reusedRect,false,_reusedRect.size);
}
bool Label::recordLetterInfo(const cocos2d::Point& point,unsigned short int theChar, int spriteIndex)
bool Label::recordLetterInfo(const cocos2d::Point& point,const FontLetterDefinition& letterDef, int spriteIndex)
{
if (static_cast<std::size_t>(spriteIndex) >= _lettersInfo.size())
{
LetterInfo tmpInfo;
_lettersInfo.push_back(tmpInfo);
}
_fontAtlas->getLetterDefinitionForChar(theChar, _lettersInfo[spriteIndex].def);
}
_lettersInfo[spriteIndex].def = letterDef;
_lettersInfo[spriteIndex].position = point;
_lettersInfo[spriteIndex].contentSize.width = _lettersInfo[spriteIndex].def.width;
_lettersInfo[spriteIndex].contentSize.height = _lettersInfo[spriteIndex].def.height;
@ -633,18 +641,18 @@ void Label::setLabelEffect(LabelEffect effect,const Color3B& effectColor)
_uniformEffectColor = glGetUniformLocation(_shaderProgram->getProgram(), "v_effectColor");
}
void Label::setFontSize(int fontSize)
void Label::setFontScale(float fontScale)
{
_fontSize = fontSize;
Node::setScale(1.0f*_fontSize/DISTANCEFIELD_ATLAS_FONTSIZE);
_fontScale = fontScale;
Node::setScale(_fontScale);
}
void Label::onDraw()
{
CC_PROFILER_START("CCSpriteBatchNode - draw");
CC_PROFILER_START("Label - draw");
// Optimization: Fast Dispatch
if( _textureAtlas->getTotalQuads() == 0 )
if( _batchNodes.size() == 1 && _textureAtlas->getTotalQuads() == 0 )
{
return;
}
@ -657,13 +665,18 @@ void Label::onDraw()
}
for(const auto &child: _children)
{
child->updateTransform();
}
GL::blendFunc( _blendFunc.src, _blendFunc.dst );
_textureAtlas->drawQuads();
for (const auto& batchNode:_batchNodes)
{
batchNode->getTextureAtlas()->drawQuads();
}
CC_PROFILER_STOP("CCSpriteBatchNode - draw");
CC_PROFILER_STOP("Label - draw");
}
void Label::draw()
@ -673,32 +686,49 @@ void Label::draw()
Director::getInstance()->getRenderer()->addCommand(&_customCommand);
}
void Label::visit()
{
if (! _visible)
{
return;
}
kmGLPushMatrix();
transform();
draw();
kmGLPopMatrix();
setOrderOfArrival(0);
}
///// PROTOCOL STUFF
Sprite * Label::getLetter(int ID)
Sprite * Label::getLetter(int lettetIndex)
{
if (ID < getStringLenght())
if (lettetIndex < getStringLenght())
{
if(_lettersInfo[ID].def.validDefinition == false)
if(_lettersInfo[lettetIndex].def.validDefinition == false)
return nullptr;
Sprite* sp = static_cast<Sprite*>(this->getChildByTag(ID));
Sprite* sp = static_cast<Sprite*>(this->getChildByTag(lettetIndex));
if (!sp)
{
Rect uvRect;
uvRect.size.height = _lettersInfo[ID].def.height;
uvRect.size.width = _lettersInfo[ID].def.width;
uvRect.origin.x = _lettersInfo[ID].def.U;
uvRect.origin.y = _lettersInfo[ID].def.V;
uvRect.size.height = _lettersInfo[lettetIndex].def.height;
uvRect.size.width = _lettersInfo[lettetIndex].def.width;
uvRect.origin.x = _lettersInfo[lettetIndex].def.U;
uvRect.origin.y = _lettersInfo[lettetIndex].def.V;
sp = Sprite::createWithTexture(&_fontAtlas->getTexture(_lettersInfo[ID].def.textureID),uvRect);
sp = Sprite::createWithTexture(&_fontAtlas->getTexture(_lettersInfo[lettetIndex].def.textureID),uvRect);
sp->setBatchNode(this);
sp->setAnchorPoint(Point::ANCHOR_MIDDLE);
sp->setPosition(Point(_lettersInfo[ID].position.x+uvRect.size.width/2,_lettersInfo[ID].position.y-uvRect.size.height/2));
sp->setPosition(Point(_lettersInfo[lettetIndex].position.x+uvRect.size.width/2,_lettersInfo[lettetIndex].position.y-uvRect.size.height/2));
sp->setOpacity(_realOpacity);
this->addSpriteWithoutQuad(sp, ID, ID);
this->addSpriteWithoutQuad(sp, lettetIndex, lettetIndex);
}
return sp;
}
@ -745,16 +775,6 @@ int Label::getStringLenght() const
return _currentUTF16String ? cc_wcslen(_currentUTF16String) : 0;
}
unsigned short * Label::getUTF16String() const
{
return _currentUTF16String;
}
void Label::assignNewUTF16String(unsigned short *newString)
{
setCurrentString(newString);
}
TextHAlignment Label::getTextAlignment() const
{
return _alignment;
@ -763,7 +783,7 @@ TextHAlignment Label::getTextAlignment() const
// label related stuff
float Label::getMaxLineWidth() const
{
return _width;
return _maxLineWidth;
}
bool Label::breakLineWithoutSpace() const

View File

@ -50,14 +50,6 @@ enum class LabelEffect {
GLOW
};
struct LetterInfo
{
FontLetterDefinition def;
Point position;
Size contentSize;
};
typedef struct _ttfConfig
{
std::string fontFilePath;
@ -104,7 +96,8 @@ public:
void setLabelEffect(LabelEffect effect,const Color3B& effectColor);
virtual void setAlignment(TextHAlignment alignment);
virtual void setWidth(float width);
CC_DEPRECATED_ATTRIBUTE void setWidth(float width) { setMaxLineWidth(width);}
virtual void setMaxLineWidth(float width);
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace);
virtual void setScale(float scale) override;
virtual void setScaleX(float scaleX) override;
@ -115,54 +108,58 @@ public:
virtual bool isOpacityModifyRGB() const override;
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
virtual void setColor(const Color3B& color) override;
// CCLabelTextFormat protocol implementation
virtual std::vector<LetterInfo> *getLettersInfo() { return &_lettersInfo; };
virtual bool recordLetterInfo(const cocos2d::Point& point,unsigned short int theChar, int spriteIndex);
virtual bool recordPlaceholderInfo(int spriteIndex);
virtual Sprite * getLetter(int ID);
virtual Sprite * getLetter(int lettetIndex);
// font related stuff
virtual int getCommonLineHeight() const;
virtual int* getKernings() const { return _horizontalKernings;}
virtual int getCommonLineHeight() const;
// string related stuff
virtual int getStringNumLines() const;
virtual int getStringLenght() const;
virtual unsigned short * getUTF16String() const;
virtual void assignNewUTF16String(unsigned short *newString);
virtual TextHAlignment getTextAlignment() const;
// label related stuff
virtual float getMaxLineWidth() const;
virtual bool breakLineWithoutSpace() const;
// carloX
virtual const std::string& getString() const override { return _originalUTF8String; }
void addChild(Node * child, int zOrder=0, int tag=0) override;
virtual std::string getDescription() const override;
virtual void visit() override;
virtual void draw(void) override;
virtual void onDraw();
virtual FontAtlas* getFontAtlas() const {return _fontAtlas;}
private:
struct LetterInfo
{
FontLetterDefinition def;
Point position;
Size contentSize;
};
/**
* @js NA
*/
* @js NA
*/
Label(FontAtlas *atlas = nullptr, TextHAlignment alignment = TextHAlignment::LEFT, bool useDistanceField = false,bool useA8Shader = false);
/**
* @js NA
* @lua NA
*/
~Label();
bool initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled = false, bool useA8Shader = false);
* @js NA
* @lua NA
*/
~Label();
bool initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled = false, bool useA8Shader = false);
// CCLabelTextFormat protocol implementation
virtual bool recordLetterInfo(const cocos2d::Point& point,const FontLetterDefinition& letterDef, int spriteIndex);
virtual bool recordPlaceholderInfo(int spriteIndex);
void setFontScale(float fontScale);
void setFontSize(int fontSize);
bool init();
void alignText();
@ -173,17 +170,18 @@ private:
void resetCurrentString();
void computeStringNumLines();
Sprite * updateSpriteWithLetterDefinition(Sprite *spriteToUpdate, const FontLetterDefinition &theDefinition, Texture2D *theTexture);
void updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture);
virtual void updateColor() override;
//! used for optimization
Sprite *_reusedLetter;
Rect _reusedRect;
std::vector<LetterInfo> _lettersInfo;
float _commonLineHeight;
bool _lineBreakWithoutSpaces;
float _width;
bool _lineBreakWithoutSpaces;
float _maxLineWidth;
TextHAlignment _alignment;
unsigned short int * _currentUTF16String;
unsigned short int * _originalUTF16String;
@ -194,7 +192,7 @@ private:
bool _useDistanceField;
bool _useA8Shader;
int _fontSize;
float _fontScale;
LabelEffect _currLabelEffect;
Color3B _effectColor;
@ -203,6 +201,10 @@ private:
CustomCommand _customCommand;
int _currNumLines;
std::vector<SpriteBatchNode*> _batchNodes;
friend class LabelTextFormatter;
};

View File

@ -37,7 +37,7 @@ NS_CC_BEGIN
bool LabelTextFormatter::multilineText(Label *theLabel)
{
int strLen = theLabel->getStringLenght();
auto strWhole = theLabel->getUTF16String();
auto strWhole = theLabel->_currentUTF16String;
vector<unsigned short> multiline_string;
multiline_string.reserve( strLen );
@ -45,22 +45,20 @@ bool LabelTextFormatter::multilineText(Label *theLabel)
vector<unsigned short> last_word;
last_word.reserve( strLen );
unsigned int line = 1;
bool isStartOfLine = false, isStartOfWord = false;
float startOfLine = -1, startOfWord = -1;
int skip = 0;
std::vector<LetterInfo> *leterInfo = theLabel->getLettersInfo();
int tIndex = 0;
float scalsX = theLabel->getScaleX();
float lineWidth = theLabel->getMaxLineWidth();
bool breakLineWithoutSpace = theLabel->breakLineWithoutSpace();
float lineWidth = theLabel->_maxLineWidth;
bool breakLineWithoutSpace = theLabel->_lineBreakWithoutSpaces;
Label::LetterInfo* info = nullptr;
for (int j = 0; j+skip < strLen; j++)
{
LetterInfo* info = &leterInfo->at(j+skip);
info = & theLabel->_lettersInfo.at(j+skip);
unsigned int justSkipped = 0;
@ -79,11 +77,10 @@ bool LabelTextFormatter::multilineText(Label *theLabel)
isStartOfLine = false;
startOfWord = -1;
startOfLine = -1;
++line;
}
if(tIndex < strLen)
{
info = &leterInfo->at( tIndex );
info = & theLabel->_lettersInfo.at( tIndex );
}
else
break;
@ -118,7 +115,7 @@ bool LabelTextFormatter::multilineText(Label *theLabel)
}
if (isspace ||
!last_word.empty() && iscjk_unicode(last_word.back()) && !isCJK)
(!last_word.empty() && iscjk_unicode(last_word.back()) && !isCJK))
{
// if current character is white space, put it into the current word
if (isspace) last_word.push_back(character);
@ -149,7 +146,6 @@ bool LabelTextFormatter::multilineText(Label *theLabel)
if (multiline_string.size() > 0)
multiline_string.push_back('\n');
++line;
isStartOfLine = false;
startOfLine = -1;
}
@ -165,7 +161,6 @@ bool LabelTextFormatter::multilineText(Label *theLabel)
isStartOfLine = false;
startOfWord = -1;
startOfLine = -1;
++line;
--j;
}
}
@ -187,7 +182,7 @@ bool LabelTextFormatter::multilineText(Label *theLabel)
}
strNew[size] = 0;
theLabel->assignNewUTF16String(strNew);
theLabel->setCurrentString(strNew);
return true;
}
@ -197,10 +192,9 @@ bool LabelTextFormatter::alignText(Label *theLabel)
int i = 0;
int lineNumber = 0;
int strLen = cc_wcslen(theLabel->getUTF16String());
int strLen = cc_wcslen(theLabel->_currentUTF16String);
vector<unsigned short> lastLine;
std::vector<LetterInfo> *leterInfo = theLabel->getLettersInfo();
auto strWhole = theLabel->getUTF16String();
auto strWhole = theLabel->_currentUTF16String;
for (int ctr = 0; ctr <= strLen; ++ctr)
{
@ -208,8 +202,7 @@ bool LabelTextFormatter::alignText(Label *theLabel)
if (currentChar == '\n' || currentChar == 0)
{
float lineWidth = 0.0f;
size_t lineLength = lastLine.size();
auto lineLength = lastLine.size();
// if last line is empty we must just increase lineNumber and work with next line
if (lineLength == 0)
@ -220,20 +213,25 @@ bool LabelTextFormatter::alignText(Label *theLabel)
int index = static_cast<int>(i + lineLength - 1 + lineNumber);
if (index < 0) continue;
LetterInfo* info = &leterInfo->at( index );
auto info = & theLabel->_lettersInfo.at( index );
if(info->def.validDefinition == false)
continue;
lineWidth = info->position.x + info->contentSize.width;
float shift = 0;
switch (theLabel->getTextAlignment())
switch (theLabel->_alignment)
{
case TextHAlignment::CENTER:
shift = theLabel->getContentSize().width/2.0f - lineWidth/2.0f;
break;
{
float lineWidth = info->position.x + info->contentSize.width;
shift = theLabel->getContentSize().width/2.0f - lineWidth/2.0f;
break;
}
case TextHAlignment::RIGHT:
shift = theLabel->getContentSize().width - lineWidth;
break;
{
float lineWidth = info->position.x + info->contentSize.width;
shift = theLabel->getContentSize().width - lineWidth;
break;
}
default:
break;
}
@ -245,7 +243,7 @@ bool LabelTextFormatter::alignText(Label *theLabel)
index = i + j + lineNumber;
if (index < 0) continue;
info = &leterInfo->at( index );
info = & theLabel->_lettersInfo.at( index );
if(info)
{
info->position.x += shift;
@ -275,31 +273,23 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel)
if (stringLen == 0)
return false;
int nextFontPositionX = 0;
int nextFontPositionY = 0;
unsigned short prev = -1;
Size tmpSize = Size::ZERO;
int longestLine = 0;
unsigned int totalHeight = 0;
int quantityOfLines = theLabel->getStringNumLines();
int commonLineHeight = theLabel->getCommonLineHeight();
totalHeight = commonLineHeight * quantityOfLines;
nextFontPositionY = totalHeight;
unsigned int totalHeight = theLabel->_commonLineHeight * theLabel->_currNumLines;
int nextFontPositionX = 0;
int nextFontPositionY = totalHeight;
Rect charRect;
int charXOffset = 0;
int charYOffset = 0;
int charAdvance = 0;
auto strWhole = theLabel->getUTF16String();
FontAtlas* fontAtlas = theLabel->getFontAtlas();
auto strWhole = theLabel->_currentUTF16String;
auto fontAtlas = theLabel->getFontAtlas();
FontLetterDefinition tempDefinition;
auto kernings = theLabel->getKernings();
Point letterPosition;
const auto& kernings = theLabel->_horizontalKernings;
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
for (unsigned int i = 0; i < stringLen; i++)
{
@ -320,24 +310,22 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel)
if (c == '\n')
{
nextFontPositionX = 0;
nextFontPositionY -= commonLineHeight;
nextFontPositionY -= theLabel->_commonLineHeight;
theLabel->recordPlaceholderInfo(i);
continue;
}
Point fontPos = Point((float)nextFontPositionX + charXOffset + kernings[i],
(float)nextFontPositionY - charYOffset);
letterPosition.x = (nextFontPositionX + charXOffset + kernings[i]) / contentScaleFactor;
letterPosition.y = (nextFontPositionY - charYOffset) / contentScaleFactor;
if( theLabel->recordLetterInfo(CC_POINT_PIXELS_TO_POINTS(fontPos),c,i) == false)
if( theLabel->recordLetterInfo(letterPosition,tempDefinition,i) == false)
{
log("WARNING: can't find letter definition in font file for letter: %c", c);
continue;
}
// update kerning
nextFontPositionX += charAdvance + kernings[i];
prev = c;
if (longestLine < nextFontPositionX)
{
@ -345,7 +333,8 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel)
}
}
float lastCharWidth = tempDefinition.width * CC_CONTENT_SCALE_FACTOR();
float lastCharWidth = tempDefinition.width * contentScaleFactor;
Size tmpSize = Size::ZERO;
// 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

View File

@ -167,12 +167,12 @@ public:
virtual void draw(void) override;
virtual std::string getDescription() const override;
protected:
/** Inserts a quad at a certain index into the texture atlas. The Sprite won't be added into the children array.
This method should be called only when you are dealing with very big AtlasSrite and when most of the Sprite won't be updated.
For example: a tile map (TMXMap) or a label with lots of characters (LabelBMFont)
*/
void insertQuadFromSprite(Sprite *sprite, ssize_t index);
protected:
/** Updates a quad at a certain index into the texture atlas. The Sprite won't be added into the children array.
This method should be called only when you are dealing with very big AtlasSrite and when most of the Sprite won't be updated.
For example: a tile map (TMXMap) or a label with lots of characters (LabelBMFont)

View File

@ -585,14 +585,16 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd)
// The window is being hidden or closed, clean it up.
engine_term_display(engine);
break;
case APP_CMD_GAINED_FOCUS:
case APP_CMD_RESUME:
if (cocos2d::Director::getInstance()->getOpenGLView()) {
cocos2d::Application::getInstance()->applicationWillEnterForeground();
engine->animating = 1;
if (engine->display != nullptr)
{
engine->animating = 1;
}
}
break;
case APP_CMD_LOST_FOCUS:
case APP_CMD_PAUSE:
{
cocos2d::Application::getInstance()->applicationDidEnterBackground();
cocos2d::EventCustom backgroundEvent(EVENT_COME_TO_BACKGROUND);

@ -1 +1 @@
Subproject commit 8c8ddeab158e65016e215131032a5e86dc7d93b5
Subproject commit 18b22bbaf388042f5a37fcb3588e8b53e0c45313

View File

@ -60,6 +60,7 @@ static std::function<Layer*()> createFunctions[] =
CL(LabelTTFAlignmentNew),
CL(LabelFNTBounds),
CL(LabelTTFLongLineWrapping),
CL(LabelTTFLargeText),
CL(LabelTTFColor),
CL(LabelTTFFontsTestNew),
CL(LabelTTFDynamicAlignment),
@ -850,7 +851,7 @@ void LabelFNTMultiLineAlignment::onTouchesMoved(const std::vector<Touch*>& touch
float labelWidth = fabs(this->_arrowsShouldRetain->getPosition().x - this->_labelShouldRetain->getPosition().x) * 2;
this->_labelShouldRetain->setWidth(labelWidth);
this->_labelShouldRetain->setMaxLineWidth(labelWidth);
}
void LabelFNTMultiLineAlignment::snapArrowsToEdge()
@ -978,6 +979,29 @@ std::string LabelTTFLongLineWrapping::subtitle() const
return "Uses the new Label with TTF. Testing auto-wrapping";
}
LabelTTFLargeText::LabelTTFLargeText()
{
auto size = Director::getInstance()->getWinSize();
// Long sentence
TTFConfig ttfConfig("fonts/wt021.ttf",36,GlyphCollection::DYNAMIC);
std::string text = FileUtils::getInstance()->getStringFromFile("commonly_used_words.txt");
auto label = Label::createWithTTF(ttfConfig,text, TextHAlignment::CENTER, size.width);
label->setPosition( Point(size.width/2, size.height/2) );
label->setAnchorPoint(Point::ANCHOR_MIDDLE);
addChild(label);
}
std::string LabelTTFLargeText::title() const
{
return "New Label + .TTF";
}
std::string LabelTTFLargeText::subtitle() const
{
return "Uses the new Label with TTF. Testing large text";
}
LabelTTFColor::LabelTTFColor()
{
auto size = Director::getInstance()->getWinSize();

View File

@ -420,6 +420,17 @@ protected:
void onDraw();
};
class LabelTTFLargeText : public AtlasDemoNew
{
public:
CREATE_FUNC(LabelTTFLargeText);
LabelTTFLargeText();
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
// we don't support linebreak mode
#endif

View File

@ -213,10 +213,10 @@ void LabelMainScene::onIncrease(Object* sender)
break;
case kCaseLabelUpdate:
{
TTFConfig ttfConfig("fonts/arial.ttf", 60, GlyphCollection::DYNAMIC, nullptr, true);
TTFConfig ttfConfig("fonts/arial.ttf", 60, GlyphCollection::DYNAMIC);
for( int i=0;i< kNodesIncrease;i++)
{
auto label = Label::createWithTTF(ttfConfig, "Label", TextHAlignment::CENTER, size.width);
auto label = Label::createWithTTF(ttfConfig, "Label", TextHAlignment::LEFT);
label->setPosition(Point((size.width/2 + rand() % 50), ((int)size.height/2 + rand() % 50)));
_labelContainer->addChild(label, 1, _quantityNodes);

View File

@ -0,0 +1,60 @@
三画
三于干亏士工土才寸下大丈与万上小口巾山千乞川亿个勺久凡及夕丸么广
亡门义之尸弓己已子卫也女飞刃习叉马乡
四画
丰王井开夫天无元专云扎艺木五支厅不太犬区历尤友匹车巨牙屯比互切瓦
止少日中冈贝内水见午牛手毛气升长仁什片仆化仇币仍仅斤爪反介父从今
凶分乏公仓月氏勿欠风丹匀乌凤勾文六方火为斗忆订计户认心尺引丑巴孔
队办以允予劝双书幻
五画
玉刊示末未击打巧正扑扒功扔去甘世古节本术可丙左厉右石布龙平灭轧东
卡北占业旧帅归且旦目叶甲申叮电号田由史只央兄叼叫另叨叹四生失禾丘
付仗代仙们仪白仔他斥瓜乎丛令用甩印乐句匆册犯外处冬鸟务包饥主市立
闪兰半汁汇头汉宁穴它讨写让礼训必议讯记永司尼民出辽奶奴加召皮边发
孕圣对台矛纠母幼丝
六画
式刑动扛寺吉扣考托老执巩圾扩扫地扬场耳共芒亚芝朽朴机权过臣再协西
压厌在有百存而页匠夸夺灰达列死成夹轨邪划迈毕至此贞师尘尖劣光当早
吐吓虫曲团同吊吃因吸吗屿帆岁回岂刚则肉网年朱先丢舌竹迁乔伟传乒乓
休伍伏优伐延件任伤价份华仰仿伙伪自血向似后行舟全会杀合兆企众爷伞
创肌朵杂危旬旨负各名多争色壮冲冰庄庆亦刘齐交次衣产决充妄闭问闯羊
并关米灯州汗污江池汤忙兴宇守宅字安讲军许论农讽设访寻那迅尽导异孙
阵阳收阶阴防奸如妇好她妈戏羽观欢买红纤级约纪驰巡
七画
寿弄麦形进戒吞远违运扶抚坛技坏扰拒找批扯址走抄坝贡攻赤折抓扮抢孝
均抛投坟抗坑坊抖护壳志扭块声把报却劫芽花芹芬苍芳严芦劳克苏杆杠杜
材村杏极李杨求更束豆两丽医辰励否还歼来连步坚旱盯呈时吴助县里呆园
旷围呀吨足邮男困吵串员听吩吹呜吧吼别岗帐财针钉告我乱利秃秀私每兵
估体何但伸作伯伶佣低你住位伴身皂佛近彻役返余希坐谷妥含邻岔肝肚肠
龟免狂犹角删条卵岛迎饭饮系言冻状亩况床库疗应冷这序辛弃冶忘闲间闷
判灶灿弟汪沙汽沃泛沟没沈沉怀忧快完宋宏牢究穷灾良证启评补初社识诉
诊词译君灵即层尿尾迟局改张忌际陆阿陈阻附妙妖妨努忍劲鸡驱纯纱纳纲
驳纵纷纸纹纺驴纽
八画
奉玩环武青责现表规抹拢拔拣担坦押抽拐拖拍者顶拆拥抵拘势抱垃拉拦拌
幸招坡披拨择抬其取苦若茂苹苗英范直茄茎茅林枝杯柜析板松枪构杰述枕
丧或画卧事刺枣雨卖矿码厕奔奇奋态欧垄妻轰顷转斩轮软到非叔肯齿些虎
虏肾贤尚旺具果味昆国昌畅明易昂典固忠咐呼鸣咏呢岸岩帖罗帜岭凯败贩
购图钓制知垂牧物乖刮秆和季委佳侍供使例版侄侦侧凭侨佩货依的迫质欣
征往爬彼径所舍金命斧爸采受乳贪念贫肤肺肢肿胀朋股肥服胁周昏鱼兔狐
忽狗备饰饱饲变京享店夜庙府底剂郊废净盲放刻育闸闹郑券卷单炒炊炕炎
炉沫浅法泄河沾泪油泊沿泡注泻泳泥沸波泼泽治怖性怕怜怪学宝宗定宜审
宙官空帘实试郎诗肩房诚衬衫视话诞询该详建肃录隶居届刷屈弦承孟孤陕
降限妹姑姐姓始驾参艰线练组细驶织终驻驼绍经贯
九画
奏春帮珍玻毒型挂封持项垮挎城挠政赴赵挡挺括拴拾挑指垫挣挤拼挖按挥
挪某甚革荐巷带草茧茶荒茫荡荣故胡南药标枯柄栋相查柏柳柱柿栏树要咸
威歪研砖厘厚砌砍面耐耍牵残殃轻鸦皆背战点临览竖省削尝是盼眨哄显哑
冒映星昨畏趴胃贵界虹虾蚁思蚂虽品咽骂哗咱响哈咬咳哪炭峡罚贱贴骨钞
钟钢钥钩卸缸拜看矩怎牲选适秒香种秋科重复竿段便俩贷顺修保促侮俭俗
俘信皇泉鬼侵追俊盾待律很须叙剑逃食盆胆胜胞胖脉勉狭狮独狡狱狠贸怨
急饶蚀饺饼弯将奖哀亭亮度迹庭疮疯疫疤姿亲音帝施闻阀阁差养美姜叛送
类迷前首逆总炼炸炮烂剃洁洪洒浇浊洞测洗活派洽染济洋洲浑浓津恒恢恰
恼恨举觉宣室宫宪突穿窃客冠语扁袄祖神祝误诱说诵垦退既屋昼费陡眉孩
除险院娃姥姨姻娇怒架贺盈勇怠柔垒绑绒结绕骄绘给络骆绝绞统