Cocos2d-x using Rich text controls to parse Xhml tags (text labels, picture labels, wrap labels, label properties)

Source: Internet
Author: User

Post-execution effects:

Front-End use:

Backstage Superrichtext Parsing code

void Superrichtext::rendernode (Tinyxml2::xmlnode *node) {

while (NODE!=NULLPTR) {

if (Node->totext ()) {

Cclog ("Text info:%s", Node->totext ()->value ());

Auto N=node->totext ();

std::u16string text;

Stringutils::utf8toutf16 (N->value (), text);

Std::u16string::size_type pos=0;

Pos=text.find (' \ n ');

while ((Pos!=std::u16string::npos)) {

Text.erase (pos,1);

Pos=text.find (' \ n ', POS);

}

pos=0;

Pos=text.find (' \ R ');

while ((Pos!=std::u16string::npos)) {

Text.erase (pos,1);

Pos=text.find (' \ R ', POS);

}

Std::string Utf8text;

Stringutils::utf16toutf8 (Text,utf8text);

Auto Font=_fontlist[_fontlist.size ()-1];

Auto Textelement=ui::richelementtext::create (0,font.color,font.opacity,utf8text,font.fontname,font.fontsize);

_line->pushbackelement (TextElement);

}else if (node->toelement ()) {

Auto N=node->toelement ();

std::string name=n->name ();

Std::transform (Name.begin (), Name.end (), Name.begin (),:: ToUpper);

if (name== "FONT") {

Cclog ("Font label");

Auto Attr=n->firstattribute ();

FontInfo newfont=_fontlist[_fontlist.size ()-1];

while (ATTR!=NULLPTR) {

Traverse All Properties

std::string attrname=attr->name ();

Std::transform (Attrname.begin (), Attrname.end (), Attrname.begin (),:: ToUpper);

if (attrname== "face") {

Set font

Newfont.fontname=attr->value ();

}else if (attrname== "COLOR") {

Set color

NEWFONT.COLOR=CHARTOCOLOR3B (Attr->value ());

}else if (attrname== "SIZE") {

Set size

Newfont.fontsize=attr->intvalue ();

}else if (attrname== "OPACITY") {

Set opacity

Newfont.opacity=attr->intvalue ();

}

Attr=attr->next ();

}

_fontlist.push_back (NewFont);//Add a new font

Rendernode (N->firstchild ());//Continue rendering subset

_fontlist.pop_back ();//Remove new font

}else if (name== "IMG") {

Cclog ("Picture label");

Properties of the picture label

Auto Attr=n->firstattribute ();

const char *SRC;

color3b Col (255,255,255);

Glubyte opacity=255;

while (ATTR!=NULLPTR) {

Traverse All Properties

std::string attrname=attr->name ();

Std::transform (Attrname.begin (), Attrname.end (), Attrname.begin (),:: ToUpper);

if (attrname== "SRC") {

Set Picture path

Src=attr->value ();

}else if (attrname== "COLOR") {

Set color

COL=CHARTOCOLOR3B (Attr->value ());

}else if (attrname== "OPACITY") {

Set opacity

Opacity=attr->intvalue ();

}

Attr=attr->next ();

}

Auto Img=ui::richelementimage::create (0,COL,OPACITY,SRC);

_line->pushbackelement (IMG);

}else if (name== "BR") {

Cclog ("newline label");

Addnewline ();

}

}

Node=node->nextsibling ();

}

}

Cocos2d-x using Rich text controls to parse Xhml tags (text labels, picture labels, wrap labels, label properties)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.