mirror of
https://github.com/Siccity/xNode.git
synced 2026-03-26 22:49:02 +08:00
!OA(Menu Popup Window) Move to MenuPopup folder
This commit is contained in:
parent
c0b80b59da
commit
7624d4ad1f
101
Scripts/Editor/MenuPopup/MenuPopupWindow.cs
Normal file
101
Scripts/Editor/MenuPopup/MenuPopupWindow.cs
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
using System;
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEditor.IMGUI.Controls;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace XNodeEditor
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Menu Popup Window
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add Item
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="menuPath">Item Path</param>
|
||||||
|
/// <param name="onClick"></param>
|
||||||
|
/// <param name="symbol">Path separator</param>
|
||||||
|
/// <param name="autoClose">Automatically close window after selecting an item</param>
|
||||||
|
public void AddItem(string menuPath, Action onClick, char symbol = '/',bool autoClose = true)
|
||||||
|
{
|
||||||
|
_menuTree.AddItem(menuPath, () =>
|
||||||
|
{
|
||||||
|
onClick?.Invoke();
|
||||||
|
if (autoClose)
|
||||||
|
{
|
||||||
|
editorWindow.Close();
|
||||||
|
}
|
||||||
|
},symbol);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Init or Reload Tree
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
169
Scripts/Editor/MenuPopup/MenuTreeView.cs
Normal file
169
Scripts/Editor/MenuPopup/MenuTreeView.cs
Normal file
@ -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<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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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<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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,252 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEditor.IMGUI.Controls;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace XNodeEditor {
|
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<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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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<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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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> {
|
||||||
@ -299,16 +55,16 @@ namespace XNodeEditor {
|
|||||||
NodeEditorWindow.current.AutoConnect(node);
|
NodeEditorWindow.current.AutoConnect(node);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// menu.AddSeparator("");
|
|
||||||
if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0)
|
if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0)
|
||||||
menu.AddItem("Paste", () =>
|
menu.AddItem("Paste", () =>
|
||||||
{
|
{
|
||||||
pos = NodeEditorWindow.current.WindowToGridPosition(menu.OpenBeforeMousePos);
|
pos = NodeEditorWindow.current.WindowToGridPosition(menu.OpenBeforeMousePos);
|
||||||
NodeEditorWindow.current.PasteNodes(pos);
|
NodeEditorWindow.current.PasteNodes(pos);
|
||||||
});
|
});
|
||||||
// else menu.AddDisabledItem(new GUIContent("Paste"));
|
|
||||||
menu.AddItem("Preferences", () => NodeEditorReflection.OpenPreferences());
|
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"))
|
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
|
//Get node context menu path
|
||||||
string path = GetNodeMenuName(type);
|
string path = GetNodeMenuName(type);
|
||||||
//当前Group 不支持该节点跳过
|
// skip empty path
|
||||||
if (string.IsNullOrEmpty(path))
|
if (string.IsNullOrEmpty(path))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user