예제 #1
0
        ///Duplicate the connection providing a new source and target
        public Connection Duplicate(Node newSource, Node newTarget)
        {
            if (newSource == null || newTarget == null)
            {
                Debug.LogError("Can't Duplicate a Connection without providing NewSource and NewTarget Nodes");
                return(null);
            }

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

            if (newSource.graph != null)
            {
                newSource.graph.RecordUndo("Duplicate Connection");
            }

            var resultSourceIndex = newConnection.SetSourceNode(newSource);
            var resultTargetIndex = newConnection.SetTargetNode(newTarget);

            //--
            // var assignable = this as ITaskAssignable;
            // if ( assignable != null && assignable.task != null ) {
            //     ( newConnection as ITaskAssignable ).task = assignable.task.Duplicate(newSource.graph);
            // }

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

            newConnection.OnValidate(resultSourceIndex, resultTargetIndex);
            return(newConnection);
        }
예제 #2
0
        ///Duplicate the connection providing a new source and target
        public Connection Duplicate(Node newSource, Node newTarget)
        {
            if (newSource == null || newTarget == null)
            {
                Debug.LogError("Can't Duplicate a Connection without providing NewSource and NewTarget Nodes");
                return(null);
            }

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

            if (newSource.graph != null)
            {
                newSource.graph.RecordUndo("Duplicate Connection");
            }

            newConnection.SetSource(newSource, false);
            newConnection.SetTarget(newTarget, false);

            var assignable = this as ITaskAssignable;

            if (assignable != null && assignable.task != null)
            {
                (newConnection as ITaskAssignable).task = assignable.task.Duplicate(newSource.graph);
            }

            var sourceIndex = newSource.outConnections.IndexOf(newConnection);
            var targetIndex = newTarget.inConnections.IndexOf(newConnection);

            newConnection.OnValidate(sourceIndex, targetIndex);
            return(newConnection);
        }
예제 #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);
        }
예제 #4
0
        ///Duplicate the task for the target ITaskSystem
        virtual public Task Duplicate(ITaskSystem newOwnerSystem)
        {
            var newTask = JSONSerializer.Clone <Task>(this);

            UndoUtility.RecordObject(newOwnerSystem.contextObject, "Duplicate Task");
            BBParameter.SetBBFields(newTask, newOwnerSystem.blackboard);
            newTask.Validate(newOwnerSystem);
            return(newTask);
        }
예제 #5
0
        ///Duplicate the task for the target ITaskSystem
        virtual public Task Duplicate(ITaskSystem newOwnerSystem)
        {
            //Deep clone
            var newTask = JSONSerializer.Clone <Task>(this);

            newOwnerSystem.RecordUndo("Duplicate Task");
            newTask.Validate(newOwnerSystem);
            return(newTask);
        }
예제 #6
0
        ///Returns the copy
        public static T Get <T>(bool clone = false)
        {
            object o = null;

            if (cachedCopies.TryGetValue(typeof(T), out o) && o is T)
            {
                return(clone? JSONSerializer.Clone <T>((T)o) : (T)o);
            }
            return(default(T));
        }
예제 #7
0
파일: Task.cs 프로젝트: wjezxujian/UnityLab
        //Duplicate the task for the target ITaskSystem
        virtual public Task Duplicate(ITaskSystem newOwnerSystem)
        {
            //Deep clone
            var newTask = JSONSerializer.Clone <Task>(this);

            newOwnerSystem.RecordUndo("Duplicate Task");
            newTask.SetOwnerSystem(newOwnerSystem);
            BBParameter.SetBBFields(newTask, newOwnerSystem.blackboard);
            newTask.OnValidate(newOwnerSystem);
            return(newTask);
        }
예제 #8
0
        ///Duplicate node alone assigned to the provided graph
        public Node Duplicate(Graph targetGraph)
        {
            if (targetGraph == null)
            {
                ParadoxNotion.Services.Logger.LogError("Can't duplicate a Node without providing a Target Graph", "NodeCanvas");
                return(null);
            }

            //deep clone
            var newNode = JSONSerializer.Clone <Node>(this);

            if (targetGraph != null)
            {
                targetGraph.RecordUndo("Duplicate Node");
            }

            targetGraph.allNodes.Add(newNode);
            newNode.inConnections.Clear();
            newNode.outConnections.Clear();

            if (targetGraph == this.graph)
            {
                newNode.position += new Vector2(50, 50);
            }

            newNode._UID  = null;
            newNode.graph = targetGraph;
            BBParameter.SetBBFields(newNode, targetGraph.blackboard);

            //--
            // var assignable = this as ITaskAssignable;
            // if ( assignable != null && assignable.task != null ) {
            //     ( newNode as ITaskAssignable ).task = assignable.task.Duplicate(targetGraph);
            // }

            foreach (var task in Graph.GetTasksInElement(newNode))
            {
                task.Validate(targetGraph);
            }
            //--

            newNode.OnValidate(targetGraph);
            return(newNode);
        }
