Exemplo n.º 1
0
        ///<summary>Sets the target node of the connection</summary>
        public int SetTargetNode(Node newTarget, int index = -1)
        {
            if (targetNode == newTarget)
            {
                return(-1);
            }

            UndoUtility.RecordObject(graph, "Set Target");

            //relink
            if (targetNode != null && targetNode.inConnections.Contains(this))
            {
                var i = targetNode.inConnections.IndexOf(this);
                targetNode.OnParentDisconnected(i);
                targetNode.inConnections.Remove(this);
            }

            index = index == -1 ? newTarget.inConnections.Count : index;
            newTarget.inConnections.Insert(index, this);
            newTarget.OnParentConnected(index);
            targetNode = newTarget;

#if UNITY_EDITOR
            if (sourceNode != null && targetNode != null)
            {
                targetNode.TrySortConnectionsByPositionX();
            }
#endif

            OnValidate(sourceNode != null ? sourceNode.outConnections.IndexOf(this) : -1, index);
            UndoUtility.SetDirty(graph);
            return(index);
        }
Exemplo n.º 2
0
        ///<summary>Create a new Connection. Use this for constructor</summary>
        public static Connection Create(Node source, Node target, int sourceIndex = -1, int targetIndex = -1)
        {
            if (source == null || target == null)
            {
                Logger.LogError("Can't Create a Connection without providing Source and Target Nodes");
                return(null);
            }

            if (source is MissingNode)
            {
                Logger.LogError("Creating new Connections from a 'MissingNode' is not allowed. Please resolve the MissingNode node first");
                return(null);
            }

            var newConnection = (Connection)System.Activator.CreateInstance(source.outConnectionType);

            UndoUtility.RecordObject(source.graph, "Create Connection");

            var resultSourceIndex = newConnection.SetSourceNode(source, sourceIndex);
            var resultTargetIndex = newConnection.SetTargetNode(target, targetIndex);

            newConnection.OnValidate(resultSourceIndex, resultTargetIndex);
            newConnection.OnCreate(resultSourceIndex, resultTargetIndex);
            UndoUtility.SetDirty(source.graph);

            return(newConnection);
        }
Exemplo n.º 3
0
        ///<summary>Duplicate the connection providing a new source and target</summary>
        public Connection Duplicate(Node newSource, Node newTarget)
        {
            if (newSource == null || newTarget == null)
            {
                Logger.LogError("Can't Duplicate a Connection without providing NewSource and NewTarget Nodes");
                return(null);
            }

            //deep clone
            var newConnection = JSONSerializer.Clone <Connection>(this);

            UndoUtility.RecordObject(newSource.graph, "Duplicate Connection");

            newConnection._UID       = null;
            newConnection.sourceNode = newSource;
            newConnection.targetNode = newTarget;
            newSource.outConnections.Add(newConnection);
            newTarget.inConnections.Add(newConnection);

            if (newSource.graph != null)
            {
                foreach (var task in Graph.GetTasksInElement(newConnection))
                {
                    task.Validate(newSource.graph);
                }
            }
            //--

            newConnection.OnValidate(newSource.outConnections.Count - 1, newTarget.inConnections.Count - 1);
            UndoUtility.SetDirty(newSource.graph);
            return(newConnection);
        }