mirror of
https://github.com/Siccity/xNode.git
synced 2026-02-06 15:24:55 +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>
|
||||
public static Action<XNode.Node> onUpdateNode;
|
||||
public static Dictionary<XNode.NodePort, Vector2> portPositions;
|
||||
public static int renaming;
|
||||
|
||||
/// <summary> Draws the node GUI.</summary>
|
||||
/// <param name="portPositions">Port handle positions need to be returned to the NodeEditorWindow </param>
|
||||
@ -24,7 +25,21 @@ namespace XNodeEditor {
|
||||
public virtual void OnHeaderGUI() {
|
||||
GUI.color = Color.white;
|
||||
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>
|
||||
@ -52,6 +67,15 @@ namespace XNodeEditor {
|
||||
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)]
|
||||
public class CustomNodeEditorAttribute : Attribute,
|
||||
XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib {
|
||||
|
||||
@ -291,6 +291,9 @@ namespace XNodeEditor {
|
||||
public void CreateNode(Type type, Vector2 position) {
|
||||
XNode.Node node = graph.AddNode(type);
|
||||
node.position = position;
|
||||
node.name = UnityEditor.ObjectNames.NicifyVariableName(type.ToString());
|
||||
AssetDatabase.AddObjectToAsset(node, graph);
|
||||
AssetDatabase.SaveAssets();
|
||||
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>
|
||||
public void DublicateSelectedNodes() {
|
||||
UnityEngine.Object[] newNodes = new UnityEngine.Object[Selection.objects.Length];
|
||||
|
||||
@ -109,13 +109,8 @@ namespace XNodeEditor {
|
||||
// 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, () => {
|
||||
int index;
|
||||
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("Move To Top"), false, () => MoveNodeToTop(node));
|
||||
contextMenu.AddItem(new GUIContent("Rename"), false, NodeEditor.GetEditor(node).InitiateRename);
|
||||
}
|
||||
|
||||
contextMenu.AddItem(new GUIContent("Duplicate"), false, DublicateSelectedNodes);
|
||||
|
||||
@ -10,6 +10,7 @@ namespace XNodeEditor {
|
||||
public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph> {
|
||||
/// <summary> Custom node editors defined with [CustomNodeGraphEditor] </summary>
|
||||
[NonSerialized] private static Dictionary<Type, NodeGraphEditor> editors;
|
||||
protected bool isRenaming;
|
||||
|
||||
public virtual Texture2D GetGridTexture() {
|
||||
return NodeEditorPreferences.GetSettings().gridTexture;
|
||||
|
||||
@ -71,7 +71,7 @@ namespace XNode {
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public void VerifyConnections() {
|
||||
|
||||
@ -19,13 +19,6 @@ namespace XNode {
|
||||
/// <summary> Add a node to the graph by type </summary>
|
||||
public virtual Node AddNode(Type type) {
|
||||
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);
|
||||
node.graph = this;
|
||||
return node;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user