internal static AutoSplitAlignment GetAlignment(Point mousePosition, Point targetLocation, Size targetSize) { AutoSplitAlignment alignment = AutoSplitAlignment.None; if (mousePosition.X >= targetLocation.X && mousePosition.X <= targetLocation.X + targetSize.Width) { alignment = AutoSplitAlignment.Vertical; } else if (mousePosition.Y >= targetLocation.Y && mousePosition.Y <= targetLocation.Y + targetSize.Height) { alignment = AutoSplitAlignment.Horizontal; } return(alignment); }
internal static Point CalculateDropLocation(Point mousePosition, Point originalDropLocation, Size droppedSize, Point srcLocation, Point destLocation, Size srcSize, Size destSize, HashSet <Point> shapeLocations) { Point dropLocation = originalDropLocation; double distToSrc = DesignerGeometryHelper.ManhattanDistanceBetweenPoints(mousePosition, new Point(srcLocation.X + (srcSize.Width / 2), srcLocation.Y + (srcSize.Height / 2))); double distToDest = DesignerGeometryHelper.ManhattanDistanceBetweenPoints(mousePosition, new Point(destLocation.X + (destSize.Width / 2), destLocation.Y + (destSize.Height / 2))); AutoSplitAlignment srcAlignment = GetAlignment(mousePosition, srcLocation, srcSize); AutoSplitAlignment destAlignment = GetAlignment(mousePosition, destLocation, destSize); if ((distToSrc <= distToDest || destAlignment == AutoSplitAlignment.None) && srcAlignment == AutoSplitAlignment.Vertical) { dropLocation = CalculateDropLocationToAlignVertically(dropLocation, droppedSize, srcLocation, srcSize); } else if ((distToSrc <= distToDest || destAlignment == AutoSplitAlignment.None) && srcAlignment == AutoSplitAlignment.Horizontal) { dropLocation = CalculateDropLocationToAlignHorizontally(dropLocation, droppedSize, srcLocation, srcSize); } else if ((distToSrc >= distToDest || srcAlignment == AutoSplitAlignment.None) && destAlignment == AutoSplitAlignment.Vertical) { dropLocation = CalculateDropLocationToAlignVertically(dropLocation, droppedSize, destLocation, destSize); } else if ((distToSrc >= distToDest || srcAlignment == AutoSplitAlignment.None) && destAlignment == AutoSplitAlignment.Horizontal) { dropLocation = CalculateDropLocationToAlignHorizontally(dropLocation, droppedSize, destLocation, destSize); } dropLocation.X = dropLocation.X < 0 ? 0 : dropLocation.X; dropLocation.Y = dropLocation.Y < 0 ? 0 : dropLocation.Y; // To avoid overlaps with existing shapes if (shapeLocations != null) { while (shapeLocations.Contains(dropLocation)) { dropLocation.Offset(FreeFormPanel.GridSize, FreeFormPanel.GridSize); } } return(dropLocation); }