从事cocos2d-x开发一年多了,从刚开始的手写界面到现在的cocostuio拼界面,总感觉ui这种东西体力劳动太多,就萌生了写一个解析cocostudio导出文件的想法。比较擅长java所以就用java写了。
思路1.遍历项目ui工程中的导出文件
2.解析json 保存数据
3.根据解析到的数据生成.cpp和.h文件,生成的文件放在工程目录的export下
下面讲一下代码的具体实现
主要类
1.AllJsonData.java 解析数据 导出数据
2.JsonData.java 存储一个json文件的数据
3.JsonUItem.java Json文件的节点数据
AllJsonData
public void init() {
// initFileNames();
// initJsonDatas();
searchFolder();
analyzeAllJson();
exportAllCppH();
System.out.println();
}
searchFolder 扫描当前目录下的工程文件,如果在发现json文件,再判断是否是Export文件下,如果是文件名和绝对路径保存起来
analyzeAllJson 解析扫描到的json文件
exportAllCppH 根据设计的格式将数据
JsonData
public String _jsonName; json文件名 导出文件时会用到
public JsonUItem _data = null; 根节点
JsonUItem
public JsonUItem _parent;父节点
private ArrayList<JsonUItem> _children;所有的子节点
public String _name;控件的名字
public String _stype;控件的类型
这两个算是数据结构的基本设定吧
下面讲下具体的实现 一一用文字描述太麻烦就直接粘代码了
public void searchFolder() {
// TODO
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(new File("names.txt")));
String curpath = System.getProperty("user.dir");
File file = new File(curpath);
getFilesName("", file, out);
out.close();
file = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void getFilesName(String folder, File file, BufferedWriter out)
throws IOException {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
String str = files[i].getName();
if (isJsonFile(str)) {
// String str1 = folder + str + " \\" + "\n";
String str1 = file.getAbsolutePath() + "/" + str + "";
String[] strs = str1.split("/");
for (int j = 0; j < strs.length; j++) {
if (strs[j].equals("Export")) {
out.write(str1);
System.out.println("getFilesName:"+str+"\n");
_jsonFileNames.add(str1);
JsonData jsonData = new JsonData(str);
_allJsonData.add(jsonData);
}
}
}
} else {
getFilesName(folder + files[i].getName() + "/", files[i], out);
}
}
files = (File[]) null;
}
解析json文件
/**
* 解析找到的json文件
*
* @param filename
* 绝对路径
*/
private void analyzeJson(String filename, JsonData jsonData) throws JSONException {
try {
String name = filename;
System.out.println("\n"+filename);
BufferedReader br = new BufferedReader(new FileReader(name));
String line = "";
StringBuffer buffer = new StringBuffer();
try {
while ((line = br.readLine()) != null) {
buffer.append(line);
}
} catch (IOException e1) {
e1.printStackTrace();
}
String fileContent = buffer.toString();
JSONObject jsonobj = new JSONObject(fileContent);
String widgetTree = jsonobj.getString("widgetTree");
JSONObject jsonobj1 = new JSONObject(widgetTree);
String children = jsonobj1.getString("children");
JsonUItem jsonItem = jsonData._data;
JSONArray jsonarray = new JSONArray(children);
int len = jsonarray.length();
analyzeJsonRecursion(jsonItem, jsonarray);
System.out.println("analyzeJson done!!");
//
// for (int i = 0; i < jsonarray.length(); i++) {
// JSONObject jsonobj = jsonarray.getJSONObject(i);
// String name111 = jsonobj.getString("widgetTree");
// //int age = jsonobj.getInt("age");
// }
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
导出头文件
private void exportH(String hName, JsonUItem jsonItem) {
// TODO
/**
*/
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
String time = df.format(new Date());// new Date()为获取当前系统时间
BufferedWriter out = null;
try {
String filename = "export/" + hName + ".h";
out = new BufferedWriter(new FileWriter(
new File(filename)));
String curpath = System.getProperty("user.dir");
out.write("//\n// Created by CocosJsonAnalyze on "+ time +"\n//\n\n");
out.write("#ifndef __ig__"+hName+"__\n");
out.write("#define __ig__"+hName+"__\n\n");
out.write("#include <iostream>\n\n");
out.write("#include \"cocos2d.h\"\n");
out.write("USING_NS_CC;\n\n#include\"iCocostudioLayer.h\"\n\n");
out.write("class "+hName+" : public iCocostudioLayer {\n\n");
out.write("public:\n\tCREATE_FUNC(HomeLayer);\n\tbool init();\n");
out.write("public:\n");
exportHRecursion(hName, jsonItem, out);
out.write("};\n");
out.write("#endif");
//getFilesName("", file, out);
out.close();
//file = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
导出cpp
private void exportCpp(String ccpName, String jsonName, JsonUItem jsonItem) {
// TODO
/*
*/
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
String time = df.format(new Date());// new Date()为获取当前系统时间
BufferedWriter out = null;
try {
String filename = "export/" + ccpName + ".cpp";
out = new BufferedWriter(new FileWriter(
new File(filename)));
String curpath = System.getProperty("user.dir");
out.write("//\n// Created by CocosJsonAnalyze on "+ time +"\n//\n\n");
//getFilesName("", file, out);
out.write("#include \""+ccpName+".h\"\n\n");
out.write("bool HomeLayer::init()\n{\n");
out.write("\tdo {\n");
out.write("\t\tiCocostudioLayer::initWithJson(\""+jsonName+"\");\n\n");
//TODO 初始化
exportCppInitRecursion(ccpName, jsonItem, out);
//exportCppInit(ccpName, "shop_btn_start", "Button", "", "start", out);
out.write("\t\treturn true;\n");
out.write("\t} while (0);\n\n");
out.write("\treturn false;\n");
out.write("}\n\n");
//TODO 点击事件
exportCppClickFunRecursion(ccpName, jsonItem, out);
out.close();
//file = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
喜欢交流的朋友可以加我的qq群 256105600
本文仅提供了思路 参考的文章 http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/24/3096437.html