Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Searcher.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using JetBrains.Annotations;
  5. namespace UnityEditor.Searcher
  6. {
  7. [PublicAPI]
  8. public class Searcher
  9. {
  10. public ISearcherAdapter Adapter { get; }
  11. public Comparison<SearcherItem> SortComparison { get; set; }
  12. readonly List<SearcherDatabaseBase> m_Databases;
  13. public Searcher(SearcherDatabaseBase database, string title)
  14. : this(new List<SearcherDatabaseBase> { database }, title, null)
  15. { }
  16. public Searcher(IEnumerable<SearcherDatabaseBase> databases, string title)
  17. : this(databases, title, null)
  18. { }
  19. public Searcher(SearcherDatabaseBase database, ISearcherAdapter adapter = null)
  20. : this(new List<SearcherDatabaseBase> { database }, adapter)
  21. { }
  22. public Searcher(IEnumerable<SearcherDatabaseBase> databases, ISearcherAdapter adapter = null)
  23. : this(databases, string.Empty, adapter)
  24. { }
  25. Searcher(IEnumerable<SearcherDatabaseBase> databases, string title, ISearcherAdapter adapter)
  26. {
  27. m_Databases = new List<SearcherDatabaseBase>();
  28. var databaseId = 0;
  29. foreach (var database in databases)
  30. {
  31. // This is needed for sorting items between databases.
  32. database.OverwriteId(databaseId);
  33. databaseId++;
  34. m_Databases.Add(database);
  35. }
  36. Adapter = adapter ?? new SearcherAdapter(title);
  37. }
  38. public void BuildIndices()
  39. {
  40. foreach (var database in m_Databases)
  41. {
  42. database.BuildIndex();
  43. }
  44. }
  45. public IEnumerable<SearcherItem> Search(string query)
  46. {
  47. query = query.ToLower();
  48. var results = new List<SearcherItem>();
  49. float maxScore = 0;
  50. foreach (var database in m_Databases)
  51. {
  52. var localResults = database.Search(query, out var localMaxScore);
  53. if (localMaxScore > maxScore)
  54. {
  55. // skip the highest scored item in the local results and
  56. // insert it back as the first item. The first item should always be
  57. // the highest scored item. The order of the other items does not matter
  58. // because they will be reordered to recreate the tree properly.
  59. if (results.Count > 0)
  60. {
  61. // backup previous best result
  62. results.Add(results[0]);
  63. // replace it with the new best result
  64. results[0] = localResults[0];
  65. // add remaining results at the end
  66. results.AddRange(localResults.Skip(1));
  67. }
  68. else // best result will be the first item
  69. results.AddRange(localResults);
  70. maxScore = localMaxScore;
  71. }
  72. else // no new best result just append everything
  73. {
  74. results.AddRange(localResults);
  75. }
  76. }
  77. return results;
  78. }
  79. [PublicAPI]
  80. public class AnalyticsEvent
  81. {
  82. [PublicAPI]
  83. public enum EventType{ Pending, Picked, Cancelled }
  84. public readonly EventType eventType;
  85. public readonly string currentSearchFieldText;
  86. public AnalyticsEvent(EventType eventType, string currentSearchFieldText)
  87. {
  88. this.eventType = eventType;
  89. this.currentSearchFieldText = currentSearchFieldText;
  90. }
  91. }
  92. }
  93. }