1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

improve handling of arbitrary Epsilon values

This commit is contained in:
Ell 2021-10-28 23:26:42 +02:00
parent d05262e8a6
commit 2be39a740e
4 changed files with 20 additions and 6 deletions

View file

@ -15,6 +15,7 @@ Additions
Improvements Improvements
- Exposed Camera's RoundPosition - Exposed Camera's RoundPosition
- Exposed the epsilon value used by Camera
### MLEM.Ui ### MLEM.Ui
Additions Additions
@ -23,6 +24,7 @@ Additions
Improvements Improvements
- Cache TokenizedString inner offsets for non-Left text alignments to improve performance - Cache TokenizedString inner offsets for non-Left text alignments to improve performance
- Exposed the epsilon value used by Element calculations
Fixes Fixes
- Fixed VerticalSpace height parameter being an integer - Fixed VerticalSpace height parameter being an integer

View file

@ -18,6 +18,12 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public abstract class Element : GenericDataHolder, IDisposable { public abstract class Element : GenericDataHolder, IDisposable {
/// <summary>
/// This field holds an epsilon value used in element <see cref="Size"/>, position and resulting <see cref="Area"/> calculations to mitigate floating point rounding inaccuracies.
/// If ui elements used are extremely small or extremely large, this value can be reduced or increased.
/// </summary>
public static float Epsilon = 0.01F;
/// <summary> /// <summary>
/// A list of all of this element's direct children. /// A list of all of this element's direct children.
/// Use <see cref="AddChild{T}"/> or <see cref="RemoveChild"/> to manipulate this list while calling all of the necessary callbacks. /// Use <see cref="AddChild{T}"/> or <see cref="RemoveChild"/> to manipulate this list while calling all of the necessary callbacks.
@ -607,9 +613,9 @@ 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;
// with awkward ui scale values, floating point rounding can cause an element that would usually be // with awkward ui scale values, floating point rounding can cause an element that would usually
// positioned correctly to be pushed into the next line due to a very small deviation, so we add 0.01 here // be positioned correctly to be pushed into the next line due to a very small deviation
if (newX + newSize.X <= parentArea.Right + 0.01F) { if (newX + newSize.X <= parentArea.Right + Epsilon) {
pos.X = newX; pos.X = newX;
pos.Y = prevArea.Y + this.ScaledOffset.Y; pos.Y = prevArea.Y + this.ScaledOffset.Y;
} else { } else {
@ -676,7 +682,7 @@ namespace MLEM.Ui.Elements {
} }
// we want to leave some leeway to prevent float rounding causing an infinite loop // we want to leave some leeway to prevent float rounding causing an infinite loop
if (!autoSize.Equals(this.UnscrolledArea.Size, 0.01F)) { if (!autoSize.Equals(this.UnscrolledArea.Size, Epsilon)) {
recursion++; recursion++;
if (recursion >= 16) { if (recursion >= 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?"); 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?");

View file

@ -45,7 +45,7 @@ namespace MLEM.Ui.Elements {
// force current value to be clamped // force current value to be clamped
this.CurrentValue = this.CurrentValue; this.CurrentValue = this.CurrentValue;
// auto-hide if necessary // auto-hide if necessary
var shouldHide = this.maxValue <= 0.01F; var shouldHide = this.maxValue <= Epsilon;
if (this.AutoHideWhenEmpty && this.IsHidden != shouldHide) { if (this.AutoHideWhenEmpty && this.IsHidden != shouldHide) {
this.IsHidden = shouldHide; this.IsHidden = shouldHide;
this.OnAutoHide?.Invoke(this); this.OnAutoHide?.Invoke(this);

View file

@ -11,6 +11,12 @@ namespace MLEM.Cameras {
/// </summary> /// </summary>
public class Camera { public class Camera {
/// <summary>
/// This field holds an epsilon value used in some camera calculations to mitigate floating point rounding inaccuracies.
/// If camera <see cref="Position"/> or <see cref="Viewport"/> size are extremely small or extremely big, this value can be reduced or increased.
/// </summary>
public static float Epsilon = 0.01F;
/// <summary> /// <summary>
/// The top-left corner of the camera's viewport. /// The top-left corner of the camera's viewport.
/// <seealso cref="LookingPosition"/> /// <seealso cref="LookingPosition"/>
@ -158,7 +164,7 @@ namespace MLEM.Cameras {
if (this.Max.Y > max.Y) if (this.Max.Y > max.Y)
this.Max = new Vector2(this.Max.X, max.Y); this.Max = new Vector2(this.Max.X, max.Y);
} }
return !this.Position.Equals(lastPos, 0.001F); return !this.Position.Equals(lastPos, Epsilon);
} }
/// <summary> /// <summary>