NodeGraphEditor.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace XNodeEditor {
  7. /// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>
  8. [CustomNodeGraphEditor(typeof(XNode.NodeGraph))]
  9. public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph> {
  10. [Obsolete("Use window.position instead")]
  11. public Rect position { get { return window.position; } set { window.position = value; } }
  12. /// <summary> Are we currently renaming a node? </summary>
  13. protected bool isRenaming;
  14. public virtual void OnGUI() { }
  15. /// <summary> Called when opened by NodeEditorWindow </summary>
  16. public virtual void OnOpen() { }
  17. public virtual Texture2D GetGridTexture() {
  18. return NodeEditorPreferences.GetSettings().gridTexture;
  19. }
  20. public virtual Texture2D GetSecondaryGridTexture() {
  21. return NodeEditorPreferences.GetSettings().crossTexture;
  22. }
  23. /// <summary> Return default settings for this graph type. This is the settings the user will load if no previous settings have been saved. </summary>
  24. public virtual NodeEditorPreferences.Settings GetDefaultPreferences() {
  25. return new NodeEditorPreferences.Settings();
  26. }
  27. /// <summary> Returns context node menu path. Null or empty strings for hidden nodes. </summary>
  28. public virtual string GetNodeMenuName(Type type) {
  29. //Check if type has the CreateNodeMenuAttribute
  30. XNode.Node.CreateNodeMenuAttribute attrib;
  31. if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
  32. return attrib.menuName;
  33. else // Return generated path
  34. return NodeEditorUtilities.NodeDefaultPath(type);
  35. }
  36. /// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
  37. public virtual void AddContextMenuItems(GenericMenu menu) {
  38. Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);
  39. for (int i = 0; i < NodeEditorReflection.nodeTypes.Length; i++) {
  40. Type type = NodeEditorReflection.nodeTypes[i];
  41. //Get node context menu path
  42. string path = GetNodeMenuName(type);
  43. if (string.IsNullOrEmpty(path)) continue;
  44. menu.AddItem(new GUIContent(path), false, () => {
  45. XNode.Node node = CreateNode(type, pos);
  46. NodeEditorWindow.current.AutoConnect(node);
  47. });
  48. }
  49. menu.AddSeparator("");
  50. if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0) menu.AddItem(new GUIContent("Paste"), false, () => NodeEditorWindow.current.PasteNodes(pos));
  51. else menu.AddDisabledItem(new GUIContent("Paste"));
  52. menu.AddItem(new GUIContent("Preferences"), false, () => NodeEditorReflection.OpenPreferences());
  53. menu.AddCustomContextMenuItems(target);
  54. }
  55. /// <summary> Returned gradient is used to color noodles </summary>
  56. /// <param name="output"> The output this noodle comes from. Never null. </param>
  57. /// <param name="input"> The output this noodle comes from. Can be null if we are dragging the noodle. </param>
  58. public virtual Gradient GetNoodleGradient(XNode.NodePort output, XNode.NodePort input) {
  59. Gradient grad = new Gradient();
  60. // If dragging the noodle, draw solid, slightly transparent
  61. if (input == null) {
  62. Color a = GetTypeColor(output.ValueType);
  63. grad.SetKeys(
  64. new GradientColorKey[] { new GradientColorKey(a, 0f) },
  65. new GradientAlphaKey[] { new GradientAlphaKey(0.6f, 0f) }
  66. );
  67. }
  68. // If normal, draw gradient fading from one input color to the other
  69. else {
  70. Color a = GetTypeColor(output.ValueType);
  71. Color b = GetTypeColor(input.ValueType);
  72. // If any port is hovered, tint white
  73. if (window.hoveredPort == output || window.hoveredPort == input) {
  74. a = Color.Lerp(a, Color.white, 0.8f);
  75. b = Color.Lerp(b, Color.white, 0.8f);
  76. }
  77. grad.SetKeys(
  78. new GradientColorKey[] { new GradientColorKey(a, 0f), new GradientColorKey(b, 1f) },
  79. new GradientAlphaKey[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 1f) }
  80. );
  81. }
  82. return grad;
  83. }
  84. /// <summary> Returned float is used for noodle thickness </summary>
  85. /// <param name="output"> The output this noodle comes from. Never null. </param>
  86. /// <param name="input"> The output this noodle comes from. Can be null if we are dragging the noodle. </param>
  87. public virtual float GetNoodleThickness(XNode.NodePort output, XNode.NodePort input) {
  88. return 5f;
  89. }
  90. public virtual NoodlePath GetNoodlePath(XNode.NodePort output, XNode.NodePort input) {
  91. return NodeEditorPreferences.GetSettings().noodlePath;
  92. }
  93. public virtual NoodleStroke GetNoodleStroke(XNode.NodePort output, XNode.NodePort input) {
  94. return NodeEditorPreferences.GetSettings().noodleStroke;
  95. }
  96. /// <summary> Returned color is used to color ports </summary>
  97. public virtual Color GetPortColor(XNode.NodePort port) {
  98. return GetTypeColor(port.ValueType);
  99. }
  100. /// <summary> Returns generated color for a type. This color is editable in preferences </summary>
  101. public virtual Color GetTypeColor(Type type) {
  102. return NodeEditorPreferences.GetTypeColor(type);
  103. }
  104. /// <summary> Override to display custom tooltips </summary>
  105. public virtual string GetPortTooltip(XNode.NodePort port) {
  106. Type portType = port.ValueType;
  107. string tooltip = "";
  108. tooltip = portType.PrettyName();
  109. if (port.IsOutput) {
  110. object obj = port.node.GetValue(port);
  111. tooltip += " = " + (obj != null ? obj.ToString() : "null");
  112. }
  113. return tooltip;
  114. }
  115. /// <summary> Deal with objects dropped into the graph through DragAndDrop </summary>
  116. public virtual void OnDropObjects(UnityEngine.Object[] objects) {
  117. Debug.Log("No OnDropObjects override defined for " + GetType());
  118. }
  119. /// <summary> Create a node and save it in the graph asset </summary>
  120. public virtual XNode.Node CreateNode(Type type, Vector2 position) {
  121. Undo.RecordObject(target, "Create Node");
  122. XNode.Node node = target.AddNode(type);
  123. Undo.RegisterCreatedObjectUndo(node, "Create Node");
  124. node.position = position;
  125. if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type);
  126. AssetDatabase.AddObjectToAsset(node, target);
  127. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  128. NodeEditorWindow.RepaintAll();
  129. return node;
  130. }
  131. /// <summary> Creates a copy of the original node in the graph </summary>
  132. public XNode.Node CopyNode(XNode.Node original) {
  133. Undo.RecordObject(target, "Duplicate Node");
  134. XNode.Node node = target.CopyNode(original);
  135. Undo.RegisterCreatedObjectUndo(node, "Duplicate Node");
  136. node.name = original.name;
  137. AssetDatabase.AddObjectToAsset(node, target);
  138. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  139. return node;
  140. }
  141. /// <summary> Safely remove a node and all its connections. </summary>
  142. public virtual void RemoveNode(XNode.Node node) {
  143. Undo.RecordObject(node, "Delete Node");
  144. Undo.RecordObject(target, "Delete Node");
  145. foreach (var port in node.Ports)
  146. foreach (var conn in port.GetConnections())
  147. Undo.RecordObject(conn.node, "Delete Node");
  148. target.RemoveNode(node);
  149. Undo.DestroyObjectImmediate(node);
  150. if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
  151. }
  152. [AttributeUsage(AttributeTargets.Class)]
  153. public class CustomNodeGraphEditorAttribute : Attribute,
  154. XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph>.INodeEditorAttrib {
  155. private Type inspectedType;
  156. public string editorPrefsKey;
  157. /// <summary> Tells a NodeGraphEditor which Graph type it is an editor for </summary>
  158. /// <param name="inspectedType">Type that this editor can edit</param>
  159. /// <param name="editorPrefsKey">Define unique key for unique layout settings instance</param>
  160. public CustomNodeGraphEditorAttribute(Type inspectedType, string editorPrefsKey = "xNode.Settings") {
  161. this.inspectedType = inspectedType;
  162. this.editorPrefsKey = editorPrefsKey;
  163. }
  164. public Type GetInspectedType() {
  165. return inspectedType;
  166. }
  167. }
  168. }
  169. }