Ejemplo n.º 1
0
        private void CreateEntryView()
        {
            Rect entryViewWindowRect = new Rect(position.x + position.width / 2, position.y + position.height / 2,
                                                BaseNodeView.kNodeWidht, BaseNodeView.kNodeHeight);

            /*
             * if (_entryView != null && currentGraph != null)
             * {
             *  entryViewWindowRect = currentGraph.EntryView.windowRect;
             *  AssetDatabase.RemoveObjectFromAsset(currentGraph.EntryView);
             *  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(currentGraph));
             * }*/

            _entryView             = CreateInstance <EntryNodeView>();
            _entryView.windowRect  = entryViewWindowRect;
            _entryView.windowTitle = "Entry View";
            _entryView.Init(null, true, false, false);

/*
 *          if (currentGraph != null)
 *          {
 *              AssetDatabase.AddObjectToAsset(_entryView, currentGraph);
 *              AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(currentGraph));
 *
 *              currentGraph.EntryView = _entryView;
 *          }
 */
        }
Ejemplo n.º 2
0
 public NodeSocket(Rect socketRect, NodeSocketType type, BaseNodeView node)
 {
     _socketRect = socketRect;
     SocketType  = type;
     Node        = node;
     IsHooked    = false;
 }
Ejemplo n.º 3
0
        private void ProcessEvents(Event e)
        {
            _drag = Vector2.zero;
            switch (e.type)
            {
            case EventType.MouseDown:
                //Focus();

                break;

            case EventType.MouseDrag:
                if (e.button == 0)
                {
                    if (DragWindowIfSelected(e))
                    {
                        return;
                    }

                    DragEverything(e);
                }

                break;

            case EventType.ContextClick:
                ProcessRightClickEvent(e);
                break;

            case EventType.KeyUp:

                if (e.keyCode == KeyCode.Space && position.Contains(GUIUtility.GUIToScreenPoint(e.mousePosition)) && GUI.GetNameOfFocusedControl() == "")
                {
                    ShowSearchTaskWindow(e);
                }
                else if (e.keyCode == KeyCode.Escape && NodeSocket.CurrentClickedSocket != null)
                {
                    NodeSocket.CurrentClickedSocket = null;
                }
                else if (e.keyCode == KeyCode.Escape)
                {
                    GUI.FocusControl(null);
                    _selectedNode = null;
                    Focus();
                }
                break;

            case EventType.ScrollWheel:
                if (e.delta.y > 0 && _currentZoom < MaxZoomDistance)
                {
                    _currentZoom += e.delta.y * ZoomStep;
                }
                else if (e.delta.y < 0 && _currentZoom > MinZoomDistance)
                {
                    _currentZoom += e.delta.y * ZoomStep;
                }

                GUI.changed = true;
                break;
            }
        }
Ejemplo n.º 4
0
        private void OnClickedNode(BaseNodeView node)
        {
            _selectedNode = node;

            var attributes =
                (TaskTooltipAttribute[])node.Task.GetType().GetCustomAttributes(typeof(TaskTooltipAttribute), true);

            if (attributes.Length > 0)
            {
                var attribute = attributes.First();

                if (attribute != null)
                {
                    _tooltipWindow = GetWindow <TooltipWindow>(true, "Tooltip", false);

                    _tooltipWindow.position =
                        new Rect(position.xMin + 20 + _currentZoom * 10f, position.yMax - 100, 200, 80);
                    _tooltipWindow.Tooltip = attribute.Tooltip;
                    _tooltipWindow.ShowPopup();
                }
            }
        }
Ejemplo n.º 5
0
        private BaseNodeView CopyNodeView(BaseNodeView baseNode)
        {
            var instance = CreateInstance(baseNode.GetType().FullName) as BaseNodeView;

            AssetDatabase.AddObjectToAsset(instance, currentGraph);
            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(currentGraph));
            Debug.Assert(instance != null, nameof(instance) + " != null");

            instance.Task = CreateInstance(baseNode.Task.GetType()) as ATask;
            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(currentGraph.GetInstanceID()));
            AssetDatabase.AddObjectToAsset(instance.Task, currentGraph);

            instance.windowRect  = baseNode.windowRect;
            instance.windowTitle = baseNode.windowTitle;

            instance.Init(baseNode.GUID, false, baseNode.IsRootView ? true : false, instance.Task is IComposite);

            instance.OnClickedNode += OnClickedNode;

            instance.CopyVariables(baseNode.variables);

            return(instance);
        }
