typo fix and coding style improvements.

This commit is contained in:
Dhilan007 2014-03-11 14:32:07 +08:00
parent 11d3acd478
commit 792e182e12
2 changed files with 25 additions and 46 deletions

View File

@ -50,7 +50,7 @@ Label* Label::create()
return ret; return ret;
} }
Label* Label::createWithFontDefinition(const std::string& text, FontDefinition &textDefinition) Label* Label::createWithFontDefinition(const std::string& text, const FontDefinition &textDefinition)
{ {
auto ret = new Label(); auto ret = new Label();
@ -258,6 +258,7 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,b
, _maxLineWidth(0) , _maxLineWidth(0)
, _labelWidth(0) , _labelWidth(0)
, _labelHeight(0) , _labelHeight(0)
, _labelDimensions(Size::ZERO)
, _hAlignment(alignment) , _hAlignment(alignment)
, _currentUTF16String(nullptr) , _currentUTF16String(nullptr)
, _originalUTF16String(nullptr) , _originalUTF16String(nullptr)
@ -456,7 +457,7 @@ void Label::setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment)
void Label::setMaxLineWidth(unsigned int maxLineWidth) void Label::setMaxLineWidth(unsigned int maxLineWidth)
{ {
if (_maxLineWidth != maxLineWidth) if (_labelWidth == 0 && _maxLineWidth != maxLineWidth)
{ {
_maxLineWidth = maxLineWidth; _maxLineWidth = maxLineWidth;
_contentDirty = true; _contentDirty = true;
@ -470,8 +471,11 @@ void Label::setDimensions(unsigned int width,unsigned int height)
_fontDefinition._dimensions.width = width; _fontDefinition._dimensions.width = width;
_fontDefinition._dimensions.height = height; _fontDefinition._dimensions.height = height;
_labelHeight = height;
_labelWidth = width; _labelWidth = width;
_labelHeight = height;
_labelDimensions.width = width;
_labelDimensions.height = height;
_maxLineWidth = width; _maxLineWidth = width;
_contentDirty = true; _contentDirty = true;
} }
@ -481,15 +485,8 @@ void Label::setLineBreakWithoutSpace(bool breakWithoutSpace)
{ {
if (breakWithoutSpace != _lineBreakWithoutSpaces) if (breakWithoutSpace != _lineBreakWithoutSpaces)
{ {
// store
_lineBreakWithoutSpaces = breakWithoutSpace; _lineBreakWithoutSpaces = breakWithoutSpace;
_contentDirty = true;
// need to align text again
if(_currentUTF16String)
{
resetCurrentString();
alignText();
}
} }
} }
@ -572,7 +569,7 @@ void Label::alignText()
if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) ) if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) )
LabelTextFormatter::createStringSprites(this); LabelTextFormatter::createStringSprites(this);
if(_labelWidth >0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT)) if(_labelWidth > 0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT))
LabelTextFormatter::alignText(this); LabelTextFormatter::alignText(this);
int strLen = cc_wcslen(_currentUTF16String); int strLen = cc_wcslen(_currentUTF16String);
@ -667,25 +664,6 @@ bool Label::setCurrentString(unsigned short *stringToSet)
return true; return true;
} }
void Label::resetCurrentString()
{
if ((!_currentUTF16String) && (!_originalUTF16String))
return;
// set the new string
if (_currentUTF16String)
{
delete [] _currentUTF16String;
_currentUTF16String = 0;
}
int stringLenght = cc_wcslen(_originalUTF16String);
_currentUTF16String = new unsigned short int [stringLenght + 1];
memcpy(_currentUTF16String, _originalUTF16String, stringLenght * 2);
_currentUTF16String[stringLenght] = 0;
}
void Label::updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture) void Label::updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture)
{ {
_reusedRect.size.height = theDefinition.height; _reusedRect.size.height = theDefinition.height;
@ -757,7 +735,7 @@ void Label::setLabelEffect(LabelEffect effect,const Color3B& effectColor)
void Label::enableGlow(const Color3B& glowColor) void Label::enableGlow(const Color3B& glowColor)
{ {
if(_useDistanceField == false) if(! _useDistanceField)
return; return;
_currLabelEffect = LabelEffect::GLOW; _currLabelEffect = LabelEffect::GLOW;
_effectColor = glowColor; _effectColor = glowColor;
@ -943,7 +921,7 @@ void Label::updateContent()
setOriginalString(utf16String); setOriginalString(utf16String);
if (_textSprite) if (_textSprite)
{ {
_textSprite->removeFromParent(); Node::removeChild(_textSprite,true);
_textSprite = nullptr; _textSprite = nullptr;
} }
if (_fontAtlas) if (_fontAtlas)
@ -1018,7 +996,7 @@ void Label::setFontSize(int fontSize)
{ {
if (_currentLabelType == LabelType::TTF) if (_currentLabelType == LabelType::TTF)
{ {
if (_fontConfig.distanceFieldEnabled == true) if (_fontConfig.distanceFieldEnabled)
{ {
_fontConfig.fontSize = fontSize; _fontConfig.fontSize = fontSize;
this->setFontScale(1.0f * fontSize / DefultFontSize); this->setFontScale(1.0f * fontSize / DefultFontSize);
@ -1048,7 +1026,7 @@ Sprite * Label::getLetter(int lettetIndex)
if (! _textSprite && lettetIndex < _limitShowCount) if (! _textSprite && lettetIndex < _limitShowCount)
{ {
if(_lettersInfo[lettetIndex].def.validDefinition == false) if(! _lettersInfo[lettetIndex].def.validDefinition)
return nullptr; return nullptr;
Sprite* sp = static_cast<Sprite*>(this->getChildByTag(lettetIndex)); Sprite* sp = static_cast<Sprite*>(this->getChildByTag(lettetIndex));

View File

@ -110,7 +110,7 @@ public:
* @warning It will generate texture by the platform-dependent code and create Sprite for show text. * @warning It will generate texture by the platform-dependent code and create Sprite for show text.
* To obtain better performance use createWithTTF/createWithBMFont/createWithCharMap * To obtain better performance use createWithTTF/createWithBMFont/createWithCharMap
*/ */
static Label * createWithFontDefinition(const std::string& text, FontDefinition &textDefinition); static Label * createWithFontDefinition(const std::string& text, const FontDefinition &textDefinition);
/** set TTF configuration for Label */ /** set TTF configuration for Label */
virtual bool setTTFConfig(const TTFConfig& ttfConfig); virtual bool setTTFConfig(const TTFConfig& ttfConfig);
@ -130,7 +130,7 @@ public:
const FontDefinition& getFontDefinition() const { return _fontDefinition; } const FontDefinition& getFontDefinition() const { return _fontDefinition; }
/** changes the string to render /** changes the string to render
* @warning It is as expensive as changing the string if you havn¡¯t set up using TTF/BMFont/CharMap for label. * @warning It is as expensive as changing the string if you haven't set up TTF/BMFont/CharMap for the label.
*/ */
virtual void setString(const std::string& text) override; virtual void setString(const std::string& text) override;
@ -158,7 +158,7 @@ public:
void setAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment,_vAlignment);} void setAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment,_vAlignment);}
TextHAlignment getTextAlignment() const { return _hAlignment;} TextHAlignment getTextAlignment() const { return _hAlignment;}
virtual void setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment); void setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment);
void setHorizontalAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment,_vAlignment); } void setHorizontalAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment,_vAlignment); }
TextHAlignment getHorizontalAlignment() const { return _hAlignment; } TextHAlignment getHorizontalAlignment() const { return _hAlignment; }
@ -166,13 +166,13 @@ public:
void setVerticalAlignment(TextVAlignment vAlignment) { setAlignment(_hAlignment,vAlignment); } void setVerticalAlignment(TextVAlignment vAlignment) { setAlignment(_hAlignment,vAlignment); }
TextVAlignment getVerticalAlignment() const { return _vAlignment; } TextVAlignment getVerticalAlignment() const { return _vAlignment; }
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace); void setLineBreakWithoutSpace(bool breakWithoutSpace);
/** Sets the max line width of the label. /** Sets the max line width of the label.
* The label's max line width be used for force line breaks if the set value not equal zero. * The label's max line width be used for force line breaks if the set value not equal zero.
* The label's width and max line width has not always to be equal. * The label's width and max line width has not always to be equal.
*/ */
virtual void setMaxLineWidth(unsigned int maxLineWidth); void setMaxLineWidth(unsigned int maxLineWidth);
unsigned int getMaxLineWidth() { return _maxLineWidth;} unsigned int getMaxLineWidth() { return _maxLineWidth;}
/** Sets the untransformed size of the label. /** Sets the untransformed size of the label.
@ -190,16 +190,17 @@ public:
unsigned int getHeight() const { return _labelHeight;} unsigned int getHeight() const { return _labelHeight;}
/** Sets the untransformed size of the label in a more efficient way. */ /** Sets the untransformed size of the label in a more efficient way. */
virtual void setDimensions(unsigned int width,unsigned int height); void setDimensions(unsigned int width,unsigned int height);
const Size& getDimensions() const{ return _labelDimensions;}
/** update content immediately.*/ /** update content immediately.*/
virtual void updateContent(); virtual void updateContent();
virtual void setFontName(const std::string& fontName); virtual void setFontName(const std::string& fontName);
const std::string& getFontName() const { return _fontDefinition._fontName;} virtual const std::string& getFontName() const { return _fontDefinition._fontName;}
virtual void setFontSize(int fontSize); virtual void setFontSize(int fontSize);
int getFontSize() const { return _fontDefinition._fontSize;} virtual int getFontSize() const { return _fontDefinition._fontSize;}
virtual bool isOpacityModifyRGB() const override; virtual bool isOpacityModifyRGB() const override;
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override; virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
@ -212,8 +213,8 @@ public:
// string related stuff // string related stuff
int getStringNumLines() const { return _currNumLines;} int getStringNumLines() const { return _currNumLines;}
CC_DEPRECATED_ATTRIBUTE int getStringLenght() const { return getStringLength(); }
int getStringLength() const; int getStringLength() const;
CC_DEPRECATED_ATTRIBUTE int getStringLenght() const { return getStringLength(); }
virtual void visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) override; virtual void visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) override;
virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) override; virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) override;
@ -271,7 +272,6 @@ protected:
bool computeHorizontalKernings(unsigned short int *stringToRender); bool computeHorizontalKernings(unsigned short int *stringToRender);
bool setCurrentString(unsigned short *stringToSet); bool setCurrentString(unsigned short *stringToSet);
bool setOriginalString(unsigned short *stringToSet); bool setOriginalString(unsigned short *stringToSet);
void resetCurrentString();
void computeStringNumLines(); void computeStringNumLines();
void updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture); void updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture);
@ -308,6 +308,7 @@ protected:
int * _horizontalKernings; int * _horizontalKernings;
unsigned int _maxLineWidth; unsigned int _maxLineWidth;
Size _labelDimensions;
unsigned int _labelWidth; unsigned int _labelWidth;
unsigned int _labelHeight; unsigned int _labelHeight;
TextHAlignment _hAlignment; TextHAlignment _hAlignment;