diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 976e5a7..5411cad 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -114,6 +114,7 @@ namespace MLEM.Ui.Elements { public bool CanBeMoused = true; public float DrawAlpha = 1; public bool SetHeightBasedOnChildren; + public bool SetWidthBasedOnChildren; public bool CanAutoAnchorsAttach = true; private RectangleF area; @@ -307,13 +308,24 @@ namespace MLEM.Ui.Elements { foreach (var child in this.Children) child.ForceUpdateArea(); - if (this.SetHeightBasedOnChildren && this.Children.Count > 0) { - var lowest = this.GetLowestChild(e => !e.IsHidden); - if (lowest != null) { - var newHeight = (lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale; - if ((int) newHeight != (int) this.size.Y) { - this.size.Y = newHeight; - this.ForceUpdateArea(); + if (this.Children.Count > 0) { + if (this.SetHeightBasedOnChildren) { + var lowest = this.GetLowestChild(e => !e.IsHidden); + if (lowest != null) { + var newHeight = (lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale; + if ((int) newHeight != (int) this.size.Y) { + this.size.Y = newHeight; + this.ForceUpdateArea(); + } + } + } else if (this.SetWidthBasedOnChildren) { + var rightmost = this.GetRightmostChild(e => !e.IsHidden); + if (rightmost != null) { + var newWidth = (rightmost.UnscrolledArea.Right - pos.X + this.ScaledChildPadding.X) / this.Scale; + if ((int) newWidth != (int) this.size.X) { + this.size.X = newWidth; + this.ForceUpdateArea(); + } } } } @@ -342,6 +354,19 @@ namespace MLEM.Ui.Elements { return lowest; } + public Element GetRightmostChild(Func condition = null) { + Element rightmost = null; + foreach (var child in this.Children) { + if (condition != null && !condition(child)) + continue; + if (child.Anchor < Anchor.BottomRight && child.Anchor != Anchor.TopRight && child.Anchor != Anchor.CenterRight) + continue; + if (rightmost == null || child.UnscrolledArea.Right > rightmost.UnscrolledArea.Right) + rightmost = child; + } + return rightmost; + } + public Element GetLowestOlderSibling(Func condition = null) { if (this.Parent == null) return null; @@ -351,7 +376,7 @@ namespace MLEM.Ui.Elements { break; if (condition != null && !condition(child)) continue; - if (lowest == null || child.UnscrolledArea.Bottom >= lowest.UnscrolledArea.Bottom) + if (lowest == null || child.UnscrolledArea.Bottom > lowest.UnscrolledArea.Bottom) lowest = child; } return lowest; diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 0b5fa61..12d4dc0 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -1,20 +1,11 @@ -using System; -using System.Collections.Generic; -using Coroutine; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; -using MLEM.Cameras; -using MLEM.Extended.Extensions; -using MLEM.Extended.Tiled; using MLEM.Font; -using MLEM.Misc; using MLEM.Startup; using MLEM.Textures; using MLEM.Ui; using MLEM.Ui.Elements; using MLEM.Ui.Style; -using MonoGame.Extended.Tiled; namespace Sandbox { public class GameImpl : MlemGame { @@ -37,32 +28,10 @@ namespace Sandbox { this.UiSystem.AutoScaleWithScreen = true; this.UiSystem.GlobalScale = 5; - var screen = new Panel(Anchor.Center, new Vector2(200, 100), Vector2.Zero, false, true, new Point(5, 10)); - screen.IsHidden = false; - screen.OnUpdated += (element, time) => { - if (this.InputHandler.IsKeyPressed(Keys.Escape)) - CoroutineHandler.Start(PlayAnimation(screen)); - }; - this.UiSystem.Add("Screen", screen); - - for (var i = 0; i < 100; i++) { - screen.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), i.ToString())); - } - } - - private static IEnumerator PlayAnimation(Panel screen) { - var show = screen.IsHidden; - screen.IsHidden = false; - var time = 0; - const float total = 20; - while (time <= total) { - yield return new WaitEvent(CoroutineEvents.Update); - var percent = show ? time / total : 1 - time / total; - screen.Root.Scale = percent; - time++; - } - if (!show) - screen.IsHidden = true; + var panel = new Panel(Anchor.Center, new Vector2(0, 100), Vector2.Zero) {SetWidthBasedOnChildren = true}; + panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(100, 10))); + panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10))); + this.UiSystem.Add("Panel", panel); } protected override void DoDraw(GameTime gameTime) {