No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearcherTreeUtility.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using JetBrains.Annotations;
  4. namespace UnityEditor.Searcher
  5. {
  6. // SearcherTreeUtility contains a helper function that takes a flat list of SearcherItems and constructs a SearcherItems tree using their names as paths.
  7. //
  8. // For example:
  9. // List<SearcherItem> items = new List<SearcherItem>();
  10. // items.Add(new SearcherItem("Fantasy/J. R. R. Tolkien/The Fellowship of the Ring"));
  11. // items.Add(new SearcherItem("Fantasy/J. R. R. Tolkien/The Two Towers"));
  12. // items.Add(new SearcherItem("Fantasy/J. R. R. Tolkien/The Return of the King"));
  13. // items.Add(new SearcherItem("Health & Fitness/Becoming a Supple Leopard"));
  14. // items.Add(new SearcherItem("Some Uncategorized Book"));
  15. //
  16. // List<SearcherItem> itemsTree = SearcherTreeUtility.CreateFromFlatList(items);
  17. //
  18. // Will return the follow hierarchy:
  19. // - Fantasy
  20. // - - J. R. R. Tolkien
  21. // - - - The Fellowship of the Ring
  22. // - - - The Two Towers
  23. // - - - The Return of the King
  24. // - Health & Fitness
  25. // - - Becoming a Supple Leopard
  26. // - Some Uncategorized Book
  27. //
  28. // Where the first level of SearcherItems is directly inside the list.
  29. // Note that this will also break the names into their final path component.
  30. [PublicAPI]
  31. public static class SearcherTreeUtility
  32. {
  33. public static List<SearcherItem> CreateFromFlatList(List<SearcherItem> items)
  34. {
  35. List<SearcherItem> searchList = new List<SearcherItem>();
  36. for (int i = 0; i < items.Count; ++i)
  37. {
  38. SearcherItem item = items[i];
  39. string[] pathParts = item.Name.Split('/');
  40. SearcherItem searchNode = FindNodeByName(searchList, pathParts[0]);
  41. if (searchNode == null)
  42. {
  43. searchNode = new SearcherItem(pathParts[0]);
  44. searchList.Add(searchNode);
  45. }
  46. AddItem(searchNode, item, pathParts);
  47. }
  48. return searchList;
  49. }
  50. private static void AddItem(SearcherItem root, SearcherItem item, string[] pathParts)
  51. {
  52. string itemFullPath = item.Name;
  53. string itemName = pathParts[pathParts.Length - 1];
  54. string currentPath = string.Empty;
  55. SearcherItem currentSearchNode = root;
  56. for (int i = 1; i < pathParts.Length; ++i)
  57. {
  58. SearcherItem node = FindNodeByName(currentSearchNode.Children, pathParts[i]);
  59. if (node == null)
  60. {
  61. node = new SearcherItem(pathParts[i]);
  62. currentSearchNode.AddChild(node);
  63. }
  64. currentSearchNode = node;
  65. }
  66. // Set the user data to the final node, which is guaranteed to correspond to the item.
  67. currentSearchNode.UserData = item.UserData;
  68. currentSearchNode.Icon = item.Icon;
  69. }
  70. private static SearcherItem FindNodeByName(IList<SearcherItem> searchList, string name)
  71. {
  72. for (int i = 0; i < searchList.Count; ++i)
  73. {
  74. if (searchList[i].Name.Equals(name))
  75. {
  76. return searchList[i];
  77. }
  78. }
  79. return null;
  80. }
  81. }
  82. }