@@ -43,6 +43,7 @@ NewLabelTests::NewLabelTests()
43
43
ADD_TEST_CASE (LabelFNTMultiLine);
44
44
ADD_TEST_CASE (LabelFNTRetina);
45
45
ADD_TEST_CASE (LabelFNTMultiLineAlignment);
46
+ ADD_TEST_CASE (LabelFNTMultiLineAlignmentUNICODE);
46
47
ADD_TEST_CASE (LabelFNTUNICODELanguages);
47
48
ADD_TEST_CASE (LabelFNTBounds);
48
49
ADD_TEST_CASE (LabelFNTandTTFEmpty);
@@ -556,8 +557,13 @@ std::string LabelFNTGlyphDesigner::subtitle() const
556
557
static float alignmentItemPadding = 50 ;
557
558
static float menuItemPaddingCenter = 50 ;
558
559
559
- LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment ()
560
+ bool LabelFNTMultiLineAlignment::init ()
560
561
{
562
+ if (!AtlasDemoNew::init ())
563
+ {
564
+ return false ;
565
+ }
566
+
561
567
auto listener = EventListenerTouchAllAtOnce::create ();
562
568
listener->onTouchesBegan = CC_CALLBACK_2 (LabelFNTMultiLineAlignment::onTouchesBegan, this );
563
569
listener->onTouchesMoved = CC_CALLBACK_2 (LabelFNTMultiLineAlignment::onTouchesMoved, this );
@@ -568,13 +574,10 @@ LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment()
568
574
auto size = Director::getInstance ()->getWinSize ();
569
575
570
576
// 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 );
573
578
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" );
578
581
579
582
MenuItemFont::setFontSize (20 );
580
583
auto longSentences = MenuItemFont::create (" Long Flowing Sentences" , CC_CALLBACK_1 (LabelFNTMultiLineAlignment::stringChanged, this ));
@@ -583,52 +586,50 @@ LabelFNTMultiLineAlignment::LabelFNTMultiLineAlignment()
583
586
auto stringMenu = Menu::create (longSentences, lineBreaks, mixed, nullptr );
584
587
stringMenu->alignItemsVertically ();
585
588
586
- longSentences->setColor (Color3B::RED);
587
- _lastSentenceItem = longSentences;
588
589
longSentences->setTag (LongSentences);
589
590
lineBreaks->setTag (LineBreaks);
590
591
mixed->setTag (Mixed);
591
592
593
+ _menuItems.push_back (longSentences);
594
+ _menuItems.push_back (lineBreaks);
595
+ _menuItems.push_back (mixed);
596
+
592
597
MenuItemFont::setFontSize (30 );
593
598
594
- auto left = MenuItemFont::create (" Left" , CC_CALLBACK_1 (LabelFNTMultiLineAlignment::alignmentChanged, this ));
599
+ auto left = MenuItemFont::create (" Left" , CC_CALLBACK_1 (LabelFNTMultiLineAlignment::alignmentChanged, this ));
595
600
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
+
597
603
auto alignmentMenu = Menu::create (left, center, right, nullptr );
598
604
alignmentMenu->alignItemsHorizontallyWithPadding (alignmentItemPadding);
599
605
600
- center->setColor (Color3B::RED);
601
- _lastAlignmentItem = center;
602
606
left->setTag (LeftAlign);
603
607
center->setTag (CenterAlign);
604
608
right->setTag (RightAlign);
605
609
606
610
// 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 ));
608
612
609
- this ->_arrowsBarShouldRetain ->setVisible (false );
613
+ this ->_arrowsBar ->setVisible (false );
610
614
611
615
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 ));
616
618
617
619
stringMenu->setPosition (Vec2 (size.width /2 , size.height - menuItemPaddingCenter));
618
620
alignmentMenu->setPosition (Vec2 (size.width /2 , menuItemPaddingCenter+15 ));
619
621
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 );
623
629
this ->addChild (stringMenu);
624
630
this ->addChild (alignmentMenu);
625
- }
626
631
627
- LabelFNTMultiLineAlignment::~LabelFNTMultiLineAlignment ()
628
- {
629
- this ->_labelShouldRetain ->release ();
630
- this ->_arrowsBarShouldRetain ->release ();
631
- this ->_arrowsShouldRetain ->release ();
632
+ return true ;
632
633
}
633
634
634
635
std::string LabelFNTMultiLineAlignment::title () const
@@ -641,55 +642,85 @@ std::string LabelFNTMultiLineAlignment::subtitle() const
641
642
return " " ;
642
643
}
643
644
644
- void LabelFNTMultiLineAlignment::stringChanged (cocos2d::Ref *sender )
645
+ void LabelFNTMultiLineAlignment::selectAlignmentItem (cocos2d::MenuItemFont * item )
645
646
{
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
+
649
652
this ->_lastAlignmentItem = item;
653
+ item->setColor (Color3B::RED);
650
654
651
- switch (item->getTag ())
655
+ switch (item->getTag ())
652
656
{
653
- case LongSentences :
654
- this ->_labelShouldRetain -> setString (LongSentencesExample );
657
+ case LeftAlign :
658
+ this ->_label -> setAlignment (TextHAlignment::LEFT );
655
659
break ;
656
- case LineBreaks :
657
- this ->_labelShouldRetain -> setString (LineBreaksExample );
660
+ case CenterAlign :
661
+ this ->_label -> setAlignment (TextHAlignment::CENTER );
658
662
break ;
659
- case Mixed :
660
- this ->_labelShouldRetain -> setString (MixedExample );
663
+ case RightAlign :
664
+ this ->_label -> setAlignment (TextHAlignment::RIGHT );
661
665
break ;
662
666
663
667
default :
664
668
break ;
665
669
}
666
-
667
- this ->snapArrowsToEdge ();
668
670
}
669
671
670
- void LabelFNTMultiLineAlignment::alignmentChanged (cocos2d::Ref *sender )
672
+ void LabelFNTMultiLineAlignment::selectSentenceItem (cocos2d::MenuItemFont* item )
671
673
{
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;
673
680
item->setColor (Color3B::RED);
674
- this ->_lastAlignmentItem ->setColor (Color3B::WHITE);
675
- this ->_lastAlignmentItem = item;
676
681
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 ())
678
691
{
679
- case LeftAlign :
680
- this -> _labelShouldRetain -> setAlignment (TextHAlignment::LEFT) ;
692
+ case LongSentences :
693
+ str = LongSentencesExample ;
681
694
break ;
682
- case CenterAlign :
683
- this -> _labelShouldRetain -> setAlignment (TextHAlignment::CENTER) ;
695
+ case LineBreaks :
696
+ str = LineBreaksExample ;
684
697
break ;
685
- case RightAlign :
686
- this -> _labelShouldRetain -> setAlignment (TextHAlignment::RIGHT) ;
698
+ case Mixed :
699
+ str = MixedExample ;
687
700
break ;
688
701
689
702
default :
690
703
break ;
691
704
}
692
705
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
+
693
724
this ->snapArrowsToEdge ();
694
725
}
695
726
@@ -698,10 +729,10 @@ void LabelFNTMultiLineAlignment::onTouchesBegan(const std::vector<Touch*>& touch
698
729
auto touch = touches[0 ];
699
730
auto location = touch->getLocationInView ();
700
731
701
- if (this ->_arrowsShouldRetain ->getBoundingBox ().containsPoint (location))
732
+ if (this ->_arrows ->getBoundingBox ().containsPoint (location))
702
733
{
703
734
_drag = true ;
704
- this ->_arrowsBarShouldRetain ->setVisible (true );
735
+ this ->_arrowsBar ->setVisible (true );
705
736
}
706
737
}
707
738
@@ -710,7 +741,7 @@ void LabelFNTMultiLineAlignment::onTouchesEnded(const std::vector<Touch*>& touch
710
741
_drag = false ;
711
742
this ->snapArrowsToEdge ();
712
743
713
- this ->_arrowsBarShouldRetain ->setVisible (false );
744
+ this ->_arrowsBar ->setVisible (false );
714
745
}
715
746
716
747
void LabelFNTMultiLineAlignment::onTouchesMoved (const std::vector<Touch*>& touches, cocos2d::Event *event)
@@ -725,24 +756,83 @@ void LabelFNTMultiLineAlignment::onTouchesMoved(const std::vector<Touch*>& touch
725
756
726
757
auto winSize = Director::getInstance ()->getWinSize ();
727
758
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 ));
730
761
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 ;
732
763
733
- this ->_labelShouldRetain ->setMaxLineWidth (labelWidth);
764
+ this ->_label ->setMaxLineWidth (labelWidth);
734
765
}
735
766
736
767
void LabelFNTMultiLineAlignment::snapArrowsToEdge ()
737
768
{
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;
740
830
}
741
831
742
832
// / BMFontUnicodeNew
743
833
LabelFNTUNICODELanguages::LabelFNTUNICODELanguages ()
744
834
{
745
- auto strings = FileUtils::getInstance ()->getValueMapFromFile (" fonts/ strings.xml" );
835
+ auto strings = FileUtils::getInstance ()->getValueMapFromFile (" strings/LabelFNTUNICODELanguages .xml" );
746
836
std::string chinese = strings[" chinese1" ].asString ();
747
837
std::string russian = strings[" russian" ].asString ();
748
838
std::string spanish = strings[" spanish" ].asString ();
0 commit comments