Exemplo n.º 1
0
 public void AddNodePanel(NodePanel nodePanel)
 {
     nodePanels.Add(nodePanel);
     _nodeToNodePanelDict.Add(nodePanel.Node, nodePanel);
     _guidToNodePanelDict.Add(nodePanel.guid, nodePanel);
     SaveState();
 }
Exemplo n.º 2
0
        public override void OnPointerDrag(PointerEvent evt)
        {
            NodeHandle handle   = (NodeHandle)evt.downObject;
            Vector2    mousePos = evt.mousePos;

            if (_draggingHandle == null)
            {
                if (handle.type == NodeHandle.HandleType.Out)
                {
                    _nodeCanvas.CreatePreviewConnection(handle, evt.mousePos);
                    _draggingHandle = handle;
                }
                else
                {
                    NodePanel parent = handle.NodePanel.Parent;
                    if (parent != null)
                    {
                        _draggingHandle = parent.outHandle;
                        _nodeCanvas.RemoveChild(handle.NodePanel);
                        _nodeCanvas.CreatePreviewConnection(parent.outHandle, evt.mousePos);
                    }
                }
            }

            if (_draggingHandle != null && _nodeCanvas.previewConnection != null)
            {
                _nodeCanvas.previewConnection.SetEndPosition(CanvasUtility.ScreenToCanvasPosition(mousePos, _nodeCanvas.canvasState));
                _nodeCanvas.previewConnection.UpdatePoints(new List <NodeConnection>());
            }
        }
Exemplo n.º 3
0
        public override void OnPointerDrag(PointerEvent evt)
        {
            if (evt.shift)
            {
                return;
            }

            if (topLevelPanels == null)
            {
                topLevelPanels = GetTopLevelPanels(_nodeCanvas.selectedPanels);
            }

            float factor = BehaviourEditor.SNAPPING_FACTOR;

            deltaPos += evt.deltaPos;

            float deltaX = Mathf.Abs(deltaPos.x) > factor ? deltaPos.x : 0;
            float deltaY = Mathf.Abs(deltaPos.y) > factor ? deltaPos.y : 0;

            for (int i = 0; i < topLevelPanels.Count; i++)
            {
                NodePanel draggingPanel = topLevelPanels[i];
                draggingPanel.transform.position += new Vector2(deltaX, deltaY);
                draggingPanel.transform.position  = CanvasUtility.SnapPosition(draggingPanel.transform.position, factor);

                if (draggingPanel.Parent != null)
                {
                    draggingPanel.Parent.outHandle.UpdateConnections();
                }

                draggingPanel.UpdateAllConnections();
            }

            deltaPos = new Vector2(deltaX == 0 ? deltaPos.x : 0, deltaY == 0 ? deltaPos.y : 0);
        }
Exemplo n.º 4
0
        public static NodePanel CreateDefaultNodePanel(Node node, CanvasState canvasState)
        {
            // create the nodePanel rect
            Rect nodePanelRect = new Rect(new Vector2(0, 0), new Vector2(BehaviourEditorStyles.NODE_DEFAULT_WIDTH, BehaviourEditorStyles.NODE_DEFAULT_HEIGHT));

            int handleSize    = 16;
            int handlePadding = 2;

            Rect inHandleRect = new Rect(Vector2.zero, new Vector2(handleSize, handleSize));

            inHandleRect.center = new Vector2(nodePanelRect.width / 2,
                                              0 - handleSize / 2 - handlePadding);

            Rect outHandleRect = new Rect(Vector2.zero, new Vector2(handleSize, handleSize));

            outHandleRect.center = new Vector2(nodePanelRect.width / 2,
                                               (nodePanelRect.size.y) + (handleSize / 2) + handlePadding);

            //NodePanel nodePanel = new NodePanel(node, nodePanelRect, inHandleRect, outHandleRect);
            NodePanel nodePanel = node.CanHaveChildren ?
                                  new NodePanel(node, nodePanelRect, inHandleRect, outHandleRect, canvasState) :
                                  new NodePanel(node, nodePanelRect, inHandleRect, canvasState);

            nodePanel.SetIcon(CanvasUtility.GetIcon(node.GetType()));
            nodePanel.SetColours(Color.white, BehaviourEditorStyles.nodeNormalColour, 1);

            return(nodePanel);
        }
