1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 01:39:23 +01:00

added element auto-resizing

This commit is contained in:
Ellpeck 2019-08-11 18:50:39 +02:00
parent adf0ce56cb
commit cb12ead7cc
4 changed files with 20 additions and 15 deletions

View file

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

View file

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

View file

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

View file

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