Ejemplo n.º 6
0
        private void LoadTreeGraph()
        {
            if (currentGraph == null)
            {
                UDebug.LogError("Select a graph to Load Data From");
            }
            else
            {
                _nodeViews.Clear();

                _connections.Clear();

                AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GetAssetPath(currentGraph));

                foreach (var savedNode in currentGraph.SavedNodes)
                {
                    if (savedNode.IsRootView)
                    {
                        _rootView = CopyNodeView(savedNode);
                    }
                }

                if (_entryView != null && _rootView != null)
                {
                    _nodeViews.Add(_rootView);
                    _connections.Add(new NodeConnection(_entryView.exitSocket, _rootView.entrySocket,
                                                        currentGraph.entryConnection.ConnectionColor, true));
                }

                foreach (var nodeConnection in currentGraph.SavedConnections)
                {
                    BaseNodeView copiedStartNode = null, copiedEndNode = null;

                    if (nodeConnection.StartSocket.IsHooked)
                    {
                        copiedStartNode = CopyNodeView(nodeConnection.StartSocket.Node);
                    }

                    if (nodeConnection.EndSocket.IsHooked)
                    {
                        copiedEndNode = CopyNodeView(nodeConnection.EndSocket.Node);
                    }

                    if (copiedStartNode != null && copiedEndNode != null && copiedStartNode != _entryView)
                    {
                        BaseNodeView startNode = null;
                        if (!_nodeViews.Contains(copiedStartNode))
                        {
                            _nodeViews.Add(copiedStartNode);
                            startNode = copiedStartNode;
                        }
                        else
                        {
                            startNode = _nodeViews[_nodeViews.IndexOf(copiedStartNode)];
                        }

                        BaseNodeView endNode = null;
                        if (!_nodeViews.Contains(copiedEndNode))
                        {
                            _nodeViews.Add(copiedEndNode);
                            endNode = copiedEndNode;
                        }
                        else
                        {
                            endNode = _nodeViews[_nodeViews.IndexOf(copiedEndNode)];
                        }

                        _connections.Add(new NodeConnection(startNode.exitSocket, endNode.entrySocket,
                                                            nodeConnection.ConnectionColor, false));
                    }
                    else
                    {
                        UDebug.LogError(
                            "Trying to load an invalid connection. One of the connection socket was null");
                    }
                }

                //copy the unconnected nodes
                foreach (var savedNode in currentGraph.SavedNodes)
                {
                    if (!_nodeViews.Contains(savedNode))
                    {
                        _nodeViews.Add(CopyNodeView(savedNode));
                    }
                }

                /*
                 *
                 * if (_nodesToDestroy.Count > 0)
                 * {
                 *  foreach (var nodeView in _nodesToDestroy)
                 *  {
                 *      AssetDatabase.RemoveObjectFromAsset(nodeView);
                 *      AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(nodeView));
                 *      DestroyImmediate(nodeView);
                 *  }
                 *
                 * }
                 *
                 * _nodesToDestroy.Clear();
                 */

                GUI.changed = true;
            }
        }
