cocos2d-x之利用富文本控件遍历xml
2016-03-12 23:11
477 查看
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;
}
#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;
}
相关文章推荐
- Cocos2dx制作2048(2.数字方块的创建)
- Cocos2d-x 2048游戏教程 第一部分
- cocos2d-x之Box2d初试
- cocos2d-x 菜鸟实习生学习篇(二)helloworld完全注释
- cocos2dx 3.x 声音播放、音量调节
- Cocos2d-x 3.x版2048游戏开发
- 【cocos2dx 3.2】2048
- cocos2dx实例开发之2048(添加动画版)
- cocos2dx3.x 观察者模式
- 【potatoes游戏开发】cocos2dx3.X项目重写(八)观察者模式(中)
- cocos2dx3.X项目重写(四)添加地板,障碍物和碰撞检测
- cocos2dx3.X项目重写(七)观察者模式以及schedule引起的bug
- cocos2dx3.X项目重写(二)新的物理引擎
- cocos2dx_3.X项目重新写(一)帧动画的改变
- cocos2dx TMX地图
- Win 7平台搭建Cocos2d-x Android开发环境
- 创建cocos2d-x工程及文件夹解析
- cocos2d-html5 各平台声音播放总结
- Cocos2dx-- 3.0前后对比
- protubuf在cocos2dx的应用安装