public Guidelines GenerateGuidelines(IEnumerable <Rect> rectangles) { _guidelines.Clear(); foreach (var rect in rectangles) { var centerPoint = Bounds.CenterOf(rect); _guidelines.Add(new Guideline { Position = rect.Top, Orientation = GuideLineOrientation.Horizontal, Type = GuidelineType.Edge }); _guidelines.Add(new Guideline { Position = rect.Bottom, Orientation = GuideLineOrientation.Horizontal, Type = GuidelineType.Edge }); _guidelines.Add(new Guideline { Position = centerPoint.Y, Orientation = GuideLineOrientation.Horizontal, Type = GuidelineType.Center }); _guidelines.Add(new Guideline { Position = rect.Left, Orientation = GuideLineOrientation.Vertical, Type = GuidelineType.Edge }); _guidelines.Add(new Guideline { Position = rect.Right, Orientation = GuideLineOrientation.Vertical, Type = GuidelineType.Edge }); _guidelines.Add(new Guideline { Position = centerPoint.X, Orientation = GuideLineOrientation.Vertical, Type = GuidelineType.Center }); } OnLinesUpdated(); return(this); }
private static Point GetTrackingPoint(CardElement element, string direction) { var point = Bounds.CenterOf(element.Bounds); if (direction.Contains("Top")) { point.Y = element.Top; } if (direction.Contains("Bottom")) { point.Y = element.Bounds.Bottom; } if (direction.Contains("Left")) { point.X = element.Left; } if (direction.Contains("Right")) { point.X = element.Bounds.Right; } return(point); }
private Rect GetHorizontalSnap(Rect rect) { var results = new List <SnapResult>(); foreach (var guideline in _guidelines.Where(gl => gl.Orientation == GuideLineOrientation.Vertical && gl.Type == GuidelineType.Edge)) { guideline.IsActive = false; var delta = rect.Left - guideline.Position; if (_inSnapDistance(delta)) { results.Add(new SnapResult { GuideLine = guideline, Delta = delta, SnappedRect = new Rect(guideline.Position, rect.Top, rect.Width, rect.Height) }); } delta = rect.Right - guideline.Position; if (_inSnapDistance(delta)) { results.Add(new SnapResult { GuideLine = guideline, Delta = delta, SnappedRect = new Rect(guideline.Position - rect.Width, rect.Top, rect.Width, rect.Height) }); } } foreach (var guideline in _guidelines.Where(gl => gl.Orientation == GuideLineOrientation.Vertical && gl.Type == GuidelineType.Center)) { guideline.IsActive = false; var centerLine = Bounds.CenterOf(rect).X; var delta = centerLine - guideline.Position; if (_inSnapDistance(delta)) { results.Add(new SnapResult { GuideLine = guideline, Delta = delta, SnappedRect = new Rect(guideline.Position - rect.Width / 2f, rect.Top, rect.Width, rect.Height) }); } } if (results.Any()) { var closest = results.OrderBy(r => r.Delta).First(); foreach (var result in results.Where(r => r.Delta == closest.Delta)) { result.GuideLine.IsActive = true; } return(closest.SnappedRect); } return(rect); }