Ejemplo n.º 7
0
        //Called every time the editor is enabled.
        private void StartEditor(bool hasEnteredEditMode)
        {
            UDebug.Log("On Enable Called");
            Application.quitting += OnApplicationQuit;
            EditorApplication.playModeStateChanged += EditorApplicationOnPlayModeStateChanged;

            _saveOnPlay  = EditorPrefs.GetBool(saveOnPlayKey, false);
            _saveOnClose = EditorPrefs.GetBool(saveOnCloseKey, false);

            _skin = Resources.Load <GUISkin>("BTSkin");

            if (NodeSocket.OnSocketClicked == null)
            {
                NodeSocket.OnSocketClicked += OnNodeSocketClicked;
            }

            if (nodeIds == null)
            {
                nodeIds = new List <int>();
            }

            if (_blackBoardVariablesID == null)
            {
                _blackBoardVariablesID = new List <IDsListWrapper>();
            }

            BaseNodeView.OnNodeRightClicked += view => _rightClickedNode = view;

            _nodeViews          = new List <BaseNodeView>();
            _connections        = new List <NodeConnection>();
            _nodesToDestroy     = new List <BaseNodeView>();
            _variablesToDestroy = new List <BlackBoardVariable>();


            CreateEntryView();

            if (hasEnteredEditMode || EditorApplication.isPlayingOrWillChangePlaymode)
            {
                UDebug.Log("Getting the ids");
                _nodeViews.Clear();
                _connections.Clear();

                for (int i = 0; i < currentGraph.SavedNodes.Count; i++)
                {
                    _nodeViews.Add(currentGraph.SavedNodes[i]);

                    _nodeViews[i].Init(_nodeViews[i].GUID, _nodeViews[i].IsEntryPoint, _nodeViews[i].IsRootView, _nodeViews[i].IsParentView);
                    _nodeViews[i].OnClickedNode += OnClickedNode;

                    for (int j = 0; j < currentGraph.SavedNodes[i].variables.Count; j++)
                    {
                        _nodeViews[i].variables[j] = currentGraph.SavedNodes[i].variables[j];
                    }

                    //todo hacer conexiones por guid para que serialicen por guid, no por referenceia directa.

                    if (_nodeViews[i].IsRootView)
                    {
                        _connections.Add(new NodeConnection(_entryView.exitSocket, _nodeViews[i].entrySocket, Color.white, true));
                    }
                }

                EditorFix.SetObjectDirty(currentGraph);
            }
            else
            {
                if (currentGraph != null)
                {
                    LoadTreeGraph();
                    UDebug.Log("Full Load");
                }
            }


            _tooltipWindow = null;
            UDebug.Log("Awake Called");
        }
Ejemplo n.º 8
0
        private void ProcessRightClickEvent(Event e)
        {
            UDebug.Log("Right Clicked the window");
            var genericMenu = new GenericMenu();

            if (_rightClickedNode)
            {
                genericMenu.AddItem(new GUIContent("Delete Node", "Delete the following node."), false, () =>
                {
                    var connectionsToRemove = new List <int>();

                    foreach (var connection in _connections)
                    {
                        if (connection.EndSocket == _rightClickedNode.entrySocket ||
                            connection.StartSocket == _rightClickedNode.exitSocket)
                        {
                            connectionsToRemove.Add(_connections.IndexOf(connection));
                        }
                    }

                    foreach (var conn in connectionsToRemove)
                    {
                        _connections.RemoveAt(conn);
                    }

                    _nodeViews.Remove(_rightClickedNode);
                    AssetDatabase.RemoveObjectFromAsset(_rightClickedNode.Task);
                    AssetDatabase.RemoveObjectFromAsset(_rightClickedNode);
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(currentGraph));
                    _rightClickedNode = null;

                    SaveGraphData();
                });

                genericMenu.AddItem(
                    new GUIContent("Make Root Node", "Make the selected node the entry point of the tree."), false,
                    () =>
                {
                    var connectionsToRemove = new List <NodeConnection>();

                    foreach (var connection in _connections)
                    {
                        if (connection.IsEntryConnection)
                        {
                            connectionsToRemove.Add(connection);
                        }
                        else if (connection.EndSocket == _rightClickedNode.entrySocket)
                        {
                            connectionsToRemove.Add(connection);
                        }
                    }

                    foreach (var toRemove in connectionsToRemove)
                    {
                        _connections.Remove(toRemove);
                    }

                    _entryView.exitSocket.IsHooked         = true;
                    _rightClickedNode.entrySocket.IsHooked = true;

                    _connections.Add(new NodeConnection(_entryView.exitSocket, _rightClickedNode.entrySocket,
                                                        Color.white, true));
                });
            }
            else
            {
                genericMenu.AddItem(new GUIContent("Add Task", "Add a built-in task or a custom made one."), false,
                                    () => { ShowSearchTaskWindow(e); });
                genericMenu.AddSeparator("");
                genericMenu.AddItem(new GUIContent("About", "about the library"), false, () =>
                {
                    UDebug.Log("Author's name is Eduardo Simon Picon.");
                    Application.OpenURL("https://www.eduardosimonpicon.com");
                });
            }

            genericMenu.ShowAsContext();
        }