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>
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;
/// <summary>
/// Add Item
@ -31,7 +31,7 @@ namespace XNodeEditor
/// <param name="autoClose">Automatically close window after selecting an item</param>
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
/// </summary>
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;

View File

@ -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 {
/// <summary> Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. </summary>
[CustomNodeEditor(typeof(XNode.Node))]
public class NodeEditor : XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node> {
private readonly Color DEFAULTCOLOR = new Color32(90, 97, 105, 255);
/// <summary> Fires every whenever a node was modified through the editor </summary>
public static Action<XNode.Node> onUpdateNode;
public readonly static Dictionary<XNode.NodePort, Vector2> portPositions = new Dictionary<XNode.NodePort, Vector2>();
#if ODIN_INSPECTOR
internal static bool inNodeEditor = false;
#endif
private List<string> _excludesField;
public override void OnCreate()
{
_excludesField = new List<string> { "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<string> GetExcludesField()
{
return null;
}
/// <summary> Draws standard field editors for all public fields </summary>
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<string> _names = new List<string>();
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);
}
/// <summary> Returns color for target node </summary>
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;
}
/// <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>
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);
}
}
/// <summary> Rename the node asset. This will trigger a reimport of the node. </summary>
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<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib {
private Type inspectedType;
/// <summary> Tells a NodeEditor which Node type it is an editor for </summary>
/// <param name="inspectedType">Type that this editor can edit</param>
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 {
/// <summary> Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. </summary>
[CustomNodeEditor(typeof(XNode.Node))]
public class NodeEditor : XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node> {
private readonly Color DEFAULTCOLOR = new Color32(90, 97, 105, 255);
/// <summary> Fires every whenever a node was modified through the editor </summary>
public static Action<XNode.Node> onUpdateNode;
public readonly static Dictionary<XNode.NodePort, Vector2> portPositions = new Dictionary<XNode.NodePort, Vector2>();
#if ODIN_INSPECTOR
internal static bool inNodeEditor = false;
#endif
private List<string> excludesField;
private List<string> portNames = new List<string>();
public override void OnCreate()
{
excludesField = new List<string> { "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<string> GetExcludesField()
{
return null;
}
/// <summary> Draws standard field editors for all public fields </summary>
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);
}
/// <summary> Returns color for target node </summary>
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;
}
/// <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) {
// 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);
}
}
/// <summary> Rename the node asset. This will trigger a reimport of the node. </summary>
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<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib {
private Type inspectedType;
/// <summary> Tells a NodeEditor which Node type it is an editor for </summary>
/// <param name="inspectedType">Type that this editor can edit</param>
public CustomNodeEditorAttribute(Type inspectedType) {
this.inspectedType = inspectedType;
}
public Type GetInspectedType() {
return inspectedType;
}
}
}
}

View File

@ -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<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.
} 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;

View File

@ -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;

View File

@ -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)

View File

@ -75,7 +75,7 @@ namespace XNodeEditor {
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) {
KeyValuePair<ContextMenu, MethodInfo>[] items = GetContextMenuMethods(obj);
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()
{
var windows = Resources.FindObjectsOfTypeAll<NodeEditorWindow>();

View File

@ -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);

View File

@ -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();