1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-02-08 16:24:53 +08:00

Move context menu functions around to allow more customization (#87)

Moved NodeEditorWindow.ShowNodeContextMenu to NodeEditor.AddContextMenuItems
Moved NodeEditorWindow.ShowGraphContextMenu to NodeGraphEditor.AddContextMenuItems
Moved NodeEditorWindow.CreateNode to NodeGraphEditor.CreateNode
This commit is contained in:
Thor Brigsted 2018-12-15 12:40:16 +01:00 committed by GitHub
parent a0eee5b9ca
commit 5b11e81027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 65 deletions

View File

@ -81,6 +81,26 @@ namespace XNodeEditor {
return NodeEditorResources.styles.nodeBody;
}
/// <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) {
// Actions if only one node is selected
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
XNode.Node node = Selection.activeObject as XNode.Node;
menu.AddItem(new GUIContent("Move To Top"), false, () => NodeEditorWindow.current.MoveNodeToTop(node));
menu.AddItem(new GUIContent("Rename"), false, NodeEditorWindow.current.RenameSelectedNode);
}
// Add actions to any number of selected nodes
menu.AddItem(new GUIContent("Duplicate"), false, NodeEditorWindow.current.DuplicateSelectedNodes);
menu.AddItem(new GUIContent("Remove"), false, NodeEditorWindow.current.RemoveSelectedNodes);
// Custom sctions if only one node is selected
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
XNode.Node node = Selection.activeObject as XNode.Node;
NodeEditorWindow.AddCustomContextMenuItems(menu, node);
}
}
public void InitiateRename() {
renaming = 1;
}

View File

@ -260,9 +260,13 @@ namespace XNodeEditor {
ShowPortContextMenu(hoveredPort);
} else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
if (!Selection.Contains(hoveredNode)) SelectNode(hoveredNode, false);
ShowNodeContextMenu();
GenericMenu menu = new GenericMenu();
NodeEditor.GetEditor(hoveredNode).AddContextMenuItems(menu);
menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
} else if (!IsHoveringNode) {
ShowGraphContextMenu();
GenericMenu menu = new GenericMenu();
graphEditor.AddContextMenuItems(menu);
menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
}
}
isPanning = false;
@ -323,15 +327,6 @@ namespace XNodeEditor {
panOffset = Vector2.zero;
}
public void CreateNode(Type type, Vector2 position) {
XNode.Node node = graph.AddNode(type);
node.position = position;
node.name = UnityEditor.ObjectNames.NicifyVariableName(type.Name);
AssetDatabase.AddObjectToAsset(node, graph);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
Repaint();
}
/// <summary> Remove nodes in the graph in Selection.objects</summary>
public void RemoveSelectedNodes() {
// We need to delete reroutes starting at the highest point index to avoid shifting indices

View File

@ -116,60 +116,6 @@ namespace XNodeEditor {
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
}
/// <summary> Show right-click context menu for selected nodes </summary>
public void ShowNodeContextMenu() {
GenericMenu contextMenu = new GenericMenu();
// If only one node is selected
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
XNode.Node node = Selection.activeObject as XNode.Node;
contextMenu.AddItem(new GUIContent("Move To Top"), false, () => MoveNodeToTop(node));
contextMenu.AddItem(new GUIContent("Rename"), false, RenameSelectedNode);
}
contextMenu.AddItem(new GUIContent("Duplicate"), false, DuplicateSelectedNodes);
contextMenu.AddItem(new GUIContent("Remove"), false, RemoveSelectedNodes);
// If only one node is selected
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
XNode.Node node = Selection.activeObject as XNode.Node;
AddCustomContextMenuItems(contextMenu, node);
}
contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
}
/// <summary> Show right-click context menu for current graph </summary>
void ShowGraphContextMenu() {
GenericMenu contextMenu = new GenericMenu();
Vector2 pos = WindowToGridPosition(Event.current.mousePosition);
for (int i = 0; i < nodeTypes.Length; i++) {
Type type = nodeTypes[i];
//Get node context menu path
string path = graphEditor.GetNodeMenuName(type);
if (string.IsNullOrEmpty(path)) continue;
contextMenu.AddItem(new GUIContent(path), false, () => {
CreateNode(type, pos);
});
}
contextMenu.AddSeparator("");
contextMenu.AddItem(new GUIContent("Preferences"), false, () => OpenPreferences());
AddCustomContextMenuItems(contextMenu, graph);
contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
}
void AddCustomContextMenuItems(GenericMenu contextMenu, object obj) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo>[] items = GetContextMenuMethods(obj);
if (items.Length != 0) {
contextMenu.AddSeparator("");
for (int i = 0; i < items.Length; i++) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo> kvp = items[i];
contextMenu.AddItem(new GUIContent(kvp.Key.menuItem), false, () => kvp.Value.Invoke(obj, null));
}
}
}
/// <summary> Draw a bezier from startpoint to endpoint, both in grid coordinates </summary>
public void DrawConnection(Vector2 startPoint, Vector2 endPoint, Color col) {
startPoint = GridToWindowPosition(startPoint);

View File

@ -71,6 +71,17 @@ namespace XNodeEditor {
return types.ToArray();
}
public static void AddCustomContextMenuItems(GenericMenu contextMenu, object obj) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo>[] items = GetContextMenuMethods(obj);
if (items.Length != 0) {
contextMenu.AddSeparator("");
for (int i = 0; i < items.Length; i++) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo> kvp = items[i];
contextMenu.AddItem(new GUIContent(kvp.Key.menuItem), false, () => kvp.Value.Invoke(obj, null));
}
}
}
public static KeyValuePair<ContextMenu, MethodInfo>[] GetContextMenuMethods(object obj) {
Type type = obj.GetType();
MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

View File

@ -38,10 +38,39 @@ namespace XNodeEditor {
return ObjectNames.NicifyVariableName(type.ToString().Replace('.', '/'));
}
/// <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) {
Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);
for (int i = 0; i < NodeEditorWindow.nodeTypes.Length; i++) {
Type type = NodeEditorWindow.nodeTypes[i];
//Get node context menu path
string path = GetNodeMenuName(type);
if (string.IsNullOrEmpty(path)) continue;
menu.AddItem(new GUIContent(path), false, () => {
CreateNode(type, pos);
});
}
menu.AddSeparator("");
menu.AddItem(new GUIContent("Preferences"), false, () => NodeEditorWindow.OpenPreferences());
NodeEditorWindow.AddCustomContextMenuItems(menu, target);
}
public virtual Color GetTypeColor(Type type) {
return NodeEditorPreferences.GetTypeColor(type);
}
/// <summary> Create a node and save it in the graph asset </summary>
public virtual void CreateNode(Type type, Vector2 position) {
XNode.Node node = target.AddNode(type);
node.position = position;
node.name = UnityEditor.ObjectNames.NicifyVariableName(type.Name);
AssetDatabase.AddObjectToAsset(node, target);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
NodeEditorWindow.RepaintAll();
}
/// <summary> Creates a copy of the original node in the graph </summary>
public XNode.Node CopyNode(XNode.Node original) {
XNode.Node node = target.CopyNode(original);