首页 > 其他 > 详细

cocos2d-x之利用富文本控件遍历xml

时间:2016-03-13 00:36:01      阅读:251      评论:0      收藏:0      [点我收藏+]

1.

#ifndef SuperRichText_hpp

#define SuperRichText_hpp

 

#include <stdio.h>

#include "cocos2d.h"

#include "ui/UIRichText.h"

#include "tinyxml2/tinyxml2.h"

 

USING_NS_CC;

 

struct FontInfo{

    std::string fontName;

    float fontSize;

    Color3B color;

    GLubyte opacity;

};

 

class SuperRichText:public ui::Widget

{

protected:

    std::vector<FontInfo> _fontList;

    std::vector<ui::RichText*> _lineList;

    ui::RichText* _line;

public:

    SuperRichText();

    virtual ~SuperRichText();

    CREATE_FUNC(SuperRichText);

    virtual void renderHtml(const char* html);

    virtual void renderNode(tinyxml2::XMLNode* node);

    void addNewLine();

    void updateLine();

};

 

#endif /* SuperRichText_hpp */

 

#include "SuperRichText.hpp"

 

SuperRichText::SuperRichText(){

    FontInfo defaultFont;

    defaultFont.color=Color3B(255,255,255);

    defaultFont.fontSize=24.0f;

    defaultFont.opacity=255;

    defaultFont.fontName="";

    

    _fontList.push_back(defaultFont);

    

    addNewLine();

}

 

SuperRichText::~SuperRichText(){

    this->removeAllChildren();

}

 

void SuperRichText::renderHtml(const char *html){

    tinyxml2::XMLDocument xml;

    xml.Parse(html);

    renderNode(xml.FirstChild());

    updateLine();

}

 

void SuperRichText::renderNode(tinyxml2::XMLNode *node){

    while (node!=nullptr) {

        if (node->ToText()) {

            CCLOG("文本信息:%s",node->ToText()->Value());

        }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("字体标签");

                renderNode(n->FirstChild());//继续渲染子集

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

                CCLOG("图片标签");

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

                CCLOG("换行标签");

            }

        }

        node=node->NextSibling();

    }

}

 

void SuperRichText::addNewLine(){

    _line=ui::RichText::create();

    _lineList.push_back(_line);

    

    addChild(_line);

}

 

void SuperRichText::updateLine(){

    

}

2.

bool HelloWorld::init()

{

    if ( !Layer::init() )

    {

        return false;

    }

    

    Size visibleSize = Director::getInstance()->getVisibleSize();

    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    

    auto richText=SuperRichText::create();

    

    richText->setPosition(visibleSize/2);

    

    richText->renderHtml("\

<font color=‘ffffff‘ size=‘60‘ opacity=‘255‘>\

你好<img src=‘CloseNormal.png‘/>daochong\

<br/>\

</font>");

    

    return true;

}

 

cocos2d-x之利用富文本控件遍历xml

原文:http://www.cnblogs.com/daochong/p/5270889.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!