public override void Layout(LayoutEventArgs layoutEventArgs) { GuiWidget parent = layoutEventArgs.ParentWidget; if (parent != null) { parent.SuspendLayout(); // if we didn't specify a chiled than achor all the children if (layoutEventArgs.ChildWidget == null) { foreach (GuiWidget child in parent.Children) { if (child.Visible == false) { continue; } ApplyHAnchorToChild(parent, child); ApplyVAnchorToChild(parent, child); } } else { ApplyHAnchorToChild(parent, layoutEventArgs.ChildWidget); ApplyVAnchorToChild(parent, layoutEventArgs.ChildWidget); } // make sure we fit to the children after anchoring bool parentChangedSize = false; DoFitToChildrenHorizontal(parent, ref parentChangedSize); DoFitToChildrenVertical(parent, ref parentChangedSize); // if we changed size again, than try one more time to anchor the children if (parentChangedSize) { foreach (GuiWidget child in parent.Children) { if (child.Visible == false) { continue; } ApplyHAnchorToChild(parent, child); ApplyVAnchorToChild(parent, child); } } parent.ResumeLayout(); } }
public override void Layout(LayoutEventArgs layoutEventArgs) { GuiWidget parent = layoutEventArgs.ParentWidget; if (parent != null) { parent.SuspendLayout(); for (int i = 0; i < parent.Children.Count; i++) { GuiWidget child = parent.Children[i]; if (child.Visible == false) { continue; } ApplyHAnchorToChild(parent, child); ApplyVAnchorToChild(parent, child); } DoLayoutChildren(layoutEventArgs); FixOriginXIfRightToLeft(parent); FixOriginYIfTopToBottom(parent); bool parentChangedSize = false; DoFitToChildrenHorizontal(parent, ref parentChangedSize); DoFitToChildrenVertical(parent, ref parentChangedSize); if (parentChangedSize) { foreach (GuiWidget child in parent.Children) { if (child.Visible == false) { continue; } ApplyHAnchorToChild(parent, child); ApplyVAnchorToChild(parent, child); } DoLayoutChildren(layoutEventArgs); } parent.ResumeLayout(); } }
public abstract void Layout(LayoutEventArgs layoutEventArgs);
public virtual void OnLayout(LayoutEventArgs layoutEventArgs) { #if DEBUG && DUMP_SLOW_TIMES using (new DumpCallStackIfSlow(dumpIfLongerThanTime, "OnLayout")) #endif { if (Visible && layoutSuspendCount < 1) { if (LayoutEngine != null) { SuspendLayout(); LayoutEngine.Layout(layoutEventArgs); ResumeLayout(); } if (Layout != null) { Layout(this, layoutEventArgs); } } } }
private void DoLayoutChildren(LayoutEventArgs layoutEventArgs) { GuiWidget parent = layoutEventArgs.ParentWidget; if (parent.CountVisibleChildren() == 0) { return; } RectangleDouble boundsOfAllChildrenIncludingMargin = RectangleDouble.ZeroIntersection; double totalWidthWithMargin = 0; double totalHeightWithMargin = 0; double totalWidthOfStaticItems = 0; double totalHeightOfStaticItems = 0; int numItemsNeedingExpanding = 0; double totalMinimumWidthOfAllItems = 0; double totalMinimumHeightOfAllItems = 0; for (int childIndex = 0; childIndex < parent.Children.Count; childIndex++) { GuiWidget child = parent.Children[childIndex]; if (child.Visible == false) { continue; } RectangleDouble childBoundsWithMargin = child.LocalBounds; childBoundsWithMargin.Inflate(child.Margin); totalWidthWithMargin += childBoundsWithMargin.Width; totalHeightWithMargin += childBoundsWithMargin.Height; boundsOfAllChildrenIncludingMargin.ExpandToInclude(childBoundsWithMargin); switch (FlowDirection) { case UI.FlowDirection.LeftToRight: case UI.FlowDirection.RightToLeft: totalMinimumWidthOfAllItems += child.MinimumSize.x + child.Margin.Width; totalMinimumHeightOfAllItems = Math.Max(totalMinimumHeightOfAllItems, child.MinimumSize.y + child.Margin.Height); if (child.HAnchorIsSet(HAnchor.ParentLeftRight)) { numItemsNeedingExpanding++; totalWidthOfStaticItems += child.Margin.Width; } else if (child.HAnchor == HAnchor.None || child.HAnchorIsSet(HAnchor.FitToChildren)) { totalWidthOfStaticItems += childBoundsWithMargin.Width; } else { throw new Exception("Only None or ParentLeftRight are valid HAnchor for a horizontal flowWidget."); } break; case UI.FlowDirection.TopToBottom: case UI.FlowDirection.BottomToTop: totalMinimumWidthOfAllItems = Math.Max(totalMinimumWidthOfAllItems, child.MinimumSize.x + child.Margin.Width); totalMinimumHeightOfAllItems += child.MinimumSize.y + child.Margin.Height; if (child.VAnchorIsSet(VAnchor.ParentBottomTop)) { numItemsNeedingExpanding++; totalHeightOfStaticItems += child.Margin.Height; } else if (child.VAnchor == VAnchor.None || child.VAnchorIsSet(VAnchor.FitToChildren)) { totalHeightOfStaticItems += childBoundsWithMargin.Height; } else { throw new Exception("Only None or ParentBottomTop are valid VAnchor for a vertial flowWidget."); } break; default: throw new NotImplementedException(); } } switch (FlowDirection) { case UI.FlowDirection.LeftToRight: { double curX = parent.Padding.Left; foreach (GuiWidget child in parent.Children) { if (child.Visible == true) { double newX = curX - child.LocalBounds.Left + child.Margin.Left; child.OriginRelativeParent = new Vector2(newX, child.OriginRelativeParent.y); if (child.HAnchorIsSet(HAnchor.ParentLeftRight)) { RectangleDouble curChildBounds = child.LocalBounds; double newWidth = (parent.LocalBounds.Width - parent.Padding.Width - totalWidthOfStaticItems) / numItemsNeedingExpanding; child.LocalBounds = new RectangleDouble(curChildBounds.Left, curChildBounds.Bottom, newWidth, curChildBounds.Top); } curX += child.LocalBounds.Width + child.Margin.Width; } } } break; case UI.FlowDirection.RightToLeft: { double curX = parent.LocalBounds.Right - parent.Padding.Right; foreach (GuiWidget child in parent.Children) { if (child.Visible == true) { if (child.HAnchorIsSet(HAnchor.ParentLeftRight)) { RectangleDouble curChildBounds = child.LocalBounds; double newWidth = (parent.LocalBounds.Width - parent.Padding.Width - totalWidthOfStaticItems) / numItemsNeedingExpanding; child.LocalBounds = new RectangleDouble(curChildBounds.Left, curChildBounds.Bottom, newWidth, curChildBounds.Top); } double newX = curX - child.LocalBounds.Left - (child.LocalBounds.Width + child.Margin.Right); child.OriginRelativeParent = new Vector2(newX, child.OriginRelativeParent.y); curX -= (child.LocalBounds.Width + child.Margin.Width); } } } break; case UI.FlowDirection.BottomToTop: { double curY = parent.Padding.Bottom; foreach (GuiWidget child in parent.Children) { if (child.Visible == true) { double newY = curY - child.LocalBounds.Bottom + child.Margin.Bottom; child.OriginRelativeParent = new Vector2(child.OriginRelativeParent.x, newY); if (child.VAnchorIsSet(VAnchor.ParentBottomTop)) { RectangleDouble curChildBounds = child.LocalBounds; double newHeight = (parent.LocalBounds.Height - parent.Padding.Height - totalHeightOfStaticItems) / numItemsNeedingExpanding; child.LocalBounds = new RectangleDouble(curChildBounds.Left, curChildBounds.Bottom, curChildBounds.Right, newHeight); } curY += child.LocalBounds.Height + child.Margin.Height; } } } break; case UI.FlowDirection.TopToBottom: { double curY = parent.LocalBounds.Top - parent.Padding.Top; foreach (GuiWidget child in parent.Children) { if (child.Visible == true) { if (child.VAnchorIsSet(VAnchor.ParentBottomTop)) { RectangleDouble curChildBounds = child.LocalBounds; double newHeight = (parent.LocalBounds.Height - parent.Padding.Height - totalHeightOfStaticItems) / numItemsNeedingExpanding; child.LocalBounds = new RectangleDouble(curChildBounds.Left, curChildBounds.Bottom, curChildBounds.Right, newHeight); } double newY = curY - child.LocalBounds.Bottom - (child.LocalBounds.Height + child.Margin.Top); child.OriginRelativeParent = new Vector2(child.OriginRelativeParent.x, newY); curY -= (child.LocalBounds.Height + child.Margin.Height); } } } break; default: throw new NotImplementedException(); } }
public abstract void Layout(LayoutEventArgs layoutEventArgs);