Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions osu.Framework.Tests/Visual/UserInterface/TestSceneSliderBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ public TestSceneSliderBar()
KeyboardStep = 1,
Current = sliderBarValue
},
new SpriteText
{
Text = "w/ WithDisplayRange:",
},
new BasicSliderBar<double>
{
Size = new Vector2(200, 10),
BackgroundColour = Color4.White,
SelectionColour = Color4.Pink,
FocusColour = Color4.OrangeRed,
KeyboardStep = 1,
Current = sliderBarValue,
MaxDisplayRange = 20,
MinDisplayRange = 0
}
}
});
}
Expand Down
91 changes: 79 additions & 12 deletions osu.Framework/Graphics/UserInterface/SliderBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,44 @@ public abstract partial class SliderBar<T> : Container, IHasCurrentValue<T>
/// </summary>
public float RangePadding;

private T maxDisplayRange = T.MaxValue;

private T minDisplayRange = T.MinValue;

/// <summary>
/// The maximum value to display on the slider bar.
/// By default, the slider bar will display values between <see cref="RangeConstrainedBindable{T}.MinValue"/> and <see cref="RangeConstrainedBindable{T}.MaxValue"/> of <see cref="Current"/>.
/// To display a different range, you can set <see cref="MaxDisplayRange"/> and <see cref="MinDisplayRange"/>.
/// </summary>
public T MaxDisplayRange
{
get
{
if (maxDisplayRange == T.MaxValue)
return currentNumberInstantaneous.MaxValue;

return maxDisplayRange;
}
set => maxDisplayRange = value;
}

/// <summary>
/// The minimum value to display on the slider bar.
/// By default, the slider bar will display values between <see cref="RangeConstrainedBindable{T}.MinValue"/> and <see cref="RangeConstrainedBindable{T}.MaxValue"/> of <see cref="Current"/>.
/// To display a different range, you can set <see cref="MaxDisplayRange"/> and <see cref="MinDisplayRange"/>.
/// </summary>
public T MinDisplayRange
{
get
{
if (minDisplayRange == T.MinValue)
return currentNumberInstantaneous.MinValue;

return minDisplayRange;
}
set => minDisplayRange = value;
}

public float UsableWidth => DrawWidth - 2 * RangePadding;

/// <summary>
Expand Down Expand Up @@ -95,10 +133,30 @@ protected float NormalizedValue
}
}

/// <summary>
/// The normalized value representing the position between <see cref="MinDisplayRange"/> and <see cref="MaxDisplayRange"/>.
/// </summary>
protected float NormalizedPosition
{
get
{
float min = float.CreateTruncating(MinDisplayRange);
float max = float.CreateTruncating(MaxDisplayRange);

if (max - min == 0)
return 1;

float val = float.CreateTruncating(currentNumberInstantaneous.Value);
return (val - min) / (max - min);
}
}

/// <summary>
/// Triggered when the <see cref="Current"/> value has changed. Used to update the displayed value.
/// </summary>
/// <param name="value">The normalized <see cref="Current"/> value.</param>
/// <param name="value">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this parameter should be renamed to position.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

/// The normalized value representing the position between <see cref="MinDisplayRange"/> and <see cref="MaxDisplayRange"/>.
/// </param>
protected abstract void UpdateValue(float value);

protected override void LoadComplete()
Expand All @@ -112,10 +170,10 @@ protected override void LoadComplete()
Scheduler.AddOnce(updateValue);
}

private void updateValue() => UpdateValue(NormalizedValue);
private void updateValue() => UpdateValue(NormalizedPosition);

private bool handleClick;
private float? relativeValueAtMouseDown;
private float? normalizedPositionAtMouseDown;

protected override bool OnMouseDown(MouseDownEvent e)
{
Expand All @@ -125,11 +183,14 @@ protected override bool OnMouseDown(MouseDownEvent e)

if (ShouldHandleAsRelativeDrag(e))
{
float min = float.CreateTruncating(currentNumberInstantaneous.MinValue);
float max = float.CreateTruncating(currentNumberInstantaneous.MaxValue);
float min = float.CreateTruncating(MinDisplayRange);
float max = float.CreateTruncating(MaxDisplayRange);
float val = float.CreateTruncating(currentNumberInstantaneous.Value);

relativeValueAtMouseDown = (val - min) / (max - min);
if (max - min == 0)
normalizedPositionAtMouseDown = 1;

normalizedPositionAtMouseDown = (val - min) / (max - min);

// Click shouldn't be handled if relative dragging is happening (i.e. while holding a nub).
// This is generally an expectation by most OSes and UIs.
Expand All @@ -138,7 +199,7 @@ protected override bool OnMouseDown(MouseDownEvent e)
else
{
handleClick = true;
relativeValueAtMouseDown = null;
normalizedPositionAtMouseDown = null;
}

return base.OnMouseDown(e);
Expand Down Expand Up @@ -246,18 +307,24 @@ private void handleMouseInput(MouseButtonEvent e)

float localX = ToLocalSpace(e.ScreenSpaceMousePosition).X;

float newValue;
float normalizedPosition;

if (relativeValueAtMouseDown != null && e is DragEvent drag)
if (normalizedPositionAtMouseDown != null && e is DragEvent drag)
{
newValue = relativeValueAtMouseDown.Value + (localX - ToLocalSpace(drag.ScreenSpaceMouseDownPosition).X) / UsableWidth;
normalizedPosition = normalizedPositionAtMouseDown.Value + (localX - ToLocalSpace(drag.ScreenSpaceMouseDownPosition).X) / UsableWidth;
}
else
{
newValue = (localX - RangePadding) / UsableWidth;
normalizedPosition = (localX - RangePadding) / UsableWidth;
}

currentNumberInstantaneous.SetProportional(newValue, e.ShiftPressed ? KeyboardStep : 0);
double min = double.CreateTruncating(MinDisplayRange);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably explain here that this implementation matches BindableNumber.SetProportional (now unused and having no test coverage). Or maybe even remove that method (although note that it is public..)

double max = double.CreateTruncating(MaxDisplayRange);
double value = min + (max - min) * normalizedPosition;
if (e.ShiftPressed) // if shift is pressed, snap the final value to the closest multiple of KeyboardStep
value = Math.Round(value / KeyboardStep) * KeyboardStep;

currentNumberInstantaneous.Set(value);
onUserChange(currentNumberInstantaneous.Value);
}

Expand Down
Loading