void AddSpacingPosition(float spacingPos, ReferenceRects referenceRects) { if (!m_SpacingPositions.ContainsKey(spacingPos)) { m_SpacingPositions.Add(spacingPos, referenceRects); } }
void ComputeSpacingPositions(ReferenceRects referenceRects, Rect sourceRect) { SnapReference startReference = referenceRects.Orientation == Orientation.Vertical ? SnapReference.TopEdge : SnapReference.LeftEdge; SnapReference endReference = referenceRects.Orientation == Orientation.Vertical ? SnapReference.BottomEdge : SnapReference.RightEdge; for (int i = 0; i < referenceRects.Rects.Count; ++i) { Rect firstRect = referenceRects.Rects[i]; int nextRectIndex = i + 1; // After rect i is done, we don't consider it anymore for the next iterations for (int j = nextRectIndex; j < referenceRects.Rects.Count; ++j) { Rect secondRect = referenceRects.Rects[j]; // For each rect i, we find the 3 spacing positions: (examples are for horizontal orientation) // - 1. position before rect i // +-----+ +-----+ +-----+ // | pos | | i | | j | // +-----+ +-----+ +-----+ // - 2. position between rect i and rect j // +-----+ +-----+ +-----+ // | i | | pos | | j | // +-----+ +-----+ +-----+ // - 3. position after rect j // +-----+ +-----+ +-----+ // | i | | j | | pos | // +-----+ +-----+ +-----+ List <float> spacingPositions = GetSpacingPositions(sourceRect, firstRect, secondRect, startReference, endReference, referenceRects.Orientation); AddSpacingPositions(spacingPositions, sourceRect, firstRect, secondRect, referenceRects.Orientation); } } }
SnapToSpacingResult GetSnapToSpacingResult(Rect sourceRect, float middlePos, ReferenceRects referenceRects) { float sourceRectCenter = referenceRects.Orientation == Orientation.Vertical ? GetMaxPos(sourceRect, SnapReference.VerticalCenter).y : GetMaxPos(sourceRect, SnapReference.HorizontalCenter).x; float offset = sourceRectCenter - middlePos; SnapToSpacingResult minResult = new SnapToSpacingResult { Offset = offset, ReferenceRects = referenceRects }; return(minResult.Distance <= SnapDistance * 1 / m_CurrentScale ? minResult : null); }