BehaviorTree / Editor / GraphElements / PortElement.cs
PortElement.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;

namespace AI.BT
{
    public class PortElement : Port
    {
        public int m_Index { get; private set; }

        private class DefaultEdgeConnectorListener : IEdgeConnectorListener
        {
            private GraphViewChange m_GraphViewChange;

            private List<Edge> m_EdgesToCreate;

            private List<GraphElement> m_EdgesToDelete;

           
            public DefaultEdgeConnectorListener()
            {
                m_EdgesToCreate = new List<Edge>();
                m_EdgesToDelete = new List<GraphElement>();
                m_GraphViewChange.edgesToCreate = m_EdgesToCreate;
            }

            public void OnDropOutsidePort(Edge edge, Vector2 position)
            {
            }

            public void OnDrop(GraphView graphView, Edge edge)
            {
                m_EdgesToCreate.Clear();
                m_EdgesToCreate.Add(edge);
                m_EdgesToDelete.Clear();
                if (edge.input.capacity == Capacity.Single)
                {
                    foreach (Edge connection in edge.input.connections)
                    {
                        if (connection != edge)
                        {
                            m_EdgesToDelete.Add(connection);
                        }
                    }
                }

                if (edge.output.capacity == Capacity.Single)
                {
                    foreach (Edge connection2 in edge.output.connections)
                    {
                        if (connection2 != edge)
                        {
                            m_EdgesToDelete.Add(connection2);
                        }
                    }
                }

                if (m_EdgesToDelete.Count > 0)
                {
                    graphView.DeleteElements(m_EdgesToDelete);
                }

                List<Edge> edgesToCreate = m_EdgesToCreate;
                if (graphView.graphViewChanged != null)
                {
                    edgesToCreate = graphView.graphViewChanged(m_GraphViewChange).edgesToCreate;
                }

                foreach (Edge item in edgesToCreate)
                {
                    graphView.AddElement(item);
                    edge.input.Connect(item);
                    edge.output.Connect(item);
                }
            }
        }

        private Length Width = new Length(75, LengthUnit.Pixel);

        private Length Height = new Length(40, LengthUnit.Pixel);

        protected PortElement(Orientation portOrientation, Direction portDirection, Capacity portCapacity, Type type, int index) 
            : base(portOrientation, portDirection, portCapacity, type)
        {
           m_Index = index;
        }

        public override bool ContainsPoint(Vector2 localPoint)
        {
            Rect rect = m_ConnectorBox.layout;

            Rect rect2;
            if (direction == Direction.Output)
            {
                rect2 = new Rect(0f - rect.xMin, 0f - rect.yMin, rect.width + rect.xMin, layout.height);
              
            }
            else
            {
                rect2 = new Rect(0f, 0f - rect.yMin, layout.width + rect.xMin, layout.height);
                float num = rect.xMin;
                rect2.xMin -= num;
                rect2.width += num;
            }

            return rect2.Contains(this.ChangeCoordinatesTo(m_ConnectorBox, localPoint));
        }

        public static PortElement Create<TEdge>(Orientation orientation, Direction direction, Capacity capacity, Type type, int index) where TEdge : Edge, new()
        {
            DefaultEdgeConnectorListener listener = new DefaultEdgeConnectorListener();
            PortElement port = new PortElement(orientation, direction, capacity, type, index)
            {
                m_EdgeConnector = new EdgeConnector<TEdge>(listener)
            };
            port.AddManipulator(port.m_EdgeConnector);
            return port;
        }
    }
}