using Codice.Client.Common; using System; using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEditor.Search; using UnityEditor.SearchService; using UnityEngine; namespace AI.BT { internal class NodeSearchWindow : ScriptableObject, ISearchWindowProvider { private Texture2D _indentation; //passes node type and mouse position public Action<System.Type, Vector2> OnEntrySelected { get; internal set; } public void OnEnable() { _indentation = new Texture2D(1, 1); _indentation.SetPixel(0, 0, Color.clear); _indentation.Apply(); } public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context) { var entry = new List<SearchTreeEntry>() { new SearchTreeGroupEntry(new GUIContent("Create New Node"), 0)}; CreateNodeGroup(typeof(BTNode), 1, ref entry); return entry; } public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context) { OnEntrySelected?.Invoke(SearchTreeEntry.userData as System.Type, context.screenMousePosition); return true; } private void CreateNodeGroup(System.Type nodetype, int level, ref List<SearchTreeEntry> entry ) { foreach(var type in TypeCache.GetTypesDerivedFrom(nodetype).Where(t => t.BaseType == nodetype)) { var displayname = BTNode.GetDisplayName(type); if (type.IsAbstract) { entry.Add(new SearchTreeGroupEntry(new GUIContent(displayname), level)); CreateNodeGroup(type, level + 1, ref entry); } else { entry.Add(new SearchTreeEntry(new GUIContent(displayname, _indentation)) { level = level, userData = type }); } } } } }