FR2_Selection.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityObject = UnityEngine.Object;
  6. namespace vietlabs.fr2
  7. {
  8. public class FR2_Selection : IRefDraw
  9. {
  10. internal HashSet<string> guidSet = new HashSet<string>();
  11. internal HashSet<string> instSet = new HashSet<string>(); // Do not reference directly to SceneObject (which might be destroyed anytime)
  12. public int Count
  13. {
  14. get { return guidSet.Count + instSet.Count; }
  15. }
  16. public bool Contains(string guidOrInstID)
  17. {
  18. return guidSet.Contains(guidOrInstID) || instSet.Contains(guidOrInstID);
  19. }
  20. public bool Contains(UnityObject sceneObject)
  21. {
  22. var id = sceneObject.GetInstanceID().ToString();
  23. return instSet.Contains(id);
  24. }
  25. public void Add(UnityObject sceneObject)
  26. {
  27. if (sceneObject == null) return;
  28. var id = sceneObject.GetInstanceID().ToString();
  29. instSet.Add(id); // hashset does not need to check exist before add
  30. dirty = true;
  31. }
  32. public void AddRange(params UnityObject[] sceneObjects)
  33. {
  34. foreach (var go in sceneObjects)
  35. {
  36. var id = go.GetInstanceID().ToString();
  37. instSet.Add(id); // hashset does not need to check exist before add
  38. }
  39. dirty = true;
  40. }
  41. public void Add(string guid)
  42. {
  43. if (guidSet.Contains(guid)) return;
  44. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  45. if (string.IsNullOrEmpty(assetPath))
  46. {
  47. Debug.LogWarning("Invalid GUID: " + guid);
  48. return;
  49. }
  50. guidSet.Add(guid);
  51. dirty = true;
  52. }
  53. public void AddRange(params string[] guids)
  54. {
  55. foreach (var id in guids)
  56. {
  57. Add(id);
  58. }
  59. dirty = true;
  60. }
  61. public void Remove(UnityObject sceneObject)
  62. {
  63. if (sceneObject == null) return;
  64. var id = sceneObject.GetInstanceID().ToString();
  65. instSet.Remove(id);
  66. dirty = true;
  67. }
  68. public void Remove(string guidOrInstID)
  69. {
  70. guidSet.Remove(guidOrInstID);
  71. instSet.Remove(guidOrInstID);
  72. dirty = true;
  73. }
  74. public void Clear()
  75. {
  76. guidSet.Clear();
  77. instSet.Clear();
  78. dirty = true;
  79. }
  80. public bool isSelectingAsset
  81. {
  82. get { return instSet.Count == 0; }
  83. }
  84. public void Add(FR2_Ref rf)
  85. {
  86. if (rf.isSceneRef)
  87. {
  88. Add(rf.component);
  89. }
  90. else
  91. {
  92. Add(rf.asset.guid);
  93. }
  94. }
  95. public void Remove(FR2_Ref rf)
  96. {
  97. if (rf.isSceneRef)
  98. {
  99. Remove(rf.component);
  100. }
  101. else
  102. {
  103. Remove(rf.asset.guid);
  104. }
  105. }
  106. // ------------ instance
  107. private bool dirty;
  108. private readonly FR2_RefDrawer drawer;
  109. internal Dictionary<string, FR2_Ref> refs;
  110. internal bool isLock;
  111. public FR2_Selection(IWindow window)
  112. {
  113. this.window = window;
  114. drawer = new FR2_RefDrawer(window);
  115. drawer.groupDrawer.hideGroupIfPossible = true;
  116. drawer.forceHideDetails = true;
  117. drawer.level0Group = string.Empty;
  118. dirty = true;
  119. drawer.SetDirty();
  120. }
  121. public IWindow window { get; set; }
  122. public int ElementCount()
  123. {
  124. return refs == null ? 0 : refs.Count;
  125. }
  126. public bool DrawLayout()
  127. {
  128. if (dirty) RefreshView();
  129. return drawer.DrawLayout();
  130. }
  131. public bool Draw(Rect rect)
  132. {
  133. if (dirty) RefreshView();
  134. if (refs == null) return false;
  135. DrawLock(new Rect(rect.xMax - 12f, rect.yMin - 12f, 16f, 16f));
  136. return drawer.Draw(rect);
  137. }
  138. public void SetDirty()
  139. {
  140. drawer.SetDirty();
  141. }
  142. private static readonly Color PRO = new Color(0.8f, 0.8f, 0.8f, 1f);
  143. private static readonly Color INDIE = new Color(0.1f, 0.1f, 0.1f, 1f);
  144. public void DrawLock(Rect rect)
  145. {
  146. GUI2.ContentColor(() =>
  147. {
  148. var icon = isLock ? FR2_Icon.Lock : FR2_Icon.Unlock;
  149. if (GUI2.Toggle(rect, ref isLock, icon))
  150. {
  151. window.WillRepaint = true;
  152. window.OnSelectionChange();
  153. }
  154. }, GUI2.Theme(PRO, INDIE));
  155. }
  156. public void RefreshView()
  157. {
  158. if (refs == null) refs = new Dictionary<string, FR2_Ref>();
  159. refs.Clear();
  160. if (instSet.Count > 0)
  161. {
  162. foreach (var instId in instSet)
  163. {
  164. refs.Add(instId, new FR2_SceneRef(0, EditorUtility.InstanceIDToObject(int.Parse(instId))));
  165. }
  166. }
  167. else
  168. {
  169. foreach (var guid in guidSet)
  170. {
  171. var asset = FR2_Cache.Api.Get(guid, false);
  172. refs.Add(guid, new FR2_Ref(0, 0, asset, null)
  173. {
  174. isSceneRef = false
  175. });
  176. }
  177. }
  178. drawer.SetRefs(refs);
  179. dirty = false;
  180. }
  181. }
  182. }