/// <summary> /// Gets the smallest rectangle that bounds all of the items</summary> /// <param name="layoutContext">Layout context</param> /// <param name="items">Items</param> /// <param name="bounds">Bounding rectangle, in world coordinates (output parameter)</param> /// <returns>Rectangle that bounds all the items</returns> public static BoundsSpecified GetBounds(this ILayoutContext layoutContext, IEnumerable <object> items, out Rectangle bounds) { BoundsSpecified resultFlags = BoundsSpecified.None; Rectangle resultBounds = new Rectangle(); bool firstTime = true; foreach (object item in items) { Rectangle itemBounds; BoundsSpecified itemFlags = layoutContext.GetBounds(item, out itemBounds); if (itemFlags == BoundsSpecified.All) { if (firstTime) { firstTime = false; resultBounds = itemBounds; resultFlags = itemFlags; } else { resultBounds = Rectangle.Union(resultBounds, itemBounds); resultFlags &= itemFlags; } } } bounds = resultBounds; return(resultFlags); }
/// <summary> /// Gets the smallest rectangle that bounds all of the items</summary> /// <param name="layoutContext">Layout context</param> /// <param name="items">Items</param> /// <param name="bounds">Bounding rectangle (output parameter)</param> /// <returns>Rectangle that bounds all of the items</returns> public static BoundsSpecified GetBounds(this ILayoutContext layoutContext, IEnumerable <object> items, out Rect bounds) { BoundsSpecified resultFlags = BoundsSpecified.None; Rect resultBounds = Rect.Empty; foreach (object item in items) { Rect itemBounds; BoundsSpecified itemFlags = layoutContext.GetBounds(item, out itemBounds); if (itemFlags == BoundsSpecified.All) { if (resultBounds.IsEmpty) { resultBounds = itemBounds; resultFlags = itemFlags; } else { resultBounds = Rect.Union(resultBounds, itemBounds); resultFlags &= itemFlags; } } } bounds = resultBounds; return(resultFlags); }
/// <summary> /// Moves the item, so that the center of its bounding box is at the given point</summary> /// <param name="layoutContext">Layout context containing item</param> /// <param name="item">Item to center</param> /// <param name="center">Centering point</param> public static void Center(this ILayoutContext layoutContext, object item, Point center) { Rect oldBounds; layoutContext.GetBounds(item, out oldBounds); Point topLeft = new Point( center.X - oldBounds.Width / 2, center.Y - oldBounds.Height / 2); layoutContext.SetBounds(item, oldBounds, new Rect(topLeft, oldBounds.Size), BoundsSpecified.Location); }
/// <summary> /// Sets the height of all of the items to that of the tallest item</summary> /// <param name="items">Items to resize</param> /// <param name="layoutContext">Layout context of items to resize</param> public virtual void MakeHeightEqual(IEnumerable <object> items, ILayoutContext layoutContext) { Size maxSize = GetMaxSize(items, layoutContext); foreach (object item in items) { Rectangle bounds; layoutContext.GetBounds(item, out bounds); bounds.Height = maxSize.Height; layoutContext.SetBounds(item, bounds, BoundsSpecified.Height); } }
/// <summary> /// Moves the items so that their bounding boxes all have the Left (x) value /// of the left-most item</summary> /// <param name="items">Items to move</param> /// <param name="layoutContext">Layout context of items to move</param> public virtual void AlignLefts(IEnumerable<object> items, ILayoutContext layoutContext) { Rectangle bounds; LayoutContexts.GetBounds(layoutContext, items, out bounds); foreach (object item in items) { Rectangle itemBounds; layoutContext.GetBounds(item, out itemBounds); itemBounds.X = bounds.X; layoutContext.SetBounds(item, itemBounds, BoundsSpecified.X); } }
/// <summary> /// Moves the items so that their bounding boxes all have the Right (x + Width) value /// of the right-most item</summary> /// <param name="items">Items to move</param> /// <param name="layoutContext">Layout context of items to move</param> public virtual void AlignRights(IEnumerable <object> items, ILayoutContext layoutContext) { Rectangle bounds; LayoutContexts.GetBounds(layoutContext, items, out bounds); foreach (object item in items) { Rectangle itemBounds; layoutContext.GetBounds(item, out itemBounds); itemBounds.X = bounds.Right - itemBounds.Width; layoutContext.SetBounds(item, itemBounds, BoundsSpecified.X); } }
/// <summary> /// Moves the items so that their bounding boxes all have the Bottom (y + Height) value /// of the bottom-most item</summary> /// <param name="items">Items to move</param> /// <param name="layoutContext">Layout context of items to move</param> public virtual void AlignBottoms(IEnumerable <object> items, ILayoutContext layoutContext) { Rectangle bounds; LayoutContexts.GetBounds(layoutContext, items, out bounds); foreach (object item in items) { Rectangle itemBounds; layoutContext.GetBounds(item, out itemBounds); itemBounds.Y = bounds.Bottom - itemBounds.Height; layoutContext.SetBounds(item, itemBounds, BoundsSpecified.Y); } }
private Size GetMaxSize(IEnumerable <object> items, ILayoutContext layoutContext) { Size size = new Size(); foreach (object item in items) { Rectangle bounds; layoutContext.GetBounds(item, out bounds); size = new Size( Math.Max(size.Width, bounds.Width), Math.Max(size.Height, bounds.Height)); } return(size); }
/// <summary> /// Moves all items so that the center of their bounding box is at the given point</summary> /// <param name="layoutContext">Layout context containing item</param> /// <param name="items">Items to center</param> /// <param name="center">Centering point</param> public static void Center(this ILayoutContext layoutContext, IEnumerable <object> items, Point center) { Rect bounds; GetBounds(layoutContext, items, out bounds); // calculate offset Point offset = new Point( center.X - (bounds.Left + bounds.Width / 2), center.Y - (bounds.Top + bounds.Height / 2)); foreach (object item in items) { Rect itemBounds; layoutContext.GetBounds(item, out itemBounds); Point topLeft = new Point( itemBounds.Left + offset.X, itemBounds.Top + offset.Y); layoutContext.SetBounds(item, itemBounds, new Rect(topLeft, itemBounds.Size), BoundsSpecified.Location); } }
private Size GetMaxSize(IEnumerable<object> items, ILayoutContext layoutContext) { Size size = new Size(); foreach (object item in items) { Rectangle bounds; layoutContext.GetBounds(item, out bounds); size = new Size( Math.Max(size.Width, bounds.Width), Math.Max(size.Height, bounds.Height)); } return size; }
/// <summary> /// Sets the height of all of the items to that of the tallest item</summary> /// <param name="items">Items to resize</param> /// <param name="layoutContext">Layout context of items to resize</param> public virtual void MakeHeightEqual(IEnumerable<object> items, ILayoutContext layoutContext) { Size maxSize = GetMaxSize(items, layoutContext); foreach (object item in items) { Rectangle bounds; layoutContext.GetBounds(item, out bounds); bounds.Height = maxSize.Height; layoutContext.SetBounds(item, bounds, BoundsSpecified.Height); } }
/// <summary> /// Moves the items up or down so that their bounding box centers all have the same /// Y value, which is the Y value of the center of the bounding box around the items' /// original positions</summary> /// <param name="items">Items to move</param> /// <param name="layoutContext">Layout context of items to move</param> public virtual void AlignMiddles(IEnumerable<object> items, ILayoutContext layoutContext) { Rectangle bounds; LayoutContexts.GetBounds(layoutContext, items, out bounds); int boundsMiddle = (bounds.Top + bounds.Bottom) / 2; foreach (object item in items) { Rectangle itemBounds; layoutContext.GetBounds(item, out itemBounds); itemBounds.Y = boundsMiddle - itemBounds.Height / 2; layoutContext.SetBounds(item, itemBounds, BoundsSpecified.Y); } }
/// <summary> /// Moves the items left or right so that their bounding box centers all have the same /// X value, which is the X value of the center of the bounding box around the items' /// original positions</summary> /// <param name="items">Items to move</param> /// <param name="layoutContext">Layout context of items to move</param> public virtual void AlignCenters(IEnumerable<object> items, ILayoutContext layoutContext) { Rectangle bounds; LayoutContexts.GetBounds(layoutContext, items, out bounds); int boundsCenter = (bounds.Left + bounds.Right) / 2; foreach (object item in items) { Rectangle itemBounds; layoutContext.GetBounds(item, out itemBounds); itemBounds.X = boundsCenter - itemBounds.Width / 2; layoutContext.SetBounds(item, itemBounds, BoundsSpecified.X); } }