A game about forced loneliness, made by TACStudios
1#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
2using System;
3using System.Collections.Generic;
4using UnityEditor;
5using UnityEditor.Search;
6using UnityEngine.Search;
7
8namespace UnityEngine.InputSystem.Editor
9{
10 internal static class SearchConstants
11 {
12 // SearchFlags: these flags are used to customize how search is performed and how search
13 // results are displayed in the advanced object picker.
14 // Note: SearchFlags.Packages is not currently used and hides all results from packages.
15 internal static readonly SearchFlags PickerSearchFlags = SearchFlags.Sorted | SearchFlags.OpenPicker;
16
17 // Search.SearchViewFlags : these flags are used to customize the appearance of the PickerWindow.
18 internal static readonly Search.SearchViewFlags PickerViewFlags = SearchViewFlags.DisableBuilderModeToggle
19 | SearchViewFlags.DisableInspectorPreview
20 | SearchViewFlags.ListView
21 | SearchViewFlags.DisableSavedSearchQuery;
22 }
23
24 internal static class InputActionReferenceSearchProviders
25 {
26 const string k_AssetFolderSearchProviderId = "AssetsInputActionReferenceSearchProvider";
27 const string k_ProjectWideActionsSearchProviderId = "ProjectWideInputActionReferenceSearchProvider";
28
29 // Search provider for InputActionReferences for all assets in the project, without project-wide actions.
30 internal static SearchProvider CreateInputActionReferenceSearchProviderForAssets()
31 {
32 return CreateInputActionReferenceSearchProvider(k_AssetFolderSearchProviderId,
33 "Asset Input Actions",
34 // Show the asset path in the description.
35 (obj) => AssetDatabase.GetAssetPath((obj as InputActionReference).asset),
36 () => InputActionImporter.LoadInputActionReferencesFromAssetDatabase(skipProjectWide: true));
37 }
38
39 // Search provider for InputActionReferences for project-wide actions
40 internal static SearchProvider CreateInputActionReferenceSearchProviderForProjectWideActions()
41 {
42 return CreateInputActionReferenceSearchProvider(k_ProjectWideActionsSearchProviderId,
43 "Project-Wide Input Actions",
44 (obj) => "(Project-Wide Input Actions)",
45 () =>
46 {
47 var asset = InputSystem.actions;
48 if (asset == null)
49 return Array.Empty<Object>();
50 var assetPath = AssetDatabase.GetAssetPath(asset);
51 return InputActionImporter.LoadInputActionReferencesFromAsset(assetPath);
52 });
53 }
54
55 private static SearchProvider CreateInputActionReferenceSearchProvider(string id, string displayName,
56 Func<Object, string> createItemFetchDescription, Func<IEnumerable<Object>> fetchAssets)
57 {
58 // Match icon used for sub-assets from importer for InputActionReferences.
59 // We assign description+label in FilteredSearch but also provide a fetchDescription+fetchLabel below.
60 // This is needed to support all zoom-modes for an unknown reason.
61 // Also, fetchLabel/fetchDescription and what is provided to CreateItem is playing different
62 // roles at different zoom levels.
63 var inputActionReferenceIcon = InputActionAssetIconLoader.LoadActionIcon();
64
65 return new SearchProvider(id, displayName)
66 {
67 priority = 25,
68 fetchDescription = FetchLabel,
69 fetchItems = (context, items, provider) => FilteredSearch(context, provider, FetchLabel, createItemFetchDescription,
70 fetchAssets, "(Project-Wide Input Actions)"),
71 fetchLabel = FetchLabel,
72 fetchPreview = (item, context, size, options) => inputActionReferenceIcon,
73 fetchThumbnail = (item, context) => inputActionReferenceIcon,
74 toObject = (item, type) => item.data as Object,
75 };
76 }
77
78 // Custom search function with label matching filtering.
79 private static IEnumerable<SearchItem> FilteredSearch(SearchContext context, SearchProvider provider,
80 Func<Object, string> fetchObjectLabel, Func<Object, string> createItemFetchDescription, Func<IEnumerable<Object>> fetchAssets, string description)
81 {
82 foreach (var asset in fetchAssets())
83 {
84 var label = fetchObjectLabel(asset);
85 if (!label.Contains(context.searchText, System.StringComparison.InvariantCultureIgnoreCase))
86 continue; // Ignore due to filtering
87 yield return provider.CreateItem(context, asset.GetInstanceID().ToString(), label, createItemFetchDescription(asset),
88 null, asset);
89 }
90 }
91
92 // Note that this is overloaded to allow utilizing FetchLabel inside fetchItems to keep label formatting
93 // consistent between CreateItem and additional fetchLabel calls.
94 private static string FetchLabel(Object obj)
95 {
96 return obj.name;
97 }
98
99 private static string FetchLabel(SearchItem item, SearchContext context)
100 {
101 return FetchLabel((item.data as Object) !);
102 }
103 }
104}
105#endif