From d231386f861de98b03d30cc5ad29bfbcc5b8fa65 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 24 Aug 2019 15:12:11 +0200 Subject: [PATCH] made auto-anchored elements look for the lowest older sibling --- Demos/UiDemo.cs | 2 +- MLEM.Ui/Elements/Element.cs | 21 ++++++++++++--------- MLEM.Ui/Elements/ElementHelper.cs | 8 +++----- MLEM.Ui/Elements/Panel.cs | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 3a3fa11..190628f 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -160,7 +160,7 @@ namespace Demos { root.AddChild(ElementHelper.NumberField(Anchor.AutoLeft, new Vector2(1, 10))).PositionOffset = new Vector2(0, 1); root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "There is an easy helper method to make any amount of same-sized columns:") {PositionOffset = new Vector2(0, 1)}); - var cols = ElementHelper.MakeColumns(root, Anchor.AutoLeft, new Vector2(1), 3); + var cols = ElementHelper.MakeColumns(root, new Vector2(1), 3); cols[0].AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is the first column")); cols[1].AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is the second column")); cols[2].AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is the third column")); diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 9f96c72..11ffd41 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -263,7 +263,7 @@ namespace MLEM.Ui.Elements { } if (this.Anchor >= Anchor.AutoLeft) { - var previousChild = this.GetPreviousChild(false, false); + var previousChild = this.GetLowestOlderSibling(false, false); if (previousChild != null) { var prevArea = previousChild.GetAreaForAutoAnchors(); switch (this.Anchor) { @@ -296,7 +296,7 @@ namespace MLEM.Ui.Elements { child.ForceUpdateArea(); if (this.SetHeightBasedOnChildren && this.Children.Count > 0) { - var lowest = this.GetLowestReachingChild(false, true); + var lowest = this.GetLowestChild(false, true); var newHeight = (lowest.area.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale; if (newHeight != this.size.Y) { this.size.Y = newHeight; @@ -316,7 +316,7 @@ namespace MLEM.Ui.Elements { return this.Area; } - public Element GetLowestReachingChild(bool hiddenAlso, bool unattachableAlso) { + public Element GetLowestChild(bool hiddenAlso, bool unattachableAlso) { Element lowest = null; // the lowest child is expected to be towards the back, so search is usually faster if done backwards for (var i = this.Children.Count - 1; i >= 0; i--) { @@ -331,19 +331,22 @@ namespace MLEM.Ui.Elements { return lowest; } - public Element GetPreviousChild(bool hiddenAlso, bool unattachableAlso) { + public Element GetLowestOlderSibling(bool hiddenAlso, bool unattachableAlso) { if (this.Parent == null) return null; - Element lastChild = null; + Element lowest = null; foreach (var child in this.Parent.Children) { - if (!hiddenAlso && child.IsHidden || !unattachableAlso && !child.CanAutoAnchorsAttach) - continue; if (child == this) break; - lastChild = child; + if (!hiddenAlso && child.IsHidden || !unattachableAlso && !child.CanAutoAnchorsAttach) + continue; + if (child.Anchor > Anchor.TopRight && child.Anchor < Anchor.AutoLeft) + continue; + if (lowest == null || child.Area.Bottom > lowest.Area.Bottom) + lowest = child; } - return lastChild; + return lowest; } public IEnumerable GetSiblings(bool hiddenAlso) { diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index 7963e64..2559d84 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -18,15 +18,13 @@ namespace MLEM.Ui.Elements { return box; } - public static Group[] MakeColumns(Element parent, Anchor anchor, Vector2 totalSize, int amount, bool setHeightBasedOnChildren = true) { - var group = new Group(anchor, totalSize, setHeightBasedOnChildren); + public static Group[] MakeColumns(Element parent, Vector2 totalSize, int amount, bool setHeightBasedOnChildren = true) { var cols = new Group[amount]; for (var i = 0; i < amount; i++) { cols[i] = new Group(Anchor.AutoInline, new Vector2(totalSize.X / amount, totalSize.Y), setHeightBasedOnChildren); - group.AddChild(cols[i]); + if (parent != null) + parent.AddChild(cols[i]); } - if (parent != null) - parent.AddChild(group); return cols; } diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 7349c34..3ef523c 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -65,7 +65,7 @@ namespace MLEM.Ui.Elements { return; // the "real" first child is the scroll bar, which we want to ignore var firstChild = this.Children[1]; - var lowestChild = this.GetLowestReachingChild(false, true); + var lowestChild = this.GetLowestChild(false, true); // the max value of the scrollbar is the amount of non-scaled pixels taken up by overflowing components var childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top; this.ScrollBar.MaxValue = (childrenHeight - this.Area.Height) / this.Scale + this.ChildPadding.Y * 2;