diff --git a/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/components/AnalogStickPanel.cs b/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/components/AnalogStickPanel.cs index ddbf3e11a3..0f853701ae 100644 --- a/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/components/AnalogStickPanel.cs +++ b/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/components/AnalogStickPanel.cs @@ -70,8 +70,8 @@ namespace BizHawk.Client.EmuHawk private readonly MutableRange _rangeX = new MutableRange(-128, 127); private readonly MutableRange _rangeY = new MutableRange(-128, 127); - private RangeStruct _actualRangeX = new RangeStruct { Start = -128, EndInclusive = 127 }; - private RangeStruct _actualRangeY = new RangeStruct { Start = -128, EndInclusive = 127 }; + private MutableRange _actualRangeX = (-128).MutableRangeTo(127); + private MutableRange _actualRangeY = (-128).MutableRangeTo(127); private bool _reverseX; private bool _reverseY; diff --git a/BizHawk.Common/Ranges.cs b/BizHawk.Common/Ranges.cs index 2d7b3aa2e5..e7a427be91 100644 --- a/BizHawk.Common/Ranges.cs +++ b/BizHawk.Common/Ranges.cs @@ -6,14 +6,6 @@ using BizHawk.Common.NumberExtensions; namespace BizHawk.Common { - /// semantically similar to , but obviously does no checks at runtime - public struct RangeStruct where T : unmanaged, IComparable - { - public T Start; - - public T EndInclusive; - } - /// represents a closed range of (class invariant: ) public interface Range where T : unmanaged, IComparable { @@ -25,7 +17,7 @@ namespace BizHawk.Common /// represents a closed range of which can be grown or shrunk (class invariant: ) public class MutableRange : Range where T : unmanaged, IComparable { - private RangeStruct r; + private (T Start, T EndInclusive) r; /// public MutableRange(T start, T endInclusive) => Overwrite(start, endInclusive); @@ -52,14 +44,18 @@ namespace BizHawk.Common /// < public void Overwrite(T start, T endInclusive) { - var range = new RangeStruct { Start = start, EndInclusive = endInclusive }; - if (range.EndInclusive.CompareTo(range.Start) < 0) throw new ArgumentOutOfRangeException(nameof(range), range, "range end < start"); - if (range is RangeStruct fr && (float.IsNaN(fr.Start) || float.IsNaN(fr.EndInclusive)) - || range is RangeStruct dr && (double.IsNaN(dr.Start) || double.IsNaN(dr.EndInclusive))) + if (endInclusive.CompareTo(start) < 0) throw new ArgumentOutOfRangeException(nameof(endInclusive), endInclusive, "range end < start"); + if (start is float fs) { - throw new ArgumentException("range bound is NaN", nameof(range)); + if (float.IsNaN(fs)) throw new ArgumentException("range start is NaN", nameof(start)); + if (endInclusive is float fe && float.IsNaN(fe)) throw new ArgumentException("range end is NaN", nameof(endInclusive)); } - r = range; + else if (start is double ds) + { + if (double.IsNaN(ds)) throw new ArgumentException("range start is NaN", nameof(start)); + if (endInclusive is double de && double.IsNaN(de)) throw new ArgumentException("range end is NaN", nameof(endInclusive)); + } + r = (start, endInclusive); } } @@ -108,6 +104,13 @@ namespace BizHawk.Common public static uint Count(this Range range) => (uint) (range.EndInclusive - range.Start + 1); + public static void Deconstruct(this Range range, out T start, out T endInclusive) + where T : unmanaged, IComparable + { + start = range.Start; + endInclusive = range.EndInclusive; + } + public static IEnumerable Enumerate(this Range range) => Enumerable.Range(range.Start, (int) range.Count()).Select(i => (byte) i); ///