1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 14:07:52 +08:00
osu-lazer/osu.Game/Rulesets/Mods/DifficultyAdjustSettingsControl.cs

114 lines
3.8 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Beatmaps;
using osu.Game.Overlays.Settings;
namespace osu.Game.Rulesets.Mods
{
public class DifficultyAdjustSettingsControl : SettingsItem<float?>
{
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; }
2021-07-08 16:39:59 +08:00
/// <summary>
/// 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>
private readonly BindableNumber<float> displayNumber = new BindableNumber<float>();
protected override Drawable CreateControl() => new SliderControl(displayNumber);
private bool isInternalChange;
2021-07-08 15:40:32 +08:00
private DifficultyBindable difficultyBindable;
public override Bindable<float?> Current
{
get => base.Current;
set
{
// intercept and extract the DifficultyBindable.
difficultyBindable = (DifficultyBindable)value;
2021-07-08 16:39:59 +08:00
// this bind is used to transfer bounds/precision only.
displayNumber.BindTo(difficultyBindable.CurrentNumber);
2021-07-08 15:40:32 +08:00
base.Current = value;
}
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-07-08 16:39:59 +08:00
beatmap.BindValueChanged(b => updateFromDifficulty(), true);
Current.BindValueChanged(current =>
{
2021-07-08 16:39:59 +08:00
// the user override has changed; transfer the correct value to the visual display.
if (current.NewValue == null)
updateFromDifficulty();
2021-07-08 16:39:59 +08:00
else
displayNumber.Value = current.NewValue.Value;
});
2021-07-08 16:39:59 +08:00
displayNumber.BindValueChanged(number =>
{
if (!isInternalChange)
Current.Value = number.NewValue;
});
}
private void updateFromDifficulty()
{
var difficulty = beatmap.Value.BeatmapInfo.BaseDifficulty;
if (difficulty == null)
return;
if (Current.Value == null && difficultyBindable.ReadCurrentFromDifficulty != null)
{
2021-07-08 16:39:59 +08:00
// ensure the beatmap's value is not transferred as a user override.
isInternalChange = true;
2021-07-09 12:24:26 +08:00
displayNumber.Value = difficultyBindable.ReadCurrentFromDifficulty(difficulty);
isInternalChange = false;
}
}
private class SliderControl : CompositeDrawable, IHasCurrentValue<float?>
{
private readonly BindableWithCurrent<float?> current = new BindableWithCurrent<float?>();
2021-07-08 16:39:59 +08:00
// Mainly just for fulfilling the interface requirements.
// 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)
{
InternalChildren = new Drawable[]
{
new SettingsSlider<float>
{
ShowsDefaultIndicator = false,
Current = currentNumber,
}
};
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
}
}
}
}