private AdornerDataThumb<ModelItem, Connector> GetConnectorThumb(Connector conn) { AdornerDataThumb<ModelItem, Connector> thumb = new AdornerDataThumb<ModelItem, Connector>(AdornedElement as ModelItem, conn); thumb.Style = FindResource("ConnectorStyle_" + conn.Type.ToString()) as Style; thumb.DragDelta += OnDragDelta_DetectPath; thumb.DragCompleted += OnDragCompleted_Connect; return thumb; }
/// <summary> /// Do hit test, if the target is a thumb (another connector), then return it /// </summary> /// <param name="hitPoint"></param> /// <returns></returns> private static Connector HitTest(AdornerDataThumb<ModelItem, Connector> thumb, Point hitPoint) { ModelItem elem = thumb.AdornedParent; Canvas canvas = elem.ContainerCanvas; HitTestResult hitRes = VisualTreeHelper.HitTest(canvas, hitPoint); DependencyObject hitObj = null; Model model = elem.ContentObject; ModelItem res = null; if (hitRes != null) hitObj = hitRes.VisualHit; while (hitObj != null && hitObj != canvas && (res = hitObj as ModelItem) == null) hitObj = VisualTreeHelper.GetParent(hitObj); #if DEBUG_ON // test value System.Console.WriteLine("{0} hitObj {1} ", System.DateTime.Now.Millisecond, hitObj.GetType()); #endif // test fails if (res == null || res == elem) return null; // spilit the rectangle into four areas with two line: y=-h/w*x+h, y=h/w*x double width = res.Size.Width, height = res.Size.Height; Vector relativeVector = hitPoint - res.Position; double y1 = relativeVector.X * height / width, y2 = -y1 + height; ConnectorType type = ConnectorType.Center; // now calculate which connector is that if (relativeVector.Y < y1 && relativeVector.Y < y2) type = ConnectorType.Top; else if (relativeVector.Y > y1 && relativeVector.Y < y2) type = ConnectorType.Left; else if (relativeVector.Y > y1 && relativeVector.Y > y2) type = ConnectorType.Bottom; else if (relativeVector.Y < y1 && relativeVector.Y > y2) type = ConnectorType.Right; #if DEBUG_ON // test value System.Console.WriteLine("{0} Connector.Type {1} width {2} height {3}", System.DateTime.Now.Millisecond, type.ToString(), width, height); #endif return res.ContentObject.GetConnector(type); }