diff --git a/Editor/Scripts/Editor/Windows/ImageProcessingGraphEdgeConnectorListener.cs b/Editor/Scripts/Editor/Windows/ImageProcessingGraphEdgeConnectorListener.cs index ee5e037..889a535 100644 --- a/Editor/Scripts/Editor/Windows/ImageProcessingGraphEdgeConnectorListener.cs +++ b/Editor/Scripts/Editor/Windows/ImageProcessingGraphEdgeConnectorListener.cs @@ -25,8 +25,8 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind public void OnDropOutsidePort(Edge edge, Vector2 position) { - window.searchProvider.target = (VisualElement)window.focusController.focusedElement; - SearchWindow.Open(new SearchWindowContext(position), window.searchProvider); + // window.searchProvider.target = (VisualElement)window.focusController.focusedElement; + // SearchWindow.Open(new SearchWindowContext(position), window.searchProvider); List connections = new List(); diff --git a/Editor/Scripts/Editor/Windows/ImageProcessingGraphSearchProvider.cs b/Editor/Scripts/Editor/Windows/ImageProcessingGraphSearchProvider.cs index 4b7e2d6..cfb47ad 100644 --- a/Editor/Scripts/Editor/Windows/ImageProcessingGraphSearchProvider.cs +++ b/Editor/Scripts/Editor/Windows/ImageProcessingGraphSearchProvider.cs @@ -1,138 +1,92 @@ using System; +using System.Reflection; +using UnityEditor; +using UnityEditor.Search; +using UnityEditor.Experimental.GraphView; using System.Collections.Generic; using System.Linq; -using System.Reflection; -using Codice.Client.Common; using ImageProcessingGraph.Editor.Nodes.NodeAttributes; -using UnityEditor; -using UnityEditor.Experimental.GraphView; -using UnityEngine; -using UnityEngine.UIElements; - -namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Windows +using Object = UnityEngine.Object; +static class IPT_SearchProvider { - public struct SearchContextElement - { - public object target { get; private set; } - public string title { get; private set; } + internal static string id = "IPT_Tree"; + internal static List elements; - public SearchContextElement(object target, string title) + [SearchItemProvider] + internal static SearchProvider CreateProvider() + { + return new SearchProvider(id, "IPT Nodes") { - this.target = target; - this.title = title; + filterId = "tree:", + priority = 99999, + showDetailsOptions = ShowDetailsOptions.Inspector | ShowDetailsOptions.Actions, + fetchItems = (context, items, provider) => FetchItems(context, provider), + fetchLabel = (item, context) => item.label, + fetchDescription = (item, context) => item.description, + toObject = (item, type) => item.data as Object + }; + } + + private static IEnumerable FetchItems(SearchContext context, SearchProvider provider) + { + if (elements == null) + BuildElements(); + + foreach (var element in elements) + { + if (!element.title.ToLower().Contains(context.searchQuery.ToLower())) + continue; + + // var item = provider.CreateItem(context, element.title, element.title, $"Node: {element.title}", 0, element.target); + var item = provider.CreateItem(context, element.title, element.title, element.title, null, element.target); + yield return item; } } - - public class ImageProcessingGraphSearchProvider : ScriptableObject, ISearchWindowProvider + + private static void BuildElements() { - public ImageProcessingGraphViewWindow graph; - public VisualElement target; + elements = new List(); - public static List elements; - private Assembly[] assemblies; - public List CreateSearchTree(SearchWindowContext context) + foreach (var type in TypeCache.GetTypesWithAttribute()) { - List tree = new List(); - tree.Add(new SearchTreeGroupEntry(new GUIContent("Nodes"), 0)); - - elements = new List(); - - /* - assemblies = AppDomain.CurrentDomain.GetAssemblies(); - */ + var attr = type.GetCustomAttribute(); + if (string.IsNullOrEmpty(attr.MenuItem)) + continue; - /*foreach (var assembly in assemblies) - { - foreach (Type type in assembly.GetTypes()) - { - if (type.CustomAttributes.ToList() != null) - { - var attr = type.GetCustomAttribute(typeof(NodeInfoAttribute)); - if (attr != null) - { - NodeInfoAttribute info = attr as NodeInfoAttribute; - var node = Activator.CreateInstance(type); - if(string.IsNullOrEmpty(info.MenuItem)) continue; - - elements.Add(new SearchContextElement(node, info.MenuItem)); - } - } - } - }*/ - - foreach (var type in TypeCache.GetTypesWithAttribute()) - { - var attr = type.GetCustomAttribute(); - NodeInfoAttribute info = attr as NodeInfoAttribute; - var node = Activator.CreateInstance(type); - if(string.IsNullOrEmpty(info.MenuItem)) continue; - - elements.Add(new SearchContextElement(node, info.MenuItem)); - } - - elements.Sort((entry1, entry2) => - { - string[] splits1 = entry1.title.Split('/'); - string[] splits2 = entry2.title.Split('/'); - - for (int i = 0; i < splits1.Length; i++) - { - if (i >= splits2.Length) return 1; - - int value = splits1[i].CompareTo(splits2[i]); - if (value != 0) - { - if(splits1.Length != splits2.Length && (i == splits1.Length - 1 || i == splits2.Length - 1)) - return splits1.Length < splits2.Length ? 1 : -1; - - return value; - } - } - - return 0; - }); - - List groups = new List(); - - foreach (var element in elements) - { - string[] entryTitle = element.title.Split('/'); - - string groupName = ""; - - for (int i = 0; i < entryTitle.Length - 1; i++) - { - groupName += entryTitle[i]; - if (!groups.Contains(groupName)) - { - tree.Add(new SearchTreeGroupEntry(new GUIContent(groupName), i + 1)); - groups.Add(groupName); - } - groupName += '/'; - } - - SearchTreeEntry entry = new SearchTreeEntry(new GUIContent(entryTitle.Last())); - entry.level = entryTitle.Length; - entry.userData = new SearchContextElement(element.target, element.title); - tree.Add(entry); - } - - return tree; + var node = Activator.CreateInstance(type); + elements.Add(new SearchContextElement(node, attr.MenuItem)); } - public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context) + elements.Sort((entry1, entry2) => { - var mousePos = graph.ChangeCoordinatesTo(graph, context.screenMousePosition - graph.Window.position.position); - var graphMousePosition = graph.contentViewContainer.WorldToLocal(mousePos); - - SearchContextElement element = (SearchContextElement)SearchTreeEntry.userData; - - BaseImageNode node = (BaseImageNode)element.target; - node.SetPosition(new Rect(graphMousePosition, new Vector2())); - graph.Add(node); - node.asset = graph.asset; + string[] splits1 = entry1.title.Split('/'); + string[] splits2 = entry2.title.Split('/'); - return true; - } + for (int i = 0; i < splits1.Length; i++) + { + if (i >= splits2.Length) return 1; + int value = splits1[i].CompareTo(splits2[i]); + if (value != 0) + { + if (splits1.Length != splits2.Length && (i == splits1.Length - 1 || i == splits2.Length - 1)) + return splits1.Length < splits2.Length ? 1 : -1; + + return value; + } + } + return 0; + }); } -} \ No newline at end of file +} + +public class SearchContextElement +{ + public object target; + public string title; + + public SearchContextElement(object target, string title) + { + this.target = target; + this.title = title; + } +} diff --git a/Editor/Scripts/Editor/Windows/ImageProcessingGraphViewWindow.cs b/Editor/Scripts/Editor/Windows/ImageProcessingGraphViewWindow.cs index ba90db5..3b1979b 100644 --- a/Editor/Scripts/Editor/Windows/ImageProcessingGraphViewWindow.cs +++ b/Editor/Scripts/Editor/Windows/ImageProcessingGraphViewWindow.cs @@ -4,6 +4,7 @@ using ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Windows; using ImageProcessingGraph.Editor.Windows; using UnityEditor; using UnityEditor.Experimental.GraphView; +using UnityEditor.Search; using UnityEngine; using UnityEngine.UIElements; @@ -20,7 +21,7 @@ namespace ImageProcessingGraph.Editor public Dictionary nodeDictionary; public Dictionary connectionDictionary; - internal ImageProcessingGraphSearchProvider searchProvider; + // internal ImageProcessingGraphSearchProvider searchProvider; internal ImageProcessingGraphEdgeConnectorListener edgeConnectorListener; private bool isDropdownEnabled = false; @@ -37,8 +38,8 @@ namespace ImageProcessingGraph.Editor nodeDictionary = new Dictionary(); connectionDictionary = new Dictionary(); - searchProvider = ScriptableObject.CreateInstance(); - searchProvider.graph = this; + // searchProvider = ScriptableObject.CreateInstance(); + // searchProvider.graph = this; edgeConnectorListener = new ImageProcessingGraphEdgeConnectorListener(this); @@ -491,8 +492,10 @@ namespace ImageProcessingGraph.Editor private void ShowSearchWindow(NodeCreationContext obj) { - searchProvider.target = (VisualElement)focusController.focusedElement; - SearchWindow.Open(new SearchWindowContext(obj.screenMousePosition), searchProvider); + // searchProvider.target = (VisualElement)focusController.focusedElement; + // SearchWindow.Open(new SearchWindowContext(obj.screenMousePosition), searchProvider); + + SearchService.ShowContextual("IPT_Tree"); } #endregion