1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 01:02:56 +08:00

Allow CreateSettingsControls to work with all bindables in target class

This commit is contained in:
Dean Herbert 2021-03-11 12:43:04 +09:00
parent b9b095ee75
commit a33ffd56b8

View File

@ -7,6 +7,7 @@ using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using osu.Framework.Bindables;
using osu.Framework.Extensions.TypeExtensions;
using osu.Framework.Graphics;
using osu.Framework.Localisation;
using osu.Game.Overlays.Settings;
@ -61,9 +62,13 @@ namespace osu.Game.Configuration
public static class SettingSourceExtensions
{
public static IEnumerable<Drawable> CreateSettingsControls(this object obj)
public static IEnumerable<Drawable> CreateSettingsControls(this object obj) => createSettingsControls(obj, obj.GetOrderedSettingsSourceProperties());
public static IEnumerable<Drawable> CreateSettingsControlsFromAllBindables(this object obj) => createSettingsControls(obj, obj.GetSettingsSourcePropertiesFromBindables());
private static IEnumerable<Drawable> createSettingsControls(object obj, IEnumerable<(SettingSourceAttribute, PropertyInfo)> sourceAttribs)
{
foreach (var (attr, property) in obj.GetOrderedSettingsSourceProperties())
foreach (var (attr, property) in sourceAttribs)
{
object value = property.GetValue(obj);
@ -139,6 +144,30 @@ namespace osu.Game.Configuration
}
}
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourcePropertiesFromBindables(this object obj)
{
HashSet<string> handledProperties = new HashSet<string>();
// reverse and de-dupe properties to surface base class settings to the top of return order.
foreach (var type in obj.GetType().EnumerateBaseTypes().Reverse())
{
foreach (var property in type.GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
if (handledProperties.Contains(property.Name))
continue;
handledProperties.Add(property.Name);
if (typeof(IBindable).IsAssignableFrom(property.PropertyType))
{
var val = property.GetValue(obj);
string description = (val as IHasDescription)?.Description ?? string.Empty;
yield return (new SettingSourceAttribute(property.Name, description), property);
}
}
}
}
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourceProperties(this object obj)
{
foreach (var property in obj.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))