Esempio n. 1
0
        void ManageEvents(Event e)
        {
            switch (data.CurrentEvent)
            {
            case EditorData.InputEventType.DisplayContextMenu:
            {
                if (data.CurrentEvent == EditorData.InputEventType.None)
                {
                    return;
                }
                GenericMenu menu = new GenericMenu();

                if (data.DeleteNode)
                {
                    if (data.ClickedNode != data.Behaviour.RootNode.NodeID)
                    {
                        menu.AddItem(new GUIContent("Delete node"), false, () =>
                            {
                                BehaviourNode node = data.Behaviour.GetBaseNode(data.ClickedNode);
                                if (node.BehaviourNodeType == BehaviourNodeType.Action)
                                {
                                    var actionNode = node as ActionBehaviourNode;
                                    data.Behaviour.RemoveNode((typeof(ActionBehaviourNode)), actionNode);
                                }

                                else if (node.BehaviourNodeType == BehaviourNodeType.Condition)
                                {
                                    var conditionNode = node as ConditionBehaviourNode;
                                    data.Behaviour.RemoveNode((typeof(ConditionBehaviourNode)), conditionNode);
                                }
                                else if (node.BehaviourNodeType == BehaviourNodeType.Sequence)
                                {
                                    var sequence = node as SequenceBehaviourNode;
                                    data.Behaviour.RemoveNode(typeof(SequenceBehaviourNode), sequence);
                                }

                                data.ClickedNode = string.Empty;
                            });

                        menu.AddSeparator("");
                    }

                    BehaviourNode baseNode = data.Behaviour.GetBaseNode(data.ClickedNode);

                    if (baseNode != null)
                    {
                        bool canAddSeparator = true;

                        //Draw specifics node type actions
                        switch (baseNode.BehaviourNodeType)
                        {
                        case BehaviourNodeType.Action:
                            canAddSeparator = false;
                            break;

                        case BehaviourNodeType.Condition:
                            canAddSeparator = false;
                            break;

                        case BehaviourNodeType.Sequence:
                        {
                            SequenceBehaviourNode sequenceBehaviourNode = baseNode as SequenceBehaviourNode;
                            menu.AddItem(new GUIContent("Add sequence output"), false, () =>
                                    {
                                        sequenceBehaviourNode.AddSequenceOutput();
                                    });

                            menu.AddItem(new GUIContent("Remove Sequence output"), false, () =>
                                    {
                                        sequenceBehaviourNode.RemoveSequenceOutput();
                                    });
                        }
                        break;
                        }

                        if (canAddSeparator)
                        {
                            menu.AddSeparator("");
                        }
                    }

                    data.DeleteNode = false;
                }

                menu.AddItem(new GUIContent("Add Condition Node"), false, () =>
                    {
                        var node = data.Behaviour.CreateNode <ConditionBehaviourNode>(typeof(ConditionBehaviourNode));
                        node.InEditorWindow.NodePosition = e.mousePosition;
                    });
                menu.AddItem(new GUIContent("Add Action Node"), false, () =>
                    {
                        var node = data.Behaviour.CreateNode <ActionBehaviourNode>(typeof(ActionBehaviourNode));
                        node.InEditorWindow.NodePosition = e.mousePosition;
                    });
                menu.AddItem(new GUIContent("Add Sequence Node"), false, () =>
                    {
                        var node = data.Behaviour.CreateNode <SequenceBehaviourNode>(typeof(SequenceBehaviourNode));
                        node.InEditorWindow.NodePosition = e.mousePosition;
                    });

                menu.ShowAsContext();
                data.CurrentEvent = EditorData.InputEventType.None;
            }
            break;

            case EditorData.InputEventType.CreatingConnection:
            {
                if (e == null)
                {
                    break;
                }

                if (e.keyCode == KeyCode.Escape)
                {
                    ClearFromCreatintNodeOutput();
                    data.FromCreatingNode = string.Empty;
                    break;
                }

                int           fromOutPutId   = data.FromCreatingNodeOutputID;
                BehaviourNode fromNode       = data.ClickedNode != data.Behaviour.RootNode.NodeID ? data.Behaviour.GetBaseNode(data.ClickedNode) : data.Behaviour.RootNode;
                Vector3       outputPosition = GetOutputPosition(fromNode, data.FromCreatingNodeOutputID);
                Handles.DrawBezier(outputPosition, e.mousePosition, outputPosition + Vector3.right * 50, (Vector3)e.mousePosition + Vector3.left * 50, Color.black, null, 2);
            }
            break;

            case EditorData.InputEventType.CreateConnection:
            {
                BehaviourNode fromNode = data.FromCreatingNode != data.Behaviour.RootNode.NodeID ? data.Behaviour.GetBaseNode(data.FromCreatingNode) : data.Behaviour.RootNode;

                switch (fromNode.BehaviourNodeType)
                {
                case BehaviourNodeType.Root:
                {
                    RootBehaviourNode root = fromNode as RootBehaviourNode;
                    data.Behaviour.RootNode.SetOutputChildId(data.ClickedNode);
                }
                break;

                case BehaviourNodeType.Condition:
                {
                    ConditionBehaviourNode conditionNode = fromNode as ConditionBehaviourNode;
                    if (data.FromCreatingNodeOutputID == 0)
                    {
                        conditionNode.SetFalseChildId(data.ClickedNode);
                    }
                    else
                    {
                        conditionNode.SetTrueChildId(data.ClickedNode);
                    }
                }
                break;

                case BehaviourNodeType.Action:
                {
                    ActionBehaviourNode actionNode = fromNode as ActionBehaviourNode;
                    actionNode.SetOutputChild(data.ClickedNode);
                }
                break;

                case BehaviourNodeType.Sequence:
                {
                    SequenceBehaviourNode sequenceNode = fromNode as SequenceBehaviourNode;
                    sequenceNode.SetChild(data.FromCreatingNodeOutputID, data.ClickedNode);
                }
                break;
                }

                BehaviourNode toNode = data.Behaviour.GetBaseNode(data.ClickedNode);
                toNode.ParentId   = data.FromCreatingNode;
                data.CurrentEvent = EditorData.InputEventType.None;
            }
            break;

            case EditorData.InputEventType.Drag:
            {
                data.CurrentEvent = EditorData.InputEventType.None;
            }
            break;
            }
        }
