mirror of
https://github.com/ppy/osu.git
synced 2025-03-23 16:27:20 +08:00
Register/Unregister -> BeginTracking/StopTracking and add exceptions
This commit is contained in:
parent
dee298c395
commit
a94ea7025e
@ -118,35 +118,48 @@ namespace osu.Game.Overlays
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(FrameworkConfigManager frameworkConfig)
|
||||
{
|
||||
Register(this, frameworkConfig);
|
||||
BeginTracking(this, frameworkConfig);
|
||||
}
|
||||
|
||||
private readonly Dictionary<(IDisposable, IConfigManager), TrackedSettings> trackedConfigManagers = new Dictionary<(IDisposable, IConfigManager), TrackedSettings>();
|
||||
private readonly Dictionary<(object, IConfigManager), TrackedSettings> trackedConfigManagers = new Dictionary<(object, IConfigManager), TrackedSettings>();
|
||||
|
||||
public void Register(IDisposable source, ITrackableConfigManager configManager)
|
||||
/// <summary>
|
||||
/// Registers a <see cref="ConfigManager{T}"/> to have its settings tracked by this <see cref="OnScreenDisplay"/>.
|
||||
/// </summary>
|
||||
/// <param name="source">The object that is registering the <see cref="ConfigManager{T}"/> to be tracked.</param>
|
||||
/// <param name="configManager">The <see cref="ConfigManager{T}"/> to be tracked.</param>
|
||||
/// <exception cref="ArgumentNullException">If <paramref name="configManager"/> is null.</exception>
|
||||
/// <exception cref="InvalidOperationException">If <paramref name="configManager"/> is already being tracked from the same <paramref name="source"/>.</exception>
|
||||
public void BeginTracking(object source, ITrackableConfigManager configManager)
|
||||
{
|
||||
if (configManager == null) throw new ArgumentNullException(nameof(configManager));
|
||||
|
||||
if (trackedConfigManagers.ContainsKey((source, configManager)))
|
||||
return;
|
||||
throw new InvalidOperationException($"{nameof(configManager)} is already registered.");
|
||||
|
||||
var trackedSettings = configManager.CreateTrackedSettings();
|
||||
if (trackedSettings == null)
|
||||
return;
|
||||
|
||||
configManager.LoadInto(trackedSettings);
|
||||
|
||||
trackedSettings.SettingChanged += display;
|
||||
|
||||
trackedConfigManagers.Add((source, configManager), trackedSettings);
|
||||
}
|
||||
|
||||
public void Unregister(IDisposable source, ITrackableConfigManager configManager)
|
||||
/// <summary>
|
||||
/// Unregisters a <see cref="ConfigManager{T}"/> from having its settings tracked by this <see cref="OnScreenDisplay"/>.
|
||||
/// </summary>
|
||||
/// <param name="source">The object that registered the <see cref="ConfigManager{T}"/> to be tracked.</param>
|
||||
/// <param name="configManager">The <see cref="ConfigManager{T}"/> that is being tracked.</param>
|
||||
/// <exception cref="ArgumentNullException">If <paramref name="configManager"/> is null.</exception>
|
||||
/// <exception cref="InvalidOperationException">If <paramref name="configManager"/> is not being tracked from the same <see cref="source"/>.</exception>
|
||||
public void StopTracking(object source, ITrackableConfigManager configManager)
|
||||
{
|
||||
if (configManager == null) throw new ArgumentNullException(nameof(configManager));
|
||||
|
||||
if (!trackedConfigManagers.TryGetValue((source, configManager), out var existing))
|
||||
return;
|
||||
throw new InvalidOperationException($"{nameof(configManager)} is not registered.");
|
||||
|
||||
existing.Unload();
|
||||
existing.SettingChanged -= display;
|
||||
|
@ -97,7 +97,7 @@ namespace osu.Game.Rulesets.UI
|
||||
if (rulesetConfig != null)
|
||||
{
|
||||
dependencies.Cache(rulesetConfig);
|
||||
onScreenDisplay?.Register(this, rulesetConfig);
|
||||
onScreenDisplay?.BeginTracking(this, rulesetConfig);
|
||||
}
|
||||
}
|
||||
|
||||
@ -146,7 +146,7 @@ namespace osu.Game.Rulesets.UI
|
||||
|
||||
if (rulesetConfig != null)
|
||||
{
|
||||
onScreenDisplay?.Unregister(this, rulesetConfig);
|
||||
onScreenDisplay?.StopTracking(this, rulesetConfig);
|
||||
rulesetConfig = null;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user