1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

added SetWidthBasedOnChildren

This commit is contained in:
Ellpeck 2019-11-05 21:44:51 +01:00
parent a5f6e83321
commit 6340f75acc
2 changed files with 37 additions and 43 deletions

View file

@ -114,6 +114,7 @@ namespace MLEM.Ui.Elements {
public bool CanBeMoused = true; public bool CanBeMoused = true;
public float DrawAlpha = 1; public float DrawAlpha = 1;
public bool SetHeightBasedOnChildren; public bool SetHeightBasedOnChildren;
public bool SetWidthBasedOnChildren;
public bool CanAutoAnchorsAttach = true; public bool CanAutoAnchorsAttach = true;
private RectangleF area; private RectangleF area;
@ -307,13 +308,24 @@ namespace MLEM.Ui.Elements {
foreach (var child in this.Children) foreach (var child in this.Children)
child.ForceUpdateArea(); child.ForceUpdateArea();
if (this.SetHeightBasedOnChildren && this.Children.Count > 0) { if (this.Children.Count > 0) {
var lowest = this.GetLowestChild(e => !e.IsHidden); if (this.SetHeightBasedOnChildren) {
if (lowest != null) { var lowest = this.GetLowestChild(e => !e.IsHidden);
var newHeight = (lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale; if (lowest != null) {
if ((int) newHeight != (int) this.size.Y) { var newHeight = (lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Y) / this.Scale;
this.size.Y = newHeight; if ((int) newHeight != (int) this.size.Y) {
this.ForceUpdateArea(); 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; return lowest;
} }
public Element GetRightmostChild(Func<Element, bool> 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<Element, bool> condition = null) { public Element GetLowestOlderSibling(Func<Element, bool> condition = null) {
if (this.Parent == null) if (this.Parent == null)
return null; return null;
@ -351,7 +376,7 @@ namespace MLEM.Ui.Elements {
break; break;
if (condition != null && !condition(child)) if (condition != null && !condition(child))
continue; continue;
if (lowest == null || child.UnscrolledArea.Bottom >= lowest.UnscrolledArea.Bottom) if (lowest == null || child.UnscrolledArea.Bottom > lowest.UnscrolledArea.Bottom)
lowest = child; lowest = child;
} }
return lowest; return lowest;

View file

@ -1,20 +1,11 @@
using System;
using System.Collections.Generic;
using Coroutine;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; 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.Font;
using MLEM.Misc;
using MLEM.Startup; using MLEM.Startup;
using MLEM.Textures; using MLEM.Textures;
using MLEM.Ui; using MLEM.Ui;
using MLEM.Ui.Elements; using MLEM.Ui.Elements;
using MLEM.Ui.Style; using MLEM.Ui.Style;
using MonoGame.Extended.Tiled;
namespace Sandbox { namespace Sandbox {
public class GameImpl : MlemGame { public class GameImpl : MlemGame {
@ -37,32 +28,10 @@ namespace Sandbox {
this.UiSystem.AutoScaleWithScreen = true; this.UiSystem.AutoScaleWithScreen = true;
this.UiSystem.GlobalScale = 5; this.UiSystem.GlobalScale = 5;
var screen = new Panel(Anchor.Center, new Vector2(200, 100), Vector2.Zero, false, true, new Point(5, 10)); var panel = new Panel(Anchor.Center, new Vector2(0, 100), Vector2.Zero) {SetWidthBasedOnChildren = true};
screen.IsHidden = false; panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(100, 10)));
screen.OnUpdated += (element, time) => { panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10)));
if (this.InputHandler.IsKeyPressed(Keys.Escape)) this.UiSystem.Add("Panel", panel);
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<Wait> 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;
} }
protected override void DoDraw(GameTime gameTime) { protected override void DoDraw(GameTime gameTime) {