using System; using Gwen.Control; namespace Gwen { /// /// Utility class for manipulating control's position according to its parent. Rarely needed, use control.Dock. /// public static class Align { /// /// Centers the control inside its parent. /// /// Control to center. public static void Center(Base control) { Base parent = control.Parent; if (parent == null) return; control.SetPosition( parent.Padding.Left + (((parent.Width - parent.Padding.Left - parent.Padding.Right) - control.Width)/2), (parent.Height - control.Height)/2); } /// /// Moves the control to the left of its parent. /// /// public static void AlignLeft(Base control) { Base parent = control.Parent; if (null == parent) return; control.SetPosition(parent.Padding.Left, control.Y); } /// /// Centers the control horizontally inside its parent. /// /// public static void CenterHorizontally(Base control) { Base parent = control.Parent; if (null == parent) return; control.SetPosition(parent.Padding.Left + (((parent.Width - parent.Padding.Left - parent.Padding.Right) - control.Width) / 2), control.Y); } /// /// Moves the control to the right of its parent. /// /// public static void AlignRight(Base control) { Base parent = control.Parent; if (null == parent) return; control.SetPosition(parent.Width - control.Width - parent.Padding.Right, control.Y); } /// /// Moves the control to the top of its parent. /// /// public static void AlignTop(Base control) { control.SetPosition(control.X, 0); } /// /// Centers the control vertically inside its parent. /// /// public static void CenterVertically(Base control) { Base parent = control.Parent; if (null == parent) return; control.SetPosition(control.X, (parent.Height - control.Height) / 2); } /// /// Moves the control to the bottom of its parent. /// /// public static void AlignBottom(Base control) { Base parent = control.Parent; if (null == parent) return; control.SetPosition(control.X, parent.Height - control.Height); } /// /// Places the control below other control (left aligned), taking margins into account. /// /// Control to place. /// Anchor control. /// Optional spacing. public static void PlaceDownLeft(Base control, Base anchor, int spacing = 0) { control.SetPosition(anchor.X, anchor.Bottom + spacing); } /// /// Places the control to the right of other control (bottom aligned), taking margins into account. /// /// Control to place. /// Anchor control. /// Optional spacing. public static void PlaceRightBottom(Base control, Base anchor, int spacing = 0) { control.SetPosition(anchor.Right + spacing, anchor.Y - control.Height + anchor.Height); } } }