private void OnGUI()
        {
            m_event         = Event.current;
            m_mousePosition = m_event.mousePosition;

            DrawNodes();

            DrawMainMenu();

            Repaint();

            //Move view
            if (m_event.button == 0 && m_event.type == EventType.MouseDrag)
            {
                if (GetClickedNode() == null)
                {
                    m_offSetView += m_event.delta;
                }
            }

            //Opening a creating node menu
            if (m_event.button == 1 && m_event.type == EventType.MouseDown)
            {
                m_clickedNode = GetClickedNode();
                if (m_clickedNode == null)
                {
                    m_createNodesMenu.ShowAsContext();
                }
                else
                {
                    m_nodeMenu.ShowAsContext();
                }
            }

            //Connect a output/input
            if (m_clickedOutput != null && m_clickedInput != null)
            {
                NodeUtility.ConnectNode(m_clickedInput, m_clickedOutput);

                m_clickedOutput = null;
                m_clickedInput  = null;
            }

            //Remove de current m_clickedOutput
            if (m_event.button == 0 && m_event.type == EventType.MouseDown)
            {
                if (GetClickedNode() == null && m_clickedOutput != null)
                {
                    m_clickedOutput = null;
                }
            }

            //Deconnect a input/output
            if (m_clickedInput != null)
            {
                DeconnectInputOutput();

                m_clickedInput = null;
            }

            //Display the current curve
            if (m_clickedOutput != null)
            {
                Rect rect = m_clickedOutput.m_rect;

                Vector3 startPosition = rect.position + Vector2.Scale(rect.size, new Vector2(1.0f, 0.5f)) + m_offSetView;
                Vector3 endPosition   = m_mousePosition;

                Vector3 startTangente = startPosition + Vector3.right * 50;
                Vector3 endTangente   = endPosition + Vector3.left * 50;

                Handles.DrawBezier(startPosition, endPosition, startTangente, endTangente, Color.black, null, 2);
            }
        }
Exemple #2
0
        /// <summary>
        /// return null if path is null or empty, file doesnt exists, otherwise, true.
        /// </summary>
        static public Graph LoadFromFile(string path)
        {
            if (!string.IsNullOrEmpty(path) && File.Exists(path))
            {
                Graph newGraph = new Graph();

                StreamReader sr      = File.OpenText(path);
                JObject      allData = JObject.Parse(sr.ReadToEnd());
                sr.Close();

                List <JObject> jinputs = new List <JObject>();
                foreach (var node in allData["nodes"])
                {
                    if ((string)node["type"] == "IntNode")
                    {
                        int     id           = (int)node["id"];
                        Vector2 position     = new Vector2((float)node["position"]["x"], (float)node["position"]["y"]);
                        string  variableName = (string)node["variableName"];
                        int     variable     = (int)node["variable"];

                        IntNode intNode = new IntNode(id, position);
                        intNode.m_variable     = variable;
                        intNode.m_variableName = variableName;
                        newGraph.m_nodes.Add(intNode);
                    }
                    else if ((string)node["type"] == "AdditionIntNode")
                    {
                        int     id       = (int)node["id"];
                        Vector2 position = new Vector2((float)node["position"]["x"], (float)node["position"]["y"]);

                        AdditionIntNode additionIntNode = new AdditionIntNode(id, position);
                        newGraph.m_nodes.Add(additionIntNode);

                        jinputs.Add(new JObject(new JProperty("idParent", id), new JProperty("inputs", node["inputs"])));
                    }
                    else if ((string)node["type"] == "SubstractionIntNode")
                    {
                        int     id       = (int)node["id"];
                        Vector2 position = new Vector2((float)node["position"]["x"], (float)node["position"]["y"]);

                        SubstractionIntNode additionIntNode = new SubstractionIntNode(id, position);
                        newGraph.m_nodes.Add(additionIntNode);

                        jinputs.Add(new JObject(new JProperty("idParent", id), new JProperty("inputs", node["inputs"])));
                    }
                    else if ((string)node["type"] == "MultiplicationIntNode")
                    {
                        int     id       = (int)node["id"];
                        Vector2 position = new Vector2((float)node["position"]["x"], (float)node["position"]["y"]);

                        MultiplicationIntNode additionIntNode = new MultiplicationIntNode(id, position);
                        newGraph.m_nodes.Add(additionIntNode);

                        jinputs.Add(new JObject(new JProperty("idParent", id), new JProperty("inputs", node["inputs"])));
                    }
                    else if ((string)node["type"] == "DivisionIntNode")
                    {
                        int     id       = (int)node["id"];
                        Vector2 position = new Vector2((float)node["position"]["x"], (float)node["position"]["y"]);

                        DivisionIntNode additionIntNode = new DivisionIntNode(id, position);
                        newGraph.m_nodes.Add(additionIntNode);

                        jinputs.Add(new JObject(new JProperty("idParent", id), new JProperty("inputs", node["inputs"])));
                    }
                }

                foreach (var jinput in jinputs)
                {
                    int idParent = (int)jinput["idParent"];
                    foreach (var input in jinput["inputs"])
                    {
                        int indexInput = (int)input["index"];

                        int idNode      = (int)input["output"]["idNode"];
                        int indexOutput = (int)input["output"]["index"];

                        NodeUtility.ConnectNode(newGraph.m_nodes[idParent].m_inputs[indexInput], newGraph.m_nodes[idNode].m_outputs[indexOutput]);
                    }
                }

                return(newGraph);
            }

            return(null);
        }