2019-12-06 08:09:24 +00:00
|
|
|
// 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;
|
2020-02-10 04:11:37 +00:00
|
|
|
using System.Linq;
|
2019-12-06 08:09:24 +00:00
|
|
|
using System.Reflection;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Game.Overlays.Settings;
|
|
|
|
|
|
|
|
namespace osu.Game.Configuration
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// An attribute to mark a bindable as being exposed to the user via settings controls.
|
|
|
|
/// Can be used in conjunction with <see cref="SettingSourceExtensions.CreateSettingsControls"/> to automatically create UI controls.
|
|
|
|
/// </summary>
|
2020-02-11 00:21:49 +00:00
|
|
|
/// <remarks>
|
2020-02-12 23:55:16 +00:00
|
|
|
/// All controls with <see cref="OrderPosition"/> set will be placed first in ascending order.
|
|
|
|
/// All controls with no <see cref="OrderPosition"/> will come afterward in default order.
|
2020-02-11 00:21:49 +00:00
|
|
|
/// </remarks>
|
2019-12-06 08:09:24 +00:00
|
|
|
[MeansImplicitUse]
|
|
|
|
[AttributeUsage(AttributeTargets.Property)]
|
|
|
|
public class SettingSourceAttribute : Attribute
|
|
|
|
{
|
|
|
|
public string Label { get; }
|
|
|
|
|
|
|
|
public string Description { get; }
|
|
|
|
|
2020-02-12 05:52:22 +00:00
|
|
|
public int? OrderPosition { get; }
|
2020-02-10 04:11:37 +00:00
|
|
|
|
2020-02-12 05:52:22 +00:00
|
|
|
public SettingSourceAttribute(string label, string description = null)
|
2019-12-06 08:09:24 +00:00
|
|
|
{
|
|
|
|
Label = label ?? string.Empty;
|
|
|
|
Description = description ?? string.Empty;
|
2020-02-12 05:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public SettingSourceAttribute(string label, string description, int orderPosition)
|
|
|
|
: this(label, description)
|
|
|
|
{
|
2020-02-10 04:11:37 +00:00
|
|
|
OrderPosition = orderPosition;
|
2019-12-06 08:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class SettingSourceExtensions
|
|
|
|
{
|
|
|
|
public static IEnumerable<Drawable> CreateSettingsControls(this object obj)
|
|
|
|
{
|
2020-02-10 04:11:37 +00:00
|
|
|
foreach (var (attr, property) in obj.GetOrderedSettingsSourceProperties())
|
2019-12-06 08:09:24 +00:00
|
|
|
{
|
2020-01-17 04:27:47 +00:00
|
|
|
object value = property.GetValue(obj);
|
2019-12-06 11:14:06 +00:00
|
|
|
|
2020-01-17 04:27:47 +00:00
|
|
|
switch (value)
|
2019-12-06 08:09:24 +00:00
|
|
|
{
|
|
|
|
case BindableNumber<float> bNumber:
|
|
|
|
yield return new SettingsSlider<float>
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
2020-02-06 10:43:33 +00:00
|
|
|
Bindable = bNumber,
|
|
|
|
KeyboardStep = 0.1f,
|
2019-12-06 08:09:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BindableNumber<double> bNumber:
|
|
|
|
yield return new SettingsSlider<double>
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
2020-02-06 10:43:33 +00:00
|
|
|
Bindable = bNumber,
|
|
|
|
KeyboardStep = 0.1f,
|
2019-12-06 08:09:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BindableNumber<int> bNumber:
|
|
|
|
yield return new SettingsSlider<int>
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
|
|
|
Bindable = bNumber
|
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Bindable<bool> bBool:
|
|
|
|
yield return new SettingsCheckbox
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
|
|
|
Bindable = bBool
|
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
2019-12-06 08:49:41 +00:00
|
|
|
|
|
|
|
case Bindable<string> bString:
|
|
|
|
yield return new SettingsTextBox
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
|
|
|
Bindable = bString
|
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IBindable bindable:
|
|
|
|
var dropdownType = typeof(SettingsEnumDropdown<>).MakeGenericType(bindable.GetType().GetGenericArguments()[0]);
|
|
|
|
var dropdown = (Drawable)Activator.CreateInstance(dropdownType);
|
|
|
|
|
2020-02-28 01:30:06 +00:00
|
|
|
dropdownType.GetProperty(nameof(SettingsDropdown<object>.LabelText))?.SetValue(dropdown, attr.Label);
|
|
|
|
dropdownType.GetProperty(nameof(SettingsDropdown<object>.Bindable))?.SetValue(dropdown, bindable);
|
2019-12-06 08:49:41 +00:00
|
|
|
|
|
|
|
yield return dropdown;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-01-17 04:27:47 +00:00
|
|
|
throw new InvalidOperationException($"{nameof(SettingSourceAttribute)} was attached to an unsupported type ({value})");
|
2019-12-06 08:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 04:27:47 +00:00
|
|
|
|
|
|
|
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourceProperties(this object obj)
|
|
|
|
{
|
|
|
|
foreach (var property in obj.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
|
|
|
|
{
|
|
|
|
var attr = property.GetCustomAttribute<SettingSourceAttribute>(true);
|
|
|
|
|
|
|
|
if (attr == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
yield return (attr, property);
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 04:11:37 +00:00
|
|
|
|
|
|
|
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetOrderedSettingsSourceProperties(this object obj)
|
|
|
|
{
|
|
|
|
var original = obj.GetSettingsSourceProperties();
|
|
|
|
|
2020-02-12 05:52:22 +00:00
|
|
|
var orderedRelative = original.Where(attr => attr.Item1.OrderPosition != null).OrderBy(attr => attr.Item1.OrderPosition);
|
2020-02-11 00:21:49 +00:00
|
|
|
var unordered = original.Except(orderedRelative);
|
2020-02-10 04:11:37 +00:00
|
|
|
|
2020-02-11 00:21:49 +00:00
|
|
|
return orderedRelative.Concat(unordered);
|
2020-02-10 04:11:37 +00:00
|
|
|
}
|
2019-12-06 08:09:24 +00:00
|
|
|
}
|
|
|
|
}
|