private void DrawNodes(RuntimeStateGraph graph)
        {
            if (graph == null)
            {
                return;
            }
            var currentNode = graph.Current?.Node;

            for (int i = 0; i < graph.OriginalGraph.Count; i++)
            {
                var node     = graph.OriginalGraph.Nodes[i];
                var maxWidth = node.Rect.x * 0.8f;
                var style    = node == currentNode ? _nodeSelectedStyle : _nodeStyle;
                GUILayout.BeginArea(node.Rect, style);
                GUILayout.Space(10);
                GUILayout.Label(node.Title, _nodeTextStyle, GUILayout.MaxWidth(maxWidth));
                GUILayout.Label("ID: " + node.Id, _nodeTextStyle, GUILayout.MaxWidth(maxWidth));
                GUILayout.BeginHorizontal();
                GUILayout.Space(20);
                GUILayout.Label("Enter");
                EditorGUILayout.LabelField(node.EnterEvent);
                GUILayout.Label("Exit");
                EditorGUILayout.LabelField(node.ExitEvent);
                GUILayout.Space(20);
                GUILayout.EndHorizontal();
                GUILayout.Label(graph.GetRuntimeNode(node.Id).DebugInfo);
                GUILayout.EndArea();
                var inSpacing = Mathf.Clamp(0.8f / node.InPoints.Count, 0.02f, 1);
                for (int c = 0; c < node.InPoints.Count; c++)
                {
                    if (node.InPoints[c] == null)
                    {
                        node.InPoints.RemoveAt(c);
                        break;
                    }
                    StateGraphExtensions.DrawConnectionPoint(node.InPoints[c], c, inSpacing, null, null);
                }
                var outSpacing = Mathf.Clamp(0.8f / node.OutPoints.Count, 0.02f, 1);
                for (int c = 0; c < node.OutPoints.Count; c++)
                {
                    if (node.OutPoints[c] == null)
                    {
                        node.OutPoints.RemoveAt(c);
                        break;
                    }
                    StateGraphExtensions.DrawConnectionPoint(node.OutPoints[c], c, outSpacing, null, null);
                }
            }
        }
