// Copyright (c) ppy Pty Ltd . 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 { // TODO: make abstract once we finish making each implementation. public class DifficultyAdjustSettingsControl : SettingsItem { [Resolved] private IBindable beatmap { get; set; } protected readonly BindableNumber CurrentNumber = new BindableNumber(); protected override Drawable CreateControl() => new ControlDrawable(CurrentNumber); private bool isInternalChange; private DifficultyBindable difficultyBindable; public override Bindable Current { get => base.Current; set { // intercept and extract the DifficultyBindable. difficultyBindable = (DifficultyBindable)value; CurrentNumber.BindTo(difficultyBindable.CurrentNumber); base.Current = value; } } protected override void LoadComplete() { base.LoadComplete(); beatmap.BindValueChanged(b => { updateFromDifficulty(); }, true); Current.BindValueChanged(current => { if (current.NewValue == null) updateFromDifficulty(); }); CurrentNumber.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) { isInternalChange = true; CurrentNumber.Value = UpdateFromDifficulty(difficulty); isInternalChange = false; } } // TODO: make abstract protected virtual float UpdateFromDifficulty(BeatmapDifficulty difficulty) => 0; private class ControlDrawable : CompositeDrawable, IHasCurrentValue { private readonly BindableWithCurrent current = new BindableWithCurrent(); public Bindable Current { get => current.Current; set => current.Current = value; } public ControlDrawable(BindableNumber currentNumber) { InternalChildren = new Drawable[] { new SettingsSlider { ShowsDefaultIndicator = false, Current = currentNumber, } }; AutoSizeAxes = Axes.Y; RelativeSizeAxes = Axes.X; } } } }