
虽然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_Dodo{ #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; } stringclassname; stringname; intobjecttag; CNodePropVec_vecUIProp; CNodePropVec_vecGObjects; CNodePropVec_vecComs; SNodeProP*parent; }; file*pfH = NulL; file*pfCPP = NulL; boolbNet = false; boolbcsd = false; boolg_bUI = false; stringg_path = ""; stringg_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",pstProp->name.c_str()); } else if(pstProp->classname == "ImageVIEw") { //默认 fprintf(pfH,pstProp->name.c_str()); } else if(pstProp->classname == "LabelAtlas") { //默认 fprintf(pfH,pstProp->name.c_str()); } else if(pstProp->classname == "LabelBMFont") { //默认 fprintf(pfH,pstProp->name.c_str()); } else if(pstProp->classname == "Loadingbar") { //默认 fprintf(pfH,pstProp->name.c_str()); } else if(pstProp->classname == "SlIDer") { //默认 fprintf(pfH,pstProp->name.c_str()); } else if(pstProp->classname == "Label") { //默认 fprintf(pfH,pstProp->name.c_str()); } else if(pstProp->classname == "TextFIEld") { //默认 fprintf(pfH,"voID %s_%s_textFIEldEvent(Ref* pSender,TextFIEld::EventType type);\n",pstProp->name.c_str()); } else if(pstProp->classname == "Layout") { //默认 fprintf(pfH,pstProp->name.c_str()); } else if(pstProp->classname == "ScrollVIEw") { //默认 fprintf(pfH,pstProp->name.c_str()); } else if(pstProp->classname == "ListVIEw") { fprintf(pfH,"voID %s_%s_selectedItemEvent(Ref* pSender,ListVIEw::EventType type);\n",pstProp->name.c_str()); fprintf(pfH,"voID %s_%s_selectedItemEventScrollVIEw(Ref* pSender,ui::ScrollVIEw::EventType type);\n",pstProp->name.c_str()); } else if(pstProp->classname == "PageVIEw") { fprintf(pfH,"voID %s_%s_pageVIEwEvent(Ref *pSender,PageVIEw::EventType type);\n",pstProp->name.c_str()); } else if(pstProp->classname == "CustomImageVIEw") { //默认 fprintf(pfH,pstProp->name.c_str()); } else if(pstProp->classname == "CustomParticleWidget") { //默认 fprintf(pfH,pstProp->name.c_str()); } else { //默认 fprintf(pfH,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); } } for(CNodePropVecItr itr = pstProp->_vecComs.begin(); itr != pstProp->_vecComs.end(); itr++) { if (parent == "") { WritePropFun2H(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->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); } } for(CNodePropVecItr itr = pstProp->_vecGObjects.begin(); itr != pstProp->_vecGObjects.end(); itr++) { if (parent == "") { WriteProp2H(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",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); } } for(CNodePropVecItr itr = pstProp->_vecGObjects.begin(); itr != pstProp->_vecGObjects.end(); itr++) { if (parent == "") { WritePropInit2Cpp(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",pstProp->name.c_str(),pstProp->name.c_str()); //默认 fprintf(pfCPP,"_%s_%s->addtouchEventListener(this,toucheventselector(%s::%s_%s_touchEvent));\n",g_strClassname.c_str(),pstProp->name.c_str()); } else if(pstProp->classname == "CheckBox") { fprintf(pfCPP,pstProp->name.c_str()); fprintf(pfCPP,"_%s_%s->addEventListener(this,toucheventselector(%s::%s_%s_selected));\n",pstProp->name.c_str()); } else if(pstProp->classname == "ImageVIEw") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "LabelAtlas") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "LabelBMFont") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "Loadingbar") { fprintf(pfCPP,"_%s_%s->setPercent(0);\n",pstProp->name.c_str()); } else if(pstProp->classname == "SlIDer") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "Label") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "TextFIEld") { fprintf(pfCPP,toucheventselector(%s::%s_%s_textFIEldEvent));\n",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",pstProp->name.c_str()); } if (NulL != pstProp->parent && pstProp->parent->classname == "UILayer") { fprintf(pfCPP,"_%s_%s = dynamic_cast<%s*>(_%s->getWidgetByname(\"%s\"));\n",pstProp->name.c_str()); } else { fprintf(pfCPP,pstProp->name.c_str()); } //默认 fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "ScrollVIEw") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "ListVIEw") { fprintf(pfCPP,"_%s_%s->addEventListener((ui::ListVIEw::ccListVIEwCallback)this,toucheventselector(%s::%s_%s_selectedItemEvent));\n","_%s_%s->addEventListener((ui::ListVIEw::ccScrollVIEwCallback)this,toucheventselector(%s::%s_%s_selectedItemEventScrollVIEw,this));\n",pstProp->name.c_str()); } else if(pstProp->classname == "PageVIEw") { fprintf(pfCPP,toucheventselector(%s::%s_%s_pageVIEwEvent));\n",pstProp->name.c_str()); } else if(pstProp->classname == "CustomImageVIEw") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "CustomParticleWidget") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "Sprite") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "Node") { fprintf(pfCPP,pstProp->name.c_str()); } ////////////////////////////////////////////////////////////////////////// else if(pstProp->classname == "CCComAudio") { fprintf(pfCPP,"_%s_%s = dynamic_cast<%s*>(_%s->getComponent(\"%s\"));\n",pstProp->name.c_str()); } else if(pstProp->classname == "UILayer") { fprintf(pfCPP,"_%s_%s = dynamic_cast<%s*>(dynamic_cast<CCComrender*>(_%s->getComponent(\"%s\"))->getNode());\n",pstProp->name.c_str()); } else if (pstProp->classname == "CCSprite" || pstProp->classname == "CCTMXTiledMap" || pstProp->classname == "CCParticleSystemQuad" || pstProp->classname == "CCArmature" || pstProp->classname == "GUIComponent") { fprintf(pfCPP,pstProp->name.c_str()); } else if(pstProp->classname == "CCNode") { fprintf(pfCPP,"_%s_%s = dynamic_cast<%s*>(Helper::seekNodeByTag(_%s,%d));\n",pstProp->objecttag); } else { fprintf(pfCPP,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,true); } } for(CNodePropVecItr itr = pstProp->_vecComs.begin(); itr != pstProp->_vecComs.end(); itr++) { if (parent == "") { Write2InitCpp(pstProp->name,false); } else { Write2InitCpp(parent + "_" + pstProp->name,false); } } for(CNodePropVecItr itr = pstProp->_vecGObjects.begin(); itr != pstProp->_vecGObjects.end(); itr++) { if (parent == "") { Write2InitCpp(pstProp->name,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","{\n"); fprintf(pfCPP,"// _%s_%s\n","cclOG(\"_%s_%s\");\n","switch (type)\n"); fprintf(pfCPP,"case touch_EVENT_BEGAN:\n"); fprintf(pfCPP,"break;\n"); fprintf(pfCPP,"case touch_EVENT_MOVED:\n"); fprintf(pfCPP,"case touch_EVENT_ENDED:\n"); fprintf(pfCPP,"case touch_EVENT_CANCELED:\n"); fprintf(pfCPP,"default:\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","case CheckBox::EventType::UNSELECTED:\n"); fprintf(pfCPP,"case CheckBox::EventType::SELECTED:\n"); fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "ImageVIEw") { //默认 fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "LabelAtlas") { //默认 fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "LabelBMFont") { //默认 fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "Loadingbar") { //默认 fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "SlIDer") { //默认 fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "Label") { //默认 fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "TextFIEld") { //默认 fprintf(pfCPP,"voID %s::%s_%s_textFIEldEvent(Ref* pSender,TextFIEld::EventType type)\n","case TextFIEld::EventType::ATTACH_WITH_IME:\n"); fprintf(pfCPP,"case TextFIEld::EventType::DETACH_WITH_IME:\n"); fprintf(pfCPP,"case TextFIEld::EventType::INSERT_TEXT:\n"); fprintf(pfCPP,"case TextFIEld::EventType::DELETE_BACKWARD:\n"); fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "Layout") { //默认 fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "ScrollVIEw") { //默认 fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "ListVIEw") { fprintf(pfCPP,"voID %s::%s_%s_selectedItemEvent(Ref* pSender,ListVIEw::EventType type)\n","case cocos2d::ui::ListVIEw::EventType::ON_SELECTED_ITEM_START:\n"); fprintf(pfCPP,"case cocos2d::ui::ListVIEw::EventType::ON_SELECTED_ITEM_END:\n"); fprintf(pfCPP,"}\n\n"); fprintf(pfCPP,"voID %s::%s_%s_selectedItemEventScrollVIEw(Ref* pSender,ui::ScrollVIEw::EventType type)\n","// _%s\n","case ui::ScrollVIEw::EventType::SCRolL_TO_BottOM:\n"); fprintf(pfCPP,"case ui::ScrollVIEw::EventType::SCRolL_TO_top:\n"); fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "PageVIEw") { fprintf(pfCPP,"voID %s::%s_%s_pageVIEwEvent(Ref* pSender,PageVIEw::EventType type)\n","case PageVIEw::EventType::TURNING:\n"); fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "CustomImageVIEw") { //默认 fprintf(pfCPP,"}\n\n"); } else if(pstProp->classname == "CustomParticleWidget") { //默认 fprintf(pfCPP,"}\n\n"); } else { //默认 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); } } for(CNodePropVecItr itr = pstProp->_vecComs.begin(); itr != pstProp->_vecComs.end(); itr++) { if (parent == "") { WritePropFun2Cpp(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","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,"static Scene* createScene() \n"); fprintf(pfH,"{ \n"); fprintf(pfH,"Scene* pScene = Scene::create(); \n"); fprintf(pfH,"%s* uiLayer = new (std::nothrow) %s(); \n","if (uiLayer && uiLayer->init()) \n"); fprintf(pfH,"uiLayer->autorelease(); \n"); fprintf(pfH,"pScene->addChild(uiLayer); \n"); fprintf(pfH,"} \n"); fprintf(pfH,"else \n"); fprintf(pfH,"CC_SAFE_DELETE(uiLayer); \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,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()); 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","MGNetConnection::sharedConnection()->unregisterCmdHandler(this);\n"); } fprintf(pfCPP,"bool %s::init()\n","{\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",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","_%s = dynamic_cast<CCNode*>(extension::SceneReader::sharedSceneReader()->createNodeWithScenefile(\"%s\"));\n","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,"return true;\n"); fprintf(pfCPP,"voID %s::proccessDatas(MGNetDataReader * reader)\n","{\n\n"); fprintf(pfCPP,"voID %s::netSendDatasFailed(MGNetDataWriter * writer)\n","}\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); } return 0; }
总结以上是内存溢出为你收集整理的cocos2dx导出的json转c++代码全部内容,希望文章能够帮你解决cocos2dx导出的json转c++代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)