1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 18:52:55 +08:00

Refactor DifficultyAdjustSettingsControl to help with readability

This commit is contained in:
Dean Herbert 2021-07-09 13:50:07 +09:00
parent 51bd83b3f4
commit e0277763d0

View File

@ -18,11 +18,14 @@ namespace osu.Game.Rulesets.Mods
/// <summary> /// <summary>
/// Used to track the display value on the setting slider. /// Used to track the display value on the setting slider.
/// This can either be a user override or the beatmap default (when <see cref="Current"/> is null).
/// </summary> /// </summary>
private readonly BindableNumber<float> displayNumber = new BindableNumber<float>(); /// <remarks>
/// When the mod is overriding a default, this will match the value of <see cref="Current"/>.
/// When there is no override (ie. <see cref="Current"/> is null), this value will match the beatmap provided default via <see cref="updateFromDifficulty"/>.
/// </remarks>
private readonly BindableNumber<float> sliderDisplayCurrent = new BindableNumber<float>();
protected override Drawable CreateControl() => new SliderControl(displayNumber); protected override Drawable CreateControl() => new SliderControl(sliderDisplayCurrent);
private bool isInternalChange; private bool isInternalChange;
@ -33,11 +36,10 @@ namespace osu.Game.Rulesets.Mods
get => base.Current; get => base.Current;
set set
{ {
// intercept and extract the DifficultyBindable. // Intercept and extract the internal number bindable from DifficultyBindable.
// This will provide bounds and precision specifications for the slider bar.
difficultyBindable = (DifficultyBindable)value; difficultyBindable = (DifficultyBindable)value;
sliderDisplayCurrent.BindTo(difficultyBindable.CurrentNumber);
// this bind is used to transfer bounds/precision only.
displayNumber.BindTo(difficultyBindable.CurrentNumber);
base.Current = value; base.Current = value;
} }
@ -51,15 +53,22 @@ namespace osu.Game.Rulesets.Mods
Current.BindValueChanged(current => Current.BindValueChanged(current =>
{ {
// the user override has changed; transfer the correct value to the visual display. if (current.NewValue != null)
if (current.NewValue == null) {
updateFromDifficulty(); // a user override has been added or updated.
sliderDisplayCurrent.Value = current.NewValue.Value;
}
else else
displayNumber.Value = current.NewValue.Value; {
// user override was removed, so restore the beatmap provided value.
updateFromDifficulty();
}
}); });
displayNumber.BindValueChanged(number => sliderDisplayCurrent.BindValueChanged(number =>
{ {
// this handles the transfer of the slider value to the main bindable.
// as such, should be skipped if the slider is being updated via updateFromDifficulty().
if (!isInternalChange) if (!isInternalChange)
Current.Value = number.NewValue; Current.Value = number.NewValue;
}); });
@ -76,23 +85,16 @@ namespace osu.Game.Rulesets.Mods
{ {
// ensure the beatmap's value is not transferred as a user override. // ensure the beatmap's value is not transferred as a user override.
isInternalChange = true; isInternalChange = true;
displayNumber.Value = difficultyBindable.ReadCurrentFromDifficulty(difficulty); sliderDisplayCurrent.Value = difficultyBindable.ReadCurrentFromDifficulty(difficulty);
isInternalChange = false; isInternalChange = false;
} }
} }
private class SliderControl : CompositeDrawable, IHasCurrentValue<float?> private class SliderControl : CompositeDrawable, IHasCurrentValue<float?>
{ {
private readonly BindableWithCurrent<float?> current = new BindableWithCurrent<float?>(); // This is required as SettingsItem relies heavily on this bindable for internal use.
// The actual update flow is done via the bindable provided in the constructor.
// Mainly just for fulfilling the interface requirements. public Bindable<float?> Current { get; set; } = new Bindable<float?>();
// The actual update flow is done via the provided number.
// Of note, this is used for the "reset to default" flow.
public Bindable<float?> Current
{
get => current.Current;
set => current.Current = value;
}
public SliderControl(BindableNumber<float> currentNumber) public SliderControl(BindableNumber<float> currentNumber)
{ {