1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00

Implement enum attributes to set display order

This commit is contained in:
Andrei Zavatski 2020-02-21 01:37:36 +03:00
parent d50cca6264
commit 58903759f1
2 changed files with 66 additions and 2 deletions

View File

@ -1,6 +1,7 @@
// 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.ComponentModel;
using osu.Framework.IO.Network;
using osu.Game.Overlays;
@ -92,19 +93,59 @@ namespace osu.Game.Online.API.Requests
Electronic = 10
}
[HasOrderedElements]
public enum BeatmapSearchLanguage
{
[Order(0)]
Any,
[Order(11)]
Other,
[Order(1)]
English,
[Order(6)]
Japanese,
[Order(2)]
Chinese,
[Order(10)]
Instrumental,
[Order(7)]
Korean,
[Order(3)]
French,
[Order(4)]
German,
[Order(9)]
Swedish,
[Order(8)]
Spanish,
[Order(5)]
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,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
@ -13,6 +14,7 @@ using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API.Requests;
using osuTK;
using osuTK.Graphics;
@ -79,9 +81,30 @@ namespace osu.Game.Overlays.BeatmapListing
TabContainer.Spacing = new Vector2(10, 0);
if (typeof(T).IsEnum)
var type = typeof(T);
if (type.IsEnum)
{
foreach (var val in (T[])Enum.GetValues(typeof(T)))
if (Attribute.GetCustomAttribute(type, typeof(HasOrderedElementsAttribute)) != null)
{
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);
}
}