1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 02:43:19 +08:00

Add helper class for tracking changes to mod settings

This commit is contained in:
smoogipoo 2021-02-10 19:39:47 +09:00
parent d91e17542a
commit 393cd6c74a
2 changed files with 37 additions and 21 deletions

View File

@ -9,6 +9,7 @@ using JetBrains.Annotations;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Overlays.Settings; using osu.Game.Overlays.Settings;
using osu.Game.Rulesets.Mods;
namespace osu.Game.Configuration namespace osu.Game.Configuration
{ {
@ -140,4 +141,30 @@ namespace osu.Game.Configuration
return orderedRelative.Concat(unordered); return orderedRelative.Concat(unordered);
} }
} }
public class ModSettingChangeTracker : IDisposable
{
public Action<Mod> SettingChanged;
private readonly List<ISettingsItem> references = new List<ISettingsItem>();
public ModSettingChangeTracker(IEnumerable<Mod> mods)
{
foreach (var mod in mods)
{
foreach (var setting in mod.CreateSettingsControls().OfType<ISettingsItem>())
{
setting.SettingChanged += () => SettingChanged?.Invoke(mod);
references.Add(setting);
}
}
}
public void Dispose()
{
foreach (var r in references)
r.Dispose();
references.Clear();
}
}
} }

View File

@ -18,7 +18,6 @@ using System.Threading;
using osu.Framework.Threading; using osu.Framework.Threading;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Overlays.Settings;
using osu.Game.Rulesets; using osu.Game.Rulesets;
namespace osu.Game.Screens.Select.Details namespace osu.Game.Screens.Select.Details
@ -83,32 +82,22 @@ namespace osu.Game.Screens.Select.Details
mods.BindValueChanged(modsChanged, true); mods.BindValueChanged(modsChanged, true);
} }
private readonly List<ISettingsItem> references = new List<ISettingsItem>(); private ModSettingChangeTracker settingChangeTracker;
private ScheduledDelegate debouncedStatisticsUpdate;
private void modsChanged(ValueChangedEvent<IReadOnlyList<Mod>> mods) private void modsChanged(ValueChangedEvent<IReadOnlyList<Mod>> mods)
{ {
// TODO: find a more permanent solution for this if/when it is needed in other components. settingChangeTracker?.Dispose();
// this is generating drawables for the only purpose of storing bindable references.
foreach (var r in references)
r.Dispose();
references.Clear(); settingChangeTracker = new ModSettingChangeTracker(mods.NewValue);
settingChangeTracker.SettingChanged += m =>
ScheduledDelegate debounce = null;
foreach (var mod in mods.NewValue.OfType<IApplicableToDifficulty>())
{ {
foreach (var setting in mod.CreateSettingsControls().OfType<ISettingsItem>()) if (!(m is IApplicableToDifficulty))
{ return;
setting.SettingChanged += () =>
{
debounce?.Cancel();
debounce = Scheduler.AddDelayed(updateStatistics, 100);
};
references.Add(setting); debouncedStatisticsUpdate?.Cancel();
} debouncedStatisticsUpdate = Scheduler.AddDelayed(updateStatistics, 100);
} };
updateStatistics(); updateStatistics();
} }