NodeEditor.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using XNode;
  8. #if ODIN_INSPECTOR
  9. using Sirenix.OdinInspector.Editor;
  10. using Sirenix.Utilities;
  11. using Sirenix.Utilities.Editor;
  12. #endif
  13. namespace XNodeEditor
  14. {
  15. /// <summary> Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. </summary>
  16. [CustomNodeEditor(typeof(XNode.Node))]
  17. public class NodeEditor : XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>
  18. {
  19. private readonly Color DEFAULTCOLOR = new Color32(90, 97, 105, 255);
  20. /// <summary> Fires every whenever a node was modified through the editor </summary>
  21. public static Action<XNode.Node> onUpdateNode;
  22. public readonly static Dictionary<XNode.NodePort, Vector2> portPositions = new Dictionary<XNode.NodePort, Vector2>();
  23. #if ODIN_INSPECTOR
  24. protected internal static bool inNodeEditor = false;
  25. #endif
  26. public virtual void OnHeaderGUI()
  27. {
  28. GUILayout.Label(target.name, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
  29. }
  30. /// <summary> Draws standard field editors for all public fields </summary>
  31. public virtual void OnBodyGUI()
  32. {
  33. #if ODIN_INSPECTOR
  34. inNodeEditor = true;
  35. #endif
  36. // Unity specifically requires this to save/update any serial object.
  37. // serializedObject.Update(); must go at the start of an inspector gui, and
  38. // serializedObject.ApplyModifiedProperties(); goes at the end.
  39. serializedObject.Update();
  40. string[] excludes = { "m_Script", "graph", "position", "ports" };
  41. #if ODIN_INSPECTOR
  42. InspectorUtilities.BeginDrawPropertyTree(objectTree, true);
  43. GUIHelper.PushLabelWidth(84);
  44. objectTree.Draw(true);
  45. InspectorUtilities.EndDrawPropertyTree(objectTree);
  46. GUIHelper.PopLabelWidth();
  47. #else
  48. Node.CreateNodeMenuAttribute createNodeMenuAttribute = this.target.GetType().GetCustomAttribute(typeof(Node.CreateNodeMenuAttribute)) as Node.CreateNodeMenuAttribute;
  49. if (createNodeMenuAttribute != null)
  50. {
  51. if (GUILayout.Button("帮助"))
  52. {
  53. if (EditorUtility.DisplayDialog("帮助", createNodeMenuAttribute.tooltip, "确定")) //显示对话框
  54. {
  55. }
  56. }
  57. }
  58. // Iterate through serialized properties and draw them like the Inspector (But with ports)
  59. SerializedProperty iterator = serializedObject.GetIterator();
  60. XNode.Node rootNode = serializedObject.targetObject as XNode.Node;
  61. bool enterChildren = true;
  62. while (iterator.NextVisible(enterChildren))
  63. {
  64. enterChildren = false;
  65. if (excludes.Contains(iterator.name)) continue;
  66. NodeEditorGUILayout.PropertyField(iterator, rootNode, true);
  67. }
  68. #endif
  69. // Iterate through dynamic ports and draw them in the order in which they are serialized
  70. foreach (XNode.NodePort dynamicPort in target.DynamicPorts)
  71. {
  72. if (NodeEditorGUILayout.IsDynamicPortListPort(dynamicPort)) continue;
  73. NodeEditorGUILayout.PortField(dynamicPort);
  74. }
  75. serializedObject.ApplyModifiedProperties();
  76. #if ODIN_INSPECTOR
  77. // Call repaint so that the graph window elements respond properly to layout changes coming from Odin
  78. if (GUIHelper.RepaintRequested) {
  79. GUIHelper.ClearRepaintRequest();
  80. window.Repaint();
  81. }
  82. #endif
  83. #if ODIN_INSPECTOR
  84. inNodeEditor = false;
  85. #endif
  86. }
  87. public virtual int GetWidth()
  88. {
  89. Type type = target.GetType();
  90. int width;
  91. if (type.TryGetAttributeWidth(out width)) return width;
  92. else return 208;
  93. }
  94. /// <summary> Returns color for target node </summary>
  95. public virtual Color GetTint()
  96. {
  97. // Try get color from [NodeTint] attribute
  98. Type type = target.GetType();
  99. Color color;
  100. if (type.TryGetAttributeTint(out color)) return color;
  101. // Return default color (grey)
  102. else return DEFAULTCOLOR;
  103. }
  104. public virtual GUIStyle GetBodyStyle()
  105. {
  106. return NodeEditorResources.styles.nodeBody;
  107. }
  108. public virtual GUIStyle GetBodyHighlightStyle()
  109. {
  110. return NodeEditorResources.styles.nodeHighlight;
  111. }
  112. /// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
  113. public virtual void AddContextMenuItems(GenericMenu menu)
  114. {
  115. bool canRemove = true;
  116. // Actions if only one node is selected
  117. if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node)
  118. {
  119. XNode.Node node = Selection.activeObject as XNode.Node;
  120. menu.AddItem(new GUIContent("Move To Top"), false, () => NodeEditorWindow.current.MoveNodeToTop(node));
  121. menu.AddItem(new GUIContent("Rename"), false, NodeEditorWindow.current.RenameSelectedNode);
  122. canRemove = NodeGraphEditor.GetEditor(node.graph, NodeEditorWindow.current).CanRemove(node);
  123. }
  124. // Add actions to any number of selected nodes
  125. menu.AddItem(new GUIContent("Copy"), false, NodeEditorWindow.current.CopySelectedNodes);
  126. menu.AddItem(new GUIContent("Duplicate"), false, NodeEditorWindow.current.DuplicateSelectedNodes);
  127. if (canRemove) menu.AddItem(new GUIContent("Remove"), false, NodeEditorWindow.current.RemoveSelectedNodes);
  128. else menu.AddItem(new GUIContent("Remove"), false, null);
  129. // Custom sctions if only one node is selected
  130. if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node)
  131. {
  132. XNode.Node node = Selection.activeObject as XNode.Node;
  133. menu.AddCustomContextMenuItems(node);
  134. }
  135. }
  136. /// <summary> Rename the node asset. This will trigger a reimport of the node. </summary>
  137. public void Rename(string newName)
  138. {
  139. if (newName == null || newName.Trim() == "") newName = NodeEditorUtilities.NodeDefaultName(target.GetType());
  140. target.name = newName;
  141. OnRename();
  142. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
  143. }
  144. /// <summary> Called after this node's name has changed. </summary>
  145. public virtual void OnRename()
  146. {
  147. }
  148. [AttributeUsage(AttributeTargets.Class)]
  149. public class CustomNodeEditorAttribute : Attribute,
  150. XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib
  151. {
  152. private Type inspectedType;
  153. /// <summary> Tells a NodeEditor which Node type it is an editor for </summary>
  154. /// <param name="inspectedType">Type that this editor can edit</param>
  155. public CustomNodeEditorAttribute(Type inspectedType)
  156. {
  157. this.inspectedType = inspectedType;
  158. }
  159. public Type GetInspectedType()
  160. {
  161. return inspectedType;
  162. }
  163. }
  164. }
  165. }