Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Scripts/Editor/NodeEditorGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ void ShowPortContextMenu(XNode.NodePort hoveredPort) {
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 += () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about unsubscribe?

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("");
Expand Down
71 changes: 71 additions & 0 deletions Scripts/Editor/TextInputPopup.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary> 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<TextInputPopup>(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;
}
}
}
11 changes: 11 additions & 0 deletions Scripts/Editor/TextInputPopup.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Scripts/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,32 @@ public void RemoveDynamicPort(string fieldName) {
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);
}

/// <summary> Rename an dynamic port</summary>
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) { }

/// <summary> Removes all dynamic ports from the node </summary>
[ContextMenu("Clear Dynamic Ports")]
public void ClearDynamicPorts() {
Expand Down