|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using UnityEditor; |
| 6 | +using UnityEditorInternal; |
| 7 | +using UnityEngine; |
| 8 | + |
| 9 | +namespace BrunoMikoski.ScriptableObjectCollections.Browser |
| 10 | +{ |
| 11 | + [Serializable] |
| 12 | + public class BrowserSettings |
| 13 | + { |
| 14 | + private const string PATH = "UserSettings/ScriptableObjectCollectionBrowser.json"; |
| 15 | + |
| 16 | + [SettingsProvider] |
| 17 | + public static SettingsProvider CreateSettingsProvider() |
| 18 | + { |
| 19 | + return new SettingsProvider("Project/Scriptable Object Collection/Browser", SettingsScope.Project) |
| 20 | + { |
| 21 | + label = "Browser", |
| 22 | + guiHandler = Instance.OnGUI, |
| 23 | + keywords = new string[] { "SOC", "Scriptable Objects", "Scriptable Objects Collection", "Browser" } |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + private static BrowserSettings instance; |
| 28 | + |
| 29 | + public static BrowserSettings Instance |
| 30 | + { |
| 31 | + get |
| 32 | + { |
| 33 | + if (instance != null) |
| 34 | + return instance; |
| 35 | + |
| 36 | + if (File.Exists(PATH)) |
| 37 | + { |
| 38 | + string json = File.ReadAllText(PATH); |
| 39 | + instance = JsonUtility.FromJson<BrowserSettings>(json); |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + instance = new BrowserSettings(); |
| 44 | + } |
| 45 | + |
| 46 | + return instance; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + [SerializeField] private List<string> serializedTypesToIgnore = new(); |
| 51 | + [SerializeField] private bool showHiddenCollections; |
| 52 | + [NonSerialized] private List<Type> uiTypesToIgnore = new(); |
| 53 | + [NonSerialized] private TypeCache.TypeCollection cachedDerivedTypes; |
| 54 | + [NonSerialized] private bool hasCachedDerivedTypes; |
| 55 | + |
| 56 | + private ReorderableList reorderableList; |
| 57 | + |
| 58 | + public TypeCache.TypeCollection DerivedTypes |
| 59 | + { |
| 60 | + get |
| 61 | + { |
| 62 | + if (hasCachedDerivedTypes) |
| 63 | + return cachedDerivedTypes; |
| 64 | + |
| 65 | + cachedDerivedTypes = TypeCache.GetTypesDerivedFrom<ScriptableObjectCollection>(); |
| 66 | + hasCachedDerivedTypes = true; |
| 67 | + return cachedDerivedTypes; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public bool ShowHiddenCollections |
| 72 | + { |
| 73 | + get => showHiddenCollections; |
| 74 | + set |
| 75 | + { |
| 76 | + showHiddenCollections = value; |
| 77 | + Save(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public event Action SettingsChanged; |
| 82 | + |
| 83 | + public bool IsHiddenCollection(Type type) |
| 84 | + { |
| 85 | + return serializedTypesToIgnore.Contains(type.AssemblyQualifiedName); |
| 86 | + } |
| 87 | + |
| 88 | + private void OnGUI(string searchContext) |
| 89 | + { |
| 90 | + if (reorderableList == null) |
| 91 | + { |
| 92 | + InitializeList(); |
| 93 | + } |
| 94 | + |
| 95 | + EditorGUI.BeginChangeCheck(); |
| 96 | + showHiddenCollections = EditorGUILayout.Toggle("Show Hidden Collections", showHiddenCollections); |
| 97 | + if (EditorGUI.EndChangeCheck()) |
| 98 | + { |
| 99 | + Save(); |
| 100 | + } |
| 101 | + |
| 102 | + EditorGUILayout.LabelField("Collections to hide", EditorStyles.boldLabel); |
| 103 | + reorderableList.DoLayoutList(); |
| 104 | + } |
| 105 | + |
| 106 | + private void InitializeList() |
| 107 | + { |
| 108 | + uiTypesToIgnore = serializedTypesToIgnore.Select(Type.GetType).ToList(); |
| 109 | + |
| 110 | + reorderableList = new ReorderableList(uiTypesToIgnore, typeof(Type), true, false, true, true); |
| 111 | + reorderableList.drawElementCallback += OnDrawElement; |
| 112 | + reorderableList.onAddCallback += OnAddElement; |
| 113 | + reorderableList.onRemoveCallback += OnRemoveElement; |
| 114 | + } |
| 115 | + |
| 116 | + private void OnDrawElement(Rect rect, int index, bool isActive, bool isFocused) |
| 117 | + { |
| 118 | + GUI.Label(rect, uiTypesToIgnore[index].Name); |
| 119 | + } |
| 120 | + |
| 121 | + private void OnAddElement(ReorderableList list) |
| 122 | + { |
| 123 | + List<Type> items = GetFilteredTypes(); |
| 124 | + GenericMenu genericMenu = new(); |
| 125 | + |
| 126 | + foreach (Type type in items) |
| 127 | + { |
| 128 | + // We don't want to allow the user to remove the last collection, that breaks the tree view |
| 129 | + if (items.Count == 1) |
| 130 | + { |
| 131 | + genericMenu.AddDisabledItem(new GUIContent(type.Name)); |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + genericMenu.AddItem(new GUIContent(type.Name), |
| 136 | + false, |
| 137 | + () => |
| 138 | + { |
| 139 | + uiTypesToIgnore.Add(type); |
| 140 | + serializedTypesToIgnore.Add(type.AssemblyQualifiedName); |
| 141 | + Save(); |
| 142 | + }); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + genericMenu.ShowAsContext(); |
| 147 | + } |
| 148 | + |
| 149 | + private void OnRemoveElement(ReorderableList list) |
| 150 | + { |
| 151 | + uiTypesToIgnore.RemoveAt(list.index); |
| 152 | + serializedTypesToIgnore.RemoveAt(list.index); |
| 153 | + Save(); |
| 154 | + } |
| 155 | + |
| 156 | + private void Save() |
| 157 | + { |
| 158 | + string json = EditorJsonUtility.ToJson(this, prettyPrint: true); |
| 159 | + File.WriteAllText(PATH, json); |
| 160 | + SettingsChanged?.Invoke(); |
| 161 | + } |
| 162 | + |
| 163 | + public void ToggleCollection(ScriptableObjectCollection collection) |
| 164 | + { |
| 165 | + Type type = collection.GetType(); |
| 166 | + |
| 167 | + if (serializedTypesToIgnore.Contains(type.AssemblyQualifiedName)) |
| 168 | + { |
| 169 | + uiTypesToIgnore.Remove(type); |
| 170 | + serializedTypesToIgnore.Remove(type.AssemblyQualifiedName); |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + uiTypesToIgnore.Add(type); |
| 175 | + serializedTypesToIgnore.Add(type.AssemblyQualifiedName); |
| 176 | + } |
| 177 | + |
| 178 | + Save(); |
| 179 | + } |
| 180 | + |
| 181 | + public bool CanHide(ScriptableObjectCollection collection) |
| 182 | + { |
| 183 | + if (serializedTypesToIgnore.Contains(collection.GetType().AssemblyQualifiedName)) |
| 184 | + return true; |
| 185 | + |
| 186 | + List<Type> items = GetFilteredTypes(); |
| 187 | + return items.Count > 1; |
| 188 | + } |
| 189 | + |
| 190 | + private List<Type> GetFilteredTypes() |
| 191 | + { |
| 192 | + List<Type> items = new(); |
| 193 | + TypeCache.TypeCollection typesDerivedFrom = DerivedTypes; |
| 194 | + foreach (Type type in typesDerivedFrom) |
| 195 | + { |
| 196 | + if (type.IsAbstract) |
| 197 | + continue; |
| 198 | + |
| 199 | + if (type.ContainsGenericParameters) |
| 200 | + continue; |
| 201 | + |
| 202 | + if (uiTypesToIgnore.Contains(type)) |
| 203 | + continue; |
| 204 | + |
| 205 | + items.Add(type); |
| 206 | + } |
| 207 | + |
| 208 | + return items; |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments