cocos2dx导出的json转c++代码

虽然2dx提供了CocoStudio界面编辑工具,但是他并非一个开源产品,没有提供原码修改,更做不到像vs一样的控件集成。在一个界面设计完成后,往往要把相关的界面上的东西转成相对应的原码基本都是一至的,这些动作繁琐而又没有意思看如下代码等:

_Panel = static_cast<Layout*>(extension::GUIReader::shareReader()->widgetFromJsonFile("DemoShop.ExportJson"));

this->addWidget(_Panel);

_Panel_shop_ScrollView = static_cast<ScrollView*>(Helper::seekWidgetByName(_Panel, "shop_ScrollView"));

_Panel_shop_ScrollView->addTouchEventListener(this, toucheventselector(CDemoShop::Panel_shop_ScrollView_touchEvent));

看这些代码又有一些相关类似外,无非就是把json文件解析,然后一一对到对应控件,获得对应地址等。与其在上面花费大量无用时间写这些代码。考虑做一个代码转换工作

代码如下:

// exportjson2cpp.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include <string>

#include <fstream>

#include <iostream>

#include <vector>

#include <stdlib.h>

#include <assert.h>

#include <map>

#include <stdio.h>

#include <json/json.h>

#include <dwxmlfile.h>

#include <dwhashmap.h>

#include <algorithm>

using namespace std;

using namespace DWDP;

//const TCHAR*  DWGetModulePath()

//{

//    static TCHAR szPath[DW_MAX_PATH];

//    static bool bFirstTime = true;

//    if (bFirstTime)

//    {

//#if (defined(WIN32) || defined(WIN64))

//        bFirstTime = false;

//        GetModuleFileName(NULL, (TCHAR*)szPath, sizeof(szPath));

//        TCHAR *p = _tcsrchr(szPath, _DWT(‘\\‘));

//        *p = _DWT(‘\0‘);

//        _tcscat(szPath, _DWT("\\"));

//#else

//        CHAR szTmp[DW_MAX_PATH];

//        getcwd(szTmp,sizeof(szTmp));

//        CHAR szTempPath[DW_MAX_PATH];

//        sprintf(szTempPath,"%s/",szTmp);

//        _DWTStrncpy(szPath,_DWTA2T(szTempPath),DW_MAX_PATH);

//#endif //

//    }

//    return szPath;

//}

#define CC_LOOP_DO do{

#define CC_LOOP_WHILE(condition) }while(condition);

#define CC_LOOP_WHILE_START(condition) while(condition){

#define CC_LOOP_WHILE_END };

#define CC_LOOP_FOR_START(condition) for(condition){

#define CC_LOOP_FOR_END };

#define CC_LOOP_BREAK(condition) if(condition)break;

#define CC_LOOP_BREAK_EXPRESSION(condition, Expression)
\

if(condition)\

{\

Expression;\

break;\

}

#define CC_LOOP_CONTINUE(condition) if(condition)continue;

#define CC_LOOP_RETURN(condition, Expression)
\

if(condition)\

{\

return Expression;\

}

struct SNodeProp;

typedef vector<SNodeProp*> CNodePropVec;

typedef CNodePropVec::iterator CNodePropVecItr;

struct SNodeProp

{

SNodeProp()

{

classname = "";

name = "";

objecttag =
0;

_vecUIProp.clear();

_vecGObjects.clear();

_vecComs.clear();

parent = NULL;

}

string classname;

string name;

int objecttag;

CNodePropVec _vecUIProp;

CNodePropVec _vecGObjects;

CNodePropVec _vecComs;

SNodeProp *parent;

};

FILE* pfH
= NULL;

FILE* pfCPP
= NULL;

bool bNet
= false;

bool bcsd
= false;

bool g_bUI
= false;

string g_path
= "";

string g_strClassName
= "";

bool LoadUIJson(string strFile, SNodeProp *pstProp);

bool ParseUIJsonChildren(Json::Value  childrens, SNodeProp *pstProp);

bool WriteUI(string strFile, SNodeProp *pstProp);

std::string getGUIClassName(const std::string &_classname)

