-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathClassesItemBuilder.cs
More file actions
83 lines (68 loc) · 2.5 KB
/
ClassesItemBuilder.cs
File metadata and controls
83 lines (68 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using TNRD.Items;
using TNRD.Utilities;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine.Assertions;
namespace TNRD.Builders
{
internal sealed class ClassesItemBuilder
{
private readonly Dictionary<string, AdvancedDropdownItem> splitToItem;
private readonly Type interfaceType;
public ClassesItemBuilder(Type interfaceType)
{
Assert.IsNotNull(interfaceType);
Assert.IsTrue(interfaceType.IsInterface);
splitToItem = new Dictionary<string, AdvancedDropdownItem>();
this.interfaceType = interfaceType;
}
public AdvancedDropdownItem Build()
{
AdvancedDropdownItem root = new AdvancedDropdownItem("Classes");
TypeCache.TypeCollection types = TypeCache.GetTypesDerivedFrom(interfaceType);
foreach (Type type in types)
{
if (type.IsAbstract || type.IsInterface) continue;
if (type.IsSubclassOf(typeof(UnityEngine.Object))) continue;
AdvancedDropdownItem parent = GetOrCreateParentItem(type, root);
parent.AddChild(new ClassDropdownItem(type));
}
if (!root.children.Any())
{
return null;
}
return root;
}
private AdvancedDropdownItem GetOrCreateParentItem(Type type, AdvancedDropdownItem root)
{
Assert.IsNotNull(type);
Assert.IsNotNull(root);
Assert.IsNotNull(splitToItem);
if (string.IsNullOrEmpty(type.Namespace))
return root;
string[] splits = type.Namespace.Split('.');
AdvancedDropdownItem splitItem = root;
string currentPath = string.Empty;
foreach (string split in splits)
{
currentPath += split + '.';
if (splitToItem.TryGetValue(currentPath, out AdvancedDropdownItem foundItem))
{
splitItem = foundItem;
continue;
}
AdvancedDropdownItem newSplitItem = new AdvancedDropdownItem(split)
{
icon = IconUtility.FolderIcon
};
splitToItem.Add(currentPath, newSplitItem);
splitItem.AddChild(newSplitItem);
splitItem = newSplitItem;
}
return splitItem;
}
}
}