mirror of
https://github.com/Siccity/xNode.git
synced 2026-03-26 22:49:02 +08:00
Added NodeRename #11
This commit is contained in:
parent
85d15871ca
commit
19e244212c
@ -13,6 +13,7 @@ namespace XNodeEditor {
|
|||||||
/// <summary> Fires every whenever a node was modified through the editor </summary>
|
/// <summary> Fires every whenever a node was modified through the editor </summary>
|
||||||
public static Action<XNode.Node> onUpdateNode;
|
public static Action<XNode.Node> onUpdateNode;
|
||||||
public static Dictionary<XNode.NodePort, Vector2> portPositions;
|
public static Dictionary<XNode.NodePort, Vector2> portPositions;
|
||||||
|
public static int renaming;
|
||||||
|
|
||||||
/// <summary> Draws the node GUI.</summary>
|
/// <summary> Draws the node GUI.</summary>
|
||||||
/// <param name="portPositions">Port handle positions need to be returned to the NodeEditorWindow </param>
|
/// <param name="portPositions">Port handle positions need to be returned to the NodeEditorWindow </param>
|
||||||
@ -24,7 +25,21 @@ namespace XNodeEditor {
|
|||||||
public virtual void OnHeaderGUI() {
|
public virtual void OnHeaderGUI() {
|
||||||
GUI.color = Color.white;
|
GUI.color = Color.white;
|
||||||
string title = target.name;
|
string title = target.name;
|
||||||
GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
|
if (renaming != 0 && Selection.Contains(target)) {
|
||||||
|
int controlID = EditorGUIUtility.GetControlID(FocusType.Keyboard) + 1;
|
||||||
|
if (renaming == 1) {
|
||||||
|
EditorGUIUtility.keyboardControl = controlID;
|
||||||
|
EditorGUIUtility.editingTextField = true;
|
||||||
|
renaming = 2;
|
||||||
|
}
|
||||||
|
target.name = EditorGUILayout.TextField(target.name, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
|
||||||
|
if (!EditorGUIUtility.editingTextField) {
|
||||||
|
Rename(target.name);
|
||||||
|
renaming = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Draws standard field editors for all public fields </summary>
|
/// <summary> Draws standard field editors for all public fields </summary>
|
||||||
@ -52,6 +67,15 @@ namespace XNodeEditor {
|
|||||||
else return Color.white;
|
else return Color.white;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void InitiateRename() {
|
||||||
|
renaming = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Rename(string newName) {
|
||||||
|
target.name = newName;
|
||||||
|
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
|
||||||
|
}
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class CustomNodeEditorAttribute : Attribute,
|
public class CustomNodeEditorAttribute : Attribute,
|
||||||
XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib {
|
XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib {
|
||||||
|
|||||||
@ -291,6 +291,9 @@ namespace XNodeEditor {
|
|||||||
public void CreateNode(Type type, Vector2 position) {
|
public void CreateNode(Type type, Vector2 position) {
|
||||||
XNode.Node node = graph.AddNode(type);
|
XNode.Node node = graph.AddNode(type);
|
||||||
node.position = position;
|
node.position = position;
|
||||||
|
node.name = UnityEditor.ObjectNames.NicifyVariableName(type.ToString());
|
||||||
|
AssetDatabase.AddObjectToAsset(node, graph);
|
||||||
|
AssetDatabase.SaveAssets();
|
||||||
Repaint();
|
Repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,6 +307,15 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Draw this node on top of other nodes by placing it last in the graph.nodes list </summary>
|
||||||
|
public void MoveNodeToTop(XNode.Node node) {
|
||||||
|
int index;
|
||||||
|
while ((index = graph.nodes.IndexOf(node)) != graph.nodes.Count - 1) {
|
||||||
|
graph.nodes[index] = graph.nodes[index + 1];
|
||||||
|
graph.nodes[index + 1] = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary> Dublicate selected nodes and select the dublicates </summary>
|
/// <summary> Dublicate selected nodes and select the dublicates </summary>
|
||||||
public void DublicateSelectedNodes() {
|
public void DublicateSelectedNodes() {
|
||||||
UnityEngine.Object[] newNodes = new UnityEngine.Object[Selection.objects.Length];
|
UnityEngine.Object[] newNodes = new UnityEngine.Object[Selection.objects.Length];
|
||||||
|
|||||||
@ -109,13 +109,8 @@ namespace XNodeEditor {
|
|||||||
// If only one node is selected
|
// If only one node is selected
|
||||||
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
|
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
|
||||||
XNode.Node node = Selection.activeObject as XNode.Node;
|
XNode.Node node = Selection.activeObject as XNode.Node;
|
||||||
contextMenu.AddItem(new GUIContent("Move To Top"), false, () => {
|
contextMenu.AddItem(new GUIContent("Move To Top"), false, () => MoveNodeToTop(node));
|
||||||
int index;
|
contextMenu.AddItem(new GUIContent("Rename"), false, NodeEditor.GetEditor(node).InitiateRename);
|
||||||
while ((index = graph.nodes.IndexOf(node)) != graph.nodes.Count - 1) {
|
|
||||||
graph.nodes[index] = graph.nodes[index + 1];
|
|
||||||
graph.nodes[index + 1] = node;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
contextMenu.AddItem(new GUIContent("Duplicate"), false, DublicateSelectedNodes);
|
contextMenu.AddItem(new GUIContent("Duplicate"), false, DublicateSelectedNodes);
|
||||||
|
|||||||
@ -10,6 +10,7 @@ namespace XNodeEditor {
|
|||||||
public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph> {
|
public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph> {
|
||||||
/// <summary> Custom node editors defined with [CustomNodeGraphEditor] </summary>
|
/// <summary> Custom node editors defined with [CustomNodeGraphEditor] </summary>
|
||||||
[NonSerialized] private static Dictionary<Type, NodeGraphEditor> editors;
|
[NonSerialized] private static Dictionary<Type, NodeGraphEditor> editors;
|
||||||
|
protected bool isRenaming;
|
||||||
|
|
||||||
public virtual Texture2D GetGridTexture() {
|
public virtual Texture2D GetGridTexture() {
|
||||||
return NodeEditorPreferences.GetSettings().gridTexture;
|
return NodeEditorPreferences.GetSettings().gridTexture;
|
||||||
|
|||||||
@ -71,7 +71,7 @@ namespace XNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Initialize node. Called on creation. </summary>
|
/// <summary> Initialize node. Called on creation. </summary>
|
||||||
protected virtual void Init() { name = GetType().Name; }
|
protected virtual void Init() { }
|
||||||
|
|
||||||
/// <summary> Checks all connections for invalid references, and removes them. </summary>
|
/// <summary> Checks all connections for invalid references, and removes them. </summary>
|
||||||
public void VerifyConnections() {
|
public void VerifyConnections() {
|
||||||
|
|||||||
@ -19,13 +19,6 @@ namespace XNode {
|
|||||||
/// <summary> Add a node to the graph by type </summary>
|
/// <summary> Add a node to the graph by type </summary>
|
||||||
public virtual Node AddNode(Type type) {
|
public virtual Node AddNode(Type type) {
|
||||||
Node node = ScriptableObject.CreateInstance(type) as Node;
|
Node node = ScriptableObject.CreateInstance(type) as Node;
|
||||||
#if UNITY_EDITOR
|
|
||||||
if (!Application.isPlaying) {
|
|
||||||
UnityEditor.AssetDatabase.AddObjectToAsset(node, this);
|
|
||||||
UnityEditor.AssetDatabase.SaveAssets();
|
|
||||||
node.name = UnityEditor.ObjectNames.NicifyVariableName(node.name);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
nodes.Add(node);
|
nodes.Add(node);
|
||||||
node.graph = this;
|
node.graph = this;
|
||||||
return node;
|
return node;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user