diff --git a/Scripts/Editor/MenuPopup/MenuPopupWindow.cs b/Scripts/Editor/MenuPopup/MenuPopupWindow.cs index ef9c61b..62ea6ef 100644 --- a/Scripts/Editor/MenuPopup/MenuPopupWindow.cs +++ b/Scripts/Editor/MenuPopup/MenuPopupWindow.cs @@ -10,17 +10,17 @@ namespace XNodeEditor /// public class MenuPopupWindow : PopupWindowContent { - public Vector2 OpenBeforeMousePos; - private SearchField _search; - private MenuTreeView _menuTree; - public Action OnCloseA; + public Vector2 openBeforeMousePos; + private SearchField search; + private MenuTreeView menuTree; + public Action onCloseAction; public MenuPopupWindow() { - _search = new SearchField(); - _menuTree = new MenuTreeView(); + search = new SearchField(); + menuTree = new MenuTreeView(); } - private bool _isInit; + private bool isInit; /// /// Add Item @@ -31,7 +31,7 @@ namespace XNodeEditor /// Automatically close window after selecting an item public void AddItem(string menuPath, Action onClick, char symbol = '/',bool autoClose = true) { - _menuTree.AddItem(menuPath, () => + menuTree.AddItem(menuPath, () => { onClick?.Invoke(); if (autoClose) @@ -46,52 +46,52 @@ namespace XNodeEditor /// public void Init() { - _menuTree.Reload(); - _isInit = true; + menuTree.Reload(); + isInit = true; } public override void OnOpen() { - _search.SetFocus(); + search.SetFocus(); } public override void OnClose() { - OnCloseA?.Invoke(); + onCloseAction?.Invoke(); } private string _str; public override void OnGUI(Rect rect) { - if (!_isInit) + if (!isInit) { Init(); } - _action(); + EventAction(); EditorGUI.BeginChangeCheck(); { - _str = _search.OnGUI(new Rect(rect.position, new Vector2(rect.width, 20)),_str); + _str = search.OnGUI(new Rect(rect.position, new Vector2(rect.width, 20)),_str); } if (EditorGUI.EndChangeCheck()) { - _menuTree.searchString = _str; + menuTree.searchString = _str; } - _menuTree.OnGUI(new Rect(new Vector2(0,25),rect.size - new Vector2(0,20))); + menuTree.OnGUI(new Rect(new Vector2(0,25),rect.size - new Vector2(0,20))); } - private void _action() + private void EventAction() { Event e = Event.current; switch (e.type) { case EventType.KeyDown: - if (e.keyCode == KeyCode.DownArrow && !_menuTree.HasFocus()) + if (e.keyCode == KeyCode.DownArrow && !menuTree.HasFocus()) { - _menuTree.SetFocusAndEnsureSelectedItem(); + menuTree.SetFocusAndEnsureSelectedItem(); e.Use(); } break; diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index a287263..f6dda2f 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -1,210 +1,191 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using UnityEditor; -using UnityEngine; -#if ODIN_INSPECTOR -using Sirenix.OdinInspector.Editor; -using Sirenix.Utilities; -using Sirenix.Utilities.Editor; -#endif - -namespace XNodeEditor { - /// Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. - [CustomNodeEditor(typeof(XNode.Node))] - public class NodeEditor : XNodeEditor.Internal.NodeEditorBase { - - private readonly Color DEFAULTCOLOR = new Color32(90, 97, 105, 255); - - /// Fires every whenever a node was modified through the editor - public static Action onUpdateNode; - public readonly static Dictionary portPositions = new Dictionary(); - -#if ODIN_INSPECTOR - internal static bool inNodeEditor = false; -#endif - private List _excludesField; - public override void OnCreate() - { - _excludesField = new List { "m_Script", "graph", "position", "ports" }; - - var fields = GetExcludesField(); - - if (fields != null) - { - _excludesField.AddRange(fields); - } - } - - public virtual void OnHeaderGUI() { - GUILayout.Label(target.name, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30)); - } - - protected virtual IEnumerable GetExcludesField() - { - return null; - } - - /// Draws standard field editors for all public fields - public virtual void OnBodyGUI() { -#if ODIN_INSPECTOR - inNodeEditor = true; -#endif - - // Unity specifically requires this to save/update any serial object. - // serializedObject.Update(); must go at the start of an inspector gui, and - // serializedObject.ApplyModifiedProperties(); goes at the end. - serializedObject.Update(); - -#if ODIN_INSPECTOR - InspectorUtilities.BeginDrawPropertyTree(objectTree, true); - GUIHelper.PushLabelWidth(84); - objectTree.Draw(true); - InspectorUtilities.EndDrawPropertyTree(objectTree); - GUIHelper.PopLabelWidth(); -#else - - // Iterate through serialized properties and draw them like the Inspector (But with ports) - SerializedProperty iterator = serializedObject.GetIterator(); - bool enterChildren = true; - List _names = new List(); - while (iterator.NextVisible(enterChildren)) { - enterChildren = false; - if (_excludesField.Contains(iterator.name)) continue; - NodeEditorGUILayout.PropertyField(iterator, true); - _names.Add(iterator.name); - } - - //处理一下没被绘制的端口 - foreach (var port in target.Ports) - { - //动态的跳过 - if (port.IsDynamic) - { - continue; - } - - //不受unity序列化支持,但是被标记为了输入或输出 - if (!_names.Contains(port.fieldName)) - { - NodeEditorGUILayout.PortField(port); - } - } -#endif - - // Iterate through dynamic ports and draw them in the order in which they are serialized - foreach (XNode.NodePort dynamicPort in target.DynamicPorts) { - if (NodeEditorGUILayout.IsDynamicPortListPort(dynamicPort)) continue; - NodeEditorGUILayout.PortField(dynamicPort); - } - - serializedObject.ApplyModifiedProperties(); - -#if ODIN_INSPECTOR - // Call repaint so that the graph window elements respond properly to layout changes coming from Odin - if (GUIHelper.RepaintRequested) { - GUIHelper.ClearRepaintRequest(); - window.Repaint(); - } -#endif - -#if ODIN_INSPECTOR - inNodeEditor = false; -#endif - } - - public virtual int GetWidth() { - Type type = target.GetType(); - int width; - if (type.TryGetAttributeWidth(out width)) return width; - else return 208; - } - - public Vector2 GetCurrentMousePosition(float yOffset = 10) - { - return new Vector2(Event.current.mousePosition.x,Event.current.mousePosition.y + yOffset); - } - - /// Returns color for target node - public virtual Color GetTint() { - // Try get color from [NodeTint] attribute - Type type = target.GetType(); - Color color; - if (type.TryGetAttributeTint(out color)) return color; - // Return default color (grey) - else return DEFAULTCOLOR; - } - - public virtual GUIStyle GetBodyStyle() { - return NodeEditorResources.styles.nodeBody; - } - - public virtual GUIStyle GetBodyHighlightStyle() { - return NodeEditorResources.styles.nodeHighlight; - } - - /// Add items for the context menu when right-clicking this node. Override to add custom menu items. - public virtual void AddContextMenuItems(MenuPopupWindow menu) { - // Actions if only one node is selected - if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) { - XNode.Node node = Selection.activeObject as XNode.Node; - menu.AddItem("Move To Top", () => NodeEditorWindow.current.MoveNodeToTop(node)); - menu.AddItem("Rename", NodeEditorWindow.current.RenameSelectedNode); - } - - // Add actions to any number of selected nodes - menu.AddItem("Copy", NodeEditorWindow.current.CopySelectedNodes); - menu.AddItem("Duplicate", NodeEditorWindow.current.DuplicateSelectedNodes); - menu.AddItem("Remove", NodeEditorWindow.current.RemoveSelectedNodes); - - // Custom sctions if only one node is selected - if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) { - XNode.Node node = Selection.activeObject as XNode.Node; - menu.AddCustomContextMenuItems(node); - } - } - - /// Add items for the context menu when right-clicking this node. Override to add custom menu items. - public virtual void AddContextMenuItems(GenericMenu menu) { - // Actions if only one node is selected - if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) { - XNode.Node node = Selection.activeObject as XNode.Node; - menu.AddItem(new GUIContent("Move To Top"), false, () => NodeEditorWindow.current.MoveNodeToTop(node)); - menu.AddItem(new GUIContent("Rename"), false, NodeEditorWindow.current.RenameSelectedNode); - } - - // Add actions to any number of selected nodes - menu.AddItem(new GUIContent("Copy"), false, NodeEditorWindow.current.CopySelectedNodes); - menu.AddItem(new GUIContent("Duplicate"), false, NodeEditorWindow.current.DuplicateSelectedNodes); - menu.AddItem(new GUIContent("Remove"), false, NodeEditorWindow.current.RemoveSelectedNodes); - - // Custom sctions if only one node is selected - if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) { - XNode.Node node = Selection.activeObject as XNode.Node; - menu.AddCustomContextMenuItems(node); - } - } - - /// Rename the node asset. This will trigger a reimport of the node. - public void Rename(string newName) { - if (newName == null || newName.Trim() == "") newName = NodeEditorUtilities.NodeDefaultName(target.GetType()); - target.name = newName; - AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); - } - - [AttributeUsage(AttributeTargets.Class)] - public class CustomNodeEditorAttribute : Attribute, - XNodeEditor.Internal.NodeEditorBase.INodeEditorAttrib { - private Type inspectedType; - /// Tells a NodeEditor which Node type it is an editor for - /// Type that this editor can edit - public CustomNodeEditorAttribute(Type inspectedType) { - this.inspectedType = inspectedType; - } - - public Type GetInspectedType() { - return inspectedType; - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEngine; +#if ODIN_INSPECTOR +using Sirenix.OdinInspector.Editor; +using Sirenix.Utilities; +using Sirenix.Utilities.Editor; +#endif + +namespace XNodeEditor { + /// Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. + [CustomNodeEditor(typeof(XNode.Node))] + public class NodeEditor : XNodeEditor.Internal.NodeEditorBase { + + private readonly Color DEFAULTCOLOR = new Color32(90, 97, 105, 255); + + /// Fires every whenever a node was modified through the editor + public static Action onUpdateNode; + public readonly static Dictionary portPositions = new Dictionary(); + +#if ODIN_INSPECTOR + internal static bool inNodeEditor = false; +#endif + private List excludesField; + private List portNames = new List(); + + public override void OnCreate() + { + excludesField = new List { "m_Script", "graph", "position", "ports" }; + + var fields = GetExcludesField(); + + if (fields != null) + { + excludesField.AddRange(fields); + } + } + + public virtual void OnHeaderGUI() { + GUILayout.Label(target.name, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30)); + } + + protected virtual IEnumerable GetExcludesField() + { + return null; + } + + /// Draws standard field editors for all public fields + public virtual void OnBodyGUI() { +#if ODIN_INSPECTOR + inNodeEditor = true; +#endif + + // Unity specifically requires this to save/update any serial object. + // serializedObject.Update(); must go at the start of an inspector gui, and + // serializedObject.ApplyModifiedProperties(); goes at the end. + serializedObject.Update(); + +#if ODIN_INSPECTOR + InspectorUtilities.BeginDrawPropertyTree(objectTree, true); + GUIHelper.PushLabelWidth(84); + objectTree.Draw(true); + InspectorUtilities.EndDrawPropertyTree(objectTree); + GUIHelper.PopLabelWidth(); +#else + + // Iterate through serialized properties and draw them like the Inspector (But with ports) + SerializedProperty iterator = serializedObject.GetIterator(); + bool enterChildren = true; + portNames.Clear(); + while (iterator.NextVisible(enterChildren)) { + enterChildren = false; + if (excludesField.Contains(iterator.name)) continue; + NodeEditorGUILayout.PropertyField(iterator, true); + portNames.Add(iterator.name); + } + + //Deal with ports that are not drawn + foreach (var port in target.Ports) + { + //Dynamic skip + if (port.IsDynamic) + { + continue; + } + + //Not supported by unity serialization, but marked as input or output + if (!portNames.Contains(port.fieldName)) + { + NodeEditorGUILayout.PortField(port); + } + } +#endif + + // Iterate through dynamic ports and draw them in the order in which they are serialized + foreach (XNode.NodePort dynamicPort in target.DynamicPorts) { + if (NodeEditorGUILayout.IsDynamicPortListPort(dynamicPort)) continue; + NodeEditorGUILayout.PortField(dynamicPort); + } + + serializedObject.ApplyModifiedProperties(); + +#if ODIN_INSPECTOR + // Call repaint so that the graph window elements respond properly to layout changes coming from Odin + if (GUIHelper.RepaintRequested) { + GUIHelper.ClearRepaintRequest(); + window.Repaint(); + } +#endif + +#if ODIN_INSPECTOR + inNodeEditor = false; +#endif + } + + public virtual int GetWidth() { + Type type = target.GetType(); + int width; + if (type.TryGetAttributeWidth(out width)) return width; + else return 208; + } + + public Vector2 GetCurrentMousePosition(float yOffset = 10) + { + return new Vector2(Event.current.mousePosition.x,Event.current.mousePosition.y + yOffset); + } + + /// Returns color for target node + public virtual Color GetTint() { + // Try get color from [NodeTint] attribute + Type type = target.GetType(); + Color color; + if (type.TryGetAttributeTint(out color)) return color; + // Return default color (grey) + else return DEFAULTCOLOR; + } + + public virtual GUIStyle GetBodyStyle() { + return NodeEditorResources.styles.nodeBody; + } + + public virtual GUIStyle GetBodyHighlightStyle() { + return NodeEditorResources.styles.nodeHighlight; + } + + /// Add items for the context menu when right-clicking this node. Override to add custom menu items. + public virtual void AddContextMenuItems(GenericMenu menu) { + // Actions if only one node is selected + if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) { + XNode.Node node = Selection.activeObject as XNode.Node; + menu.AddItem(new GUIContent("Move To Top"), false, () => NodeEditorWindow.current.MoveNodeToTop(node)); + menu.AddItem(new GUIContent("Rename"), false, NodeEditorWindow.current.RenameSelectedNode); + } + + // Add actions to any number of selected nodes + menu.AddItem(new GUIContent("Copy"), false, NodeEditorWindow.current.CopySelectedNodes); + menu.AddItem(new GUIContent("Duplicate"), false, NodeEditorWindow.current.DuplicateSelectedNodes); + menu.AddItem(new GUIContent("Remove"), false, NodeEditorWindow.current.RemoveSelectedNodes); + + // Custom sctions if only one node is selected + if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) { + XNode.Node node = Selection.activeObject as XNode.Node; + menu.AddCustomContextMenuItems(node); + } + } + + /// Rename the node asset. This will trigger a reimport of the node. + public void Rename(string newName) { + if (newName == null || newName.Trim() == "") newName = NodeEditorUtilities.NodeDefaultName(target.GetType()); + target.name = newName; + AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); + } + + [AttributeUsage(AttributeTargets.Class)] + public class CustomNodeEditorAttribute : Attribute, + XNodeEditor.Internal.NodeEditorBase.INodeEditorAttrib { + private Type inspectedType; + /// Tells a NodeEditor which Node type it is an editor for + /// Type that this editor can edit + public CustomNodeEditorAttribute(Type inspectedType) { + this.inspectedType = inspectedType; + } + + public Type GetInspectedType() { + return inspectedType; + } + } + } +} diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 40bea2f..26e9873 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -32,13 +32,13 @@ namespace XNodeEditor { private Rect selectionBox; private bool isDoubleClick = false; private Vector2 lastMousePosition; - private MenuPopupWindow _menuPopupWindow; + private MenuPopupWindow menuPopupWindow; public void Controls() { - if (_menuPopupWindow == null) + if (menuPopupWindow == null) { - _menuPopupWindow = new MenuPopupWindow(); - graphEditor.AddContextMenuItems(_menuPopupWindow); + menuPopupWindow = new MenuPopupWindow(); + graphEditor.AddContextMenuItems(menuPopupWindow); } wantsMouseMove = true; Event e = Event.current; @@ -226,12 +226,12 @@ namespace XNodeEditor { } // Open context menu for auto-connection else if (NodeEditorPreferences.GetSettings().dragToCreate && autoConnectOutput != null) { - _menuPopupWindow.OnCloseA = _releaseDraggedConnection; - _menuPopupWindow.OpenBeforeMousePos = e.mousePosition; - PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),_menuPopupWindow); + menuPopupWindow.OnCloseA = ReleaseDraggedConnection; + menuPopupWindow.OpenBeforeMousePos = e.mousePosition; + PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),menuPopupWindow); } - _releaseDraggedConnection(); + ReleaseDraggedConnection(); } else if (currentActivity == NodeActivity.DragNode) { IEnumerable nodes = Selection.objects.Where(x => x is XNode.Node).Select(x => x as XNode.Node); @@ -286,8 +286,8 @@ namespace XNodeEditor { e.Use(); // Fixes copy/paste context menu appearing in Unity 5.6.6f2 - doesn't occur in 2018.3.2f1 Probably needs to be used in other places. } else if (!IsHoveringNode) { autoConnectOutput = null; - _menuPopupWindow.OpenBeforeMousePos = e.mousePosition; - PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),_menuPopupWindow); + menuPopupWindow.OpenBeforeMousePos = e.mousePosition; + PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),menuPopupWindow); } } isPanning = false; @@ -346,7 +346,7 @@ namespace XNodeEditor { } } - private void _releaseDraggedConnection() + private void ReleaseDraggedConnection() { //Release dragged connection draggedOutput = null; diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index de9374c..65d3aa6 100755 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -29,7 +29,7 @@ namespace XNodeEditor { DrawConnections(); DrawNodes(); DrawSelectionBox(); - _drawGroupName(); + DrawGroupName(); DrawTooltip(); graphEditor.OnGUI(); @@ -42,7 +42,7 @@ namespace XNodeEditor { GUI.matrix = m; } - private void _drawGroupName() + private void DrawGroupName() { var guiContent = new GUIContent(this.graph.name); var col = EditorStyles.label.normal.textColor; diff --git a/Scripts/Editor/NodeEditorGUILayout.cs b/Scripts/Editor/NodeEditorGUILayout.cs index b4a756a..542ea03 100644 --- a/Scripts/Editor/NodeEditorGUILayout.cs +++ b/Scripts/Editor/NodeEditorGUILayout.cs @@ -234,7 +234,7 @@ namespace XNodeEditor { Color col = NodeEditorWindow.current.graphEditor.GetPortColor(port); DrawPortHandle(rect, backgroundColor, col); - //褰撻夋嫨鑺傜偣鏃舵樉绀烘墍鏈夌殑杈撳叆鐐圭储寮 + //Show all input point indexes when node is selected if (port.direction == XNode.NodePort.IO.Output) { if (port.Connection != null && port.Connection.ConnectionCount > 1) diff --git a/Scripts/Editor/NodeEditorReflection.cs b/Scripts/Editor/NodeEditorReflection.cs index 27136e0..2ef5ac1 100644 --- a/Scripts/Editor/NodeEditorReflection.cs +++ b/Scripts/Editor/NodeEditorReflection.cs @@ -75,7 +75,7 @@ namespace XNodeEditor { return types.ToArray(); } - /// Find methods marked with the [ContextMenu] attribute and add them to the context menu + /// Find methods marked with the [ContextMenu] attribute and add them to the menu popup window public static void AddCustomContextMenuItems(this MenuPopupWindow contextMenu, object obj) { KeyValuePair[] items = GetContextMenuMethods(obj); if (items.Length != 0) { diff --git a/Scripts/Editor/NodeEditorWindow.cs b/Scripts/Editor/NodeEditorWindow.cs index 201a26d..eff4a1b 100644 --- a/Scripts/Editor/NodeEditorWindow.cs +++ b/Scripts/Editor/NodeEditorWindow.cs @@ -26,7 +26,7 @@ namespace XNodeEditor { }); } - [MenuItem("Icarus/Node Editor/Close All Editor Window")] + [MenuItem("xNode/Close All Editor Window")] static void CloseAllNodeEditorWindow() { var windows = Resources.FindObjectsOfTypeAll(); diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index 86b0176..b884da0 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -104,7 +104,7 @@ namespace XNode { System.Type tempType = nodeType; while ((tempType = tempType.BaseType) != typeof(XNode.Node)) { - //鍙繑鍥炵鏈夌殑,淇濇姢绛夊叾浠栫殑涓嶉渶瑕 + // Only return private, protected, etc. var fieldInfos = tempType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x=>x.IsPrivate); fieldInfo.AddRange(fieldInfos); diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 146a8ca..c4341ea 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -53,7 +53,7 @@ namespace XNode { private Type valueType; #if UNITY_EDITOR - public const string FieldNameEditor = nameof(_fieldName); + public const string FIELDNAMEEDITOR = nameof(_fieldName); #endif [SerializeField] private string _fieldName; [SerializeField] private Node _node; @@ -219,7 +219,7 @@ namespace XNode { if (connectionType == Node.ConnectionType.Override && ConnectionCount != 0) { var conPort = GetConnection(0); - + //Same connection, not disconnect if(conPort.node != port.node || conPort != port) { ClearConnections();