A game framework written with osu! in mind.
at master 47 lines 1.8 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using osu.Framework.Bindables; 6 7namespace osu.Framework.Configuration.Tracking 8{ 9 /// <summary> 10 /// A singular tracked setting. 11 /// </summary> 12 /// <typeparam name="TValue">The type of the tracked value.</typeparam> 13 public abstract class TrackedSetting<TValue> : ITrackedSetting 14 { 15 public event Action<SettingDescription> SettingChanged; 16 17 private readonly object setting; 18 private readonly Func<TValue, SettingDescription> generateDescription; 19 20 private Bindable<TValue> bindable; 21 22 /// <summary> 23 /// Constructs a new <see cref="TrackedSetting{TValue}"/>. 24 /// </summary> 25 /// <param name="setting">The config setting to be tracked.</param> 26 /// <param name="generateDescription">A function that generates the description for the setting, invoked every time the value changes.</param> 27 protected TrackedSetting(object setting, Func<TValue, SettingDescription> generateDescription) 28 { 29 this.setting = setting; 30 this.generateDescription = generateDescription; 31 } 32 33 public void LoadFrom<TLookup>(ConfigManager<TLookup> configManager) 34 where TLookup : struct, Enum 35 { 36 bindable = configManager.GetBindable<TValue>((TLookup)setting); 37 bindable.ValueChanged += displaySetting; 38 } 39 40 public void Unload() 41 { 42 bindable.ValueChanged -= displaySetting; 43 } 44 45 private void displaySetting(ValueChangedEvent<TValue> args) => SettingChanged?.Invoke(generateDescription(args.NewValue)); 46 } 47}