Beispiel #1
0
        public override void DrawEditorGui()
        {
#if UNITY_EDITOR
            var animationLabels = AnimationEvents.GetValues();
            var index           = System.Array.IndexOf(animationLabels, EventName);
            var newIndex        = UnityEditor.EditorGUILayout.Popup("Event", index, animationLabels);
            if (newIndex >= 0)
            {
                EventName = animationLabels[newIndex];
                UnityEditor.EditorUtility.SetDirty(this);
            }
#endif
        }
Beispiel #2
0
        public override bool DrawGui(GUIStyle textStyle, GUIStyle buttonStyle)
        {
            bool changed = false;

#if UNITY_EDITOR
            GUILayout.BeginHorizontal();
            GUILayout.Space(20);
            var animationLabels = AnimationEvents.GetValues();
            var index           = System.Array.IndexOf(animationLabels, EventName);
            var newIndex        = UnityEditor.EditorGUILayout.Popup("Event", index, animationLabels);
            if (newIndex >= 0)
            {
                EventName = animationLabels[newIndex];
                UnityEditor.EditorUtility.SetDirty(this);
            }
            if (!Loop)
            {
                if (GUILayout.Button("Add Loop Condition", buttonStyle))
                {
                    Loop          = true;
                    LoopCondition = new EventCondition();
                    changed       = true;
                }
            }
            GUILayout.Space(20);
            GUILayout.EndHorizontal();
            if (Loop && LoopCondition != null)
            {
                LoopCondition.DrawGui(this, textStyle, buttonStyle);
            }
            if (changed)
            {
                UnityEditor.EditorUtility.SetDirty(this);
            }
#endif
            return(changed);
        }
Beispiel #3
0
        public bool DrawType(StateGraph graph, GUIStyle textStyle, GUIStyle buttonStyle)
        {
            bool changed = false;

#if UNITY_EDITOR
            switch (Type)
            {
            case ConditionType.Trigger:
            case ConditionType.EntityTag:
                break;

            case ConditionType.IsAttacking:
                GUILayout.Label("IsAttacking", textStyle);
                break;

            default:
                var graphLabels = GraphVariables.GetValues();
                var index       = System.Array.IndexOf(graphLabels, VariableName);
                var newVar      = UnityEditor.EditorGUILayout.Popup(index, graphLabels, buttonStyle, new [] { GUILayout.MaxWidth
                                                                                                                  (StateGraphNode.DefaultNodeSize.x * 0.5f) });
                if (newVar != index)
                {
                    VariableName = graphLabels[newVar];
                }
                break;
            }
            switch (Type)
            {
            case ConditionType.IsAttacking:
                bool.TryParse(Value, out bool oldBool);
                if (GUILayout.Button(oldBool.ToString(), buttonStyle))
                {
                    Value = (!oldBool).ToString();
                }
                break;

            case ConditionType.Trigger:
                var labels   = graph.GlobalTriggers.Select(t => t.Key).ToArray();
                var index    = System.Array.IndexOf(labels, Value);
                var newIndex = UnityEditor.EditorGUILayout.Popup(index, labels, buttonStyle);
                if (newIndex >= 0)
                {
                    Value   = labels[newIndex];
                    changed = true;
                }
                break;

            case ConditionType.StringVariable:
                Value = GUILayout.TextField(Value, buttonStyle);
                break;

            case ConditionType.BoolVariable:
                bool.TryParse(Value, out bool oldValue);
                if (GUILayout.Button(oldValue.ToString()))
                {
                    Value = (!oldValue).ToString();
                }
                break;

            case ConditionType.EntityTag:
                int.TryParse(Value, out int oldTagInt);
                var animationLabels = AnimationEvents.GetValues();
                var newTag          = UnityEditor.EditorGUILayout.Popup(oldTagInt, animationLabels);
                if (newTag != oldTagInt)
                {
                    Value = newTag.ToString();
                }
                break;

            case ConditionType.FloatVariable:
                float.TryParse(Value, out float oldFloat);
                var newFloatStr = GUILayout.TextField(Value, buttonStyle);
                if (float.TryParse(newFloatStr, out var newFloat) && Math.Abs(newFloat - oldFloat) > 0.001f)
                {
                    Value = newFloatStr;
                }
                break;

            case ConditionType.IntVariable:
                int.TryParse(Value, out int oldInt);
                var newIntStr = GUILayout.TextField(Value, buttonStyle);
                if (int.TryParse(newIntStr, out var newInt) && newInt != oldInt)
                {
                    Value = newIntStr;
                }
                break;
            }
#endif
            return(changed);
        }
Beispiel #4
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);
                    }
                }
            }
        }