public void ConvertToSubgraphBlock(VFXView sourceView, IEnumerable <Controller> controllers, Rect rect)
            {
                this.m_Rect = rect;
                Init(sourceView, controllers);
                CreateUniqueSubgraph("SubgraphBlock", VisualEffectSubgraphBlock.Extension, VisualEffectAssetEditorUtility.CreateNew <VisualEffectSubgraphBlock>);

                m_SourceControllers.RemoveAll(t => t is VFXContextController); // Don't copy contexts
                CopyPasteNodes();

                m_SourceBlockControllers = m_SourceControllers.OfType <VFXBlockController>().OrderBy(t => t.index).ToList();

                VFXContextController sourceContextController = m_SourceBlockControllers.First().contextController;

                object copyData = VFXCopy.CopyBlocks(m_SourceBlockControllers);

                var targetContext = m_TargetController.graph.children.OfType <VFXBlockSubgraphContext>().FirstOrDefault();

                if (targetContext == null)
                {
                    targetContext = ScriptableObject.CreateInstance <VFXBlockSubgraphContext>();
                    m_TargetController.graph.AddChild(targetContext);
                }
                m_TargetController.LightApplyChanges();
                targetContext.position = sourceContextController.position;
                targetContext.SetSettingValue("m_SuitableContexts", (VFXBlockSubgraphContext.ContextType)m_SourceBlockControllers.Select(t => t.model.compatibleContexts).Aggregate((t, s) => t & s));
                m_TargetBlocks = new List <VFXBlockController>();

                VFXPaste.PasteBlocks(m_TargetController, copyData, targetContext, 0, m_TargetBlocks);

                var otherSourceControllers = m_SourceControllers.OfType <VFXNodeController>().Where(t => !(t is VFXBlockController)).ToList();

                //Create lost links between nodes and blocks
                foreach (var edge in m_SourceController.dataEdges.Where(t => otherSourceControllers.Contains(t.output.sourceNode) && m_SourceBlockControllers.Contains(t.input.sourceNode)))
                {
                    var outputNode = m_TargetControllers[m_SourceControllers.IndexOf(edge.output.sourceNode)];
                    var output     = outputNode.outputPorts.First(t => t.path == edge.output.path);

                    var inputBlock = m_TargetBlocks[m_SourceBlockControllers.IndexOf(edge.input.sourceNode as VFXBlockController)];
                    var input      = inputBlock.inputPorts.First(t => t.path == edge.input.path);

                    m_TargetController.CreateLink(input, output);
                }

                var sourceBlock = ScriptableObject.CreateInstance <VFXSubgraphBlock>();

                m_SourceNode = sourceBlock;
                sourceContextController.model.AddChild(m_SourceNode, m_SourceBlockControllers.Select(t => t.index).Min());
                sourceContextController.ApplyChanges();
                m_SourceNodeController = sourceContextController.blockControllers.First(t => t.model == m_SourceNode);
                PostSetup();
                m_SourceNodeController.ApplyChanges();

                var targetContextController = m_TargetController.GetRootNodeController(targetContext, 0) as VFXContextController;

                m_SourceControllersWithBlocks = m_SourceControllers.Concat(m_SourceBlockControllers);
                TransferEdges();
                UninitSmart();
            }
