NodeGraphEditor.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace XNodeEditor
  7. {
  8. /// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>
  9. [CustomNodeGraphEditor(typeof(XNode.NodeGraph))]
  10. public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph>
  11. {
  12. [Obsolete("Use window.position instead")]
  13. public Rect position
  14. {
  15. get { return window.position; }
  16. set { window.position = value; }
  17. }
  18. /// <summary> Are we currently renaming a node? </summary>
  19. protected bool isRenaming;
  20. public virtual void OnGUI()
  21. {
  22. }
  23. public virtual void OnGUI(UnityEngine.Object selectionCache)
  24. {
  25. }
  26. /// <summary> Called when opened by NodeEditorWindow </summary>
  27. public virtual void OnOpen()
  28. {
  29. }
  30. public virtual void OnDisable()
  31. {
  32. }
  33. /// <summary> Called when NodeEditorWindow gains focus </summary>
  34. public virtual void OnWindowFocus()
  35. {
  36. }
  37. /// <summary> Called when NodeEditorWindow loses focus </summary>
  38. public virtual void OnWindowFocusLost()
  39. {
  40. }
  41. public virtual Texture2D GetGridTexture()
  42. {
  43. return NodeEditorPreferences.GetSettings().gridTexture;
  44. }
  45. public virtual Texture2D GetSecondaryGridTexture()
  46. {
  47. return NodeEditorPreferences.GetSettings().crossTexture;
  48. }
  49. /// <summary> Return default settings for this graph type. This is the settings the user will load if no previous settings have been saved. </summary>
  50. public virtual NodeEditorPreferences.Settings GetDefaultPreferences()
  51. {
  52. return new NodeEditorPreferences.Settings();
  53. }
  54. /// <summary> Returns context node menu path. Null or empty strings for hidden nodes. </summary>
  55. public virtual string GetNodeMenuName(Type type)
  56. {
  57. //Check if type has the CreateNodeMenuAttribute
  58. XNode.Node.CreateNodeMenuAttribute attrib;
  59. if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
  60. return attrib.menuName;
  61. else // Return generated path
  62. return NodeEditorUtilities.NodeDefaultPath(type);
  63. }
  64. /// <summary> The order by which the menu items are displayed. </summary>
  65. public virtual int GetNodeMenuOrder(Type type)
  66. {
  67. //Check if type has the CreateNodeMenuAttribute
  68. XNode.Node.CreateNodeMenuAttribute attrib;
  69. if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
  70. return attrib.order;
  71. else
  72. return 0;
  73. }
  74. /// <summary>
  75. /// 这个方法是新增的,用于在创建的时候给节点自动改ShowName
  76. /// </summary>
  77. /// <param name="type"></param>
  78. /// <returns></returns>
  79. public virtual string GetShowName(Type type)
  80. {
  81. //Check if type has the CreateNodeMenuAttribute
  82. XNode.Node.CreateNodeMenuAttribute attrib;
  83. if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
  84. {
  85. if (attrib.showName != "")
  86. return attrib.showName;
  87. else
  88. return NodeEditorUtilities.NodeDefaultPath(type);
  89. }
  90. else // Return generated path
  91. return NodeEditorUtilities.NodeDefaultPath(type);
  92. }
  93. /// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
  94. public virtual void AddContextMenuItems(GenericMenu menu)
  95. {
  96. Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);
  97. var nodeTypes = NodeEditorReflection.nodeTypes.OrderBy(type => GetNodeMenuOrder(type)).ToArray();
  98. for (int i = 0; i < nodeTypes.Length; i++)
  99. {
  100. Type type = nodeTypes[i];
  101. //Get node context menu path
  102. string path = GetNodeMenuName(type);
  103. if (string.IsNullOrEmpty(path)) continue;
  104. // Check if user is allowed to add more of given node type
  105. XNode.Node.DisallowMultipleNodesAttribute disallowAttrib;
  106. bool disallowed = false;
  107. if (NodeEditorUtilities.GetAttrib(type, out disallowAttrib))
  108. {
  109. int typeCount = target.nodes.Count(x => x.GetType() == type);
  110. if (typeCount >= disallowAttrib.max) disallowed = true;
  111. }
  112. // Add node entry to context menu
  113. if (disallowed) menu.AddItem(new GUIContent(path), false, null);
  114. else
  115. menu.AddItem(new GUIContent(path), false, () =>
  116. {
  117. XNode.Node node = CreateNode(type, pos);
  118. NodeEditorWindow.current.AutoConnect(node);
  119. });
  120. }
  121. menu.AddSeparator("");
  122. if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0) menu.AddItem(new GUIContent("Paste"), false, () => NodeEditorWindow.current.PasteNodes(pos));
  123. else menu.AddDisabledItem(new GUIContent("Paste"));
  124. menu.AddItem(new GUIContent("Preferences"), false, () => NodeEditorReflection.OpenPreferences());
  125. menu.AddCustomContextMenuItems(target);
  126. }
  127. /// <summary> Returned gradient is used to color noodles </summary>
  128. /// <param name="output"> The output this noodle comes from. Never null. </param>
  129. /// <param name="input"> The output this noodle comes from. Can be null if we are dragging the noodle. </param>
  130. public virtual Gradient GetNoodleGradient(XNode.NodePort output, XNode.NodePort input)
  131. {
  132. Gradient grad = new Gradient();
  133. // If dragging the noodle, draw solid, slightly transparent
  134. if (input == null)
  135. {
  136. Color a = GetTypeColor(output.ValueType);
  137. grad.SetKeys(
  138. new GradientColorKey[] {new GradientColorKey(a, 0f)},
  139. new GradientAlphaKey[] {new GradientAlphaKey(0.6f, 0f)}
  140. );
  141. }
  142. // If normal, draw gradient fading from one input color to the other
  143. else
  144. {
  145. Color a = GetTypeColor(output.ValueType);
  146. Color b = GetTypeColor(input.ValueType);
  147. // If any port is hovered, tint white
  148. if (window.hoveredPort == output || window.hoveredPort == input)
  149. {
  150. a = Color.Lerp(a, Color.white, 0.8f);
  151. b = Color.Lerp(b, Color.white, 0.8f);
  152. }
  153. grad.SetKeys(
  154. new GradientColorKey[] {new GradientColorKey(a, 0f), new GradientColorKey(b, 1f)},
  155. new GradientAlphaKey[] {new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 1f)}
  156. );
  157. }
  158. return grad;
  159. }
  160. /// <summary> Returned float is used for noodle thickness </summary>
  161. /// <param name="output"> The output this noodle comes from. Never null. </param>
  162. /// <param name="input"> The output this noodle comes from. Can be null if we are dragging the noodle. </param>
  163. public virtual float GetNoodleThickness(XNode.NodePort output, XNode.NodePort input)
  164. {
  165. return 5f;
  166. }
  167. public virtual NoodlePath GetNoodlePath(XNode.NodePort output, XNode.NodePort input)
  168. {
  169. return NodeEditorPreferences.GetSettings().noodlePath;
  170. }
  171. public virtual NoodleStroke GetNoodleStroke(XNode.NodePort output, XNode.NodePort input)
  172. {
  173. return NodeEditorPreferences.GetSettings().noodleStroke;
  174. }
  175. /// <summary> Returned color is used to color ports </summary>
  176. public virtual Color GetPortColor(XNode.NodePort port)
  177. {
  178. return GetTypeColor(port.ValueType);
  179. }
  180. /// <summary> Returns generated color for a type. This color is editable in preferences </summary>
  181. public virtual Color GetTypeColor(Type type)
  182. {
  183. return NodeEditorPreferences.GetTypeColor(type);
  184. }
  185. /// <summary> Override to display custom tooltip </summary>
  186. public virtual string GetPortTooltip(XNode.NodePort port)
  187. {
  188. Type portType = port.ValueType;
  189. string tooltip = "";
  190. tooltip = portType.PrettyName();
  191. if (port.IsOutput)
  192. {
  193. object obj = port.node.GetValue(port);
  194. tooltip += " = " + (obj != null ? obj.ToString() : "null");
  195. }
  196. return tooltip;
  197. }
  198. /// <summary> Deal with objects dropped into the graph through DragAndDrop </summary>
  199. public virtual void OnDropObjects(UnityEngine.Object[] objects)
  200. {
  201. if (GetType() != typeof(NodeGraphEditor)) Debug.Log("No OnDropObjects override defined for " + GetType());
  202. }
  203. /// <summary> Create a node and save it in the graph asset </summary>
  204. public virtual XNode.Node CreateNode(Type type, Vector2 position)
  205. {
  206. Undo.RecordObject(target, "Create Node");
  207. XNode.Node node = target.AddNode(type);
  208. Undo.RegisterCreatedObjectUndo(node, "Create Node");
  209. node.position = position;
  210. node.name = GetShowName(type);
  211. if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(target))) AssetDatabase.AddObjectToAsset(node, target);
  212. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  213. NodeEditorWindow.RepaintAll();
  214. return node;
  215. }
  216. /// <summary> Creates a copy of the original node in the graph </summary>
  217. public virtual XNode.Node CopyNode(XNode.Node original)
  218. {
  219. Undo.RecordObject(target, "Duplicate Node");
  220. XNode.Node node = target.CopyNode(original);
  221. SetNodeIndex(node);
  222. Undo.RegisterCreatedObjectUndo(node, "Duplicate Node");
  223. node.name = original.name;
  224. AssetDatabase.AddObjectToAsset(node, target);
  225. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  226. return node;
  227. }
  228. protected virtual void SetNodeIndex(XNode.Node node)
  229. {
  230. }
  231. /// <summary> Return false for nodes that can't be removed </summary>
  232. public virtual bool CanRemove(XNode.Node node)
  233. {
  234. // Check graph attributes to see if this node is required
  235. Type graphType = target.GetType();
  236. XNode.NodeGraph.RequireNodeAttribute[] attribs = Array.ConvertAll(
  237. graphType.GetCustomAttributes(typeof(XNode.NodeGraph.RequireNodeAttribute), true), x => x as XNode.NodeGraph.RequireNodeAttribute);
  238. if (attribs.Any(x => x.Requires(node.GetType())))
  239. {
  240. if (target.nodes.Count(x => x.GetType() == node.GetType()) <= 1)
  241. {
  242. return false;
  243. }
  244. }
  245. return true;
  246. }
  247. /// <summary> Safely remove a node and all its connections. </summary>
  248. public virtual void RemoveNode(XNode.Node node)
  249. {
  250. if (!CanRemove(node)) return;
  251. // Remove the node
  252. Undo.RecordObject(node, "Delete Node");
  253. Undo.RecordObject(target, "Delete Node");
  254. foreach (var port in node.Ports)
  255. foreach (var conn in port.GetConnections())
  256. Undo.RecordObject(conn.node, "Delete Node");
  257. target.RemoveNode(node);
  258. Undo.DestroyObjectImmediate(node);
  259. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  260. }
  261. [AttributeUsage(AttributeTargets.Class)]
  262. public class CustomNodeGraphEditorAttribute : Attribute,
  263. XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph>.INodeEditorAttrib
  264. {
  265. private Type inspectedType;
  266. public string editorPrefsKey;
  267. /// <summary> Tells a NodeGraphEditor which Graph type it is an editor for </summary>
  268. /// <param name="inspectedType">Type that this editor can edit</param>
  269. /// <param name="editorPrefsKey">Define unique key for unique layout settings instance</param>
  270. public CustomNodeGraphEditorAttribute(Type inspectedType, string editorPrefsKey = "xNode.Settings")
  271. {
  272. this.inspectedType = inspectedType;
  273. this.editorPrefsKey = editorPrefsKey;
  274. }
  275. public Type GetInspectedType()
  276. {
  277. return inspectedType;
  278. }
  279. }
  280. }
  281. }