C#窗体应用程序实现cocos2dx json文件自动生成lua文件

文件解析部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace WindowsFormsApplication3
{
    class GetLuaFile
    {
        private String name = "";
        private String Name;
        private String srcFilePath;
        private String desFilePath;
        private List<String[]> buttonList;
        private List<String[]> labelList;
 
        public int ToLuaFile(String jsonFile,String luaFile,String tableName)
        {
            if (jsonFile == null || luaFile == null || tableName == null)
            {
                return -1;
            }
            srcFilePath = jsonFile;
            desFilePath = luaFile;
            name = tableName;
            if(!File.Exists(@srcFilePath))
            {
                return -2;
            }
            StreamReader reader = new StreamReader(@srcFilePath);
            string lineFromTheReader = reader.ReadLine();
            GetOptionList(lineFromTheReader);
            InitLuaFile();
            reader.Close();
            return 1;
        }
        private void GetOptionList(String data)
        {
            buttonList = new List<String[]>();
            labelList = new List<String[]>();
            String str_option = "options";
       String str_button = "Button";
       String str_label = "Label";
       String str1 = "classname\":";
       String str2 = ",\"name\":";
       String str3 = ",\"ZOrder";
       String str4 = "text\":";
       String str5 = "\"";
            int p2, p3, p5, p6;
            int p1 = 1;
            int p4 = 1;
            while(p1 != -1)
            {
                p1 = data.IndexOf(str_option, p4);
                if(p1 != -1)
                {
                    p2 = data.IndexOf(str1, p1);
                    p3 = data.IndexOf(str2, p2);
                    p4 = data.IndexOf(str3, p3);                    
                    String[] t = new String[3];
                    if (p2 != -1 && p3 != -1 && p4 != -1)
                    {
                        p5 = data.IndexOf(str4, p4);
                        p5 = p5 + str4.Length + 1;
                        p6 = data.IndexOf(str5, p5);
                        p2 = p2 + str1.Length + 1;                        
                        t[0] = data.Substring(p2, p3 - p2 - 1);
                        p3 = p3 + str2.Length + 1;
                        t[1] = data.Substring(p3, p4 - p3 - 1);
                        t[2] = data.Substring(p5, p6 - p5);
                        if(t[0].IndexOf(str_button,0) != -1)
                        {
                            buttonList.Add(t);
                        }
                        if(t[0].IndexOf(str_label, 0) != -1)
                        {
                            labelList.Add(t);
                        }
                    }
                }
            }
        }
        private void GameLoginOut(StreamWriter writer)
        {
            writer.WriteLine("function p.gameLoginOut()");
            writer.WriteLine("end");
            writer.WriteLine();
        }
        private void Create(StreamWriter writer)
        {
            writer.WriteLine("function p.create()");
            writer.WriteLine("\tlocal layer = TouchGroup:create()");
            writer.WriteLine("\tp.ui = ui_delegate(GUIReader:shareReader():widgetFromJsonFile(\"{0}\"))", srcFilePath);
            writer.WriteLine("\tp.AddClickEvent()");
            writer.WriteLine("\tp.SetUIDefaultText()");
            writer.WriteLine("\tlayer:addWidget(p.ui.nativeUI)");
            writer.WriteLine("\tlayer:registerScriptHandler(p.OnNodeEvent)");
            writer.WriteLine("\treturn layer");
            writer.WriteLine("end");
            writer.WriteLine();
        }
        private void AddClickEvent(StreamWriter writer)
        {
            writer.WriteLine("function p.AddClickEvent()");
            foreach(String[] t in buttonList)
            {
                writer.WriteLine("\tui_add_click_listener(p.ui." + t[1] + ", p." + t[1] + "_event)");
            }
            writer.WriteLine("end");
            writer.WriteLine();
            //创建按钮事件
            foreach(String[] t in buttonList)
            {
                writer.WriteLine("function p." + t[1] + "_event()");
                writer.WriteLine("end");
                writer.WriteLine();
            }
        }
        private void SetUIDefaultText(StreamWriter writer)
        {
            writer.WriteLine("function p.SetUIDefaultText()");
            foreach(String[] t in buttonList)
            {
                if(t[2].Length > 1)
                {
                    //writer.WriteLine("\tsetButtonText(p.ui.{0},\"{1}\")", t[1], t[2]);
                    writer.WriteLine("\ttolua.cast(p.ui.{0},\"{1}\"):setTitleText(\"{2}\")", t[1], t[0], t[2]);
                }
            }
            writer.WriteLine();
            foreach(String[] t in labelList)
            {
                if(t[2].Length > 1)
                {
                    string LabelBMFont = "LabelBMFont";
                    if (t[0].IndexOf(LabelBMFont, 0) != -1)
                    {
                        //writer.WriteLine("\tsetBMLabelText(p.ui.{0},\"{1}\")", t[1], t[2]);
                        writer.WriteLine("\ttolua.cast(p.ui.{0},\"{1}\"):setText(\"{2}\")", t[1], t[0], t[2]);
                    }
                    else
                    {
                        //writer.WriteLine("\tsetLabelText(p.ui.{0},\"{1}\")", t[1], t[2]);
                        writer.WriteLine("\ttolua.cast(p.ui.{0},\"{1}\"):setText(\"{2}\")", t[1], t[0], t[2]);
                    }                    
                }
            }
            writer.WriteLine("end");
            writer.WriteLine();
        }
        private void OnNodeEvent(StreamWriter writer)
        {
            writer.WriteLine("function p.OnNodeEvent(event)");
            writer.WriteLine("\tif \"enter\" == event then");
            writer.WriteLine("\t\tp._bInUI = true");
            writer.WriteLine("\telseif \"exit\" == event then");
            writer.WriteLine("\t\tp._bInUI = false");
            writer.WriteLine("\t\tp.ui = nil");
            writer.WriteLine("\tend");
            writer.WriteLine("end");
            writer.WriteLine("");
        }
        private void CloseWin(StreamWriter writer)
        {
            Name = name.ToUpper();
            writer.WriteLine("function p.CloseWin()");
            writer.WriteLine("\tCloseLayer(UI_TAG.UI_{0})",Name);
            writer.WriteLine("end");
            writer.WriteLine();
        }
        private void InitLuaFile()
        {
            StreamWriter writer = new StreamWriter(@desFilePath,false);
            //创建表p
            writer.WriteLine(name + " = {}");
            writer.WriteLine("local p = {0}",name);
            writer.WriteLine();
            writer.WriteLine();
            //创建gameLoginOut()
            GameLoginOut(writer);
            //创建create()
            Create(writer);
             //创建OnNodeEvent()
            OnNodeEvent(writer);
            //创建SetUIDefaultText()
            SetUIDefaultText(writer);
            //创建AddClickEvent()
            AddClickEvent(writer);
            //创建CloseWin()
            CloseWin(writer);
            //返回表p
            writer.WriteLine("return p");
            writer.Close();
        }
    }
}

