-
Notifications
You must be signed in to change notification settings - Fork 459
Add display range to SliderBar #6618
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
Open
EYHN
wants to merge
4
commits into
ppy:master
Choose a base branch
from
EYHN:eyhn/slider-bar-display-range
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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> | ||
|
|
@@ -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"> | ||
| /// 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() | ||
|
|
@@ -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) | ||
| { | ||
|
|
@@ -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. | ||
|
|
@@ -138,7 +199,7 @@ protected override bool OnMouseDown(MouseDownEvent e) | |
| else | ||
| { | ||
| handleClick = true; | ||
| relativeValueAtMouseDown = null; | ||
| normalizedPositionAtMouseDown = null; | ||
| } | ||
|
|
||
| return base.OnMouseDown(e); | ||
|
|
@@ -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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably explain here that this implementation matches |
||
| 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); | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree