Skip to content

Commit

Permalink
Fix Slider to Support Non-Default Range (#11742)
Browse files Browse the repository at this point in the history
Fixes Canvas visuals so that normalized value is used. Updates slider movement calculations.
  • Loading branch information
marlenaklein-msft authored Jul 25, 2023
1 parent 8185c4b commit ea862f0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ public IEnumerator TestAssembleTouchNoSnapSlider()
Assert.IsFalse(interactionUpdated, "Slider updated value when we didn't touch the handle and snapToPosition = false");
Assert.AreEqual(0.5f, slider.Value, "Slider shouldn't have snapped to the finger point when SnapToPosition = false");

// Must move down then up in order to avoid nudging the slider
yield return rightHand.MoveTo(Vector3.zero, 60);
yield return RuntimeTestUtilities.WaitForUpdates();

Expand All @@ -404,7 +405,7 @@ public IEnumerator TestAssembleTouchNoSnapSlider()

Assert.IsTrue(slider.IsPokeSelected, "Slider should be poked!");
Assert.IsTrue(interactionStarted, "Slider didn't start interaction when we poked the handle");
Assert.IsTrue(interactionUpdated, "Slider didn't invoke OnValueUpdated when we poked the handle");
Assert.IsFalse(interactionUpdated, "Slider incorrectly invoked OnValueUpdated when we poked the handle");
Assert.AreEqual(0.5f, slider.Value, 0.001f, "Slider should still be roughly the same value");

interactionUpdated = false;
Expand Down Expand Up @@ -682,4 +683,4 @@ private void InstantiateDefaultSliderPrefab(Vector3 position, Vector3 rotation,
#endregion Private methods
}
}
#pragma warning restore CS1591
#pragma warning restore CS1591
9 changes: 5 additions & 4 deletions com.microsoft.mrtk.uxcore/Slider/Slider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ private float SnapSliderToStepPositions(float value)
{
var stepCount = value / SliderStepVal;
var snappedValue = SliderStepVal * Mathf.RoundToInt(stepCount);
Mathf.Clamp(snappedValue, MinValue, MaxValue);
Mathf.Clamp(snappedValue, 0f, 1.0f);
return snappedValue;
}

Expand All @@ -369,10 +369,11 @@ private void UpdateSliderValue()

var handDelta = Vector3.Dot(SliderTrackDirection.normalized, interactorDelta);

float unsnappedValue = Mathf.Clamp(StartSliderValue + handDelta / SliderTrackDirection.magnitude, MinValue, MaxValue);
float unsnappedValue = Mathf.Clamp(StartSliderValue + handDelta / SliderTrackDirection.magnitude, 0f, 1.0f);

Value = useSliderStepDivisions ? SnapSliderToStepPositions(unsnappedValue) : unsnappedValue;
}
var normalizedValue = useSliderStepDivisions ? SnapSliderToStepPositions(unsnappedValue) : unsnappedValue;
Value = normalizedValue * (MaxValue - MinValue) + MinValue;
}

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private void Update()
// Update the things that depend on the slider value.
void UpdateHandle(SliderEventData data)
{
UpdateHandle(data.NewValue);
UpdateHandle(SliderState.NormalizedValue);
}

// Update the things that depend on the slider value.
Expand Down

0 comments on commit ea862f0

Please sign in to comment.