diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 3175623..aa85299 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -14,7 +14,7 @@ namespace MLEM.Ui.Elements { private Anchor anchor; private Vector2 size; private Point offset; - private Point padding; + public Point Padding; private Point childPadding; public Anchor Anchor { get => this.anchor; @@ -43,15 +43,6 @@ namespace MLEM.Ui.Elements { this.SetDirty(); } } - public Point Padding { - get => this.padding; - set { - if (this.padding == value) - return; - this.padding = value; - this.SetDirty(); - } - } public Point ChildPadding { get => this.childPadding; set { @@ -103,6 +94,7 @@ namespace MLEM.Ui.Elements { public bool IgnoresMouse; public float DrawAlpha = 1; public bool HasCustomStyle; + public bool SetHeightBasedOnChildren; private Rectangle area; public Rectangle Area { @@ -269,9 +261,22 @@ namespace MLEM.Ui.Elements { } this.area = new Rectangle(pos, actualSize); - foreach (var child in this.children) child.ForceUpdateArea(); + + if (this.SetHeightBasedOnChildren) { + 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.ChildPadding.Y; + if (newHeight != this.size.Y) { + this.size.Y = newHeight; + this.ForceUpdateArea(); + } + } } protected virtual Point CalcActualSize(Rectangle parentArea) { diff --git a/MLEM.Ui/Elements/Image.cs b/MLEM.Ui/Elements/Image.cs index 72a3506..9c0ef5e 100644 --- a/MLEM.Ui/Elements/Image.cs +++ b/MLEM.Ui/Elements/Image.cs @@ -13,7 +13,6 @@ namespace MLEM.Ui.Elements { public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) { this.texture = texture; this.scaleToImage = scaleToImage; - this.Padding = new Point(1); } protected override Point CalcActualSize(Rectangle parentArea) { diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 3cd5dff..04e010f 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -8,9 +8,10 @@ namespace MLEM.Ui.Elements { public NinePatch Texture; - public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture = null) : base(anchor, size) { + public Panel(Anchor anchor, Vector2 size, Point positionOffset, bool setHeightBasedOnChildren = false, NinePatch texture = null) : base(anchor, size) { this.Texture = texture; this.PositionOffset = positionOffset; + this.SetHeightBasedOnChildren = setHeightBasedOnChildren; this.ChildPadding = new Point(5); } diff --git a/Tests/GameImpl.cs b/Tests/GameImpl.cs index 2ce4366..1b61243 100644 --- a/Tests/GameImpl.cs +++ b/Tests/GameImpl.cs @@ -41,11 +41,11 @@ namespace Tests { this.UiSystem.Style = style; this.UiSystem.GlobalScale = 1.25F; - var root = new Panel(Anchor.Center, new Vector2(300, 450), Point.Zero); + var root = new Panel(Anchor.BottomLeft, new Vector2(300, 450), Point.Zero, true); this.UiSystem.Add("Test", root); root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a test text that is hopefully long enough to cover at least a few lines, otherwise it would be very sad.")); - var image = root.AddChild(new Image(Anchor.AutoCenter, new Vector2(50, 50), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true}); + var image = root.AddChild(new Image(Anchor.AutoCenter, new Vector2(70, 70), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true, Padding = new Point(10)}); root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 40), "Test Button") { OnClicked = (element, button) => { if (button == MouseButton.Left)