窗体声明部分:

namespace WindowsFormsApplication3
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;


        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        #region Windows 窗体设计器生成的代码


        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox_src = new System.Windows.Forms.TextBox();
            this.textBox_des = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label_explain = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label_result = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.textBox_tableName = new System.Windows.Forms.TextBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.button_update = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.Location = new System.Drawing.Point(568, 157);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(178, 16);
            this.label1.TabIndex = 0;
            this.label1.Text = "srcFilePath(json):";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label2.Location = new System.Drawing.Point(577, 216);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(169, 16);
            this.label2.TabIndex = 1;
            this.label2.Text = "desFilePath(lua):";
            // 
            // button1
            // 
            this.button1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.button1.Location = new System.Drawing.Point(716, 276);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(101, 35);
            this.button1.TabIndex = 2;
            this.button1.Text = "Generate";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox_src
            // 
            this.textBox_src.AllowDrop = true;
            this.textBox_src.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.textBox_src.Location = new System.Drawing.Point(738, 154);
            this.textBox_src.Name = "textBox_src";
            this.textBox_src.Size = new System.Drawing.Size(253, 26);
            this.textBox_src.TabIndex = 3;
            this.textBox_src.Text = "f:/Sacrifice/Sacrifice.json";
            this.textBox_src.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox_src_DragDrop);
            this.textBox_src.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox_src_DragEnter);
            this.textBox_src.DoubleClick += new System.EventHandler(this.textBox_src_DoubleClick);
            // 
            // textBox_des
            // 
            this.textBox_des.AllowDrop = true;
            this.textBox_des.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.textBox_des.Location = new System.Drawing.Point(738, 210);
            this.textBox_des.Name = "textBox_des";
            this.textBox_des.Size = new System.Drawing.Size(253, 26);
            this.textBox_des.TabIndex = 4;
            this.textBox_des.Text = "f:/Sacrifice/Sacrifice.lua";
            this.textBox_des.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox_des_DragDrop);
            this.textBox_des.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox_des_DragEnter);
            this.textBox_des.DoubleClick += new System.EventHandler(this.textBox_des_DoubleClick);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label3.Location = new System.Drawing.Point(16, 35);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(59, 16);
            this.label3.TabIndex = 5;
            this.label3.Text = "说明:";
            // 
            // label_explain
            // 
            this.label_explain.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label_explain.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label_explain.Location = new System.Drawing.Point(68, 37);
            this.label_explain.Name = "label_explain";
            this.label_explain.Size = new System.Drawing.Size(461, 106);
            this.label_explain.TabIndex = 6;
            this.label_explain.Text = "\"\"";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label4.Location = new System.Drawing.Point(16, 162);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(59, 16);
            this.label4.TabIndex = 7;
            this.label4.Text = "结果:";
            // 
            // label_result
            // 
            this.label_result.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label_result.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label_result.Location = new System.Drawing.Point(68, 162);
            this.label_result.Name = "label_result";
            this.label_result.Size = new System.Drawing.Size(461, 134);
            this.label_result.TabIndex = 8;
            this.label_result.Text = "\"\"";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label5.Location = new System.Drawing.Point(640, 112);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(106, 16);
            this.label5.TabIndex = 9;
            this.label5.Text = "tableName:";
            // 
            // textBox_tableName
            // 
            this.textBox_tableName.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.textBox_tableName.Location = new System.Drawing.Point(738, 106);
            this.textBox_tableName.Name = "textBox_tableName";
            this.textBox_tableName.Size = new System.Drawing.Size(189, 26);
            this.textBox_tableName.TabIndex = 10;
            this.textBox_tableName.Text = "Sacrifice";
            this.textBox_tableName.TextChanged += new System.EventHandler(this.textBox_tableName_TextChanged);
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(933, 15);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(107, 99);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 11;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
            // 
            // button_update
            // 
            this.button_update.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.button_update.Location = new System.Drawing.Point(571, 35);
            this.button_update.Name = "button_update";
            this.button_update.Size = new System.Drawing.Size(121, 44);
            this.button_update.TabIndex = 14;
            this.button_update.Text = "版本更新";
            this.button_update.UseVisualStyleBackColor = true;
            this.button_update.Click += new System.EventHandler(this.button_update_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1052, 342);
            this.Controls.Add(this.button_update);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.textBox_tableName);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label_result);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label_explain);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.textBox_des);
            this.Controls.Add(this.textBox_src);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "GetLuaFromJson";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();


        }


        #endregion


        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox_src;
        private System.Windows.Forms.TextBox textBox_des;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label_explain;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label_result;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox textBox_tableName;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button button_update;






    }
}


