diff --git a/Scripts/Editor/MenuPopup/MenuPopupWindow.cs b/Scripts/Editor/MenuPopup/MenuPopupWindow.cs new file mode 100644 index 0000000..ef9c61b --- /dev/null +++ b/Scripts/Editor/MenuPopup/MenuPopupWindow.cs @@ -0,0 +1,101 @@ +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 OnCloseA; + 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() + { + OnCloseA?.Invoke(); + } + + private string _str; + public override void OnGUI(Rect rect) + { + if (!_isInit) + { + Init(); + } + + _action(); + + 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 _action() + { + Event e = Event.current; + switch (e.type) + { + case EventType.KeyDown: + + if (e.keyCode == KeyCode.DownArrow && !_menuTree.HasFocus()) + { + _menuTree.SetFocusAndEnsureSelectedItem(); + e.Use(); + } + break; + } + } + } +} \ No newline at end of file diff --git a/Scripts/Editor/MenuPopup/MenuTreeView.cs b/Scripts/Editor/MenuPopup/MenuTreeView.cs new file mode 100644 index 0000000..79c880c --- /dev/null +++ b/Scripts/Editor/MenuPopup/MenuTreeView.cs @@ -0,0 +1,169 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEditor.IMGUI.Controls; +using UnityEngine; + +namespace XNodeEditor +{ + public class MenuTreeView:TreeView + { + class MenuItem:TreeViewItem + { + public readonly Action OnClick; + + public MenuItem(int id, int depth, string displayName, Action onClick) : base(id, depth, displayName) + { + OnClick = onClick; + } + } + + public TreeViewItem Root { get; } + + public MenuTreeView():this(new TreeViewState()) + { + } + + public MenuTreeView(TreeViewState state, MultiColumnHeader multiColumnHeader = null) : base(state, multiColumnHeader) + { + Root = new TreeViewItem(_id++,-1,nameof(Root)); + } + + private int _id = -1; + + private Dictionary> _menuCache = new Dictionary>(); + + /// + /// + /// + /// + /// + /// + public void AddItem(string menuPath,Action onClick,char symbol = '/') + { + var paths = menuPath.Split(symbol); + + int depth = 0; + + TreeViewItem last = Root; + + if (paths.Length > 1) + { + for (var i = 0; i < paths.Length - 1; i++) + { + var path = paths[i]; + + if (!_menuCache.TryGetValue(depth, out var caches)) + { + caches = new List(); + _menuCache.Add(depth, caches); + } + + while (true) + { + if (last.hasChildren) + { + foreach (var item in last.children) + { + if (item.displayName == path) + { + last = item; + depth++; + goto end; + } + } + } + + break; + } + + if (last.hasChildren) + { + foreach (var child in last.children) + { + if (child.displayName == path) + { + return; + } + } + } + + var temp = new TreeViewItem(_id++,depth++,path); + + last.AddChild(temp); + + last = temp; + + end: ; + } + } + + last.AddChild(new MenuItem(_id++,depth,paths.Last(),onClick)); + } + + protected override bool DoesItemMatchSearch(TreeViewItem item, string search) + { + if (item.parent != null && item.parent.displayName.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0) + { + return true; + } + + return base.DoesItemMatchSearch(item, search); + } + + List _ids = new List(); + protected override void DoubleClickedItem(int id) + { + var item = FindItem(id,Root); + if (item.hasChildren) + { + if (hasSearch) + { + searchString = ""; + + _ids.Clear(); + + while (item != null) + { + _ids.Add(item.id); + item = item.parent; + } + + SetExpanded(_ids); + } + else + { + SetExpanded(id, !IsExpanded(id)); + } + } + else + { + if (item is MenuItem menuItem) + { + menuItem.OnClick?.Invoke(); + } + } + } + + protected override TreeViewItem BuildRoot() + { + return Root; + } + + protected override void KeyEvent() + { + Event e = Event.current; + switch (e.type) + { + case EventType.KeyDown: + + if (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.KeypadEnter) + { + DoubleClickedItem(GetSelection()[0]); + e.Use(); + } + break; + } + } + } +} \ No newline at end of file diff --git a/Scripts/Editor/NodeGraphEditor.cs b/Scripts/Editor/NodeGraphEditor.cs index a7adf05..0eb2a62 100644 --- a/Scripts/Editor/NodeGraphEditor.cs +++ b/Scripts/Editor/NodeGraphEditor.cs @@ -1,252 +1,8 @@ using System; -using System.Collections.Generic; -using System.Linq; using UnityEditor; -using UnityEditor.IMGUI.Controls; using UnityEngine; namespace XNodeEditor { - public class MenuPopupWindow : PopupWindowContent - { - public Vector2 OpenBeforeMousePos; - private SearchField _search; - private MenuTreeView _menuTree; - public Action OnCloseA; - public MenuPopupWindow() - { - _search = new SearchField(); - _menuTree = new MenuTreeView(); - } - - private bool _isInit; - - public void AddItem(string menuPath, Action onClick, char symbol = '/',bool autoClose = true) - { - _menuTree.AddItem(menuPath, () => - { - onClick?.Invoke(); - if (autoClose) - { - editorWindow.Close(); - } - },symbol); - } - - public void Init() - { - _menuTree.Reload(); - _isInit = true; - } - - public override void OnOpen() - { - _search.SetFocus(); - } - - public override void OnClose() - { - OnCloseA?.Invoke(); - } - - private string _str; - public override void OnGUI(Rect rect) - { - if (!_isInit) - { - Init(); - } - - _action(); - - 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 _action() - { - Event e = Event.current; - switch (e.type) - { - case EventType.KeyDown: - - if (e.keyCode == KeyCode.DownArrow && !_menuTree.HasFocus()) - { - _menuTree.SetFocusAndEnsureSelectedItem(); - e.Use(); - } - break; - } - } - } - public class MenuTreeView:TreeView - { - class MenuItem:TreeViewItem - { - public readonly Action OnClick; - - public MenuItem(int id, int depth, string displayName, Action onClick) : base(id, depth, displayName) - { - OnClick = onClick; - } - } - - public TreeViewItem Root { get; } - - public MenuTreeView():this(new TreeViewState()) - { - } - - public MenuTreeView(TreeViewState state, MultiColumnHeader multiColumnHeader = null) : base(state, multiColumnHeader) - { - Root = new TreeViewItem(_id++,-1,nameof(Root)); - } - - private int _id = -1; - - private Dictionary> _menuCache = new Dictionary>(); - - /// - /// - /// - /// - /// - /// - public void AddItem(string menuPath,Action onClick,char symbol = '/') - { - var paths = menuPath.Split(symbol); - - int depth = 0; - - TreeViewItem last = Root; - - if (paths.Length > 1) - { - for (var i = 0; i < paths.Length - 1; i++) - { - var path = paths[i]; - - if (!_menuCache.TryGetValue(depth, out var caches)) - { - caches = new List(); - _menuCache.Add(depth, caches); - } - - while (true) - { - if (last.hasChildren) - { - foreach (var item in last.children) - { - if (item.displayName == path) - { - last = item; - depth++; - goto end; - } - } - } - - break; - } - - if (last.hasChildren) - { - foreach (var child in last.children) - { - if (child.displayName == path) - { - return; - } - } - } - - var temp = new TreeViewItem(_id++,depth++,path); - - last.AddChild(temp); - - last = temp; - - end: ; - } - } - - last.AddChild(new MenuItem(_id++,depth,paths.Last(),onClick)); - } - - protected override bool DoesItemMatchSearch(TreeViewItem item, string search) - { - if (item.parent != null && item.parent.displayName.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0) - { - return true; - } - - return base.DoesItemMatchSearch(item, search); - } - - List _ids = new List(); - protected override void DoubleClickedItem(int id) - { - var item = FindItem(id,Root); - if (item.hasChildren) - { - if (hasSearch) - { - searchString = ""; - - _ids.Clear(); - - while (item != null) - { - _ids.Add(item.id); - item = item.parent; - } - - SetExpanded(_ids); - } - else - { - SetExpanded(id, !IsExpanded(id)); - } - } - else - { - if (item is MenuItem menuItem) - { - menuItem.OnClick?.Invoke(); - } - } - } - - protected override TreeViewItem BuildRoot() - { - return Root; - } - - protected override void KeyEvent() - { - Event e = Event.current; - switch (e.type) - { - case EventType.KeyDown: - - if (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.KeypadEnter) - { - DoubleClickedItem(GetSelection()[0]); - e.Use(); - } - break; - } - } - } - /// Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. [CustomNodeGraphEditor(typeof(XNode.NodeGraph))] public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase { @@ -299,16 +55,16 @@ namespace XNodeEditor { NodeEditorWindow.current.AutoConnect(node); }); } -// menu.AddSeparator(""); if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0) menu.AddItem("Paste", () => { pos = NodeEditorWindow.current.WindowToGridPosition(menu.OpenBeforeMousePos); NodeEditorWindow.current.PasteNodes(pos); }); -// else menu.AddDisabledItem(new GUIContent("Paste")); + menu.AddItem("Preferences", () => NodeEditorReflection.OpenPreferences()); - menu.AddItem("创建所有的节点 ---> 测试用", () => + + menu.AddItem("Create All Node ---> Test use", () => { if (!EditorUtility.DisplayDialog("warning","Are you sure you want to create all the nodes?","ok","no")) { @@ -323,7 +79,7 @@ namespace XNodeEditor { //Get node context menu path string path = GetNodeMenuName(type); - //当前Group 不支持该节点跳过 + // skip empty path if (string.IsNullOrEmpty(path)) { continue;