Remove duplicate code in SettingsEnumDropdown

This commit is contained in:
DrabWeb 2017-05-19 17:04:59 -03:00
parent 5b2c74be50
commit 1d61fc84c7
2 changed files with 10 additions and 25 deletions

View File

@ -13,7 +13,7 @@ namespace osu.Game.Graphics.UserInterface
public OsuEnumDropdown()
{
if (!typeof(T).IsEnum)
throw new InvalidOperationException("SettingsDropdown only supports enums as the generic type argument");
throw new InvalidOperationException("OsuEnumDropdown only supports enums as the generic type argument");
List<KeyValuePair<string, T>> items = new List<KeyValuePair<string, T>>();
foreach(var val in (T[])Enum.GetValues(typeof(T)))

View File

@ -1,32 +1,17 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using osu.Framework.Graphics;
using osu.Game.Graphics.UserInterface;
namespace osu.Game.Overlays.Settings
{
public class SettingsEnumDropdown<T> : SettingsDropdown<T>
{
public SettingsEnumDropdown()
protected override Drawable CreateControl() => new OsuEnumDropdown<T>
{
if (!typeof(T).IsEnum)
throw new InvalidOperationException("SettingsDropdown only supports enums as the generic type argument");
List<KeyValuePair<string, T>> items = new List<KeyValuePair<string, T>>();
foreach(var val in (T[])Enum.GetValues(typeof(T)))
{
var field = typeof(T).GetField(Enum.GetName(typeof(T), val));
items.Add(
new KeyValuePair<string, T>(
field.GetCustomAttribute<DescriptionAttribute>()?.Description ?? Enum.GetName(typeof(T), val),
val
)
);
}
Items = items;
}
Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X,
};
}
}