mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
added element auto-resizing
This commit is contained in:
parent
adf0ce56cb
commit
cb12ead7cc
4 changed files with 20 additions and 15 deletions
|
@ -14,7 +14,7 @@ namespace MLEM.Ui.Elements {
|
||||||
private Anchor anchor;
|
private Anchor anchor;
|
||||||
private Vector2 size;
|
private Vector2 size;
|
||||||
private Point offset;
|
private Point offset;
|
||||||
private Point padding;
|
public Point Padding;
|
||||||
private Point childPadding;
|
private Point childPadding;
|
||||||
public Anchor Anchor {
|
public Anchor Anchor {
|
||||||
get => this.anchor;
|
get => this.anchor;
|
||||||
|
@ -43,15 +43,6 @@ namespace MLEM.Ui.Elements {
|
||||||
this.SetDirty();
|
this.SetDirty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public Point Padding {
|
|
||||||
get => this.padding;
|
|
||||||
set {
|
|
||||||
if (this.padding == value)
|
|
||||||
return;
|
|
||||||
this.padding = value;
|
|
||||||
this.SetDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public Point ChildPadding {
|
public Point ChildPadding {
|
||||||
get => this.childPadding;
|
get => this.childPadding;
|
||||||
set {
|
set {
|
||||||
|
@ -103,6 +94,7 @@ namespace MLEM.Ui.Elements {
|
||||||
public bool IgnoresMouse;
|
public bool IgnoresMouse;
|
||||||
public float DrawAlpha = 1;
|
public float DrawAlpha = 1;
|
||||||
public bool HasCustomStyle;
|
public bool HasCustomStyle;
|
||||||
|
public bool SetHeightBasedOnChildren;
|
||||||
|
|
||||||
private Rectangle area;
|
private Rectangle area;
|
||||||
public Rectangle Area {
|
public Rectangle Area {
|
||||||
|
@ -269,9 +261,22 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.area = new Rectangle(pos, actualSize);
|
this.area = new Rectangle(pos, actualSize);
|
||||||
|
|
||||||
foreach (var child in this.children)
|
foreach (var child in this.children)
|
||||||
child.ForceUpdateArea();
|
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) {
|
protected virtual Point CalcActualSize(Rectangle parentArea) {
|
||||||
|
|
|
@ -13,7 +13,6 @@ namespace MLEM.Ui.Elements {
|
||||||
public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) {
|
public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) {
|
||||||
this.texture = texture;
|
this.texture = texture;
|
||||||
this.scaleToImage = scaleToImage;
|
this.scaleToImage = scaleToImage;
|
||||||
this.Padding = new Point(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override Point CalcActualSize(Rectangle parentArea) {
|
protected override Point CalcActualSize(Rectangle parentArea) {
|
||||||
|
|
|
@ -8,9 +8,10 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
public NinePatch Texture;
|
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.Texture = texture;
|
||||||
this.PositionOffset = positionOffset;
|
this.PositionOffset = positionOffset;
|
||||||
|
this.SetHeightBasedOnChildren = setHeightBasedOnChildren;
|
||||||
this.ChildPadding = new Point(5);
|
this.ChildPadding = new Point(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,11 +41,11 @@ namespace Tests {
|
||||||
this.UiSystem.Style = style;
|
this.UiSystem.Style = style;
|
||||||
this.UiSystem.GlobalScale = 1.25F;
|
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);
|
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."));
|
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") {
|
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 40), "Test Button") {
|
||||||
OnClicked = (element, button) => {
|
OnClicked = (element, button) => {
|
||||||
if (button == MouseButton.Left)
|
if (button == MouseButton.Left)
|
||||||
|
|
Loading…
Reference in a new issue