/Assets/Vendor/AppModules/DevTools/DevGui/Scripts/Dev.cs

https://github.com/crookookoo/handUI · C# · 187 lines · 149 code · 38 blank · 0 comment · 24 complexity · 8ef214cd877449cbf5d4708cdc4e0231 MD5 · raw file

  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Leap.Unity.DevGui {
  6. using Query;
  7. public class Dev : MonoBehaviour {
  8. private static bool _enabled = false;
  9. private static Vector2 _scroll;
  10. private static Dictionary<object, List<DevElement>> _registered;
  11. private static HashSet<string> _expandedCategories;
  12. private static List<CategoryInfo> _categories;
  13. private class CategoryInfo {
  14. public string name;
  15. public List<DevElementInfo> elementInfo = new List<DevElementInfo>();
  16. public CategoryInfo(string name) {
  17. this.name = name;
  18. }
  19. }
  20. private class DevElementInfo {
  21. public List<object> targets;
  22. public DevElement element;
  23. }
  24. static Dev() {
  25. _registered = new Dictionary<object, List<DevElement>>();
  26. _expandedCategories = new HashSet<string>();
  27. _categories = new List<CategoryInfo>();
  28. }
  29. private static Dev _devInstance;
  30. [RuntimeInitializeOnLoadMethod]
  31. private static void ensureInstanceExists() {
  32. if (_devInstance == null) {
  33. _devInstance = FindObjectOfType<Dev>();
  34. if (_devInstance == null) {
  35. _devInstance = new GameObject("__Dev").AddComponent<Dev>();
  36. DontDestroyOnLoad(_devInstance.gameObject);
  37. }
  38. }
  39. }
  40. private static void onOpenGui() {
  41. _registered.Clear();
  42. var objs = FindObjectsOfType<MonoBehaviour>();
  43. foreach (var obj in objs) {
  44. List<DevElement> elements;
  45. if (DevElementBuilder.TryBuildElements(obj, out elements)) {
  46. _registered.Add(obj, elements);
  47. }
  48. }
  49. rebuildCategoryMap();
  50. }
  51. private static void rebuildCategoryMap() {
  52. _categories.Clear();
  53. var typeMap = new Dictionary<Type, KeyValuePair<List<object>, List<DevElement>>>();
  54. foreach (var reg in _registered) {
  55. var obj = reg.Key;
  56. var elements = reg.Value;
  57. KeyValuePair<List<object>, List<DevElement>> pair;
  58. if (!typeMap.TryGetValue(obj.GetType(), out pair)) {
  59. pair = new KeyValuePair<List<object>, List<DevElement>>(new List<object>(), elements);
  60. typeMap[obj.GetType()] = pair;
  61. }
  62. pair.Key.Add(obj);
  63. }
  64. foreach (var pair in typeMap) {
  65. var objs = pair.Value.Key;
  66. var elements = pair.Value.Value;
  67. foreach (var element in elements) {
  68. CategoryInfo categoryInfo = _categories.Query().FirstOrDefault(t => t.name == element.category);
  69. if (categoryInfo == null) {
  70. categoryInfo = new CategoryInfo(element.category);
  71. _categories.Add(categoryInfo);
  72. }
  73. categoryInfo.elementInfo.Add(new DevElementInfo() {
  74. targets = new List<object>(objs),
  75. element = element.Clone()
  76. });
  77. }
  78. }
  79. _categories.Sort((a, b) => a.name.CompareTo(b.name));
  80. }
  81. private void Update() {
  82. if (Input.GetKeyDown(KeyCode.F12)) {
  83. _enabled = !_enabled;
  84. if (_enabled) {
  85. onOpenGui();
  86. }
  87. }
  88. }
  89. private void OnGUI() {
  90. if (!Application.isPlaying) {
  91. return;
  92. }
  93. if (_enabled) {
  94. GUIStyle boxStyle = new GUIStyle();
  95. boxStyle.normal.background = Resources.Load<Texture2D>("ImageBackground");
  96. boxStyle.border = new RectOffset(4, 4, 4, 4);
  97. GUIStyle overlayStyle = new GUIStyle();
  98. overlayStyle.normal.background = Resources.Load<Texture2D>("ImageOverlay");
  99. overlayStyle.border = new RectOffset(4, 4, 4, 4);
  100. GUIStyle overlayFooterStyle = new GUIStyle();
  101. overlayFooterStyle.normal.background = Resources.Load<Texture2D>("ImageOverlayFooter");
  102. overlayFooterStyle.border = new RectOffset(4, 4, 4, 4);
  103. GUIStyle foldoutStyle = new GUIStyle();
  104. foldoutStyle.normal.background = Resources.Load<Texture2D>("ImageFoldout");
  105. foldoutStyle.border = new RectOffset(4, 4, 4, 4);
  106. GUIStyle blackStyle = new GUIStyle(GUIStyle.none);
  107. blackStyle.normal.background = Resources.Load<Texture2D>("ImageBlack");
  108. blackStyle.border = new RectOffset(4, 4, 4, 4);
  109. GUIStyle barStyle = new GUIStyle(GUIStyle.none);
  110. barStyle.normal.background = Resources.Load<Texture2D>("ImageBar");
  111. barStyle.border = new RectOffset(4, 4, 0, 0);
  112. _scroll = GUILayout.BeginScrollView(_scroll, GUIStyle.none, GUIStyle.none);
  113. GUILayout.BeginVertical(boxStyle);
  114. GUILayout.Label("-- Leap Motion Dev Window --");
  115. for (int i = 0; i < _categories.Count; i++) {
  116. var category = _categories[i];
  117. bool isExpanded = _expandedCategories.Contains(category.name);
  118. if (i == _categories.Count - 1 && !isExpanded) {
  119. GUILayout.BeginVertical(overlayFooterStyle);
  120. } else {
  121. GUILayout.BeginVertical(overlayStyle);
  122. }
  123. bool shouldBeExpanded = GUILayout.Toggle(isExpanded, category.name);
  124. GUILayout.EndVertical();
  125. if (shouldBeExpanded) {
  126. _expandedCategories.Add(category.name);
  127. } else {
  128. _expandedCategories.Remove(category.name);
  129. }
  130. if (shouldBeExpanded) {
  131. GUILayout.BeginHorizontal();
  132. GUILayout.Box("", blackStyle, GUILayout.Width(4), GUILayout.ExpandHeight(true));
  133. GUILayout.BeginVertical(foldoutStyle);
  134. for (int j = 0; j < category.elementInfo.Count; j++) {
  135. if (j != 0) {
  136. GUILayout.Box("", barStyle, GUILayout.Height(1), GUILayout.ExpandWidth(true));
  137. }
  138. category.elementInfo[j].element.Draw(category.elementInfo[j].targets);
  139. }
  140. GUILayout.EndVertical();
  141. GUILayout.EndHorizontal();
  142. }
  143. }
  144. GUILayout.EndVertical();
  145. GUILayout.EndScrollView();
  146. }
  147. }
  148. }
  149. }