窗体事件部分:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        GetLuaFile Instance;
        public Form1()
        {
            InitializeComponent();
            Instance = new GetLuaFile();
            label_explain.Text = "1、将JSON文件路径输入srcFilePath(json);\n"
                                + "2、将LUA文件路径输入desFilePath(lua);\n"
                                + "3、在tableName中输入要生成的lua文件的表名称;\n"
                                + "4、点击Generate按钮,查看结果!\n"
                                + "5、进行处理的json文件为UI编辑器非格式化生成的!\n";
        }


        private void button1_Click(object sender, EventArgs e)
        {
            int result = Instance.ToLuaFile(textBox_src.Text, textBox_des.Text, textBox_tableName.Text);
            if (result == 1)
            {
                label_result.Text = "成功生成" + textBox_tableName.Text + ".lua!";
            }
            if (result == -2)
            {
                label_result.Text = "找不到文件" + textBox_src.Text + "!";
            }
            if (result == -1)
            {
                label_result.Text = "无效的参数!";
            }
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("version:1.0\tdate:2015/11/10\n"
                            + "version:1.1\tdate:2015/11/11\n"
                            + "version:1.2\tdate:2015/11/16\n"
                            + "version:1.3\tdate:2015/11/20\n"
                            + "version:1.4\tdate:2015/12/10", "about");
        }


        private void button_update_Click(object sender, EventArgs e)
        {
            MessageBox.Show("version:1.1     date:2015/11/11\n"
                            + "1、修复Button的错误方法setText为setTitleText;\n"
                            + "2、ui_add_click_listener的#2去掉多余的();\n"
                            + "\n"
                            + "version:1.2     date:2015/11/16\n"
                            + "1、添加函数gameLoginOut;\n"
                            + "2、代码优化;\n"
                            + "\n"
                            + "version:1.3     date:2015/11/20\n"
                            + "1、新方法setButtonText、setBMlabelText、setLabelText;\n"
                            + "\n"
                            + "version:1.4     date:2015/12/10\n"
                            + "1、新增获取文件名的方式,拖动文件和双击浏览;", "update");
        }


        private void textBox_tableName_TextChanged(object sender, EventArgs e)
        {
            textBox_src.Text = "f:/" + textBox_tableName.Text + "/" + textBox_tableName.Text + ".json";
            textBox_des.Text = "f:/" + "Sacrifice" + "/" + textBox_tableName.Text + ".lua";
        }


        private void textBox_src_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else
                e.Effect = DragDropEffects.None;
        }
        //这样就可以得到拖到文件的路径了,任意文件
        private void textBox_src_DragDrop(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(DataFormats.FileDrop) )
            {
                string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);
                try
                {
                    string file = filePath[0];
                    string limit = ".json";
                    if (file.IndexOf(limit, 0) != -1)
                        textBox_src.Text = filePath[0];
                    else
                        MessageBox.Show("invalid file,please try again!", "warn");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }


        private void textBox_src_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"F:\Sacrifice";
            openFileDialog1.Filter = "json files (*.json)|*.json";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                textBox_src.Text = openFileDialog1.FileName;
        }


        private void textBox_des_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else
                e.Effect = DragDropEffects.None;
        }
        //这样就可以得到拖到文件的路径了,任意文件
        private void textBox_des_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);
                try
                {                    
                    string file = filePath[0];
                    string limit = ".lua";
                    if (file.IndexOf(limit, 0) != -1)
                        textBox_des.Text = filePath[0];
                    else
                        MessageBox.Show("invalid file,please try again!", "warn");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }


        private void textBox_des_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"F:\Sacrifice";
            openFileDialog1.Filter = "json files (*.lua)|*.lua";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                textBox_des.Text = openFileDialog1.FileName;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值