using System; using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; using XNode; namespace XNodeEditor { /// Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. public class NodeEditor { /// Fires every whenever a node was modified through the editor public static Action onUpdateNode; public Node target; public SerializedObject serializedObject; public static Dictionary portPositions; /// Draws the node GUI. /// Port handle positions need to be returned to the NodeEditorWindow public void OnNodeGUI() { OnHeaderGUI(); OnBodyGUI(); } public virtual void OnHeaderGUI() { GUI.color = Color.white; string title = NodeEditorUtilities.PrettifyCamelCase(target.name); GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30)); } /// Draws standard field editors for all public fields public virtual void OnBodyGUI() { string[] excludes = { "m_Script", "graph", "position", "ports" }; portPositions = new Dictionary(); SerializedProperty iterator = serializedObject.GetIterator(); bool enterChildren = true; EditorGUIUtility.labelWidth = 84; while (iterator.NextVisible(enterChildren)) { enterChildren = false; if (excludes.Contains(iterator.name)) continue; NodeEditorGUILayout.PropertyField(iterator, true); } } public virtual int GetWidth() { return 200; } public virtual Color GetTint() { Type type = target.GetType(); if (NodeEditorWindow.nodeTint.ContainsKey(type)) return NodeEditorWindow.nodeTint[type]; else return Color.white; } } [AttributeUsage(AttributeTargets.Class)] public class CustomNodeEditorAttribute : Attribute { public Type inspectedType { get { return _inspectedType; } } private Type _inspectedType; /// Tells a NodeEditor which Node type it is an editor for /// Type that this editor can edit /// Path to the node public CustomNodeEditorAttribute(Type inspectedType) { _inspectedType = inspectedType; } } }