diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c2d0b1..ef81dff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,9 @@ Additions - Added the ability to set the anchor that should be used when a tooltip attaches to an element or the mouse - Added the ability to display tooltips using the auto-nav style even when using the mouse +Improvements +- Allow scrolling panels to set height based on children with TreatSizeAsMaximum + ## 6.3.1 No code changes diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 934d010..c401601 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -223,14 +223,16 @@ namespace Demos { PositionOffset = new Vector2(0, 1) }); - var subPanel = this.root.AddChild(new Panel(Anchor.AutoLeft, new Vector2(1, 25), Vector2.Zero, false, true) { - PositionOffset = new Vector2(0, 1), + this.root.AddChild(new VerticalSpace(3)); + var dynamicSubPanel = this.root.AddChild(new Panel(Anchor.AutoLeft, new Vector2(1, 50), Vector2.Zero, true, true) { Texture = null, - ChildPadding = Padding.Empty + ChildPadding = Padding.Empty, + TreatSizeAsMaximum = true + }); + dynamicSubPanel.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a dynamic height nested panel with a maximum height, at which point it starts displaying a scroll bar instead!")); + dynamicSubPanel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Press to add more") { + OnPressed = _ => dynamicSubPanel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "I do nothing")) }); - subPanel.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a nested scrolling panel!")); - for (var i = 1; i <= 5; i++) - subPanel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), $"Button {i}") {PositionOffset = new Vector2(0, 1)}); const string alignText = "Paragraphs can have left aligned text, right aligned text and center aligned text."; this.root.AddChild(new VerticalSpace(3)); diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 2f1dc9d..2da13d3 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -119,8 +119,8 @@ namespace MLEM.Ui.Elements { public override void ForceUpdateArea() { if (this.scrollOverflow) { // sanity check - if (this.SetHeightBasedOnChildren) - throw new NotSupportedException("A panel can't both set height based on children and scroll overflow"); + if (this.SetHeightBasedOnChildren && !this.TreatSizeAsMaximum) + throw new NotSupportedException("A panel can't both scroll overflow and set height based on children without a maximum"); foreach (var child in this.Children) { if (child != this.ScrollBar && !child.Anchor.IsAuto()) throw new NotSupportedException($"A panel that handles overflow can't contain non-automatic anchors ({child})"); @@ -295,7 +295,7 @@ namespace MLEM.Ui.Elements { if (this.Children.Count > 1) { var highestValidChild = this.Children.FirstOrDefault(c => c != this.ScrollBar && !c.IsHidden); var lowestChild = this.GetLowestChild(c => c != this.ScrollBar && !c.IsHidden, true); - childrenHeight = lowestChild.GetTotalCoveredArea(false).Bottom - highestValidChild.Area.Top; + childrenHeight = lowestChild.GetTotalCoveredArea(true).Bottom - highestValidChild.UnscrolledArea.Top; } else { // if we only have one child (the scroll bar), then the children take up no visual height childrenHeight = 0;