Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Slider to Support Non-Default Range #11742

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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