-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIRichText.cpp
806 lines (723 loc) · 24.9 KB
/
UIRichText.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/****************************************************************************
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"
#include "platform/CCFileUtils.h"
#include "2d/CCLabel.h"
#include "2d/CCSprite.h"
#include "base/ccUTF8.h"
#include "ui/UIHelper.h"
NS_CC_BEGIN
namespace ui {
bool RichElement::init(int tag, const Color3B &color, GLubyte opacity)
{
_tag = tag;
_color = color;
_opacity = opacity;
return true;
}
RichElementText* RichElementText::create(int tag, const Color3B &color, GLubyte opacity, const std::string& text, const std::string& fontName, float fontSize)
{
RichElementText* element = new (std::nothrow) RichElementText();
if (element && element->init(tag, color, opacity, text, fontName, fontSize))
{
element->autorelease();
return element;
}
CC_SAFE_DELETE(element);
return nullptr;
}
bool RichElementText::init(int tag, const Color3B &color, GLubyte opacity, const std::string& text, const std::string& fontName, float fontSize)
{
if (RichElement::init(tag, color, opacity))
{
_text = text;
_fontName = fontName;
_fontSize = fontSize;
return true;
}
return false;
}
RichElementImage* RichElementImage::create(int tag, const Color3B &color, GLubyte opacity, const std::string& filePath)
{
RichElementImage* element = new (std::nothrow) RichElementImage();
if (element && element->init(tag, color, opacity, filePath))
{
element->autorelease();
return element;
}
CC_SAFE_DELETE(element);
return nullptr;
}
bool RichElementImage::init(int tag, const Color3B &color, GLubyte opacity, const std::string& filePath)
{
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 (std::nothrow) RichElementCustomNode();
if (element && element->init(tag, color, opacity, customNode))
{
element->autorelease();
return element;
}
CC_SAFE_DELETE(element);
return nullptr;
}
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;
}
RichElementNewLine* RichElementNewLine::create(int tag, const Color3B& color, GLubyte opacity)
{
RichElementNewLine* element = new (std::nothrow) RichElementNewLine();
if (element && element->init(tag, color, opacity))
{
element->autorelease();
return element;
}
CC_SAFE_DELETE(element);
return nullptr;
}
RichText::RichText():
_formatTextDirty(true),
_leftSpaceWidth(0.0f),
_verticalSpace(0.0f),
m_fLimitMaxWidth(0.0f)
{
}
RichText::~RichText()
{
_richElements.clear();
}
RichText* RichText::create()
{
RichText* widget = new (std::nothrow) RichText();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
bool RichText::init()
{
if (Widget::init())
{
return true;
}
return false;
}
void RichText::initRenderer()
{
}
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)
{
this->removeAllProtectedChildren();
_elementRenders.clear();
if (_ignoreSize)
{
addNewLine();
for (ssize_t i=0; i<_richElements.size(); i++)
{
RichElement* element = _richElements.at(i);
Node* elementRenderer = nullptr;
switch (element->_type)
{
case RichElement::Type::TEXT:
{
RichElementText* elmtText = static_cast<RichElementText*>(element);
if (FileUtils::getInstance()->isFileExist(elmtText->_fontName))
{
elementRenderer = Label::createWithTTF(elmtText->_text.c_str(), elmtText->_fontName, elmtText->_fontSize);
}
else
{
elementRenderer = Label::createWithSystemFont(elmtText->_text.c_str(), elmtText->_fontName, elmtText->_fontSize);
}
break;
}
case RichElement::Type::IMAGE:
{
RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
elementRenderer = Sprite::create(elmtImage->_filePath.c_str());
break;
}
case RichElement::Type::CUSTOM:
{
RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
elementRenderer = elmtCustom->_customNode;
break;
}
case RichElement::Type::NEWLINE:
{
addNewLine();
break;
}
default:
break;
}
elementRenderer->setColor(element->_color);
elementRenderer->setOpacity(element->_opacity);
pushToContainer(elementRenderer);
}
}
else
{
addNewLine();
for (ssize_t i=0; i<_richElements.size(); i++)
{
RichElement* element = static_cast<RichElement*>(_richElements.at(i));
switch (element->_type)
{
case RichElement::Type::TEXT:
{
RichElementText* elmtText = static_cast<RichElementText*>(element);
handleTextRenderer(elmtText->_text.c_str(), elmtText->_fontName.c_str(), elmtText->_fontSize, elmtText->_color, elmtText->_opacity);
break;
}
case RichElement::Type::IMAGE:
{
RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
handleImageRenderer(elmtImage->_filePath.c_str(), elmtImage->_color, elmtImage->_opacity);
break;
}
case RichElement::Type::CUSTOM:
{
RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
handleCustomRenderer(elmtCustom->_customNode);
break;
}
case RichElement::Type::NEWLINE:
{
addNewLine();
break;
}
default:
break;
}
}
}
formarRenderers();
_formatTextDirty = false;
}
}
void RichText::selectMaxValue(int& outValue, int& inputValue)
{
outValue = (outValue > inputValue) ? outValue : inputValue;
}
void RichText::selectMinValue(int& outValue, int& inputValue)
{
//小于0忽略
if (inputValue > 0)
{
outValue = (outValue < inputValue) ? outValue : inputValue;
}
}
void RichText::adjustStringComping(int& leftLength, float& leftWidth, int findNewLinePos, std::string& leftStr, std::string& curText, Label* textRenderer)
{
if (leftLength <= 0 || leftStr.empty() || curText.empty())
{
return;
}
textRenderer->setString(leftStr);
float newTextWidth = textRenderer->getContentSize().width;
bool isOver = (newTextWidth + 12.0f) > m_fLimitMaxWidth;
//取leftLength该位置的字符
std::string endStr = curText.substr(leftLength - 1, 1);
if (endStr != " ")
{
endStr = curText.substr(leftLength, 1);
}
//从当前已经截断的字符串的末位分别查找前后最近的空格字符的位置
int nextSpaceCharPos = (endStr == " ") ? leftLength + 1 : curText.find_first_of(" ", leftLength);
int preSpaceCharPos = (endStr == " ") ? curText.find_last_of(" ", leftLength - 1) : curText.find_last_of(" ", leftLength);
//英文逗号
int nextCommaCharPos = (endStr == ",") ? leftLength + 1 : curText.find_first_of(",", leftLength);
int preCommaCharPos = (endStr == ",") ? curText.find_last_of(",", leftLength - 1) : curText.find_last_of(",", leftLength);
//英文句号
int nextDotCharPos = (endStr == ".") ? leftLength + 1 : curText.find_first_of(".", leftLength);
int preDotCharPos = (endStr == ".") ? curText.find_last_of(".", leftLength - 1) : curText.find_last_of(".", leftLength);
//英文问号
int nextQuestionCharPos = (endStr == "?") ? leftLength + 1 : curText.find_first_of("?", leftLength);
int preQuestionCharPos = (endStr == "?") ? curText.find_last_of("?", leftLength - 1) : curText.find_last_of("?", leftLength);
//英文分号
int nextSemicolonCharPos = (endStr == ";") ? leftLength + 1 : curText.find_first_of(";", leftLength);
int preSemicolonCharPos = (endStr == ";") ? curText.find_last_of(";", leftLength - 1) : curText.find_last_of(";", leftLength);
//英文感叹号
int nextExclamationMarkCharPos = (endStr == "!") ? leftLength + 1 : curText.find_first_of("!", leftLength);
int preExclamationMarkCharPos = (endStr == "!") ? curText.find_last_of("!", leftLength - 1) : curText.find_last_of("!", leftLength);
//英文等于号
int nextEqualMarkCharPos = (endStr == "=") ? leftLength + 1 : curText.find_first_of("=", leftLength);
int preEqualMarkCarPos = (endStr == "=") ? curText.find_last_of("=", leftLength - 1) : curText.find_last_of("=", leftLength);
//英文小括号
int nextParenthesesCharPos = (endStr == ")") ? leftLength + 1 : curText.find_first_of(")", leftLength);
int preParenthesesCharPos = curText.find_last_of("(", leftLength);
//英文中括号
int nextMiddleMarkCharPos = (endStr == "]") ? leftLength + 1 : curText.find_first_of("]", leftLength);
int preMiddleMarkCharPos = curText.find_last_of("[", leftLength);
//英文冒号
int nextColonCharPos = (endStr == ":") ? leftLength + 1 : curText.find_first_of(":", leftLength);
int preColonCharPos = (endStr == ":") ? curText.find_last_of(":", leftLength - 1) : curText.find_last_of(":", leftLength);
//比较所有前置符号谁更大
int preMaxMark = preSpaceCharPos;
selectMaxValue(preMaxMark, preCommaCharPos);
selectMaxValue(preMaxMark, preDotCharPos);
selectMaxValue(preMaxMark, preQuestionCharPos);
selectMaxValue(preMaxMark, preSemicolonCharPos);
selectMaxValue(preMaxMark, preExclamationMarkCharPos);
selectMaxValue(preMaxMark, preEqualMarkCarPos);
selectMaxValue(preMaxMark, preParenthesesCharPos);
selectMaxValue(preMaxMark, preMiddleMarkCharPos);
selectMaxValue(preMaxMark, preColonCharPos);
if (findNewLinePos > 0)
{
nextSpaceCharPos = ((nextSpaceCharPos > findNewLinePos) || (nextSpaceCharPos == -1)) ?
findNewLinePos : nextSpaceCharPos;
}
//比较所有后置符号谁更小
int nextMinMark = nextSpaceCharPos;
selectMinValue(nextMinMark, nextCommaCharPos);
selectMinValue(nextMinMark, nextDotCharPos);
selectMinValue(nextMinMark, nextQuestionCharPos);
selectMinValue(nextMinMark, nextSemicolonCharPos);
selectMinValue(nextMinMark, nextExclamationMarkCharPos);
selectMinValue(nextMinMark, nextEqualMarkCharPos);
selectMinValue(nextMinMark, nextParenthesesCharPos);
selectMinValue(nextMinMark, nextMiddleMarkCharPos);
selectMinValue(nextMinMark, nextColonCharPos);
if (nextMinMark != std::string::npos && preMaxMark != std::string::npos)
{
if (nextMinMark <= preMaxMark)
{
return;
}
//说明被截断位置的后续字符跟前面字符是一个整体
if (nextMinMark - leftLength > 0)
{
leftLength -= (leftLength - preMaxMark - 1);
leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
textRenderer->setString(leftStr);
leftWidth = textRenderer->getContentSize().width;
}
if (!isOver)
{
return;
}
//超框处理
if ((leftWidth + 12.0f) >= m_fLimitMaxWidth && m_fLimitMaxWidth > 0.0f)
{
leftLength -= (nextMinMark - preMaxMark - 1);
leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
textRenderer->setString(leftStr);
leftWidth = textRenderer->getContentSize().width;
}
}
}
void RichText::handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B &color, GLubyte opacity)
{
auto fileExist = FileUtils::getInstance()->isFileExist(fontName);
Label* textRenderer = nullptr;
if (fileExist)
{
textRenderer = Label::createWithTTF(text, fontName, fontSize);
}
else
{
textRenderer = Label::createWithSystemFont(text, fontName, fontSize);
}
float textRendererWidth = textRenderer->getContentSize().width;
_leftSpaceWidth -= textRendererWidth;
// add by zhong
int findNewLinePos = text.find("\n");
if (std::string::npos != findNewLinePos) {
// 计算\n在utf中第几个字符
std::string strTemp = text.substr(0, findNewLinePos);
findNewLinePos = StringUtils::getCharacterCountInUTF8String(strTemp);
}
if (_leftSpaceWidth < 0.0f)
{
float overstepPercent = (-_leftSpaceWidth) / textRendererWidth;
std::string curText = text;
size_t stringLength = StringUtils::getCharacterCountInUTF8String(text);
int leftLength = stringLength * (1.0f - overstepPercent);
// The adjustment of the new line position
auto originalLeftSpaceWidth = _leftSpaceWidth + textRendererWidth;
auto leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
textRenderer->setString(leftStr);
auto leftWidth = textRenderer->getContentSize().width;
if (originalLeftSpaceWidth < leftWidth) {
// Have protruding
for (;;) {
leftLength--;
leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
textRenderer->setString(leftStr);
leftWidth = textRenderer->getContentSize().width;
//================add by wangzhen=======
if (leftWidth <= originalLeftSpaceWidth || leftLength <= 0)
{
//检测是否是完整的英文单词
adjustStringComping(leftLength, leftWidth, findNewLinePos, leftStr, curText, textRenderer);
break;
}
//=======================================
/*if (leftWidth <= originalLeftSpaceWidth) {
break;
}
else if (leftLength <= 0) {
break;
}*/
}
}
else if (leftWidth < originalLeftSpaceWidth) {
// A wide margin
for (;;) {
leftLength++;
leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
textRenderer->setString(leftStr);
leftWidth = textRenderer->getContentSize().width;
//================add by wangzhen==========================================
if (originalLeftSpaceWidth < leftWidth || stringLength <= leftLength)
{
//检测是否是完整的英文单词
adjustStringComping(leftLength, leftWidth, findNewLinePos, leftStr, curText, textRenderer);
break;
}
if ((stringLength >> 1) <= leftLength)
{
break;
}
//===============================================================================================
/*if (originalLeftSpaceWidth < leftWidth) {
leftLength--;
break;
}
else if (stringLength <= leftLength) {
break;
}*/
}
}
else
{
//检测是否是完整的英文单词
adjustStringComping(leftLength, leftWidth, findNewLinePos, leftStr, curText, textRenderer);
}
//The minimum cut length is 1, otherwise will cause the infinite loop.
//if (0 == leftLength) leftLength = 1;
// 假如有\n要提前换行 modify by zhong
bool bFindNewLine = false;
if (std::string::npos != findNewLinePos && findNewLinePos < leftLength) {
leftLength = findNewLinePos;
bFindNewLine = true;
}
std::string leftWords;
if (leftLength > 0)
{
leftWords = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
//================add by wangzhen=============
adjustStringComping(leftLength, leftWidth, findNewLinePos, leftWords, curText, textRenderer);
//===========================================
}
Label* leftRenderer = nullptr;
if (leftLength > 0) {
if (fileExist) {
leftRenderer = Label::createWithTTF(Helper::getSubStringOfUTF8String(leftWords, 0, leftLength), fontName, fontSize);
}
else {
leftRenderer = Label::createWithSystemFont(Helper::getSubStringOfUTF8String(leftWords, 0, leftLength), fontName, fontSize);
}
}
else {
// leftRenderer = Label::create();
//add wangzhen 多个换行符不能忽略高度
if (fileExist)
{
leftRenderer = Label::createWithTTF(" ", fontName, fontSize);
}
else
{
leftRenderer = Label::createWithSystemFont(" ", fontName, fontSize);
}
}
if (leftRenderer) {
leftRenderer->setColor(color);
leftRenderer->setOpacity(opacity);
pushToContainer(leftRenderer);
}
addNewLine();
// 跳过\n
if (bFindNewLine) {
++ leftLength;
}
std::string cutWords = Helper::getSubStringOfUTF8String(curText, leftLength, stringLength - leftLength);
handleTextRenderer(cutWords.c_str(), fontName, fontSize, color, opacity);
}
else
{
// 假如有\n要提前换行 modify by zhong
if (std::string::npos != findNewLinePos) {
size_t stringLength = StringUtils::getCharacterCountInUTF8String(text);
std::string curText = text;
std::string leftWords;
int leftLength = findNewLinePos;
if (leftLength > 0)
{
leftWords = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
//================add by wangzhen=======
float leftWidth = 0.0f;
adjustStringComping(leftLength, leftWidth, findNewLinePos, leftWords, curText, textRenderer);
}
Label* leftRenderer = nullptr;
if (leftLength > 0)
{
if (fileExist)
{
leftRenderer = Label::createWithTTF(Helper::getSubStringOfUTF8String(leftWords, 0, leftLength), fontName, fontSize);
}
else
{
leftRenderer = Label::createWithSystemFont(Helper::getSubStringOfUTF8String(leftWords, 0, leftLength), fontName, fontSize);
}
}
else
{
//leftRenderer = Label::create();
//add wangzhen 多个换行符不能忽略高度
if (fileExist)
{
leftRenderer = Label::createWithTTF(" ", fontName, fontSize);
}
else
{
leftRenderer = Label::createWithSystemFont(" ", fontName, fontSize);
}
}
if (leftRenderer)
{
leftRenderer->setColor(color);
leftRenderer->setOpacity(opacity);
pushToContainer(leftRenderer);
}
addNewLine();
// 跳过\n
++leftLength;
std::string cutWords = Helper::getSubStringOfUTF8String(curText, leftLength, stringLength - leftLength);
handleTextRenderer(cutWords.c_str(), fontName, fontSize, color, opacity);
} else {
textRenderer->setColor(color);
textRenderer->setOpacity(opacity);
pushToContainer(textRenderer);
}
}
}
void RichText::handleImageRenderer(const std::string& fileParh, const Color3B &color, GLubyte opacity)
{
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;
for (ssize_t j=0; j<row->size(); j++)
{
Node* l = row->at(j);
l->setAnchorPoint(Vec2::ZERO);
l->setPosition(nextPosX, 0.0f);
this->addProtectedChild(l, 1);
Size iSize = l->getContentSize();
newContentSizeWidth += iSize.width;
newContentSizeHeight = MAX(newContentSizeHeight, iSize.height);
nextPosX += iSize.width;
}
this->setContentSize(Size(newContentSizeWidth, newContentSizeHeight));
}
else
{
float newContentSizeHeight = 0.0f;
float *maxHeights = new float[_elementRenders.size()];
for (size_t i=0; i<_elementRenders.size(); i++)
{
// add by zhong
if (0 != i) {
newContentSizeHeight += _verticalSpace;
}
Vector<Node*>* row = (_elementRenders[i]);
float maxHeight = 0.0f;
for (ssize_t j=0; j<row->size(); j++)
{
Node* l = row->at(j);
maxHeight = MAX(l->getContentSize().height, maxHeight);
}
maxHeights[i] = maxHeight;
newContentSizeHeight += maxHeights[i];
}
//====add wangzhen====
_customSize.height = newContentSizeHeight;
float realWidth = 0;
//================
float nextPosY = _customSize.height;
for (size_t i=0; i<_elementRenders.size(); i++)
{
Vector<Node*>* row = (_elementRenders[i]);
float nextPosX = 0.0f;
// modify by zhong
float newlineHeight = maxHeights[i];
if (0 != i) {
newlineHeight += _verticalSpace;
}
nextPosY -= newlineHeight;
for (ssize_t j=0; j<row->size(); j++)
{
Node* l = row->at(j);
l->setAnchorPoint(Vec2::ZERO);
l->setPosition(nextPosX, nextPosY);
this->addProtectedChild(l, 1);
nextPosX += l->getContentSize().width;
}
//====add wangzhen====
if (realWidth < nextPosX)
{
realWidth = nextPosX;
}
//================
}
//=====add wangzhen======
m_realRichSize.width = realWidth;
m_realRichSize.height = _customSize.height;
//============================
delete [] maxHeights;
}
size_t length = _elementRenders.size();
for (size_t i = 0; i<length; i++)
{
Vector<Node*>* l = _elementRenders[i];
l->clear();
delete l;
}
_elementRenders.clear();
if (_ignoreSize)
{
Size s = getVirtualRendererSize();
this->setContentSize(s);
}
else
{
this->setContentSize(_customSize);
}
updateContentSizeWithTextureSize(_contentSize);
}
void RichText::adaptRenderers()
{
this->formatText();
}
void RichText::pushToContainer(cocos2d::Node *renderer)
{
if (_elementRenders.size() <= 0)
{
return;
}
_elementRenders[_elementRenders.size()-1]->pushBack(renderer);
}
void RichText::setVerticalSpace(float space)
{
_verticalSpace = space;
}
void RichText::ignoreContentAdaptWithSize(bool ignore)
{
if (_ignoreSize != ignore)
{
_formatTextDirty = true;
Widget::ignoreContentAdaptWithSize(ignore);
}
}
std::string RichText::getDescription() const
{
return "RichText";
}
Size RichText::getRealSize()
{
return m_realRichSize;
}
void RichText::refreshRichText()
{
_ignoreSize = false;
_formatTextDirty = true;
this->formatText();
}
}
NS_CC_END