1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

!TX(Code format)

This commit is contained in:
Icarus 2019-12-23 23:29:28 +08:00
parent f7251ee02a
commit 5d2259608e
9 changed files with 230 additions and 249 deletions

View File

@ -10,17 +10,17 @@ namespace XNodeEditor
/// </summary> /// </summary>
public class MenuPopupWindow : PopupWindowContent public class MenuPopupWindow : PopupWindowContent
{ {
public Vector2 OpenBeforeMousePos; public Vector2 openBeforeMousePos;
private SearchField _search; private SearchField search;
private MenuTreeView _menuTree; private MenuTreeView menuTree;
public Action OnCloseA; public Action onCloseAction;
public MenuPopupWindow() public MenuPopupWindow()
{ {
_search = new SearchField(); search = new SearchField();
_menuTree = new MenuTreeView(); menuTree = new MenuTreeView();
} }
private bool _isInit; private bool isInit;
/// <summary> /// <summary>
/// Add Item /// Add Item
@ -31,7 +31,7 @@ namespace XNodeEditor
/// <param name="autoClose">Automatically close window after selecting an item</param> /// <param name="autoClose">Automatically close window after selecting an item</param>
public void AddItem(string menuPath, Action onClick, char symbol = '/',bool autoClose = true) public void AddItem(string menuPath, Action onClick, char symbol = '/',bool autoClose = true)
{ {
_menuTree.AddItem(menuPath, () => menuTree.AddItem(menuPath, () =>
{ {
onClick?.Invoke(); onClick?.Invoke();
if (autoClose) if (autoClose)
@ -46,52 +46,52 @@ namespace XNodeEditor
/// </summary> /// </summary>
public void Init() public void Init()
{ {
_menuTree.Reload(); menuTree.Reload();
_isInit = true; isInit = true;
} }
public override void OnOpen() public override void OnOpen()
{ {
_search.SetFocus(); search.SetFocus();
} }
public override void OnClose() public override void OnClose()
{ {
OnCloseA?.Invoke(); onCloseAction?.Invoke();
} }
private string _str; private string _str;
public override void OnGUI(Rect rect) public override void OnGUI(Rect rect)
{ {
if (!_isInit) if (!isInit)
{ {
Init(); Init();
} }
_action(); EventAction();
EditorGUI.BeginChangeCheck(); 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()) 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; Event e = Event.current;
switch (e.type) switch (e.type)
{ {
case EventType.KeyDown: case EventType.KeyDown:
if (e.keyCode == KeyCode.DownArrow && !_menuTree.HasFocus()) if (e.keyCode == KeyCode.DownArrow && !menuTree.HasFocus())
{ {
_menuTree.SetFocusAndEnsureSelectedItem(); menuTree.SetFocusAndEnsureSelectedItem();
e.Use(); e.Use();
} }
break; break;

View File

@ -23,16 +23,18 @@ namespace XNodeEditor {
#if ODIN_INSPECTOR #if ODIN_INSPECTOR
internal static bool inNodeEditor = false; internal static bool inNodeEditor = false;
#endif #endif
private List<string> _excludesField; private List<string> excludesField;
private List<string> portNames = new List<string>();
public override void OnCreate() public override void OnCreate()
{ {
_excludesField = new List<string> { "m_Script", "graph", "position", "ports" }; excludesField = new List<string> { "m_Script", "graph", "position", "ports" };
var fields = GetExcludesField(); var fields = GetExcludesField();
if (fields != null) if (fields != null)
{ {
_excludesField.AddRange(fields); excludesField.AddRange(fields);
} }
} }
@ -67,25 +69,25 @@ namespace XNodeEditor {
// Iterate through serialized properties and draw them like the Inspector (But with ports) // Iterate through serialized properties and draw them like the Inspector (But with ports)
SerializedProperty iterator = serializedObject.GetIterator(); SerializedProperty iterator = serializedObject.GetIterator();
bool enterChildren = true; bool enterChildren = true;
List<string> _names = new List<string>(); portNames.Clear();
while (iterator.NextVisible(enterChildren)) { while (iterator.NextVisible(enterChildren)) {
enterChildren = false; enterChildren = false;
if (_excludesField.Contains(iterator.name)) continue; if (excludesField.Contains(iterator.name)) continue;
NodeEditorGUILayout.PropertyField(iterator, true); NodeEditorGUILayout.PropertyField(iterator, true);
_names.Add(iterator.name); portNames.Add(iterator.name);
} }
//处理一下没被绘制的端口 //Deal with ports that are not drawn
foreach (var port in target.Ports) foreach (var port in target.Ports)
{ {
//动态的跳过 //Dynamic skip
if (port.IsDynamic) if (port.IsDynamic)
{ {
continue; continue;
} }
//不受unity序列化支持,但是被标记为了输入或输出 //Not supported by unity serialization, but marked as input or output
if (!_names.Contains(port.fieldName)) if (!portNames.Contains(port.fieldName))
{ {
NodeEditorGUILayout.PortField(port); NodeEditorGUILayout.PortField(port);
} }
@ -143,27 +145,6 @@ namespace XNodeEditor {
return NodeEditorResources.styles.nodeHighlight; return NodeEditorResources.styles.nodeHighlight;
} }
/// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
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);
}
}
/// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary> /// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
public virtual void AddContextMenuItems(GenericMenu menu) { public virtual void AddContextMenuItems(GenericMenu menu) {
// Actions if only one node is selected // Actions if only one node is selected

View File

@ -32,13 +32,13 @@ namespace XNodeEditor {
private Rect selectionBox; private Rect selectionBox;
private bool isDoubleClick = false; private bool isDoubleClick = false;
private Vector2 lastMousePosition; private Vector2 lastMousePosition;
private MenuPopupWindow _menuPopupWindow; private MenuPopupWindow menuPopupWindow;
public void Controls() { public void Controls() {
if (_menuPopupWindow == null) if (menuPopupWindow == null)
{ {
_menuPopupWindow = new MenuPopupWindow(); menuPopupWindow = new MenuPopupWindow();
graphEditor.AddContextMenuItems(_menuPopupWindow); graphEditor.AddContextMenuItems(menuPopupWindow);
} }
wantsMouseMove = true; wantsMouseMove = true;
Event e = Event.current; Event e = Event.current;
@ -226,12 +226,12 @@ namespace XNodeEditor {
} }
// Open context menu for auto-connection // Open context menu for auto-connection
else if (NodeEditorPreferences.GetSettings().dragToCreate && autoConnectOutput != null) { else if (NodeEditorPreferences.GetSettings().dragToCreate && autoConnectOutput != null) {
_menuPopupWindow.OnCloseA = _releaseDraggedConnection; menuPopupWindow.OnCloseA = ReleaseDraggedConnection;
_menuPopupWindow.OpenBeforeMousePos = e.mousePosition; menuPopupWindow.OpenBeforeMousePos = e.mousePosition;
PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),_menuPopupWindow); PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),menuPopupWindow);
} }
_releaseDraggedConnection(); ReleaseDraggedConnection();
} else if (currentActivity == NodeActivity.DragNode) { } else if (currentActivity == NodeActivity.DragNode) {
IEnumerable<XNode.Node> nodes = Selection.objects.Where(x => x is XNode.Node).Select(x => x as XNode.Node); IEnumerable<XNode.Node> 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. 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) { } else if (!IsHoveringNode) {
autoConnectOutput = null; autoConnectOutput = null;
_menuPopupWindow.OpenBeforeMousePos = e.mousePosition; menuPopupWindow.OpenBeforeMousePos = e.mousePosition;
PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),_menuPopupWindow); PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero),menuPopupWindow);
} }
} }
isPanning = false; isPanning = false;
@ -346,7 +346,7 @@ namespace XNodeEditor {
} }
} }
private void _releaseDraggedConnection() private void ReleaseDraggedConnection()
{ {
//Release dragged connection //Release dragged connection
draggedOutput = null; draggedOutput = null;

View File

@ -29,7 +29,7 @@ namespace XNodeEditor {
DrawConnections(); DrawConnections();
DrawNodes(); DrawNodes();
DrawSelectionBox(); DrawSelectionBox();
_drawGroupName(); DrawGroupName();
DrawTooltip(); DrawTooltip();
graphEditor.OnGUI(); graphEditor.OnGUI();
@ -42,7 +42,7 @@ namespace XNodeEditor {
GUI.matrix = m; GUI.matrix = m;
} }
private void _drawGroupName() private void DrawGroupName()
{ {
var guiContent = new GUIContent(this.graph.name); var guiContent = new GUIContent(this.graph.name);
var col = EditorStyles.label.normal.textColor; var col = EditorStyles.label.normal.textColor;

View File

@ -234,7 +234,7 @@ namespace XNodeEditor {
Color col = NodeEditorWindow.current.graphEditor.GetPortColor(port); Color col = NodeEditorWindow.current.graphEditor.GetPortColor(port);
DrawPortHandle(rect, backgroundColor, col); DrawPortHandle(rect, backgroundColor, col);
//当选择节点时显示所有的输入点索引 //Show all input point indexes when node is selected
if (port.direction == XNode.NodePort.IO.Output) if (port.direction == XNode.NodePort.IO.Output)
{ {
if (port.Connection != null && port.Connection.ConnectionCount > 1) if (port.Connection != null && port.Connection.ConnectionCount > 1)

View File

@ -75,7 +75,7 @@ namespace XNodeEditor {
return types.ToArray(); return types.ToArray();
} }
/// <summary> Find methods marked with the [ContextMenu] attribute and add them to the context menu </summary> /// <summary> Find methods marked with the [ContextMenu] attribute and add them to the menu popup window</summary>
public static void AddCustomContextMenuItems(this MenuPopupWindow contextMenu, object obj) { public static void AddCustomContextMenuItems(this MenuPopupWindow contextMenu, object obj) {
KeyValuePair<ContextMenu, MethodInfo>[] items = GetContextMenuMethods(obj); KeyValuePair<ContextMenu, MethodInfo>[] items = GetContextMenuMethods(obj);
if (items.Length != 0) { if (items.Length != 0) {

View File

@ -26,7 +26,7 @@ namespace XNodeEditor {
}); });
} }
[MenuItem("Icarus/Node Editor/Close All Editor Window")] [MenuItem("xNode/Close All Editor Window")]
static void CloseAllNodeEditorWindow() static void CloseAllNodeEditorWindow()
{ {
var windows = Resources.FindObjectsOfTypeAll<NodeEditorWindow>(); var windows = Resources.FindObjectsOfTypeAll<NodeEditorWindow>();

View File

@ -104,7 +104,7 @@ namespace XNode {
System.Type tempType = nodeType; System.Type tempType = nodeType;
while ((tempType = tempType.BaseType) != typeof(XNode.Node)) while ((tempType = tempType.BaseType) != typeof(XNode.Node))
{ {
//只返回私有的,保护等其他的不需要 // Only return private, protected, etc.
var fieldInfos = tempType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x=>x.IsPrivate); var fieldInfos = tempType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x=>x.IsPrivate);
fieldInfo.AddRange(fieldInfos); fieldInfo.AddRange(fieldInfos);

View File

@ -53,7 +53,7 @@ namespace XNode {
private Type valueType; private Type valueType;
#if UNITY_EDITOR #if UNITY_EDITOR
public const string FieldNameEditor = nameof(_fieldName); public const string FIELDNAMEEDITOR = nameof(_fieldName);
#endif #endif
[SerializeField] private string _fieldName; [SerializeField] private string _fieldName;
[SerializeField] private Node _node; [SerializeField] private Node _node;
@ -219,7 +219,7 @@ namespace XNode {
if (connectionType == Node.ConnectionType.Override && ConnectionCount != 0) if (connectionType == Node.ConnectionType.Override && ConnectionCount != 0)
{ {
var conPort = GetConnection(0); var conPort = GetConnection(0);
//Same connection, not disconnect
if(conPort.node != port.node || conPort != port) if(conPort.node != port.node || conPort != port)
{ {
ClearConnections(); ClearConnections();