[1.1.2566] Clicking sound when using HorizontalSlider, was fine in 1.0.7087

Started by Vigorous Bribe, March 07, 2020, 10:18:16 AM

Previous topic - Next topic

Vigorous Bribe

Hello,

My mod started to have clicking sound issue, on building settings window, when 1.1 version was introduced, and looks like new code in HorizontalSlider is causing it.

Widgets.HorizontalSlider is playing sound of SoundDefOf.DragSlider.PlayOneShotOnCamera(null) every time it is called (tens of times per second). The call to PlayOneShotOnCamera was added in 1.1 version.

Here is call triggering it:  (10.4 value (used below) is triggering it, but for example 2.4 is not)
in building ITab, protected override void FillTab()
Widgets.BeginScrollView(...);
var settingRect = new Rect(240f, 2027f, 234f, 45f); // rect:(x:240.00, y:2027.00, width:234.00, height:45.00)
float value = 624.TicksToSeconds(); // 10.4
float leftValue = 1.TicksToSeconds(); // 0.01666667
float rightValue = 200f;
float roundTo = 0.2f;
var ret = Widgets.HorizontalSlider(settingRect, value, leftValue, rightValue, true, null, null, null, roundTo);
// here ret != value, but both are 10.4
Widgets.EndScrollView();


the call to PlayOneShotOnCamera is in:
//Verse.Widgets
public static float HorizontalSlider(Rect rect, float value, float leftValue, float rightValue, bool middleAlignment = false, string label = null, string leftAlignedLabel = null, string rightAlignedLabel = null, float roundTo = -1f)


if (roundTo > 0f)
{
  num = (float)Mathf.RoundToInt(num / roundTo) * roundTo;
}
if (value != num)
{
  SoundDefOf.DragSlider.PlayOneShotOnCamera(null);
}


I think changing it to below code would solve the issue. Issue is probably caused by rounding float value.

if (roundTo > 0f)
{
  num = (float)Mathf.RoundToInt(num / roundTo) * roundTo;
  if (Math.Abs(num - value) < roundTo/4f)  // most likely they differ by very small epsylon value
  {
      num = value;
  }
}
if (value != num)
{
  SoundDefOf.DragSlider.PlayOneShotOnCamera(null);
}