public NodeTree(Node _creatorNode, Canvas _nodeEditor) { nodeEditor = _nodeEditor; nodes = new HashSet <Node>(); visualConnectionsStore = new List <VisualConnection>(); visualBuckets = new SortedDictionary <int, VisualBucket>(); nodes.Add(_creatorNode); _creatorNode.NodeTree = this; visualBuckets[0] = new VisualBucket(); visualBuckets[0].Add(_creatorNode); position = new Point() { X = Canvas.GetLeft(_creatorNode), Y = Canvas.GetTop(_creatorNode), }; }
/// <summary> /// Connects a node to a node inside this tree. /// The old tree of the node is combined with this tree. /// </summary> public void ConnectNode(Node _node, Node _originNode) { // Node tree of _connectTo node must be this tree! Debug.Assert(_originNode.NodeTree == this); if (_node.GetDirectlyConnectedNodes(NodeType.NONE).Contains(_originNode)) { return; } NodeConnector origin = null; NodeConnector target = null; foreach (NodeConnector originConnector in _originNode.Connectors) { foreach (NodeConnector targetConnector in _node.Connectors) { if (originConnector.CanConnect(targetConnector) && targetConnector.CanConnect(originConnector)) { origin = originConnector; target = targetConnector; } } } //Check if no connection possible if (origin == null || target == null) { MessageBox.Show("Nodes can't be connected!"); return; } //Link the connectors origin.ConnectTo(target); target.ConnectTo(origin); //Create visual connection visualConnectionsStore.Add(new VisualConnection(origin, target, nodeEditor)); //Combine the two trees if the new node has one if (_node.NodeTree != null && _node.NodeTree != this) { if (_node.NodeTree.NodeCount() > 0) { List <Node> removeNodes = new List <Node>(); foreach (Node node in _node.NodeTree.nodes) { //TODO: Tranfer connections from the old tree nodes.Add(node); removeNodes.Add(node); } foreach (Node node in removeNodes) { _node.NodeTree.RemoveNode(node); } } } else { _node.NodeTree = this; } nodes.Add(_node); //Sort the new node in right bucket int index = GetNodeVisualBucketIndex(_originNode); if (origin.Type == NodeConnectorType.INPUT) { index--; } else if (origin.Type == NodeConnectorType.OUTPUT) { index++; } if (!visualBuckets.ContainsKey(index)) { visualBuckets[index] = new VisualBucket(); } visualBuckets[index].Add(_node); AutoPosition(); }