Esempio n. 2
0
        void DrawConnectors()
        {
            if (!string.IsNullOrEmpty(data.Behaviour.RootNode.OutputId))
            {
                if (!Array.Exists(data.Behaviour.AllNodeIds, x => x == data.Behaviour.RootNode.OutputId))
                {
                    data.Behaviour.RootNode.SetOutputChildId(string.Empty);
                }
                else
                {
                    BehaviourNode toNode       = data.Behaviour.GetBaseNode(data.Behaviour.RootNode.OutputId);
                    Vector2       fromPosition = GetOutputPosition(data.Behaviour.RootNode, 0);
                    Handles.DrawBezier(fromPosition, toNode.InEditorWindow.InputPosition, (Vector3)fromPosition + Vector3.right * 50, (Vector3)toNode.InEditorWindow.InputPosition + Vector3.left * 50, Color.black, null, 2);
                }
            }

            foreach (var nodeId in data.Behaviour.AllNodeIds)
            {
                var baseNode = data.Behaviour.GetBaseNode(nodeId);
                switch (baseNode.BehaviourNodeType)
                {
                case BehaviourNodeType.Condition:
                {
                    ConditionBehaviourNode conditionNode = baseNode as ConditionBehaviourNode;

                    if (!string.IsNullOrEmpty(conditionNode.FalseNode))
                    {
                        if (!Array.Exists(data.Behaviour.AllNodeIds, x => x == conditionNode.FalseNode))
                        {
                            conditionNode.SetFalseChildId(string.Empty);
                            break;
                        }

                        BehaviourNode toFalseNode  = data.Behaviour.GetBaseNode(conditionNode.FalseNode);
                        Vector2       fromPosition = GetOutputPosition(conditionNode, 0);
                        Handles.DrawBezier(fromPosition, toFalseNode.InEditorWindow.InputPosition, (Vector3)fromPosition + Vector3.right * 50, (Vector3)toFalseNode.InEditorWindow.InputPosition + Vector3.left * 50, Color.black, null, 2);
                    }
                    if (!string.IsNullOrEmpty(conditionNode.TrueNode))
                    {
                        if (!Array.Exists(data.Behaviour.AllNodeIds, x => x == conditionNode.TrueNode))
                        {
                            conditionNode.SetTrueChildId(string.Empty);
                            break;
                        }

                        BehaviourNode toTrueNode   = data.Behaviour.GetBaseNode(conditionNode.TrueNode);
                        Vector2       fromPosition = GetOutputPosition(conditionNode, 1);
                        Handles.DrawBezier(fromPosition, toTrueNode.InEditorWindow.InputPosition, (Vector3)fromPosition + Vector3.right * 50, (Vector3)toTrueNode.InEditorWindow.InputPosition + Vector3.left * 50, Color.black, null, 2);
                    }
                }
                break;

                case BehaviourNodeType.Action:
                {
                    ActionBehaviourNode actionNode = baseNode as ActionBehaviourNode;

                    if (!string.IsNullOrEmpty(actionNode.OutputId))
                    {
                        if (!Array.Exists(data.Behaviour.AllNodeIds, x => x == actionNode.OutputId))
                        {
                            actionNode.SetOutputChild(string.Empty);
                            break;
                        }

                        BehaviourNode toNode       = data.Behaviour.GetBaseNode(actionNode.OutputId);
                        Vector2       fromPosition = GetOutputPosition(actionNode, 0);
                        Handles.DrawBezier(fromPosition, toNode.InEditorWindow.InputPosition, (Vector3)fromPosition + Vector3.right * 50, (Vector3)toNode.InEditorWindow.InputPosition + Vector3.left * 50, Color.black, null, 2);
                    }
                }
                break;

                case BehaviourNodeType.Sequence:
                {
                    SequenceBehaviourNode sequenceNode = baseNode as SequenceBehaviourNode;

                    for (int i = 0; i < sequenceNode.Outputs.Length; i++)
                    {
                        string outputId = sequenceNode.Outputs[i];

                        if (string.IsNullOrEmpty(outputId))
                        {
                            continue;
                        }

                        if (!Array.Exists(data.Behaviour.AllNodeIds, x => x == outputId))
                        {
                            sequenceNode.SetChild(i, string.Empty);
                            continue;
                        }

                        BehaviourNode toNode       = data.Behaviour.GetBaseNode(outputId);
                        Vector2       fromPosition = GetOutputPosition(sequenceNode, i);
                        Handles.DrawBezier(fromPosition, toNode.InEditorWindow.InputPosition, (Vector3)fromPosition + Vector3.right * 50, (Vector3)toNode.InEditorWindow.InputPosition + Vector3.left * 50, Color.black, null, 2);
                    }
                }
                break;
                }
            }
        }