Esempio n. 2
0
        void PasteAll(VFXViewController viewController, Vector2 center, ref SerializableGraph serializableGraph, VFXView view, VFXGroupNodeController groupNode, List <VFXNodeController> nodesInTheSameOrder)
        {
            newControllers.Clear();

            m_NodesInTheSameOrder = new VFXNodeID[serializableGraph.controllerCount];

            var graph = viewController.graph;

            pasteOffset = (serializableGraph.bounds.width > 0 && serializableGraph.bounds.height > 0) ? center - serializableGraph.bounds.center : Vector2.zero;
            MakePasteOffsetUnique(viewController, serializableGraph);

            // Paste all nodes
            PasteContexts(viewController, ref serializableGraph);
            PasteOperators(viewController, ref serializableGraph);
            PasteParameters(viewController, ref serializableGraph);

            // Create controllers for all new nodes
            viewController.LightApplyChanges();

            // Register all nodes for usage in groupNodes and edges
            RegisterContexts(viewController);
            RegisterOperators(viewController);
            RegisterParameterNodes(viewController);

            VFXUI ui = viewController.graph.UIInfos;

            firstCopiedGroup      = -1;
            firstCopiedStickyNote = ui.stickyNoteInfos != null ? ui.stickyNoteInfos.Length : 0;

            //Paste Everything else
            PasteGroupNodes(ref serializableGraph, ui);
            PasteStickyNotes(ref serializableGraph, ui);

            PasteDatas(ref serializableGraph); // TODO Data settings should be pasted at context creation. This can lead to issues as blocks are added before data is initialized
            PasteDataEdges(ref serializableGraph);
            PasteFlowEdges(ref serializableGraph);

            // Create all ui based on model
            viewController.LightApplyChanges();

            if (nodesInTheSameOrder != null)
            {
                nodesInTheSameOrder.Clear();
                nodesInTheSameOrder.AddRange(m_NodesInTheSameOrder.Select(t => t.model == null ? null : viewController.GetNodeController(t.model, t.id)));
            }

            if (view != null)
            {
                SelectCopiedElements(view, groupNode);
            }
        }
 void PostSetupNode()
 {
     PostSetup();
     m_SourceNode.position = m_Rect.center;
     m_SourceController.graph.AddChild(m_SourceNode);
     m_SourceController.LightApplyChanges();
     m_SourceNodeController = m_SourceController.GetRootNodeController(m_SourceNode, 0);
     m_SourceNodeController.ApplyChanges();
 }
Esempio n. 4
0
 void PostSetupNode()
 {
     PostSetup();
     m_SourceNode.position = m_Rect.center;
     m_SourceController.graph.AddChild(m_SourceNode);
     m_SourceNode.SetSettingValue("m_Subgraph", m_TargetSubgraph);
     m_SourceController.LightApplyChanges();
     m_SourceNodeController = m_SourceController.GetRootNodeController(m_SourceNode, 0);
     m_SourceNodeController.ApplyChanges();
 }
Esempio n. 5
0
        void PasteAll(VFXViewController viewController, Vector2 center, ref SerializableGraph serializableGraph, VFXView view, VFXGroupNodeController groupNode)
        {
            newControllers.Clear();

            var graph = viewController.graph;

            pasteOffset = (serializableGraph.bounds.width > 0 && serializableGraph.bounds.height > 0) ? center - serializableGraph.bounds.center : Vector2.zero;
            MakePasteOffsetUnique(viewController, serializableGraph);

            // Paste all nodes
            PasteContexts(viewController, ref serializableGraph);
            PasteOperators(viewController, ref serializableGraph);
            PasteParameters(viewController, ref serializableGraph);

            // Create controllers for all new nodes
            viewController.LightApplyChanges();

            // Register all nodes for usage in groupNodes and edges
            RegisterContexts(viewController);
            RegisterOperators(viewController);
            RegisterParameterNodes(viewController);

            VFXUI ui = viewController.graph.UIInfos;

            firstCopiedGroup      = -1;
            firstCopiedStickyNote = ui.stickyNoteInfos != null ? ui.stickyNoteInfos.Length : 0;

            //Paste Everything else
            PasteGroupNodes(ref serializableGraph, ui);
            PasteStickyNotes(ref serializableGraph, ui);
            PasteDataEdges(ref serializableGraph);
            PasteFlowEdges(ref serializableGraph);
            PasteDatas(ref serializableGraph);

            // Create all ui based on model
            viewController.LightApplyChanges();

            if (view != null)
            {
                SelectCopiedElements(view, groupNode);
            }
        }
