Skip to content

Commit 3d63730

Browse files
Dimon4egminggo
authored andcommitted
Add test wrap unicode text (cocos2d#18570)
* fix LabelFNTMultiLineAlignment test * Added test for unicode text wrap
1 parent b2ecbca commit 3d63730

File tree

4 files changed

+187
-69
lines changed

4 files changed

+187
-69
lines changed

tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp

Lines changed: 152 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ NewLabelTests::NewLabelTests()
4343
ADD_TEST_CASE(LabelFNTMultiLine);
4444
ADD_TEST_CASE(LabelFNTRetina);
4545
ADD_TEST_CASE(LabelFNTMultiLineAlignment);
46+
ADD_TEST_CASE(LabelFNTMultiLineAlignmentUNICODE);
4647
ADD_TEST_CASE(LabelFNTUNICODELanguages);
4748
ADD_TEST_CASE(LabelFNTBounds);
4849
ADD_TEST_CASE(LabelFNTandTTFEmpty);
@@ -556,8 +557,13 @@ std::string LabelFNTGlyphDesigner::subtitle() const
556557
static float alignmentItemPadding = 50;
557558
static float menuItemPaddingCenter = 50;
558559

559-
LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment()
560+
bool LabelFNTMultiLineAlignment::init()
560561
{
562+
if (!AtlasDemoNew::init())
563+
{
564+
return false;
565+
}
566+
561567
auto listener = EventListenerTouchAllAtOnce::create();
562568
listener->onTouchesBegan = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesBegan, this);
563569
listener->onTouchesMoved = CC_CALLBACK_2(LabelFNTMultiLineAlignment::onTouchesMoved, this);
@@ -568,13 +574,10 @@ LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment()
568574
auto size = Director::getInstance()->getWinSize();
569575

570576
// create and initialize a Label
571-
this->_labelShouldRetain = Label::createWithBMFont("fonts/markerFelt.fnt", LongSentencesExample, TextHAlignment::CENTER, size.width/1.5);
572-
this->_labelShouldRetain->retain();
577+
this->_label = Label::createWithBMFont("fonts/markerFelt.fnt", "", TextHAlignment::CENTER, size.width/1.5);
573578

574-
this->_arrowsBarShouldRetain = Sprite::create("Images/arrowsBar.png");
575-
this->_arrowsBarShouldRetain->retain();
576-
this->_arrowsShouldRetain = Sprite::create("Images/arrows.png");
577-
this->_arrowsShouldRetain->retain();
579+
this->_arrowsBar = Sprite::create("Images/arrowsBar.png");
580+
this->_arrows = Sprite::create("Images/arrows.png");
578581

579582
MenuItemFont::setFontSize(20);
580583
auto longSentences = MenuItemFont::create("Long Flowing Sentences", CC_CALLBACK_1(LabelFNTMultiLineAlignment::stringChanged, this));
@@ -583,52 +586,50 @@ LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment()
583586
auto stringMenu = Menu::create(longSentences, lineBreaks, mixed, nullptr);
584587
stringMenu->alignItemsVertically();
585588

586-
longSentences->setColor(Color3B::RED);
587-
_lastSentenceItem = longSentences;
588589
longSentences->setTag(LongSentences);
589590
lineBreaks->setTag(LineBreaks);
590591
mixed->setTag(Mixed);
591592

593+
_menuItems.push_back(longSentences);
594+
_menuItems.push_back(lineBreaks);
595+
_menuItems.push_back(mixed);
596+
592597
MenuItemFont::setFontSize(30);
593598

594-
auto left = MenuItemFont::create("Left", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
599+
auto left = MenuItemFont::create("Left", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
595600
auto center = MenuItemFont::create("Center", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
596-
auto right = MenuItemFont::create("Right", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
601+
auto right = MenuItemFont::create("Right", CC_CALLBACK_1(LabelFNTMultiLineAlignment::alignmentChanged, this));
602+
597603
auto alignmentMenu = Menu::create(left, center, right, nullptr);
598604
alignmentMenu->alignItemsHorizontallyWithPadding(alignmentItemPadding);
599605

600-
center->setColor(Color3B::RED);
601-
_lastAlignmentItem = center;
602606
left->setTag(LeftAlign);
603607
center->setTag(CenterAlign);
604608
right->setTag(RightAlign);
605609

606610
// position the label on the center of the screen
607-
this->_labelShouldRetain->setPosition(Vec2(size.width/2, size.height/2));
611+
this->_label->setPosition(Vec2(size.width/2, size.height/2));
608612

609-
this->_arrowsBarShouldRetain->setVisible(false);
613+
this->_arrowsBar->setVisible(false);
610614

611615
float arrowsWidth = (ArrowsMax - ArrowsMin) * size.width;
612-
this->_arrowsBarShouldRetain->setScaleX(arrowsWidth / this->_arrowsBarShouldRetain->getContentSize().width);
613-
this->_arrowsBarShouldRetain->setPosition(Vec2(((ArrowsMax + ArrowsMin) / 2) * size.width, this->_labelShouldRetain->getPosition().y));
614-
615-
this->snapArrowsToEdge();
616+
this->_arrowsBar->setScaleX(arrowsWidth / this->_arrowsBar->getContentSize().width);
617+
this->_arrowsBar->setPosition(Vec2(((ArrowsMax + ArrowsMin) / 2) * size.width, this->_label->getPosition().y));
616618

617619
stringMenu->setPosition(Vec2(size.width/2, size.height - menuItemPaddingCenter));
618620
alignmentMenu->setPosition(Vec2(size.width/2, menuItemPaddingCenter+15));
619621

620-
this->addChild(this->_labelShouldRetain);
621-
this->addChild(this->_arrowsBarShouldRetain);
622-
this->addChild(this->_arrowsShouldRetain);
622+
this->selectSentenceItem(longSentences);
623+
this->selectAlignmentItem(center);
624+
this->snapArrowsToEdge();
625+
626+
this->addChild(this->_label);
627+
this->addChild(this->_arrowsBar);
628+
this->addChild(this->_arrows);
623629
this->addChild(stringMenu);
624630
this->addChild(alignmentMenu);
625-
}
626631

627-
LabelFNTMultiLineAlignment::~LabelFNTMultiLineAlignment()
628-
{
629-
this->_labelShouldRetain->release();
630-
this->_arrowsBarShouldRetain->release();
631-
this->_arrowsShouldRetain->release();
632+
return true;
632633
}
633634

634635
std::string LabelFNTMultiLineAlignment::title() const
@@ -641,55 +642,85 @@ std::string LabelFNTMultiLineAlignment::subtitle() const
641642
return "";
642643
}
643644

644-
void LabelFNTMultiLineAlignment::stringChanged(cocos2d::Ref *sender)
645+
void LabelFNTMultiLineAlignment::selectAlignmentItem(cocos2d::MenuItemFont * item)
645646
{
646-
auto item = (MenuItemFont*)sender;
647-
item->setColor(Color3B::RED);
648-
this->_lastAlignmentItem->setColor(Color3B::WHITE);
647+
if (this->_lastAlignmentItem && this->_lastAlignmentItem != item)
648+
{
649+
this->_lastAlignmentItem->setColor(Color3B::WHITE);
650+
}
651+
649652
this->_lastAlignmentItem = item;
653+
item->setColor(Color3B::RED);
650654

651-
switch(item->getTag())
655+
switch (item->getTag())
652656
{
653-
case LongSentences:
654-
this->_labelShouldRetain->setString(LongSentencesExample);
657+
case LeftAlign:
658+
this->_label->setAlignment(TextHAlignment::LEFT);
655659
break;
656-
case LineBreaks:
657-
this->_labelShouldRetain->setString(LineBreaksExample);
660+
case CenterAlign:
661+
this->_label->setAlignment(TextHAlignment::CENTER);
658662
break;
659-
case Mixed:
660-
this->_labelShouldRetain->setString(MixedExample);
663+
case RightAlign:
664+
this->_label->setAlignment(TextHAlignment::RIGHT);
661665
break;
662666

663667
default:
664668
break;
665669
}
666-
667-
this->snapArrowsToEdge();
668670
}
669671

670-
void LabelFNTMultiLineAlignment::alignmentChanged(cocos2d::Ref *sender)
672+
void LabelFNTMultiLineAlignment::selectSentenceItem(cocos2d::MenuItemFont* item)
671673
{
672-
auto item = static_cast<MenuItemFont*>(sender);
674+
if (this->_lastSentenceItem && this->_lastSentenceItem != item)
675+
{
676+
this->_lastSentenceItem->setColor(Color3B::WHITE);
677+
}
678+
679+
this->_lastSentenceItem = item;
673680
item->setColor(Color3B::RED);
674-
this->_lastAlignmentItem->setColor(Color3B::WHITE);
675-
this->_lastAlignmentItem = item;
676681

677-
switch(item->getTag())
682+
auto str = this->getItemString(item);
683+
this->_label->setString(str);
684+
}
685+
686+
std::string LabelFNTMultiLineAlignment::getItemString(cocos2d::MenuItemFont* item)
687+
{
688+
std::string str;
689+
690+
switch (item->getTag())
678691
{
679-
case LeftAlign:
680-
this->_labelShouldRetain->setAlignment(TextHAlignment::LEFT);
692+
case LongSentences:
693+
str = LongSentencesExample;
681694
break;
682-
case CenterAlign:
683-
this->_labelShouldRetain->setAlignment(TextHAlignment::CENTER);
695+
case LineBreaks:
696+
str = LineBreaksExample;
684697
break;
685-
case RightAlign:
686-
this->_labelShouldRetain->setAlignment(TextHAlignment::RIGHT);
698+
case Mixed:
699+
str = MixedExample;
687700
break;
688701

689702
default:
690703
break;
691704
}
692705

706+
return str;
707+
}
708+
709+
void LabelFNTMultiLineAlignment::stringChanged(cocos2d::Ref *sender)
710+
{
711+
auto item = (MenuItemFont*)sender;
712+
713+
selectSentenceItem(item);
714+
715+
this->snapArrowsToEdge();
716+
}
717+
718+
void LabelFNTMultiLineAlignment::alignmentChanged(cocos2d::Ref *sender)
719+
{
720+
auto item = static_cast<MenuItemFont*>(sender);
721+
722+
selectAlignmentItem(item);
723+
693724
this->snapArrowsToEdge();
694725
}
695726

@@ -698,10 +729,10 @@ void LabelFNTMultiLineAlignment::onTouchesBegan(const std::vector<Touch*>& touch
698729
auto touch = touches[0];
699730
auto location = touch->getLocationInView();
700731

701-
if (this->_arrowsShouldRetain->getBoundingBox().containsPoint(location))
732+
if (this->_arrows->getBoundingBox().containsPoint(location))
702733
{
703734
_drag = true;
704-
this->_arrowsBarShouldRetain->setVisible(true);
735+
this->_arrowsBar->setVisible(true);
705736
}
706737
}
707738

@@ -710,7 +741,7 @@ void LabelFNTMultiLineAlignment::onTouchesEnded(const std::vector<Touch*>& touch
710741
_drag = false;
711742
this->snapArrowsToEdge();
712743

713-
this->_arrowsBarShouldRetain->setVisible(false);
744+
this->_arrowsBar->setVisible(false);
714745
}
715746

716747
void LabelFNTMultiLineAlignment::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
@@ -725,24 +756,83 @@ void LabelFNTMultiLineAlignment::onTouchesMoved(const std::vector<Touch*>& touch
725756

726757
auto winSize = Director::getInstance()->getWinSize();
727758

728-
this->_arrowsShouldRetain->setPosition(Vec2(MAX(MIN(location.x, ArrowsMax*winSize.width), ArrowsMin*winSize.width),
729-
this->_arrowsShouldRetain->getPosition().y));
759+
this->_arrows->setPosition(Vec2(MAX(MIN(location.x, ArrowsMax*winSize.width), ArrowsMin*winSize.width),
760+
this->_arrows->getPosition().y));
730761

731-
float labelWidth = fabs(this->_arrowsShouldRetain->getPosition().x - this->_labelShouldRetain->getPosition().x) * 2;
762+
float labelWidth = fabs(this->_arrows->getPosition().x - this->_label->getPosition().x) * 2;
732763

733-
this->_labelShouldRetain->setMaxLineWidth(labelWidth);
764+
this->_label->setMaxLineWidth(labelWidth);
734765
}
735766

736767
void LabelFNTMultiLineAlignment::snapArrowsToEdge()
737768
{
738-
this->_arrowsShouldRetain->setPosition(Vec2(this->_labelShouldRetain->getPosition().x + this->_labelShouldRetain->getContentSize().width/2,
739-
this->_labelShouldRetain->getPosition().y));
769+
this->_arrows->setPosition(Vec2(this->_label->getPosition().x + this->_label->getContentSize().width/2,
770+
this->_label->getPosition().y));
771+
}
772+
773+
/// LabelFNTMultiLineAlignmentUNICODE
774+
775+
bool LabelFNTMultiLineAlignmentUNICODE::init()
776+
{
777+
if (!LabelFNTMultiLineAlignment::init())
778+
{
779+
return false;
780+
}
781+
782+
this->_menuItems[0]->setString("French");
783+
this->_menuItems[1]->setString("Spanish");
784+
this->_menuItems[2]->setString("Ukrainian");
785+
786+
auto ttfConfig = this->_label->getTTFConfig();
787+
ttfConfig.fontSize = 20;
788+
ttfConfig.fontFilePath = "fonts/tahoma.ttf";
789+
this->_label->setTTFConfig(ttfConfig);
790+
791+
this->selectSentenceItem(this->_menuItems[0]);
792+
this->snapArrowsToEdge();
793+
794+
return true;
795+
}
796+
797+
std::string LabelFNTMultiLineAlignmentUNICODE::title() const
798+
{
799+
return "";
800+
}
801+
802+
std::string LabelFNTMultiLineAlignmentUNICODE::subtitle() const
803+
{
804+
return "";
805+
}
806+
807+
std::string LabelFNTMultiLineAlignmentUNICODE::getItemString(cocos2d::MenuItemFont* item)
808+
{
809+
std::string str;
810+
811+
auto strings = FileUtils::getInstance()->getValueMapFromFile("strings/LabelFNTMultiLineAlignmentUNICODE.xml");
812+
813+
switch (item->getTag())
814+
{
815+
case LongSentences:
816+
str = strings["french"].asString();
817+
break;
818+
case LineBreaks:
819+
str = strings["spanish"].asString();
820+
break;
821+
case Mixed:
822+
str = strings["ukrainian"].asString();
823+
break;
824+
825+
default:
826+
break;
827+
}
828+
829+
return str;
740830
}
741831

742832
/// BMFontUnicodeNew
743833
LabelFNTUNICODELanguages::LabelFNTUNICODELanguages()
744834
{
745-
auto strings = FileUtils::getInstance()->getValueMapFromFile("fonts/strings.xml");
835+
auto strings = FileUtils::getInstance()->getValueMapFromFile("strings/LabelFNTUNICODELanguages.xml");
746836
std::string chinese = strings["chinese1"].asString();
747837
std::string russian = strings["russian"].asString();
748838
std::string spanish = strings["spanish"].asString();

tests/cpp-tests/Classes/LabelTest/LabelTestNew.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ class LabelFNTMultiLineAlignment : public AtlasDemoNew
142142
public:
143143
CREATE_FUNC(LabelFNTMultiLineAlignment);
144144

145-
LabelFNTMultiLineAlignment();
146-
~LabelFNTMultiLineAlignment();
145+
virtual bool init() override;
147146
void snapArrowsToEdge();
148147
virtual std::string title() const override;
149148
virtual std::string subtitle() const override;
@@ -152,13 +151,30 @@ class LabelFNTMultiLineAlignment : public AtlasDemoNew
152151
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
153152
void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
154153
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
154+
void selectAlignmentItem(cocos2d::MenuItemFont* item);
155+
void selectSentenceItem(cocos2d::MenuItemFont* item);
156+
virtual std::string getItemString(cocos2d::MenuItemFont* item);
155157

158+
protected:
159+
cocos2d::Label* _label = nullptr;
160+
cocos2d::Sprite* _arrowsBar = nullptr;
161+
cocos2d::Sprite* _arrows = nullptr;
162+
std::vector<cocos2d::MenuItemFont*> _menuItems;
163+
cocos2d::MenuItemFont *_lastSentenceItem = nullptr;
164+
cocos2d::MenuItemFont *_lastAlignmentItem = nullptr;
165+
bool _drag = false;
166+
};
167+
168+
class LabelFNTMultiLineAlignmentUNICODE : public LabelFNTMultiLineAlignment
169+
{
156170
public:
157-
cocos2d::Label* _labelShouldRetain;
158-
cocos2d::Sprite* _arrowsBarShouldRetain;
159-
cocos2d::Sprite* _arrowsShouldRetain;
160-
cocos2d::MenuItemFont *_lastSentenceItem, *_lastAlignmentItem;
161-
bool _drag;
171+
CREATE_FUNC(LabelFNTMultiLineAlignmentUNICODE);
172+
173+
bool init() override;
174+
175+
virtual std::string title() const override;
176+
virtual std::string subtitle() const override;
177+
virtual std::string getItemString(cocos2d::MenuItemFont* item) override;
162178
};
163179

164180
class LabelFNTUNICODELanguages : public AtlasDemoNew
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>french</key>
6+
<string>Loin, très loin, au delà des monts Mots, à mille lieues des pays Voyellie et Consonnia, demeurent les Bolos Bolos</string>
7+
<key>spanish</key>
8+
<string>Muy lejos, más allá de las montañas de palabras, alejados de los países de las vocales y las consonantes, viven</string>
9+
<key>ukrainian</key>
10+
<string>Далеко-далеко за словесними горами в країні голосних і приголосних живуть рибні тексти. Далеко від усіх живуть вони в буквених будинках</string>
11+
</dict>
12+
</plist>

0 commit comments

Comments
 (0)