|
| 1 | +// Unity C# reference source |
| 2 | +// Copyright (c) Unity Technologies. For terms of use, see |
| 3 | +// https://unity3d.com/legal/licenses/Unity_Reference_Only_License |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Reflection; |
| 9 | +using UnityEngine.Categorization; |
| 10 | +using Unity.Collections; |
| 11 | + |
| 12 | +namespace UnityEditor.Categorization |
| 13 | +{ |
| 14 | + /* Sorting API that rely on DisplayCategory and DisplaySubCategory */ |
| 15 | + |
| 16 | + internal interface ICategorizable |
| 17 | + { |
| 18 | + Type type { get; } |
| 19 | + } |
| 20 | + |
| 21 | + interface IOrdering |
| 22 | + { |
| 23 | + string name { get; } |
| 24 | + int order { get; } |
| 25 | + } |
| 26 | + |
| 27 | + //Need to be a class to prevent loop definition with leaf that can cause issue when loading type through reflection |
| 28 | + internal class Category<T> : IEnumerable<T>, IOrdering |
| 29 | + where T : ICategorizable, IOrdering |
| 30 | + { |
| 31 | + public List<T> content { get; private set; } |
| 32 | + public string name { get; private set; } |
| 33 | + public int order { get; private set; } |
| 34 | + public Type type => content.Count > 0 ? content[0].type : null; |
| 35 | + public T this[int i] => content[i]; |
| 36 | + public int count => content.Count; |
| 37 | + |
| 38 | + public Category(string name, int order) |
| 39 | + { |
| 40 | + content = new(); |
| 41 | + this.name = name; |
| 42 | + this.order = order; |
| 43 | + } |
| 44 | + |
| 45 | + public void Add(T newElement, IComparer<T> comparer) |
| 46 | + => content.AddSorted(newElement, comparer); |
| 47 | + |
| 48 | + public IEnumerator<T> GetEnumerator() => content.GetEnumerator(); |
| 49 | + IEnumerator IEnumerable.GetEnumerator() => content.GetEnumerator(); |
| 50 | + } |
| 51 | + |
| 52 | + internal struct LeafElement<T> : IOrdering, ICategorizable |
| 53 | + where T : ICategorizable |
| 54 | + { |
| 55 | + public T data { get; private set; } |
| 56 | + public static implicit operator T(LeafElement<T> leaftElement) |
| 57 | + => leaftElement.data; |
| 58 | + |
| 59 | + public string name { get; private set; } |
| 60 | + public int order { get; private set; } |
| 61 | + public Category<LeafElement<T>> parent { get; private set; } |
| 62 | + public Type type => typeof(T); |
| 63 | + |
| 64 | + public LeafElement(T data, string name, int order, Category<LeafElement<T>> parent) |
| 65 | + { |
| 66 | + this.data = data; |
| 67 | + this.name = name; |
| 68 | + this.order = order; |
| 69 | + this.parent = parent; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + internal static class CategorizeHelper |
| 74 | + { |
| 75 | + struct OrderingComparer<T> : IComparer<T> |
| 76 | + where T : IOrdering |
| 77 | + { |
| 78 | + public int Compare(T a, T b) |
| 79 | + { |
| 80 | + var order = a.order.CompareTo(b.order); |
| 81 | + if (order != 0) |
| 82 | + return order; |
| 83 | + return a.name.CompareTo(b.name); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + internal static List<Category<LeafElement<T>>> SortByCategory<T>(this List<T> list) |
| 88 | + where T : ICategorizable |
| 89 | + { |
| 90 | + var categories = new Dictionary<string, Category<LeafElement<T>>>(); |
| 91 | + var result = new List<Category<LeafElement<T>>>(); |
| 92 | + var comparerLeaf = new OrderingComparer<LeafElement<T>>(); |
| 93 | + var comparerCategory = new OrderingComparer<Category<LeafElement<T>>>(); |
| 94 | + |
| 95 | + foreach(var entry in list) |
| 96 | + { |
| 97 | + var type = entry.type; |
| 98 | + CategoryInfoAttribute categoryInfo = type.GetCustomAttribute<CategoryInfoAttribute>(); |
| 99 | + ElementInfoAttribute displayInfo = type.GetCustomAttribute<ElementInfoAttribute>(); |
| 100 | + int categoryOrder = categoryInfo?.Order ?? int.MaxValue; |
| 101 | + int inCategoryOrder = displayInfo?.Order ?? int.MaxValue; |
| 102 | + string categoryName = categoryInfo?.Name |
| 103 | + // Keep compatibility with previous used attribute for 23.3LTS |
| 104 | + ?? type.GetCustomAttribute<System.ComponentModel.CategoryAttribute>()?.Category; |
| 105 | + string name = displayInfo?.Name ?? ObjectNames.NicifyVariableName(type.Name); |
| 106 | + categoryName ??= name; |
| 107 | + |
| 108 | + Category<LeafElement<T>> category; |
| 109 | + if (!categories.TryGetValue(categoryName, out category)) |
| 110 | + { |
| 111 | + category = new Category<LeafElement<T>>(categoryName, categoryOrder); |
| 112 | + categories[categoryName] = category; |
| 113 | + result.AddSorted(category, comparerCategory); |
| 114 | + } |
| 115 | + |
| 116 | + category.Add(new LeafElement<T>(entry, name, inCategoryOrder, category), comparerLeaf); |
| 117 | + } |
| 118 | + |
| 119 | + return result; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments