/// <summary>Scan for available functions and update funtion references if needed.</summary>
        public void Scan()
        {
            try {
                JITFunction.Scan(functionDir, kalc);
                error = null;
            } catch (Exception e) {
                error     = "Cannot access function dir (" + functionDir + "): " + e.Message;
                functions = new Dictionary <string, JITFunction>();
                throw e;
            }

            if (selectedFunction != null)
            {
                if (functions.ContainsKey(selectedFunction.Id))
                {
                    selectedFunction    = functions[selectedFunction.Id];
                    functionDescription = FunctionDescription(selectedFunction);
                }
                else
                {
                    selectedFunction    = null;
                    functionDescription = "";
                }

                functionDescriptionHeight = GUI.skin.GetStyle("label").CalcHeight(new GUIContent(functionDescription), 225);
            }

            if (editFunction != null)
            {
                if (functions.ContainsKey(editFunction.Id))
                {
                    editFunction = functions[editFunction.Id];
                }
                else
                {
                    editFunction      = null;
                    editWindowEnabled = false;
                }
            }

            if (runFunction != null)
            {
                if (functions.ContainsKey(runFunction.Id))
                {
                    runFunction = functions[runFunction.Id];
                }
                else
                {
                    runFunction = null;
                }

                functionOutput = "";
            }

            List <int> envsToRemove = new List <int>();

            foreach (KeyValuePair <int, ExecutionEnvironment> pair in envs)
            {
                if (functions.ContainsKey(pair.Value.func.Id))
                {
                    pair.Value.func = functions[pair.Value.func.Id];
                }
                else
                {
                    envsToRemove.Add(pair.Key);
                }
            }
            foreach (int id in envsToRemove)
            {
                envs.Remove(id);
            }
        }
        /// <summary>Draws the GUI</summary>
        public void OnGUI()
        {
            // Initiate styles
            if (!stylesInitiated)
            {
                keyboard         = new GUIStyle(GUI.skin.GetStyle("button"));
                keyboard.padding = new RectOffset(0, 0, 2, 2);

                defaultButton         = new GUIStyle(GUI.skin.GetStyle("button"));
                defaultButton.padding = new RectOffset(4, 4, 4, 4);

                tooltipStyle = new GUIStyle(GUI.skin.GetStyle("label"));
                Texture2D texBack = new Texture2D(1, 1, TextureFormat.ARGB32, false);
                texBack.SetPixel(0, 0, new Color(0.0f, 0.0f, 0.0f, 1f));
                texBack.Apply();
                tooltipStyle.normal.background = texBack;

                validFunctionName   = new GUIStyle(GUI.skin.GetStyle("TextField"));
                editFunctionStyle   = validFunctionName;
                invalidFunctionName = new GUIStyle(GUI.skin.GetStyle("TextField"));
                invalidFunctionName.normal.textColor  = Color.red;
                invalidFunctionName.active.textColor  = Color.red;
                invalidFunctionName.hover.textColor   = Color.red;
                invalidFunctionName.focused.textColor = Color.red;

                stylesInitiated = true;
            }

            if (drawMainButton)
            {
                // Draw the main button
                if (GUI.Button(mainButtonPos, kerbulatorIcon, defaultButton))
                {
                    ChangeState(!mainWindowEnabled);
                }
            }

            if (reload)
            {
                reload = false;

                // Rebuild the list of functions. It could be that they were edited outside of KSP
                JITFunction.Scan(functionDir, kalc);

                // Reload the function being edited
                if (editFunction != null)
                {
                    editFunctionContent = System.IO.File.ReadAllText(functionFile);
                }
            }

            // Draw the windows (if enabled)
            if (mainWindowEnabled)
            {
                options.mainWindowPos = GUI.Window(windowId, options.mainWindowPos, DrawMainWindow, "Kerbulator");
                glue.PreventClickthrough(options.mainWindowPos, "KerbulatorMainWindow");
            }
            else
            {
                glue.EnsureLockReleased("KerbulatorMainWindow");
            }

            if (editWindowEnabled)
            {
                options.editWindowPos = GUI.Window(windowId + 1, options.editWindowPos, DrawEditWindow, "Function Editor");
                glue.PreventClickthrough(options.editWindowPos, "KerbulatorEditWindow");
            }
            else
            {
                glue.EnsureLockReleased("KerbulatorEditWindow");
            }

            if (runWindowEnabled)
            {
                options.runWindowPos = GUI.Window(windowId + 2, options.runWindowPos, DrawRunWindow, "Run " + RunFunction.Id);
                glue.PreventClickthrough(options.runWindowPos, "KerbulatorRunWindow");
            }
            else
            {
                glue.EnsureLockReleased("KerbulatorRunWindow");
            }

            if (running)
            {
                foreach (KeyValuePair <int, ExecutionEnvironment> pair in envs)
                {
                    pair.Value.windowPos = GUI.Window(pair.Key, pair.Value.windowPos, DrawRepeatedWindow, pair.Value.func.Id);
                    glue.PreventClickthrough(pair.Value.windowPos, "KerbulatorEnvironment" + pair.Value.func.Id);
                }
            }

            DrawToolTip();
        }
示例#3
0
        public Kerbulator(string functionDir)
        {
            operators = new Dictionary <string, Operator>();
            operators.Add("=", new Operator("=", 1, Arity.BINARY));                               // Assignment
            operators.Add("-", new Operator("-", 4, Arity.BOTH));                                 // Substraction or negation
            operators.Add("+", new Operator("+", 4, Arity.BINARY));                               // Addition
            operators.Add("/", new Operator("/", 5, Arity.BINARY));                               // Division
            operators.Add("÷", new Operator("÷", 5, Arity.BINARY));                               // Division
            operators.Add("√", new Operator("√", 5, Arity.BOTH));                                 // Square Root or ^(1/n)
            operators.Add("%", new Operator("%", 5, Arity.BINARY));                               // Modulo
            operators.Add("*", new Operator("*", 5, Arity.BINARY));                               // Multiplication
            operators.Add("·", new Operator("·", 5, Arity.BINARY));                               // Multiplication
            operators.Add("×", new Operator("×", 5, Arity.BINARY));                               // Multiplication
            operators.Add("^", new Operator("^", 6, Arity.BINARY));                               // Multiplication
            operators.Add("|", new Operator("|", 6, Arity.UNARY));                                // Absolute
            operators.Add("⌊", new Operator("⌊", 6, Arity.UNARY));                                // Floor
            operators.Add("⌈", new Operator("⌈", 6, Arity.UNARY));                                // Ceiling
            operators.Add("<", new Operator("<", 3, Arity.BINARY));                               // Less than
            operators.Add(">", new Operator(">", 3, Arity.BINARY));                               // Greater than
            operators.Add("<=", new Operator("<=", 3, Arity.BINARY));                             // Less than or equal
            operators.Add("≤", new Operator("≤", 3, Arity.BINARY));                               // Less than or equal
            operators.Add(">=", new Operator(">=", 3, Arity.BINARY));                             // Greater than or equal
            operators.Add("≥", new Operator("≥", 3, Arity.BINARY));                               // Greater than or equal
            operators.Add("==", new Operator("==", 3, Arity.BINARY));                             // Equals
            operators.Add("!=", new Operator("!=", 3, Arity.BINARY));                             // Not equals
            operators.Add("≠", new Operator("≠", 3, Arity.BINARY));                               // Not equals
            operators.Add("!", new Operator("!", 6, Arity.UNARY));                                // Boolean not
            operators.Add("¬", new Operator("¬", 6, Arity.UNARY));                                // Boolean not
            operators.Add("and", new Operator("and", 2, Arity.BINARY));                           // AND
            operators.Add("∧", new Operator("∧", 2, Arity.BINARY));                               // AND
            operators.Add("or", new Operator("or", 2, Arity.BINARY));                             // OR
            operators.Add("∨", new Operator("∨", 2, Arity.BINARY));                               // OR
            operators.Add("buildin-function", new Operator("buildin-function", 5, Arity.BINARY)); // Execute buildin function as unary operator
            operators.Add("user-function", new Operator("user-function", 5, Arity.BINARY));       // Execute user function as unary operator

            buildInFunctions = new Dictionary <string, BuildInFunction>();
            buildInFunctions.Add("abs", new BuildInFunction("abs", 1));
            buildInFunctions.Add("acos", new BuildInFunction("acos", 1));
            buildInFunctions.Add("acos_rad", new BuildInFunction("acos_rad", 1));
            buildInFunctions.Add("asin", new BuildInFunction("asin", 1));
            buildInFunctions.Add("asin_rad", new BuildInFunction("asin_rad", 1));
            buildInFunctions.Add("atan", new BuildInFunction("atan", 1));
            buildInFunctions.Add("atan_rad", new BuildInFunction("atan_rad", 1));
            buildInFunctions.Add("atan2", new BuildInFunction("atan2", 2));
            buildInFunctions.Add("atan2_rad", new BuildInFunction("atan2_rad", 2));
            buildInFunctions.Add("ceil", new BuildInFunction("ceil", 1));
            buildInFunctions.Add("cos", new BuildInFunction("cos", 1));
            buildInFunctions.Add("cos_rad", new BuildInFunction("cos_rad", 1));
            buildInFunctions.Add("cosh", new BuildInFunction("cosh", 1));
            buildInFunctions.Add("cosh_rad", new BuildInFunction("cosh_rad", 1));
            buildInFunctions.Add("exp", new BuildInFunction("exp", 1));
            buildInFunctions.Add("floor", new BuildInFunction("floor", 1));
            buildInFunctions.Add("ln", new BuildInFunction("ln", 1));
            buildInFunctions.Add("log", new BuildInFunction("log", 1));
            buildInFunctions.Add("log10", new BuildInFunction("log10", 1));
            buildInFunctions.Add("max", new BuildInFunction("max", 2));
            buildInFunctions.Add("min", new BuildInFunction("min", 2));
            buildInFunctions.Add("pow", new BuildInFunction("pow", 2));
            buildInFunctions.Add("rand", new BuildInFunction("rand", 0));
            buildInFunctions.Add("round", new BuildInFunction("round", 2));
            buildInFunctions.Add("sign", new BuildInFunction("sign", 1));
            buildInFunctions.Add("sin", new BuildInFunction("sin", 1));
            buildInFunctions.Add("sin_rad", new BuildInFunction("sin_rad", 1));
            buildInFunctions.Add("sinh", new BuildInFunction("sinh", 1));
            buildInFunctions.Add("sinh_rad", new BuildInFunction("sinh_rad", 1));
            buildInFunctions.Add("sqrt", new BuildInFunction("sqrt", 1));
            buildInFunctions.Add("tan", new BuildInFunction("tan", 1));
            buildInFunctions.Add("tan_rad", new BuildInFunction("tan_rad", 1));
            buildInFunctions.Add("tanh", new BuildInFunction("tanh", 1));
            buildInFunctions.Add("tanh_rad", new BuildInFunction("tanh_rad", 1));
            buildInFunctions.Add("len", new BuildInFunction("len", 1));
            buildInFunctions.Add("mag", new BuildInFunction("mag", 1));
            buildInFunctions.Add("norm", new BuildInFunction("norm", 1));
            buildInFunctions.Add("dot", new BuildInFunction("dot", 2));
            buildInFunctions.Add("cross", new BuildInFunction("cross", 2));
            buildInFunctions.Add("all", new BuildInFunction("all", 1));
            buildInFunctions.Add("any", new BuildInFunction("any", 1));

            globals = new Dictionary <string, Object>();
            globals.Add("pi", Math.PI);
            globals.Add("π", Math.PI);
            globals.Add("e", Math.E);
            globals.Add("G", 6.67408E-11);
            globals.Add("Inf", double.PositiveInfinity);
            globals.Add("∞", double.PositiveInfinity);

            functions = new SortedDictionary <string, JITFunction>();
            JITFunction.Scan(functionDir, this);
        }
