yeah i dont think this is gon work

This commit is contained in:
Chromium 2025-04-29 00:14:27 +01:00
parent 370504dcfc
commit c76d20e362
3 changed files with 84 additions and 127 deletions

View File

@ -25,8 +25,8 @@ namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Wind
public void OnDropOutsidePort(Edge edge, Vector2 position) public void OnDropOutsidePort(Edge edge, Vector2 position)
{ {
window.searchProvider.target = (VisualElement)window.focusController.focusedElement; // window.searchProvider.target = (VisualElement)window.focusController.focusedElement;
SearchWindow.Open(new SearchWindowContext(position), window.searchProvider); // SearchWindow.Open(new SearchWindowContext(position), window.searchProvider);
List<GraphConnection> connections = new List<GraphConnection>(); List<GraphConnection> connections = new List<GraphConnection>();

View File

@ -1,138 +1,92 @@
using System; using System;
using System.Reflection;
using UnityEditor;
using UnityEditor.Search;
using UnityEditor.Experimental.GraphView;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using Codice.Client.Common;
using ImageProcessingGraph.Editor.Nodes.NodeAttributes; using ImageProcessingGraph.Editor.Nodes.NodeAttributes;
using UnityEditor; using Object = UnityEngine.Object;
using UnityEditor.Experimental.GraphView; static class IPT_SearchProvider
using UnityEngine;
using UnityEngine.UIElements;
namespace ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Windows
{ {
public struct SearchContextElement internal static string id = "IPT_Tree";
{ internal static List<SearchContextElement> elements;
public object target { get; private set; }
public string title { get; private set; }
public SearchContextElement(object target, string title) [SearchItemProvider]
internal static SearchProvider CreateProvider()
{
return new SearchProvider(id, "IPT Nodes")
{ {
this.target = target; filterId = "tree:",
this.title = title; 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<SearchItem> 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; elements = new List<SearchContextElement>();
public VisualElement target;
public static List<SearchContextElement> elements; foreach (var type in TypeCache.GetTypesWithAttribute<NodeInfoAttribute>())
private Assembly[] assemblies;
public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context)
{ {
List<SearchTreeEntry> tree = new List<SearchTreeEntry>(); var attr = type.GetCustomAttribute<NodeInfoAttribute>();
tree.Add(new SearchTreeGroupEntry(new GUIContent("Nodes"), 0)); if (string.IsNullOrEmpty(attr.MenuItem))
continue;
elements = new List<SearchContextElement>();
/*
assemblies = AppDomain.CurrentDomain.GetAssemblies();
*/
/*foreach (var assembly in assemblies) var node = Activator.CreateInstance(type);
{ elements.Add(new SearchContextElement(node, attr.MenuItem));
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<NodeInfoAttribute>())
{
var attr = type.GetCustomAttribute<NodeInfoAttribute>();
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<string> groups = new List<string>();
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;
} }
public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context) elements.Sort((entry1, entry2) =>
{ {
var mousePos = graph.ChangeCoordinatesTo(graph, context.screenMousePosition - graph.Window.position.position); string[] splits1 = entry1.title.Split('/');
var graphMousePosition = graph.contentViewContainer.WorldToLocal(mousePos); string[] splits2 = entry2.title.Split('/');
SearchContextElement element = (SearchContextElement)SearchTreeEntry.userData;
BaseImageNode node = (BaseImageNode)element.target;
node.SetPosition(new Rect(graphMousePosition, new Vector2()));
graph.Add(node);
node.asset = graph.asset;
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;
});
} }
} }
public class SearchContextElement
{
public object target;
public string title;
public SearchContextElement(object target, string title)
{
this.target = target;
this.title = title;
}
}

View File

@ -4,6 +4,7 @@ using ImageProcessingGraph.Editor.Unity_Image_Processing.Scripts.Editor.Windows;
using ImageProcessingGraph.Editor.Windows; using ImageProcessingGraph.Editor.Windows;
using UnityEditor; using UnityEditor;
using UnityEditor.Experimental.GraphView; using UnityEditor.Experimental.GraphView;
using UnityEditor.Search;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
@ -20,7 +21,7 @@ namespace ImageProcessingGraph.Editor
public Dictionary<string, ImageProcessingGraphNodeVisual> nodeDictionary; public Dictionary<string, ImageProcessingGraphNodeVisual> nodeDictionary;
public Dictionary<Edge, GraphConnection> connectionDictionary; public Dictionary<Edge, GraphConnection> connectionDictionary;
internal ImageProcessingGraphSearchProvider searchProvider; // internal ImageProcessingGraphSearchProvider searchProvider;
internal ImageProcessingGraphEdgeConnectorListener edgeConnectorListener; internal ImageProcessingGraphEdgeConnectorListener edgeConnectorListener;
private bool isDropdownEnabled = false; private bool isDropdownEnabled = false;
@ -37,8 +38,8 @@ namespace ImageProcessingGraph.Editor
nodeDictionary = new Dictionary<string, ImageProcessingGraphNodeVisual>(); nodeDictionary = new Dictionary<string, ImageProcessingGraphNodeVisual>();
connectionDictionary = new Dictionary<Edge, GraphConnection>(); connectionDictionary = new Dictionary<Edge, GraphConnection>();
searchProvider = ScriptableObject.CreateInstance<ImageProcessingGraphSearchProvider>(); // searchProvider = ScriptableObject.CreateInstance<ImageProcessingGraphSearchProvider>();
searchProvider.graph = this; // searchProvider.graph = this;
edgeConnectorListener = new ImageProcessingGraphEdgeConnectorListener(this); edgeConnectorListener = new ImageProcessingGraphEdgeConnectorListener(this);
@ -491,8 +492,10 @@ namespace ImageProcessingGraph.Editor
private void ShowSearchWindow(NodeCreationContext obj) private void ShowSearchWindow(NodeCreationContext obj)
{ {
searchProvider.target = (VisualElement)focusController.focusedElement; // searchProvider.target = (VisualElement)focusController.focusedElement;
SearchWindow.Open(new SearchWindowContext(obj.screenMousePosition), searchProvider); // SearchWindow.Open(new SearchWindowContext(obj.screenMousePosition), searchProvider);
SearchService.ShowContextual("IPT_Tree");
} }
#endregion #endregion