private void CleanupOldPorts() { //clear all the inputs but the first one //which is the family instance //first kill all the connectors for (int i = 1; i < InPortData.Count; i++) { dynPort p = InPorts[i]; //must remove the connectors iteratively //do not use a foreach here! while (p.Connectors.Count > 0) { dynConnector c = p.Connectors[p.Connectors.Count - 1] as dynConnector; c.Kill(); } } //then remove all the ports while (InPorts.Count > 1) { InPorts.RemoveAt(InPorts.Count - 1); InPortData.RemoveAt(InPortData.Count - 1); } while (gridLeft.RowDefinitions.Count > 1) { //remove the port from the children list gridLeft.Children.RemoveAt(gridLeft.RowDefinitions.Count - 1); gridLeft.RowDefinitions.RemoveAt(gridLeft.RowDefinitions.Count - 1); } }
void OnMouseLeftButtonDown(object sender, System.Windows.Input.MouseEventArgs e) { //Keyboard.Focus(this); hitResultsList.Clear(); TestClick(e.GetPosition(workBench)); #region test for a port dynPort p = null; if (hitResultsList.Count > 0) { foreach (DependencyObject depObj in hitResultsList) { //traverse the tree through all the //hit elements to see if you get a port p = ElementClicked(depObj, typeof(dynPort)) as dynPort; if (p != null) { break; } } } if (p != null) { if (!isConnecting) { //test if port already has a connection if so grab it //and begin connecting to somewhere else //don't allow the grabbing of the start connector if (p.Connectors.Count > 0 && p.Connectors[0].Start != p) { activeConnector = p.Connectors[0]; activeConnector.Disconnect(p); isConnecting = true; workBench.isConnecting = true; } else { try { //you've begun creating a connector dynConnector c = new dynConnector(p, workBench, e.GetPosition(workBench)); activeConnector = c; isConnecting = true; workBench.isConnecting = true; } catch (Exception ex) { Debug.WriteLine(ex.Message); } } } else { //attempt a connection between the port //and the connector if (!activeConnector.Connect(p)) { activeConnector.Kill(); isConnecting = false; workBench.isConnecting = false; activeConnector = null; } else { //you've already started connecting //now you're going to stop isConnecting = false; workBench.isConnecting = false; activeConnector = null; } } //set the handled flag so that the element doesn't get dragged e.Handled = true; } else { //if you click on the canvas and you're connecting //then drop the connector, otherwise do nothing if (activeConnector != null) { activeConnector.Kill(); isConnecting = false; workBench.isConnecting = false; activeConnector = null; } } #endregion #region test for canvas hitResultsList.Clear(); TestClick(e.GetPosition(workBench)); DragCanvas dc = null; if (hitResultsList.Count > 0) { foreach (DependencyObject depObj in hitResultsList) { //traverse the tree through all the //hit elements to see if you get a port dc = ElementClicked(depObj, typeof(DragCanvas)) as DragCanvas; if (dc != null) { Debug.WriteLine("Canvas clicked"); ClearSelection(); break; } } } #endregion #region test for dyn element hitResultsList.Clear(); TestClick(e.GetPosition(workBench)); dynElement element = null; if (hitResultsList.Count > 0) { foreach (DependencyObject depObj in hitResultsList) { //traverse the tree through all the //hit elements to see if you get an element element = ElementClicked(depObj, typeof(dynElement)) as dynElement; if (element != null) { SelectElement(element); break; } } } #endregion }
/// <summary> /// Add a dynPort element to this control. /// </summary> /// <param name="isInput">Is the port an input?</param> /// <param name="index">The index of the port in the port list.</param> public void AddPort(object el, PortType portType, string name, int index) { dynPort p = new dynPort(index); //create a text block for the name of the port TextBlock tb = new TextBlock(); tb.VerticalAlignment = VerticalAlignment.Center; tb.FontSize = 12; tb.FontWeight = FontWeights.Normal; tb.Foreground = new SolidColorBrush(Colors.Black); tb.Text = name; //set the z order to the back //Canvas.SetZIndex(p, 1); if (portType == PortType.INPUT) { tb.HorizontalAlignment = HorizontalAlignment.Left; p.PortType = PortType.INPUT; inPorts.Add(p); gridLeft.Children.Add(p); Grid.SetColumn(p, 0); Grid.SetRow(p, index); //portNamesLeft.Children.Add(tb); gridLeft.Children.Add(tb); Grid.SetColumn(tb, 1); Grid.SetRow(tb, index); } else if (portType == PortType.OUTPUT) { tb.HorizontalAlignment = HorizontalAlignment.Right; p.PortType = PortType.OUTPUT; outPorts.Add(p); gridRight.Children.Add(p); Grid.SetColumn(p, 1); Grid.SetRow(p, index); //portNamesRight.Children.Add(tb); gridRight.Children.Add(tb); Grid.SetColumn(tb, 0); Grid.SetRow(tb, index); } //else if (portType == PortType.STATE) //{ // tb.HorizontalAlignment = HorizontalAlignment.Center; // p.PortType = PortType.STATE; // statePorts.Add(p); // gridBottom.Children.Add(p); // Grid.SetColumn(p, index); // Grid.SetRow(p, 0); // portNamesBottom.Children.Add(tb); // Grid.SetColumn(tb, index); // Grid.SetRow(tb, 0); //} p.Owner = this; //register listeners on the port p.PortConnected += new PortConnectedHandler(p_PortConnected); p.PortDisconnected += new PortConnectedHandler(p_PortDisconnected); }