1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-07 15:23:37 +02: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 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<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) {
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;

View file

@ -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<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;
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) {