2021-07-08 07:40:32 +00:00
|
|
|
// 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 System;
|
|
|
|
using osu.Framework.Bindables;
|
2021-07-08 07:56:16 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2021-07-08 07:40:32 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Mods
|
|
|
|
{
|
|
|
|
public class DifficultyBindable : Bindable<float?>
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Whether the extended limits should be applied to this bindable.
|
|
|
|
/// </summary>
|
2021-07-09 04:24:26 +00:00
|
|
|
public readonly BindableBool ExtendedLimits = new BindableBool();
|
2021-07-08 07:40:32 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// An internal numeric bindable to hold and propagate min/max/precision.
|
|
|
|
/// The value of this bindable should not be set.
|
|
|
|
/// </summary>
|
2021-07-12 02:26:30 +00:00
|
|
|
internal readonly BindableFloat CurrentNumber = new BindableFloat
|
2021-07-08 07:40:32 +00:00
|
|
|
{
|
|
|
|
MinValue = 0,
|
|
|
|
MaxValue = 10,
|
|
|
|
};
|
|
|
|
|
2021-07-08 07:56:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A function that can extract the current value of this setting from a beatmap difficulty for display purposes.
|
|
|
|
/// </summary>
|
2022-07-03 14:19:35 +00:00
|
|
|
public Func<IBeatmapDifficultyInfo, float>? ReadCurrentFromDifficulty;
|
2021-07-08 07:56:16 +00:00
|
|
|
|
2021-07-08 07:40:32 +00:00
|
|
|
public float Precision
|
|
|
|
{
|
|
|
|
set => CurrentNumber.Precision = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float MinValue
|
|
|
|
{
|
|
|
|
set => CurrentNumber.MinValue = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private float maxValue;
|
|
|
|
|
|
|
|
public float MaxValue
|
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == maxValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
maxValue = value;
|
|
|
|
updateMaxValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private float? extendedMaxValue;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The maximum value to be used when extended limits are applied.
|
|
|
|
/// </summary>
|
|
|
|
public float? ExtendedMaxValue
|
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == extendedMaxValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
extendedMaxValue = value;
|
|
|
|
updateMaxValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public DifficultyBindable()
|
2021-07-12 07:52:57 +00:00
|
|
|
: this(null)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public DifficultyBindable(float? defaultValue = null)
|
|
|
|
: base(defaultValue)
|
2021-07-08 07:40:32 +00:00
|
|
|
{
|
|
|
|
ExtendedLimits.BindValueChanged(_ => updateMaxValue());
|
2021-07-08 08:40:09 +00:00
|
|
|
}
|
2021-07-08 07:40:32 +00:00
|
|
|
|
2021-07-08 08:40:09 +00:00
|
|
|
public override float? Value
|
|
|
|
{
|
|
|
|
get => base.Value;
|
|
|
|
set
|
2021-07-08 07:40:32 +00:00
|
|
|
{
|
|
|
|
// Ensure that in the case serialisation runs in the wrong order (and limit extensions aren't applied yet) the deserialised value is still propagated.
|
2021-07-08 08:40:09 +00:00
|
|
|
if (value != null)
|
|
|
|
CurrentNumber.MaxValue = MathF.Max(CurrentNumber.MaxValue, value.Value);
|
|
|
|
|
|
|
|
base.Value = value;
|
|
|
|
}
|
2021-07-08 07:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateMaxValue()
|
|
|
|
{
|
|
|
|
CurrentNumber.MaxValue = ExtendedLimits.Value && extendedMaxValue != null ? extendedMaxValue.Value : maxValue;
|
|
|
|
}
|
2021-07-11 13:12:46 +00:00
|
|
|
|
2022-11-22 05:51:17 +00:00
|
|
|
public override void CopyTo(Bindable<float?> them)
|
2021-07-11 13:12:46 +00:00
|
|
|
{
|
2021-07-12 07:52:57 +00:00
|
|
|
if (!(them is DifficultyBindable otherDifficultyBindable))
|
2022-11-22 05:51:17 +00:00
|
|
|
throw new InvalidOperationException($"Cannot copy to a non-{nameof(DifficultyBindable)}.");
|
|
|
|
|
|
|
|
base.CopyTo(them);
|
2021-07-12 07:52:57 +00:00
|
|
|
|
2022-11-22 05:51:17 +00:00
|
|
|
otherDifficultyBindable.ReadCurrentFromDifficulty = ReadCurrentFromDifficulty;
|
2021-07-12 07:52:57 +00:00
|
|
|
|
2021-07-12 20:25:33 +00:00
|
|
|
// the following max value copies are only safe as long as these values are effectively constants.
|
2022-11-22 05:51:17 +00:00
|
|
|
otherDifficultyBindable.MaxValue = maxValue;
|
|
|
|
otherDifficultyBindable.ExtendedMaxValue = extendedMaxValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void BindTo(Bindable<float?> them)
|
|
|
|
{
|
|
|
|
if (!(them is DifficultyBindable otherDifficultyBindable))
|
|
|
|
throw new InvalidOperationException($"Cannot bind to a non-{nameof(DifficultyBindable)}.");
|
|
|
|
|
2022-12-06 21:50:14 +00:00
|
|
|
// ensure that MaxValue and ExtendedMaxValue are copied across first before continuing.
|
|
|
|
// not doing so may cause the value of CurrentNumber to be truncated to 10.
|
|
|
|
otherDifficultyBindable.CopyTo(this);
|
|
|
|
|
|
|
|
// set up mutual binding for ExtendedLimits to correctly set the upper bound of CurrentNumber.
|
2021-07-12 20:25:33 +00:00
|
|
|
ExtendedLimits.BindTarget = otherDifficultyBindable.ExtendedLimits;
|
|
|
|
|
2022-12-06 21:50:14 +00:00
|
|
|
// set up mutual binding for CurrentNumber. this must happen after all of the above.
|
2021-07-12 20:25:33 +00:00
|
|
|
CurrentNumber.BindTarget = otherDifficultyBindable.CurrentNumber;
|
2022-11-24 04:57:43 +00:00
|
|
|
|
2022-12-06 21:50:14 +00:00
|
|
|
// finish up the binding by setting up weak references via the base call.
|
|
|
|
// unfortunately this will call `.CopyTo()` again, but fixing that is problematic and messy.
|
2022-11-24 04:57:43 +00:00
|
|
|
base.BindTo(them);
|
2021-07-12 07:52:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 08:33:29 +00:00
|
|
|
public override void UnbindFrom(IUnbindable them)
|
|
|
|
{
|
|
|
|
if (!(them is DifficultyBindable otherDifficultyBindable))
|
|
|
|
throw new InvalidOperationException($"Cannot unbind from a non-{nameof(DifficultyBindable)}.");
|
|
|
|
|
|
|
|
base.UnbindFrom(them);
|
|
|
|
|
|
|
|
CurrentNumber.UnbindFrom(otherDifficultyBindable.CurrentNumber);
|
|
|
|
ExtendedLimits.UnbindFrom(otherDifficultyBindable.ExtendedLimits);
|
|
|
|
}
|
|
|
|
|
2021-08-18 01:16:57 +00:00
|
|
|
protected override Bindable<float?> CreateInstance() => new DifficultyBindable();
|
2021-07-08 07:40:32 +00:00
|
|
|
}
|
|
|
|
}
|