Initial storage changes

This commit is contained in:
Dean Herbert
2021-07-08 14:28:13 +09:00
parent 8d94e8f534
commit 0e4f4a6fde
4 changed files with 40 additions and 123 deletions

View File

@ -13,23 +13,23 @@ namespace osu.Game.Rulesets.Catch.Mods
public class CatchModDifficultyAdjust : ModDifficultyAdjust, IApplicableToBeatmapProcessor public class CatchModDifficultyAdjust : ModDifficultyAdjust, IApplicableToBeatmapProcessor
{ {
[SettingSource("Circle Size", "Override a beatmap's set CS.", FIRST_SETTING_ORDER - 1)] [SettingSource("Circle Size", "Override a beatmap's set CS.", FIRST_SETTING_ORDER - 1)]
public BindableNumber<float> CircleSize { get; } = new BindableFloatWithLimitExtension public Bindable<float?> CircleSize { get; } = new Bindable<float?>
{ {
/*
Precision = 0.1f, Precision = 0.1f,
MinValue = 1, MinValue = 1,
MaxValue = 10, MaxValue = 10,
Default = 5, */
Value = 5,
}; };
[SettingSource("Approach Rate", "Override a beatmap's set AR.", LAST_SETTING_ORDER + 1)] [SettingSource("Approach Rate", "Override a beatmap's set AR.", LAST_SETTING_ORDER + 1)]
public BindableNumber<float> ApproachRate { get; } = new BindableFloatWithLimitExtension public Bindable<float?> ApproachRate { get; } = new Bindable<float?>
{ {
/*
Precision = 0.1f, Precision = 0.1f,
MinValue = 1, MinValue = 1,
MaxValue = 10, MaxValue = 10,
Default = 5, */
Value = 5,
}; };
[SettingSource("Spicy Patterns", "Adjust the patterns as if Hard Rock is enabled.")] [SettingSource("Spicy Patterns", "Adjust the patterns as if Hard Rock is enabled.")]
@ -39,8 +39,9 @@ namespace osu.Game.Rulesets.Catch.Mods
{ {
base.ApplyLimits(extended); base.ApplyLimits(extended);
CircleSize.MaxValue = extended ? 11 : 10; // TODO: reimplement
ApproachRate.MaxValue = extended ? 11 : 10; // CircleSize.MaxValue = extended ? 11 : 10;
// ApproachRate.MaxValue = extended ? 11 : 10;
} }
public override string SettingDescription public override string SettingDescription
@ -61,20 +62,12 @@ namespace osu.Game.Rulesets.Catch.Mods
} }
} }
protected override void TransferSettings(BeatmapDifficulty difficulty)
{
base.TransferSettings(difficulty);
TransferSetting(CircleSize, difficulty.CircleSize);
TransferSetting(ApproachRate, difficulty.ApproachRate);
}
protected override void ApplySettings(BeatmapDifficulty difficulty) protected override void ApplySettings(BeatmapDifficulty difficulty)
{ {
base.ApplySettings(difficulty); base.ApplySettings(difficulty);
ApplySetting(CircleSize, cs => difficulty.CircleSize = cs); if (CircleSize.Value != null) difficulty.CircleSize = CircleSize.Value.Value;
ApplySetting(ApproachRate, ar => difficulty.ApproachRate = ar); if (ApproachRate.Value != null) difficulty.ApproachRate = ApproachRate.Value.Value;
} }
public void ApplyToBeatmapProcessor(IBeatmapProcessor beatmapProcessor) public void ApplyToBeatmapProcessor(IBeatmapProcessor beatmapProcessor)

View File

@ -12,31 +12,32 @@ namespace osu.Game.Rulesets.Osu.Mods
public class OsuModDifficultyAdjust : ModDifficultyAdjust public class OsuModDifficultyAdjust : ModDifficultyAdjust
{ {
[SettingSource("Circle Size", "Override a beatmap's set CS.", FIRST_SETTING_ORDER - 1)] [SettingSource("Circle Size", "Override a beatmap's set CS.", FIRST_SETTING_ORDER - 1)]
public BindableNumber<float> CircleSize { get; } = new BindableFloatWithLimitExtension public Bindable<float?> CircleSize { get; } = new Bindable<float?>
{ {
/*
Precision = 0.1f, Precision = 0.1f,
MinValue = 0, MinValue = 0,
MaxValue = 10, MaxValue = 10,
Default = 5, */
Value = 5,
}; };
[SettingSource("Approach Rate", "Override a beatmap's set AR.", LAST_SETTING_ORDER + 1)] [SettingSource("Approach Rate", "Override a beatmap's set AR.", LAST_SETTING_ORDER + 1)]
public BindableNumber<float> ApproachRate { get; } = new BindableFloatWithLimitExtension public Bindable<float?> ApproachRate { get; } = new Bindable<float?>
{ {
/*
Precision = 0.1f, Precision = 0.1f,
MinValue = 0, MinValue = 0,
MaxValue = 10, MaxValue = 10,
Default = 5, */
Value = 5,
}; };
protected override void ApplyLimits(bool extended) protected override void ApplyLimits(bool extended)
{ {
base.ApplyLimits(extended); base.ApplyLimits(extended);
CircleSize.MaxValue = extended ? 11 : 10; // TODO: reimplement
ApproachRate.MaxValue = extended ? 11 : 10; // CircleSize.MaxValue = extended ? 11 : 10;
// ApproachRate.MaxValue = extended ? 11 : 10;
} }
public override string SettingDescription public override string SettingDescription
@ -55,20 +56,12 @@ namespace osu.Game.Rulesets.Osu.Mods
} }
} }
protected override void TransferSettings(BeatmapDifficulty difficulty)
{
base.TransferSettings(difficulty);
TransferSetting(CircleSize, difficulty.CircleSize);
TransferSetting(ApproachRate, difficulty.ApproachRate);
}
protected override void ApplySettings(BeatmapDifficulty difficulty) protected override void ApplySettings(BeatmapDifficulty difficulty)
{ {
base.ApplySettings(difficulty); base.ApplySettings(difficulty);
ApplySetting(CircleSize, cs => difficulty.CircleSize = cs); if (CircleSize.Value != null) difficulty.CircleSize = CircleSize.Value.Value;
ApplySetting(ApproachRate, ar => difficulty.ApproachRate = ar); if (ApproachRate.Value != null) difficulty.ApproachRate = ApproachRate.Value.Value;
} }
} }
} }

View File

@ -12,13 +12,13 @@ namespace osu.Game.Rulesets.Taiko.Mods
public class TaikoModDifficultyAdjust : ModDifficultyAdjust public class TaikoModDifficultyAdjust : ModDifficultyAdjust
{ {
[SettingSource("Scroll Speed", "Adjust a beatmap's set scroll speed", LAST_SETTING_ORDER + 1)] [SettingSource("Scroll Speed", "Adjust a beatmap's set scroll speed", LAST_SETTING_ORDER + 1)]
public BindableNumber<float> ScrollSpeed { get; } = new BindableFloat public Bindable<float?> ScrollSpeed { get; } = new Bindable<float?>
{ {
/*
Precision = 0.05f, Precision = 0.05f,
MinValue = 0.25f, MinValue = 0.25f,
MaxValue = 4, MaxValue = 4,
Default = 1, */
Value = 1,
}; };
public override string SettingDescription public override string SettingDescription
@ -39,7 +39,7 @@ namespace osu.Game.Rulesets.Taiko.Mods
{ {
base.ApplySettings(difficulty); base.ApplySettings(difficulty);
ApplySetting(ScrollSpeed, scroll => difficulty.SliderMultiplier *= scroll); if (ScrollSpeed.Value != null) difficulty.SliderMultiplier = ScrollSpeed.Value.Value;
} }
} }
} }

View File

