1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 19:32:55 +08:00

Clean up enum sorting attribute code

This commit is contained in:
Dean Herbert 2020-04-16 17:26:09 +09:00
parent 86e8aaa2f4
commit 2ab4a7293e
3 changed files with 56 additions and 42 deletions

View File

@ -1,12 +1,12 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.ComponentModel; using System.ComponentModel;
using osu.Framework.IO.Network; using osu.Framework.IO.Network;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Overlays.Direct; using osu.Game.Overlays.Direct;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Utils;
namespace osu.Game.Online.API.Requests namespace osu.Game.Online.API.Requests
{ {
@ -139,20 +139,4 @@ namespace osu.Game.Online.API.Requests
[Order(5)] [Order(5)]
Italian Italian
} }
[AttributeUsage(AttributeTargets.Field)]
public class OrderAttribute : Attribute
{
public readonly int Order;
public OrderAttribute(int order)
{
Order = order;
}
}
[AttributeUsage(AttributeTargets.Enum)]
public class HasOrderedElementsAttribute : Attribute
{
}
} }

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
@ -14,10 +13,10 @@ using osu.Framework.Input.Events;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API.Requests;
using osuTK; using osuTK;
using osuTK.Graphics; using osuTK.Graphics;
using Humanizer; using Humanizer;
using osu.Game.Utils;
namespace osu.Game.Overlays.BeatmapListing namespace osu.Game.Overlays.BeatmapListing
{ {
@ -82,30 +81,9 @@ namespace osu.Game.Overlays.BeatmapListing
TabContainer.Spacing = new Vector2(10, 0); TabContainer.Spacing = new Vector2(10, 0);
var type = typeof(T); if (typeof(T).IsEnum)
if (type.IsEnum)
{ {
if (Attribute.GetCustomAttribute(type, typeof(HasOrderedElementsAttribute)) != null) foreach (var val in OrderAttributeUtils.GetValuesInOrder<T>())
{
var enumValues = Enum.GetValues(type).Cast<T>().ToArray();
var enumNames = Enum.GetNames(type);
int[] enumPositions = Array.ConvertAll(enumNames, n =>
{
var orderAttr = (OrderAttribute)type.GetField(n).GetCustomAttributes(typeof(OrderAttribute), false)[0];
return orderAttr.Order;
});
Array.Sort(enumPositions, enumValues);
foreach (var val in enumValues)
AddItem(val);
return;
}
foreach (var val in (T[])Enum.GetValues(type))
AddItem(val); AddItem(val);
} }
} }

View File

@ -0,0 +1,52 @@
// 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;
using System.Linq;
namespace osu.Game.Utils
{
public static class OrderAttributeUtils
{
/// <summary>
/// Get values of an enum in order. Supports custom ordering via <see cref="OrderAttribute"/>.
/// </summary>
public static IEnumerable<T> GetValuesInOrder<T>()
{
var type = typeof(T);
if (!type.IsEnum)
throw new InvalidOperationException("T must be an enum");
IEnumerable<T> items = (T[])Enum.GetValues(type);
if (Attribute.GetCustomAttribute(type, typeof(HasOrderedElementsAttribute)) == null)
return items;
return items.OrderBy(i =>
{
if (type.GetField(i.ToString()).GetCustomAttributes(typeof(OrderAttribute), false).FirstOrDefault() is OrderAttribute attr)
return attr.Order;
return 0;
});
}
}
[AttributeUsage(AttributeTargets.Field)]
public class OrderAttribute : Attribute
{
public readonly int Order;
public OrderAttribute(int order)
{
Order = order;
}
}
[AttributeUsage(AttributeTargets.Enum)]
public class HasOrderedElementsAttribute : Attribute
{
}
}