From ac7d1a580e19c29eb0caeddd11277ee6bdb825f0 Mon Sep 17 00:00:00 2001 From: tangzefei Date: Wed, 30 Jun 2021 16:28:16 +0800 Subject: [PATCH 1/2] NodePort context menu add 'remove'/'rename' Remove dynamic port Rename dyname port --- Scripts/Editor/NodeEditorGUI.cs | 13 +++++ Scripts/Editor/TextInputPopup.cs | 71 +++++++++++++++++++++++++++ Scripts/Editor/TextInputPopup.cs.meta | 11 +++++ Scripts/Node.cs | 22 +++++++++ Scripts/NodeGraph.cs | 3 +- 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 Scripts/Editor/TextInputPopup.cs create mode 100644 Scripts/Editor/TextInputPopup.cs.meta diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 70c4cd1..2d1b696 100755 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -118,6 +118,19 @@ namespace XNodeEditor { contextMenu.AddItem(new GUIContent(string.Format("Disconnect({0})", name)), false, () => hoveredPort.Disconnect(index)); } contextMenu.AddItem(new GUIContent("Clear Connections"), false, () => hoveredPort.ClearConnections()); + if (hoveredPort.IsDynamic && hoveredPort.IsOutput) { + contextMenu.AddItem(new GUIContent($"Remove({hoveredPort.fieldName})"), false, () => { + hoveredPort.node.RemoveDynamicPort(hoveredPort); + }); + contextMenu.AddItem(new GUIContent($"Rename({hoveredPort.fieldName})"), false, () => { + TextInputPopup dlg = TextInputPopup.Show(hoveredPort.fieldName, "Rename Port", "Port Name"); + dlg.onAfterClose += () => { + XNode.Node node = hoveredPort.node; + node.RenameDynamicPort(hoveredPort, dlg.input); + EditorUtility.SetDirty(graph); + }; + }); + } //Get compatible nodes with this port if (NodeEditorPreferences.GetSettings().createFilter) { contextMenu.AddSeparator(""); diff --git a/Scripts/Editor/TextInputPopup.cs b/Scripts/Editor/TextInputPopup.cs new file mode 100644 index 0000000..027e700 --- /dev/null +++ b/Scripts/Editor/TextInputPopup.cs @@ -0,0 +1,71 @@ +using System; +using UnityEditor; +using UnityEngine; + +namespace XNodeEditor { + public class TextInputPopup : EditorWindow { + private string inputControlName = ""; + + public static TextInputPopup current { get; private set; } + public string input; + public Action onAfterClose; + private bool firstFrame = true; + + /// Show a rename popup for an asset at mouse position. Will trigger reimport of the asset on apply. + public static TextInputPopup Show( + string textContnet, + string title = "Text", + string inputControlName = "Input", + float width = 200) { + TextInputPopup window = EditorWindow.GetWindow(true, title, true); + if (current != null) current.Close(); + current = window; + window.inputControlName = inputControlName; + window.input = textContnet; + window.minSize = new Vector2(100, 44); + window.position = new Rect(0, 0, width, 44); + window.UpdatePositionToMouse(); + return window; + } + + private void UpdatePositionToMouse() { + if (Event.current == null) return; + Vector3 mousePoint = GUIUtility.GUIToScreenPoint(Event.current.mousePosition); + Rect pos = position; + pos.x = mousePoint.x - position.width * 0.5f; + pos.y = mousePoint.y - 10; + position = pos; + } + + private void OnLostFocus() { + // Make the popup close on lose focus + Close(); + } + + private void OnGUI() { + if (firstFrame) { + UpdatePositionToMouse(); + firstFrame = false; + } + GUI.SetNextControlName(inputControlName); + input = EditorGUILayout.TextField(input); + EditorGUI.FocusTextInControl(inputControlName); + Event e = Event.current; + // If input is empty, revert name to default instead + if (input != null && input.Trim() != "") { + if (GUILayout.Button("Confirm") || (e.isKey && e.keyCode == KeyCode.Return)) { + Close(); + onAfterClose?.Invoke(); + } + } + + if (e.isKey && e.keyCode == KeyCode.Escape) { + Close(); + } + } + + private void OnDestroy() { + EditorGUIUtility.editingTextField = false; + } + } +} \ No newline at end of file diff --git a/Scripts/Editor/TextInputPopup.cs.meta b/Scripts/Editor/TextInputPopup.cs.meta new file mode 100644 index 0000000..aefb863 --- /dev/null +++ b/Scripts/Editor/TextInputPopup.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 72aa667b695ba02478ca2a447c37c410 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 704e99d..28b7128 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -181,10 +181,32 @@ namespace XNode { public void RemoveDynamicPort(NodePort port) { if (port == null) throw new ArgumentNullException("port"); else if (port.IsStatic) throw new ArgumentException("cannot remove static port"); + OnRemoveDynamicPort(port); port.ClearConnections(); ports.Remove(port.fieldName); } + /// Rename an dynamic port + public void RenameDynamicPort(NodePort port, string newFileName) { + if(!port.IsDynamic) { + return; + } + XNode.NodePort newPort = AddDynamicOutput( + port.ValueType, + port.connectionType, + port.typeConstraint, + newFileName + ); + if (port.Connection != null) { + newPort.Connect(port.Connection); + } + OnRenameDynamicPort(port.fieldName, newFileName); + RemoveDynamicPort(port); + } + + protected virtual void OnRenameDynamicPort(string oldName, string newName) { } + protected virtual void OnRemoveDynamicPort(NodePort port) { } + /// Removes all dynamic ports from the node [ContextMenu("Clear Dynamic Ports")] public void ClearDynamicPorts() { diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index d928f94..0a0d8e2 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -1,4 +1,5 @@ -using System; +using Sirenix.OdinInspector; +using System; using System.Collections.Generic; using UnityEngine; From 82a102459a1a798b07563fbd31f7becf2bea25f8 Mon Sep 17 00:00:00 2001 From: tangzefei Date: Wed, 30 Jun 2021 16:33:14 +0800 Subject: [PATCH 2/2] Remove using Sirenix.OdinInspector; --- Scripts/NodeGraph.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index 0a0d8e2..d928f94 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -1,5 +1,4 @@ -using Sirenix.OdinInspector; -using System; +using System; using System.Collections.Generic; using UnityEngine;