虽然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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。