1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 09:49:23 +01:00

Added Range extension methods GetPercentage and FromPercentage

This commit is contained in:
Ell 2022-11-10 15:41:24 +01:00
parent 5906278091
commit 6ac2ba6151
5 changed files with 93 additions and 8 deletions

View file

@ -84,6 +84,9 @@ Removals
- Marked DynamicEnum as obsolete due to its reimplementation in [DynamicEnums](https://www.nuget.org/packages/DynamicEnums)
## MLEM.Extended
Additions
- Added Range extension methods GetPercentage and FromPercentage
Improvements
- Multi-target net452, making MLEM compatible with MonoGame for consoles
- Added trimming and AOT annotations and made MLEM.Extended trimmable

View file

@ -40,5 +40,53 @@ namespace MLEM.Extended.Extensions {
return rect.ToMlem().Penetrate(other.ToMlem(), out normal, out penetration);
}
/// <summary>
/// Returns how far between the given <paramref name="range"/>'s <see cref="Range{T}.Min"/> and <see cref="Range{T}.Max"/> value the given <paramref name="value"/> is, as a number between 0 and 1.
/// Note that, if the <paramref name="value"/> is outside the given <paramref name="range"/>, a correct proportional value outside the 0 to 1 range will still be returned.
/// This method is the reverse action of <see cref="FromPercentage(MonoGame.Extended.Range{float},float)"/>.
/// </summary>
/// <param name="range">The range to query.</param>
/// <param name="value">The value to query.</param>
/// <returns>The percentage.</returns>
public static float GetPercentage(this Range<float> range, float value) {
return (value - range.Min) / (range.Max - range.Min);
}
/// <summary>
/// Returns how far between the given <paramref name="range"/>'s <see cref="Range{T}.Min"/> and <see cref="Range{T}.Max"/> value the given <paramref name="value"/> is, as a number between 0 and 1.
/// Note that, if the <paramref name="value"/> is outside the given <paramref name="range"/>, a correct proportional value outside the 0 to 1 range will still be returned.
/// This method is the reverse action of <see cref="FromPercentage(MonoGame.Extended.Range{int},float)"/>.
/// </summary>
/// <param name="range">The range to query.</param>
/// <param name="value">The value to query.</param>
/// <returns>The percentage.</returns>
public static float GetPercentage(this Range<int> range, float value) {
return (value - range.Min) / (range.Max - range.Min);
}
/// <summary>
/// Returns a value within the given <paramref name="range"/>'s <see cref="Range{T}.Min"/> and <see cref="Range{T}.Max"/> values based on the given <paramref name="percentage"/> into the range.
/// Note that, if the <paramref name="percentage"/> is outside the 0 to 1 range, a correct value outside the <paramref name="range"/> will still be returned.
/// This method is the reverse action of <see cref="GetPercentage(MonoGame.Extended.Range{float},float)"/>.
/// </summary>
/// <param name="range">The range to query.</param>
/// <param name="percentage">The percentage to query.</param>
/// <returns>The value.</returns>
public static float FromPercentage(this Range<float> range, float percentage) {
return (range.Max - range.Min) * percentage + range.Min;
}
/// <summary>
/// Returns a value within the given <paramref name="range"/>'s <see cref="Range{T}.Min"/> and <see cref="Range{T}.Max"/> values based on the given <paramref name="percentage"/> into the range.
/// Note that, if the <paramref name="percentage"/> is outside the 0 to 1 range, a correct value outside the <paramref name="range"/> will still be returned.
/// This method is the reverse action of <see cref="GetPercentage(MonoGame.Extended.Range{int},float)"/>.
/// </summary>
/// <param name="range">The range to query.</param>
/// <param name="percentage">The percentage to query.</param>
/// <returns>The value.</returns>
public static float FromPercentage(this Range<int> range, float percentage) {
return (range.Max - range.Min) * percentage + range.Min;
}
}
}

View file

@ -1,9 +1,14 @@
using Microsoft.Xna.Framework;
using MLEM.Extensions;
using MLEM.Misc;
using NUnit.Framework;
using RectangleF = MLEM.Misc.RectangleF;
namespace Tests;
#if !FNA
using MonoGame.Extended;
using MLEM.Extended.Extensions;
#endif
namespace Tests;
public class NumberTests {
@ -68,4 +73,28 @@ public class NumberTests {
Assert.AreEqual(penetration, 0);
}
#if !FNA
[Test]
public void TestRangePercentage() {
Assert.AreEqual(0.5F, new Range<int>(1, 7).GetPercentage(4));
Assert.AreEqual(1, new Range<int>(1, 7).GetPercentage(7));
Assert.AreEqual(0, new Range<int>(1, 7).GetPercentage(1));
Assert.AreEqual(4, new Range<int>(1, 7).FromPercentage(0.5F));
Assert.AreEqual(7, new Range<int>(1, 7).FromPercentage(1));
Assert.AreEqual(1, new Range<int>(1, 7).FromPercentage(0));
Assert.AreEqual(0.5F, new Range<float>(-1, 1).GetPercentage(0));
Assert.AreEqual(0.25F, new Range<float>(-1, 1).GetPercentage(-0.5F));
Assert.AreEqual(0.75F, new Range<float>(-1, 1).GetPercentage(0.5F));
Assert.AreEqual(0, new Range<float>(-1, 1).FromPercentage(0.5F));
Assert.AreEqual(-0.5F, new Range<float>(-1, 1).FromPercentage(0.25F));
Assert.AreEqual(0.5F, new Range<float>(-1, 1).FromPercentage(0.75F));
Assert.AreEqual(1.5F, new Range<float>(8, 10).GetPercentage(11));
Assert.AreEqual(-0.5F, new Range<float>(8, 10).GetPercentage(7));
Assert.AreEqual(11, new Range<float>(8, 10).FromPercentage(1.5F));
Assert.AreEqual(7, new Range<float>(8, 10).FromPercentage(-0.5F));
}
#endif
}

View file

@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MLEM.Extended\MLEM.Extended.FNA.csproj" />
<ProjectReference Include="..\MLEM.Startup\MLEM.Startup.FNA.csproj" />
<ProjectReference Include="..\MLEM.Data\MLEM.Data.FNA.csproj" />
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.FNA.csproj" />
@ -17,12 +18,13 @@
<ItemGroup>
<ProjectReference Include="..\FNA\FNA.Core.csproj" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NunitXml.TestLogger" Version="3.0.117" />

View file

@ -5,27 +5,30 @@
<VSTestResultsDirectory>TestResults</VSTestResultsDirectory>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj" />
<ProjectReference Include="..\MLEM.Startup\MLEM.Startup.csproj" />
<ProjectReference Include="..\MLEM.Data\MLEM.Data.csproj" />
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" />
<ProjectReference Include="..\MLEM\MLEM.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="MonoGame.Extended" Version="3.8.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NunitXml.TestLogger" Version="3.0.117" />
</ItemGroup>
<ItemGroup>
<Content Include="Content/**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>