mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-21 01:36:03 +08:00
# Conflicts: # Scripts/Editor/NodeEditorAction.cs # Scripts/Editor/NodeEditorGUI.cs # Scripts/Editor/NodeGraphEditor.cs
191 lines
9.2 KiB
C#
191 lines
9.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace XNodeEditor {
|
|
/// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>
|
|
[CustomNodeGraphEditor(typeof(XNode.NodeGraph))]
|
|
public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph> {
|
|
[Obsolete("Use window.position instead")]
|
|
public Rect position { get { return window.position; } set { window.position = value; } }
|
|
/// <summary> Are we currently renaming a node? </summary>
|
|
protected bool isRenaming;
|
|
|
|
public virtual void OnGUI() { }
|
|
|
|
/// <summary> Called when opened by NodeEditorWindow </summary>
|
|
public virtual void OnOpen() { }
|
|
|
|
public virtual Texture2D GetGridTexture() {
|
|
return NodeEditorPreferences.GetSettings().gridTexture;
|
|
}
|
|
|
|
public virtual Texture2D GetSecondaryGridTexture() {
|
|
return NodeEditorPreferences.GetSettings().crossTexture;
|
|
}
|
|
|
|
/// <summary> Return default settings for this graph type. This is the settings the user will load if no previous settings have been saved. </summary>
|
|
public virtual NodeEditorPreferences.Settings GetDefaultPreferences() {
|
|
return new NodeEditorPreferences.Settings();
|
|
}
|
|
|
|
/// <summary> Returns context node menu path. Null or empty strings for hidden nodes. </summary>
|
|
public virtual string GetNodeMenuName(Type type) {
|
|
//Check if type has the CreateNodeMenuAttribute
|
|
XNode.Node.CreateNodeMenuAttribute attrib;
|
|
if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
|
|
return attrib.menuName;
|
|
else // Return generated path
|
|
return ObjectNames.NicifyVariableName(type.ToString().Replace('.', '/'));
|
|
}
|
|
|
|
/// <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) {
|
|
Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);
|
|
for (int i = 0; i < NodeEditorWindow.nodeTypes.Length; i++) {
|
|
Type type = NodeEditorWindow.nodeTypes[i];
|
|
|
|
//Get node context menu path
|
|
string path = GetNodeMenuName(type);
|
|
if (string.IsNullOrEmpty(path)) continue;
|
|
|
|
menu.AddItem(new GUIContent(path), false, () => {
|
|
CreateNode(type, pos);
|
|
});
|
|
}
|
|
menu.AddSeparator("");
|
|
menu.AddItem(new GUIContent("Add group"), false, () => CreateGroup(pos));
|
|
menu.AddSeparator("");
|
|
if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0) menu.AddItem(new GUIContent("Paste"), false, () => NodeEditorWindow.current.PasteNodes(pos));
|
|
else menu.AddDisabledItem(new GUIContent("Paste"));
|
|
menu.AddItem(new GUIContent("Preferences"), false, () => NodeEditorWindow.OpenPreferences());
|
|
NodeEditorWindow.AddCustomContextMenuItems(menu, target);
|
|
}
|
|
|
|
/// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
|
|
public virtual void AddGroupContextMenuItems(GenericMenu menu) {
|
|
Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);
|
|
for (int i = 0; i < NodeEditorWindow.nodeTypes.Length; i++) {
|
|
Type type = NodeEditorWindow.nodeTypes[i];
|
|
|
|
//Get node context menu path
|
|
string path = GetNodeMenuName(type);
|
|
if (string.IsNullOrEmpty(path)) continue;
|
|
|
|
menu.AddItem(new GUIContent(path), false, () => {
|
|
CreateNode(type, pos);
|
|
});
|
|
}
|
|
|
|
menu.AddSeparator("");
|
|
menu.AddItem(new GUIContent("Add group"), false, () => CreateGroup(pos));
|
|
menu.AddSeparator("");
|
|
// Actions if only one node is selected
|
|
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.NodeGroup) {
|
|
XNode.NodeGroup group = Selection.activeObject as XNode.NodeGroup;
|
|
menu.AddItem(new GUIContent("Move To Top"), false, () => NodeEditorWindow.current.MoveGroupToTop(group));
|
|
menu.AddItem(new GUIContent("Rename"), false, NodeEditorWindow.current.RenameSelectedGroup);
|
|
}
|
|
|
|
// Add actions to any number of selected nodes
|
|
menu.AddItem(new GUIContent("Duplicate"), false, NodeEditorWindow.current.DuplicateSelectedNodes);
|
|
menu.AddItem(new GUIContent("Remove"), false, NodeEditorWindow.current.RemoveSelectedNodes);
|
|
}
|
|
|
|
public virtual Color GetPortColor(XNode.NodePort port) {
|
|
return GetTypeColor(port.ValueType);
|
|
}
|
|
|
|
public virtual Color GetTypeColor(Type type) {
|
|
return NodeEditorPreferences.GetTypeColor(type);
|
|
}
|
|
|
|
public virtual string GetPortTooltip(XNode.NodePort port) {
|
|
Type portType = port.ValueType;
|
|
string tooltip = "";
|
|
tooltip = portType.PrettyName();
|
|
if (port.IsOutput) {
|
|
object obj = port.node.GetValue(port);
|
|
tooltip += " = " + (obj != null ? obj.ToString() : "null");
|
|
}
|
|
return tooltip;
|
|
}
|
|
|
|
/// <summary> Deal with objects dropped into the graph through DragAndDrop </summary>
|
|
public virtual void OnDropObjects(UnityEngine.Object[] objects) {
|
|
Debug.Log("No OnDropItems override defined for " + GetType());
|
|
}
|
|
|
|
/// <summary> Create a node and save it in the graph asset </summary>
|
|
public virtual void CreateNode(Type type, Vector2 position) {
|
|
XNode.Node node = target.AddNode(type);
|
|
node.position = position;
|
|
if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type);
|
|
AssetDatabase.AddObjectToAsset(node, target);
|
|
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
|
NodeEditorWindow.RepaintAll();
|
|
}
|
|
|
|
/// <summary> Creates a copy of the original node in the graph </summary>
|
|
public XNode.Node CopyNode(XNode.Node original) {
|
|
XNode.Node node = target.CopyNode(original);
|
|
node.name = original.name;
|
|
AssetDatabase.AddObjectToAsset(node, target);
|
|
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
|
return node;
|
|
}
|
|
|
|
/// <summary> Safely remove a node and all its connections. </summary>
|
|
public virtual void RemoveNode(XNode.Node node) {
|
|
target.RemoveNode(node);
|
|
UnityEngine.Object.DestroyImmediate(node, true);
|
|
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
|
}
|
|
|
|
/// <summary> Create a group and save it in the graph asset </summary>
|
|
public void CreateGroup(Vector2 position) {
|
|
XNode.NodeGroup group = target.AddGroup();
|
|
group.position = position;
|
|
group.name = "New group";
|
|
AssetDatabase.AddObjectToAsset(group, target);
|
|
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
|
NodeEditorWindow.RepaintAll();
|
|
}
|
|
|
|
/// <summary> Creates a copy of the original group in the graph </summary>
|
|
public XNode.NodeGroup CopyGroup(XNode.NodeGroup original) {
|
|
XNode.NodeGroup group = target.CopyGroup(original);
|
|
group.name = original.name;
|
|
AssetDatabase.AddObjectToAsset(group, target);
|
|
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
|
return group;
|
|
}
|
|
|
|
/// <summary> Safely remove a group </summary>
|
|
public void RemoveGroup(XNode.NodeGroup group) {
|
|
UnityEngine.Object.DestroyImmediate(group, true);
|
|
target.RemoveGroup(group);
|
|
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
|
}
|
|
|
|
[AttributeUsage(AttributeTargets.Class)]
|
|
public class CustomNodeGraphEditorAttribute : Attribute,
|
|
XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph>.INodeEditorAttrib {
|
|
private Type inspectedType;
|
|
public string editorPrefsKey;
|
|
/// <summary> Tells a NodeGraphEditor which Graph type it is an editor for </summary>
|
|
/// <param name="inspectedType">Type that this editor can edit</param>
|
|
/// <param name="editorPrefsKey">Define unique key for unique layout settings instance</param>
|
|
public CustomNodeGraphEditorAttribute(Type inspectedType, string editorPrefsKey = "xNode.Settings") {
|
|
this.inspectedType = inspectedType;
|
|
this.editorPrefsKey = editorPrefsKey;
|
|
}
|
|
|
|
public Type GetInspectedType() {
|
|
return inspectedType;
|
|
}
|
|
}
|
|
}
|
|
} |