1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 08:32:54 +08:00
osu-lazer/osu.Game/Configuration/ModSettingChangeTracker.cs

55 lines
1.7 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.
2022-06-17 15:37:17 +08:00
#nullable disable
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 19:11:36 +08: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 19:11:36 +08:00
/// <summary>
/// Notifies that the setting of a <see cref="Mod"/> has changed.
/// </summary>
public Action<Mod> SettingChanged;
2021-02-10 19:11:36 +08:00
private readonly List<ISettingsItem> settings = new List<ISettingsItem>();
2021-02-10 19:11:36 +08: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 19:11:36 +08:00
settings.Add(setting);
}
}
}
public void Dispose()
{
SettingChanged = null;
2021-02-10 19:11:36 +08:00
foreach (var r in settings)
r.Dispose();
2021-02-10 19:11:36 +08:00
settings.Clear();
}
}
}