From d69033a3150ef75aa4216a69724dd11f7adee492 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 24 Aug 2019 14:34:08 +0200 Subject: [PATCH] fixed some bugs and added column helper --- Demos/UiDemo.cs | 6 ++++++ MLEM.Ui/Elements/Element.cs | 24 +++++++++++++++++------- MLEM.Ui/Elements/ElementHelper.cs | 10 ++++++++++ MLEM.Ui/Elements/Panel.cs | 4 ++-- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index fbfa64f..b6de705 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -159,6 +159,12 @@ namespace Demos { root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "There are also some additional \"components\" which are created as combinations of other components. You can find all of them in the ElementHelper class. Here are some examples:")); 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, 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")); + // Below are some querying examples that help you find certain elements easily var children = root.GetChildren(); diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 007e0de..859ce6c 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -296,13 +296,8 @@ namespace MLEM.Ui.Elements { child.ForceUpdateArea(); if (this.SetHeightBasedOnChildren && this.Children.Count > 0) { - var height = 0; - foreach (var child in this.Children) { - if (!child.isHidden && (child.Anchor <= Anchor.TopRight || child.Anchor >= Anchor.AutoLeft) && child.area.Bottom > height) - height = child.area.Bottom; - } - - var newHeight = (height - pos.Y + this.ScaledChildPadding.Y) / this.Scale; + var lowest = this.GetLowestReachingChild(); + var newHeight = (lowest.area.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale; if (newHeight != this.size.Y) { this.size.Y = newHeight; if (this.Anchor > Anchor.TopRight) @@ -321,6 +316,21 @@ namespace MLEM.Ui.Elements { return this.Area; } + public Element GetLowestReachingChild() { + 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--) { + var child = this.Children[i]; + if (child.isHidden) + continue; + if (child.Anchor > Anchor.TopRight && child.Anchor < Anchor.AutoLeft) + continue; + if (lowest == null || child.Area.Bottom > lowest.Area.Bottom) + lowest = child; + } + return lowest; + } + public Element GetPreviousChild(bool hiddenAlso, bool unattachableAlso) { if (this.Parent == null) return null; diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index d2d8e83..fe5ba96 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -4,6 +4,16 @@ using MLEM.Input; namespace MLEM.Ui.Elements { public static class ElementHelper { + 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); + if (parent != null) + parent.AddChild(cols[i]); + } + return cols; + } + public static Group NumberField(Anchor anchor, Vector2 size, int defaultValue = 0, int stepPerClick = 1, TextField.Rule rule = null, TextField.TextChanged onTextChange = null) { var group = new Group(anchor, size, false); diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 79ea216..a687671 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -65,9 +65,9 @@ namespace MLEM.Ui.Elements { return; // the "real" first child is the scroll bar, which we want to ignore var firstChild = this.Children[1]; - var lastChild = this.Children[this.Children.Count - 1]; + var lowestChild = this.GetLowestReachingChild(); // the max value of the scrollbar is the amount of non-scaled pixels taken up by overflowing components - var childrenHeight = lastChild.Area.Bottom - firstChild.Area.Top; + var childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top; this.ScrollBar.MaxValue = (childrenHeight - this.Area.Height) / this.Scale + this.ChildPadding.Y * 2; // update the render target