NodeEditor.cs 6.3 KB

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