From eb3d24ff1d2999e8183498bfd11c724738892ac8 Mon Sep 17 00:00:00 2001
From: Icarus <1375400884@qq.com>
Date: Sun, 24 Nov 2019 04:56:46 +0800
Subject: [PATCH] =?UTF-8?q?!W=20=E5=8F=B3=E9=94=AE=E6=B7=BB=E5=8A=A0?=
=?UTF-8?q?=E8=8A=82=E7=82=B9=E7=9A=84=E6=96=B9=E5=BC=8F=E4=BB=8E`GenericM?=
=?UTF-8?q?enu`=E6=94=B9=E4=B8=BA`PopupWindow`,=E7=9B=AE=E5=89=8D=E6=9C=89?=
=?UTF-8?q?=E4=B8=AA=E5=B0=8Fbug,=E7=82=B9=E5=87=BA=E7=BA=BF=E5=90=8E,?=
=?UTF-8?q?=E5=8F=B3=E9=94=AE=E6=9C=AC=E6=9D=A5=E5=BA=94=E8=AF=A5=E6=98=AF?=
=?UTF-8?q?=E5=8F=96=E6=B6=88=E7=BA=BF=E7=9A=84=E9=80=89=E6=8B=A9=E7=8A=B6?=
=?UTF-8?q?=E6=80=81,=E4=BD=86=E7=9B=AE=E5=89=8D=E6=97=A0=E6=B3=95,?=
=?UTF-8?q?=E5=90=8E=E7=BB=AD=E5=9C=A8=E8=A7=A3=E5=86=B3,=E9=97=AE?=
=?UTF-8?q?=E9=A2=98=E4=B8=8D=E5=A4=A7=3D-=3D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Scripts/Editor/NodeEditorAction.cs | 8 +-
Scripts/Editor/NodeEditorReflection.cs | 17 ++
Scripts/Editor/NodeGraphEditor.cs | 210 ++++++++++++++++++++++++-
3 files changed, 224 insertions(+), 11 deletions(-)
diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs
index 35fbdd1..6894c88 100644
--- a/Scripts/Editor/NodeEditorAction.cs
+++ b/Scripts/Editor/NodeEditorAction.cs
@@ -219,9 +219,9 @@ namespace XNodeEditor {
}
// Open context menu for auto-connection
else if (NodeEditorPreferences.GetSettings().dragToCreate && autoConnectOutput != null) {
- GenericMenu menu = new GenericMenu();
+ MenuPopupWindow menu = new MenuPopupWindow();
graphEditor.AddContextMenuItems(menu);
- menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
+ PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),menu);
}
//Release dragged connection
draggedOutput = null;
@@ -281,9 +281,9 @@ namespace XNodeEditor {
e.Use(); // Fixes copy/paste context menu appearing in Unity 5.6.6f2 - doesn't occur in 2018.3.2f1 Probably needs to be used in other places.
} else if (!IsHoveringNode) {
autoConnectOutput = null;
- GenericMenu menu = new GenericMenu();
+ MenuPopupWindow menu = new MenuPopupWindow();
graphEditor.AddContextMenuItems(menu);
- menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
+ PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),menu);
}
}
isPanning = false;
diff --git a/Scripts/Editor/NodeEditorReflection.cs b/Scripts/Editor/NodeEditorReflection.cs
index 0a0a36a..27136e0 100644
--- a/Scripts/Editor/NodeEditorReflection.cs
+++ b/Scripts/Editor/NodeEditorReflection.cs
@@ -74,6 +74,23 @@ namespace XNodeEditor {
}
return types.ToArray();
}
+
+ /// Find methods marked with the [ContextMenu] attribute and add them to the context menu
+ public static void AddCustomContextMenuItems(this MenuPopupWindow contextMenu, object obj) {
+ KeyValuePair[] items = GetContextMenuMethods(obj);
+ if (items.Length != 0) {
+ List invalidatedEntries = new List();
+ foreach (KeyValuePair checkValidate in items) {
+ if (checkValidate.Key.validate && !(bool) checkValidate.Value.Invoke(obj, null)) {
+ invalidatedEntries.Add(checkValidate.Key.menuItem);
+ }
+ }
+ for (int i = 0; i < items.Length; i++) {
+ KeyValuePair kvp = items[i];
+ contextMenu.AddItem(kvp.Key.menuItem, () => kvp.Value.Invoke(obj, null));
+ }
+ }
+ }
/// Find methods marked with the [ContextMenu] attribute and add them to the context menu
public static void AddCustomContextMenuItems(this GenericMenu contextMenu, object obj) {
diff --git a/Scripts/Editor/NodeGraphEditor.cs b/Scripts/Editor/NodeGraphEditor.cs
index 7b706fa..a17c1c4 100644
--- a/Scripts/Editor/NodeGraphEditor.cs
+++ b/Scripts/Editor/NodeGraphEditor.cs
@@ -2,9 +2,199 @@
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
+using UnityEditor.IMGUI.Controls;
using UnityEngine;
namespace XNodeEditor {
+ public class MenuPopupWindow : PopupWindowContent
+ {
+ private SearchField _search;
+ private MenuTreeView _menuTree;
+ 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();
+ }
+
+ private string _str;
+ public override void OnGUI(Rect rect)
+ {
+ if (!_isInit)
+ {
+ Init();
+ }
+
+ 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)));
+ }
+ }
+ 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;
+ }
+
+ 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;
+ }
+ }
+
/// 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 {
@@ -42,7 +232,7 @@ namespace XNodeEditor {
}
/// Add items for the context menu when right-clicking this node. Override to add custom menu items.
- public virtual void AddContextMenuItems(GenericMenu menu) {
+ public virtual void AddContextMenuItems(MenuPopupWindow menu) {
Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);
for (int i = 0; i < NodeEditorReflection.nodeTypes.Length; i++) {
Type type = NodeEditorReflection.nodeTypes[i];
@@ -51,17 +241,23 @@ namespace XNodeEditor {
string path = GetNodeMenuName(type);
if (string.IsNullOrEmpty(path)) continue;
- menu.AddItem(new GUIContent(path), false, () => {
+ menu.AddItem(path, () => {
XNode.Node node = CreateNode(type, pos);
NodeEditorWindow.current.AutoConnect(node);
});
}
- menu.AddSeparator("");
- if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0) menu.AddItem(new GUIContent("Paste"), false, () => NodeEditorWindow.current.PasteNodes(pos));
- else menu.AddDisabledItem(new GUIContent("Paste"));
- menu.AddItem(new GUIContent("Preferences"), false, () => NodeEditorReflection.OpenPreferences());
- menu.AddItem(new GUIContent("创建所有的节点 ---> 测试用"), false, () =>
+// menu.AddSeparator("");
+ if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0)
+ menu.AddItem("Paste", () => NodeEditorWindow.current.PasteNodes(pos));
+// else menu.AddDisabledItem(new GUIContent("Paste"));
+ menu.AddItem("Preferences", () => NodeEditorReflection.OpenPreferences());
+ menu.AddItem("创建所有的节点 ---> 测试用", () =>
{
+ if (!EditorUtility.DisplayDialog("warning","Are you sure you want to create all the nodes?","ok","no"))
+ {
+ return;
+ }
+
for (int i = 0; i < NodeEditorReflection.nodeTypes.Length; i++)
{
Type type = NodeEditorReflection.nodeTypes[i];