2019-12-06 16:09:24 +08: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;
|
2021-09-08 22:07:53 +08:00
|
|
|
using System.Collections.Concurrent;
|
2019-12-06 16:09:24 +08:00
|
|
|
using System.Collections.Generic;
|
2022-03-15 13:53:50 +08:00
|
|
|
using System.Diagnostics;
|
2020-02-10 12:11:37 +08:00
|
|
|
using System.Linq;
|
2019-12-06 16:09:24 +08:00
|
|
|
using System.Reflection;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Framework.Bindables;
|
2021-04-28 01:46:34 +08:00
|
|
|
using osu.Framework.Extensions.TypeExtensions;
|
2019-12-06 16:09:24 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-02-25 16:46:35 +08:00
|
|
|
using osu.Framework.Localisation;
|
2021-06-23 04:32:45 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2019-12-06 16:09:24 +08:00
|
|
|
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 08:21:49 +08:00
|
|
|
/// <remarks>
|
2020-02-13 07:55:16 +08: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 08:21:49 +08:00
|
|
|
/// </remarks>
|
2019-12-06 16:09:24 +08:00
|
|
|
[MeansImplicitUse]
|
|
|
|
[AttributeUsage(AttributeTargets.Property)]
|
2021-02-27 22:30:05 +08:00
|
|
|
public class SettingSourceAttribute : Attribute, IComparable<SettingSourceAttribute>
|
2019-12-06 16:09:24 +08:00
|
|
|
{
|
2021-02-25 16:46:35 +08:00
|
|
|
public LocalisableString Label { get; }
|
2019-12-06 16:09:24 +08:00
|
|
|
|
2021-06-26 01:10:04 +08:00
|
|
|
public LocalisableString Description { get; }
|
2019-12-06 16:09:24 +08:00
|
|
|
|
2020-02-12 13:52:22 +08:00
|
|
|
public int? OrderPosition { get; }
|
2020-02-10 12:11:37 +08:00
|
|
|
|
2021-04-28 01:46:34 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The type of the settings control which handles this setting source.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2021-04-28 02:54:42 +08:00
|
|
|
/// Must be a type deriving <see cref="SettingsItem{T}"/> with a public parameterless constructor.
|
2021-04-28 01:46:34 +08:00
|
|
|
/// </remarks>
|
|
|
|
public Type? SettingControlType { get; set; }
|
|
|
|
|
|
|
|
public SettingSourceAttribute(string? label, string? description = null)
|
2019-12-06 16:09:24 +08:00
|
|
|
{
|
|
|
|
Label = label ?? string.Empty;
|
|
|
|
Description = description ?? string.Empty;
|
2020-02-12 13:52:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public SettingSourceAttribute(string label, string description, int orderPosition)
|
|
|
|
: this(label, description)
|
|
|
|
{
|
2020-02-10 12:11:37 +08:00
|
|
|
OrderPosition = orderPosition;
|
2019-12-06 16:09:24 +08:00
|
|
|
}
|
2021-02-27 22:30:05 +08:00
|
|
|
|
|
|
|
public int CompareTo(SettingSourceAttribute other)
|
|
|
|
{
|
|
|
|
if (OrderPosition == other.OrderPosition)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// unordered items come last (are greater than any ordered items).
|
|
|
|
if (OrderPosition == null)
|
|
|
|
return 1;
|
|
|
|
if (other.OrderPosition == null)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// ordered items are sorted by the order value.
|
|
|
|
return OrderPosition.Value.CompareTo(other.OrderPosition);
|
|
|
|
}
|
2019-12-06 16:09:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class SettingSourceExtensions
|
|
|
|
{
|
|
|
|
public static IEnumerable<Drawable> CreateSettingsControls(this object obj)
|
|
|
|
{
|
2020-02-10 12:11:37 +08:00
|
|
|
foreach (var (attr, property) in obj.GetOrderedSettingsSourceProperties())
|
2019-12-06 16:09:24 +08:00
|
|
|
{
|
2020-01-17 12:27:47 +08:00
|
|
|
object value = property.GetValue(obj);
|
2019-12-06 19:14:06 +08:00
|
|
|
|
2021-04-28 01:46:34 +08:00
|
|
|
if (attr.SettingControlType != null)
|
|
|
|
{
|
|
|
|
var controlType = attr.SettingControlType;
|
|
|
|
if (controlType.EnumerateBaseTypes().All(t => !t.IsGenericType || t.GetGenericTypeDefinition() != typeof(SettingsItem<>)))
|
|
|
|
throw new InvalidOperationException($"{nameof(SettingSourceAttribute)} had an unsupported custom control type ({controlType.ReadableName()})");
|
|
|
|
|
|
|
|
var control = (Drawable)Activator.CreateInstance(controlType);
|
2022-04-01 15:00:51 +08:00
|
|
|
controlType.GetProperty(nameof(SettingsItem<object>.SettingSourceObject))?.SetValue(control, obj);
|
2021-04-28 01:46:34 +08:00
|
|
|
controlType.GetProperty(nameof(SettingsItem<object>.LabelText))?.SetValue(control, attr.Label);
|
|
|
|
controlType.GetProperty(nameof(SettingsItem<object>.TooltipText))?.SetValue(control, attr.Description);
|
|
|
|
controlType.GetProperty(nameof(SettingsItem<object>.Current))?.SetValue(control, value);
|
|
|
|
|
|
|
|
yield return control;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:27:47 +08:00
|
|
|
switch (value)
|
2019-12-06 16:09:24 +08:00
|
|
|
{
|
|
|
|
case BindableNumber<float> bNumber:
|
|
|
|
yield return new SettingsSlider<float>
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
2021-02-11 15:38:17 +08:00
|
|
|
TooltipText = attr.Description,
|
2020-10-06 16:18:41 +08:00
|
|
|
Current = bNumber,
|
2020-02-06 18:43:33 +08:00
|
|
|
KeyboardStep = 0.1f,
|
2019-12-06 16:09:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BindableNumber<double> bNumber:
|
|
|
|
yield return new SettingsSlider<double>
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
2021-02-11 15:38:17 +08:00
|
|
|
TooltipText = attr.Description,
|
2020-10-06 16:18:41 +08:00
|
|
|
Current = bNumber,
|
2020-02-06 18:43:33 +08:00
|
|
|
KeyboardStep = 0.1f,
|
2019-12-06 16:09:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BindableNumber<int> bNumber:
|
|
|
|
yield return new SettingsSlider<int>
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
2021-02-11 15:38:17 +08:00
|
|
|
TooltipText = attr.Description,
|
2020-10-06 16:18:41 +08:00
|
|
|
Current = bNumber
|
2019-12-06 16:09:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Bindable<bool> bBool:
|
|
|
|
yield return new SettingsCheckbox
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
2021-02-11 15:38:17 +08:00
|
|
|
TooltipText = attr.Description,
|
2020-10-06 16:18:41 +08:00
|
|
|
Current = bBool
|
2019-12-06 16:09:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
2019-12-06 16:49:41 +08:00
|
|
|
|
|
|
|
case Bindable<string> bString:
|
|
|
|
yield return new SettingsTextBox
|
|
|
|
{
|
|
|
|
LabelText = attr.Label,
|
2021-02-11 15:38:17 +08:00
|
|
|
TooltipText = attr.Description,
|
2020-10-06 16:18:41 +08:00
|
|
|
Current = bString
|
2019-12-06 16:49:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IBindable bindable:
|
2021-06-23 04:32:45 +08:00
|
|
|
var dropdownType = typeof(ModSettingsEnumDropdown<>).MakeGenericType(bindable.GetType().GetGenericArguments()[0]);
|
2019-12-06 16:49:41 +08:00
|
|
|
var dropdown = (Drawable)Activator.CreateInstance(dropdownType);
|
|
|
|
|
2020-02-28 09:30:06 +08:00
|
|
|
dropdownType.GetProperty(nameof(SettingsDropdown<object>.LabelText))?.SetValue(dropdown, attr.Label);
|
2021-02-11 15:38:17 +08:00
|
|
|
dropdownType.GetProperty(nameof(SettingsDropdown<object>.TooltipText))?.SetValue(dropdown, attr.Description);
|
2020-10-06 16:18:41 +08:00
|
|
|
dropdownType.GetProperty(nameof(SettingsDropdown<object>.Current))?.SetValue(dropdown, bindable);
|
2019-12-06 16:49:41 +08:00
|
|
|
|
|
|
|
yield return dropdown;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-01-17 12:27:47 +08:00
|
|
|
throw new InvalidOperationException($"{nameof(SettingSourceAttribute)} was attached to an unsupported type ({value})");
|
2019-12-06 16:09:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 12:27:47 +08:00
|
|
|
|
2021-09-09 13:56:39 +08:00
|
|
|
private static readonly ConcurrentDictionary<Type, (SettingSourceAttribute, PropertyInfo)[]> property_info_cache = new ConcurrentDictionary<Type, (SettingSourceAttribute, PropertyInfo)[]>();
|
2021-09-08 22:07:53 +08:00
|
|
|
|
2022-03-15 13:53:50 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the underlying value of the given mod setting object.
|
|
|
|
/// Can be used for serialization and equality comparison purposes.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="setting">A <see cref="SettingSourceAttribute"/> bindable.</param>
|
|
|
|
public static object GetUnderlyingSettingValue(this object setting)
|
|
|
|
{
|
|
|
|
switch (setting)
|
|
|
|
{
|
|
|
|
case Bindable<double> d:
|
|
|
|
return d.Value;
|
|
|
|
|
|
|
|
case Bindable<int> i:
|
|
|
|
return i.Value;
|
|
|
|
|
|
|
|
case Bindable<float> f:
|
|
|
|
return f.Value;
|
|
|
|
|
|
|
|
case Bindable<bool> b:
|
|
|
|
return b.Value;
|
|
|
|
|
|
|
|
case IBindable u:
|
|
|
|
// An unknown (e.g. enum) generic type.
|
|
|
|
var valueMethod = u.GetType().GetProperty(nameof(IBindable<int>.Value));
|
|
|
|
Debug.Assert(valueMethod != null);
|
|
|
|
return valueMethod.GetValue(u);
|
|
|
|
|
|
|
|
default:
|
|
|
|
// fall back for non-bindable cases.
|
|
|
|
return setting;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 22:07:53 +08:00
|
|
|
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourceProperties(this object obj)
|
|
|
|
{
|
|
|
|
var type = obj.GetType();
|
|
|
|
|
|
|
|
if (!property_info_cache.TryGetValue(type, out var properties))
|
2021-09-09 13:56:39 +08:00
|
|
|
property_info_cache[type] = properties = getSettingsSourceProperties(type).ToArray();
|
2021-09-08 22:07:53 +08:00
|
|
|
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static IEnumerable<(SettingSourceAttribute, PropertyInfo)> getSettingsSourceProperties(Type type)
|
|
|
|
{
|
|
|
|
foreach (var property in type.GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
|
|
|
|
{
|
|
|
|
var attr = property.GetCustomAttribute<SettingSourceAttribute>(true);
|
|
|
|
|
|
|
|
if (attr == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
yield return (attr, property);
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 12:11:37 +08:00
|
|
|
|
2021-02-27 22:30:05 +08:00
|
|
|
public static ICollection<(SettingSourceAttribute, PropertyInfo)> GetOrderedSettingsSourceProperties(this object obj)
|
2021-02-27 22:46:18 +08:00
|
|
|
=> obj.GetSettingsSourceProperties()
|
|
|
|
.OrderBy(attr => attr.Item1)
|
|
|
|
.ToArray();
|
2021-06-23 04:32:45 +08:00
|
|
|
|
|
|
|
private class ModSettingsEnumDropdown<T> : SettingsEnumDropdown<T>
|
|
|
|
where T : struct, Enum
|
|
|
|
{
|
|
|
|
protected override OsuDropdown<T> CreateDropdown() => new ModDropdownControl();
|
|
|
|
|
|
|
|
private class ModDropdownControl : DropdownControl
|
|
|
|
{
|
2021-06-25 05:32:43 +08:00
|
|
|
// Set menu's max height low enough to workaround nested scroll issues (see https://github.com/ppy/osu-framework/issues/4536).
|
|
|
|
protected override DropdownMenu CreateMenu() => base.CreateMenu().With(m => m.MaxHeight = 100);
|
2021-06-23 04:32:45 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-06 16:09:24 +08:00
|
|
|
}
|
|
|
|
}
|