Merge pull request #14487 from frenzibyte/alternative-difficulty-bindable

Fix unbind exception in `DifficultyBindable` due to different bindable type bindings
This commit is contained in:
Bartłomiej Dach 2021-08-26 23:29:54 +02:00 committed by GitHub
commit aacca006e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -91,7 +92,7 @@ namespace osu.Game.Rulesets.Mods
{ {
// This is required as SettingsItem relies heavily on this bindable for internal use. // 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. // The actual update flow is done via the bindable provided in the constructor.
private readonly BindableWithCurrent<float?> current = new BindableWithCurrent<float?>(); private readonly DifficultyBindableWithCurrent current = new DifficultyBindableWithCurrent();
public Bindable<float?> Current public Bindable<float?> Current
{ {
@ -114,5 +115,30 @@ namespace osu.Game.Rulesets.Mods
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
} }
} }
private class DifficultyBindableWithCurrent : DifficultyBindable, IHasCurrentValue<float?>
{
private Bindable<float?> currentBound;
public Bindable<float?> Current
{
get => this;
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (currentBound != null) UnbindFrom(currentBound);
BindTo(currentBound = value);
}
}
public DifficultyBindableWithCurrent(float? defaultValue = default)
: base(defaultValue)
{
}
protected override Bindable<float?> CreateInstance() => new DifficultyBindableWithCurrent();
}
} }
} }