// TODO: // 1) Move to Node extension method // 2) Use predicate which returns true as soon as we find graphElementModel that corresponds to node's graphElementModel public static bool IsNodeConnectedTo(Unity.GraphElements.Node node, Unity.GraphElements.Node otherNode) { if (node == null) { return(false); } var otherGraphElementModel = (otherNode as IHasGraphElementModel)?.GraphElementModel; if (otherGraphElementModel == null) { return(false); } // Fetch all nodes first HashSet <Unity.GraphElements.Node> visitedNodes = new HashSet <Unity.GraphElements.Node>(); VisitNodes(node, ref visitedNodes); return(visitedNodes.Any(x => x is IHasGraphElementModel hasGraphElementModel && ReferenceEquals(hasGraphElementModel.GraphElementModel, otherGraphElementModel))); }
private static Port GetPortFromPortModel(IPortModel port, Unity.GraphElements.Node nodeUi) { Unity.GraphElements.Node n = nodeUi; var portContainer = port.Direction == Direction.Output ? n.outputContainer : n.inputContainer; Port p = portContainer.childCount > 0 ? portContainer.Query <Port>().Where(x => x.Model == port).First() : null; return(p); }
static void VisitNodes(Unity.GraphElements.Node node, ref HashSet <Unity.GraphElements.Node> visitedGraphElements) { if (node == null || visitedGraphElements == null || visitedGraphElements.Contains(node)) { return; } if (node is StackNode) { foreach (var visualElement in node.inputContainer.Children()) { var port = (Port)visualElement; if (port?.connections == null) { continue; } foreach (Unity.GraphElements.Edge connection in port.connections) { Unity.GraphElements.Port otherConnection = connection.output; Unity.GraphElements.Node otherNode = otherConnection?.node; if (otherNode == null) { continue; } visitedGraphElements.Add(otherNode); VisitNodes(otherNode, ref visitedGraphElements); } } } else { foreach (var visualElement in node.outputContainer.Children()) { var port = (Port)visualElement; if (port?.connections == null) { continue; } foreach (Unity.GraphElements.Edge connection in port.connections) { Unity.GraphElements.Port otherConnection = connection.input; Unity.GraphElements.Node otherNode = otherConnection?.node; if (otherNode == null) { continue; } visitedGraphElements.Add(otherNode); VisitNodes(otherNode, ref visitedGraphElements); } } } switch (node) { case FunctionNode functionNode: visitedGraphElements.Add(functionNode); break; case Node vsNode when vsNode.IsInStack: visitedGraphElements.Add(vsNode.Stack); break; } }