Example #1
0
        private void FunctionsGUI()
        {
            if (m_ScriptPrescription.m_Functions == null)
            {
                GUILayout.FlexibleSpace();
                return;
            }

            EditorGUILayout.BeginHorizontal();
            {
                GUILayout.Label("Functions", GUILayout.Width(kLabelWidth - 4));

                EditorGUILayout.BeginVertical(m_Styles.m_LoweredBox);
                m_OptionsScroll = EditorGUILayout.BeginScrollView(m_OptionsScroll);
                {
                    bool expanded = FunctionHeader("General", true);

                    for (int i = 0; i < m_ScriptPrescription.m_Functions.Length; i++)
                    {
                        FunctionData func = m_ScriptPrescription.m_Functions[i];

                        if (func.name == null)
                        {
                            expanded = FunctionHeader(func.comment, false);
                        }
                        else if (expanded)
                        {
                            Rect toggleRect = GUILayoutUtility.GetRect(GUIContent.none, EditorStyles.toggle);
                            toggleRect.x     += 15;
                            toggleRect.width -= 15;
                            bool include = GUI.Toggle(toggleRect, func.include, new GUIContent(func.name, func.comment));
                            if (include != func.include)
                            {
                                m_ScriptPrescription.m_Functions[i].include = include;
                                SetFunctionIsIncluded(m_BaseClass, func.name, include);
                            }
                        }
                    }
                }
                EditorGUILayout.EndScrollView();
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndHorizontal();
        }
Example #2
0
        private void HandleBaseClass()
        {
            if (m_TemplateNames.Length == 0)
            {
                m_BaseClass = null;
                return;
            }

            // Get base class
            m_BaseClass = GetBaseClass(m_ScriptPrescription.m_Template);

            // If base class was found, strip first line from template
            if (m_BaseClass != null)
            {
                m_ScriptPrescription.m_Template =
                    m_ScriptPrescription.m_Template.Substring(m_ScriptPrescription.m_Template.IndexOf("\n") + 1);
            }

            m_IsEditorClass  = IsEditorClass(m_BaseClass);
            m_IsCustomEditor = (m_BaseClass == kCustomEditorClassName);
            m_ScriptPrescription.m_StringReplacements.Clear();

            // Try to find function file first in custom templates folder and then in built-in
            string functionDataFilePath = Path.Combine(GetAbsoluteCustomTemplatePath(), m_BaseClass + ".functions.txt");

            if (!File.Exists(functionDataFilePath))
            {
                functionDataFilePath = Path.Combine(GetAbsoluteBuiltinTemplatePath(), m_BaseClass + ".functions.txt");
            }

            if (!File.Exists(functionDataFilePath))
            {
                m_ScriptPrescription.m_Functions = null;
            }
            else
            {
                StreamReader        reader       = new StreamReader(functionDataFilePath);
                List <FunctionData> functionList = new List <FunctionData>();
                int lineNr = 1;
                while (!reader.EndOfStream)
                {
                    string functionLine      = reader.ReadLine();
                    string functionLineWhole = functionLine;
                    try
                    {
                        if (functionLine.Substring(0, 7).ToLower() == "header ")
                        {
                            functionList.Add(new FunctionData(functionLine.Substring(7)));
                            continue;
                        }

                        FunctionData function = new FunctionData();

                        bool defaultInclude = false;
                        if (functionLine.Substring(0, 8) == "DEFAULT ")
                        {
                            defaultInclude = true;
                            functionLine   = functionLine.Substring(8);
                        }

                        if (functionLine.Substring(0, 9) == "override ")
                        {
                            function.isVirtual = true;
                            functionLine       = functionLine.Substring(9);
                        }

                        string returnTypeString = GetStringUntilSeperator(ref functionLine, " ");
                        function.returnType = (returnTypeString == "void" ? null : returnTypeString);
                        function.name       = GetStringUntilSeperator(ref functionLine, "(");
                        string parameterString = GetStringUntilSeperator(ref functionLine, ")");
                        if (function.returnType != null)
                        {
                            function.returnDefault = GetStringUntilSeperator(ref functionLine, ";");
                        }
                        function.comment = functionLine;

                        string[]             parameterStrings = parameterString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        List <ParameterData> parameterList    = new List <ParameterData>();
                        for (int i = 0; i < parameterStrings.Length; i++)
                        {
                            string[] paramSplit = parameterStrings[i].Trim().Split(' ');
                            parameterList.Add(new ParameterData(paramSplit[1], paramSplit[0]));
                        }
                        function.parameters = parameterList.ToArray();

                        function.include = GetFunctionIsIncluded(m_BaseClass, function.name, defaultInclude);

                        functionList.Add(function);
                    }
                    catch (Exception e)
                    {
                        Debug.LogWarning("Malformed function line: \"" + functionLineWhole + "\"\n  at " + functionDataFilePath + ":" + lineNr + "\n" + e);
                    }
                    lineNr++;
                }
                m_ScriptPrescription.m_Functions = functionList.ToArray();
            }
        }
        private void WriteFunction(FunctionData function)
        {
            string paramString = string.Empty;
            string overrideString;
            string returnTypeString;
            string functionContentString;

            switch (m_ScriptPrescription.m_Lang)
            {
            case Language.JavaScript:
                // Comment
                WriteComment(function.comment);

                // Function header
                for (int i = 0; i < function.parameters.Length; i++)
                {
                    paramString += function.parameters[i].name + " : " + TranslateTypeToJavascript(function.parameters[i].type);
                    if (i < function.parameters.Length - 1)
                    {
                        paramString += ", ";
                    }
                }
                overrideString   = (function.isVirtual ? "override " : string.Empty);
                returnTypeString = (function.returnType == null ? " " : " : " + TranslateTypeToJavascript(function.returnType) + " ");
                m_Writer.WriteLine(m_Indentation + overrideString + "function " + function.name + " (" + paramString + ")" + returnTypeString + "{");

                // Function content
                IndentLevel++;
                functionContentString = (function.returnType == null ? string.Empty : function.returnDefault + ";");
                m_Writer.WriteLine(m_Indentation + functionContentString);
                IndentLevel--;
                m_Writer.WriteLine(m_Indentation + "}");

                break;

            case Language.CSharp:
                // Comment
                WriteComment(function.comment);

                // Function header
                for (int i = 0; i < function.parameters.Length; i++)
                {
                    paramString += function.parameters[i].type + " " + function.parameters[i].name;
                    if (i < function.parameters.Length - 1)
                    {
                        paramString += ", ";
                    }
                }
                overrideString   = (function.isVirtual ? "public override " : string.Empty);
                returnTypeString = (function.returnType == null ? "void " : function.returnType + " ");
                m_Writer.WriteLine(m_Indentation + overrideString + returnTypeString + function.name + " (" + paramString + ") {");

                // Function content
                IndentLevel++;
                functionContentString = (function.returnType == null ? string.Empty : function.returnDefault + ";");
                m_Writer.WriteLine(m_Indentation + functionContentString);
                IndentLevel--;
                m_Writer.WriteLine(m_Indentation + "}");

                break;

            case Language.Boo:
                // Comment
                WriteComment(function.comment);

                // Function header
                for (int i = 0; i < function.parameters.Length; i++)
                {
                    paramString += function.parameters[i].name + " as " + TranslateTypeToBoo(function.parameters[i].type);
                    if (i < function.parameters.Length - 1)
                    {
                        paramString += ", ";
                    }
                }
                overrideString   = (function.isVirtual ? "public override " : string.Empty);
                returnTypeString = (function.returnType == null ? string.Empty : " as " + TranslateTypeToJavascript(function.returnType));
                m_Writer.WriteLine(m_Indentation + overrideString + "def " + function.name + " (" + paramString + ")" + returnTypeString + ":");

                // Function content
                IndentLevel++;
                functionContentString = (function.returnType == null ? "pass" : function.returnDefault);
                m_Writer.WriteLine(m_Indentation + functionContentString);
                IndentLevel--;

                break;
            }
        }