Exemple #2
0
        private void DrawNodes()
        {
            if (_graph == null)
            {
                return;
            }
            var aeLabels  = AnimationEvents.GetNames().ToArray();
            var aeValues  = AnimationEvents.GetValues();
            var tagLabels = GraphNodeTags.GetNames().ToArray();
            var tagValues = GraphNodeTags.GetValues();

            for (int i = 0; i < _graph.Nodes.Count; i++)
            {
                var node       = _graph.Nodes[i];
                var maxWidth   = node.Rect.x * 0.8f;
                int styleIndex = 0;
                if (node == _graph.Default)
                {
                    styleIndex = StyleIndexDefault;
                }
                else if (node.IsGlobal)
                {
                    styleIndex = StyleIndexGlobal;
                }
                else if (node.HasConditions)
                {
                    styleIndex = node.AllowEarlyExit ? StyleHasEarlyExit : StyleHasConditions;
                }
                var style = node == _selected ? _nodeSelectedStyles[styleIndex] : _nodeStyles[styleIndex];
                GUILayout.BeginArea(node.Rect, style);
                EditorGUI.BeginChangeCheck();
                GUILayout.Space(10);
                GUILayout.Label(node.Title, StateGraphExtensions.NodeTextStyle, GUILayout.MaxWidth(maxWidth));
                GUILayout.BeginHorizontal();
                GUILayout.Space(20);
                var tagIndex = System.Array.IndexOf(tagValues, node.Tag);
                var newTag   = UnityEditor.EditorGUILayout.Popup(tagIndex, tagLabels, GUILayout.MaxWidth(maxWidth / 2));
                if (newTag != tagIndex)
                {
                    node.Tag = tagValues[newTag];
                }
                GUILayout.Label("ID: " + node.Id, StateGraphExtensions.NodeTextStyle, GUILayout.MaxWidth(maxWidth / 2));
                GUILayout.Space(20);
                GUILayout.EndHorizontal();
                if (node.DrawGui(StateGraphExtensions.NodeTextStyle, _nodeButtonStyle))
                {
                    Repaint();
                }
                for (int c = 0; c < node.Conditions.Count; c++)
                {
                    node.Conditions[c].DrawGui(node, StateGraphExtensions.NodeTextStyle, _nodeButtonStyle);
                    GUILayout.Space(10);
                }
                if (node.Conditions.Count > 0 && node.OutPoints.Count > 1)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Space(20);
                    GUILayout.Label("Else exit ", StateGraphExtensions.NodeTextStyle);
                    var indices = new string[node.OutPoints.Count];
                    for (int idx = 0; idx < indices.Length; idx++)
                    {
                        indices[idx] = idx.ToString();
                    }
                    node.DefaultExit = UnityEditor.EditorGUILayout.Popup(node.DefaultExit, indices, StateGraphExtensions.NodeTextStyle);
                    GUILayout.Space(20);
                    GUILayout.EndHorizontal();
                }
                GUILayout.BeginHorizontal();
                GUILayout.Space(20);
                var enterIndex = System.Array.IndexOf(aeValues, node.EnterEvent);
                var exitIndex  = System.Array.IndexOf(aeValues, node.ExitEvent);
                GUILayout.Label("Enter");
                var newEnter = UnityEditor.EditorGUILayout.Popup(enterIndex, aeLabels);
                if (newEnter != enterIndex)
                {
                    node.EnterEvent = aeValues[newEnter];
                }
                GUILayout.Label("Exit");
                var newExit = UnityEditor.EditorGUILayout.Popup(exitIndex, aeLabels);
                if (newExit != exitIndex)
                {
                    node.ExitEvent = aeValues[newExit];
                }
                GUILayout.Space(20);
                GUILayout.EndHorizontal();
                if (node.Conditions.Count < node.MaxConditions)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Space(20);
                    if (GUILayout.Button("Add Condition", _nodeButtonStyle))
                    {
                        node.Conditions.Add(new ConditionExit());
                        node.CheckSize();
                    }
                    GUILayout.Space(20);
                    GUILayout.EndHorizontal();
                }
                if (EditorGUI.EndChangeCheck())
                {
                    EditorUtility.SetDirty(node);
                }
                GUILayout.EndArea();
                var inSpacing = Mathf.Clamp(0.8f / node.InPoints.Count, 0.02f, 1);
                for (int c = 0; c < node.InPoints.Count; c++)
                {
                    if (node.InPoints[c] == null)
                    {
                        node.InPoints.RemoveAt(c);
                        break;
                    }
                    StateGraphExtensions.DrawConnectionPoint(node.InPoints[c], c, inSpacing, OnClickInPoint, RemoveConnectionPoint);
                }
                var outSpacing = Mathf.Clamp(0.8f / node.OutPoints.Count, 0.02f, 1);
                for (int c = 0; c < node.OutPoints.Count; c++)
                {
                    if (node.OutPoints[c] == null)
                    {
                        node.OutPoints.RemoveAt(c);
                        break;
                    }
                    StateGraphExtensions.DrawConnectionPoint(node.OutPoints[c], c, outSpacing, OnClickOutPoint, RemoveConnectionPoint);
                }
                if (node.InPoints.Count < node.InputMax)
                {
                    var addRect = new Rect(
                        node.Rect.x + ConnectionOutPoint.Width * 0.5f,
                        node.Rect.y + node.Rect.height - (ConnectionOutPoint.Height * 1.25f),
                        ConnectionOutPoint.Width, ConnectionOutPoint.Height);
                    if (GUI.Button(addRect, "+", StateGraphExtensions.NodeTextStyle))
                    {
                        node.InPoints.Add(new ConnectionInPoint(node, node.FindMinConnectionId()));
                        EditorUtility.SetDirty(node);
                    }
                }
                if (node.OutPoints.Count < node.OutputMax)
                {
                    var addRect = new Rect(
                        node.Rect.x + node.Rect.width - (ConnectionOutPoint.Width * 1.25f),
                        node.Rect.y + node.Rect.height - (ConnectionOutPoint.Height * 1.25f),
                        ConnectionOutPoint.Width, ConnectionOutPoint.Height);
                    if (GUI.Button(addRect, "+", StateGraphExtensions.NodeTextStyle))
                    {
                        node.OutPoints.Add(new ConnectionOutPoint(node, node.FindMinConnectionId()));
                        EditorUtility.SetDirty(node);
                    }
                }
            }
        }