private void PasteFlowEdges(ref SerializableGraph serializableGraph) { if (serializableGraph.flowEdges != null) { foreach (var flowEdge in serializableGraph.flowEdges) { VFXContext inputContext = newControllers.ContainsKey(flowEdge.input.contextIndex) ? (newControllers[flowEdge.input.contextIndex] as VFXContextController).model : null; VFXContext outputContext = newControllers.ContainsKey(flowEdge.output.contextIndex) ? (newControllers[flowEdge.output.contextIndex] as VFXContextController).model : null; if (inputContext != null && outputContext != null) { inputContext.LinkFrom(outputContext, flowEdge.output.flowIndex, flowEdge.input.flowIndex); } } } }
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); } } } } }