Example #1
0
        List <SnapResult> GetClosestSnapElements(Rect sourceRect, Orientation orientation)
        {
            SnapReference     startReference  = orientation == Orientation.Horizontal ? SnapReference.LeftEdge : SnapReference.TopEdge;
            SnapReference     centerReference = orientation == Orientation.Horizontal ? SnapReference.HorizontalCenter : SnapReference.VerticalCenter;
            SnapReference     endReference    = orientation == Orientation.Horizontal ? SnapReference.RightEdge : SnapReference.BottomEdge;
            List <SnapResult> results         = new List <SnapResult>(3);
            SnapResult        result          = GetClosestSnapElement(sourceRect, startReference, startReference, centerReference, endReference);

            if (result != null)
            {
                results.Add(result);
            }
            result = GetClosestSnapElement(sourceRect, centerReference, startReference, centerReference, endReference);
            if (result != null)
            {
                results.Add(result);
            }
            result = GetClosestSnapElement(sourceRect, endReference, startReference, centerReference, endReference);
            if (result != null)
            {
                results.Add(result);
            }
            // Look for the minimum
            if (results.Count > 0)
            {
                results.Sort((a, b) => a.distance.CompareTo(b.distance));
                float minDistance = results[0].distance;
                results.RemoveAll(r => Math.Abs(r.distance - minDistance) > 0.01f);
            }
            return(results);
        }
Example #2
0
        SnapResult GetClosestSnapElement(Rect sourceRect, SnapReference sourceRef, Rect snappableRect, SnapReference startReference, SnapReference centerReference, SnapReference endReference)
        {
            float         sourcePos             = GetPos(sourceRect, sourceRef);
            float         offsetStart           = sourcePos - GetPos(snappableRect, startReference);
            float         offsetEnd             = sourcePos - GetPos(snappableRect, endReference);
            float         minOffset             = offsetStart;
            SnapReference minSnappableReference = startReference;

            if (Math.Abs(minOffset) > Math.Abs(offsetEnd))
            {
                minOffset             = offsetEnd;
                minSnappableReference = endReference;
            }
            SnapResult minResult = new SnapResult
            {
                sourceRect         = sourceRect,
                sourceReference    = sourceRef,
                snappableRect      = snappableRect,
                snappableReference = minSnappableReference,
                offset             = minOffset
            };

            if (minResult.distance <= snapDistance * 1 / m_CurrentScale)
            {
                return(minResult);
            }
            else
            {
                return(null);
            }
        }
Example #3
0
 void ApplyResult(Rect sourceRect, ref Rect r1, SnapResult result)
 {
     if (result.snappableReference <= SnapReference.RightEdge)
     {
         r1.x = sourceRect.x - result.offset;
     }
     else
     {
         r1.y = sourceRect.y - result.offset;
     }
 }
Example #4
0
        SnapResult GetClosestSnapElement(Rect sourceRect, SnapReference sourceRef, SnapReference startReference, SnapReference centerReference, SnapReference endReference)
        {
            SnapResult minResult   = null;
            float      minDistance = float.MaxValue;

            foreach (Rect snappableRect in m_SnappableRects)
            {
                SnapResult result = GetClosestSnapElement(sourceRect, sourceRef, snappableRect, startReference, centerReference, endReference);
                if (result != null && minDistance > result.distance)
                {
                    minDistance = result.distance;
                    minResult   = result;
                }
            }
            return(minResult);
        }