示例#4
0
        public Kerbulator(string functionDir)
        {
            operators = new Dictionary <string, Operator>();
            operators.Add("=", new Operator("=", 1, Arity.BINARY));                               // Assignment
            operators.Add("-", new Operator("-", 1, Arity.BOTH));                                 // Substraction or negation
            operators.Add("+", new Operator("+", 1, Arity.BINARY));                               // Addition
            operators.Add("/", new Operator("/", 2, Arity.BINARY));                               // Division
            operators.Add("÷", new Operator("÷", 2, Arity.BINARY));                               // Division
            operators.Add("√", new Operator("√", 2, Arity.BOTH));                                 // Square Root or ^(1/n)
            operators.Add("%", new Operator("%", 2, Arity.BINARY));                               // Modulo
            operators.Add("*", new Operator("*", 2, Arity.BINARY));                               // Multiplication
            operators.Add("·", new Operator("·", 2, Arity.BINARY));                               // Multiplication
            operators.Add("×", new Operator("×", 2, Arity.BINARY));                               // Multiplication
            operators.Add("^", new Operator("^", 3, Arity.BINARY));                               // Multiplication
            operators.Add("|", new Operator("|", 3, Arity.UNARY));                                // Absolute
            operators.Add("⌊", new Operator("⌊", 3, Arity.UNARY));                                // Floor
            operators.Add("⌈", new Operator("⌈", 3, Arity.UNARY));                                // Ceiling
            operators.Add("func", new Operator("func", 2, Arity.BINARY));                         // Execute buildin function as unary operator
            operators.Add("buildin-function", new Operator("buildin-function", 2, Arity.BINARY)); // Execute buildin function as unary operator
            operators.Add("user-function", new Operator("user-function", 2, Arity.BINARY));       // Execute user function as unary operator

            buildInFunctions = new Dictionary <string, BuildInFunction>();
            buildInFunctions.Add("abs", new BuildInFunction("abs", 1));
            buildInFunctions.Add("acos", new BuildInFunction("acos", 1));
            buildInFunctions.Add("acos_rad", new BuildInFunction("acos_rad", 1));
            buildInFunctions.Add("asin", new BuildInFunction("asin", 1));
            buildInFunctions.Add("asin_rad", new BuildInFunction("asin_rad", 1));
            buildInFunctions.Add("atan", new BuildInFunction("atan", 1));
            buildInFunctions.Add("atan_rad", new BuildInFunction("atan_rad", 1));
            buildInFunctions.Add("ceil", new BuildInFunction("ceil", 1));
            buildInFunctions.Add("cos", new BuildInFunction("cos", 1));
            buildInFunctions.Add("cos_rad", new BuildInFunction("cos_rad", 1));
            buildInFunctions.Add("cosh", new BuildInFunction("cosh", 1));
            buildInFunctions.Add("cosh_rad", new BuildInFunction("cosh_rad", 1));
            buildInFunctions.Add("exp", new BuildInFunction("exp", 1));
            buildInFunctions.Add("floor", new BuildInFunction("floor", 1));
            buildInFunctions.Add("ln", new BuildInFunction("ln", 1));
            buildInFunctions.Add("log", new BuildInFunction("log", 1));
            buildInFunctions.Add("log10", new BuildInFunction("log10", 1));
            buildInFunctions.Add("max", new BuildInFunction("max", 2));
            buildInFunctions.Add("min", new BuildInFunction("min", 2));
            buildInFunctions.Add("pow", new BuildInFunction("pow", 2));
            buildInFunctions.Add("rand", new BuildInFunction("rand", 0));
            buildInFunctions.Add("round", new BuildInFunction("round", 2));
            buildInFunctions.Add("sign", new BuildInFunction("sign", 1));
            buildInFunctions.Add("sin", new BuildInFunction("sin", 1));
            buildInFunctions.Add("sin_rad", new BuildInFunction("sin_rad", 1));
            buildInFunctions.Add("sinh", new BuildInFunction("sinh", 1));
            buildInFunctions.Add("sinh_rad", new BuildInFunction("sinh_rad", 1));
            buildInFunctions.Add("sqrt", new BuildInFunction("sqrt", 1));
            buildInFunctions.Add("tan", new BuildInFunction("tan", 1));
            buildInFunctions.Add("tan_rad", new BuildInFunction("tan_rad", 1));
            buildInFunctions.Add("tanh", new BuildInFunction("tanh", 1));
            buildInFunctions.Add("tanh_rad", new BuildInFunction("tanh_rad", 1));
            buildInFunctions.Add("len", new BuildInFunction("len", 1));
            buildInFunctions.Add("mag", new BuildInFunction("mag", 1));
            buildInFunctions.Add("norm", new BuildInFunction("norm", 1));
            buildInFunctions.Add("dot", new BuildInFunction("dot", 2));
            buildInFunctions.Add("cross", new BuildInFunction("cross", 2));

            globals = new Dictionary <string, Object>();
            globals.Add("pi", Math.PI);
            globals.Add("π", Math.PI);
            globals.Add("e", Math.E);
            globals.Add("G", 6.67384E-11);

            functions = new SortedDictionary <string, JITFunction>();
            JITFunction.Scan(functionDir, this);
        }
