private void ProcessMouseDownOverNode(Event e)
        {
            bool isLeftClickDown  = e.type == EventType.MouseDown && e.button == 0;
            bool isRightClickDown = e.type == EventType.MouseDown && e.button == 1;

            if (isLeftClickDown || isRightClickDown)
            {
                if (currentGraph != null && currentGraph.nodes != null)
                {
                    foreach (AG_Node node in currentGraph.nodes)
                    {
                        if (node.nodeRect.Contains(e.mousePosition))
                        {
                            if (isLeftClickDown)
                            {
                                if (node.CanBeDragged(e))
                                {
                                    selectedNode = node;
                                }
                            }
                            else if (isRightClickDown)
                            {
                                ProcessContextMenuNode(e, node);
                            }

                            e.Use();
                        }
                    }
                }
            }
        }
        private void ProcessMouseDownOverPoint(Event e)
        {
            bool isLeftClickDown = e.type == EventType.MouseDown && e.button == 0;

            if (isLeftClickDown && currentGraph != null && currentGraph.nodes != null)
            {
                foreach (AG_Node node in currentGraph.nodes)
                {
                    if (node.outputPoints != null)
                    {
                        foreach (AG_NodeLinkPoint point in node.outputPoints)
                        {
                            if (point.clickableRect.Contains(e.mousePosition))
                            {
                                draggingLinkNode   = true;
                                clickedInputNode   = node;
                                clickedOutputPoint = point;

                                e.Use();
                            }
                        }
                    }
                }
            }
        }
 public AG_NodeLink(AG_Node _inputNode, AG_NodeLinkPoint _beginningPoint, AG_Node _outputNode, AG_NodeLinkPoint _endingPoint)
 {
     inputNode      = _inputNode;
     beginningPoint = _beginningPoint;
     outputNode     = _outputNode;
     endingPoint    = _endingPoint;
 }
 public AG_NodeLink(AG_Node _inputNode, AG_NodeLinkPoint _beginningPoint, AG_Node _outputNode, AG_NodeLinkPoint _endingPoint, int _duration)
 {
     inputNode      = _inputNode;
     beginningPoint = _beginningPoint;
     outputNode     = _outputNode;
     endingPoint    = _endingPoint;
     duration       = _duration;
 }
        private void ContextCallback(object obj)
        {
            switch (obj.ToString())
            {
            case "0":
                AG_CreateGraphWindow.InitNodePopup();
                break;

            case "1":
                LoadGraph();
                break;

            case "2":

                AG_Node beginNode = AG_Node.CreateNode(mousePosition, currentGraph, AG_NodeType.Begin);

                beginNode.nodeType = AG_NodeType.Begin;

                AssetDatabase.AddObjectToAsset(beginNode, currentGraph);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();

                currentGraph.nodes.Add(beginNode);

                break;



            case "3":

                AG_Node dialogueNode = AG_Node.CreateNode(mousePosition, currentGraph, AG_NodeType.Dialogue);

                dialogueNode.nodeType = AG_NodeType.Dialogue;

                AssetDatabase.AddObjectToAsset(dialogueNode, currentGraph);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();

                currentGraph.nodes.Add(dialogueNode);

                break;

            case "4":

                AG_Node testNode = AG_Node.CreateNode(mousePosition, currentGraph, AG_NodeType.Test);

                testNode.nodeType = AG_NodeType.Test;

                AssetDatabase.AddObjectToAsset(testNode, currentGraph);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();

                currentGraph.nodes.Add(testNode);

                break;
            }
        }
 public static void DeleteNode(AG_Node currentNode, AG_Graph currentGraph)
 {
     if (currentGraph != null)
     {
         currentGraph.nodes.Remove(currentNode);
         GameObject.DestroyImmediate(currentNode, true);
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();
     }
 }
        private void ProcessContextMenuNode(Event e, AG_Node node)
        {
            GenericMenu menu = new GenericMenu();

            mousePosition = e.mousePosition;

            rightClickNode = node;

            menu.AddItem(new GUIContent("Delete node"), false, ContextCallbackNode, "0");

            menu.ShowAsContext();
            e.Use();
        }
        private void ProcessGenericMouseUp(Event e)
        {
            bool isLeftClickUp = e.type == EventType.MouseUp && e.button == 0;

            if (isLeftClickUp)
            {
                DeselectAllNodes();

                draggingLinkNode   = false;
                clickedInputNode   = null;
                clickedOutputPoint = null;

                selectedNode = null;

                e.Use();
            }
        }
        public static AG_Node CreateNode(Vector2 mousePosition, AG_Graph parentGraph, AG_NodeType nodeType)
        {
            AG_Node currentNode = null;

            switch (nodeType)
            {
            case AG_NodeType.Dialogue:
                currentNode          = ScriptableObject.CreateInstance <AG_DialogueNode>();
                currentNode.nodeName = "Dialogue Node";
                break;

            case AG_NodeType.Begin:
                currentNode          = ScriptableObject.CreateInstance <AG_BeginNode>();
                currentNode.nodeName = "Starting Node";
                break;

            case AG_NodeType.Test:
                currentNode = ScriptableObject.CreateInstance <AG_TestNode>();
                break;
                //			case AG_NodeType.Add:
                //				currentNode = ScriptableObject.CreateInstance<AddNode>();
                //				currentNode.NodeName = "Add Node";
                //				break;
            }


            if (currentNode != null)
            {
                currentNode.InitNode();

                Rect nodeRect = new Rect(mousePosition.x - currentNode.nodeSize.x / 2, mousePosition.y, currentNode.nodeSize.x, currentNode.nodeSize.y);

                currentNode.nodeRect    = nodeRect;
                currentNode.parentGraph = parentGraph;
            }

            return(currentNode);
        }
        void UpdateNodeLinksPointsAndGUI(AG_Node node)
        {
            if (node.inputPoints != null)
            {
                for (int i = 0; i < node.inputPoints.Count; i++)
                {
                    // TODO: update snappable rect position

                    // Image Rect update
                    float imageXSize               = 10.0f;
                    float imageYSize               = 10.0f;
                    float imageYBeginDist          = 20.0f;
                    float imageYCumulativeDistance = 30.0f;
                    node.inputPoints[i].imageRect = new Rect(node.nodeRect.x - imageXSize / 2.0f,
                                                             node.nodeRect.y + node.nodeRect.height - imageYBeginDist - i * imageYCumulativeDistance,
                                                             imageXSize, imageYSize);

                    // Clickable Rect update
                    Vector2 imageRectCenter = node.inputPoints[i].imageRect.center;

                    float clickableXSize = 40.0f;
                    float clickableYSize = 28.0f;
                    node.inputPoints[i].clickableRect = new Rect(imageRectCenter.x - clickableXSize / 2.0f,
                                                                 imageRectCenter.y - clickableYSize / 2.0f,
                                                                 clickableXSize, clickableYSize);



                    // Link point names
                    if (node.inputPoints[i].showName)
                    {
                        float xSize     = 90.0f;
                        float ySize     = 20.0f;
                        float xDistance = 10.0f;

                        Rect labelRect = new Rect(imageRectCenter.x + xDistance,
                                                  imageRectCenter.y - ySize / 2.0f,
                                                  xSize, ySize);

                        GUIStyle style = new GUIStyle();
                        style.alignment        = TextAnchor.MiddleLeft;
                        style.normal.textColor = Color.white;

                        GUILayout.BeginArea(labelRect);
                        EditorGUILayout.LabelField(node.inputPoints[i].name, style);
                        GUILayout.EndArea();
                    }


                    GUI.DrawTexture(node.inputPoints[i].imageRect, linkPointTexture);
                }

                for (int i = 0; i < node.outputPoints.Count; i++)
                {
                    // TODO: update snappable rect position

                    // Image Rect update
                    float imageXSize               = 10.0f;
                    float imageYSize               = 10.0f;
                    float imageYBeginDist          = 20.0f;
                    float imageYCumulativeDistance = 30.0f;
                    node.outputPoints[i].imageRect = new Rect(node.nodeRect.x + node.nodeRect.width - imageXSize / 2.0f,
                                                              node.nodeRect.y + node.nodeRect.height - imageYBeginDist - i * imageYCumulativeDistance,
                                                              imageXSize, imageYSize);

                    // Clickable Rect update
                    Vector2 imageRectCenter = node.outputPoints[i].imageRect.center;

                    float clickableXSize = 40.0f;
                    float clickableYSize = 28.0f;
                    node.outputPoints[i].clickableRect = new Rect(imageRectCenter.x - clickableXSize / 2.0f,
                                                                  imageRectCenter.y - clickableYSize / 2.0f,
                                                                  clickableXSize, clickableYSize);

                    // Link point names
                    if (node.outputPoints[i].showName)
                    {
                        //TextAnchor oldAnchor = GUI.skin.label.alignment;
                        //GUI.skin.label.alignment = TextAnchor.MiddleRight;

                        //Vector2 imageRectCenter = node.outputPoints[i].imageRect.center;

                        float xSize     = 90.0f;
                        float ySize     = 20.0f;
                        float xDistance = 10.0f;

                        Rect labelRect = new Rect(imageRectCenter.x - xDistance - xSize,
                                                  imageRectCenter.y - ySize / 2.0f,
                                                  xSize, ySize);

                        GUILayout.BeginArea(labelRect);

                        GUIStyle style = new GUIStyle();
                        style.alignment        = TextAnchor.MiddleRight;
                        style.normal.textColor = Color.white;

                        //style.

                        //GUI.Label(labelRect, node.outputPoints[i].name);
                        EditorGUILayout.LabelField(node.outputPoints[i].name, style);
                        GUILayout.EndArea();

                        //GUI.skin.label.alignment = oldAnchor;
                    }



                    GUI.DrawTexture(node.outputPoints[i].imageRect, linkPointTexture);
                }
            }
        }