Example #1
0
 public Graph()
 {
     Hash1 = ShaderDefs.DefaultHash1;
     Hash2 = ShaderDefs.DefaultHash2;
     Hash3 = ShaderDefs.DefaultHash3;
     Output = new NodeInput(0.5f);
     OutputPos = new Rect(200.0f, 0.0f, 100.0f, 50.0f);
 }
Example #2
0
        //Serialization code:
        public Graph(SerializationInfo info, StreamingContext context)
        {
            NextUID = info.GetInt32("NextUID");
            Output = (NodeInput)info.GetValue("Output", typeof(NodeInput));

            float posX = info.GetSingle("OutputPosX"),
                  posY = info.GetSingle("OutputPosY"),
                  sizeX = info.GetSingle("OutputSizeX"),
                  sizeY = info.GetSingle("OutputSizeY");
            OutputPos = new Rect(posX, posY, sizeX, sizeY);

            Hash1 = info.GetString("Hash1");
            Hash2 = info.GetString("Hash2");
            Hash3 = info.GetString("Hash3");

            int nNodes = info.GetInt32("NNodes");
            for (int i = 0; i < nNodes; ++i)
            {
                nodes.Add((Node)info.GetValue("Node" + i.ToString(), typeof(Node)));
            }
        }
Example #3
0
        /// <summary>
        /// Runs the GUI display window for this node.
        /// Returns what happened.
        /// </summary>
        /// <param name="clickedInput">
        /// If the user clicked an input,
        /// the index of that input will be stored in this variable.
        /// </param>
        /// <param name="isSelected">
        /// If this node's output was previously selected, pass -1.
        /// If an input was selected, pass the index of that input.
        /// Otherwise, pass anything else.
        /// </param>
        public GUIResults OnGUI(ref int clickedInput, int isSelected)
        {
            GUIResults result = GUIResults.Nothing;

            GUILayout.BeginHorizontal();

            GUILayout.BeginVertical();

            for (int i = 0; i < Inputs.Count; ++i)
            {
                GUILayout.BeginHorizontal();

                GUILayout.Label(InputNames[i]);

                //Button to select input.
                string buttStr = (isSelected == i ? "x" : "X");
                if (GUILayout.Button(buttStr))
                {
                    result = GUIResults.ClickInput;
                    clickedInput = i;
                }

                //If this input is a constant, expose a text box to edit it.
                if (Inputs[i].IsAConstant)
                {
                    float newVal = EditorGUILayout.FloatField(Inputs[i].ConstantValue);
                    if (AreFloatsDifferent(newVal, Inputs[i].ConstantValue))
                    {
                        result = GUIResults.Other;
                        Inputs[i] = new NodeInput(newVal);
                    }
                }
                //Otherwise, expose a button to release the connection.
                else
                {
                    Rect otherPos = Owner.GetNode(Inputs[i].NodeID).Pos;
                    Vector2 endPos = new Vector2(otherPos.xMax, otherPos.yMin + OutputHeight) - Pos.min;

                    GUIUtil.DrawLine(new Vector2(0.0f, TitleBarHeight + ((float)i * InputSpacing)),
                                     endPos, 2.0f, Color.white);

                    if (GUILayout.Button("Disconnect"))
                    {
                        Inputs[i] = new NodeInput(InputDefaultVals[i]);

                        result = GUIResults.Other;
                    }
                }

                GUILayout.EndHorizontal();
            }

            if (CustomGUI())
            {
                result = GUIResults.Other;
            }

            GUILayout.EndVertical();
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();

            //Output button.
            if (GUILayout.Button(isSelected == -1 ? "o" : "O"))
            {
                result = GUIResults.ClickOutput;
            }

            GUILayout.EndVertical();

            GUILayout.EndHorizontal();

            //"Duplicate" button.
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Duplicate"))
            {
                result = GUIResults.Duplicate;
            }

            GUILayout.FlexibleSpace();

            //"Delete" button.
            if (GUILayout.Button("Delete"))
            {
                result = GUIResults.Delete;
            }

            GUILayout.EndHorizontal();

            return result;
        }
Example #4
0
        public void RemoveNode(Node n)
        {
            nodes.Remove(n);
            uidToNode.Remove(n.UID);
            n.Owner = null;

            //Remove any connections to the node.
            if (!Output.IsAConstant && Output.NodeID == n.UID)
                Output = new NodeInput(0.5f);
            foreach (Node n2 in nodes)
                for (int i = 0; i < n2.Inputs.Count; ++i)
                    if (!n2.Inputs[i].IsAConstant && n.UID == n2.Inputs[i].NodeID)
                        n2.Inputs[i] = new NodeInput(n2.GetInputDefaultValue(i));
        }
Example #5
0
        /// <summary>
        /// Re-loads this graph from its file-path, effectively wiping out any changes to it.
        /// Returns an error message, or an empty string if nothing went wrong.
        /// </summary>
        public string Load()
        {
            IFormatter formatter = new BinaryFormatter();
            Stream s = null;
            Graph g = null;

            try
            {
                s = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                g = (Graph)formatter.Deserialize(s);
            }
            catch (System.Exception e)
            {
                return "Error opening/reading file: " + e.Message;
            }
            finally
            {
                if (s != null)
                    s.Close();
            }

            NextUID = g.NextUID;
            Hash1 = g.Hash1;
            Hash2 = g.Hash2;
            Hash3 = g.Hash3;
            Output = g.Output;
            OutputPos = g.OutputPos;
            nodes = g.nodes;
            uidToNode = g.uidToNode;

            foreach (Node n in nodes)
                n.Owner = this;
            foreach (Node n in nodes)
                n.OnGraphLoaded();

            return "";
        }