コード例 #1
0
    private void DrawConsoleUI(float fYOffset)
    {
        if (m_bNeedResetFocus)
        {
            GUI.FocusControl(null);
            m_bNeedResetFocus = false;
        }

        GUILayout.Space(10);

        GUILayout.BeginHorizontal();
        GUILayout.Space(20f);
        Color kOrgColor = GUI.color;

        GUI.color = Color.cyan;
        GUILayout.Label("Please see unity console for output detail.", GUILayout.Width(300));
        GUI.color = kOrgColor;
        GUILayout.EndHorizontal();

        GUILayout.Space(5);

        GUILayout.BeginHorizontal();
        GUILayout.Space(20f);
        GUILayout.Label("RPC Command:", GUILayout.Width(100));
        GUILayout.EndHorizontal();

        //GUILayout.Space(5);

        GUILayoutOption[] arrLO = new GUILayoutOption[2];
        arrLO[0] = GUILayout.Width(170);
        arrLO[1] = GUILayout.Height(20);

        GUILayout.BeginHorizontal();
        GUILayout.Space(20.0f);
        Color kOldC = GUI.color;

        GUI.color = new Color(255 / 255.0f, 160 / 255.0f, 50 / 255.0f);
        GUILayout.Label("Command Type:", GUILayout.Width(120.0f));
        int iSelCmdTypeIndex = EditorGUILayout.Popup(m_iLastSelCmdTypeIndex, m_arrCmdType, arrLO);

        if (iSelCmdTypeIndex != m_iLastSelCmdTypeIndex)
        {
            m_iLastSelCmdIndex     = 0;
            m_iLastSelCmdTypeIndex = iSelCmdTypeIndex;
        }
        GUI.color = kOldC;
        GUILayout.EndHorizontal();

        string[] arrCmd     = m_listCommand[m_iLastSelCmdTypeIndex];
        string[] arrArgInfo = m_listArgsInfo[m_iLastSelCmdTypeIndex];

        GUILayout.BeginHorizontal();
        GUILayout.Space(20.0f);
        kOldC     = GUI.color;
        GUI.color = Color.yellow;
        GUILayout.Label("Command Select:", GUILayout.Width(120.0f));
        int iSelCmdIndex = EditorGUILayout.Popup(m_iLastSelCmdIndex, arrCmd, arrLO);

        GUI.color = kOldC;
        if (iSelCmdIndex != m_iLastSelCmdIndex)
        {
            if (arrCmd[iSelCmdIndex] != COMMAND_SPLITE)
            {
                m_strRawCurArgInfo = arrArgInfo[iSelCmdIndex];
                //m_strCurArgsInfo = arrArgInfo[iSelCmdIndex];
                ResolveArgType();
                if (string.IsNullOrEmpty(m_strCurArgsInfo))
                {
                    m_strRawExeRpcCommand = arrCmd[iSelCmdIndex];
                }
                else
                {
                    m_strRawExeRpcCommand = arrCmd[iSelCmdIndex] + " ";
                }

                GUI.FocusControl("RPCCmdInput");
            }
            else
            {
                m_strRawExeRpcCommand = "";
                m_strCurArgsInfo      = "";
                GUI.FocusControl(null);
            }
            m_iLastSelCmdIndex = iSelCmdIndex;
        }
        GUILayout.Space(5);
        kOldC     = GUI.color;
        GUI.color = new Color(0.69f, 0.77f, 0.95f);
        GUILayout.Label(m_strCurArgsInfo, GUILayout.Width(400.0f));
        GUI.color = kOldC;
        GUILayout.EndHorizontal();

        GUILayout.Space(5);

        GUILayout.BeginHorizontal();
        GUILayout.Space(20f);
        //arrLO = new GUILayoutOption[2];
        float fTAWidth = ms_kSig.position.width - 40.0f;

        arrLO[0] = GUILayout.Width(fTAWidth);
        //arrLO[0] = GUILayout.Width(560);
        arrLO[1] = GUILayout.Height(100);
        GUI.SetNextControlName("RPCCmdInput");
        m_strRawExeRpcCommand = EditorGUILayout.TextArea(m_strRawExeRpcCommand, arrLO);
        //m_strRpcCommand = GUILayout.TextArea(m_strRpcCommand, arrLO);
        GUILayout.EndHorizontal();

        GUILayout.Space(10);

        GUILayout.BeginHorizontal();
        float fBtnWidth   = 100.0f;
        float fBtnHeight  = 25.0f;
        float fBtnXOffset = 210.0f;
        float fXSpace     = fBtnXOffset;

        GUILayout.Space(fXSpace);
        arrLO[0] = GUILayout.Width(fBtnWidth);
        arrLO[1] = GUILayout.Height(fBtnHeight);
        if (GUILayout.Button("Execute", arrLO))
        {
            if (m_bCBInited == false)
            {
                if (EditorUtility.DisplayDialog("Erorr", "MagnaChain has not initialized", "OK"))
                {
                    return;
                }
            }

            if (string.IsNullOrEmpty(m_strRawExeRpcCommand))
            {
                if (EditorUtility.DisplayDialog("Prompt", "The rpc command is empty.", "OK"))
                {
                    return;
                }
            }

            try
            {
                ResolveExeCommand();

                RPCResponse kRsp = m_kCB.SendCommand(m_strExeRpcCommand, m_arrRpcArgs);
                if (kRsp == null)
                {
                    LogError("Execute command error!");
                }
                else
                {
                    if (kRsp.Error == null)
                    {
                        LogSys(kRsp.ResultString);
                    }
                    else
                    {
                        LogError("Error: " + kRsp.Error.Code + " msg: " + kRsp.Error.Message);
                    }
                }
            }
            catch (Exception e)
            {
                LogError(e.Message);
            }
        }

        float fXSpace2 = (ms_kSig.position.width * 0.5f - fBtnWidth - fBtnXOffset) * 2.0f;

        //fXSpace = fXSpace2 - fXSpace;
        GUILayout.Space(fXSpace2);
        if (GUILayout.Button("Clear", arrLO))
        {
            m_strRawExeRpcCommand = "";

            GUI.FocusControl(null);
        }
        GUILayout.EndHorizontal();

        GUILayout.Space(20);

        GUILayout.BeginHorizontal();
        GUILayout.Space(20.0f);
        GUILayout.Label("Template Contract:", GUILayout.Width(120.0f));
        ms_iSelTemplateLuaFineIndex = EditorGUILayout.Popup(ms_iSelTemplateLuaFineIndex, ms_arrTemplateLuaFile, GUILayout.Width(200));
        if (ms_arrTemplateLuaFile.Length > 0 && ms_iSelTemplateLuaFineIndex > 0)
        {
            ms_strSelTemplateLuaFile = ms_arrTemplateLuaFile[ms_iSelTemplateLuaFineIndex];

            string strFullPath = ms_strTemplateLuaPrefix + ms_strSelTemplateLuaFile;
            if (strFullPath != m_strLuaFile)
            {
                m_strLuaFile = strFullPath;
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.Space(6);

        //m_strLuaFile = DrawFileChooser("CFLabe", "CFInput", "CFBtn", 20.0f, true, "Contract File:", 90.0f, m_strLuaFile, 220.0f, "", false, false, "lua", 70, 100);
        GUILayout.BeginHorizontal();
        GUILayout.Space(20.0f);
        GUI.SetNextControlName("CFLabe");
        GUILayout.Label("Contract File:", GUILayout.Width(90.0f));    // 120
        //strFilePath = EditorGUILayout.TextField(strFilePath, GUILayout.Width(fTextFiledWidth));   // 330
        float fTFWidth;

        fTFWidth = ms_kSig.position.width - 20.0f - 90.0f - 10.0f - 70.0f - 20.0f - 10.0f - 90.0f;

        //arrLO = new GUILayoutOption[2];
        arrLO[0] = GUILayout.Height(18.0f);
        arrLO[1] = GUILayout.Width(fTFWidth);
        GUI.SetNextControlName("CFInput");

        //strFilePath = GUILayout.TextField(strFilePath, 512, GUILayout.Width(fTextFiledWidth));
        m_strLuaFile = EditorGUILayout.TextField(m_strLuaFile, arrLO);

        GUILayout.Space(10);
        arrLO[1] = GUILayout.Width(70.0f);
        GUI.SetNextControlName("CFBtn");
        if (GUILayout.Button("Browser", arrLO))
        {
            ms_iSelTemplateLuaFineIndex = 0;
            m_strLuaFile = EditorUtility.OpenFilePanel("Choose " + "Contract File:", "", "lua");
        }

        arrLO[0] = GUILayout.Width(70.0f);
        fXSpace  = 5.0f;
        GUILayout.Space(fXSpace);
        if (GUILayout.Button("Edit", arrLO))
        {
            if (!string.IsNullOrEmpty(m_strLuaFile))
            {
                Process process = new Process();
                process.StartInfo.FileName               = "notepad.exe";
                process.StartInfo.Arguments              = m_strLuaFile;
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardInput  = false;
                process.StartInfo.RedirectStandardOutput = false;
                process.StartInfo.RedirectStandardError  = false;
                process.StartInfo.CreateNoWindow         = true;
                process.Start();

                m_bEditLuaFile = true;
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.Space(10);

        m_strCostCLAddress = DrawFileChooser("CCALabel", "CCAInput", "CCABtn", 20.0f, false, "Cost MCL Address:", 130.0f, m_strCostCLAddress, 320.0f, "", false, true);

        GUILayout.Space(5);

        m_strCostAmount = DrawFileChooser("CAXLabel", "CAXInput", "CAXBtn", 20.0f, false, "Cost Amount:", 130.0f, m_strCostAmount, 320.0f, "", false, true);

        GUILayout.Space(5);

        m_strSenderCLAddress = DrawFileChooser("CRALabel", "CRAInput", "CRABtn", 20.0f, false, "Sender MCL Address:", 130.0f, m_strSenderCLAddress, 320.0f, "", false, true);

        GUILayout.Space(10);

        GUILayout.BeginHorizontal();
        fBtnWidth = 160.0f;
        arrLO[0]  = GUILayout.Width(fBtnWidth);
        arrLO[1]  = GUILayout.Height(25.0f);
        fXSpace   = (ms_kSig.position.width - fBtnWidth) * 0.5f;
        GUILayout.Space(fXSpace);
        if (GUILayout.Button("Publish Contract", arrLO))
        {
            if (m_bCBInited == false)
            {
                if (EditorUtility.DisplayDialog("Erorr", "MagnaChain has not initialized", "OK"))
                {
                    return;
                }
            }

            if (string.IsNullOrEmpty(m_strLuaFile))
            {
                if (EditorUtility.DisplayDialog("Prompt", "Please select contract file first.", "OK"))
                {
                    return;
                }
            }

            if (m_bEditLuaFile)
            {
                if (EditorUtility.DisplayDialog("Prompt", "You may have modified contract file, please make sure the contract file have saved.", "Continue", "Cancel") == false)
                {
                    return;
                }
                m_bEditLuaFile = false;
            }

            string strTxt = null;

            try
            {
                strTxt = File.ReadAllText(m_strLuaFile);
            }
            catch (Exception e)
            {
                LogError(e.Message);
                return;
            }

            if (string.IsNullOrEmpty(strTxt))
            {
                EditorUtility.DisplayDialog("Prompt", "The contract content is empty!", "OK");
                return;
            }

            float fCostAmount = 0.0f;

            try
            {
                fCostAmount = Convert.ToSingle(m_strCostAmount);
            }
            catch
            {
                fCostAmount = 0.0f;
            }
            bool bRet = m_kCB.PublishContract(out ms_strContractAddress, strTxt, m_strCostCLAddress, fCostAmount, m_strSenderCLAddress);
            if (bRet == false)
            {
                LogError("Publish contract failed: " + m_strLuaFile);
                return;
            }

            LogSys("Publish contract successfully, contract address: " + ms_strContractAddress);

            string strPrefix = Application.dataPath;
            string strKey    = strPrefix + "#ms_strContractAddress";
            PlayerPrefs.SetString(strKey, ms_strContractAddress);
            PlayerPrefs.Save();
        }
        GUILayout.EndHorizontal();

        GUILayout.Space(15);

        ms_strContractAddress = DrawFileChooser("CADLabel", "CADInput", "CADBtn", 20.0f, false, "Contract Address:", 120.0f, ms_strContractAddress, 330.0f, "", false, true);

        GUILayout.Space(5);

        m_strCallFunction = DrawFileChooser("CFLabel", "CFInput", "CFBtn", 20.0f, false, "Call Function:", 120.0f, m_strCallFunction, 330.0f, "", false, true);

        GUILayout.Space(5);

        m_strCallArgs = DrawFileChooser("CALabel", "CAInput", "CABtn", 20.0f, false, "Call Arguments:", 120.0f, m_strCallArgs, 330.0f, "", false, true);

        GUILayout.Space(15);

        GUILayout.BeginHorizontal();

        float fTogWidth = 120.0f;

        arrLO[0] = GUILayout.Width(fTogWidth);
        float fTogX = 40.0f;

        GUILayout.Space(fTogX);
        m_bContractSendCall = GUILayout.Toggle(m_bContractSendCall, "Send Call", arrLO);

        fBtnWidth = 180.0f;
        arrLO[0]  = GUILayout.Width(fBtnWidth);
        fXSpace   = ms_kSig.position.width * 0.5f - fTogWidth - fTogX - fBtnWidth * 0.5f;
        GUILayout.Space(fXSpace);
        if (GUILayout.Button("Call Contract Function", arrLO))
        {
            if (m_bCBInited == false)
            {
                if (EditorUtility.DisplayDialog("Erorr", "MagnaChain has not initialized", "OK"))
                {
                    return;
                }
            }

            if (string.IsNullOrEmpty(ms_strContractAddress))
            {
                if (EditorUtility.DisplayDialog("Prompt", "Please input contract address first.", "OK"))
                {
                    return;
                }
            }

            if (string.IsNullOrEmpty(m_strCallFunction))
            {
                if (EditorUtility.DisplayDialog("Prompt", "Please input call function name first.", "OK"))
                {
                    return;
                }
            }

            float fCostAmount = 0.0f;
            try
            {
                fCostAmount = Convert.ToSingle(m_strCostAmount);
            }
            catch
            {
                fCostAmount = 0.0f;
            }

            object[] arrArg = null;
            if (!string.IsNullOrEmpty(m_strCallArgs))
            {
                //char[] arrSpt = new char[1];
                //arrSpt[0] = ' ';
                arrArg = System.Text.RegularExpressions.Regex.Split(m_strCallArgs, @"\s+");//m_strCallArgs.Split(arrSpt);
            }

            string strRet = m_kCB.CallContractFunction(m_bContractSendCall, ms_strContractAddress, m_strCostCLAddress, fCostAmount, m_strSenderCLAddress, m_strCallFunction, arrArg);
            if (string.IsNullOrEmpty(strRet))
            {
                LogError("CallContractFunction failed!");
                return;
            }
            LogSys("CallContractFunction succeed: " + strRet);
        }

        //fXSpace = ms_kSig.position.width - fBtnOffset * 2.0f - fBtnWidth * 2.0f;
        //GUILayout.Space(fXSpace);
        //if (GUILayout.Button("SendCall Contract", arrLO))
        //{
        //    if (m_bCBInited == false)
        //    {
        //        if (EditorUtility.DisplayDialog("Erorr", "MagnaChain has not initialized", "OK"))
        //        {
        //            return;
        //        }
        //    }

        //    if (string.IsNullOrEmpty(m_strContractAddress))
        //    {
        //        if (EditorUtility.DisplayDialog("Prompt", "Please publish contract first.", "OK"))
        //        {
        //            return;
        //        }
        //    }

        //    if (string.IsNullOrEmpty(m_strCallFunction))
        //    {
        //        if (EditorUtility.DisplayDialog("Prompt", "Please input call function name first.", "OK"))
        //        {
        //            return;
        //        }
        //    }

        //    string strRet = m_kCB.SendCallContract(m_strContractAddress, m_strCallFunction, m_strSenderAddress, m_strCallArgs);
        //    if (string.IsNullOrEmpty(strRet))
        //    {
        //        LogError("SendCallContract failed: " + m_strContractAddress);
        //        return;
        //    }
        //    LogSys("SendCallContract succeed: " + strRet);
        //}
        GUILayout.EndHorizontal();
    }