public static Rectangle SetRectangleEdge(Rectangle rect, MagnetEdge edge) { var prevRight = rect.Right; var prevBottom = rect.Bottom; switch (edge.EdgeDir) { case EdgeDirection.Left: rect.X = edge.Start.X; rect.Width = prevRight - edge.Start.X; break; case EdgeDirection.Right: rect.Width = edge.Start.X - rect.Left; break; case EdgeDirection.Top: rect.Y = edge.Start.Y; rect.Height = prevBottom - edge.Start.Y; break; case EdgeDirection.Bottom: rect.Height = edge.Start.Y - rect.Top; break; } return(rect); }
private int FindClosestEdge(MagnetEdge refEdge) { if (refEdge == null) { return(int.MinValue); } // get all forms edges list var edges = Forms .Where(frm => (frm != m_OriginalForm) && frm.Visible) .SelectMany(MagnetUtils.FormToEdges) .Where(t => !m_MoveLocks.Any(q => (q.Item1 == t.Window) && (q.Item2 == MagnetUtils.OppsiteEdge(t.EdgeDir)))) .ToList(); var corners = Forms .Where(frm => (frm != m_OriginalForm) && frm.Visible) .SelectMany(MagnetUtils.FormToCorners) .Where(t => !m_ResizeLocks.Any(q => (q.Item1 == t.Window) && (q.Item2 == t.AttractsEdge))) .ToList(); var bestDist = int.MaxValue; foreach (var edge in edges) { var dist = edge.MeasureAttraction(refEdge); if (dist != int.MinValue) { if (Math.Abs(dist) < bestDist) { bestDist = dist; } } } foreach (var corner in corners) { var dist = corner.MeasureAttraction(refEdge); if (dist != int.MinValue) { if (Math.Abs(dist) < bestDist) { bestDist = dist; } } } return(bestDist); }
public int MeasureAttraction(MagnetEdge other) { if (other == null) { return(int.MinValue); } // Do not compare to self if (Window == other.Window) { return(int.MinValue); } // Top-to-Bottom match if ((EdgeDir == EdgeDirection.Top) && (other.EdgeDir == EdgeDirection.Bottom) && (Start.X < other.End.X) && (End.X > other.Start.X)) { return(Start.Y - other.Start.Y); } // Bottom-to-Top match if ((EdgeDir == EdgeDirection.Bottom) && (other.EdgeDir == EdgeDirection.Top) && (Start.X < other.End.X) && (End.X > other.Start.X)) { return(other.Start.Y - Start.Y); } // Left-to-Right match if ((EdgeDir == EdgeDirection.Left) && (other.EdgeDir == EdgeDirection.Right) && (Start.Y < other.End.Y) && (End.Y > other.Start.Y)) { return(Start.X - other.Start.X); } // Right-to-Left match if ((EdgeDir == EdgeDirection.Right) && (other.EdgeDir == EdgeDirection.Left) && (Start.Y < other.End.Y) && (End.Y > other.Start.Y)) { return(other.Start.X - Start.X); } return(int.MinValue); }
public static Rectangle SetEntireRectangleEdge(Rectangle rect, MagnetEdge edge) { switch (edge.EdgeDir) { case EdgeDirection.Left: rect.X = edge.Start.X; break; case EdgeDirection.Right: rect.X = edge.Start.X - rect.Width; break; case EdgeDirection.Top: rect.Y = edge.Start.Y; break; case EdgeDirection.Bottom: rect.Y = edge.Start.Y - rect.Height; break; } return(rect); }
public int MeasureAttraction(MagnetEdge other) { if (other == null) { return(int.MinValue); } // Do not compare to self if (Window == other.Window) { return(int.MinValue); } // Is edge effected by this corner? if (other.EdgeDir != AttractsEdge) { return(int.MinValue); } // Select near/far var edgeEnd = AttractsEdgeEnd == EdgeEnd.Near ? other.Start : other.End; // Select axis var aboveOrBelow = (AttractsEdge == EdgeDirection.Left) || (AttractsEdge == EdgeDirection.Right); // Check if edge touchs the corner var otherPos = aboveOrBelow ? edgeEnd.Y : edgeEnd.X; var cornerPos = aboveOrBelow ? Position.Y : Position.X; if (otherPos != cornerPos) { return(int.MinValue); } // Compute offset otherPos = aboveOrBelow ? edgeEnd.X : edgeEnd.Y; cornerPos = aboveOrBelow ? Position.X : Position.Y; return(OffsetDirection * (otherPos - cornerPos)); }