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 }