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. // 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;
@ -92,19 +93,59 @@ namespace osu.Game.Online.API.Requests
Electronic = 10 Electronic = 10
} }
[HasOrderedElements]
public enum BeatmapSearchLanguage public enum BeatmapSearchLanguage
{ {
[Order(0)]
Any, Any,
[Order(11)]
Other, Other,
[Order(1)]
English, English,
[Order(6)]
Japanese, Japanese,
[Order(2)]
Chinese, Chinese,
[Order(10)]
Instrumental, Instrumental,
[Order(7)]
Korean, Korean,
[Order(3)]
French, French,
[Order(4)]
German, German,
[Order(9)]
Swedish, Swedish,
[Order(8)]
Spanish, Spanish,
[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,6 +2,7 @@
// 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;
@ -13,6 +14,7 @@ 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;
@ -79,9 +81,30 @@ namespace osu.Game.Overlays.BeatmapListing
TabContainer.Spacing = new Vector2(10, 0); 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); AddItem(val);
} }
} }