1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-23 05:08:34 +01:00

added an option to limit auto-heights in elements

This commit is contained in:
Ell 2021-03-24 01:25:39 +01:00
parent 3d314172d0
commit dcee3c5010

View file

@ -254,13 +254,21 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public bool CanAutoAnchorsAttach = true; public bool CanAutoAnchorsAttach = true;
/// <summary> /// <summary>
/// Set this field to true to cause this element's width to be automatically calculated based on the area that its <see cref="Children"/> take up.
/// To use this element's <see cref="Size"/>'s X coordinate as a minimum width rather than ignoring it, set <see cref="TreatSizeAsMinimum"/> to true.
/// </summary>
public bool SetWidthBasedOnChildren;
/// <summary>
/// Set this field to true to cause this element's height to be automatically calculated based on the area that its <see cref="Children"/> take up. /// Set this field to true to cause this element's height to be automatically calculated based on the area that its <see cref="Children"/> take up.
/// To use this element's <see cref="Size"/>'s Y coordinate as a minimum height rather than ignoring it, set <see cref="TreatSizeAsMinimum"/> to true.
/// </summary> /// </summary>
public bool SetHeightBasedOnChildren; public bool SetHeightBasedOnChildren;
/// <summary> /// <summary>
/// Set this field to true to cause this element's width to be automatically calculated based on the area that is <see cref="Children"/> take up. /// If this field is set to true, and <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled, the resulting width or height will always be greather than or equal to this element's <see cref="Size"/>.
/// For example, if an element's <see cref="Size"/>'s Y coordinate is set to 20, but there is only one child with a height of 10 in it, the element's height would be shrunk to 10 if this value was false, but would remain at 20 if it was true.
/// Note that this value only has an effect if <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled.
/// </summary> /// </summary>
public bool SetWidthBasedOnChildren; public bool TreatSizeAsMinimum;
/// <summary> /// <summary>
/// Set this field to true to cause this element's final display area to never exceed that of its <see cref="Parent"/>. /// Set this field to true to cause this element's final display area to never exceed that of its <see cref="Parent"/>.
/// If the resulting area is too large, the size of this element is shrunk to fit the target area. /// If the resulting area is too large, the size of this element is shrunk to fit the target area.
@ -513,7 +521,9 @@ namespace MLEM.Ui.Elements {
var parentArea = this.Parent != null ? this.Parent.ChildPaddedArea : (RectangleF) this.system.Viewport; var parentArea = this.Parent != null ? this.Parent.ChildPaddedArea : (RectangleF) this.system.Viewport;
var parentCenterX = parentArea.X + parentArea.Width / 2; var parentCenterX = parentArea.X + parentArea.Width / 2;
var parentCenterY = parentArea.Y + parentArea.Height / 2; var parentCenterY = parentArea.Y + parentArea.Height / 2;
UpdateDisplayArea(this.CalcActualSize(parentArea));
var actualSize = this.CalcActualSize(parentArea);
UpdateDisplayArea(actualSize);
if (this.Children.Count > 0) { if (this.Children.Count > 0) {
var autoSize = this.DisplayArea.Size; var autoSize = this.DisplayArea.Size;
@ -527,11 +537,13 @@ namespace MLEM.Ui.Elements {
if (rightmost != null) if (rightmost != null)
autoSize.X = rightmost.UnscrolledArea.Right - this.DisplayArea.X + this.ScaledChildPadding.Right; autoSize.X = rightmost.UnscrolledArea.Right - this.DisplayArea.X + this.ScaledChildPadding.Right;
} }
if (this.TreatSizeAsMinimum)
autoSize = Vector2.Max(autoSize, actualSize);
if (autoSize != this.DisplayArea.Size) if (autoSize != this.DisplayArea.Size)
UpdateDisplayArea(autoSize); UpdateDisplayArea(autoSize);
} }
void UpdateDisplayArea(Vector2 actualSize) { void UpdateDisplayArea(Vector2 newSize) {
var pos = new Vector2(); var pos = new Vector2();
switch (this.anchor) { switch (this.anchor) {
case Anchor.TopLeft: case Anchor.TopLeft:
@ -543,37 +555,37 @@ namespace MLEM.Ui.Elements {
break; break;
case Anchor.TopCenter: case Anchor.TopCenter:
case Anchor.AutoCenter: case Anchor.AutoCenter:
pos.X = parentCenterX - actualSize.X / 2 + this.ScaledOffset.X; pos.X = parentCenterX - newSize.X / 2 + this.ScaledOffset.X;
pos.Y = parentArea.Y + this.ScaledOffset.Y; pos.Y = parentArea.Y + this.ScaledOffset.Y;
break; break;
case Anchor.TopRight: case Anchor.TopRight:
case Anchor.AutoRight: case Anchor.AutoRight:
pos.X = parentArea.Right - actualSize.X - this.ScaledOffset.X; pos.X = parentArea.Right - newSize.X - this.ScaledOffset.X;
pos.Y = parentArea.Y + this.ScaledOffset.Y; pos.Y = parentArea.Y + this.ScaledOffset.Y;
break; break;
case Anchor.CenterLeft: case Anchor.CenterLeft:
pos.X = parentArea.X + this.ScaledOffset.X; pos.X = parentArea.X + this.ScaledOffset.X;
pos.Y = parentCenterY - actualSize.Y / 2 + this.ScaledOffset.Y; pos.Y = parentCenterY - newSize.Y / 2 + this.ScaledOffset.Y;
break; break;
case Anchor.Center: case Anchor.Center:
pos.X = parentCenterX - actualSize.X / 2 + this.ScaledOffset.X; pos.X = parentCenterX - newSize.X / 2 + this.ScaledOffset.X;
pos.Y = parentCenterY - actualSize.Y / 2 + this.ScaledOffset.Y; pos.Y = parentCenterY - newSize.Y / 2 + this.ScaledOffset.Y;
break; break;
case Anchor.CenterRight: case Anchor.CenterRight:
pos.X = parentArea.Right - actualSize.X - this.ScaledOffset.X; pos.X = parentArea.Right - newSize.X - this.ScaledOffset.X;
pos.Y = parentCenterY - actualSize.Y / 2 + this.ScaledOffset.Y; pos.Y = parentCenterY - newSize.Y / 2 + this.ScaledOffset.Y;
break; break;
case Anchor.BottomLeft: case Anchor.BottomLeft:
pos.X = parentArea.X + this.ScaledOffset.X; pos.X = parentArea.X + this.ScaledOffset.X;
pos.Y = parentArea.Bottom - actualSize.Y - this.ScaledOffset.Y; pos.Y = parentArea.Bottom - newSize.Y - this.ScaledOffset.Y;
break; break;
case Anchor.BottomCenter: case Anchor.BottomCenter:
pos.X = parentCenterX - actualSize.X / 2 + this.ScaledOffset.X; pos.X = parentCenterX - newSize.X / 2 + this.ScaledOffset.X;
pos.Y = parentArea.Bottom - actualSize.Y - this.ScaledOffset.Y; pos.Y = parentArea.Bottom - newSize.Y - this.ScaledOffset.Y;
break; break;
case Anchor.BottomRight: case Anchor.BottomRight:
pos.X = parentArea.Right - actualSize.X - this.ScaledOffset.X; pos.X = parentArea.Right - newSize.X - this.ScaledOffset.X;
pos.Y = parentArea.Bottom - actualSize.Y - this.ScaledOffset.Y; pos.Y = parentArea.Bottom - newSize.Y - this.ScaledOffset.Y;
break; break;
} }
@ -594,7 +606,7 @@ namespace MLEM.Ui.Elements {
break; break;
case Anchor.AutoInline: case Anchor.AutoInline:
var newX = prevArea.Right + this.ScaledOffset.X; var newX = prevArea.Right + this.ScaledOffset.X;
if (newX + actualSize.X <= parentArea.Right) { if (newX + newSize.X <= parentArea.Right) {
pos.X = newX; pos.X = newX;
pos.Y = prevArea.Y + this.ScaledOffset.Y; pos.Y = prevArea.Y + this.ScaledOffset.Y;
} else { } else {
@ -614,13 +626,13 @@ namespace MLEM.Ui.Elements {
pos.X = parentArea.X; pos.X = parentArea.X;
if (pos.Y < parentArea.Y) if (pos.Y < parentArea.Y)
pos.Y = parentArea.Y; pos.Y = parentArea.Y;
if (pos.X + actualSize.X > parentArea.Right) if (pos.X + newSize.X > parentArea.Right)
actualSize.X = parentArea.Right - pos.X; newSize.X = parentArea.Right - pos.X;
if (pos.Y + actualSize.Y > parentArea.Bottom) if (pos.Y + newSize.Y > parentArea.Bottom)
actualSize.Y = parentArea.Bottom - pos.Y; newSize.Y = parentArea.Bottom - pos.Y;
} }
this.area = new RectangleF(pos, actualSize); this.area = new RectangleF(pos, newSize);
this.System.OnElementAreaUpdated?.Invoke(this); this.System.OnElementAreaUpdated?.Invoke(this);
foreach (var child in this.Children) foreach (var child in this.Children)