@ -1,13 +1,12 @@
// 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 osu.Game.Beatmaps; using System;
using System.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using System; using osu.Game.Beatmaps;
using System.Collections.Generic;
using osu.Game.Configuration; using osu.Game.Configuration;
using System.Linq;
namespace osu.Game.Rulesets.Mods namespace osu.Game.Rulesets.Mods
{ {
@ -34,23 +33,23 @@ namespace osu.Game.Rulesets.Mods
protected const int LAST_SETTING_ORDER = 2; protected const int LAST_SETTING_ORDER = 2;
[SettingSource("HP Drain", "Override a beatmap's set HP.", FIRST_SETTING_ORDER)] [SettingSource("HP Drain", "Override a beatmap's set HP.", FIRST_SETTING_ORDER)]
public BindableNumber<float> DrainRate { get; } = new BindableFloatWithLimitExtension public Bindable<float?> DrainRate { get; } = new Bindable<float?>
{ {
/*
Precision = 0.1f, Precision = 0.1f,
MinValue = 0, MinValue = 0,
MaxValue = 10, MaxValue = 10,
Default = 5, */
Value = 5,
}; };
[SettingSource("Accuracy", "Override a beatmap's set OD.", LAST_SETTING_ORDER)] [SettingSource("Accuracy", "Override a beatmap's set OD.", LAST_SETTING_ORDER)]
public BindableNumber<float> OverallDifficulty { get; } = new BindableFloatWithLimitExtension public Bindable<float?> OverallDifficulty { get; } = new Bindable<float?>
{ {
/*
Precision = 0.1f, Precision = 0.1f,
MinValue = 0, MinValue = 0,
MaxValue = 10, MaxValue = 10,
Default = 5, */
Value = 5,
}; };
[SettingSource("Extended Limits", "Adjust difficulty beyond sane limits.")] [SettingSource("Extended Limits", "Adjust difficulty beyond sane limits.")]
@ -67,8 +66,9 @@ namespace osu.Game.Rulesets.Mods
/// <param name="extended">Whether limits should extend beyond sane ranges.</param> /// <param name="extended">Whether limits should extend beyond sane ranges.</param>
protected virtual void ApplyLimits(bool extended) protected virtual void ApplyLimits(bool extended)
{ {
DrainRate.MaxValue = extended ? 11 : 10; // TODO: reimplement
OverallDifficulty.MaxValue = extended ? 11 : 10; // DrainRate.MaxValue = extended ? 11 : 10;
// OverallDifficulty.MaxValue = extended ? 11 : 10;
} }
public override string SettingDescription public override string SettingDescription
@ -86,89 +86,20 @@ namespace osu.Game.Rulesets.Mods
} }
} }
private BeatmapDifficulty difficulty;
public void ReadFromDifficulty(BeatmapDifficulty difficulty) public void ReadFromDifficulty(BeatmapDifficulty difficulty)
{ {
if (this.difficulty == null || this.difficulty.ID != difficulty.ID)
{
TransferSettings(difficulty);
this.difficulty = difficulty;
}
} }
public void ApplyToDifficulty(BeatmapDifficulty difficulty) => ApplySettings(difficulty); public void ApplyToDifficulty(BeatmapDifficulty difficulty) => ApplySettings(difficulty);
/// <summary>
/// Transfer initial settings from the beatmap to settings.
/// </summary>
/// <param name="difficulty">The beatmap's initial values.</param>
protected virtual void TransferSettings(BeatmapDifficulty difficulty)
{
TransferSetting(DrainRate, difficulty.DrainRate);
TransferSetting(OverallDifficulty, difficulty.OverallDifficulty);
}
private readonly Dictionary<IBindable, bool> userChangedSettings = new Dictionary<IBindable, bool>();
/// <summary>
/// Transfer a setting from <see cref="BeatmapDifficulty"/> to a configuration bindable.
/// Only performs the transfer if the user is not currently overriding.
/// </summary>
protected void TransferSetting<T>(BindableNumber<T> bindable, T beatmapDefault)
where T : struct, IComparable<T>, IConvertible, IEquatable<T>
{
bindable.UnbindEvents();
userChangedSettings.TryAdd(bindable, false);
bindable.Default = beatmapDefault;
// users generally choose a difficulty setting and want it to stick across multiple beatmap changes.
// we only want to value transfer if the user hasn't changed the value previously.
if (!userChangedSettings[bindable])
bindable.Value = beatmapDefault;
bindable.ValueChanged += _ => userChangedSettings[bindable] = !bindable.IsDefault;
}
internal override void CopyAdjustedSetting(IBindable target, object source)
{
// if the value is non-bindable, it's presumably coming from an external source (like the API) - therefore presume it is not default.
// if the value is bindable, defer to the source's IsDefault to be able to tell.
userChangedSettings[target] = !(source is IBindable bindableSource) || !bindableSource.IsDefault;
base.CopyAdjustedSetting(target, source);
}
/// <summary>
/// Applies a setting from a configuration bindable using <paramref name="applyFunc"/>, if it has been changed by the user.
/// </summary>
protected void ApplySetting<T>(BindableNumber<T> setting, Action<T> applyFunc)
where T : struct, IComparable<T>, IConvertible, IEquatable<T>
{
if (userChangedSettings.TryGetValue(setting, out bool userChangedSetting) && userChangedSetting)
applyFunc.Invoke(setting.Value);
}
/// <summary> /// <summary>
/// Apply all custom settings to the provided beatmap. /// Apply all custom settings to the provided beatmap.
/// </summary> /// </summary>
/// <param name="difficulty">The beatmap to have settings applied.</param> /// <param name="difficulty">The beatmap to have settings applied.</param>
protected virtual void ApplySettings(BeatmapDifficulty difficulty) protected virtual void ApplySettings(BeatmapDifficulty difficulty)
{ {
ApplySetting(DrainRate, dr => difficulty.DrainRate = dr); if (DrainRate.Value != null) difficulty.DrainRate = DrainRate.Value.Value;
ApplySetting(OverallDifficulty, od => difficulty.OverallDifficulty = od); if (OverallDifficulty.Value != null) difficulty.OverallDifficulty = OverallDifficulty.Value.Value;
}
public override void ResetSettingsToDefaults()
{
base.ResetSettingsToDefaults();
if (difficulty != null)
{
// base implementation potentially overwrite modified defaults that came from a beatmap selection.
TransferSettings(difficulty);
}
} }
/// <summary> /// <summary>