UnityImageProcessing_Package/Scripts/Editor/Windows/ImageProcessingGraphSearchProvider.cs
2025-04-26 19:40:12 +01:00

138 lines
5.1 KiB
C#

using System;
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
{
public struct SearchContextElement
{
public object target { get; private set; }
public string title { get; private set; }
public SearchContextElement(object target, string title)
{
this.target = target;
this.title = title;
}
}
public class ImageProcessingGraphSearchProvider : ScriptableObject, ISearchWindowProvider
{
public ImageProcessingGraphViewWindow graph;
public VisualElement target;
public static List<SearchContextElement> elements;
private Assembly[] assemblies;
public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context)
{
List<SearchTreeEntry> tree = new List<SearchTreeEntry>();
tree.Add(new SearchTreeGroupEntry(new GUIContent("Nodes"), 0));
elements = new List<SearchContextElement>();
/*
assemblies = AppDomain.CurrentDomain.GetAssemblies();
*/
/*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<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)
{
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;
return true;
}
}
}