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

avoid recursion in Element.ForceUpdateArea

This commit is contained in:
Ell 2021-03-24 01:10:42 +01:00
parent a477fef230
commit 3d314172d0

View file

@ -174,7 +174,6 @@ namespace MLEM.Ui.Elements {
} }
} }
private bool areaDirty; private bool areaDirty;
private int areaUpdateRecursionCount;
/// <summary> /// <summary>
/// The <see cref="UnscrolledArea"/> of this element, but with <see cref="ScaledScrollOffset"/> applied. /// The <see cref="UnscrolledArea"/> of this element, but with <see cref="ScaledScrollOffset"/> applied.
/// </summary> /// </summary>
@ -514,10 +513,26 @@ 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); if (this.Children.Count > 0) {
var autoSize = this.DisplayArea.Size;
if (this.SetHeightBasedOnChildren) {
var lowest = this.GetLowestChild(e => !e.IsHidden);
if (lowest != null)
autoSize.Y = lowest.UnscrolledArea.Bottom - this.DisplayArea.Y + this.ScaledChildPadding.Bottom;
}
if (this.SetWidthBasedOnChildren) {
var rightmost = this.GetRightmostChild(e => !e.IsHidden);
if (rightmost != null)
autoSize.X = rightmost.UnscrolledArea.Right - this.DisplayArea.X + this.ScaledChildPadding.Right;
}
if (autoSize != this.DisplayArea.Size)
UpdateDisplayArea(autoSize);
}
void UpdateDisplayArea(Vector2 actualSize) {
var pos = new Vector2(); var pos = new Vector2();
switch (this.anchor) { switch (this.anchor) {
case Anchor.TopLeft: case Anchor.TopLeft:
case Anchor.AutoLeft: case Anchor.AutoLeft:
@ -610,39 +625,6 @@ namespace MLEM.Ui.Elements {
foreach (var child in this.Children) foreach (var child in this.Children)
child.ForceUpdateArea(); child.ForceUpdateArea();
if (this.Children.Count > 0) {
Element foundChild = null;
if (this.SetHeightBasedOnChildren) {
var lowest = this.GetLowestChild(e => !e.IsHidden);
if (lowest != null) {
var newHeight = (lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Bottom) / this.Scale;
if (!newHeight.Equals(this.size.Y, 0.01F)) {
this.size.Y = newHeight;
foundChild = lowest;
}
}
}
if (this.SetWidthBasedOnChildren) {
var rightmost = this.GetRightmostChild(e => !e.IsHidden);
if (rightmost != null) {
var newWidth = (rightmost.UnscrolledArea.Right - pos.X + this.ScaledChildPadding.Right) / this.Scale;
if (!newWidth.Equals(this.size.X, 0.01F)) {
this.size.X = newWidth;
foundChild = rightmost;
}
}
}
if (foundChild != null) {
this.areaUpdateRecursionCount++;
if (this.areaUpdateRecursionCount >= 16) {
throw new ArithmeticException($"The area of {this} with root {this.Root?.Name} has recursively updated too often. Does its child {foundChild} contain any conflicting auto-sizing settings?");
} else {
this.ForceUpdateArea();
}
} else {
this.areaUpdateRecursionCount = 0;
}
} }
} }