File size: 4,432 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #if UNITY_EDITOR
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
namespace UnityEngine.InputSystem.Editor
{
internal abstract class AdvancedDropdownDataSource
{
private static readonly string kSearchHeader = L10n.Tr("Search");
public AdvancedDropdownItem mainTree { get; private set; }
public AdvancedDropdownItem searchTree { get; private set; }
public List<int> selectedIDs { get; } = new List<int>();
protected AdvancedDropdownItem root => mainTree;
protected List<AdvancedDropdownItem> m_SearchableElements;
public void ReloadData()
{
mainTree = FetchData();
}
protected abstract AdvancedDropdownItem FetchData();
public void RebuildSearch(string search)
{
searchTree = Search(search);
}
protected bool AddMatchItem(AdvancedDropdownItem e, string name, string[] searchWords, List<AdvancedDropdownItem> matchesStart, List<AdvancedDropdownItem> matchesWithin)
{
var didMatchAll = true;
var didMatchStart = false;
// See if we match ALL the search words.
for (var w = 0; w < searchWords.Length; w++)
{
var search = searchWords[w];
if (name.Contains(search))
{
// If the start of the item matches the first search word, make a note of that.
if (w == 0 && name.StartsWith(search))
didMatchStart = true;
}
else
{
// As soon as any word is not matched, we disregard this item.
didMatchAll = false;
break;
}
}
// We always need to match all search words.
// If we ALSO matched the start, this item gets priority.
if (didMatchAll)
{
if (didMatchStart)
matchesStart.Add(e);
else
matchesWithin.Add(e);
}
return didMatchAll;
}
protected virtual AdvancedDropdownItem PerformCustomSearch(string searchString)
{
return null;
}
protected virtual AdvancedDropdownItem Search(string searchString)
{
if (m_SearchableElements == null)
{
BuildSearchableElements();
}
if (string.IsNullOrEmpty(searchString))
return null;
var searchTree = PerformCustomSearch(searchString);
if (searchTree == null)
{
// Support multiple search words separated by spaces.
var searchWords = searchString.ToLower().Split(' ');
// We keep two lists. Matches that matches the start of an item always get first priority.
var matchesStart = new List<AdvancedDropdownItem>();
var matchesWithin = new List<AdvancedDropdownItem>();
foreach (var e in m_SearchableElements)
{
var name = e.searchableName.ToLower().Replace(" ", "");
AddMatchItem(e, name, searchWords, matchesStart, matchesWithin);
}
searchTree = new AdvancedDropdownItem(kSearchHeader);
matchesStart.Sort();
foreach (var element in matchesStart)
{
searchTree.AddChild(element);
}
matchesWithin.Sort();
foreach (var element in matchesWithin)
{
searchTree.AddChild(element);
}
}
return searchTree;
}
private void BuildSearchableElements()
{
m_SearchableElements = new List<AdvancedDropdownItem>();
BuildSearchableElements(root);
}
private void BuildSearchableElements(AdvancedDropdownItem item)
{
if (!item.children.Any())
{
if (!item.IsSeparator())
m_SearchableElements.Add(item);
return;
}
foreach (var child in item.children)
{
BuildSearchableElements(child);
}
}
}
}
#endif // UNITY_EDITOR
|