mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 17:26:02 +08:00
79 lines
3.6 KiB
C#
79 lines
3.6 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> {
|
|
/// <summary> The position of the window in screen space. </summary>
|
|
public Rect position;
|
|
/// <summary> Are we currently renaming a node? </summary>
|
|
protected bool isRenaming;
|
|
|
|
public virtual void OnGUI() { }
|
|
|
|
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 menu path. Returns null if node is not available. </summary>
|
|
public virtual string GetNodePath(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('.', '/'));
|
|
}
|
|
|
|
public virtual Color GetTypeColor(Type type) {
|
|
return NodeEditorPreferences.GetTypeColor(type);
|
|
}
|
|
|
|
/// <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 void RemoveNode(XNode.Node node) {
|
|
UnityEngine.Object.DestroyImmediate(node, true);
|
|
target.RemoveNode(node);
|
|
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="uniquePreferencesID">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;
|
|
}
|
|
}
|
|
}
|
|
} |