2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "UIRichText.h"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "2d/CCLabel.h"
|
|
|
|
#include "2d/CCSprite.h"
|
2014-07-17 17:58:40 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2014-07-17 17:58:40 +08:00
|
|
|
static std::string utf8_substr(const std::string& str, unsigned long start, unsigned long leng)
|
2014-03-28 18:54:06 +08:00
|
|
|
{
|
2014-07-17 17:58:40 +08:00
|
|
|
if (leng==0)
|
2014-03-28 18:54:06 +08:00
|
|
|
{
|
2014-07-17 17:58:40 +08:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
unsigned long c, i, ix, q, min=std::string::npos, max=std::string::npos;
|
|
|
|
for (q=0, i=0, ix=str.length(); i < ix; i++, q++)
|
|
|
|
{
|
|
|
|
if (q==start)
|
2014-03-28 18:54:06 +08:00
|
|
|
{
|
2014-07-17 17:58:40 +08:00
|
|
|
min = i;
|
2014-03-28 18:54:06 +08:00
|
|
|
}
|
2014-07-17 17:58:40 +08:00
|
|
|
if (q <= start+leng || leng==std::string::npos)
|
|
|
|
{
|
|
|
|
max = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = (unsigned char) str[i];
|
|
|
|
|
|
|
|
if (c<=127) i+=0;
|
|
|
|
else if ((c & 0xE0) == 0xC0) i+=1;
|
|
|
|
else if ((c & 0xF0) == 0xE0) i+=2;
|
|
|
|
else if ((c & 0xF8) == 0xF0) i+=3;
|
|
|
|
else return "";//invalid utf8
|
|
|
|
}
|
|
|
|
if (q <= start+leng || leng == std::string::npos)
|
|
|
|
{
|
|
|
|
max = i;
|
2014-03-28 18:54:06 +08:00
|
|
|
}
|
2014-07-17 17:58:40 +08:00
|
|
|
if (min==std::string::npos || max==std::string::npos)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return str.substr(min,max);
|
2014-03-28 18:54:06 +08:00
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
bool RichElement::init(int tag, const Color3B &color, GLubyte opacity)
|
|
|
|
{
|
|
|
|
_tag = tag;
|
|
|
|
_color = color;
|
|
|
|
_opacity = opacity;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
RichElementText* RichElementText::create(int tag, const Color3B &color, GLubyte opacity, const std::string& text, const std::string& fontName, float fontSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElementText* element = new RichElementText();
|
|
|
|
if (element && element->init(tag, color, opacity, text, fontName, fontSize))
|
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(element);
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
bool RichElementText::init(int tag, const Color3B &color, GLubyte opacity, const std::string& text, const std::string& fontName, float fontSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (RichElement::init(tag, color, opacity))
|
|
|
|
{
|
|
|
|
_text = text;
|
|
|
|
_fontName = fontName;
|
|
|
|
_fontSize = fontSize;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
RichElementImage* RichElementImage::create(int tag, const Color3B &color, GLubyte opacity, const std::string& filePath)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElementImage* element = new RichElementImage();
|
|
|
|
if (element && element->init(tag, color, opacity, filePath))
|
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(element);
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
bool RichElementImage::init(int tag, const Color3B &color, GLubyte opacity, const std::string& filePath)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (RichElement::init(tag, color, opacity))
|
|
|
|
{
|
|
|
|
_filePath = filePath;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RichElementCustomNode* RichElementCustomNode::create(int tag, const Color3B &color, GLubyte opacity, cocos2d::Node *customNode)
|
|
|
|
{
|
|
|
|
RichElementCustomNode* element = new RichElementCustomNode();
|
|
|
|
if (element && element->init(tag, color, opacity, customNode))
|
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(element);
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RichElementCustomNode::init(int tag, const Color3B &color, GLubyte opacity, cocos2d::Node *customNode)
|
|
|
|
{
|
|
|
|
if (RichElement::init(tag, color, opacity))
|
|
|
|
{
|
|
|
|
_customNode = customNode;
|
|
|
|
_customNode->retain();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RichText::RichText():
|
|
|
|
_formatTextDirty(true),
|
|
|
|
_leftSpaceWidth(0.0f),
|
|
|
|
_verticalSpace(0.0f),
|
2014-04-09 22:53:59 +08:00
|
|
|
_elementRenderersContainer(nullptr)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RichText::~RichText()
|
|
|
|
{
|
|
|
|
_richElements.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
RichText* RichText::create()
|
|
|
|
{
|
|
|
|
RichText* widget = new RichText();
|
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::init()
|
|
|
|
{
|
|
|
|
if (Widget::init())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::initRenderer()
|
|
|
|
{
|
|
|
|
_elementRenderersContainer = Node::create();
|
2014-05-15 01:07:09 +08:00
|
|
|
_elementRenderersContainer->setAnchorPoint(Vec2(0.5f, 0.5f));
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_elementRenderersContainer, 0, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::insertElement(RichElement *element, int index)
|
|
|
|
{
|
|
|
|
_richElements.insert(index, element);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::pushBackElement(RichElement *element)
|
|
|
|
{
|
|
|
|
_richElements.pushBack(element);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::removeElement(int index)
|
|
|
|
{
|
|
|
|
_richElements.erase(index);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::removeElement(RichElement *element)
|
|
|
|
{
|
|
|
|
_richElements.eraseObject(element);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::formatText()
|
|
|
|
{
|
|
|
|
if (_formatTextDirty)
|
|
|
|
{
|
|
|
|
_elementRenderersContainer->removeAllChildren();
|
|
|
|
_elementRenders.clear();
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
addNewLine();
|
2014-03-20 15:03:48 +08:00
|
|
|
for (ssize_t i=0; i<_richElements.size(); i++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElement* element = _richElements.at(i);
|
2014-07-10 00:45:27 +08:00
|
|
|
Node* elementRenderer = nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
switch (element->_type)
|
|
|
|
{
|
2014-05-12 10:39:30 +08:00
|
|
|
case RichElement::Type::TEXT:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElementText* elmtText = static_cast<RichElementText*>(element);
|
2014-04-09 23:31:05 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(elmtText->_fontName))
|
|
|
|
{
|
|
|
|
elementRenderer = Label::createWithTTF(elmtText->_text.c_str(), elmtText->_fontName, elmtText->_fontSize);
|
2014-04-17 14:08:25 +08:00
|
|
|
}
|
2014-04-09 23:31:05 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
elementRenderer = Label::createWithSystemFont(elmtText->_text.c_str(), elmtText->_fontName, elmtText->_fontSize);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-05-12 10:39:30 +08:00
|
|
|
case RichElement::Type::IMAGE:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
|
|
|
|
elementRenderer = Sprite::create(elmtImage->_filePath.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2014-05-12 10:39:30 +08:00
|
|
|
case RichElement::Type::CUSTOM:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
|
|
|
|
elementRenderer = elmtCustom->_customNode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
elementRenderer->setColor(element->_color);
|
|
|
|
elementRenderer->setOpacity(element->_opacity);
|
|
|
|
pushToContainer(elementRenderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addNewLine();
|
2014-03-20 15:03:48 +08:00
|
|
|
for (ssize_t i=0; i<_richElements.size(); i++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
RichElement* element = static_cast<RichElement*>(_richElements.at(i));
|
|
|
|
switch (element->_type)
|
|
|
|
{
|
2014-05-12 10:39:30 +08:00
|
|
|
case RichElement::Type::TEXT:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElementText* elmtText = static_cast<RichElementText*>(element);
|
|
|
|
handleTextRenderer(elmtText->_text.c_str(), elmtText->_fontName.c_str(), elmtText->_fontSize, elmtText->_color, elmtText->_opacity);
|
|
|
|
break;
|
|
|
|
}
|
2014-05-12 10:39:30 +08:00
|
|
|
case RichElement::Type::IMAGE:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
|
|
|
|
handleImageRenderer(elmtImage->_filePath.c_str(), elmtImage->_color, elmtImage->_opacity);
|
|
|
|
break;
|
|
|
|
}
|
2014-05-12 10:39:30 +08:00
|
|
|
case RichElement::Type::CUSTOM:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
|
|
|
|
handleCustomRenderer(elmtCustom->_customNode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
formarRenderers();
|
|
|
|
_formatTextDirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
void RichText::handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B &color, GLubyte opacity)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
auto fileExist = FileUtils::getInstance()->isFileExist(fontName);
|
|
|
|
Label* textRenderer = nullptr;
|
|
|
|
if (fileExist)
|
|
|
|
{
|
|
|
|
textRenderer = Label::createWithTTF(text, fontName, fontSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textRenderer = Label::createWithSystemFont(text, fontName, fontSize);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
float textRendererWidth = textRenderer->getContentSize().width;
|
|
|
|
_leftSpaceWidth -= textRendererWidth;
|
|
|
|
if (_leftSpaceWidth < 0.0f)
|
|
|
|
{
|
|
|
|
float overstepPercent = (-_leftSpaceWidth) / textRendererWidth;
|
|
|
|
std::string curText = text;
|
2014-07-17 17:58:40 +08:00
|
|
|
size_t stringLength = StringUtils::getCharacterCountInUTF8String(text);
|
2014-03-11 17:13:54 +08:00
|
|
|
int leftLength = stringLength * (1.0f - overstepPercent);
|
2014-07-17 17:58:40 +08:00
|
|
|
std::string leftWords = utf8_substr(curText,0,leftLength);
|
2014-07-23 14:10:00 +08:00
|
|
|
std::string cutWords = utf8_substr(curText, leftLength, stringLength - leftLength);
|
2014-03-11 17:13:54 +08:00
|
|
|
if (leftLength > 0)
|
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
Label* leftRenderer = nullptr;
|
|
|
|
if (fileExist)
|
|
|
|
{
|
2014-07-17 17:58:40 +08:00
|
|
|
leftRenderer = Label::createWithTTF(utf8_substr(leftWords, 0, leftLength), fontName, fontSize);
|
|
|
|
}
|
2014-04-09 23:31:05 +08:00
|
|
|
else
|
|
|
|
{
|
2014-07-17 17:58:40 +08:00
|
|
|
leftRenderer = Label::createWithSystemFont(utf8_substr(leftWords, 0, leftLength), fontName, fontSize);
|
2014-04-09 23:31:05 +08:00
|
|
|
}
|
|
|
|
if (leftRenderer)
|
|
|
|
{
|
|
|
|
leftRenderer->setColor(color);
|
|
|
|
leftRenderer->setOpacity(opacity);
|
|
|
|
pushToContainer(leftRenderer);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
addNewLine();
|
|
|
|
handleTextRenderer(cutWords.c_str(), fontName, fontSize, color, opacity);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textRenderer->setColor(color);
|
|
|
|
textRenderer->setOpacity(opacity);
|
|
|
|
pushToContainer(textRenderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
void RichText::handleImageRenderer(const std::string& fileParh, const Color3B &color, GLubyte opacity)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Sprite* imageRenderer = Sprite::create(fileParh);
|
|
|
|
handleCustomRenderer(imageRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::handleCustomRenderer(cocos2d::Node *renderer)
|
|
|
|
{
|
|
|
|
Size imgSize = renderer->getContentSize();
|
|
|
|
_leftSpaceWidth -= imgSize.width;
|
|
|
|
if (_leftSpaceWidth < 0.0f)
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
pushToContainer(renderer);
|
|
|
|
_leftSpaceWidth -= imgSize.width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pushToContainer(renderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::addNewLine()
|
|
|
|
{
|
|
|
|
_leftSpaceWidth = _customSize.width;
|
|
|
|
_elementRenders.push_back(new Vector<Node*>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::formarRenderers()
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
float newContentSizeWidth = 0.0f;
|
|
|
|
float newContentSizeHeight = 0.0f;
|
|
|
|
|
|
|
|
Vector<Node*>* row = (_elementRenders[0]);
|
|
|
|
float nextPosX = 0.0f;
|
2014-03-20 11:58:36 +08:00
|
|
|
for (ssize_t j=0; j<row->size(); j++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Node* l = row->at(j);
|
2014-05-15 01:07:09 +08:00
|
|
|
l->setAnchorPoint(Vec2::ZERO);
|
|
|
|
l->setPosition(Vec2(nextPosX, 0.0f));
|
2014-06-25 11:27:48 +08:00
|
|
|
_elementRenderersContainer->addChild(l, 1);
|
2014-03-11 17:13:54 +08:00
|
|
|
Size iSize = l->getContentSize();
|
|
|
|
newContentSizeWidth += iSize.width;
|
|
|
|
newContentSizeHeight = MAX(newContentSizeHeight, iSize.height);
|
|
|
|
nextPosX += iSize.width;
|
|
|
|
}
|
|
|
|
_elementRenderersContainer->setContentSize(Size(newContentSizeWidth, newContentSizeHeight));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float newContentSizeHeight = 0.0f;
|
|
|
|
float *maxHeights = new float[_elementRenders.size()];
|
|
|
|
|
2014-03-24 10:12:40 +08:00
|
|
|
for (size_t i=0; i<_elementRenders.size(); i++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Vector<Node*>* row = (_elementRenders[i]);
|
|
|
|
float maxHeight = 0.0f;
|
2014-03-20 11:58:36 +08:00
|
|
|
for (ssize_t j=0; j<row->size(); j++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Node* l = row->at(j);
|
|
|
|
maxHeight = MAX(l->getContentSize().height, maxHeight);
|
|
|
|
}
|
|
|
|
maxHeights[i] = maxHeight;
|
|
|
|
newContentSizeHeight += maxHeights[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float nextPosY = _customSize.height;
|
2014-03-24 10:12:40 +08:00
|
|
|
for (size_t i=0; i<_elementRenders.size(); i++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Vector<Node*>* row = (_elementRenders[i]);
|
|
|
|
float nextPosX = 0.0f;
|
|
|
|
nextPosY -= (maxHeights[i] + _verticalSpace);
|
|
|
|
|
2014-03-20 11:58:36 +08:00
|
|
|
for (ssize_t j=0; j<row->size(); j++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Node* l = row->at(j);
|
2014-05-15 01:07:09 +08:00
|
|
|
l->setAnchorPoint(Vec2::ZERO);
|
|
|
|
l->setPosition(Vec2(nextPosX, nextPosY));
|
2014-06-25 11:27:48 +08:00
|
|
|
_elementRenderersContainer->addChild(l, 1);
|
2014-03-11 17:13:54 +08:00
|
|
|
nextPosX += l->getContentSize().width;
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
_elementRenderersContainer->setContentSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
delete [] maxHeights;
|
|
|
|
}
|
|
|
|
|
2014-03-14 15:38:43 +08:00
|
|
|
size_t length = _elementRenders.size();
|
2014-03-24 10:12:40 +08:00
|
|
|
for (size_t i = 0; i<length; i++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Vector<Node*>* l = _elementRenders[i];
|
|
|
|
l->clear();
|
|
|
|
delete l;
|
|
|
|
}
|
|
|
|
_elementRenders.clear();
|
|
|
|
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
2014-04-17 14:08:25 +08:00
|
|
|
Size s = getVirtualRendererSize();
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(s);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(_customSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
updateContentSizeWithTextureSize(_contentSize);
|
2014-04-17 14:08:25 +08:00
|
|
|
_elementRenderersContainer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::pushToContainer(cocos2d::Node *renderer)
|
|
|
|
{
|
|
|
|
if (_elementRenders.size() <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_elementRenders[_elementRenders.size()-1]->pushBack(renderer);
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void RichText::visit(cocos2d::Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (_enabled)
|
|
|
|
{
|
|
|
|
formatText();
|
2014-05-31 07:42:05 +08:00
|
|
|
Widget::visit(renderer, parentTransform, parentFlags);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setVerticalSpace(float space)
|
|
|
|
{
|
|
|
|
_verticalSpace = space;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void RichText::setAnchorPoint(const Vec2 &pt)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Widget::setAnchorPoint(pt);
|
|
|
|
_elementRenderersContainer->setAnchorPoint(pt);
|
|
|
|
}
|
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
const Size& RichText::getVirtualRendererSize() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _elementRenderersContainer->getContentSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::ignoreContentAdaptWithSize(bool ignore)
|
|
|
|
{
|
|
|
|
if (_ignoreSize != ignore)
|
|
|
|
{
|
|
|
|
_formatTextDirty = true;
|
|
|
|
Widget::ignoreContentAdaptWithSize(ignore);
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 15:25:44 +08:00
|
|
|
|
|
|
|
std::string RichText::getDescription() const
|
|
|
|
{
|
|
|
|
return "RichText";
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-19 19:45:24 +08:00
|
|
|
NS_CC_END
|