Exemplo n.º 5
0
        public void RebuildConnections()
        {
            for (int i = 0; i < nodePanels.Count; i++)
            {
                NodePanel  panel     = nodePanels[i];
                NodeHandle outHandle = panel.outHandle;

                List <NodeConnection> curConnections = panel.outHandle.Connections;
                List <NodeHandle>     inHandles      = new List <NodeHandle>();

                for (int j = 0; j < panel.childrenGuids.Count; j++)
                {
                    var child = GetNodePanel(panel.childrenGuids[j]);
                    inHandles.Add(child.inHandle);
                }

                foreach (var inHandle in inHandles)
                {
                    outHandle.Disconnect(inHandle);
                }
            }

            foreach (var nodePanel in nodePanels)
            {
                foreach (var child in nodePanel.Children)
                {
                    nodePanel.outHandle.Connect(child.inHandle);
                }

                nodePanel.outHandle.UpdateConnections();
            }
        }
Exemplo n.º 6
0
        public void SetParent(NodePanel parent)
        {
            if (parent != null)
            {
                parent.AddChild(this);
            }

            this.Parent = parent;

            this.transform.Parent = parent != null ? parent.transform : null;
        }
Exemplo n.º 7
0
        public override void OnPointerDown(PointerEvent evt)
        {
            NodePanel downPanel = (NodePanel)evt.downObject;

            if (!_nodeCanvas.selectedPanels.Contains(downPanel))
            {
                _nodeCanvas.SelectPanel(downPanel, evt.shift);
            }
            else if (evt.shift)
            {
                _nodeCanvas.selectedPanels.Remove(downPanel);
            }
        }
Exemplo n.º 8
0
        public void RemoveNodePanel(NodePanel panel)
        {
            RemoveCanvasTransform(panel.inHandle.transform);

            if (panel.hasOuthandle)
            {
                RemoveCanvasTransform(panel.outHandle.transform);
            }

            RemoveCanvasTransform(panel.transform);

            _nodeToNodePanelDict.Remove(panel.Node);
            _guidToNodePanelDict.Remove(panel.guid);

            nodePanels.Remove(panel);
        }
Exemplo n.º 9
0
        void OnNodeSelected(Node n, NodePanel p)
        {
            redrawInspector = true;

            selectedNode  = n;
            selectedPanel = p;

            if (selectedNode == null)
            {
                return;
            }

            renameable = true;
            System.Type nodeType  = selectedNode.GetType();
            object[]    nodeAttrs = nodeType.GetCustomAttributes(typeof(NodeAttribute), true);

            _fields.Clear();
            _fieldToAttributeDict.Clear();

            NodeAttribute attr = (NodeAttribute)nodeAttrs[0];

            renameable = attr.renameable;

            //NodeFieldNames = new List<string>();
            //nodeDefaultVariables = new Dictionary<string, FieldInfo>();
            //nodeGetterVariables = new Dictionary<string, FieldInfo>();
            //nodeSetterVariables = new Dictionary<string, FieldInfo>();
            _fields = new List <FieldInfo>();
            _fieldToAttributeDict = new Dictionary <FieldInfo, NodeField>();

            foreach (var field in nodeType.GetFields())
            {
                NodeField[] varField = (NodeField[])field.GetCustomAttributes(typeof(NodeField), true);

                bool   addField  = false;
                string label     = null;
                string fieldName = null;

                NodeField[] nodeField = (NodeField[])field.GetCustomAttributes(typeof(NodeField), true);

                if (nodeField != null && nodeField.Length > 0)
                {
                    _fields.Add(field);
                    _fieldToAttributeDict.Add(field, nodeField[0]);
                }
            }
        }
Exemplo n.º 10
0
        public CanvasState CloneWithSubstituteTree(BehaviourTree treeAsset)
        {
            CanvasState canvasState = ScriptableObject.CreateInstance <CanvasState>();

            canvasState.tree          = treeAsset;
            canvasState.RootNodePanel = this.RootNodePanel;

            canvasState.nodePanels = new List <NodePanel>();

            for (int i = 0; i < nodePanels.Count; i++)
            {
                canvasState.nodePanels.Add(nodePanels[i].Clone(treeAsset, canvasState));
            }


            canvasState.canvasRect = canvasRect;
            canvasState.zoom       = zoom;
            canvasState.panOffset  = panOffset;
            canvasState.OnEnable();

            for (int i = 0; i < nodePanels.Count; i++)
            {
                NodePanel clonedPanel = canvasState.GetNodePanel(nodePanels[i].guid);
                NodePanel oldPanel    = nodePanels[i];

                clonedPanel.inHandle.transform.rect = oldPanel.inHandle.transform.rect;
                if (clonedPanel.hasOuthandle)
                {
                    clonedPanel.outHandle.transform.rect = oldPanel.outHandle.transform.rect;
                }
            }

            for (int i = 0; i < nodePanels.Count; i++)
            {
                NodePanel nodePanel = nodePanels[i];

                for (int j = 0; j < nodePanel.Children.Count; j++)
                {
                    NodePanel child = nodePanel.Children[j];
                    nodePanel.outHandle.Connect(child.inHandle);
                }
            }

            return(canvasState);
        }