{

const char* classname = _classname.c_str();

std::string convertedClassName = classname;

if (classname && strcmp(classname, "Button") == 0)

{

convertedClassName = "Button";

}

else if (classname && strcmp(classname, "CheckBox") == 0)

{

convertedClassName = "CheckBox";

}

else if (classname && strcmp(classname, "Label") == 0)

{

convertedClassName = "Label";

}

else if (classname && strcmp(classname, "LabelAtlas") == 0)

{

convertedClassName = "LabelAtlas";

}

else if (classname && strcmp(classname, "LoadingBar") == 0)

{

convertedClassName = "LoadingBar";

}

else if (classname && strcmp(classname, "ScrollView") == 0)

{

convertedClassName = "ScrollView";

}

else if (classname && strcmp(classname, "TextArea") == 0)

{

convertedClassName = "Label";

}

else if (classname && strcmp(classname, "TextButton") == 0)

{

convertedClassName = "Button";

}

else if (classname && strcmp(classname, "TextField") == 0)

{

convertedClassName = "TextField";

}

else if (classname && strcmp(classname, "ImageView") == 0)

{

convertedClassName = "ImageView";

}

else if (classname && strcmp(classname, "Panel") == 0)

{

convertedClassName = "Layout";

}

else if (classname && strcmp(classname, "Slider") == 0)

{

convertedClassName = "Slider";

}

else if (classname && strcmp(classname, "LabelBMFont") == 0)

{

convertedClassName = "LabelBMFont";

}

else if (classname && strcmp(classname, "DragPanel") == 0)

{

convertedClassName = "ScrollView";

}

else if(classname && strcmp(classname, "DragPanel") == 0 )

{

convertedClassName = "Node";

}

else if(classname && strcmp(classname, "SimpleAudio") == 0)

{

convertedClassName = "Node";

}

else if(classname && strcmp(classname, "SingleNode") == 0)

{

convertedClassName = "Node";

}

return convertedClassName;

}

bool LoadUIJson(string strFile, SNodeProp *pstProp)

{

ifstream file(strFile.c_str());

if(!file.is_open())

{

printf("Parse error");

return false;

}

Json::Value  Panel;

Json::Reader reader;

if(!reader.parse(file, Panel, false))

{

printf("Parse error");

return false;

}

if(!Panel["widgetTree"].isObject())

{

printf("Parse error");

return false;

}

Json::Value widgetTree = Panel["widgetTree"];

Json::Value options = widgetTree["options"];

//解析返回的状态码

if(!options["classname"].isString())

{

printf("Parse error");

return false;

}

if(!options["name"].isString())

{

printf("Parse error");

return false;

}

if(!widgetTree["children"].isArray())

{

printf("Parse error");

return false;

}

pstProp->classname = options["classname"].asString();

pstProp->name = options["name"].asString();

ParseUIJsonChildren(widgetTree["children"], pstProp);

pstProp->classname = getGUIClassName(pstProp->classname);

return true;

}

bool ParseUIJsonChildren(Json::Value  childrens, SNodeProp *pstProp)

{

if(!childrens.isArray())

{

printf("Parse error");

return false;

}

for(int idx = 0; idx < childrens.size(); idx++)

{

Json::Value  children = childrens[idx];

if(!children.isObject())

{

printf("Parse error");

return false;

}

//解析返回的状态码

if(!children["classname"].isString())

{

printf("Parse error");

return false;

}

Json::Value options = children["options"];

SNodeProp *_pstProp = new SNodeProp;;

_pstProp->parent = pstProp;

_pstProp->classname = options["classname"].asString();

_pstProp->name = options["name"].asString();

ParseUIJsonChildren(children["children"], _pstProp);

_pstProp->classname = getGUIClassName(_pstProp->classname);

pstProp->_vecUIProp.push_back(_pstProp);

}

return true;

}

bool WritePropFun2H(string parent, SNodeProp  *pstProp)

{

CC_LOOP_DO

CC_LOOP_BREAK(parent == "")

if(pstProp->classname == "Button")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "CheckBox")

{

fprintf(pfH, " void %s_%s_selected(Ref* pSender, CheckBox::EventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "ImageView")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "LabelAtlas")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "LabelBMFont")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "LoadingBar")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "Slider")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "Label")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "TextField")

{

//默认

fprintf(pfH, " void %s_%s_textFieldEvent(Ref* pSender, TextField::EventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "Layout")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "ScrollView")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "ListView")

{

fprintf(pfH, " void %s_%s_selectedItemEvent(Ref* pSender, ListView::EventType type);\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfH, " void %s_%s_selectedItemEventScrollView(Ref* pSender, ui::ScrollView::EventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "PageView")

{

fprintf(pfH, " void %s_%s_pageViewEvent(Ref *pSender, PageView::EventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "CustomImageView")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "CustomParticleWidget")

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

else

{

//默认

fprintf(pfH, " void %s_%s_touchEvent(Ref* pSender, TouchEventType type);\n", parent.c_str(), pstProp->name.c_str());

}

CC_LOOP_WHILE(0)

for(CNodePropVecItr itr = pstProp->_vecUIProp.begin(); itr != pstProp->_vecUIProp.end(); itr++)

{

if (parent == "")

{

WritePropFun2H(pstProp->name, *itr);

}

else

{

WritePropFun2H(parent + "_" + pstProp->name, *itr);

}

}

for(CNodePropVecItr itr = pstProp->_vecGObjects.begin(); itr != pstProp->_vecGObjects.end(); itr++)

{

if (parent == "")

{

WritePropFun2H(pstProp->name, *itr);

}

else

{

WritePropFun2H(parent + "_" + pstProp->name, *itr);

}

}

for(CNodePropVecItr itr = pstProp->_vecComs.begin(); itr != pstProp->_vecComs.end(); itr++)

{

if (parent == "")

{

WritePropFun2H(pstProp->name, *itr);

}

else

{

WritePropFun2H(parent + "_" + pstProp->name, *itr);

}

}

return true;

}

bool WriteProp2H(string parent, SNodeProp  *pstProp)

{

if (parent == "")

{

fprintf(pfH, " %-25s*
_%s;\n", pstProp->classname.c_str(), pstProp->name.c_str());

}

else

{

fprintf(pfH, " %-25s*
_%s_%s;\n", pstProp->classname.c_str(), parent.c_str(), pstProp->name.c_str());

}

for(CNodePropVecItr itr = pstProp->_vecUIProp.begin(); itr != pstProp->_vecUIProp.end(); itr++)

{

if (parent == "")

{

WriteProp2H(pstProp->name, *itr);

}

else

{

WriteProp2H(parent + "_" + pstProp->name, *itr);

}

}

for(CNodePropVecItr itr = pstProp->_vecComs.begin(); itr != pstProp->_vecComs.end(); itr++)

{

if (parent == "")

{

WriteProp2H(pstProp->name, *itr);

}

else

{

WriteProp2H(parent + "_" + pstProp->name, *itr);

}

}

for(CNodePropVecItr itr = pstProp->_vecGObjects.begin(); itr != pstProp->_vecGObjects.end(); itr++)

{

if (parent == "")

{

WriteProp2H(pstProp->name, *itr);

}

else

{

WriteProp2H(parent + "_" + pstProp->name, *itr);

}

}

return true;

}

bool WritePropInit2Cpp(string parent, SNodeProp *pstProp)

{

static string ps = "";

if (parent == "")

{

fprintf(pfCPP, "%s_%s(nullptr)\n", ps.c_str(), pstProp->name.c_str());

}

else

{

fprintf(pfCPP, "%s_%s_%s(nullptr)\n", ps.c_str(), parent.c_str(), pstProp->name.c_str());

}

ps = ",";

for(CNodePropVecItr itr = pstProp->_vecUIProp.begin(); itr != pstProp->_vecUIProp.end(); itr++)

{

if (parent == "")

{

WritePropInit2Cpp(pstProp->name, *itr);

}

else

{

WritePropInit2Cpp(parent + "_" + pstProp->name, *itr);

}

}

for(CNodePropVecItr itr = pstProp->_vecComs.begin(); itr != pstProp->_vecComs.end(); itr++)

{

if (parent == "")

{

WritePropInit2Cpp(pstProp->name, *itr);

}

else

{

WritePropInit2Cpp(parent + "_" + pstProp->name, *itr);

}

}

for(CNodePropVecItr itr = pstProp->_vecGObjects.begin(); itr != pstProp->_vecGObjects.end(); itr++)

{

if (parent == "")

{

WritePropInit2Cpp(pstProp->name, *itr);

}

else

{

WritePropInit2Cpp(parent + "_" + pstProp->name, *itr);

}

}

return true;

}

bool Write2InitCpp(string parent, SNodeProp *pstProp, bool bUI)

{

CC_LOOP_DO

CC_LOOP_BREAK(parent == "")

//////////////////////////////////////////////////////////////////////////

if(pstProp->classname == "Button")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

//默认

fprintf(pfCPP, " _%s_%s->addTouchEventListener(this, toucheventselector(%s::%s_%s_touchEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "CheckBox")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

fprintf(pfCPP, " _%s_%s->addEventListener(this, toucheventselector(%s::%s_%s_selected));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "ImageView")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

//默认

fprintf(pfCPP, " _%s_%s->addTouchEventListener(this, toucheventselector(%s::%s_%s_touchEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "LabelAtlas")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "LabelBMFont")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "LoadingBar")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

fprintf(pfCPP, " _%s_%s->setPercent(0);\n", parent.c_str(), pstProp->name.c_str());

}

else if(pstProp->classname == "Slider")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

//默认

fprintf(pfCPP, " _%s_%s->addTouchEventListener(this, toucheventselector(%s::%s_%s_touchEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "Label")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "TextField")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

//默认

fprintf(pfCPP, " _%s_%s->addEventListener(this, toucheventselector(%s::%s_%s_textFieldEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "Layout")

{

if (NULL != pstProp->parent && pstProp->parent->classname == "CCComRender")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(_%s->getNode());\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

if (NULL != pstProp->parent && pstProp->parent->classname == "UILayer")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(_%s->getWidgetByName(\"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

//默认

fprintf(pfCPP, " _%s_%s->addTouchEventListener(this, toucheventselector(%s::%s_%s_touchEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "ScrollView")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

//默认

fprintf(pfCPP, " _%s_%s->addTouchEventListener(this, toucheventselector(%s::%s_%s_touchEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "ListView")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

fprintf(pfCPP, " _%s_%s->addEventListener((ui::ListView::ccListViewCallback)this, toucheventselector(%s::%s_%s_selectedItemEvent));\n", parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

fprintf(pfCPP, " _%s_%s->addEventListener((ui::ListView::ccScrollViewCallback)this, toucheventselector(%s::%s_%s_selectedItemEventScrollView,this));\n", parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "PageView")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

fprintf(pfCPP, " _%s_%s->addEventListener(this, toucheventselector(%s::%s_%s_pageViewEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "CustomImageView")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

//默认

fprintf(pfCPP, " _%s_%s->addTouchEventListener(this, toucheventselector(%s::%s_%s_touchEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "CustomParticleWidget")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

//默认

fprintf(pfCPP, " _%s_%s->addTouchEventListener(this, toucheventselector(%s::%s_%s_touchEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "Sprite")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "Node")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

//////////////////////////////////////////////////////////////////////////

else if(pstProp->classname == "CCComAudio")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(_%s->getComponent(\"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "UILayer")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(dynamic_cast<CCComRender*>(_%s->getComponent(\"%s\"))->getNode());\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if (pstProp->classname == "CCSprite" ||

pstProp->classname == "CCTMXTiledMap" ||

pstProp->classname == "CCParticleSystemQuad" ||

pstProp->classname == "CCArmature" ||

pstProp->classname == "GUIComponent")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(dynamic_cast<CCComRender*>(_%s->getComponent(\"%s\"))->getNode());\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

else if(pstProp->classname == "CCNode")

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekNodeByTag(_%s, %d));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->objecttag);

}

else

{

fprintf(pfCPP, " _%s_%s = dynamic_cast<%s*>(Helper::seekWidgetByName(_%s, \"%s\"));\n",

parent.c_str(),

pstProp->name.c_str(),

pstProp->classname.c_str(),

parent.c_str(),

pstProp->name.c_str());

//默认

fprintf(pfCPP, " _%s_%s->addTouchEventListener(this, toucheventselector(%s::%s_%s_touchEvent));\n",

parent.c_str(),

pstProp->name.c_str(),

g_strClassName.c_str(),

parent.c_str(),

pstProp->name.c_str());

}

fprintf(pfCPP, "\n");

CC_LOOP_WHILE(0)

for(CNodePropVecItr itr = pstProp->_vecUIProp.begin(); itr != pstProp->_vecUIProp.end(); itr++)

{

if (parent == "")

{

Write2InitCpp(pstProp->name, *itr, true);

}

else

{

Write2InitCpp(parent + "_" + pstProp->name, *itr, true);

}

}

for(CNodePropVecItr itr = pstProp->_vecComs.begin(); itr != pstProp->_vecComs.end(); itr++)

{

if (parent == "")

{

Write2InitCpp(pstProp->name, *itr, false);

}

else

{

Write2InitCpp(parent + "_" + pstProp->name, *itr, false);

}

}

for(CNodePropVecItr itr = pstProp->_vecGObjects.begin(); itr != pstProp->_vecGObjects.end(); itr++)

{

if (parent == "")

{

Write2InitCpp(pstProp->name, *itr, false);

}

else

{

Write2InitCpp(parent + "_" + pstProp->name, *itr, false);

}

}

return true;

}

bool WritePropFun2Cpp(string parent, SNodeProp *pstProp)

{

CC_LOOP_DO

CC_LOOP_BREAK(parent == "")

//////////////////////////////////////////////////////////////////////////

if(pstProp->classname == "Button")

{

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "CheckBox")

{

fprintf(pfCPP, "void %s::%s_%s_selected(Ref* pSender, CheckBox::EventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case CheckBox::EventType::UNSELECTED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case CheckBox::EventType::SELECTED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "ImageView")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "LabelAtlas")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "LabelBMFont")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "LoadingBar")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "Slider")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "Label")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "TextField")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_textFieldEvent(Ref* pSender, TextField::EventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TextField::EventType::ATTACH_WITH_IME:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TextField::EventType::DETACH_WITH_IME:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TextField::EventType::INSERT_TEXT:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TextField::EventType::DELETE_BACKWARD:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "Layout")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "ScrollView")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "ListView")

{

fprintf(pfCPP, "void %s::%s_%s_selectedItemEvent(Ref* pSender, ListView::EventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_START:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_END:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

fprintf(pfCPP, "void %s::%s_%s_selectedItemEventScrollView(Ref* pSender, ui::ScrollView::EventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s\n", pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case ui::ScrollView::EventType::SCROLL_TO_BOTTOM:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case ui::ScrollView::EventType::SCROLL_TO_TOP:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "PageView")

{

fprintf(pfCPP, "void %s::%s_%s_pageViewEvent(Ref* pSender, PageView::EventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case PageView::EventType::TURNING:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "CustomImageView")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else if(pstProp->classname == "CustomParticleWidget")

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

else

{

//默认

fprintf(pfCPP, "void %s::%s_%s_touchEvent(Ref* pSender, TouchEventType type)\n", g_strClassName.c_str(), parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " // _%s_%s\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " CCLOG(\"_%s_%s\");\n", parent.c_str(), pstProp->name.c_str());

fprintf(pfCPP, " switch (type)\n");

fprintf(pfCPP, " {\n");

fprintf(pfCPP, " case TOUCH_EVENT_BEGAN:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_MOVED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_ENDED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " case TOUCH_EVENT_CANCELED:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " default:\n");

fprintf(pfCPP, " break;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, "}\n\n");

}

fprintf(pfCPP, "\n");

CC_LOOP_WHILE(0)

for(CNodePropVecItr itr = pstProp->_vecUIProp.begin(); itr != pstProp->_vecUIProp.end(); itr++)

{

if (parent == "")

{

WritePropFun2Cpp(pstProp->name, *itr);

}

else

{

WritePropFun2Cpp(parent + "_" + pstProp->name, *itr);

}

}

for(CNodePropVecItr itr = pstProp->_vecGObjects.begin(); itr != pstProp->_vecGObjects.end(); itr++)

{

if (parent == "")

{

WritePropFun2Cpp(pstProp->name, *itr);

}

else

{

WritePropFun2Cpp(parent + "_" + pstProp->name, *itr);

}

}

for(CNodePropVecItr itr = pstProp->_vecComs.begin(); itr != pstProp->_vecComs.end(); itr++)

{

if (parent == "")

{

WritePropFun2Cpp(pstProp->name, *itr);

}

else

{

WritePropFun2Cpp(parent + "_" + pstProp->name, *itr);

}

}

return true;

}

bool ParseSceneJsonChildren(Json::Value  childrens,SNodeProp *pstProp);

bool LoadSceneJson(string strFile, SNodeProp *pstProp)

{

ifstream file(strFile.c_str());

assert(file.is_open());

Json::Value  Root;

Json::Reader reader;

if(!reader.parse(file, Root, false))

{

printf("Parse error");

return false;

}

ParseSceneJsonChildren(Root, pstProp);

return true;

}

bool ParseSceneJsonChildren(Json::Value  childrens, SNodeProp *pstProp)

{

pstProp->classname = childrens["classname"].asString();

pstProp->name = childrens["name"].asString();

pstProp->objecttag = childrens["objecttag"].asInt();

if(strcmp(pstProp->classname.c_str(), "CCNode") != 0)

{

return true;

}

if (pstProp->name == "")

{

pstProp->name = pstProp->classname;

}

if(childrens["components"].isArray())

{

for(int idx = 0; idx < childrens["components"].size(); idx++)

{

Json::Value  children = childrens["components"][idx];

if(!children.isObject())

{

printf("Parse error");

return false;

}

SNodeProp *_pstProp = new SNodeProp;;

_pstProp->parent = pstProp;

_pstProp->classname = children["classname"].asString();

_pstProp->name = children["name"].asString();

if (_pstProp->classname == "CCSprite" ||

_pstProp->classname == "CCTMXTiledMap" ||

_pstProp->classname == "CCParticleSystemQuad" ||

_pstProp->classname == "CCArmature")

{

//////////////////////////////////////////////////////////////////////////

}

else if (_pstProp->classname == "GUIComponent")

{

SNodeProp *pstProp = new SNodeProp;

pstProp->parent = _pstProp;

Json::Value fileData = children["fileData"];

//解析返回的状态码

if(fileData["path"].isString())

{

string dirpath = g_path;

string _path = fileData["path"].asString();

int num = 10;

while(num--)

{

string path = dirpath + _path;

if (LoadUIJson(path, pstProp))

{

break;

}

printf("%s not found \n", path.c_str());

size_t pos = dirpath.find_last_of(‘\\‘);

dirpath = dirpath.substr(0, pos);

pos = dirpath.find_last_of(‘\\‘);

dirpath = dirpath.substr(0, pos + 1);

}

_pstProp->_vecUIProp.push_back(pstProp);

}

_pstProp->classname = "UILayer";

}

else if (_pstProp->classname == "CCComAudio" ||

_pstProp->classname == "CCBackgroundAudio")

{

_pstProp->classname = "CCComAudio";

}

else if (_pstProp->classname == "CCComController")

{

_pstProp->classname = "CCComController";

}

else if (_pstProp->classname == "CCComAttribute")

{

_pstProp->classname = "CCComAttribute";

}

//else if (_pstProp->classname == "CCScene")

//{

//    _pstProp->classname = "CCScene";

//}

else

{

continue;

}

pstProp->_vecComs.push_back(_pstProp);

}

}

if(childrens["gameobjects"].isArray())

{

for(int idx = 0; idx < childrens["gameobjects"].size(); idx++)

{

Json::Value  children = childrens["gameobjects"][idx];

if(!children.isObject())

{

printf("Parse error");

return false;

}

SNodeProp *_pstProp = new SNodeProp;;

_pstProp->parent = pstProp;

ParseSceneJsonChildren(children, _pstProp);

pstProp->_vecGObjects.push_back(_pstProp);

}

}

return true;

}

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////

bool Write2H(string strFile, SNodeProp *pstProp)

{

int dwPos = strFile.rfind(‘\\‘);

string strFilePath = strFile.substr(0, dwPos + 1);

string strFileName = strFile.substr(dwPos + 1, strFile.size());

dwPos = strFileName.rfind(‘.‘);

string strBassName = strFileName.substr(0, dwPos);

string strH = strFilePath + strBassName + ".h";

pfH = fopen(strH.c_str(), "w");

if(NULL == pfH)

{

return false;

}

string strHEAD_LINE = "__" + strBassName + "_" + "H" + "__";

transform(strHEAD_LINE.begin(), strHEAD_LINE.end(), strHEAD_LINE.begin(), ::toupper);

g_strClassName = "C" + strBassName;

//////////////////////////////////////////////////////////////////////////

fprintf(pfH, "#ifndef %s\n#define %s\n\n", strHEAD_LINE.c_str(), strHEAD_LINE.c_str());

fprintf(pfH, "#include \"cocos2d.h\"\n");

//2.2.6

fprintf(pfH, "#include \"cocos-ext.h\"\n");

//end 2.2.6

//2.2.6

//fprintf(pfH, "#include \"ui/CocosGUI.h\"\n");

//fprintf(pfH, "#include \"cocostudio/CocoStudio.h\"\n");

//end 2.2.6

if(bNet)

{

fprintf(pfH, "#include \"MGInclude.h\"\n");

fprintf(pfH, "#include \"MGNetConnection.h\"\n");

fprintf(pfH, "#include \"MGNetCmdHandler.h\"\n\n\n");

}

fprintf(pfH, "USING_NS_CC;\n");

//fprintf(pfH, "USING_NS_CC_EXT;\n");

fprintf(pfH, "using namespace ui;\n");

//fprintf(pfH, "using namespace MyGame;\n");

fprintf(pfH, "using namespace extension;\n\n\n");

fprintf(pfH, "typedef  UILayer Layer;\n");

fprintf(pfH, "typedef  CCObject Ref;\n");

fprintf(pfH, "typedef  CCScene Scene;\n");

fprintf(pfH, "typedef  UIHelper Helper;\n\n\n");

if(bNet)

{

fprintf(pfH, "class %s : public Layer, public MGNetCmdHandler\n", g_strClassName.c_str());

}

else

{

fprintf(pfH, "class %s : public Layer\n", g_strClassName.c_str());

}

fprintf(pfH, "{\n");

fprintf(pfH, "public:\n");

fprintf(pfH, " %s();\n", g_strClassName.c_str());

fprintf(pfH, " ~%s();\n", g_strClassName.c_str());

fprintf(pfH, " bool init();\n");

WritePropFun2H("", pstProp);

if(bNet)

{

fprintf(pfH, "protected:\n");

fprintf(pfH, " virtual  bool canProcess(ActionCode_t code);\n");

fprintf(pfH, " virtual  void proccessDatas(MGNetDataReader * reader);\n");

fprintf(pfH, " virtual  void netSendDatasFailed(MGNetDataWriter * writer);\n");

}

fprintf(pfH, "public:\n");

fprintf(pfH, " static Scene* createScene() \n");

fprintf(pfH, " { \n");

fprintf(pfH, " Scene* pScene = Scene::create(); \n");

fprintf(pfH, " %s* uiLayer = new (std::nothrow) %s(); \n", g_strClassName.c_str(), g_strClassName.c_str());

fprintf(pfH, " if (uiLayer && uiLayer->init()) \n");

fprintf(pfH, " { \n");

fprintf(pfH, " uiLayer->autorelease(); \n");

fprintf(pfH, " pScene->addChild(uiLayer); \n");

fprintf(pfH, " } \n");

fprintf(pfH, " else \n");

fprintf(pfH, " { \n");

fprintf(pfH, " CC_SAFE_DELETE(uiLayer); \n");

fprintf(pfH, " } \n");

fprintf(pfH, " return pScene; \n");

fprintf(pfH, " }\n");

fprintf(pfH, "protected:\n");

WriteProp2H("", pstProp);

fprintf(pfH, "};\n\n\n");

fprintf(pfH, "#endif /* defined(%s) */", strHEAD_LINE.c_str());

return true;

}

bool Write2Cpp(string strFile, SNodeProp *pstProp)

{

int dwPos = strFile.rfind(‘\\‘);

string strFilePath = strFile.substr(0, dwPos + 1);

string strFileName = strFile.substr(dwPos + 1, strFile.size());

dwPos = strFileName.rfind(‘.‘);

string strBassName = strFileName.substr(0, dwPos);

string strCPP = strFilePath + strBassName + ".cpp";

pfCPP = fopen(strCPP.c_str(), "w");

if(NULL == pfCPP)

{

return false;

}

//////////////////////////////////////////////////////////////////////////

fprintf(pfCPP, "#include \"%s.h\"\n", strBassName.c_str());

//fprintf(pfCPP, "#include \"extensions/cocos-ext.h\"\n\n\n");

fprintf(pfCPP, "%s::%s()\n", g_strClassName.c_str(), g_strClassName.c_str());

fprintf(pfCPP, ":");

WritePropInit2Cpp("", pstProp);

fprintf(pfCPP, "{\n");

if(bNet)

{

fprintf(pfCPP, " MGNetConnection::sharedConnection()->registerCmdHandler(this);\n");

}

fprintf(pfCPP, "}\n\n");

fprintf(pfCPP, "%s::~%s()\n", g_strClassName.c_str(), g_strClassName.c_str());

fprintf(pfCPP, "{\n");

if(bNet)

{

fprintf(pfCPP, " MGNetConnection::sharedConnection()->unregisterCmdHandler(this);\n");

}

fprintf(pfCPP, "}\n\n");

fprintf(pfCPP, "bool %s::init()\n", g_strClassName.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " if (Layer::init())\n");

fprintf(pfCPP, " {\n");

if(bcsd)

{

if (g_bUI)

{

fprintf(pfCPP, " _%s = dynamic_cast<%s*>(CSLoader::createNode(\"%s\"));\n", pstProp->name.c_str(), pstProp->classname.c_str(), strFileName.c_str());

fprintf(pfCPP, " this->addWidget(_%s);\n\n\n", pstProp->name.c_str());

}

else

{

}

}

else

{

if (g_bUI)

{

fprintf(pfCPP, " _%s = dynamic_cast<Layout*>(extension::GUIReader::shareReader()->widgetFromJsonFile(\"%s\"));\n", pstProp->name.c_str(), strFileName.c_str());

fprintf(pfCPP, " this->addWidget(_%s);\n\n\n", pstProp->name.c_str());

}

else

{

fprintf(pfCPP, " _%s = dynamic_cast<CCNode*>(extension::SceneReader::sharedSceneReader()->createNodeWithSceneFile(\"%s\"));\n", pstProp->name.c_str(), strFileName.c_str());

fprintf(pfCPP, " this->addChild(_%s);\n\n\n", pstProp->name.c_str());

}

}

Write2InitCpp("", pstProp, true);

fprintf(pfCPP, " return true;\n");

fprintf(pfCPP, " }\n");

fprintf(pfCPP, " return false;\n");

fprintf(pfCPP, "}\n\n");

if(bNet)

{

fprintf(pfCPP, "bool %s::canProcess(ActionCode_t code)\n", g_strClassName.c_str());

fprintf(pfCPP, "{\n");

fprintf(pfCPP, " return true;\n");

fprintf(pfCPP, "}\n\n");

fprintf(pfCPP, "void %s::proccessDatas(MGNetDataReader * reader)\n", g_strClassName.c_str());

fprintf(pfCPP, "{\n\n");

fprintf(pfCPP, "}\n\n");

fprintf(pfCPP, "void %s::netSendDatasFailed(MGNetDataWriter * writer)\n", g_strClassName.c_str());

fprintf(pfCPP, "{\n\n");

fprintf(pfCPP, "}\n\n");

}

WritePropFun2Cpp("", pstProp);

return true;

}

int _tmain(int argc, _TCHAR* argv[])

{

if(argc < 2)

{

cout << "Usage:" << argv[0] << " [ExportJson file path] " << " [net -n]" << endl;

return -1;

}

if(argc >= 3)

{

string strNet = argv[2];

if(strNet == "-n")

{

bNet = true;

}

}

string _path = (char*)argv[1];

size_t pos = _path.find_last_of(‘\\‘);

g_path = _path.substr(0, pos + 1);

if (g_path == "")

{

g_path = DWGetModulePath();

}

pos = _path.find_last_of(‘.‘);

std::string suffix = _path.substr(pos + 1, _path.length());

if(suffix == "json")

{

g_bUI = false;

SNodeProp *pstProp = new SNodeProp;

LoadSceneJson(_path, pstProp);

Write2H(_path, pstProp);

Write2Cpp(_path, pstProp);

}

else if (suffix == "ExportJson")

{

g_bUI = true;

SNodeProp *pstProp = new SNodeProp;

LoadUIJson(_path, pstProp);

Write2H(_path, pstProp);

Write2Cpp(_path, pstProp);

}

return 0;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 21:16:22

cocos2dx导出的json转c++代码的相关文章

cocos2d-x3.0.1,加载cocostudio ui编辑器导出的json文件出现&quot;Buffer is too small&quot; &amp;&amp; 0解决方案

刚到这个公司,开始进行cocos2d-x的真正项目开发,为了以后能够更好扩展,改动不大,决定使用3.X版本开发,而且使用ui编辑器.在导入的时候,今天遇到一个问题,Buffer is too small,搜了不少,但是都没有解决的方案,最后查到一个错误诱因,是因为ui中使用了label所致,于是我试了试,发现还真是label导致的问题,每次运行异常都发生在CCDevice.cpp的 bool setFont(const char * pFontName = NULL, int nSize = 0

Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析

Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidjson/wiki:http://code.google.com/p/rapidjson/wiki/UserGuide 下面就通过实例代码讲解rapidjson的用法. 使用rapidjson解析json串 引入头文件 1 2 #include "json/rapidjson.h" #inc

Twaver的mono-desiner导出的json文件解析

以画的交换机为例,其他大概都差不多. 利用Twaver做出交换机模型如图1所示,其中,每一个端口都是一个单独的对象.具体Twaver操作流程参见网址:http://twaver.servasoft.com/doc 中的操作指南. 将做好的模型导出为json格式的文件.下面将以图一所示的交换机为例对json文件进行简单解析.如图二所示为图一交换机的所导出的json文件的简单结构. 首先,所导出的json文件为一个json对象,里面包含有两个数组为primitives(原始)和assembles(装

jQuery获取json数据实现代码

jQuery获取json数据实现代码:使用jQuery操作json数据是非常的频繁的,下面提供两端代码供大家参考之用,大家可以自行分析,以便灵活应用. /栏目 //发送ajax请求 $.getJSON( "../../../Templet/GetInfoHandler.ashx", //产生JSON数据的服务端页面 {id: "0", sid: "1;2;3", rid: Math.round(Math.random() * 10) }, //向

网页内容导出word/excel的js代码

IE设置: 工具-> Internet选项-> 安全->自定义级别-> 对没有标记安全级别的ActiveX控件进行初始化  设为启用! 1.导出word //指定区域导出到Word function html2word(Area) { var oWD = new ActiveXObject("Word.Application"); var oDC = oWD.Documents.Add("", 0, 1); var oRange = oDC.

备份和恢复(或叫导入导出)按钮中的代码及后台控制器中的代码案例

//备份(导出)按钮中的代码 function(button, e) { var grid = this.getView(); var recs = grid.getSelectionModel().getSelection(); var url='/Pc/XiTongPublic/HeChaYaoDian1Backup'; window.open(util.urlFillAll(url),'_blank'); } //备份(导出)VS中的代码 /// </summary> [HttpGet]

cocos2dx 2.2.2 cocostudio 数据编辑器导出的.json文件读取 解析

首先,创建 然后 再导出数据: 再然后,能够看到数据的格式是: 再然后,就是 数据解析代码: [cpp] view plaincopy ////////////////// rapidjson::Document _doc; bool bRet = false; unsigned long size = 0; unsigned char *pBytes = NULL; do { pBytes = cocos2d::CCFileUtils::sharedFileUtils()->getFileDa

(27)Cocos2d-x 3.0 Json用法

Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidjson/wiki:http://code.google.com/p/rapidjson/wiki/UserGuide 下面就通过实例代码讲解rapidjson的用法. 使用rapidjson解析json串 引入头文件 #include "json/rapidjson.h" #include

cocos2d-x之读取json文件

在resource文件夹下,添加data.json文件 新建->Other->empty->open 就新建一个json文件了, data.json内容如下 [{"name":"Hello","age":22},{"name":"World","age":   23}] 读取xml文件时要先引入头文件:#include <json/document.h> 在