1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 10:43:04 +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.Graphics;
using osu.Game.Overlays.Settings;
using osu.Game.Rulesets.Mods;
namespace osu.Game.Configuration
{
@ -140,4 +141,30 @@ namespace osu.Game.Configuration
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.Utils;
using osu.Game.Configuration;
using osu.Game.Overlays.Settings;
using osu.Game.Rulesets;
namespace osu.Game.Screens.Select.Details
@ -83,32 +82,22 @@ namespace osu.Game.Screens.Select.Details
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)
{
// TODO: find a more permanent solution for this if/when it is needed in other components.
// this is generating drawables for the only purpose of storing bindable references.
foreach (var r in references)
r.Dispose();
settingChangeTracker?.Dispose();
references.Clear();
ScheduledDelegate debounce = null;
foreach (var mod in mods.NewValue.OfType<IApplicableToDifficulty>())
settingChangeTracker = new ModSettingChangeTracker(mods.NewValue);
settingChangeTracker.SettingChanged += m =>
{
foreach (var setting in mod.CreateSettingsControls().OfType<ISettingsItem>())
{
setting.SettingChanged += () =>
{
debounce?.Cancel();
debounce = Scheduler.AddDelayed(updateStatistics, 100);
};
if (!(m is IApplicableToDifficulty))
return;
references.Add(setting);
}
}
debouncedStatisticsUpdate?.Cancel();
debouncedStatisticsUpdate = Scheduler.AddDelayed(updateStatistics, 100);
};
updateStatistics();
}