mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 22:33:05 +08:00
Simplify bindable update methods
This commit is contained in:
parent
e0277763d0
commit
741062a6da
@ -21,12 +21,15 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// When the mod is overriding a default, this will match the value of <see cref="Current"/>.
|
/// 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"/>.
|
/// When there is no override (ie. <see cref="Current"/> is null), this value will match the beatmap provided default via <see cref="updateCurrentFromSlider"/>.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
private readonly BindableNumber<float> sliderDisplayCurrent = new BindableNumber<float>();
|
private readonly BindableNumber<float> sliderDisplayCurrent = new BindableNumber<float>();
|
||||||
|
|
||||||
protected override Drawable CreateControl() => new SliderControl(sliderDisplayCurrent);
|
protected override Drawable CreateControl() => new SliderControl(sliderDisplayCurrent);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Guards against beatmap values displayed on slider bars being transferred to user override.
|
||||||
|
/// </summary>
|
||||||
private bool isInternalChange;
|
private bool isInternalChange;
|
||||||
|
|
||||||
private DifficultyBindable difficultyBindable;
|
private DifficultyBindable difficultyBindable;
|
||||||
@ -49,21 +52,8 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
{
|
{
|
||||||
base.LoadComplete();
|
base.LoadComplete();
|
||||||
|
|
||||||
beatmap.BindValueChanged(b => updateFromDifficulty(), true);
|
Current.BindValueChanged(current => updateCurrentFromSlider());
|
||||||
|
beatmap.BindValueChanged(b => updateCurrentFromSlider(), true);
|
||||||
Current.BindValueChanged(current =>
|
|
||||||
{
|
|
||||||
if (current.NewValue != null)
|
|
||||||
{
|
|
||||||
// a user override has been added or updated.
|
|
||||||
sliderDisplayCurrent.Value = current.NewValue.Value;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// user override was removed, so restore the beatmap provided value.
|
|
||||||
updateFromDifficulty();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
sliderDisplayCurrent.BindValueChanged(number =>
|
sliderDisplayCurrent.BindValueChanged(number =>
|
||||||
{
|
{
|
||||||
@ -74,21 +64,28 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateFromDifficulty()
|
private void updateCurrentFromSlider()
|
||||||
{
|
{
|
||||||
|
if (Current.Value != null)
|
||||||
|
{
|
||||||
|
// a user override has been added or updated.
|
||||||
|
sliderDisplayCurrent.Value = Current.Value.Value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var difficulty = beatmap.Value.BeatmapInfo.BaseDifficulty;
|
var difficulty = beatmap.Value.BeatmapInfo.BaseDifficulty;
|
||||||
|
|
||||||
if (difficulty == null)
|
if (difficulty == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (Current.Value == null && difficultyBindable.ReadCurrentFromDifficulty != null)
|
// generally should always be implemented, else the slider will have a zero default.
|
||||||
{
|
if (difficultyBindable.ReadCurrentFromDifficulty == null)
|
||||||
// ensure the beatmap's value is not transferred as a user override.
|
return;
|
||||||
|
|
||||||
isInternalChange = true;
|
isInternalChange = true;
|
||||||
sliderDisplayCurrent.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?>
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user