1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 10:12:54 +08:00

Make OptionalRange generic

This commit is contained in:
smoogipoo 2019-09-19 16:51:57 +09:00
parent c1daa187fe
commit 3c21b68b73
2 changed files with 56 additions and 22 deletions

View File

@ -196,19 +196,19 @@ namespace osu.Game.Screens.Select
switch (key)
{
case "stars" when double.TryParse(value, out var stars):
case "stars" when float.TryParse(value, out var stars):
updateCriteriaRange(ref criteria.StarDifficulty, op, stars);
break;
case "ar" when double.TryParse(value, out var ar):
case "ar" when float.TryParse(value, out var ar):
updateCriteriaRange(ref criteria.ApproachRate, op, ar);
break;
case "dr" when double.TryParse(value, out var dr):
case "dr" when float.TryParse(value, out var dr):
updateCriteriaRange(ref criteria.DrainRate, op, dr);
break;
case "cs" when double.TryParse(value, out var cs):
case "cs" when float.TryParse(value, out var cs):
updateCriteriaRange(ref criteria.CircleSize, op, cs);
break;
@ -239,7 +239,8 @@ namespace osu.Game.Screens.Select
}
}
private void updateCriteriaRange(ref FilterCriteria.OptionalRange range, string op, double value, double tolerance = 0.05)
private void updateCriteriaRange<T>(ref FilterCriteria.OptionalRange<T> range, string op, T value, double tolerance = 0.05f)
where T : struct, IComparable<T>
{
switch (op)
{
@ -249,8 +250,20 @@ namespace osu.Game.Screens.Select
case "=":
case ":":
range.IsInclusive = true;
range.Min = value - tolerance;
range.Max = value + tolerance;
switch (value)
{
case float _:
range.Min = (T)(object)((float)(object)value - tolerance);
range.Max = (T)(object)((float)(object)value + tolerance);
break;
case double _:
range.Min = (T)(object)((double)(object)value - tolerance);
range.Max = (T)(object)((double)(object)value + tolerance);
break;
}
break;
case ">":

View File

@ -14,12 +14,12 @@ namespace osu.Game.Screens.Select
public GroupMode Group;
public SortMode Sort;
public OptionalRange StarDifficulty;
public OptionalRange ApproachRate;
public OptionalRange DrainRate;
public OptionalRange CircleSize;
public OptionalRange Length;
public OptionalRange BPM;
public OptionalRange<double> StarDifficulty;
public OptionalRange<float> ApproachRate;
public OptionalRange<float> DrainRate;
public OptionalRange<float> CircleSize;
public OptionalRange<double> Length;
public OptionalRange<double> BPM;
public int? BeatDivisor;
@ -42,23 +42,44 @@ namespace osu.Game.Screens.Select
}
}
public struct OptionalRange : IEquatable<OptionalRange>
public struct OptionalRange<T> : IEquatable<OptionalRange<T>>
where T : struct, IComparable<T>
{
public bool IsInRange(double value)
public bool IsInRange(T value)
{
if (Min.HasValue && (IsInclusive ? value < Min.Value : value <= Min.Value))
return false;
if (Max.HasValue && (IsInclusive ? value > Max.Value : value >= Max.Value))
return false;
if (Min.HasValue)
{
int comparison = value.CompareTo(Min.Value);
if (comparison < 0)
return false;
if (!IsInclusive && comparison == 0)
return false;
}
if (Max.HasValue)
{
int comparison = value.CompareTo(Max.Value);
if (comparison > 0)
return false;
if (!IsInclusive && comparison == 0)
return false;
}
return true;
}
public double? Min;
public double? Max;
public T? Min;
public T? Max;
public bool IsInclusive;
public bool Equals(OptionalRange range) => Min == range.Min && Max == range.Max;
public bool Equals(OptionalRange<T> other)
=> Min.Equals(other.Min)
&& Max.Equals(other.Max)
&& IsInclusive.Equals(other.IsInclusive);
}
}
}