mirror of
https://github.com/Siccity/xNode.git
synced 2026-03-26 22:49:02 +08:00
!W 右键添加节点的方式从GenericMenu改为PopupWindow,目前有个小bug,点出线后,右键本来应该是取消线的选择状态,但目前无法,后续在解决,问题不大=-=
This commit is contained in:
parent
2248e0fa9f
commit
eb3d24ff1d
@ -219,9 +219,9 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
// Open context menu for auto-connection
|
// Open context menu for auto-connection
|
||||||
else if (NodeEditorPreferences.GetSettings().dragToCreate && autoConnectOutput != null) {
|
else if (NodeEditorPreferences.GetSettings().dragToCreate && autoConnectOutput != null) {
|
||||||
GenericMenu menu = new GenericMenu();
|
MenuPopupWindow menu = new MenuPopupWindow();
|
||||||
graphEditor.AddContextMenuItems(menu);
|
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
|
//Release dragged connection
|
||||||
draggedOutput = null;
|
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.
|
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) {
|
} else if (!IsHoveringNode) {
|
||||||
autoConnectOutput = null;
|
autoConnectOutput = null;
|
||||||
GenericMenu menu = new GenericMenu();
|
MenuPopupWindow menu = new MenuPopupWindow();
|
||||||
graphEditor.AddContextMenuItems(menu);
|
graphEditor.AddContextMenuItems(menu);
|
||||||
menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
|
PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),menu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
isPanning = false;
|
isPanning = false;
|
||||||
|
|||||||
@ -74,6 +74,23 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
return types.ToArray();
|
return types.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Find methods marked with the [ContextMenu] attribute and add them to the context menu </summary>
|
||||||
|
public static void AddCustomContextMenuItems(this MenuPopupWindow contextMenu, object obj) {
|
||||||
|
KeyValuePair<ContextMenu, MethodInfo>[] items = GetContextMenuMethods(obj);
|
||||||
|
if (items.Length != 0) {
|
||||||
|
List<string> invalidatedEntries = new List<string>();
|
||||||
|
foreach (KeyValuePair<ContextMenu, MethodInfo> 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<ContextMenu, MethodInfo> kvp = items[i];
|
||||||
|
contextMenu.AddItem(kvp.Key.menuItem, () => kvp.Value.Invoke(obj, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary> Find methods marked with the [ContextMenu] attribute and add them to the context menu </summary>
|
/// <summary> Find methods marked with the [ContextMenu] attribute and add them to the context menu </summary>
|
||||||
public static void AddCustomContextMenuItems(this GenericMenu contextMenu, object obj) {
|
public static void AddCustomContextMenuItems(this GenericMenu contextMenu, object obj) {
|
||||||
|
|||||||
@ -2,9 +2,199 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
|
using UnityEditor.IMGUI.Controls;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace XNodeEditor {
|
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<int, List<string>> _menuCache = new Dictionary<int, List<string>>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="menuPath"></param>
|
||||||
|
/// <param name="onClick"></param>
|
||||||
|
/// <param name="symbol"></param>
|
||||||
|
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<string>();
|
||||||
|
_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<int> _ids = new List<int>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>
|
/// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>
|
||||||
[CustomNodeGraphEditor(typeof(XNode.NodeGraph))]
|
[CustomNodeGraphEditor(typeof(XNode.NodeGraph))]
|
||||||
public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph> {
|
public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph> {
|
||||||
@ -42,7 +232,7 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
|
/// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
|
||||||
public virtual void AddContextMenuItems(GenericMenu menu) {
|
public virtual void AddContextMenuItems(MenuPopupWindow menu) {
|
||||||
Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);
|
Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);
|
||||||
for (int i = 0; i < NodeEditorReflection.nodeTypes.Length; i++) {
|
for (int i = 0; i < NodeEditorReflection.nodeTypes.Length; i++) {
|
||||||
Type type = NodeEditorReflection.nodeTypes[i];
|
Type type = NodeEditorReflection.nodeTypes[i];
|
||||||
@ -51,17 +241,23 @@ namespace XNodeEditor {
|
|||||||
string path = GetNodeMenuName(type);
|
string path = GetNodeMenuName(type);
|
||||||
if (string.IsNullOrEmpty(path)) continue;
|
if (string.IsNullOrEmpty(path)) continue;
|
||||||
|
|
||||||
menu.AddItem(new GUIContent(path), false, () => {
|
menu.AddItem(path, () => {
|
||||||
XNode.Node node = CreateNode(type, pos);
|
XNode.Node node = CreateNode(type, pos);
|
||||||
NodeEditorWindow.current.AutoConnect(node);
|
NodeEditorWindow.current.AutoConnect(node);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
menu.AddSeparator("");
|
// menu.AddSeparator("");
|
||||||
if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0) menu.AddItem(new GUIContent("Paste"), false, () => NodeEditorWindow.current.PasteNodes(pos));
|
if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0)
|
||||||
else menu.AddDisabledItem(new GUIContent("Paste"));
|
menu.AddItem("Paste", () => NodeEditorWindow.current.PasteNodes(pos));
|
||||||
menu.AddItem(new GUIContent("Preferences"), false, () => NodeEditorReflection.OpenPreferences());
|
// else menu.AddDisabledItem(new GUIContent("Paste"));
|
||||||
menu.AddItem(new GUIContent("创建所有的节点 ---> 测试用"), false, () =>
|
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++)
|
for (int i = 0; i < NodeEditorReflection.nodeTypes.Length; i++)
|
||||||
{
|
{
|
||||||
Type type = NodeEditorReflection.nodeTypes[i];
|
Type type = NodeEditorReflection.nodeTypes[i];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user