DirectoryReorderableList.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7. namespace Pack
  8. {
  9. public class DirectoryReorderableList
  10. {
  11. private ReorderableList m_ReorderableList;
  12. private List<string> m_Paths;
  13. private List<string> m_SrcPaths;
  14. private List<string> m_NeedFulPaths;
  15. private string m_RootPath;
  16. public DirectoryReorderableList(List<string> paths, List<string> srcPaths, List<string> needfulPaths, string rootPath)
  17. {
  18. SetParams(paths, srcPaths, needfulPaths, rootPath);
  19. }
  20. public void SetParams(List<string> paths, List<string> srcPaths, List<string> needfulPaths, string rootPath)
  21. {
  22. m_Paths = paths;
  23. m_SrcPaths = srcPaths;
  24. m_NeedFulPaths = needfulPaths;
  25. m_RootPath = rootPath;
  26. if (m_ReorderableList == null)
  27. {
  28. m_ReorderableList = new ReorderableList(m_Paths, typeof(string), true, true, true, true);
  29. m_ReorderableList.drawHeaderCallback = OnDrawHeaderCallback;
  30. m_ReorderableList.drawElementCallback = OnDrawElementCallback;
  31. m_ReorderableList.drawNoneElementCallback = OnDrawNoneElementCallback;
  32. m_ReorderableList.onCanAddCallback = OnCanAddCallback;
  33. m_ReorderableList.onAddDropdownCallback = OnAddDropdownCallback;
  34. m_ReorderableList.onCanRemoveCallback = OnCanRemoveCallback;
  35. m_ReorderableList.onRemoveCallback = OnRemoveCallback;
  36. m_ReorderableList.onChangedCallback = OnChangedCallback;
  37. m_ReorderableList.headerHeight = PackGUI.styles.reorderableListHeadBg.fixedHeight;
  38. m_ReorderableList.elementHeight = 24;
  39. }
  40. else
  41. {
  42. m_ReorderableList.list = paths;
  43. }
  44. }
  45. public float GetHeight()
  46. {
  47. return m_ReorderableList.GetHeight();
  48. }
  49. public void DoList(Rect rect)
  50. {
  51. m_ReorderableList.DoList(rect);
  52. }
  53. private void OnDrawHeaderCallback(Rect rect)
  54. {
  55. if (Event.current.type == EventType.Repaint)
  56. {
  57. Rect headerRect = rect;
  58. headerRect.y -= 1;
  59. headerRect.height += 2;
  60. headerRect.xMax += 6;
  61. headerRect.xMin -= 6;
  62. PackGUI.styles.reorderableListHeadBg.Draw(headerRect, false, false, false, false);
  63. }
  64. Rect labelRect = rect;
  65. labelRect.y += (labelRect.height - PackGUI.styles.label.fixedHeight) * 0.5f;
  66. EditorGUI.LabelField(labelRect, EditorGUIUtility.TrTempContent("从上到下依次覆盖"), PackGUI.styles.label);
  67. Vector2 size = PackGUI.styles.miniButton.CalcSize(EditorGUIUtility.TrTempContent("打开文件夹"));
  68. Rect btnRect = rect;
  69. btnRect.x += btnRect.width - size.x;
  70. btnRect.width = size.x;
  71. btnRect.y += (btnRect.height - size.y) * 0.5f;
  72. PackGUI.BeginChangeEnabled(true);
  73. if (GUI.Button(btnRect, "打开文件夹", PackGUI.styles.miniButton))
  74. {
  75. EditorUtility.OpenWithDefaultApp(Path.GetFullPath(m_RootPath));
  76. }
  77. PackGUI.EndChangeEnabled();
  78. }
  79. private void OnDrawNoneElementCallback(Rect rect)
  80. {
  81. EditorGUI.LabelField(rect, "列表是空的", PackGUI.styles.label);
  82. }
  83. private void OnDrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
  84. {
  85. Vector2 size = PackGUI.styles.miniButton.CalcSize(EditorGUIUtility.TrTempContent("打开文件夹"));
  86. Rect labelRect = rect;
  87. labelRect.width = labelRect.width - size.x;
  88. bool changed = true;
  89. if (m_SrcPaths != null)
  90. {
  91. if (index < m_SrcPaths.Count)
  92. {
  93. changed = (m_SrcPaths[index] != m_Paths[index]);
  94. }
  95. }
  96. if (changed) PackGUI.BeginChangeGUIColor(Color.red);
  97. EditorGUI.LabelField(labelRect, m_Paths[index], PackGUI.styles.label);
  98. if (changed) PackGUI.EndChangeGUIColor();
  99. Rect btnRect = rect;
  100. btnRect.x += btnRect.width - size.x;
  101. btnRect.width = size.x;
  102. PackGUI.BeginChangeEnabled(true);
  103. if (GUI.Button(btnRect, "打开文件夹", PackGUI.styles.miniButton))
  104. {
  105. EditorUtility.OpenWithDefaultApp(Path.GetFullPath(m_RootPath + m_Paths[index]));
  106. }
  107. PackGUI.EndChangeEnabled();
  108. }
  109. private bool OnCanAddCallback(ReorderableList list)
  110. {
  111. List<string> lhs = GetRelativePath(m_RootPath);
  112. return !ArrayUtility.ArrayEquals(lhs.ToArray(), m_Paths.ToArray());
  113. }
  114. private bool OnCanRemoveCallback(ReorderableList list)
  115. {
  116. return m_NeedFulPaths == null || !m_NeedFulPaths.Contains(m_Paths[list.index]);
  117. }
  118. private void OnRemoveCallback(ReorderableList list)
  119. {
  120. m_Paths.RemoveAt(list.index);
  121. }
  122. private void OnAddDropdownCallback(Rect buttonRect, ReorderableList list)
  123. {
  124. List<string> ls = GetRelativePath(m_RootPath);
  125. GenericMenu menu = new GenericMenu();
  126. for (int i = 0, iMax = ls.Count; i < iMax; i++)
  127. {
  128. if (m_Paths.Contains(ls[i]))
  129. {
  130. menu.AddDisabledItem(EditorGUIUtility.TrTextContent(ls[i]), true);
  131. }
  132. else
  133. {
  134. menu.AddItem(EditorGUIUtility.TrTextContent(ls[i]), false, AddPluginsAndroid, ls[i]);
  135. }
  136. }
  137. menu.DropDown(buttonRect);
  138. }
  139. private void AddPluginsAndroid(object value)
  140. {
  141. m_Paths.Add((string)value);
  142. }
  143. private void OnChangedCallback(ReorderableList list)
  144. {
  145. if (m_NeedFulPaths == null) return;
  146. for (int i = m_NeedFulPaths.Count - 1; i >= 0; i--)
  147. {
  148. m_Paths.Remove(m_NeedFulPaths[i]);
  149. m_Paths.Insert(0, m_NeedFulPaths[i]);
  150. }
  151. }
  152. private List<string> GetRelativePath(string rootPath)
  153. {
  154. rootPath = Path.GetFullPath(rootPath);
  155. List<string> ls = new List<string>();
  156. foreach (var item in Directory.GetDirectories(rootPath))
  157. {
  158. string path = Path.GetFullPath(item);
  159. path = path.Substring(rootPath.Length, path.Length - rootPath.Length);
  160. path = path.Replace('\\', '/');
  161. ls.Add(path);
  162. }
  163. return ls;
  164. }
  165. }
  166. }