osu/osu.Game/Configuration/ModSettingChangeTracker.cs

53 lines
1.6 KiB
C#
Raw Normal View History

// 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 System.Collections.Generic;
using System.Linq;
using osu.Game.Overlays.Settings;
using osu.Game.Rulesets.Mods;
namespace osu.Game.Configuration
{
2021-02-10 11:11:36 +00:00
/// <summary>
/// A helper class for tracking changes to the settings of a set of <see cref="Mod"/>s.
/// </summary>
/// <remarks>
/// Ensure to dispose when usage is finished.
/// </remarks>
public class ModSettingChangeTracker : IDisposable
{
2021-02-10 11:11:36 +00:00
/// <summary>
/// Notifies that the setting of a <see cref="Mod"/> has changed.
/// </summary>
public Action<Mod> SettingChanged;
2021-02-10 11:11:36 +00:00
private readonly List<ISettingsItem> settings = new List<ISettingsItem>();
2021-02-10 11:11:36 +00:00
/// <summary>
/// Creates a new <see cref="ModSettingChangeTracker"/> for a set of <see cref="Mod"/>s.
/// </summary>
/// <param name="mods">The set of <see cref="Mod"/>s whose settings need to be tracked.</param>
public ModSettingChangeTracker(IEnumerable<Mod> mods)
{
foreach (var mod in mods)
{
foreach (var setting in mod.CreateSettingsControls().OfType<ISettingsItem>())
{
setting.SettingChanged += () => SettingChanged?.Invoke(mod);
2021-02-10 11:11:36 +00:00
settings.Add(setting);
}
}
}
public void Dispose()
{
SettingChanged = null;
2021-02-10 11:11:36 +00:00
foreach (var r in settings)
r.Dispose();
2021-02-10 11:11:36 +00:00
settings.Clear();
}
}
}