PuzzleSDK
LabelTestNew.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "LabelTestNew.h"
26#include "../testResource.h"
27#include "renderer/CCRenderer.h"
28#include "2d/CCFontAtlasCache.h"
29
31using namespace ui;
32using namespace extension;
33
34enum {
41};
42
43enum {
52};
53
54//------------------------------------------------------------------
55//
56// AtlasDemoNew
57//
58//------------------------------------------------------------------
59
60NewLabelTests::NewLabelTests()
61{
77
94
97
101
127
134
136
144};
145
147{
148 _time = 0;
149
150 auto col = LayerColor::create( Color4B(128,128,128,255) );
151 addChild(col, -10);
152
153 auto label1 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "Test");
154
155 label1->setAnchorPoint( Vec2(0,0) );
156 addChild(label1, 0, kTagBitmapAtlas1);
157 auto fade = FadeOut::create(1.0f);
158 auto fade_in = fade->reverse();
159 auto seq = Sequence::create(fade, fade_in, nullptr);
160 auto repeat = RepeatForever::create(seq);
161 label1->runAction(repeat);
162
163 auto label2 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "Test");
164 label2->setColor( Color3B::RED );
165 addChild(label2, 0, kTagBitmapAtlas2);
166 auto tint = Sequence::create(TintTo::create(1, 255, 0, 0),
167 TintTo::create(1, 0, 255, 0),
168 TintTo::create(1, 0, 0, 255),
169 nullptr);
170 label2->runAction( RepeatForever::create(tint) );
171
172 auto label3 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "Test");
173 label3->setAnchorPoint( Vec2(1,1) );
174 addChild(label3, 0, kTagBitmapAtlas3);
175
176 label1->setPosition( VisibleRect::leftBottom() );
177 label2->setPosition( VisibleRect::center() );
178 label3->setPosition( VisibleRect::rightTop() );
179
180 schedule(CC_CALLBACK_1(LabelFNTColorAndOpacity::step, this), "step_key");
181}
182
184{
185 _time += dt;
186 char string[15] = {0};
187 sprintf(string, "%2.2f Test j", _time);
188
189 auto label1 = (Label*) getChildByTag(kTagBitmapAtlas1);
190 label1->setString(string);
191
192 auto label2 = (Label*) getChildByTag(kTagBitmapAtlas2);
193 label2->setString(string);
194
195 auto label3 = (Label*) getChildByTag(kTagBitmapAtlas3);
196 label3->setString(string);
197}
198
200{
201 return "New Label + Bitmap font";
202}
203
205{
206 return "Testing fade/tint action";
207}
208
210{
211 _time = 0;
212
213 auto s = Director::getInstance()->getWinSize();
214
215 auto drawNode = DrawNode::create();
216 drawNode->drawLine( Vec2(0.0f, s.height/2), Vec2(s.width, s.height/2), Color4F(1.0f, 1.0f, 1.0f, 1.0f) );
217 drawNode->drawLine( Vec2(s.width/2, 0.0f), Vec2(s.width/2, s.height), Color4F(1.0f, 1.0f, 1.0f, 1.0f) );
218 addChild(drawNode, -1);
219
220 // Upper Label
221 auto label = Label::createWithBMFont("fonts/bitmapFontTest.fnt", "Bitmap Font Atlas");
222 addChild(label);
223
224
225 label->setPosition( Vec2(s.width/2, s.height/2) );
226
227 auto BChar = (Sprite*) label->getLetter(0);
228 auto FChar = (Sprite*) label->getLetter(7);
229 auto AChar = (Sprite*) label->getLetter(12);
230
231
232 auto rotate = RotateBy::create(2, 360);
233 auto rot_4ever = RepeatForever::create(rotate);
234
235 auto scale = ScaleBy::create(2, 1.5f);
236 auto scale_back = scale->reverse();
237 auto scale_seq = Sequence::create(scale, scale_back,nullptr);
238 auto scale_4ever = RepeatForever::create(scale_seq);
239
240 auto jump = JumpBy::create(0.5f, Vec2::ZERO, 60, 1);
241 auto jump_4ever = RepeatForever::create(jump);
242
243 auto fade_out = FadeOut::create(1);
244 auto fade_in = FadeIn::create(1);
245 auto seq = Sequence::create(fade_out, fade_in, nullptr);
246 auto fade_4ever = RepeatForever::create(seq);
247
248 BChar->runAction(rot_4ever);
249 BChar->runAction(scale_4ever);
250 FChar->runAction(jump_4ever);
251 AChar->runAction(fade_4ever);
252
253
254 // Bottom Label
255 auto label2 = Label::createWithBMFont("fonts/bitmapFontTest.fnt", "00.0");
256 addChild(label2, 0, kTagBitmapAtlas2);
257 label2->setPosition( Vec2(s.width/2.0f, 80.0f) );
258
259 auto lastChar = (Sprite*) label2->getLetter(3);
260 lastChar->runAction( rot_4ever->clone() );
261
262 schedule(CC_CALLBACK_1(LabelFNTSpriteActions::step, this), 0.1f, "step_key");
263}
264
266{
267 _time += dt;
268 char string[10] = {0};
269 sprintf(string, "%04.1f", _time);
270 auto label1 = (Label*) getChildByTag(kTagBitmapAtlas2);
271 label1->setString(string);
272}
273
275{
276 return "New Label + Bitmap font";
277}
278
280{
281 return "Testing run action on the specified character";
282}
283
285{
286 auto label = Label::createWithBMFont("fonts/bitmapFontTest4.fnt", "abcdefg");
287 addChild(label);
288
289 auto s = Director::getInstance()->getWinSize();
290
291 label->setPosition( Vec2(s.width/2, s.height/2) );
292}
293
294std::string LabelFNTPadding::title() const
295{
296 return "New Label + Bitmap font";
297}
298
299std::string LabelFNTPadding::subtitle() const
300{
301 return "Testing padding";
302}
303
305{
306 auto s = Director::getInstance()->getWinSize();
307
308 Label* label = nullptr;
309 label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "FaFeFiFoFu");
310 addChild(label);
311 label->setPosition( Vec2(s.width/2, s.height/2+50) );
312
313 label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "fafefifofu");
314 addChild(label);
315 label->setPosition( Vec2(s.width/2, s.height/2) );
316
317 label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "aeiou");
318 addChild(label);
319 label->setPosition( Vec2(s.width/2, s.height/2-50) );
320}
321
322std::string LabelFNTOffset::title() const
323{
324 return "New Label + Bitmap font";
325}
326
327std::string LabelFNTOffset::subtitle() const
328{
329 return "Rendering should be OK. Testing offset";
330}
331
333{
334 auto winSize = Director::getInstance()->getWinSize();
335
336 auto label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "Blue");
337 label->setColor( Color3B::BLUE );
338 addChild(label);
339 label->setPosition(winSize.width / 2, winSize.height * 0.7f);
340
341 label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "Red");
342 addChild(label);
343 label->setPosition(winSize.width / 2, winSize.height * 0.5f);
344 label->setColor( Color3B::RED );
345
346 label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "Green");
347 addChild(label);
348 label->setPosition(winSize.width / 2, winSize.height * 0.3f);
349 label->setColor( Color3B::GREEN );
350 label->setString("Green");
351}
352
353std::string LabelFNTColor::title() const
354{
355 return "New Label + Bitmap font";
356}
357
358std::string LabelFNTColor::subtitle() const
359{
360 return "Testing color";
361}
362
364{
365 auto winSize = Director::getInstance()->getWinSize();
366
367 auto label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "Opacity 100");
368 label->setOpacity(100);
369 addChild(label);
370 label->setPosition(winSize.width / 2, winSize.height * 0.6f);
371
372 label = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "Opacity 200");
373 label->setOpacity(200);
374 addChild(label);
375 label->setPosition(winSize.width / 2, winSize.height * 0.4f);
376}
377
378std::string LabelFNTOpacity::title() const
379{
380 return "New Label + Bitmap font";
381}
382
383std::string LabelFNTOpacity::subtitle() const
384{
385 return "Testing opacity";
386}
387
389{
390 // Upper Label
391 for ( int i=0 ; i < 100;i ++ )
392 {
393 char str[6] = {0};
394 sprintf(str, "-%d-", i);
395 auto label = Label::createWithBMFont("fonts/bitmapFontTest.fnt", str);
396 addChild(label);
397
398 auto s = Director::getInstance()->getWinSize();
399
400 auto p = Vec2( CCRANDOM_0_1() * s.width, CCRANDOM_0_1() * s.height);
401 label->setPosition( p );
402 }
403}
404
406{
407 return "New Label + Bitmap font";
408}
409
411{
412 return "Creating several Labels using the same FNT file; should be fast";
413}
414
416{
417 Size s;
418
419 // Left
420 auto label1 = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", " Multi line\nLeft");
421 label1->setAnchorPoint(Vec2(0,0));
422 addChild(label1, 0, kTagBitmapAtlas1);
423
424 s = label1->getContentSize();
425 CCLOG("content size: %.2fx%.2f", s.width, s.height);
426
427
428 // Center
429 auto label2 = Label::createWithBMFont( "fonts/bitmapFontTest3.fnt", "Multi line\nCenter");
430 addChild(label2, 0, kTagBitmapAtlas2);
431
432 s= label2->getContentSize();
433 CCLOG("content size: %.2fx%.2f", s.width, s.height);
434
435 // right
436 auto label3 = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", "Multi line\nRight\nThree lines Three");
437 label3->setAnchorPoint(Vec2(1, 1));
438 addChild(label3, 0, kTagBitmapAtlas3);
439
440 s = label3->getContentSize();
441 CCLOG("content size: %.2fx%.2f", s.width, s.height);
442
443 label1->setPosition(VisibleRect::leftBottom());
444 label2->setPosition(VisibleRect::center());
445 label3->setPosition(VisibleRect::rightTop());
446}
447
448std::string LabelFNTMultiLine::title() const
449{
450 return "New Label + Bitmap font";
451}
452
454{
455 return "Multiline + anchor point";
456}
457
459{
460 auto s = Director::getInstance()->getWinSize();
461
462 // LabelBMFont
463 auto label1 = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", "", TextHAlignment::CENTER, s.width);
464 addChild(label1, 0, kTagBitmapAtlas1);
465 label1->setPosition(Vec2(s.width/2, s.height - 100));
466
467 // LabelTTF
468 TTFConfig ttfConfig("fonts/arial.ttf",24);
469 auto label2 = Label::createWithTTF(ttfConfig,"", TextHAlignment::CENTER,s.width);
470 addChild(label2, 0, kTagBitmapAtlas2);
471 label2->setPosition(Vec2(s.width/2, s.height / 2));
472
473 auto label3 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist");
474 addChild(label3, 0, kTagBitmapAtlas3);
475 label3->setPosition(Vec2(s.width/2, 100.0f));
476
477 schedule(CC_CALLBACK_1(LabelFNTandTTFEmpty::updateStrings, this), 1.0f, "update_strings_key");
478
479 setEmpty = false;
480}
481
483{
484 auto label1 = static_cast<Label*>( getChildByTag(kTagBitmapAtlas1) );
485 auto label2 = static_cast<Label*>( getChildByTag(kTagBitmapAtlas2) );
486 auto label3 = static_cast<Label*>( getChildByTag(kTagBitmapAtlas3) );
487
488 if( ! setEmpty )
489 {
490 label1->setString("not empty");
491 label2->setString("not empty");
492 label3->setString("hi");
493
494 setEmpty = true;
495 }
496 else
497 {
498 label1->setString("");
499 label2->setString("");
500 label3->setString("");
501
502 setEmpty = false;
503 }
504}
505
506std::string LabelFNTandTTFEmpty::title() const
507{
508 return "New Label + FNT/TTF/CharMap";
509}
510
512{
513 return "Testing empty string.";
514}
515
517{
518 auto s = Director::getInstance()->getWinSize();
519
520 // LabelBMFont
521 auto label1 = Label::createWithBMFont("fonts/konqa32.fnt", "TESTING RETINA DISPLAY");
522 addChild(label1);
523 label1->setPosition(Vec2(s.width/2, s.height/2));
524}
525
526std::string LabelFNTRetina::title() const
527{
528 return "New Label + Bitmap font";
529}
530
531std::string LabelFNTRetina::subtitle() const
532{
533 return "loading arista16 or arista16-hd";
534}
535
537{
538 auto winSize = Director::getInstance()->getWinSize();
539
540 auto layer = LayerColor::create(Color4B(128,128,128,255));
541 addChild(layer, -10);
542
543 auto label1 = Label::createWithBMFont("fonts/futura-48.fnt", "Testing Glyph Designer");
544 // Demo for reloadFontAtlasFNT function, after it been called, all UI widget
545 // use the special font must reset font, because the old one is invalid.
546 FontAtlasCache::reloadFontAtlasFNT("fonts/futura-48.fnt");
547 label1->setBMFontFilePath("fonts/futura-48.fnt");
548 addChild(label1);
549 label1->setPosition(Vec2(winSize.width / 2, winSize.height * 0.4f));
550
551 auto label2 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "Hello World");
552 label2->setPosition(Vec2(winSize.width / 2, winSize.height * 0.6f));
553 addChild(label2);
554}
555
557{
558 return "New Label + Bitmap font";
559}
560
562{
563 return "Testing Glyph Designer";
564}
565
567
568#define LongSentencesExample "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
569#define LineBreaksExample "Lorem ipsum dolor\nsit amet\nconsectetur adipisicing elit\nblah\nblah"
570#define MixedExample "ABC\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt\nDEF"
571
572#define ArrowsMax 0.95
573#define ArrowsMin 0.7
574
575#define LeftAlign 0
576#define CenterAlign 1
577#define RightAlign 2
578
579#define LongSentences 0
580#define LineBreaks 1
581#define Mixed 2
582
583static float alignmentItemPadding = 50;
584static float menuItemPaddingCenter = 50;
585
587{
588 if (!AtlasDemoNew::init())
589 {
590 return false;
591 }
592
593 auto listener = EventListenerTouchAllAtOnce::create();
594 listener->onTouchesBegan = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesBegan, this);
595 listener->onTouchesMoved = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesMoved, this);
596 listener->onTouchesEnded = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesEnded, this);
597
598 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
599 // ask director the the window size
600 auto size = Director::getInstance()->getWinSize();
601
602 // create and initialize a Label
603 this->_label = Label::createWithBMFont("fonts/markerFelt.fnt", "", TextHAlignment::CENTER, size.width/1.5);
604
605 this->_arrowsBar = Sprite::create("Images/arrowsBar.png");
606 this->_arrows = Sprite::create("Images/arrows.png");
607
608 MenuItemFont::setFontSize(20);
609 auto longSentences = MenuItemFont::create("Long Flowing Sentences", CC_CALLBACK_1(LabelFNTMultiLineAlignment::stringChanged, this));
610 auto lineBreaks = MenuItemFont::create("Short Sentences With Intentional Line Breaks", CC_CALLBACK_1(LabelFNTMultiLineAlignment::stringChanged, this));
611 auto mixed = MenuItemFont::create("Long Sentences Mixed With Intentional Line Breaks", CC_CALLBACK_1(LabelFNTMultiLineAlignment::stringChanged, this));
612 auto stringMenu = Menu::create(longSentences, lineBreaks, mixed, nullptr);
613 stringMenu->alignItemsVertically();
614
615 longSentences->setTag(LongSentences);
616 lineBreaks->setTag(LineBreaks);
617 mixed->setTag(Mixed);
618
619 _menuItems.push_back(longSentences);
620 _menuItems.push_back(lineBreaks);
621 _menuItems.push_back(mixed);
622
623 MenuItemFont::setFontSize(30);
624
625 auto left = MenuItemFont::create("Left", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
626 auto center = MenuItemFont::create("Center", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
627 auto right = MenuItemFont::create("Right", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
628
629 auto alignmentMenu = Menu::create(left, center, right, nullptr);
630 alignmentMenu->alignItemsHorizontallyWithPadding(alignmentItemPadding);
631
632 left->setTag(LeftAlign);
633 center->setTag(CenterAlign);
634 right->setTag(RightAlign);
635
636 // position the label on the center of the screen
637 this->_label->setPosition(Vec2(size.width/2, size.height/2));
638
639 this->_arrowsBar->setVisible(false);
640
641 float arrowsWidth = (ArrowsMax - ArrowsMin) * size.width;
642 this->_arrowsBar->setScaleX(arrowsWidth / this->_arrowsBar->getContentSize().width);
643 this->_arrowsBar->setPosition(Vec2(((ArrowsMax + ArrowsMin) / 2) * size.width, this->_label->getPosition().y));
644
645 stringMenu->setPosition(Vec2(size.width/2, size.height - menuItemPaddingCenter));
646 alignmentMenu->setPosition(Vec2(size.width/2, menuItemPaddingCenter+15));
647
648 this->selectSentenceItem(longSentences);
649 this->selectAlignmentItem(center);
650 this->snapArrowsToEdge();
651
652 this->addChild(this->_label);
653 this->addChild(this->_arrowsBar);
654 this->addChild(this->_arrows);
655 this->addChild(stringMenu);
656 this->addChild(alignmentMenu);
657
658 return true;
659}
660
662{
663 return "";
664}
665
667{
668 return "";
669}
670
671void LabelFNTMultiLineAlignment::selectAlignmentItem(cocos2d::MenuItemFont * item)
672{
673 if (this->_lastAlignmentItem && this->_lastAlignmentItem != item)
674 {
675 this->_lastAlignmentItem->setColor(Color3B::WHITE);
676 }
677
678 this->_lastAlignmentItem = item;
679 item->setColor(Color3B::RED);
680
681 switch (item->getTag())
682 {
683 case LeftAlign:
684 this->_label->setAlignment(TextHAlignment::LEFT);
685 break;
686 case CenterAlign:
687 this->_label->setAlignment(TextHAlignment::CENTER);
688 break;
689 case RightAlign:
690 this->_label->setAlignment(TextHAlignment::RIGHT);
691 break;
692
693 default:
694 break;
695 }
696}
697
698void LabelFNTMultiLineAlignment::selectSentenceItem(cocos2d::MenuItemFont* item)
699{
700 if (this->_lastSentenceItem && this->_lastSentenceItem != item)
701 {
702 this->_lastSentenceItem->setColor(Color3B::WHITE);
703 }
704
705 this->_lastSentenceItem = item;
706 item->setColor(Color3B::RED);
707
708 auto str = this->getItemString(item);
709 this->_label->setString(str);
710}
711
712std::string LabelFNTMultiLineAlignment::getItemString(cocos2d::MenuItemFont* item)
713{
714 std::string str;
715
716 switch (item->getTag())
717 {
718 case LongSentences:
720 break;
721 case LineBreaks:
722 str = LineBreaksExample;
723 break;
724 case Mixed:
725 str = MixedExample;
726 break;
727
728 default:
729 break;
730 }
731
732 return str;
733}
734
736{
737 auto item = (MenuItemFont*)sender;
738
739 selectSentenceItem(item);
740
741 this->snapArrowsToEdge();
742}
743
745{
746 auto item = static_cast<MenuItemFont*>(sender);
747
749
750 this->snapArrowsToEdge();
751}
752
753void LabelFNTMultiLineAlignment::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event *event)
754{
755 auto touch = touches[0];
756 auto location = touch->getLocationInView();
757
758 if (this->_arrows->getBoundingBox().containsPoint(location))
759 {
760 _drag = true;
761 this->_arrowsBar->setVisible(true);
762 }
763}
764
765void LabelFNTMultiLineAlignment::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event)
766{
767 _drag = false;
768 this->snapArrowsToEdge();
769
770 this->_arrowsBar->setVisible(false);
771}
772
773void LabelFNTMultiLineAlignment::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
774{
775 if (! _drag)
776 {
777 return;
778 }
779
780 auto touch = touches[0];
781 auto location = touch->getLocationInView();
782
783 auto winSize = Director::getInstance()->getWinSize();
784
785 this->_arrows->setPosition(Vec2(MAX(MIN(location.x, ArrowsMax*winSize.width), ArrowsMin*winSize.width),
786 this->_arrows->getPosition().y));
787
788 float labelWidth = fabs(this->_arrows->getPosition().x - this->_label->getPosition().x) * 2;
789
790 this->_label->setMaxLineWidth(labelWidth);
791}
792
794{
795 this->_arrows->setPosition(Vec2(this->_label->getPosition().x + this->_label->getContentSize().width/2,
796 this->_label->getPosition().y));
797}
798
800
802{
804 {
805 return false;
806 }
807
808 this->_menuItems[0]->setString("French");
809 this->_menuItems[1]->setString("Spanish");
810 this->_menuItems[2]->setString("Ukrainian");
811
812 auto ttfConfig = this->_label->getTTFConfig();
813 ttfConfig.fontSize = 20;
814 ttfConfig.fontFilePath = "fonts/tahoma.ttf";
815 this->_label->setTTFConfig(ttfConfig);
816
817 this->selectSentenceItem(this->_menuItems[0]);
818 this->snapArrowsToEdge();
819
820 return true;
821}
822
824{
825 return "";
826}
827
829{
830 return "";
831}
832
833std::string LabelFNTMultiLineAlignmentUNICODE::getItemString(cocos2d::MenuItemFont* item)
834{
835 std::string str;
836
837 auto strings = FileUtils::getInstance()->getValueMapFromFile("strings/LabelFNTMultiLineAlignmentUNICODE.xml");
838
839 switch (item->getTag())
840 {
841 case LongSentences:
842 str = strings["french"].asString();
843 break;
844 case LineBreaks:
845 str = strings["spanish"].asString();
846 break;
847 case Mixed:
848 str = strings["ukrainian"].asString();
849 break;
850
851 default:
852 break;
853 }
854
855 return str;
856}
857
860{
861 auto strings = FileUtils::getInstance()->getValueMapFromFile("strings/LabelFNTUNICODELanguages.xml");
862 std::string chinese = strings["chinese1"].asString();
863 std::string russian = strings["russian"].asString();
864 std::string spanish = strings["spanish"].asString();
865 std::string japanese = strings["japanese"].asString();
866
867 auto s = Director::getInstance()->getWinSize();
868
869 auto label1 = Label::createWithBMFont("fonts/arial-unicode-26.fnt", spanish, TextHAlignment::CENTER, 200);
870 addChild(label1);
871 label1->setPosition(Vec2(s.width/2, s.height/5*3));
872
873 auto label2 = Label::createWithBMFont("fonts/arial-unicode-26.fnt", chinese);
874 addChild(label2);
875 label2->setPosition(Vec2(s.width/2, s.height/5*2.5));
876
877 auto label3 = Label::createWithBMFont("fonts/arial-26-en-ru.fnt", russian);
878 addChild(label3);
879 label3->setPosition(Vec2(s.width/2, s.height/5*2));
880
881 auto label4 = Label::createWithBMFont("fonts/arial-unicode-26.fnt", japanese);
882 addChild(label4);
883 label4->setPosition(Vec2(s.width/2, s.height/5*1.5));
884}
885
887{
888 return "New Label + Bitmap font";
889}
890
892{
893 return "Testing Unicode.You should see 4 different labels:In Spanish, Chinese, Russian and Japanese";
894}
895
897{
898 auto s = Director::getInstance()->getWinSize();
899
900 auto layer = LayerColor::create(Color4B(128,128,128,255));
901 addChild(layer, -10);
902
903 // LabelBMFont
904 auto label1 = Label::createWithBMFont("fonts/boundsTestFont.fnt", "Testing Glyph Designer", TextHAlignment::CENTER,s.width);
905 addChild(label1);
906 label1->setPosition(Vec2(s.width/2, s.height/2));
907
908 auto drawNode = DrawNode::create();
909 auto labelSize = label1->getContentSize();
910 auto origin = Director::getInstance()->getWinSize();
911
912 origin.width = origin.width / 2 - (labelSize.width / 2);
913 origin.height = origin.height / 2 - (labelSize.height / 2);
914
915 Vec2 vertices[4]=
916 {
917 Vec2(origin.width, origin.height),
918 Vec2(labelSize.width + origin.width, origin.height),
919 Vec2(labelSize.width + origin.width, labelSize.height + origin.height),
920 Vec2(origin.width, labelSize.height + origin.height)
921 };
922 drawNode->drawPoly(vertices, 4, true, Color4F(1.0f, 1.0f, 1.0f, 1.0f));
923 addChild(drawNode);
924}
925
926std::string LabelFNTBounds::title() const
927{
928 return "New Label + Bitmap font";
929}
930
931std::string LabelFNTBounds::subtitle() const
932{
933 return "Testing bounding-box";
934}
935
937{
938 auto size = Director::getInstance()->getWinSize();
939
940 // Long sentence
941 TTFConfig ttfConfig("fonts/arial.ttf", 14);
942 auto label1 = Label::createWithTTF(ttfConfig, LongSentencesExample, TextHAlignment::CENTER,size.width);
943 label1->setPosition( Vec2(size.width/2, size.height/2) );
944 label1->setAnchorPoint(Vec2(0.5f, 1.0f));
945 addChild(label1);
946}
947
949{
950 return "New Label + .TTF";
951}
952
954{
955 return "Testing auto-wrapping";
956}
957
959{
960 auto size = Director::getInstance()->getWinSize();
961
962 TTFConfig ttfConfig("fonts/arial.ttf", 18);
963 // Green
964 auto label1 = Label::createWithTTF(ttfConfig,"Green", TextHAlignment::CENTER, size.width);
965 label1->setPosition(size.width/2, size.height * 0.5f);
966 label1->setTextColor( Color4B::GREEN );
967 addChild(label1);
968
969 // Red
970 auto label2 = Label::createWithTTF(ttfConfig,"Red", TextHAlignment::CENTER, size.width);
971 label2->setPosition(size.width/2, size.height * 0.65f);
972 label2->setTextColor( Color4B::RED );
973 addChild(label2);
974
975 // Blue
976 auto label3 = Label::createWithTTF(ttfConfig,"Blue", TextHAlignment::CENTER, size.width);
977 label3->setPosition(size.width/2, size.height * 0.35f);
978 label3->setTextColor( Color4B::BLUE );
979 addChild(label3);
980}
981
982std::string LabelTTFColor::title() const
983{
984 return "New Label + .TTF";
985}
986
987std::string LabelTTFColor::subtitle() const
988{
989 return "Testing Color";
990}
991
993{
994 auto winSize = Director::getInstance()->getWinSize();
995
996 TTFConfig ttfConfig("fonts/arial.ttf", 23);
997 _label = Label::createWithTTF(ttfConfig, LongSentencesExample, TextHAlignment::CENTER, winSize.width);
998 _label->setPosition(winSize.width / 2, winSize.height / 2);
999 addChild(_label);
1000
1001 auto menu = Menu::create(
1002 MenuItemFont::create("Left", CC_CALLBACK_1(LabelTTFDynamicAlignment::setAlignmentLeft, this)),
1003 MenuItemFont::create("Center", CC_CALLBACK_1(LabelTTFDynamicAlignment::setAlignmentCenter, this)),
1004 MenuItemFont::create("Right", CC_CALLBACK_1(LabelTTFDynamicAlignment::setAlignmentRight, this)),
1005 nullptr);
1006
1007 menu->alignItemsHorizontallyWithPadding(20);
1008 menu->setPosition(winSize.width / 2, winSize.height * 0.25f);
1009 addChild(menu);
1010}
1011
1013{
1014 if (_label)
1015 {
1016 _label->setAlignment(_horizAlign);
1017 }
1018}
1019
1021{
1022 _horizAlign = TextHAlignment::LEFT;
1023 this->updateAlignment();
1024}
1025
1027{
1028 _horizAlign = TextHAlignment::CENTER;
1029 this->updateAlignment();
1030}
1031
1033{
1034 _horizAlign = TextHAlignment::RIGHT;
1035 this->updateAlignment();
1036}
1037
1039{
1040 return "New Label + .TTF";
1041}
1042
1044{
1045 return "Testing text alignment";
1046}
1047
1048//
1049// NewLabelTTF Chinese/Japanese/Korean wrapping test
1050//
1052{
1053 auto size = Director::getInstance()->getWinSize();
1054
1055 auto drawNode = DrawNode::create();
1056 drawNode->setAnchorPoint(Vec2(0, 0));
1057 this->addChild(drawNode);
1058 drawNode->drawSegment(
1059 Vec2(size.width * 0.1f, size.height * 0.8f),
1060 Vec2(size.width * 0.1, 0.0f), 1, Color4F(1.0f, 0.0f, 0.0f, 1.0f));
1061 drawNode->drawSegment(
1062 Vec2(size.width * 0.85f, size.height * 0.8f),
1063 Vec2(size.width * 0.85f, 0.0f), 1, Color4F(1.0f, 0.0f, 0.0f, 1.0f));
1064
1065 TTFConfig ttfConfig("fonts/HKYuanMini.ttf", 25, GlyphCollection::DYNAMIC);
1066 auto label1 = Label::createWithTTF(ttfConfig,
1067 "你好,Cocos2d-x v3的New Label.", TextHAlignment::LEFT, size.width * 0.75f);
1068 if(label1) {
1069 label1->setTextColor(Color4B(128, 255, 255, 255));
1070 label1->setPosition(Vec2(size.width * 0.1f, size.height * 0.6f));
1071 label1->setAnchorPoint(Vec2(0.0f, 0.5f));
1072 this->addChild(label1);
1073 // Demo for unloadFontAtlasTTF function, after it been called, all UI widget
1074 // use the special font must reset font, because the old one is invalid.
1075 FontAtlasCache::unloadFontAtlasTTF("fonts/HKYuanMini.ttf");
1076 label1->setTTFConfig(ttfConfig);
1077 }
1078
1079 auto label2 = Label::createWithTTF(ttfConfig,
1080 "早上好,Cocos2d-x v3的New Label.", TextHAlignment::LEFT, size.width * 0.75f);
1081 if(label2) {
1082 label2->setTextColor(Color4B(255, 128, 255, 255));
1083 label2->setPosition(Vec2(size.width * 0.1f, size.height * 0.4f));
1084 label2->setAnchorPoint(Vec2(0.0f, 0.5f));
1085 this->addChild(label2);
1086 }
1087
1088 auto label3 = Label::createWithTTF(ttfConfig,
1089 "美好的一天啊美好的一天啊美好的一天啊", TextHAlignment::LEFT, size.width * 0.75f);
1090 if(label3) {
1091 label3->setTextColor(Color4B(255, 255, 128, 255));
1092 label3->setPosition(Vec2(size.width * 0.1f, size.height * 0.2f));
1093 label3->setAnchorPoint(Vec2(0.0f, 0.5f));
1094 this->addChild(label3);
1095 }
1096}
1097
1099{
1100 return "New Label + .TTF";
1101}
1102
1104{
1105 return "New Label with CJK + ASCII characters\n"
1106 "Characters should stay in the correct position";
1107}
1108
1109//
1110// NewLabelTTF unicode test
1111//
1113{
1114 auto strings = FileUtils::getInstance()->getValueMapFromFile("strings/LabelFNTUNICODELanguages.xml");
1115 std::string chinese = strings["chinese1"].asString();
1116 auto winSize = Director::getInstance()->getWinSize();
1117
1118 // Spanish
1119 auto label1 = Label::createWithTTF("Buen día, ¿cómo te llamas?", "fonts/arial.ttf", 23);
1120 label1->setPosition(winSize.width / 2, winSize.height * 0.65f);
1121 addChild(label1);
1122
1123 // German
1124 auto label2 = Label::createWithTTF("In welcher Straße haben Sie gelebt?", "fonts/arial.ttf", 23);
1125 label2->setPosition(winSize.width / 2, winSize.height * 0.5f);
1126 addChild(label2);
1127
1128 // Chinese
1129 auto label3 = Label::createWithTTF(chinese, "fonts/HKYuanMini.ttf", 24);
1130 label3->setPosition(winSize.width / 2, winSize.height * 0.35f);
1131 addChild(label3);
1132}
1133
1134std::string LabelTTFUnicodeNew::title() const
1135{
1136 return "New Label + TTF unicode";
1137}
1138
1140{
1141 return "Uses the new Label with TTF. Testing unicode";
1142}
1143
1144//
1145// LabelTTFEmoji emoji test
1146//
1148{
1149 std::string emojiString = FileUtils::getInstance()->getStringFromFile("fonts/emoji.txt");
1150 auto winSize = Director::getInstance()->getWinSize();
1151
1152 auto label = Label::createWithTTF(emojiString, "fonts/NotoEmoji-Regular.ttf", 23);
1153 label->setPosition(winSize.width / 2, winSize.height / 2);
1154 label->setDimensions(winSize.width, winSize.height);
1155 label->setVerticalAlignment(cocos2d::TextVAlignment::CENTER);
1156 label->setHorizontalAlignment(cocos2d::TextHAlignment::CENTER);
1157 addChild(label);
1158}
1159
1160std::string LabelTTFEmoji::title() const
1161{
1162 return "New Label + Emoji";
1163}
1164
1165std::string LabelTTFEmoji::subtitle() const
1166{
1167 return "Uses the new Label with TTF. Testing Emoji";
1168}
1169
1170
1172{
1173 const char *ttfpaths[] = {
1174 "fonts/A Damn Mess.ttf",
1175 "fonts/Abberancy.ttf",
1176 "fonts/Abduction.ttf",
1177 "fonts/American Typewriter.ttf",
1178 "fonts/Paint Boy.ttf",
1179 "fonts/Schwarzwald.ttf",
1180 "fonts/Scissor Cuts.ttf",
1181 };
1182
1183 int fontCount = sizeof(ttfpaths) / sizeof(ttfpaths[0]);
1184 auto size = Director::getInstance()->getWinSize();
1185 TTFConfig ttfConfig(ttfpaths[0],20, GlyphCollection::NEHE);
1186
1187 for (int i = 0; i < fontCount; ++i) {
1188 ttfConfig.fontFilePath = ttfpaths[i];
1189 auto label = Label::createWithTTF(ttfConfig, ttfpaths[i], TextHAlignment::CENTER,0);
1190 if( label ) {
1191 label->setPosition(size.width / 2, ((size.height * 0.6) / fontCount * i) + (size.height / 4));
1192 addChild(label);
1193 } else {
1194 log("ERROR: Cannot load: %s", ttfpaths[i]);
1195 }
1196 }
1197}
1198
1200{
1201 return "New Label + TTF";
1202}
1203
1205{
1206 return "";
1207}
1208
1210{
1211 auto size = Director::getInstance()->getWinSize();
1212 TTFConfig ttfConfig("fonts/arial.ttf", 40, GlyphCollection::DYNAMIC,nullptr,true);
1213
1214 auto label1 = Label::createWithTTF(ttfConfig,"Distance Field",TextHAlignment::CENTER,size.width);
1215 label1->setPosition( Vec2(size.width/2, size.height * 0.6f) );
1216 label1->setTextColor( Color4B::GREEN );
1217 addChild(label1);
1218
1219 auto action = Sequence::create(
1220 DelayTime::create(1.0f),
1221 ScaleTo::create(6.0f,5.0f,5.0f),
1222 ScaleTo::create(6.0f,1.0f,1.0f),
1223 nullptr);
1224 label1->runAction(RepeatForever::create(action));
1225
1226 // Draw the label border
1227 auto& labelContentSize = label1->getContentSize();
1228 auto borderDraw = DrawNode::create();
1229 label1->addChild(borderDraw);
1230 borderDraw->clear();
1231 borderDraw->setLineWidth(1);
1232 Vec2 vertices[4] =
1233 {
1234 Vec2::ZERO,
1235 Vec2(labelContentSize.width, 0.0f),
1236 Vec2(labelContentSize.width, labelContentSize.height),
1237 Vec2(0.0f, labelContentSize.height)
1238 };
1239 borderDraw->drawPoly(vertices, 4, true, Color4F::RED);
1240
1241 auto label2 = Label::createWithTTF(ttfConfig,"Distance Field",TextHAlignment::CENTER,size.width);
1242 label2->setPosition( Vec2(size.width/2, size.height * 0.3f) );
1243 label2->setTextColor( Color4B::RED );
1244 addChild(label2);
1245
1246 // Draw the label border
1247 auto& labelContentSize2 = label2->getContentSize();
1248 auto borderDraw2 = DrawNode::create();
1249 label2->addChild(borderDraw2);
1250 borderDraw2->clear();
1251 borderDraw2->setLineWidth(1);
1252 Vec2 vertices2[4] =
1253 {
1254 Vec2::ZERO,
1255 Vec2(labelContentSize2.width, 0.0f),
1256 Vec2(labelContentSize2.width, labelContentSize2.height),
1257 Vec2(0.0f, labelContentSize2.height)
1258 };
1259 borderDraw2->drawPoly(vertices2, 4, true, Color4F::GREEN);
1260}
1261
1263{
1264 return "New Label + .TTF";
1265}
1266
1268{
1269 return "Testing rendering base on DistanceField";
1270}
1271
1273{
1274 auto size = Director::getInstance()->getWinSize();
1275
1276 auto bg = LayerColor::create(Color4B(200,191,231,255));
1277 this->addChild(bg);
1278
1279 TTFConfig ttfConfig("fonts/arial.ttf", 40, GlyphCollection::DYNAMIC,nullptr,true);
1280
1281 auto label1 = Label::createWithTTF(ttfConfig,"Glow", TextHAlignment::CENTER, size.width);
1282 label1->setPosition( Vec2(size.width/2, size.height*0.7) );
1283 label1->setTextColor( Color4B::GREEN );
1284 label1->enableGlow(Color4B::YELLOW);
1285 addChild(label1);
1286
1287 ttfConfig.outlineSize = 1;
1288 auto label2 = Label::createWithTTF(ttfConfig,"Outline", TextHAlignment::CENTER, size.width);
1289 label2->setPosition( Vec2(size.width/2, size.height*0.6) );
1290 label2->setTextColor( Color4B::RED );
1291 label2->enableOutline(Color4B::BLUE);
1292 addChild(label2);
1293
1294 ttfConfig.outlineSize = 2;
1295 auto label3 = Label::createWithTTF(ttfConfig,"Outline", TextHAlignment::CENTER, size.width);
1296 label3->setPosition( Vec2(size.width/2, size.height*0.48) );
1297 label3->setTextColor( Color4B::RED );
1298 label3->enableOutline(Color4B::BLUE);
1299 addChild(label3);
1300
1301 ttfConfig.outlineSize = 3;
1302 auto label4 = Label::createWithTTF(ttfConfig,"Outline", TextHAlignment::CENTER, size.width);
1303 label4->setPosition( Vec2(size.width/2, size.height*0.36) );
1304 label4->setTextColor( Color4B::RED );
1305 label4->enableOutline(Color4B::BLUE);
1306 addChild(label4);
1307}
1308
1310{
1311 return "New Label + .TTF";
1312}
1313
1315{
1316 return "Testing outline and glow of label";
1317}
1318
1320{
1321
1322}
1323
1325{
1327
1328 auto size = Director::getInstance()->getWinSize();
1329
1330 auto bg = LayerColor::create(Color4B(200,191,231,255));
1331 this->addChild(bg);
1332
1333 auto slider = ui::Slider::create();
1334 slider->setTag(1);
1335 slider->setTouchEnabled(true);
1336 slider->loadBarTexture("cocosui/sliderTrack.png");
1337 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
1338 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
1339 slider->setPosition(Vec2(size.width / 2.0f, size.height * 0.15f + slider->getContentSize().height * 2.0f));
1340 slider->setPercent(52);
1341 slider->addEventListener(CC_CALLBACK_2(LabelShadowTest::sliderEvent, this));
1342 addChild(slider, 999);
1343
1344 auto slider2 = ui::Slider::create();
1345 slider2->setTag(2);
1346 slider2->setTouchEnabled(true);
1347 slider2->loadBarTexture("cocosui/sliderTrack.png");
1348 slider2->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
1349 slider2->loadProgressBarTexture("cocosui/sliderProgress.png");
1350 slider2->setPosition(Vec2(size.width * 0.15f, size.height / 2.0f));
1351 slider2->setRotation(90);
1352 slider2->setPercent(52);
1353 slider2->addEventListener(CC_CALLBACK_2(LabelShadowTest::sliderEvent, this));
1354 addChild(slider2, 999);
1355
1356 float subtitleY = _subtitleLabel->getPosition().y;
1357 float horizontalSliderY = slider->getPosition().y;
1358 float step = (subtitleY - horizontalSliderY) / 4;
1359
1360 TTFConfig ttfConfig("fonts/arial.ttf", 40, GlyphCollection::DYNAMIC,nullptr,true);
1361
1362 shadowLabelTTF = Label::createWithTTF(ttfConfig,"TTF:Shadow");
1363 shadowLabelTTF->setPosition( Vec2(size.width/2, horizontalSliderY + step * (0.5f + 3)) );
1364 shadowLabelTTF->setTextColor( Color4B::RED );
1365 shadowLabelTTF->enableShadow(Color4B::BLACK);
1366 addChild(shadowLabelTTF);
1367
1368 shadowLabelOutline = Label::createWithTTF(ttfConfig,"TTF:Shadow");
1369 shadowLabelOutline->setPosition( Vec2(size.width/2, horizontalSliderY + step * (0.5f + 2)) );
1370 shadowLabelOutline->setTextColor( Color4B::RED );
1371 shadowLabelOutline->enableOutline(Color4B::YELLOW,1);
1372 shadowLabelOutline->enableShadow(Color4B::GREEN);
1373 addChild(shadowLabelOutline);
1374
1375 shadowLabelGrow = Label::createWithTTF(ttfConfig,"TTF:Shadow");
1376 shadowLabelGrow->setPosition( Vec2(size.width/2, horizontalSliderY + step * (0.5f + 1)) );
1377 shadowLabelGrow->setTextColor( Color4B::RED );
1378 shadowLabelGrow->enableGlow(Color4B::YELLOW);
1379 shadowLabelGrow->enableShadow(Color4B::BLUE);
1380 addChild(shadowLabelGrow);
1381
1382 shadowLabelBMFont = Label::createWithBMFont("fonts/bitmapFontTest.fnt", "BMFont:Shadow");
1383 shadowLabelBMFont->setPosition( Vec2(size.width/2, horizontalSliderY + step * 0.5f));
1384 shadowLabelBMFont->setColor( Color3B::RED );
1385 shadowLabelBMFont->enableShadow(Color4B::GREEN);
1386 addChild(shadowLabelBMFont);
1387}
1388
1389void LabelShadowTest::sliderEvent(Ref *pSender, ui::Slider::EventType type)
1390{
1391 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
1392 {
1393 Slider* slider = (Slider*)this->getChildByTag(1);
1394 Slider* slider2 = (Slider*)this->getChildByTag(2);
1395
1396 auto offset = Size(slider->getPercent()-50,50 - slider2->getPercent());
1397 shadowLabelTTF->enableShadow(Color4B::BLACK,offset);
1398 shadowLabelBMFont->enableShadow(Color4B::GREEN,offset);
1399 shadowLabelOutline->enableShadow(Color4B::GREEN,offset);
1400 shadowLabelGrow->enableShadow(Color4B::BLUE,offset);
1401 }
1402}
1403
1404std::string LabelShadowTest::title() const
1405{
1406 return "New Label";
1407}
1408
1409std::string LabelShadowTest::subtitle() const
1410{
1411 return "Testing shadow of label";
1412}
1413
1415{
1416 _time = 0.0f;
1417
1418 auto label1 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist");
1419 addChild(label1, 0, kTagSprite1);
1420 label1->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
1421 label1->setPosition( Vec2(10,100) );
1422 label1->setOpacity( 200 );
1423
1424 auto label2 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist");
1425 addChild(label2, 0, kTagSprite2);
1426 label2->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
1427 label2->setPosition( Vec2(10,200) );
1428 label2->setOpacity( 32 );
1429
1430 schedule(CC_CALLBACK_1(LabelCharMapTest::step, this), "step_key");
1431}
1432
1434{
1435 _time += dt;
1436 char string[12] = {0};
1437 sprintf(string, "%2.2f Test", _time);
1438
1439 auto label1 = (Label*)getChildByTag(kTagSprite1);
1440 label1->setString(string);
1441
1442 auto label2 = (Label*)getChildByTag(kTagSprite2);
1443 sprintf(string, "%d", (int)_time);
1444 label2->setString(string);
1445}
1446
1447std::string LabelCharMapTest::title() const
1448{
1449 return "New Label + char map file";
1450}
1451
1453{
1454 return "Updating label should be fast.";
1455}
1456
1457//------------------------------------------------------------------
1458//
1459// LabelCharMapColorTest
1460//
1461//------------------------------------------------------------------
1463{
1464 auto label1 = Label::createWithCharMap( "fonts/tuffy_bold_italic-charmap.plist");
1465 addChild(label1, 0, kTagSprite1);
1466 label1->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
1467 label1->setPosition( Vec2(10,100) );
1468 label1->setOpacity( 200 );
1469
1470 auto label2 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist");
1471 addChild(label2, 0, kTagSprite2);
1472 label2->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
1473 label2->setPosition( Vec2(10,200) );
1474 label2->setColor( Color3B::RED );
1475
1476 auto fade = FadeOut::create(1.0f);
1477 auto fade_in = fade->reverse();
1478 auto cb = CallFunc::create(CC_CALLBACK_0(LabelCharMapColorTest::actionFinishCallback, this));
1479 auto seq = Sequence::create(fade, fade_in, cb, nullptr);
1480 auto repeat = RepeatForever::create( seq );
1481 label2->runAction( repeat );
1482
1483 _time = 0;
1484
1485 schedule(CC_CALLBACK_1(LabelCharMapColorTest::step, this), "step_key");
1486}
1487
1489{
1490 CCLOG("Action finished");
1491}
1492
1494{
1495 _time += dt;
1496 char string[12] = {0};
1497 sprintf(string, "%2.2f Test", _time);
1498 auto label1 = (Label*)getChildByTag(kTagSprite1);
1499 label1->setString(string);
1500
1501 auto label2 = (Label*)getChildByTag(kTagSprite2);
1502 sprintf(string, "%d", (int)_time);
1503 label2->setString( string );
1504}
1505
1507{
1508 return "New Label + CharMap";
1509}
1510
1512{
1513 return "Opacity + Color should work at the same time";
1514}
1515
1517{
1518 auto size = Director::getInstance()->getWinSize();
1519
1520 TTFConfig ttfConfig("fonts/arial.ttf", 40, GlyphCollection::DYNAMIC,nullptr,true);
1521
1522 auto label1 = Label::createWithTTF(ttfConfig,"Test崩溃123", TextHAlignment::CENTER, size.width);
1523 label1->setPosition( Vec2(size.width/2, size.height/2) );
1524 addChild(label1);
1525}
1526
1527std::string LabelCrashTest::title() const
1528{
1529 return "New Label Crash Test";
1530}
1531
1532std::string LabelCrashTest::subtitle() const
1533{
1534 return "Not crash when use character that is not contained in font.";
1535}
1536
1538{
1539 auto s = Director::getInstance()->getWinSize();
1540 float delta = s.height/4;
1541
1542 auto label1 = Label::createWithSystemFont("Cocos2d-x Label Test", "arial", 24);
1543 addChild(label1, 0, kTagBitmapAtlas1);
1544 label1->setPosition(Vec2(s.width/2, delta * 2));
1545 label1->setColor(Color3B::RED);
1546
1547 TTFConfig ttfConfig("fonts/arial.ttf", 24);
1548 auto label2 = Label::createWithTTF(ttfConfig, "Cocos2d-x Label Test");
1549 addChild(label2, 0, kTagBitmapAtlas2);
1550 label2->setPosition(Vec2(s.width/2, delta * 2));
1551
1552 auto drawNode = DrawNode::create();
1553 auto labelSize = label1->getContentSize();
1554 auto origin = Director::getInstance()->getWinSize();
1555
1556 origin.width = origin.width / 2 - (labelSize.width / 2);
1557 origin.height = origin.height / 2 - (labelSize.height / 2);
1558
1559 Vec2 vertices[4]=
1560 {
1561 Vec2(origin.width, origin.height),
1562 Vec2(labelSize.width + origin.width, origin.height),
1563 Vec2(labelSize.width + origin.width, labelSize.height + origin.height),
1564 Vec2(origin.width, labelSize.height + origin.height)
1565 };
1566 drawNode->drawPoly(vertices, 4, true, Color4F(1.0f, 0.0f, 0.0f, 1.0f));
1567
1568 labelSize = label2->getContentSize();
1569 origin = Director::getInstance()->getWinSize();
1570
1571 origin.width = origin.width / 2 - (labelSize.width / 2);
1572 origin.height = origin.height / 2 - (labelSize.height / 2);
1573
1574 Vec2 vertices2[4]=
1575 {
1576 Vec2(origin.width, origin.height),
1577 Vec2(labelSize.width + origin.width, origin.height),
1578 Vec2(labelSize.width + origin.width, labelSize.height + origin.height),
1579 Vec2(origin.width, labelSize.height + origin.height)
1580 };
1581 drawNode->drawPoly(vertices2, 4, true, Color4F(1.0f, 1.0f, 1.0f, 1.0f));
1582
1583 addChild(drawNode);
1584}
1585
1586std::string LabelTTFOldNew::title() const
1587{
1588 return "New / Old TTF";
1589}
1590
1591std::string LabelTTFOldNew::subtitle() const
1592{
1593 return "Comparison between old(red) and new(white) TTF label";
1594}
1595
1597{
1598 auto size = Director::getInstance()->getWinSize();
1599
1600 auto label1 = Label::create();
1601 label1->setString("Default Font");
1602 label1->setPosition( Vec2(size.width/2, size.height * 0.7) );
1603 addChild(label1);
1604
1605 auto label3 = Label::createWithSystemFont("Marker Felt","Marker Felt",32);
1606 label3->setPosition( Vec2(size.width/2, size.height * 0.5) );
1607 addChild(label3);
1608}
1609
1610std::string LabelFontNameTest::title() const
1611{
1612 return "New Label Test";
1613}
1614
1616{
1617 return "create label by font name,compatible with old labelTTF";
1618}
1619
1621{
1622 auto blockSize = Size(200, 160);
1623 auto s = Director::getInstance()->getWinSize();
1624
1625 auto pos = Vec2((s.width - blockSize.width) / 2, (s.height - blockSize.height) / 2);
1626 auto colorLayer = LayerColor::create(Color4B(100, 100, 100, 255), blockSize.width, blockSize.height);
1627 colorLayer->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
1628 colorLayer->setPosition(pos);
1629
1630 this->addChild(colorLayer);
1631
1632 MenuItemFont::setFontSize(30);
1633 auto menu = Menu::create(
1634 MenuItemFont::create("Left", CC_CALLBACK_1(LabelAlignmentTest::setAlignmentLeft, this)),
1635 MenuItemFont::create("Center", CC_CALLBACK_1(LabelAlignmentTest::setAlignmentCenter, this)),
1636 MenuItemFont::create("Right", CC_CALLBACK_1(LabelAlignmentTest::setAlignmentRight, this)),
1637 nullptr);
1638 menu->alignItemsVerticallyWithPadding(4);
1639 menu->setPosition(Vec2(50.0f, s.height / 2 - 20));
1640 this->addChild(menu);
1641
1642 menu = Menu::create(
1643 MenuItemFont::create("Top", CC_CALLBACK_1(LabelAlignmentTest::setAlignmentTop, this)),
1644 MenuItemFont::create("Middle", CC_CALLBACK_1(LabelAlignmentTest::setAlignmentMiddle, this)),
1645 MenuItemFont::create("Bottom", CC_CALLBACK_1(LabelAlignmentTest::setAlignmentBottom, this)),
1646 nullptr);
1647 menu->alignItemsVerticallyWithPadding(4);
1648 menu->setPosition(Vec2(s.width - 50, s.height / 2 - 20));
1649 this->addChild(menu);
1650
1651 TTFConfig ttfConfig("fonts/arial.ttf", 50);
1652 _label = Label::createWithTTF(ttfConfig, "abc efg hijk lmn opq rst uvw xyz");
1653 _label->setDimensions(200, 160);
1654 _label->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
1655 _label->setPosition(pos);
1656 addChild(_label);
1657}
1658
1660{
1661 _label->setHorizontalAlignment(TextHAlignment::LEFT);
1662}
1663
1665{
1666 _label->setHorizontalAlignment(TextHAlignment::CENTER);
1667}
1668
1670{
1671 _label->setHorizontalAlignment(TextHAlignment::RIGHT);
1672}
1673
1675{
1676 _label->setVerticalAlignment(TextVAlignment::TOP);
1677}
1678
1680{
1681 _label->setVerticalAlignment(TextVAlignment::CENTER);
1682}
1683
1685{
1686 _label->setVerticalAlignment(TextVAlignment::BOTTOM);
1687}
1688
1689std::string LabelAlignmentTest::title() const
1690{
1691 return "Testing New Label";
1692}
1693
1695{
1696 return "Test text alignment";
1697}
1698
1700{
1701 auto size = Director::getInstance()->getWinSize();
1702
1703 auto label = Label::createWithBMFont( "fonts/bitmapFontTest3.fnt", "123\n456");
1704 label->setPosition(Vec2(size.width /2.0f, size.height / 2.0f));
1705 label->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
1706 addChild(label);
1707
1708 int len = label->getStringLength();
1709 for (int i = 0; i < len; ++i)
1710 {
1711 auto sprite = label->getLetter(i);
1712 if (sprite != nullptr)
1713 {
1714 sprite->setFlippedY(true);
1715 }
1716 }
1717}
1718
1719std::string LabelIssue4428Test::title() const
1720{
1721 return "New Label Bugs Test";
1722}
1723
1725{
1726 return "Reorder issue #4428.The label should be flipped vertically.";
1727}
1728
1730{
1731 auto label = Label::createWithTTF("Smaller font test", "fonts/arial.ttf",5);
1732 label->setPosition(VisibleRect::center());
1733 label->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
1734 addChild(label);
1735}
1736
1737std::string LabelIssue4999Test::title() const
1738{
1739 return "New Label Bugs Test";
1740}
1741
1743{
1744 return "Reorder issue #4999.The label should be display cleanly.";
1745}
1746
1748{
1749 auto size = Director::getInstance()->getWinSize();
1750
1751 auto bg = LayerColor::create(Color4B(200,191,231,255));
1752 this->addChild(bg);
1753
1754 TTFConfig ttfConfig("fonts/arial.ttf", 25, GlyphCollection::DYNAMIC,nullptr,false);
1755
1756 label = Label::createWithTTF(ttfConfig,"Test\nLine\nHeight");
1757 label->setPosition( Vec2(size.width/2, size.height*0.5f) );
1758 label->setTextColor( Color4B::RED );
1759 addChild(label);
1760
1761 auto slider = ui::Slider::create();
1762 slider->setTouchEnabled(true);
1763 slider->loadBarTexture("cocosui/sliderTrack.png");
1764 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
1765 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
1766 slider->setPosition(Vec2(size.width / 2.0f, size.height * 0.15f + slider->getContentSize().height * 2.0f));
1767 slider->setPercent(label->getLineHeight());
1768 slider->addEventListener(CC_CALLBACK_2(LabelLineHeightTest::sliderEvent, this));
1769 addChild(slider);
1770}
1771
1772void LabelLineHeightTest::sliderEvent(Ref *sender, ui::Slider::EventType type)
1773{
1774 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
1775 {
1776 Slider* slider = (Slider*)sender;
1777 label->setLineHeight(slider->getPercent());
1778 }
1779}
1780
1782{
1783 return "New Label";
1784}
1785
1787{
1788 return "Testing line height of label";
1789}
1790
1792{
1793 auto size = Director::getInstance()->getWinSize();
1794
1795 auto bg = LayerColor::create(Color4B(200,191,231,255));
1796 this->addChild(bg);
1797
1798 TTFConfig ttfConfig("fonts/arial.ttf", 40, GlyphCollection::DYNAMIC,nullptr,false);
1799
1800 label = Label::createWithTTF(ttfConfig,"Test additional kerning");
1801 label->setPosition(size.width/2, size.height * 0.5f);
1802 label->setTextColor( Color4B::RED );
1803 addChild(label);
1804
1805 auto slider = ui::Slider::create();
1806 slider->setTouchEnabled(true);
1807 slider->loadBarTexture("cocosui/sliderTrack.png");
1808 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
1809 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
1810 slider->setPosition(Vec2(size.width / 2.0f, size.height * 0.15f + slider->getContentSize().height * 2.0f));
1811 slider->setPercent(0);
1812 slider->addEventListener(CC_CALLBACK_2(LabelAdditionalKerningTest::sliderEvent, this));
1813 addChild(slider);
1814}
1815
1816void LabelAdditionalKerningTest::sliderEvent(Ref *sender, ui::Slider::EventType type)
1817{
1818 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
1819 {
1820 Slider* slider = (Slider*)sender;
1821 label->setAdditionalKerning(slider->getPercent());
1822 }
1823}
1824
1826{
1827 return "New Label";
1828}
1829
1831{
1832 return "Testing additional kerning of label";
1833}
1834
1836{
1837 auto label = Label::createWithBMFont("fonts/bitmapFontChinese.fnt", "中国中国中国中国中国");
1838 label->setDimensions(5,100);
1839 label->setPosition(VisibleRect::center());
1840 addChild(label);
1841}
1842
1843std::string LabelIssue8492Test::title() const
1844{
1845 return "Reorder issue #8492";
1846}
1847
1849{
1850 return "Work fine when dimensions are not enough to fit one character";
1851}
1852
1854{
1855 auto label = Label::createWithTTF("Multi-line text\nwith\noutline feature", "fonts/arial.ttf", 24);
1856 label->enableOutline(Color4B::ORANGE,1);
1857 label->setPosition(VisibleRect::center());
1858 addChild(label);
1859}
1860
1862{
1863 return "Reorder issue #9095";
1864}
1865
1867{
1868 return "end in string 'outline feature'";
1869}
1870
1871
1873{
1874 Size s = Director::getInstance()->getWinSize();
1875 auto parent = Node::create();
1876 parent->setPosition(s.width/2, s.height/2);
1877 parent->setVisible(false);
1878 this->addChild(parent);
1879
1880 auto label = Label::createWithTTF("Crashed!!!", "fonts/HKYuanMini.ttf", 24);
1881 label->setPosition(VisibleRect::center());
1882 parent->addChild(label);
1883}
1884
1885std::string LabelIssue9255Test::title() const
1886{
1887 return "Test for Issue #9255";
1888}
1889
1891{
1892 return "switch to desktop and switch back. Crashed!!!";
1893}
1894
1896{
1897 auto label = Label::createWithSystemFont("Hello World!", "fonts/arial.ttf", 24, Size(30.0f,100.0f));
1898 label->setPosition(VisibleRect::center());
1899 addChild(label);
1900}
1901
1903{
1904 return "New Label + System font";
1905}
1906
1908{
1909 return "Testing create Label with small dimensions.Program should not dead loop";
1910}
1911
1913{
1914 auto center = VisibleRect::center();
1915
1916 auto labelA = Label::createWithSystemFont("create label with system font", "fonts/arial.ttf", 24);
1917 auto size = labelA->getContentSize();
1918 labelA->setDimensions(size.width, size.height);
1919 labelA->setPosition(center.x, center.y + 50);
1920 addChild(labelA);
1921
1922 auto labelB = Label::createWithTTF("create label with TTF", "fonts/arial.ttf", 24);
1923 size = labelB->getContentSize();
1924 labelB->setDimensions(size.width, size.height);
1925 labelB->setPosition(center.x, center.y - 50);
1926 addChild(labelB);
1927}
1928
1930{
1931 return "Test for Issue #10089";
1932}
1933
1935{
1936 return "Should be able to see two single-line text";
1937}
1938
1940{
1941 auto size = Director::getInstance()->getWinSize();
1942
1943 auto label1 = Label::createWithSystemFont("Color4B::Red", "fonts/arial.ttf", 20);
1944 label1->setPosition(Vec2(size.width / 2, size.height * 0.3f));
1945 label1->setTextColor(Color4B::RED);
1946 addChild(label1);
1947
1948 auto label2 = Label::createWithSystemFont("Color4B::Green", "fonts/arial.ttf", 20);
1949 label2->setPosition(Vec2(size.width / 2, size.height * 0.4f));
1950 label2->setTextColor(Color4B::GREEN);
1951 addChild(label2);
1952
1953 auto label3 = Label::createWithSystemFont("Color4B::Blue", "fonts/arial.ttf", 20);
1954 label3->setPosition(Vec2(size.width / 2, size.height * 0.5f));
1955 label3->setTextColor(Color4B::BLUE);
1956 addChild(label3);
1957
1958 auto label4 = Label::createWithSystemFont("Color4B(0, 0, 255, 100)", "fonts/arial.ttf", 20);
1959 label4->setPosition(Vec2(size.width / 2, size.height * 0.6f));
1960 label4->setTextColor(Color4B(0, 0, 255, 100));
1961 addChild(label4);
1962}
1963
1965{
1966 return "New Label + system font";
1967}
1968
1970{
1971 return "Testing text color of system font";
1972}
1973
1975{
1976 auto center = VisibleRect::center();
1977
1978 auto label = Label::createWithTTF("create label with TTF", "fonts/arial.ttf", 24);
1979 label->getLetter(5);
1980 label->setString("Hi");
1981 label->setPosition(center.x, center.y);
1982 addChild(label);
1983}
1984
1986{
1987 return "Test for Issue #10773";
1988}
1989
1991{
1992 return "Should not crash!";
1993}
1994
1996{
1997 auto center = VisibleRect::center();
1998
1999 auto label = Label::createWithTTF("abcdefg", "fonts/arial.ttf", 24);
2000 for (int index = 0; index < label->getStringLength(); ++index)
2001 {
2002 label->getLetter(index);
2003 }
2004
2005 this->runAction(Sequence::create(DelayTime::create(2.0f), CallFunc::create([label](){
2006 label->setString("Hello World!");
2007 }), nullptr));
2008
2009 label->setPosition(center.x, center.y);
2010 addChild(label);
2011}
2012
2014{
2015 return "Test for Issue #11576";
2016}
2017
2019{
2020 return "You should see another string displayed correctly after 2 seconds.";
2021}
2022
2024{
2025 auto center = VisibleRect::center();
2026
2027 auto label = Label::createWithTTF("中国", "fonts/HKYuanMini.ttf", 150);
2028 label->enableOutline(Color4B::RED, 2);
2029 label->setPosition(center.x, center.y);
2030 addChild(label);
2031}
2032
2034{
2035 return "Test for Issue #11699";
2036}
2037
2039{
2040 return "Outline should match with the characters exactly.";
2041}
2042
2044{
2045 auto center = VisibleRect::center();
2046
2047 auto label = Label::createWithTTF("abcdefghijklmn", "fonts/arial.ttf", 30);
2048 label->setWidth(70);
2049 label->setLineBreakWithoutSpace(true);
2050 label->setPosition(center.x, center.y);
2051 addChild(label);
2052
2053 auto labelSize = label->getContentSize();
2054 auto winSize = Director::getInstance()->getWinSize();
2055 Vec2 labelOrigin;
2056 labelOrigin.x = winSize.width / 2 - (labelSize.width / 2);
2057 labelOrigin.y = winSize.height / 2 - (labelSize.height / 2);
2058 Vec2 vertices[4] =
2059 {
2060 Vec2(labelOrigin.x, labelOrigin.y),
2061 Vec2(labelOrigin.x + labelSize.width, labelOrigin.y),
2062 Vec2(labelOrigin.x + labelSize.width, labelOrigin.y + labelSize.height),
2063 Vec2(labelOrigin.x, labelOrigin.y + labelSize.height)
2064 };
2065
2066 auto drawNode = DrawNode::create();
2067 drawNode->drawPoly(vertices, 4, true, Color4F::WHITE);
2068 addChild(drawNode);
2069}
2070
2072{
2073 return "Test for Issue #12409";
2074}
2075
2077{
2078 return "Testing auto-wrapping without space.";
2079}
2080
2082{
2083 auto center = VisibleRect::center();
2084
2085 auto label = Label::createWithTTF("Label with child node:", "fonts/arial.ttf", 24);
2086 label->setPosition(center.x, center.y);
2087 addChild(label);
2088
2089 auto jump = JumpBy::create(1.0f, Vec2::ZERO, 60, 1);
2090 auto jump_4ever = RepeatForever::create(jump);
2091 label->runAction(jump_4ever);
2092
2093 auto spite = Sprite::create("Images/SpookyPeas.png");
2094 spite->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
2095 spite->setPosition(label->getContentSize().width, label->getContentSize().height/2);
2096 label->addChild(spite);
2097}
2098
2099std::string LabelAddChildTest::title() const
2100{
2101 return "Label support add child nodes";
2102}
2103
2105{
2106 auto center = VisibleRect::center();
2107
2108 auto label = Label::createWithTTF("Hello", "fonts/xingkai-incomplete.ttf", 30);
2109 label->setPosition(center.x, center.y);
2110 addChild(label);
2111}
2112
2114{
2115 return "Test for Issue #12775";
2116}
2117
2119{
2120 return "Should not crash if the font not contain a Unicode charmap.";
2121}
2122
2124{
2125 auto center = VisibleRect::center();
2126
2127 auto label = Label::createWithTTF("Hello World", "fonts/arial.ttf", 24);
2128 label->setPosition(center.x, center.y);
2129 addChild(label);
2130
2131 label->getLetter(0)->setColor(Color3B::RED);
2132 label->getLetter(1)->setColor(Color3B::GREEN);
2133 label->getLetter(2)->setColor(Color3B::BLUE);
2134 auto action = RepeatForever::create(Sequence::create(
2135 FadeOut::create(2), FadeIn::create(2),nullptr));
2136 label->runAction(action);
2137}
2138
2140{
2141 return "Test for Issue #11585";
2142}
2143
2145{
2146 return "The color of letter should not be overridden by fade action.";
2147}
2148
2150{
2151 auto center = VisibleRect::center();
2152
2153 auto label = Label::createWithTTF("Glow MenuItemLabel", "fonts/arial.ttf", 30);
2154 label->setTextColor(Color4B::RED);
2155 label->enableGlow(Color4B::YELLOW);
2156 auto menuItem1 = MenuItemLabel::create(label, [](Ref*){});
2157 menuItem1->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
2158 menuItem1->setPosition(center.x - label->getContentSize().width/2, center.y);
2159
2160 auto menu = Menu::create(menuItem1, NULL);
2161 menu->setPosition(Vec2::ZERO);
2162 this->addChild(menu);
2163}
2164
2166{
2167 return "Test for Issue #10688";
2168}
2169
2171{
2172 return "The MenuItemLabel should be displayed in the middle of the screen.";
2173}
2174
2176{
2177 auto center = VisibleRect::center();
2178
2179 auto label = Label::createWithTTF("asdfghjklzxcvbnmqwertyuiop", "fonts/arial.ttf", 150);
2180 label->setPosition(center);
2181 addChild(label);
2182
2183 label->getContentSize();
2184 label->setString("A");
2185 this->scheduleOnce([](float dt){
2186 FontAtlasCache::purgeCachedData();
2187 }, 0.15f, "FontAtlasCache::purgeCachedData");
2188}
2189
2191{
2192 return "Test for Issue #13202";
2193}
2194
2196{
2197 return "FontAtlasCache::purgeCachedData should not cause crash.";
2198}
2199
2201{
2202 auto center = VisibleRect::center();
2203
2204 auto label = Label::createWithTTF("Spaces should not be lost", "fonts/Fingerpop.ttf", 20);
2205 label->setPosition(center);
2206 addChild(label);
2207}
2208
2209std::string LabelIssue9500Test::title() const
2210{
2211 return "Test for Issue #9500";
2212}
2213
2215{
2216 return "Spaces should not be lost if label created with Fingerpop.ttf";
2217}
2218
2220{
2221 auto minusSprite = Sprite::create("extensions/stepper-minus.png");
2222 auto plusSprite = Sprite::create("extensions/stepper-plus.png");
2223
2224 return ControlStepper::create(minusSprite, plusSprite);
2225}
2226
2228{
2229 auto size = Director::getInstance()->getVisibleSize();
2230
2231 this->initTestLabel(size);
2232
2233 this->initFontSizeChange(size);
2234 this->initToggleLabelTypeOption(size);
2235
2236 this->initWrapOption(size);
2237
2238 this->initAlignmentOption(size);
2239
2240 this->initDrawNode(size);
2241
2242 this->initSliders(size);
2243}
2244
2245void LabelLayoutBaseTest::initFontSizeChange(const cocos2d::Size& size)
2246{
2247 auto fontSizeLabel = Label::createWithSystemFont("font size:20", "Arial", 10);
2248 fontSizeLabel->setName("fontSize");
2249
2250 ControlStepper *stepper = this->makeControlStepper();
2251 stepper->setPosition(size.width * 0.5 - stepper->getContentSize().width / 2,
2252 size.height * 0.8);
2253 stepper->setValue(20);
2254 stepper->addTargetWithActionForControlEvents(this,
2255 cccontrol_selector(LabelLayoutBaseTest::valueChanged),
2256 Control::EventType::VALUE_CHANGED);
2257 this->addChild(stepper);
2258 stepper->setName("stepper");
2259 stepper->setScale(0.5);
2260
2261 fontSizeLabel->setPosition(stepper->getPosition() -
2262 Vec2(stepper->getContentSize().width/2 + fontSizeLabel->getContentSize().width/2,0.0f));
2263 this->addChild(fontSizeLabel);
2264}
2265
2266void LabelLayoutBaseTest::initWrapOption(const cocos2d::Size& size)
2267{
2268 auto label = Label::createWithSystemFont("Enable Wrap:", "Arial", 10);
2269 label->setColor(Color3B::WHITE);
2270 label->setPosition(Vec2(size.width * 0.8f - 100, size.height * 0.8f));
2271 this->addChild(label);
2272
2273 CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png",
2274 "cocosui/check_box_normal_press.png",
2275 "cocosui/check_box_active.png",
2276 "cocosui/check_box_normal_disable.png",
2277 "cocosui/check_box_active_disable.png");
2278 checkBox->setPosition(Vec2(size.width * 0.8f - 55, size.height * 0.8f));
2279 checkBox->setScale(0.5);
2280 checkBox->setSelected(true);
2281 checkBox->setName("toggleWrap");
2282
2283 checkBox->addEventListener([=](Ref* ref, CheckBox::EventType event){
2284 if (event == CheckBox::EventType::SELECTED) {
2285 _label->enableWrap(true);
2286 }else{
2287 _label->enableWrap(false);
2288 }
2289 this->updateDrawNodeSize(_label->getContentSize());
2290 });
2291 this->addChild(checkBox);
2292}
2293
2295{
2296 auto label = Label::createWithSystemFont("Toggle Label Type:", "Arial", 10);
2297 label->setColor(Color3B::WHITE);
2298 label->setPosition(Vec2(size.width * 0.8f + 15, size.height * 0.8f));
2299 this->addChild(label);
2300
2301 CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png",
2302 "cocosui/check_box_normal_press.png",
2303 "cocosui/check_box_active.png",
2304 "cocosui/check_box_normal_disable.png",
2305 "cocosui/check_box_active_disable.png");
2306 checkBox->setPosition(Vec2(size.width * 0.8f + 70, size.height * 0.8f));
2307 checkBox->setScale(0.5);
2308 checkBox->setName("toggleType");
2309 checkBox->setSelected(true);
2310
2311 auto stepper = (ControlStepper*)this->getChildByName("stepper");
2312
2313 checkBox->addEventListener([=](Ref* ref, CheckBox::EventType event){
2314 float fontSize = stepper->getValue();
2315
2316 if (event == CheckBox::EventType::SELECTED) {
2317 _labelType = 0;
2318 auto ttfConfig = _label->getTTFConfig();
2319 ttfConfig.fontSize = fontSize;
2320 _label->setTTFConfig(ttfConfig);
2321 }else{
2322 _labelType = 1;
2323 _label->setBMFontFilePath("fonts/enligsh-chinese.fnt");
2324 _label->setBMFontSize(fontSize);
2325 }
2326 });
2327 this->addChild(checkBox);
2328
2329}
2330
2331void LabelLayoutBaseTest::initAlignmentOption(const cocos2d::Size& size)
2332{
2333 //add text alignment settings
2334 MenuItemFont::setFontSize(30);
2335 auto menu = Menu::create(
2336 MenuItemFont::create("Left", CC_CALLBACK_1(LabelLayoutBaseTest::setAlignmentLeft, this)),
2337 MenuItemFont::create("Center", CC_CALLBACK_1(LabelLayoutBaseTest::setAlignmentCenter, this)),
2338 MenuItemFont::create("Right", CC_CALLBACK_1(LabelLayoutBaseTest::setAlignmentRight, this)),
2339 nullptr);
2340 menu->alignItemsVerticallyWithPadding(4);
2341 menu->setPosition(Vec2(50.0f, size.height / 2 - 20));
2342 this->addChild(menu);
2343
2344 menu = Menu::create(
2345 MenuItemFont::create("Top", CC_CALLBACK_1(LabelLayoutBaseTest::setAlignmentTop, this)),
2346 MenuItemFont::create("Middle", CC_CALLBACK_1(LabelLayoutBaseTest::setAlignmentMiddle, this)),
2347 MenuItemFont::create("Bottom", CC_CALLBACK_1(LabelLayoutBaseTest::setAlignmentBottom, this)),
2348 nullptr);
2349 menu->alignItemsVerticallyWithPadding(4);
2350 menu->setPosition(Vec2(size.width - 50, size.height / 2 - 20));
2351 this->addChild(menu);
2352}
2353
2354void LabelLayoutBaseTest::initSliders(const cocos2d::Size& size)
2355{
2356 auto slider = ui::Slider::create();
2357 slider->setTag(1);
2358 slider->setTouchEnabled(true);
2359 slider->loadBarTexture("cocosui/sliderTrack.png");
2360 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
2361 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
2362 slider->setPosition(Vec2(size.width / 2.0f, size.height * 0.15f + slider->getContentSize().height * 2.0f - 5));
2363 slider->setPercent(52);
2364 addChild(slider);
2365
2366 auto slider2 = ui::Slider::create();
2367 slider2->setTag(2);
2368 slider2->setTouchEnabled(true);
2369 slider2->loadBarTexture("cocosui/sliderTrack.png");
2370 slider2->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
2371 slider2->loadProgressBarTexture("cocosui/sliderProgress.png");
2372 slider2->setPosition(Vec2(size.width * 0.2f, size.height / 2.0f));
2373 slider2->setRotation(90);
2374 slider2->setPercent(52);
2375 addChild(slider2);
2376 auto winSize = Director::getInstance()->getVisibleSize();
2377
2378 slider->addEventListener([=](Ref* ref, Slider::EventType event){
2379 float percent = slider->getPercent();
2380 auto labelSize = _label->getContentSize();
2381 auto drawNodeSize = Size(percent / 100.0 * winSize.width, labelSize.height);
2382 if(drawNodeSize.width <=0){
2383 drawNodeSize.width = 0.1f;
2384 }
2385 _label->setDimensions(drawNodeSize.width, drawNodeSize.height);
2386 this->updateDrawNodeSize(drawNodeSize);
2387 });
2388
2389 slider2->addEventListener([=](Ref* ref, Slider::EventType event){
2390 float percent = slider2->getPercent();
2391 auto labelSize = _label->getContentSize();
2392 auto drawNodeSize = Size( labelSize.width, percent / 100.0 * winSize.height);
2393 if(drawNodeSize.height <= 0){
2394 drawNodeSize.height = 0.1f;
2395 }
2396 _label->setDimensions(drawNodeSize.width, drawNodeSize.height);
2397 this->updateDrawNodeSize(drawNodeSize);
2398 });
2399}
2400
2401void LabelLayoutBaseTest::initTestLabel(const cocos2d::Size& size)
2402{
2403 auto center = VisibleRect::center();
2404 _label = Label::createWithTTF("五六七八This is a very long sentence一二三四.", "fonts/HKYuanMini.ttf", 20);
2405 _label->setDimensions(size.width/2, size.height/2);
2406 _label->setPosition(center);
2407 _label->setName("Label");
2408 _label->setString("五六七八This is a very long sentence一二三.");
2409 addChild(_label);
2410 _labelType = 0;
2411}
2412
2413void LabelLayoutBaseTest::initDrawNode(const cocos2d::Size& size)
2414{
2415 _drawNode = DrawNode::create();
2416
2417 _drawNode->setTag(3);
2418 addChild(_drawNode);
2419 this->updateDrawNodeSize(_label->getContentSize());
2420}
2421
2422
2424{
2425 _label->setHorizontalAlignment(TextHAlignment::LEFT);
2426}
2427
2429{
2430 _label->setHorizontalAlignment(TextHAlignment::CENTER);
2431}
2432
2434{
2435 _label->setHorizontalAlignment(TextHAlignment::RIGHT);
2436}
2437
2439{
2440 _label->setVerticalAlignment(TextVAlignment::TOP);
2441}
2442
2444{
2445 _label->setVerticalAlignment(TextVAlignment::CENTER);
2446}
2447
2449{
2450 _label->setVerticalAlignment(TextVAlignment::BOTTOM);
2451}
2452
2453
2454void LabelLayoutBaseTest::valueChanged(cocos2d::Ref *sender, cocos2d::extension::Control::EventType controlEvent)
2455{
2456 ControlStepper* pControl = (ControlStepper*)sender;
2457 // Change value of label.
2458 auto fontSizeLabel = (Label*)this->getChildByName("fontSize");
2459 float fontSize = (float)pControl->getValue();
2460 fontSizeLabel->setString(StringUtils::format("font size:%d", (int)fontSize));
2461
2462 if (_labelType == 0) {
2463 auto ttfConfig = _label->getTTFConfig();
2464 ttfConfig.fontSize = fontSize;
2465 _label->setTTFConfig(ttfConfig);
2466 }else if(_labelType == 1){
2467 _label->setBMFontSize(fontSize);
2468 }else if (_labelType == 2) {
2469 _label->setSystemFontSize(fontSize);
2470 }
2471 this->updateDrawNodeSize(_label->getContentSize());
2472
2473 //FIXME::When calling getLetter, the label Overflow feature will be invalid.
2474// auto letterSprite = _label->getLetter(1);
2475// auto moveBy = ScaleBy::create(1.0,2.0);
2476// letterSprite->stopAllActions();
2477// letterSprite->runAction(Sequence::create(moveBy, moveBy->clone()->reverse(), nullptr ));
2478//
2479// CCLOG("label line height = %f", _label->getLineHeight());
2480}
2481
2482void LabelLayoutBaseTest::updateDrawNodeSize(const cocos2d::Size &drawNodeSize)
2483{
2484 auto origin = Director::getInstance()->getWinSize();
2485 auto labelSize = _label->getContentSize();
2486
2487 origin.width = origin.width / 2 - (labelSize.width / 2);
2488 origin.height = origin.height / 2 - (labelSize.height / 2);
2489
2490 Vec2 vertices[4]=
2491 {
2492 Vec2(origin.width, origin.height),
2493 Vec2(drawNodeSize.width + origin.width, origin.height),
2494 Vec2(drawNodeSize.width + origin.width, drawNodeSize.height + origin.height),
2495 Vec2(origin.width, drawNodeSize.height + origin.height)
2496 };
2497 _drawNode->clear();
2498 _drawNode->drawLine(vertices[0], vertices[1], Color4F(1.0f, 1.0f, 1.0f, 1.0f));
2499 _drawNode->drawLine(vertices[0], vertices[3], Color4F(1.0f, 1.0f, 1.0f, 1.0f));
2500 _drawNode->drawLine(vertices[2], vertices[3], Color4F(1.0f, 1.0f, 1.0f, 1.0f));
2501 _drawNode->drawLine(vertices[1], vertices[2], Color4F(1.0f, 1.0f, 1.0f, 1.0f));
2502
2503}
2504
2506{
2507 _label->setLineSpacing(5);
2508 _label->setAdditionalKerning(2);
2509 _label->setVerticalAlignment(TextVAlignment::CENTER);
2510 _label->setOverflow(Label::Overflow::CLAMP);
2511
2512}
2513
2515{
2516 return "Clamp content Test: Word Wrap";
2517}
2518
2520{
2521 return "";
2522}
2523
2525{
2526 _label->setLineBreakWithoutSpace(true);
2527 _label->setString("五六七八This \nis a very long sentence一二三四.");
2528 _label->setLineSpacing(5);
2529 _label->setAdditionalKerning(2);
2530 _label->setVerticalAlignment(TextVAlignment::TOP);
2531 _label->setOverflow(Label::Overflow::CLAMP);
2532
2533}
2534
2536{
2537 return "Clamp content Test: Char Wrap";
2538}
2539
2541{
2542 return "";
2543}
2544
2546
2548{
2549 _label->setLineBreakWithoutSpace(false);
2550 const char* no_break_space_utf8 = "\xC2\xA0"; // 0xA0 - no-break space
2551 auto str = StringUtils::format("The price is $%s1.25. \n\nthe space between \"$\" and \"1.25\" is a no break space.", no_break_space_utf8);
2552 _label->setString(str);
2553 _label->setVerticalAlignment(TextVAlignment::TOP);
2554 _label->setOverflow(Label::Overflow::CLAMP);
2555}
2556
2558{
2559 return "Wrap Test: No break space";
2560}
2561
2563{
2564 return "";
2565}
2566
2568
2570{
2571 _label->setLineSpacing(5);
2572 _label->setAdditionalKerning(2);
2573 _label->setString("This is Hello World hehe I love 一二三");
2574 _label->setVerticalAlignment(TextVAlignment::TOP);
2575 _label->setOverflow(Label::Overflow::SHRINK);
2576}
2577
2579{
2580 return "Shrink content Test: Word Wrap";
2581}
2582
2584{
2585 return "";
2586}
2587
2589{
2590 _label->setLineSpacing(5);
2591 _label->setAdditionalKerning(2);
2592 _label->setLineBreakWithoutSpace(true);
2593 _label->setString("This is Hello World hehe I love 一二三");
2594 _label->setVerticalAlignment(TextVAlignment::CENTER);
2595 _label->setOverflow(Label::Overflow::SHRINK);
2596}
2597
2599{
2600 return "Shrink content Test: Char Wrap";
2601}
2602
2604{
2605 return "";
2606}
2607
2609{
2610 _label->setLineSpacing(5);
2611 _label->setAdditionalKerning(2);
2612 _label->setVerticalAlignment(TextVAlignment::TOP);
2613 _label->setOverflow(Label::Overflow::RESIZE_HEIGHT);
2614
2615
2616 this->updateDrawNodeSize(_label->getContentSize());
2617
2618 auto slider1 = (ui::Slider*)this->getChildByTag(1);
2619
2620 auto slider2 = (ui::Slider*)this->getChildByTag(2);
2621 slider2->setVisible(false);
2622
2623 auto winSize = Director::getInstance()->getVisibleSize();
2624 slider1->addEventListener([=](Ref* ref, Slider::EventType event){
2625 float percent = slider1->getPercent();
2626 auto drawNodeSize = Size(percent / 100.0 * winSize.width,_label->getContentSize().height);
2627 if(drawNodeSize.height <= 0){
2628 drawNodeSize.height = 0.1f;
2629 }
2630 _label->setDimensions(drawNodeSize.width, drawNodeSize.height);
2631 this->updateDrawNodeSize(drawNodeSize);
2632 });
2633
2634 auto stepper = (ControlStepper*)this->getChildByName("stepper");
2635 stepper->setValue(12);
2636
2637 auto label = Label::createWithSystemFont("Char Line break:", "Arial", 10);
2638 label->setColor(Color3B::WHITE);
2639 label->setPosition(Vec2(winSize.width * 0.1f, winSize.height * 0.8f));
2640 this->addChild(label);
2641
2642 CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png",
2643 "cocosui/check_box_normal_press.png",
2644 "cocosui/check_box_active.png",
2645 "cocosui/check_box_normal_disable.png",
2646 "cocosui/check_box_active_disable.png");
2647 checkBox->setPosition(Vec2(winSize.width * 0.2f , winSize.height * 0.8f));
2648 checkBox->setScale(0.5);
2649 checkBox->setSelected(false);
2650 checkBox->setName("LineBreak");
2651
2652 checkBox->addEventListener([=](Ref* ref, CheckBox::EventType event){
2653 if (event == CheckBox::EventType::SELECTED) {
2654 _label->setLineBreakWithoutSpace(true);
2655 }else{
2656 _label->setLineBreakWithoutSpace(false);
2657 }
2658 this->updateDrawNodeSize(_label->getContentSize());
2659 });
2660 this->addChild(checkBox);
2661
2662}
2663
2664std::string LabelResizeTest::title() const
2665{
2666 return "Resize content Test";
2667}
2668
2669std::string LabelResizeTest::subtitle() const
2670{
2671 return "";
2672}
2673
2675{
2676 _label->setLineSpacing(5);
2677 _label->setAdditionalKerning(2);
2678 _label->setVerticalAlignment(TextVAlignment::CENTER);
2679 _label->setOverflow(Label::Overflow::NONE);
2680
2681
2682 this->updateDrawNodeSize(_label->getContentSize());
2683
2684 auto slider1 = (ui::Slider*)this->getChildByTag(1);
2685
2686 auto slider2 = (ui::Slider*)this->getChildByTag(2);
2687 slider2->setVisible(false);
2688
2689 auto winSize = Director::getInstance()->getVisibleSize();
2690 slider1->addEventListener([=](Ref* ref, Slider::EventType event){
2691 float percent = slider1->getPercent();
2692 auto drawNodeSize = Size(percent / 100.0 * winSize.width,_label->getContentSize().height);
2693 if(drawNodeSize.height <= 0){
2694 drawNodeSize.height = 0.1f;
2695 }
2696 _label->setDimensions(drawNodeSize.width, drawNodeSize.height);
2697 this->updateDrawNodeSize(drawNodeSize);
2698 });
2699
2700 auto stepper = (ControlStepper*)this->getChildByName("stepper");
2701 stepper->setValue(12);
2702
2703 auto label = Label::createWithSystemFont("Char Line break:", "Arial", 10);
2704 label->setColor(Color3B::WHITE);
2705 label->setPosition(Vec2(winSize.width * 0.1f, winSize.height * 0.8f));
2706 this->addChild(label);
2707
2708 CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png",
2709 "cocosui/check_box_normal_press.png",
2710 "cocosui/check_box_active.png",
2711 "cocosui/check_box_normal_disable.png",
2712 "cocosui/check_box_active_disable.png");
2713 checkBox->setPosition(Vec2(winSize.width * 0.2f , winSize.height * 0.8f));
2714 checkBox->setScale(0.5);
2715 checkBox->setSelected(false);
2716 checkBox->setName("LineBreak");
2717
2718 checkBox->addEventListener([=](Ref* ref, CheckBox::EventType event){
2719 if (event == CheckBox::EventType::SELECTED) {
2720 _label->setLineBreakWithoutSpace(true);
2721 }else{
2722 _label->setLineBreakWithoutSpace(false);
2723 }
2724 this->updateDrawNodeSize(_label->getContentSize());
2725 });
2726 this->addChild(checkBox);
2727
2728 this->initToggleCheckboxes();
2729}
2730
2732{
2733 const float BUTTON_WIDTH = 100;
2734 float startPosX = 0;
2735 Size winSize = Director::getInstance()->getVisibleSize();
2736
2737 // Create a radio button group
2738 auto radioButtonGroup = RadioButtonGroup::create();
2739 this->addChild(radioButtonGroup);
2740
2741 // Create the radio buttons
2742 static const int NUMBER_OF_BUTTONS = 4;
2743 startPosX = winSize.width / 2.0f - (NUMBER_OF_BUTTONS - 1 ) * 0.5 * BUTTON_WIDTH - 30;
2744 std::vector<std::string> labelTypes = {"Normal", "Clamp", "Shrink", "RESIZE"};
2745
2746 for(int i = 0; i < NUMBER_OF_BUTTONS; ++i)
2747 {
2748
2749 RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png");
2750 float posX = startPosX + BUTTON_WIDTH * i;
2751 radioButton->setPosition(Vec2(posX, winSize.height / 2.0f + 70));
2752 radioButton->setScale(1.2f);
2753 radioButton->addEventListener(CC_CALLBACK_2(LabelToggleTypeTest::onChangedRadioButtonSelect, this));
2754 radioButton->setTag(i);
2755 radioButtonGroup->addRadioButton(radioButton);
2756 this->addChild(radioButton);
2757
2758 auto label = Label::createWithSystemFont(labelTypes.at(i), "Arial", 20);
2759 label->setPosition(radioButton->getPosition() + Vec2(50.0f,0.0f));
2760 this->addChild(label);
2761 }
2762}
2763
2765{
2766 return "Toggle Label Type Test";
2767}
2768
2770{
2771 return "";
2772}
2773
2774
2775void LabelToggleTypeTest::onChangedRadioButtonSelect(RadioButton* radioButton, RadioButton::EventType type)
2776{
2777 if(radioButton == nullptr)
2778 {
2779 return;
2780 }
2781
2782 switch (type)
2783 {
2784 case RadioButton::EventType::SELECTED:
2785 {
2786 switch (radioButton->getTag()) {
2787 case 0:
2788 _label->setOverflow(Label::Overflow::NONE);
2789 break;
2790 case 1:
2791 _label->setOverflow(Label::Overflow::CLAMP);
2792 break;
2793 case 2:
2794 _label->setOverflow(Label::Overflow::SHRINK);
2795 break;
2796 case 3:
2797 _label->setOverflow(Label::Overflow::RESIZE_HEIGHT);
2798 break;
2799 default:
2800 break;
2801 }
2802 break;
2803 }
2804 default:
2805 break;
2806 }
2807 auto checkbox = (CheckBox*)(this->getChildByName("toggleWrap"));
2808 checkbox->setSelected(_label->isWrapEnabled());
2809 this->updateDrawNodeSize(_label->getContentSize());
2810}
2811
2813{
2814 _label->setLineSpacing(5);
2815 _label->setVerticalAlignment(TextVAlignment::CENTER);
2816 _label->setOverflow(Label::Overflow::NONE);
2817 _label->setSystemFontName("Hiragino Sans GB");
2818 _label->setSystemFontSize(20);
2819 _label->enableOutline(Color4B::RED, 1.0);
2820 _label->setString("This is a very\n 我爱你中国\n long sentence");
2821 _labelType = 2;
2822
2823 auto stepper = (ControlStepper*)this->getChildByName("stepper");
2824 stepper->setEnabled(true);
2825
2826 auto checkbox = (CheckBox*)(this->getChildByName("toggleType"));
2827 checkbox->setEnabled(false);
2828
2829 this->updateDrawNodeSize(_label->getContentSize());
2830
2831 auto slider1 = (ui::Slider*)this->getChildByTag(1);
2832
2833 auto winSize = Director::getInstance()->getVisibleSize();
2834 slider1->addEventListener([=](Ref* ref, Slider::EventType event){
2835 float percent = slider1->getPercent();
2836 auto drawNodeSize = Size(percent / 100.0 * winSize.width,_label->getContentSize().height);
2837 if(drawNodeSize.height <= 0){
2838 drawNodeSize.height = 0.1f;
2839 }
2840 _label->setDimensions(drawNodeSize.width, drawNodeSize.height);
2841 this->updateDrawNodeSize(drawNodeSize);
2842 });
2843
2844
2845 auto label = Label::createWithSystemFont("char Line break:", "Arial", 10);
2846 label->setColor(Color3B::WHITE);
2847 label->setPosition(Vec2(winSize.width * 0.1f, winSize.height * 0.8f));
2848 this->addChild(label);
2849
2850 CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png",
2851 "cocosui/check_box_normal_press.png",
2852 "cocosui/check_box_active.png",
2853 "cocosui/check_box_normal_disable.png",
2854 "cocosui/check_box_active_disable.png");
2855 checkBox->setPosition(Vec2(winSize.width * 0.2f , winSize.height * 0.8f));
2856 checkBox->setScale(0.5);
2857 checkBox->setSelected(false);
2858 checkBox->setName("LineBreak");
2859
2860 checkBox->addEventListener([=](Ref* ref, CheckBox::EventType event){
2861 if (event == CheckBox::EventType::SELECTED) {
2862 _label->setLineBreakWithoutSpace(true);
2863 }else{
2864 _label->setLineBreakWithoutSpace(false);
2865 }
2866 this->updateDrawNodeSize(_label->getContentSize());
2867 });
2868 this->addChild(checkBox);
2869
2870 this->initToggleCheckboxes();
2871
2872 auto checkboxToggleWrap = (CheckBox*)(this->getChildByName("toggleWrap"));
2873 checkboxToggleWrap->setEnabled(true);
2874}
2875
2877{
2878 const float BUTTON_WIDTH = 100;
2879 float startPosX = 0;
2880 Size winSize = Director::getInstance()->getVisibleSize();
2881
2882 // Create a radio button group
2883 auto radioButtonGroup = RadioButtonGroup::create();
2884 this->addChild(radioButtonGroup);
2885
2886 // Create the radio buttons
2887 static const int NUMBER_OF_BUTTONS = 4;
2888 startPosX = winSize.width / 2.0f - (NUMBER_OF_BUTTONS - 1 ) * 0.5 * BUTTON_WIDTH - 30;
2889 std::vector<std::string> labelTypes = {"Normal", "Clamp", "Shrink", "RESIZE"};
2890
2891 for(int i = 0; i < NUMBER_OF_BUTTONS; ++i)
2892 {
2893
2894 RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png");
2895 float posX = startPosX + BUTTON_WIDTH * i;
2896 radioButton->setPosition(Vec2(posX, winSize.height / 2.0f + 70));
2897 radioButton->setScale(1.2f);
2898 radioButton->addEventListener(CC_CALLBACK_2(LabelSystemFontTest::onChangedRadioButtonSelect, this));
2899 radioButton->setTag(i);
2900 radioButtonGroup->addRadioButton(radioButton);
2901 this->addChild(radioButton);
2902
2903 auto label = Label::createWithSystemFont(labelTypes.at(i), "Arial", 20);
2904 label->setPosition(radioButton->getPosition() + Vec2(50.0f,0.0f));
2905 this->addChild(label);
2906 }
2907}
2908
2910{
2911 return "System Font Test";
2912}
2913
2915{
2916 return "";
2917}
2918
2919
2920void LabelSystemFontTest::onChangedRadioButtonSelect(RadioButton* radioButton, RadioButton::EventType type)
2921{
2922 if(radioButton == nullptr)
2923 {
2924 return;
2925 }
2926
2927 switch (type)
2928 {
2929 case RadioButton::EventType::SELECTED:
2930 {
2931 switch (radioButton->getTag()) {
2932 case 0:
2933 _label->setOverflow(Label::Overflow::NONE);
2934 break;
2935 case 1:
2936 _label->setOverflow(Label::Overflow::CLAMP);
2937 break;
2938 case 2:
2939 _label->setOverflow(Label::Overflow::SHRINK);
2940 break;
2941 case 3:
2942 _label->setOverflow(Label::Overflow::RESIZE_HEIGHT);
2943 break;
2944 default:
2945 break;
2946 }
2947 break;
2948 }
2949 default:
2950 break;
2951 }
2952 this->updateDrawNodeSize(_label->getContentSize());
2953}
2954
2956{
2957 _label->setLineSpacing(5);
2958 _label->setVerticalAlignment(TextVAlignment::CENTER);
2959 _label->setOverflow(Label::Overflow::NONE);
2960 _label->setCharMap("fonts/tuffy_bold_italic-charmap.plist");
2961 _label->setString("Hello World, This is a char map test.");
2962 _label->setScale(0.5f);
2963
2964 auto stepper = (ControlStepper*)this->getChildByName("stepper");
2965 stepper->setEnabled(true);
2966
2967 auto checkbox = (CheckBox*)(this->getChildByName("toggleType"));
2968 checkbox->setEnabled(false);
2969
2970 this->updateDrawNodeSize(_label->getContentSize());
2971
2972}
2973
2974
2976{
2977 return "CharMap Font Test";
2978}
2979
2981{
2982 return "";
2983}
2984
2986{
2987 auto center = VisibleRect::center();
2988
2989 auto label = Label::createWithTTF("12345", "fonts/arial.ttf", 26);
2990 label->setPosition(center);
2991 addChild(label);
2992
2993 label->getLetter(2)->setVisible(false);
2994}
2995
2997{
2998 return "Test for Issue #13846";
2999}
3000
3002{
3003 return "Test hide label's letter,the label should display '12 45' as expected";
3004}
3005
3006//
3007//
3008
3010{
3011 auto center = VisibleRect::center();
3012
3013 auto richText2 = RichText::createWithXML("Mixing <b>UIRichText</b> with non <i>UIWidget</i> code. For more samples, see the UIRichTextTest.cpp file");
3014 if (richText2)
3015 {
3016 richText2->ignoreContentAdaptWithSize(false);
3017 richText2->setContentSize(Size(400.0f, 400.0f));
3018 richText2->setPosition(center);
3019
3020 addChild(richText2);
3021 }
3022}
3023
3024std::string LabelRichText::title() const
3025{
3026 return "RichText";
3027}
3028
3029std::string LabelRichText::subtitle() const
3030{
3031 return "Testing RichText";
3032}
3033
3035{
3036 auto s = Director::getInstance()->getWinSize();
3037
3038 // LabelBMFont
3039 auto label1 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "hello non-italics", TextHAlignment::CENTER, s.width);
3040 addChild(label1, 0, kTagBitmapAtlas1);
3041 label1->setPosition(Vec2(s.width/2, s.height*4/6));
3042 // you can enable italics by calling this method
3043
3044 _label1a = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "hello italics", TextHAlignment::CENTER, s.width);
3045 addChild(_label1a, 0, kTagBitmapAtlas1);
3046 _label1a->setPosition(Vec2(s.width/2, s.height*3/6));
3047 // you can enable italics by calling this method
3048 _label1a->enableItalics();
3049
3050
3051 // LabelTTF
3052 TTFConfig ttfConfig("fonts/arial.ttf",24);
3053 auto label2 = Label::createWithTTF(ttfConfig, "hello non-italics", TextHAlignment::CENTER,s.width);
3054 addChild(label2, 0, kTagBitmapAtlas2);
3055 label2->setPosition(Vec2(s.width/2, s.height*2/6));
3056
3057 // or by setting the italics parameter on TTFConfig
3058 ttfConfig.italics = true;
3059 _label2a = Label::createWithTTF(ttfConfig, "hello italics", TextHAlignment::CENTER,s.width);
3060 addChild(_label2a, 0, kTagBitmapAtlas2);
3061 _label2a->setPosition(Vec2(s.width/2, s.height*1/6));
3062
3063 auto menuItem = MenuItemFont::create("disable italics", [&](cocos2d::Ref* sender) {
3064 _label2a->disableEffect(LabelEffect::ITALICS);
3065 _label1a->disableEffect(LabelEffect::ITALICS);
3066 });
3067 menuItem->setFontSizeObj(12);
3068 auto menu = Menu::createWithItem(menuItem);
3069 addChild(menu);
3070 auto winSize = Director::getInstance()->getWinSize();
3071 menu->setPosition(winSize.width * 0.9, winSize.height * 0.25f);
3072}
3073
3074std::string LabelItalics::title() const
3075{
3076 return "Testing Italics";
3077}
3078
3079std::string LabelItalics::subtitle() const
3080{
3081 return "italics on TTF and BMfont";
3082}
3083
3085
3087{
3088 auto s = Director::getInstance()->getWinSize();
3089
3090 // LabelBMFont
3091 auto label1 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "hello non-bold", TextHAlignment::CENTER, s.width);
3092 addChild(label1, 0, kTagBitmapAtlas1);
3093 label1->setPosition(Vec2(s.width/2, s.height*4/6));
3094 // you can enable italics by calling this method
3095
3096 _label1a = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "hello bold", TextHAlignment::CENTER, s.width);
3097 addChild(_label1a, 0, kTagBitmapAtlas1);
3098 _label1a->setPosition(Vec2(s.width/2, s.height*3/6));
3099 // you can enable italics by calling this method
3100 _label1a->enableBold();
3101
3102
3103 // LabelTTF
3104 TTFConfig ttfConfig("fonts/arial.ttf",24);
3105 auto label2 = Label::createWithTTF(ttfConfig, "hello non-bold", TextHAlignment::CENTER,s.width);
3106 addChild(label2, 0, kTagBitmapAtlas2);
3107 label2->setPosition(Vec2(s.width/2, s.height*2/6));
3108
3109 // or by setting the italics parameter on TTFConfig
3110 ttfConfig.bold = true;
3111 _label2a = Label::createWithTTF(ttfConfig, "hello bold", TextHAlignment::CENTER,s.width);
3112 addChild(_label2a, 0, kTagBitmapAtlas2);
3113 _label2a->setPosition(Vec2(s.width/2, s.height*1/6));
3114
3115 auto menuItem = MenuItemFont::create("disable bold", [&](cocos2d::Ref* sender) {
3116 _label2a->disableEffect(LabelEffect::BOLD);
3117 _label1a->disableEffect(LabelEffect::BOLD);
3118 });
3119 menuItem->setFontSizeObj(12);
3120 auto menu = Menu::createWithItem(menuItem);
3121 addChild(menu);
3122 auto winSize = Director::getInstance()->getWinSize();
3123 menu->setPosition(winSize.width * 0.9, winSize.height * 0.25f);
3124}
3125
3126std::string LabelBold::title() const
3127{
3128 return "Testing Bold";
3129}
3130
3131std::string LabelBold::subtitle() const
3132{
3133 return "Bold on TTF and BMfont";
3134}
3135
3137
3139{
3140 auto s = Director::getInstance()->getWinSize();
3141
3142 // LabelBMFont
3143 auto label1 = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "hello non-underline", TextHAlignment::CENTER, s.width);
3144 addChild(label1, 0, kTagBitmapAtlas1);
3145 label1->setPosition(Vec2(s.width/2, s.height*4/6));
3146 // you can enable italics by calling this method
3147
3148 _label1a = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "hello underline", TextHAlignment::CENTER, s.width);
3149 addChild(_label1a, 0, kTagBitmapAtlas1);
3150 _label1a->setPosition(Vec2(s.width/2, s.height*3/6));
3151 // you can enable underline by calling this method
3152 _label1a->enableUnderline();
3153
3154
3155 // LabelTTF
3156 TTFConfig ttfConfig("fonts/arial.ttf",24);
3157 auto label2 = Label::createWithTTF(ttfConfig, "hello non-underline", TextHAlignment::CENTER,s.width);
3158 addChild(label2, 0, kTagBitmapAtlas2);
3159 label2->setPosition(Vec2(s.width/2, s.height*2/6));
3160
3161 // or by setting the italics parameter on TTFConfig
3162 ttfConfig.underline = true;
3163 _label2a = Label::createWithTTF(ttfConfig, "hello underline", TextHAlignment::CENTER,s.width);
3164 addChild(_label2a, 0, kTagBitmapAtlas2);
3165 _label2a->setPosition(Vec2(s.width/2, s.height*1/6));
3166
3167 auto menuItem = MenuItemFont::create("disable underline", [&](cocos2d::Ref* sender) {
3168 _label2a->disableEffect(LabelEffect::UNDERLINE);
3169 _label1a->disableEffect(LabelEffect::UNDERLINE);
3170 });
3171 menuItem->setFontSizeObj(12);
3172 auto menu = Menu::createWithItem(menuItem);
3173 addChild(menu);
3174 auto winSize = Director::getInstance()->getWinSize();
3175 menu->setPosition(winSize.width * 0.9, winSize.height * 0.25f);
3176}
3177
3178std::string LabelUnderline::title() const
3179{
3180 return "Testing Underline";
3181}
3182
3183std::string LabelUnderline::subtitle() const
3184{
3185 return "Underline on TTF and BMfont";
3186}
3187
3189
3191{
3192 auto s = Director::getInstance()->getWinSize();
3193
3194 // bmfont
3195 _label1a = Label::createWithBMFont("fonts/bitmapFontTest5.fnt", "hello underline\nand multiline", TextHAlignment::CENTER, s.width);
3196 addChild(_label1a, 0, kTagBitmapAtlas1);
3197 _label1a->setPosition(Vec2(s.width/2, s.height*2/3));
3198 // you can enable underline by calling this method
3199 _label1a->enableUnderline();
3200
3201 // ttf
3202 TTFConfig ttfConfig("fonts/arial.ttf",24);
3203 ttfConfig.underline = true;
3204 _label2a = Label::createWithTTF(ttfConfig, "hello\nunderline\nwith multiline", TextHAlignment::LEFT, s.width);
3205 addChild(_label2a, 0, kTagBitmapAtlas2);
3206 _label2a->setPosition(Vec2(s.width/2, s.height*1/3));
3207
3208 auto menuItem = MenuItemFont::create("disable underline", [&](cocos2d::Ref* sender) {
3209 _label2a->disableEffect(LabelEffect::UNDERLINE);
3210 _label1a->disableEffect(LabelEffect::UNDERLINE);
3211 });
3212 menuItem->setFontSizeObj(12);
3213 auto menu = Menu::createWithItem(menuItem);
3214 addChild(menu);
3215 auto winSize = Director::getInstance()->getWinSize();
3216 menu->setPosition(winSize.width * 0.9, winSize.height * 0.25f);
3217}
3218
3220{
3221 return "Testing Underline + multiline";
3222}
3223
3225{
3226 return "Underline on TTF and BMfont with multiline";
3227}
3228
3230
3232{
3233 auto s = Director::getInstance()->getWinSize();
3234
3235 // bmfont
3236 _label1a = Label::createWithBMFont("fonts/bitmapFontTest4.fnt", "hello strikethrough\nand multiline", TextHAlignment::LEFT, s.width);
3237 addChild(_label1a, 0, kTagBitmapAtlas1);
3238 _label1a->setPosition(Vec2(s.width/2, s.height*2/3));
3239 // you can enable underline by calling this method
3240 _label1a->enableStrikethrough();
3241
3242 // ttf
3243 TTFConfig ttfConfig("fonts/arial.ttf",24);
3244 ttfConfig.strikethrough = true;
3245 _label2a = Label::createWithTTF(ttfConfig, "hello\nstrikethrough\nwith multiline", TextHAlignment::RIGHT, s.width);
3246 addChild(_label2a, 0, kTagBitmapAtlas2);
3247 _label2a->setPosition(Vec2(s.width/2, s.height*1/3));
3248
3249 auto menuItem = MenuItemFont::create("disable underline", [&](cocos2d::Ref* sender) {
3250 _label2a->disableEffect(LabelEffect::STRIKETHROUGH);
3251 _label1a->disableEffect(LabelEffect::STRIKETHROUGH);
3252 });
3253 menuItem->setFontSizeObj(12);
3254 auto menu = Menu::createWithItem(menuItem);
3255 addChild(menu);
3256 auto winSize = Director::getInstance()->getWinSize();
3257 menu->setPosition(winSize.width * 0.9, winSize.height * 0.25f);
3258}
3259
3260std::string LabelStrikethrough::title() const
3261{
3262 return "Testing Strikethrough + multiline";
3263}
3264
3266{
3267 return "Strikethrough on TTF and BMfont with multiline";
3268}
3269
3271{
3272 _localizationJson = cocostudio::JsonLocalizationManager::getInstance();
3273 _localizationJson->initLanguageData("configs/en-US.lang.json");
3274
3275 _localizationBin = cocostudio::BinLocalizationManager::getInstance();
3276 _localizationBin->initLanguageData("configs/ENGLISH.lang.csb");
3277
3278 const float BUTTON_WIDTH = 100;
3279 float startPosX = 0;
3280 Size winSize = Director::getInstance()->getVisibleSize();
3281
3282 // Create a radio button group
3283 auto radioButtonGroup = RadioButtonGroup::create();
3284 this->addChild(radioButtonGroup);
3285
3286 // Create the radio buttons
3287 const int NUMBER_OF_BUTTONS = 3;
3288 startPosX = winSize.width / 2.0f - (NUMBER_OF_BUTTONS - 1) * 0.5 * BUTTON_WIDTH - 30;
3289 std::vector<std::string> labelTypes = { "English", "Chinese", "Japanese" };
3290
3291 for (int i = 0; i < NUMBER_OF_BUTTONS; ++i)
3292 {
3293 RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png");
3294 float posX = startPosX + BUTTON_WIDTH * i;
3295 radioButton->setPosition(Vec2(posX, winSize.height / 2.0f + 70));
3296 radioButton->setScale(1.2f);
3297 radioButton->addEventListener(CC_CALLBACK_2(LabelLocalizationTest::onChangedRadioButtonSelect, this));
3298 radioButton->setTag(i);
3299 radioButtonGroup->addRadioButton(radioButton);
3300 this->addChild(radioButton);
3301
3302 auto label = Label::createWithSystemFont(labelTypes.at(i), "Arial", 20);
3303 label->setPosition(radioButton->getPosition() + Vec2(50.0f, 0.0f));
3304 this->addChild(label);
3305 }
3306
3307 _label1 = Label::createWithSystemFont(_localizationJson->getLocalizationString("Text Label"), "Arial", 24);
3308 addChild(_label1, 0);
3309 _label1->setPosition(Vec2(winSize.width / 2, winSize.height * 1 / 3));
3310
3311 Label * label = Label::createWithSystemFont("From json data :", "Arial", 24);
3312 label->setAnchorPoint(Vec2(0.0f, 0.5f));
3313 addChild(label, 0);
3314 label->setPosition(Vec2(20.0f, winSize.height * 1 / 3 + 24));
3315
3316 _label2 = Label::createWithSystemFont(_localizationBin->getLocalizationString("Text Label"), "Arial", 24);
3317 addChild(_label2, 0);
3318 _label2->setPosition(Vec2(winSize.width / 2, winSize.height * 1 / 2));
3319
3320 label = Label::createWithSystemFont("From binary data :", "Arial", 24);
3321 label->setAnchorPoint(Vec2(0.0f, 0.5f));
3322 addChild(label, 0);
3323 label->setPosition(Vec2(20.0f, winSize.height * 1 / 2 + 24));
3324}
3325
3327{
3328 return "Localization Test";
3329}
3330
3332{
3333 return "Change language selected and see label change";
3334}
3335
3336
3337void LabelLocalizationTest::onChangedRadioButtonSelect(RadioButton* radioButton, RadioButton::EventType type)
3338{
3339 if (radioButton == nullptr)
3340 {
3341 return;
3342 }
3343
3344 switch (type)
3345 {
3346 case RadioButton::EventType::SELECTED:
3347 {
3348 switch (radioButton->getTag()) {
3349 case 0:
3350 _localizationJson->initLanguageData("configs/en-US.lang.json");
3351 _label1->setString(_localizationJson->getLocalizationString("Text Label"));
3352 _localizationBin->initLanguageData("configs/ENGLISH.lang.csb");
3353 _label2->setString(_localizationBin->getLocalizationString("Text Label"));
3354 break;
3355 case 1:
3356 _localizationJson->initLanguageData("configs/zh-CN.lang.json");
3357 _label1->setString(_localizationJson->getLocalizationString("Text Label"));
3358 _localizationBin->initLanguageData("configs/CHINESE.lang.csb");
3359 _label2->setString(_localizationBin->getLocalizationString("Text Label"));
3360 break;
3361 case 2:
3362 _localizationJson->initLanguageData("configs/ja-JP.lang.json");
3363 _label1->setString(_localizationJson->getLocalizationString("Text Label"));
3364 _localizationBin->initLanguageData("configs/JAPANESE.lang.csb");
3365 _label2->setString(_localizationBin->getLocalizationString("Text Label"));
3366 break;
3367 default:
3368 break;
3369 }
3370 break;
3371 }
3372 default:
3373 break;
3374 }
3375}
3376
3377//
3378// LabelIssue15214
3379//
3381{
3382 auto size = Director::getInstance()->getVisibleSize();
3383
3384 // 1
3385 Label* label = Label::createWithTTF("TTF with setColor()", "fonts/arial.ttf", 24.0f);
3386 label->enableUnderline();
3387 label->setColor(cocos2d::Color3B::BLUE);
3388 label->setPosition(size.width/2, size.height/5*4);
3389 this->addChild(label);
3390
3391 // 2
3392 Label* label2 = Label::createWithSystemFont("System with setColor()", "Verdana", 24.0f);
3393 label2->enableUnderline();
3394 label2->setColor(cocos2d::Color3B::BLUE);
3395 label2->setPosition(size.width/2, size.height/5*3);
3396 this->addChild(label2);
3397
3398 // 3
3399 Label* label3 = Label::createWithTTF("TTF with setTextColor()", "fonts/arial.ttf", 24.0f);
3400 label3->enableUnderline();
3401 label3->setTextColor(Color4B::BLUE);
3402 label3->setPosition(size.width/2, size.height/5*2);
3403 this->addChild(label3);
3404
3405 // 4
3406 Label* label4 = Label::createWithSystemFont("System with setTextColor()", "Verdana", 24.0f);
3407 label4->enableUnderline();
3408 label4->setTextColor(Color4B::BLUE);
3409 label4->setPosition(size.width/2, size.height/5*1);
3410 this->addChild(label4);
3411}
3412
3413std::string LabelIssue15214::title() const
3414{
3415 return "Github Issue 15214";
3416}
3417
3418std::string LabelIssue15214::subtitle() const
3419{
3420 return "Font + underline: same color with setColor()";
3421}
3422
3423//
3424// LabelIssue16293
3425//
3427{
3428 auto size = Director::getInstance()->getVisibleSize();
3429 Label* label = Label::createWithTTF("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", "fonts/arial.ttf", 12);
3430 label->setPosition(size.width/2, size.height/2);
3431 this->addChild(label);
3432}
3433
3434std::string LabelIssue16293::title() const
3435{
3436 return "Github Issue 16293";
3437}
3438
3439std::string LabelIssue16293::subtitle() const
3440{
3441 return "No TextureAtlas resizes";
3442}
3443
3444//
3445// LabelIssue16471
3446//
3448{
3449 auto size = Director::getInstance()->getVisibleSize();
3450
3451 auto node = Node::create();
3452 addChild(node, 100);
3453 node->setPosition(size.width/2, size.height/2);
3454
3455 // Used Google Translate to translate from Chinese:
3456 // Here is set to false then textLabel: TextColor valid
3457 // set to true testLabel: setTextColor invalid
3458 // Original:
3459 // 此处设置为false则testLabel:setTextColor有效
3460 // 设置为true则testLabel:setTextColor无效
3461 // if set false then testLabel:setTextColor is useful
3462 node->setCascadeColorEnabled(true);
3463 Label* label = Label::createWithTTF("Should be Yellow", "fonts/arial.ttf", 12);
3464 label->setTextColor(Color4B::YELLOW);
3465 node->addChild(label);
3466}
3467
3468std::string LabelIssue16471::title() const
3469{
3470 return "Github Issue 16471";
3471}
3472
3473std::string LabelIssue16471::subtitle() const
3474{
3475 return "Label should be yellow";
3476}
3477
3478//
3479// LabelIssue16717
3480//
3482{
3483 auto bg = Sprite::create("cocosui/Hello.png");
3484 bg->setPosition(VisibleRect::center());
3485 addChild(bg);
3486
3487 {
3488 auto label = Label::createWithTTF("Hello World", "fonts/arial.ttf", 70);
3489 label->setPosition(VisibleRect::center() + Vec2(0.0f, 40.0f));
3490 label->enableOutline(Color4B(0, 255, 0, 100), 10); // Set 100 alpha for outline
3491 label->setTextColor(Color4B(0, 0, 255, 100)); // Also set 100 alpha for text
3492 addChild(label);
3493 }
3494
3495 {
3496 auto label = Label::createWithTTF("Hello World", "fonts/arial.ttf", 70);
3497 label->setPosition(VisibleRect::center() + Vec2(0.0f, -40.0f));
3498 label->enableOutline(Color4B(0, 255, 0, 100), 10); // Set 100 alpha for outline
3499 label->setTextColor(Color4B(0, 255, 0, 100)); // Also set 100 alpha for text
3500 addChild(label);
3501 }
3502}
3503
3504std::string LabelIssue16717::title() const
3505{
3506 return "Github Issue 16717";
3507}
3508
3509std::string LabelIssue16717::subtitle() const
3510{
3511 return "";
3512}
3513
3514//
3515// LabelIssueLineGap
3516//
3518{
3519
3520 auto size = Director::getInstance()->getWinSize();
3521
3522 auto label1 = Label::createWithTTF("test \ntest", "fonts/FingerpopGap.ttf", 30);
3523 label1->setPosition(Vec2(size.width / 3, size.height / 2));
3524 addChild(label1);
3525
3526 auto label2 = Label::createWithSystemFont("test \ntest", "fonts/FingerpopGap.ttf", 30);
3527 label2->setPosition(Vec2(size.width / 3 * 1.8 , size.height / 2));
3528 addChild(label2);
3529}
3530
3531std::string LabelIssueLineGap::title() const
3532{
3533 return "Label line gap issue";
3534}
3535
3537{
3538 return "two label must have exactly the same position and distance between lines";
3539}
3540
3541//
3542// LabelIssue17902
3543//
3545{
3546 auto center = VisibleRect::center();
3547
3548 auto label = Label::createWithTTF("abcdefg\nhijklmn", "fonts/arial.ttf", 26);
3549 label->setLineHeight(40);
3550 label->setPosition(center);
3551 addChild(label);
3552
3553 scheduleOnce(CC_CALLBACK_0(LabelIssue17902::purgeCachedData, this), 1.0f, "purge_cached_data");
3554}
3555
3557{
3558 FontAtlasCache::purgeCachedData();
3559}
3560
3561std::string LabelIssue17902::title() const
3562{
3563 return "Github Issue 17902";
3564}
3565
3566std::string LabelIssue17902::subtitle() const
3567{
3568 return "";
3569}
3570
3571//
3572// LabelLetterColorsTest
3573//
3575 auto center = VisibleRect::center();
3576
3577 auto label = Label::createWithTTF("", "fonts/arial.ttf", 24);
3578 label->setPosition(center.x, center.y);
3579 addChild(label);
3580
3581 label->setString("1\n2\n3");
3582 setLetterColors(label, Color3B::RED);
3583
3584 label->setString("abcd\ne"); // Must not crash at here.
3585}
3586
3587std::string LabelLetterColorsTest::title() const {
3588 return "Test for letter colors";
3589}
3590
3592 return "Should not crash!";
3593}
3594
3595void LabelLetterColorsTest::setLetterColors(cocos2d::Label* label, const cocos2d::Color3B& color) {
3596 int n = label->getStringLength();
3597 for (int i = 0; i < n; ++i) {
3598 Sprite* letter = label->getLetter(i);
3599 if (letter != nullptr)
3600 letter->setColor(color);
3601 }
3602}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
#define LineBreaksExample
#define ArrowsMin
@ kTagBitmapAtlas3
@ kTagTileMap
@ kTagBitmapAtlas2
@ kTagAnimation1
@ kTagSpriteManager
@ kTagBitmapAtlas1
#define LineBreaks
#define MixedExample
#define CenterAlign
#define LongSentencesExample
BitmapFontMultiLineAlignmentNew
static float menuItemPaddingCenter
@ kTagSprite5
@ kTagSprite2
@ kTagSprite8
@ kTagSprite6
@ kTagSprite7
@ kTagSprite4
@ kTagSprite1
@ kTagSprite3
USING_NS_CC
#define ArrowsMax
#define LongSentences
static float alignmentItemPadding
#define Mixed
#define LeftAlign
#define RightAlign
static float startPosX
static const float BUTTON_WIDTH
virtual std::string title() const override
cocos2d::Label * label
Definition: LabelTestNew.h:501
virtual std::string subtitle() const override
void sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
virtual std::string title() const override
void setAlignmentTop(cocos2d::Ref *sender)
void setAlignmentRight(cocos2d::Ref *sender)
cocos2d::Label * _label
Definition: LabelTestNew.h:449
void setAlignmentLeft(cocos2d::Ref *sender)
virtual std::string subtitle() const override
void setAlignmentCenter(cocos2d::Ref *sender)
void setAlignmentMiddle(cocos2d::Ref *sender)
void setAlignmentBottom(cocos2d::Ref *sender)
virtual std::string title() const override
cocos2d::Label * _label2a
Definition: LabelTestNew.h:856
virtual std::string title() const override
virtual std::string subtitle() const override
cocos2d::Label * _label1a
Definition: LabelTestNew.h:855
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
void step(float dt)
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void step(float dt)
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
cocos2d::MenuItemFont * _lastAlignmentItem
Definition: LabelTestNew.h:188
void alignmentChanged(cocos2d::Ref *sender)
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void selectSentenceItem(cocos2d::MenuItemFont *item)
cocos2d::Sprite * _arrowsBar
Definition: LabelTestNew.h:184
void stringChanged(cocos2d::Ref *sender)
cocos2d::Sprite * _arrows
Definition: LabelTestNew.h:185
virtual std::string subtitle() const override
void selectAlignmentItem(cocos2d::MenuItemFont *item)
virtual bool init() override
cocos2d::Label * _label
Definition: LabelTestNew.h:183
cocos2d::MenuItemFont * _lastSentenceItem
Definition: LabelTestNew.h:187
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
std::vector< cocos2d::MenuItemFont * > _menuItems
Definition: LabelTestNew.h:186
virtual std::string title() const override
void onTouchesBegan(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string getItemString(cocos2d::MenuItemFont *item)
virtual std::string getItemString(cocos2d::MenuItemFont *item) override
virtual std::string subtitle() const override
bool init() override
LabelFNTMultiLineAlignmentUNICODE
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual void step(float dt)
virtual std::string title() const override
virtual std::string subtitle() const override
LabelFNTUNICODELanguages()
BMFontUnicodeNew
virtual std::string title() const override
virtual std::string title() const override
void updateStrings(float dt)
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual void purgeCachedData()
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
cocos2d::Label * _label1a
Definition: LabelTestNew.h:842
cocos2d::Label * _label2a
Definition: LabelTestNew.h:843
void setAlignmentMiddle(cocos2d::Ref *sender)
void initFontSizeChange(const cocos2d::Size &size)
void initTestLabel(const cocos2d::Size &size)
void initSliders(const cocos2d::Size &size)
void initWrapOption(const cocos2d::Size &size)
void initDrawNode(const cocos2d::Size &size)
void setAlignmentBottom(cocos2d::Ref *sender)
void initToggleLabelTypeOption(const cocos2d::Size &size)
void updateDrawNodeSize(const cocos2d::Size &drawNodeSize)
cocos2d::extension::ControlStepper * makeControlStepper()
void setAlignmentTop(cocos2d::Ref *sender)
void setAlignmentLeft(cocos2d::Ref *sender)
void setAlignmentRight(cocos2d::Ref *sender)
void setAlignmentCenter(cocos2d::Ref *sender)
cocos2d::Label * _label
Definition: LabelTestNew.h:705
void initAlignmentOption(const cocos2d::Size &size)
cocos2d::DrawNode * _drawNode
Definition: LabelTestNew.h:704
void valueChanged(cocos2d::Ref *sender, cocos2d::extension::Control::EventType controlEvent)
virtual std::string subtitle() const override
virtual std::string title() const override
static void setLetterColors(cocos2d::Label *label, const cocos2d::Color3B &color)
virtual std::string title() const override
void sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
cocos2d::Label * label
Definition: LabelTestNew.h:486
virtual std::string subtitle() const override
cocostudio::ILocalizationManager * _localizationBin
Definition: LabelTestNew.h:912
virtual std::string title() const override
virtual std::string subtitle() const override
void onChangedRadioButtonSelect(cocos2d::ui::RadioButton *radioButton, cocos2d::ui::RadioButton::EventType type)
cocos2d::Label * _label1
Definition: LabelTestNew.h:909
cocostudio::ILocalizationManager * _localizationJson
Definition: LabelTestNew.h:911
cocos2d::Label * _label2
Definition: LabelTestNew.h:910
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
cocos2d::Label * shadowLabelGrow
Definition: LabelTestNew.h:363
void sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
cocos2d::Label * shadowLabelOutline
Definition: LabelTestNew.h:361
cocos2d::Label * shadowLabelTTF
Definition: LabelTestNew.h:362
virtual void onEnter() override
cocos2d::Label * shadowLabelBMFont
Definition: LabelTestNew.h:364
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
cocos2d::Label * _label2a
Definition: LabelTestNew.h:895
virtual std::string subtitle() const override
cocos2d::Label * _label1a
Definition: LabelTestNew.h:894
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
void onChangedRadioButtonSelect(cocos2d::ui::RadioButton *radioButton, cocos2d::ui::RadioButton::EventType type)
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
cocos2d::Label * _label
Definition: LabelTestNew.h:275
virtual std::string subtitle() const override
virtual std::string title() const override
cocos2d::TextHAlignment _horizAlign
Definition: LabelTestNew.h:276
void setAlignmentCenter(cocos2d::Ref *sender)
void setAlignmentLeft(cocos2d::Ref *sender)
void setAlignmentRight(cocos2d::Ref *sender)
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
void onChangedRadioButtonSelect(cocos2d::ui::RadioButton *radioButton, cocos2d::ui::RadioButton::EventType type)
virtual std::string title() const override
virtual std::string subtitle() const override
cocos2d::Label * _label2a
Definition: LabelTestNew.h:869
cocos2d::Label * _label1a
Definition: LabelTestNew.h:868
virtual std::string subtitle() const override
virtual std::string title() const override
cocos2d::Label * _label2a
Definition: LabelTestNew.h:882
cocos2d::Label * _label1a
Definition: LabelTestNew.h:881
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onEnter() override
Definition: BaseTest.cpp:430
cocos2d::Label * _subtitleLabel
Definition: BaseTest.h:107
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69
static cocos2d::Vec2 rightTop()
Definition: VisibleRect.cpp:81
static cocos2d::Vec2 leftBottom()
Definition: VisibleRect.cpp:87