1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00
xNode/Scripts/Editor/NodeEditor.cs
Icarus f77183cca3 Sync Siccity https://github.com/Siccity/xNode
# Conflicts:
#	Scripts/Editor/NodeEditor.cs
#	Scripts/Editor/NodeEditorAction.cs
#	Scripts/Editor/NodeEditorPreferences.cs
2019-12-09 14:02:28 +08:00

211 lines
8.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ƵĶ˿<C4B6>
foreach (var port in target.Ports)
{
//<2F><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (port.IsDynamic)
{
continue;
}
//<2F><><EFBFBD><EFBFBD>unity<74><79><EFBFBD>л<EFBFBD>֧<EFBFBD><D6A7>,<2C><><EFBFBD>DZ<EFBFBD><C7B1><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
}
}
}
}