예제 #9
0
        ///Duplicate node alone assigned to the provided graph
        public Node Duplicate(Graph targetGraph)
        {
            if (targetGraph == null)
            {
                Logger.LogError("Can't duplicate a Node without providing a Target Graph", LogTag.GRAPH);
                return(null);
            }

            //deep clone
            var newNode = JSONSerializer.Clone <Node>(this);

            UndoUtility.RecordObject(targetGraph, "Duplicate Node");

            targetGraph.allNodes.Add(newNode);
            newNode.inConnections.Clear();
            newNode.outConnections.Clear();

            if (targetGraph == this.graph)
            {
                newNode.position += new Vector2(50, 50);
            }

            newNode._UID  = null;
            newNode.graph = targetGraph;
            BBParameter.SetBBFields(newNode, targetGraph.blackboard);

            foreach (var task in Graph.GetTasksInElement(newNode))
            {
                task.Validate(targetGraph);
            }
            //--

            newNode.Validate(targetGraph);
            UndoUtility.SetDirty(targetGraph);
            return(newNode);
        }
예제 #10
0
 ///Sets a copy
 public static void Set <T>(T obj, bool clone = false)
 {
     cachedCopies[typeof(T)] = clone? JSONSerializer.Clone <T>(obj) : obj;
 }
예제 #11
0
        ///Makes a copy of provided nodes and if targetGraph is provided, pastes the new nodes in that graph.
        public static List <Node> CloneNodes(List <Node> originalNodes, Graph targetGraph = null, Vector2 originPosition = default(Vector2))
        {
            if (targetGraph != null)
            {
                if (originalNodes.Any(n => n.GetType().IsSubclassOf(targetGraph.baseNodeType) == false))
                {
                    return(null);
                }
            }

            var newNodes = new List <Node>();
            var linkInfo = new Dictionary <Connection, KeyValuePair <int, int> >();

            //duplicate all nodes first
            foreach (var original in originalNodes)
            {
                Node newNode = null;
                if (targetGraph != null)
                {
                    newNode = original.Duplicate(targetGraph);
                    if (targetGraph != original.graph && original.graph != null && original == original.graph.primeNode)
                    {
                        targetGraph.primeNode = newNode;
                    }
                }
                else
                {
                    newNode = JSONSerializer.Clone <Node>(original);
                }
                newNodes.Add(newNode);

                //store the out connections that need dulpicate along with the indeces of source and target
                foreach (var c in original.outConnections)
                {
                    var sourceIndex = originalNodes.IndexOf(c.sourceNode);
                    var targetIndex = originalNodes.IndexOf(c.targetNode);
                    linkInfo[c] = new KeyValuePair <int, int>(sourceIndex, targetIndex);
                }
            }

            //duplicate all connections that we stored as 'need duplicating' providing new source and target
            foreach (var linkPair in linkInfo)
            {
                if (linkPair.Value.Value != -1)                  //we check this to see if the target node is part of the duplicated nodes since IndexOf returns -1 if element is not part of the list
                {
                    var newSource = newNodes[linkPair.Value.Key];
                    var newTarget = newNodes[linkPair.Value.Value];
                    if (targetGraph != null)
                    {
                        linkPair.Key.Duplicate(newSource, newTarget);
                    }
                    else
                    {
                        var newConnection = JSONSerializer.Clone <Connection>(linkPair.Key);
                        newConnection.SetSource(newSource, false);
                        newConnection.SetTarget(newTarget, false);
                    }
                }
            }

            if (originPosition != default(Vector2) && newNodes.Count > 0)
            {
                if (newNodes.Count == 1)
                {
                    newNodes[0].nodePosition = originPosition;
                }
                else
                {
                    var diff = newNodes[0].nodePosition - originPosition;
                    newNodes[0].nodePosition = originPosition;
                    for (var i = 1; i < newNodes.Count; i++)
                    {
                        newNodes[i].nodePosition -= diff;
                    }
                }
            }

            return(newNodes);
        }