using System; using UnityEditor; using UnityEditor.IMGUI.Controls; using UnityEngine; namespace XNodeEditor { /// /// Menu Popup Window /// public class MenuPopupWindow : PopupWindowContent { public Vector2 openBeforeMousePos; private SearchField search; private MenuTreeView menuTree; public Action onCloseAction; public MenuPopupWindow() { search = new SearchField(); menuTree = new MenuTreeView(); } private bool isInit; /// /// Add Item /// /// Item Path /// /// Path separator /// Automatically close window after selecting an item public void AddItem(string menuPath, Action onClick, char symbol = '/',bool autoClose = true) { menuTree.AddItem(menuPath, () => { onClick?.Invoke(); if (autoClose) { editorWindow.Close(); } },symbol); } /// /// Init or Reload Tree /// public void Init() { menuTree.Reload(); isInit = true; } public override void OnOpen() { search.SetFocus(); } public override void OnClose() { onCloseAction?.Invoke(); } private string _str; public override void OnGUI(Rect rect) { if (!isInit) { Init(); } EventAction(); EditorGUI.BeginChangeCheck(); { _str = search.OnGUI(new Rect(rect.position, new Vector2(rect.width, 20)),_str); } if (EditorGUI.EndChangeCheck()) { menuTree.searchString = _str; } menuTree.OnGUI(new Rect(new Vector2(0,25),rect.size - new Vector2(0,20))); } private void EventAction() { Event e = Event.current; switch (e.type) { case EventType.KeyDown: if (e.keyCode == KeyCode.DownArrow && !menuTree.HasFocus()) { menuTree.SetFocusAndEnsureSelectedItem(); e.Use(); } break; } } } }