示例#5
0
        /// <summary>Draws the GUI</summary>
        public void OnGUI()
        {
            // Initiate styles
            if (!stylesInitiated)
            {
                keyboard         = new GUIStyle(GUI.skin.GetStyle("button"));
                keyboard.padding = new RectOffset(0, 0, 2, 2);

                defaultButton         = new GUIStyle(GUI.skin.GetStyle("button"));
                defaultButton.padding = new RectOffset(4, 4, 4, 4);

                tooltipStyle = new GUIStyle(GUI.skin.GetStyle("label"));
                Texture2D texBack = new Texture2D(1, 1, TextureFormat.ARGB32, false);
                texBack.SetPixel(0, 0, new Color(0.0f, 0.0f, 0.0f, 1f));
                texBack.Apply();
                tooltipStyle.normal.background = texBack;

                stylesInitiated = true;
            }

            if (drawMainButton)
            {
                // Draw the main button
                if (GUI.Button(mainButtonPos, kerbulatorIcon, defaultButton))
                {
                    ChangeState(!mainWindowEnabled);
                }
            }

            if (reload)
            {
                reload = false;

                // Rebuild the list of functions. It could be that they were edited outside of KSP
                JITFunction.Scan(functionDir, kalc);

                // Reload the function being edited
                if (editFunction != null)
                {
                    editFunctionContent = System.IO.File.ReadAllText(functionFile);
                }
            }


            // Draw the windows (if enabled)
            if (mainWindowEnabled)
            {
                mainWindowPos = GUILayout.Window(windowId, mainWindowPos, DrawMainWindow, "Kerbulator", GUILayout.ExpandHeight(false));
            }

            if (editWindowEnabled)
            {
                editWindowPos = GUILayout.Window(windowId + 1, editWindowPos, DrawEditWindow, "Function Editor", GUILayout.ExpandHeight(false));
            }

            if (runWindowEnabled)
            {
                runWindowPos = GUILayout.Window(windowId + 2, runWindowPos, DrawRunWindow, "Run " + RunFunction.Id, GUILayout.ExpandHeight(false));
            }

            if (running)
            {
                foreach (KeyValuePair <int, ExecutionEnvironment> pair in envs)
                {
                    pair.Value.windowPos = GUILayout.Window(pair.Key, pair.Value.windowPos, DrawRepeatedWindow, pair.Value.func.Id, GUILayout.ExpandHeight(false));
                }
            }

            DrawToolTip();
        }