add clamp option to map function

This commit is contained in:
Mister_Nebula 2021-12-27 21:04:57 +00:00
parent 3178074803
commit 40e1c948dd
2 changed files with 9 additions and 3 deletions

View File

@ -412,7 +412,7 @@ namespace QSB.TimeSync
return;
}
var mappedTimescale = diff.Map(-PauseOrFastForwardThreshold, PauseOrFastForwardThreshold, 1 + TimescaleBounds, 1 - TimescaleBounds);
var mappedTimescale = diff.Map(-PauseOrFastForwardThreshold, PauseOrFastForwardThreshold, 1 + TimescaleBounds, 1 - TimescaleBounds, true);
if (mappedTimescale > 100f)
{
DebugLog.ToConsole($"Warning - CheckTimeDifference() returned over 100 - should have switched into fast-forward!", MessageType.Warning);

View File

@ -100,8 +100,14 @@ namespace QSB.Utility
}
}
public static float Map(this float value, float inputFrom, float inputTo, float outputFrom, float outputTo)
=> ((value - inputFrom) / (inputTo - inputFrom) * (outputTo - outputFrom)) + outputFrom;
public static float Map(this float value, float inputFrom, float inputTo, float outputFrom, float outputTo, bool clamp)
{
var mappedValue = ((value - inputFrom) / (inputTo - inputFrom) * (outputTo - outputFrom)) + outputFrom;
return clamp
? Mathf.Clamp(mappedValue, outputTo, outputFrom)
: mappedValue;
}
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
{