Exemplo n.º 11
0
        public void AddChild(NodePanel child)
        {
            if (child == null)
            {
                Debug.LogError("Cannot add null child!");
                return;
            }

            if (child.Parent != null && child.Parent != this)
            {
                child.Parent.RemoveChild(child);
            }

            child.Parent = this;
            childrenGuids.Add(child.guid);

            outHandle.Connect(child.inHandle);
        }
Exemplo n.º 12
0
        private List <NodePanel> GetDescendants(NodePanel panel)
        {
            List <NodePanel> result = new List <NodePanel>();

            for (int i = 0; i < panel.Children.Count; i++)
            {
                result.Add(panel.Children[i]);

                List <NodePanel> descendants = GetDescendants(panel.Children[i]);
                for (int j = 0; j < descendants.Count; j++)
                {
                    if (!result.Contains(descendants[j]))
                    {
                        result.Add(descendants[j]);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 13
0
        public override void OnPointerDrop(PointerEvent evt)
        {
            List <NodePanel> updatedPanels = new List <NodePanel>();

            for (int i = 0; i < _nodeCanvas.selectedPanels.Count; i++)
            {
                NodePanel parent = _nodeCanvas.selectedPanels[i].Parent;
                if (parent != null && !updatedPanels.Contains(parent))
                {
                    updatedPanels.Add(parent);
                    if (parent.SortChildren())
                    {
                        _nodeCanvas.canvasState.SaveState();
                    }
                }
            }

            topLevelPanels = null;
            deltaPos       = Vector2.zero;
        }
Exemplo n.º 14
0
        public NodeConnection(NodePanel startPanel, NodePanel endPanel, CanvasState canvasState)
        {
            this.canvasState = canvasState;

            // CanvasTransform startParent = canvasState.GetCanvasTransform(startParentGuid);
            // CanvasTransform endParent = canvasState.GetCanvasTransform(endParentGuid);

            // startParent.childrenGuids.Add(_start.guid);
            // endParent.childrenGuids.Add(_end.guid);

            _start = new CanvasTransform("Start Connection", startPanel.outHandle.transform.rect, canvasState);
            _end   = new CanvasTransform("End Connection", endPanel.inHandle.transform.rect, canvasState);

            _start.Parent = startPanel.outHandle.transform;
            _end.Parent   = endPanel.inHandle.transform;


            _startPanelGuid = startPanel.guid;
            _endPanelGuid   = endPanel.guid;

            _points = new Vector2[4];
        }
Exemplo n.º 15
0
        public NodePanel Clone(BehaviourTree tree, CanvasState state)

        {
            NodePanel clone = hasOuthandle ?
                              new NodePanel(tree.GetNode(nodeGuid), transform.rect, inHandle.transform.rect, outHandle.transform.rect, state) :
                              new NodePanel(tree.GetNode(nodeGuid), transform.rect, inHandle.transform.rect, state);

            clone.guid          = guid;
            clone.childrenGuids = childrenGuids;
            clone.parentGuid    = parentGuid;
            clone.transform     = transform.Clone(state);
            clone.icon          = icon;

            clone.inHandle     = inHandle.Clone(state);
            clone.hasOuthandle = hasOuthandle;
            if (hasOuthandle)
            {
                clone.outHandle = outHandle.Clone(state);
            }

            return(clone);
        }
Exemplo n.º 16
0
        public void MoveChild(NodePanel child, int newIndex)
        {
            if (!childrenGuids.Contains(child.guid))
            {
                if (child.Parent != null)
                {
                    child.Parent.RemoveChild(child);
                }

                AddChild(child);
            }


            int oldIndex = childrenGuids.IndexOf(child.guid);

            if (newIndex > oldIndex)
            {
                newIndex--;
            }

            if (newIndex > childrenGuids.Count)
            {
                Debug.LogWarning("Cannot move child to index " + newIndex + ". It is out of range. Total children: " + childrenGuids.Count);
                return;
            }

            childrenGuids.RemoveAt(oldIndex);
            if (newIndex < childrenGuids.Count)
            {
                childrenGuids.Insert(newIndex, child.guid);
            }
            else
            {
                // add child to the end of the list
                childrenGuids.Add(child.guid);
            }

            Debug.Log("Moving child from " + oldIndex + " to " + newIndex);
        }
Exemplo n.º 17
0
        public void RemoveChild(NodePanel child)
        {
            if (childrenGuids == null)
            {
                Debug.LogError("Cannot remove child, children is null!");
                return;
            }

            if (!childrenGuids.Contains(child.guid))
            {
                Debug.LogError("Cannot remove child, the child nodePanel is not currently a child!");
                return;
            }

            outHandle.Disconnect(child.inHandle);
            childrenGuids.Remove(child.guid);

            child.Parent           = null;
            child.transform.Parent = null;

            Node.RemoveChild(child.Node);
        }
Exemplo n.º 18
0
        void DrawConnections()
        {
            for (int i = 0; i < canvas.canvasState.nodePanels.Count; i++)
            {
                NodePanel panel = canvas.canvasState.nodePanels[i];

                if (panel.outHandle == null || panel.outHandle.Connections == null || panel.outHandle.Connections.Count == 0)
                {
                    continue;
                }

                NodeConnection runningConnection = null;

                for (int j = 0; j < panel.outHandle.Connections.Count; j++)
                {
                    NodeConnection connection = panel.outHandle.Connections[j];

                    Handles.color = !Application.isPlaying ? Color.white : new Color(0.5f, 0.5f, 0.5f, 1.0f);

                    if (Application.isPlaying)
                    {
                        // highlight connections that are running
                        if (connection.StartPanel.Node.CurrentChild == connection.EndPanel.Node)
                        {
                            runningConnection = connection;
                        }
                    }

                    Vector2[] p             = connection.Points;
                    Vector3[] pointsVector3 =
                    {
                        CanvasUtility.WorldToCanvasPoint(new Vector3(p[0].x, p[0].y, 0), canvas.canvasState),
                        CanvasUtility.WorldToCanvasPoint(new Vector3(p[1].x, p[1].y, 0), canvas.canvasState),
                        CanvasUtility.WorldToCanvasPoint(new Vector3(p[2].x, p[2].y, 0), canvas.canvasState),
                        CanvasUtility.WorldToCanvasPoint(new Vector3(p[3].x, p[3].y, 0), canvas.canvasState)
                    };

                    Handles.DrawLines(pointsVector3, new int[] { 0, 1, 1, 2, 2, 3 });
                }

                if (runningConnection != null)
                {
                    Handles.color = runningConnection.EndPanel.Node.State == Node.NodeState.Failure ?
                                    BehaviourEditorStyles.playMode_nodeFailureColour : BehaviourEditorStyles.playMode_nodeRunningColour;

                    Vector2[] p             = runningConnection.Points;
                    Vector3[] pointsVector3 = null;

                    if (Math.Abs(p[0].x - p[3].x) < 0.01f)
                    {
                        pointsVector3 = new Vector3[] {
                            CanvasUtility.WorldToCanvasPoint(new Vector3(p[0].x, p[0].y, 0), canvas.canvasState),
                            CanvasUtility.WorldToCanvasPoint(new Vector3(p[3].x, p[3].y, 0), canvas.canvasState),
                        };
                    }
                    else
                    {
                        pointsVector3 = new Vector3[] {
                            CanvasUtility.WorldToCanvasPoint(new Vector3(p[0].x, p[0].y, 0), canvas.canvasState),
                            CanvasUtility.WorldToCanvasPoint(new Vector3(p[1].x, p[1].y, 0), canvas.canvasState),
                            CanvasUtility.WorldToCanvasPoint(new Vector3(p[2].x, p[2].y, 0), canvas.canvasState),
                            CanvasUtility.WorldToCanvasPoint(new Vector3(p[3].x, p[3].y, 0), canvas.canvasState)
                        };
                    }

                    Handles.DrawAAPolyLine(10, pointsVector3.Length, pointsVector3);
                }
            }
        }
Exemplo n.º 19
0
        /*
         * public void DrawBounds () {
         *      if (canvas.ghostGroup == null)
         *              return;
         *
         *      // draw start group bounds
         *      GUI.color = new Color (1, 1, 1, 0.07f);
         *
         *      if (canvas.canvasState.groups == null)
         *              return;
         *
         *      // draw all other groups' bounds
         *      for (int i = 0; i < canvas.canvasState.groups.Count; i++) {
         *              Rect r = CanvasUtility.WorldToCanvasRect(canvas.canvasState.groups [i].transform.rect, canvas.canvasState);
         *              GUI.Box (r, "");
         *      }
         * }
         */

        public void DrawNodePanel(NodePanel panel)
        {
            // draw the backgorund
            Color bgColour = panel.bgColour;

            bgColour.a = panel.alpha;

            if (canvas.selectedPanels.Contains(panel))
            {
                bgColour += BehaviourEditorStyles.nodeSelected_BgColour;
            }

            GUI.color = bgColour;
            Rect rect = CanvasUtility.WorldToCanvasRect(panel.transform.rect, canvas.canvasState);

            GUI.Box(rect, "");
            int padding  = -5;
            int iconSize = 40;

            // draw the icon
            Color contentColour = panel.contentColour;

            contentColour.a = panel.alpha;
            GUI.color       = contentColour;

            Rect iconRect = new Rect((rect.width - iconSize) / 2 + rect.x, (rect.height - iconSize) / 2 + rect.y, iconSize, iconSize);

            //iconRect.width = rect.height;
            GUI.DrawTexture(iconRect, panel.icon);

            // draw the label
            GUIStyle labelStyle = new GUIStyle();

            labelStyle.alignment = TextAnchor.MiddleCenter;

            RectOffset paddedLabelRect = new RectOffset(padding, padding, padding / 2, 0);
            Rect       labelRect       = paddedLabelRect.Add(rect);

            labelRect.height = 20;

            string label = BehaviourEditor.debugView ? panel.guid.ToString().Substring(panel.guid.ToString().Length - 3) : panel.label;

            GUI.Label(labelRect, label, BehaviourEditor.defaultSkin.label);

            GUI.color = panel.Parent == null ? new Color(1, 1, 1, 0.2f) : new Color(1, 1, 1, 1);
            GUI.DrawTexture(CanvasUtility.WorldToCanvasRect(panel.inHandle.transform.rect, canvas.canvasState), BehaviourEditorStyles.handle);
            if (panel.outHandle != null && panel.hasOuthandle)
            {
                GUI.color = panel.Children.Count > 0 || canvas.previewConnection != null &&
                            canvas.previewConnection._start.Parent == panel.outHandle.transform
                                                ? new Color(1, 1, 1, 1)
                                                : new Color(1, 1, 1, 0.2f);
                GUI.DrawTexture(CanvasUtility.WorldToCanvasRect(panel.outHandle.transform.rect, canvas.canvasState),
                                BehaviourEditorStyles.handle);
            }

            if (BehaviourEditor.debugView)
            {
                GUI.color = Color.red;
                Rect anchorRect = new Rect(panel.transform.position, Vector2.one);
                GUI.Box(CanvasUtility.WorldToCanvasRect(anchorRect, canvas.canvasState), "x");
                anchorRect = new Rect(panel.inHandle.transform.position, Vector2.one);
            }
        }
Exemplo n.º 20
0
 public NodeHandle(NodePanel nodePanel, HandleType type, Rect rect, CanvasState canvasState) :
     this(nodePanel.guid, nodePanel.label, type, rect, canvasState)
 {
 }
Exemplo n.º 21
0
        private List <NodePanel> GetTopLevelPanels(List <NodePanel> panels)
        {
            List <NodePanel> remaining = new List <NodePanel>(panels);
            List <NodePanel> processed = new List <NodePanel>();

            List <NodePanel> result = new List <NodePanel>(panels);

            while (remaining.Count > 0)
            {
                NodePanel current = remaining[0];
                remaining.Remove(current);

                processed.Add(current);

                List <NodePanel> descendants = current.Children;

                while (descendants.Count > 0)
                {
                    NodePanel child = descendants[0];
                    descendants.Remove(child);

                    if (result.Contains(child))
                    {
                        result.Remove(child);
                    }

                    if (processed.Contains(child))
                    {
                        continue;
                    }

                    descendants.AddRange(child.Children);
                }
            }

            foreach (var item in result)
            {
                Debug.Log(item.guid);
            }

            return(result);


            //List<NodePanel> result = new List<NodePanel>(panels);
            //List<NodePanel> unique = new List<NodePanel>();

            //for (int i = 0; i < panels.Count; i++)
            //{
            //    List<NodePanel> descendants = GetDescendants(panels[i]);
            //    Debug.Log(descendants.Count);
            //    for (int j = 0; j < descendants.Count; j++)
            //    {
            //        if (!unique.Contains(descendants[j]))
            //        {
            //            unique.Add(descendants[j]);
            //        }
            //    }

            //    if (unique.Contains(panels[i]))
            //    {
            //        result.Remove(panels[i]);
            //    }
            //}

            //Debug.Log("UNIQ");
            //for (int i = 0; i < unique.Count; i++)
            //{
            //    Debug.Log(unique[i].label);
            //}

            //Debug.Log("RESULT");
            //for (int i = 0; i < result.Count; i++)
            //{
            //    Debug.Log(result[i].label);
            //}

            //return result;
        }