Esempio n. 6
0
        static void PasteNodes(VFXViewController viewController, Vector2 center, Data copyData, ScriptableObject[] allSerializedObjects, VFXView view, VFXGroupNodeController groupNode)
        {
            var     graph       = viewController.graph;
            Vector2 pasteOffset = (copyData.bounds.width > 0 && copyData.bounds.height > 0) ? center - copyData.bounds.center : Vector2.zero;

            // look if pasting there will result in the first element beeing exactly on top of other
            while (true)
            {
                bool foundSamePosition = false;
                if (copyData.contexts != null && copyData.contexts.Length > 0)
                {
                    VFXContext firstContext = copyData.contexts[0];

                    foreach (var existingContext in viewController.graph.children.OfType <VFXContext>())
                    {
                        if ((firstContext.position + pasteOffset - existingContext.position).sqrMagnitude < 1)
                        {
                            foundSamePosition = true;
                            break;
                        }
                    }
                }
                else if (copyData.slotContainers != null && copyData.slotContainers.Length > 0)
                {
                    VFXModel firstContainer = copyData.slotContainers[0];

                    foreach (var existingSlotContainer in viewController.graph.children.Where(t => t is IVFXSlotContainer))
                    {
                        if ((firstContainer.position + pasteOffset - existingSlotContainer.position).sqrMagnitude < 1)
                        {
                            foundSamePosition = true;
                            break;
                        }
                    }
                }
                else
                {
                    VFXUI ui = allSerializedObjects.OfType <VFXUI>().First();

                    if (ui != null)
                    {
                        if (ui.stickyNoteInfos != null && ui.stickyNoteInfos.Length > 0)
                        {
                            foreach (var stickyNote in viewController.stickyNotes)
                            {
                                if ((ui.stickyNoteInfos[0].position.position + pasteOffset - stickyNote.position.position).sqrMagnitude < 1)
                                {
                                    foundSamePosition = true;
                                    break;
                                }
                            }
                        }
                        else if (ui.groupInfos != null && ui.groupInfos.Length > 0)
                        {
                            foreach (var gn in viewController.groupNodes)
                            {
                                if ((ui.groupInfos[0].position.position + pasteOffset - gn.position.position).sqrMagnitude < 1)
                                {
                                    foundSamePosition = true;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (foundSamePosition)
                {
                    pasteOffset += Vector2.one * 30;
                }
                else
                {
                    break;
                }
            }


            if (copyData.contexts != null)
            {
                foreach (var slotContainer in copyData.contexts)
                {
                    var newContext = slotContainer;
                    newContext.position += pasteOffset;
                    ClearLinks(newContext);
                }
            }

            if (copyData.slotContainers != null)
            {
                foreach (var slotContainer in copyData.slotContainers)
                {
                    var newSlotContainer = slotContainer;
                    newSlotContainer.position += pasteOffset;
                    ClearLinks(newSlotContainer as IVFXSlotContainer);
                }
            }


            for (int i = 0; i < allSerializedObjects.Length; ++i)
            {
                ScriptableObject obj = allSerializedObjects[i];

                if (obj is VFXContext || obj is VFXOperator)
                {
                    graph.AddChild(obj as VFXModel);
                }
                else if (obj is VFXParameter)
                {
                    int paramIndex = System.Array.FindIndex(copyData.parameters, t => t.index == i);

                    VFXParameter existingParameter = graph.children.OfType <VFXParameter>().FirstOrDefault(t => t.GetInstanceID() == copyData.parameters[paramIndex].originalInstanceID);
                    if (existingParameter != null)
                    {
                        // The original parameter is from the current graph, add the nodes to the original
                        copyData.parameters[paramIndex].parameter       = existingParameter;
                        copyData.parameters[paramIndex].copiedParameter = obj as VFXParameter;

                        copyData.parameters[paramIndex].infoIndexOffset = existingParameter.nodes.Count;

                        foreach (var info in copyData.parameters[paramIndex].infos)
                        {
                            info.position += pasteOffset;
                        }

                        var oldIDs = copyData.parameters[paramIndex].infos.ToDictionary(t => t, t => t.id);

                        existingParameter.AddNodeRange(copyData.parameters[paramIndex].infos);

                        //keep track of new ids for groupnodes
                        copyData.parameters[paramIndex].idMap = copyData.parameters[paramIndex].infos.ToDictionary(t => oldIDs[t], t => t.id);
                    }
                    else
                    {
                        // The original parameter is from another graph : create the parameter in the other graph, but replace the infos with only the ones copied.
                        copyData.parameters[paramIndex].parameter = obj as VFXParameter;
                        copyData.parameters[paramIndex].parameter.SetNodes(copyData.parameters[paramIndex].infos);

                        graph.AddChild(obj as VFXModel);
                    }
                }
            }


            VFXUI copiedUI              = allSerializedObjects.OfType <VFXUI>().FirstOrDefault();
            int   firstCopiedGroup      = -1;
            int   firstCopiedStickyNote = -1;

            if (copiedUI != null)
            {
                VFXUI ui = viewController.graph.UIInfos;
                firstCopiedStickyNote = ui.stickyNoteInfos != null ? ui.stickyNoteInfos.Length : 0;

                if (copiedUI.groupInfos != null && copiedUI.groupInfos.Length > 0)
                {
                    if (ui.groupInfos == null)
                    {
                        ui.groupInfos = new VFXUI.GroupInfo[0];
                    }
                    firstCopiedGroup = ui.groupInfos.Length;

                    foreach (var groupInfos in copiedUI.groupInfos)
                    {
                        for (int i = 0; i < groupInfos.contents.Length; ++i)
                        {
                            // if we link the parameter node to an existing parameter instead of the copied parameter we have to patch the groupnode content to point the that parameter with the correct id.
                            if (groupInfos.contents[i].model is VFXParameter)
                            {
                                VFXParameter parameter = groupInfos.contents[i].model as VFXParameter;
                                var          paramInfo = copyData.parameters.FirstOrDefault(t => t.copiedParameter == parameter);
                                if (paramInfo.parameter != null) // parameter will not be null unless the struct returned is the default.
                                {
                                    groupInfos.contents[i].model = paramInfo.parameter;
                                    groupInfos.contents[i].id    = paramInfo.idMap[groupInfos.contents[i].id];
                                }
                            }
                            else if (groupInfos.contents[i].isStickyNote)
                            {
                                groupInfos.contents[i].id += firstCopiedStickyNote;
                            }
                        }
                    }

                    ui.groupInfos = ui.groupInfos.Concat(copiedUI.groupInfos.Select(t => new VFXUI.GroupInfo(t)
                    {
                        position = new Rect(t.position.position + pasteOffset, t.position.size)
                    })).ToArray();
                }
                if (copiedUI.stickyNoteInfos != null && copiedUI.stickyNoteInfos.Length > 0)
                {
                    if (ui.stickyNoteInfos == null)
                    {
                        ui.stickyNoteInfos = new VFXUI.StickyNoteInfo[0];
                    }
                    ui.stickyNoteInfos = ui.stickyNoteInfos.Concat(copiedUI.stickyNoteInfos.Select(t => new VFXUI.StickyNoteInfo(t)
                    {
                        position = new Rect(t.position.position + pasteOffset, t.position.size)
                    })).ToArray();
                }
            }

            CopyDataEdges(copyData, allSerializedObjects);


            if (copyData.flowEdges != null)
            {
                foreach (var flowEdge in copyData.flowEdges)
                {
                    VFXContext inputContext  = allSerializedObjects[flowEdge.input.contextIndex] as VFXContext;
                    VFXContext outputContext = allSerializedObjects[flowEdge.output.contextIndex] as VFXContext;

                    inputContext.LinkFrom(outputContext, flowEdge.input.flowIndex, flowEdge.output.flowIndex);
                }
            }

            foreach (var dataAndContexts in copyData.dataAndContexts)
            {
                VFXData data = allSerializedObjects[dataAndContexts.dataIndex] as VFXData;

                foreach (var contextIndex in dataAndContexts.contextsIndexes)
                {
                    VFXContext context = allSerializedObjects[contextIndex] as VFXContext;
                    data.CopySettings(context.GetData());
                }
            }

            // Create all ui based on model
            viewController.LightApplyChanges();

            if (view != null)
            {
                view.ClearSelection();

                var elements = view.graphElements.ToList();


                List <VFXNodeUI>    newSlotContainerUIs = new List <VFXNodeUI>();
                List <VFXContextUI> newContextUIs       = new List <VFXContextUI>();

                foreach (var slotContainer in allSerializedObjects.OfType <VFXContext>())
                {
                    VFXContextUI contextUI = elements.OfType <VFXContextUI>().FirstOrDefault(t => t.controller.model == slotContainer);
                    if (contextUI != null)
                    {
                        newSlotContainerUIs.Add(contextUI);
                        newSlotContainerUIs.AddRange(contextUI.GetAllBlocks().Cast <VFXNodeUI>());
                        newContextUIs.Add(contextUI);
                        view.AddToSelection(contextUI);
                    }
                }
                foreach (var slotContainer in allSerializedObjects.OfType <VFXOperator>())
                {
                    VFXOperatorUI slotContainerUI = elements.OfType <VFXOperatorUI>().FirstOrDefault(t => t.controller.model == slotContainer);
                    if (slotContainerUI != null)
                    {
                        newSlotContainerUIs.Add(slotContainerUI);
                        view.AddToSelection(slotContainerUI);
                    }
                }

                foreach (var param in copyData.parameters)
                {
                    foreach (var parameterUI in elements.OfType <VFXParameterUI>().Where(t => t.controller.model == param.parameter && param.parameter.nodes.IndexOf(t.controller.infos) >= param.infoIndexOffset))
                    {
                        newSlotContainerUIs.Add(parameterUI);
                        view.AddToSelection(parameterUI);
                    }
                }

                // Simply selected all data edge with the context or slot container, they can be no other than the copied ones
                foreach (var dataEdge in elements.OfType <VFXDataEdge>())
                {
                    if (newSlotContainerUIs.Contains(dataEdge.input.GetFirstAncestorOfType <VFXNodeUI>()))
                    {
                        view.AddToSelection(dataEdge);
                    }
                }
                // Simply selected all data edge with the context or slot container, they can be no other than the copied ones
                foreach (var flowEdge in elements.OfType <VFXFlowEdge>())
                {
                    if (newContextUIs.Contains(flowEdge.input.GetFirstAncestorOfType <VFXContextUI>()))
                    {
                        view.AddToSelection(flowEdge);
                    }
                }

                if (groupNode != null)
                {
                    foreach (var newSlotContainerUI in newSlotContainerUIs)
                    {
                        groupNode.AddNode(newSlotContainerUI.controller);
                    }
                }

                //Select all groups that are new
                if (firstCopiedGroup >= 0)
                {
                    foreach (var gn in elements.OfType <VFXGroupNode>())
                    {
                        if (gn.controller.index >= firstCopiedGroup)
                        {
                            view.AddToSelection(gn);
                        }
                    }
                }

                //Select all groups that are new
                if (firstCopiedStickyNote >= 0)
                {
                    foreach (var gn in elements.OfType <VFXStickyNote>())
                    {
                        if (gn.controller.index >= firstCopiedStickyNote)
                        {
                            view.AddToSelection(gn);
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        void PasteAll(VFXViewController viewController, Vector2 center, ref SerializableGraph serializableGraph, VFXView view, VFXGroupNodeController groupNode, List <VFXNodeController> nodesInTheSameOrder)
        {
            newControllers.Clear();

            m_NodesInTheSameOrder = new VFXNodeID[serializableGraph.controllerCount];

            // Can't paste context within subgraph block/operator
            if (viewController.model.visualEffectObject is VisualEffectSubgraphOperator || viewController.model.visualEffectObject is VisualEffectSubgraphBlock)
            {
                if (serializableGraph.contexts != null)
                {
                    var count = serializableGraph.contexts.Count();
                    if (count != 0)
                    {
                        Debug.LogWarningFormat("{0} context{1} been skipped during the paste operation. Contexts aren't available in this kind of subgraph.", count, count > 1 ? "s have" : " has");
                    }
                }
            }
            else
            {
                PasteContexts(viewController, center, ref serializableGraph);
            }

            PasteOperators(viewController, center, ref serializableGraph);
            PasteParameters(viewController, ref serializableGraph, center);

            // Create controllers for all new nodes
            viewController.LightApplyChanges();

            // Register all nodes for usage in groupNodes and edges
            RegisterContexts(viewController);
            RegisterOperators(viewController);
            RegisterParameterNodes(viewController);

            VFXUI ui = viewController.graph.UIInfos;

            firstCopiedGroup      = -1;
            firstCopiedStickyNote = ui.stickyNoteInfos != null ? ui.stickyNoteInfos.Length : 0;

            //Paste Everything else
            PasteGroupNodes(ref serializableGraph, center, ui);
            PasteStickyNotes(ref serializableGraph, center, ui);

            PasteDatas(ref serializableGraph); // TODO Data settings should be pasted at context creation. This can lead to issues as blocks are added before data is initialized
            PasteDataEdges(ref serializableGraph);
            PasteFlowEdges(ref serializableGraph);

            // Create all ui based on model
            viewController.LightApplyChanges();

            if (nodesInTheSameOrder != null)
            {
                nodesInTheSameOrder.Clear();
                nodesInTheSameOrder.AddRange(m_NodesInTheSameOrder.Select(t => t.model == null ? null : viewController.GetNodeController(t.model, t.id)));
            }

            if (view != null)
            {
                SelectCopiedElements(view, groupNode);
            }
        }