mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
WIP
This commit is contained in:
parent
b8c08a8eb1
commit
8424118184
8
Scripts/Editor/Interfaces.meta
Normal file
8
Scripts/Editor/Interfaces.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ed2b909d4b55a0498bc7fcb43555699
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
14
Scripts/Editor/Interfaces/ICustomEditor.cs
Normal file
14
Scripts/Editor/Interfaces/ICustomEditor.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XNodeEditor {
|
||||
/// <summary>
|
||||
/// Workaround since c# doesn't support nested interfaces.
|
||||
///
|
||||
/// Used with INodeEditor and INodeGraphEditor.
|
||||
/// </summary>
|
||||
public interface ICustomEditor<T> {
|
||||
T Target { get; }
|
||||
SerializedObject SerializedObject { get; }
|
||||
}
|
||||
}
|
||||
11
Scripts/Editor/Interfaces/ICustomEditor.cs.meta
Normal file
11
Scripts/Editor/Interfaces/ICustomEditor.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5085db84cab1cba42ad78c9310b87f36
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
48
Scripts/Editor/Interfaces/INodeEditor.cs
Normal file
48
Scripts/Editor/Interfaces/INodeEditor.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using XNode;
|
||||
|
||||
namespace XNodeEditor {
|
||||
public interface INodeGraphEditor {
|
||||
INodeGraph target { get; }
|
||||
void OnGUI();
|
||||
void OnOpen();
|
||||
Texture2D GetGridTexture();
|
||||
Texture2D GetSecondaryGridTexture();
|
||||
/// <summary> Return default settings for this graph type. This is the settings the user will load if no previous settings have been saved. </summary>
|
||||
NodeEditorPreferences.Settings GetDefaultPreferences();
|
||||
/// <summary> Returns context node menu path. Null or empty strings for hidden nodes. </summary>
|
||||
string GetNodeMenuName(Type type);
|
||||
/// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
|
||||
void AddContextMenuItems(GenericMenu menu);
|
||||
Color GetPortColor(XNode.NodePort port);
|
||||
Color GetTypeColor(Type type);
|
||||
/// <summary> Create a node and save it in the graph asset </summary>
|
||||
XNode.INode CreateNode(Type type, Vector2 position);
|
||||
/// <summary> Creates a copy of the original node in the graph </summary>
|
||||
XNode.INode CopyNode(XNode.INode original);
|
||||
/// <summary> Safely remove a node and all its connections. </summary>
|
||||
void RemoveNode(XNode.INode node);
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class CustomNodeGraphEditorAttribute : Attribute,
|
||||
XNodeEditor.Internal.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Scripts/Editor/Interfaces/INodeEditor.cs.meta
Normal file
11
Scripts/Editor/Interfaces/INodeEditor.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9aa85d886a156c14eb60e06494ae40a5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
16
Scripts/Editor/Interfaces/INodeGraphEditor.cs
Normal file
16
Scripts/Editor/Interfaces/INodeGraphEditor.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class INodeGraphEditor : MonoBehaviour {
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
11
Scripts/Editor/Interfaces/INodeGraphEditor.cs.meta
Normal file
11
Scripts/Editor/Interfaces/INodeGraphEditor.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 527d58696cdd32d429c4a4d93847ca2f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -9,13 +9,13 @@ namespace XNodeEditor.Internal {
|
||||
/// <summary> Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) </summary>
|
||||
/// <typeparam name="T">Editor Type. Should be the type of the deriving script itself (eg. NodeEditor) </typeparam>
|
||||
/// <typeparam name="A">Attribute Type. The attribute used to connect with the runtime type (eg. CustomNodeEditorAttribute) </typeparam>
|
||||
/// <typeparam name="K">Runtime Type. The Object this can be an editor for (eg. Node) </typeparam>
|
||||
public abstract class NodeEditorBase<T, A, K> : Editor where A : Attribute, INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : UnityEngine.Object {
|
||||
/// <typeparam name="K">Runtime Type. The Object this can be an editor for (eg. INode ) </typeparam>
|
||||
public abstract class NodeEditorBase<T, A, K> : Editor where T : NodeEditorBase<T, A, K>, ICustomEditor<K> where A : Attribute, INodeEditorAttrib where K : class {
|
||||
/// <summary> Custom editors defined with [CustomNodeEditor] </summary>
|
||||
private static Dictionary<Type, Type> editorTypes;
|
||||
private static Dictionary<K, T> editors = new Dictionary<K, T>();
|
||||
public NodeEditorWindow window;
|
||||
public new K target { get { return _target == base.target ? _target : _target = (K) base.target; } set { base.target = value; } }
|
||||
public new K target { get { return _target as UnityEngine.Object == base.target ? _target : _target = base.target as K; } set { base.target = value as UnityEngine.Object; } }
|
||||
private K _target;
|
||||
|
||||
public static T GetEditor<Q>(Q target, NodeEditorWindow window) where Q : class {
|
||||
|
||||
@ -3,13 +3,21 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using XNode;
|
||||
|
||||
namespace XNodeEditor {
|
||||
/// <summary> Base class to derive custom NodeGraph 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> {
|
||||
public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.INodeGraph>, ICustomEditor<XNode.INodeGraph>, INodeGraphEditor {
|
||||
|
||||
INodeGraph INodeGraphEditor.target { get { return base.target; } }
|
||||
|
||||
[Obsolete("Use window.position instead")]
|
||||
public Rect position { get { return window.position; } set { window.position = value; } }
|
||||
|
||||
INodeGraph ICustomEditor<INodeGraph>.Target { get { return target as INodeGraph; } }
|
||||
SerializedObject ICustomEditor<INodeGraph>.SerializedObject { get { return serializedObject; } }
|
||||
|
||||
/// <summary> Are we currently renaming a node? </summary>
|
||||
protected bool isRenaming;
|
||||
|
||||
@ -69,28 +77,29 @@ namespace XNodeEditor {
|
||||
}
|
||||
|
||||
/// <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 (string.IsNullOrEmpty(node.name)) node.name = UnityEditor.ObjectNames.NicifyVariableName(type.Name);
|
||||
AssetDatabase.AddObjectToAsset(node, target);
|
||||
public virtual XNode.INode CreateNode(Type type, Vector2 position) {
|
||||
XNode.INode node = ((INodeGraph) target).AddNode(type);
|
||||
node.Position = position;
|
||||
if (string.IsNullOrEmpty(node.Name)) node.Name = UnityEditor.ObjectNames.NicifyVariableName(type.Name);
|
||||
if (node is ScriptableObject) AssetDatabase.AddObjectToAsset(node as ScriptableObject, target);
|
||||
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
||||
NodeEditorWindow.RepaintAll();
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary> Creates a copy of the original node in the graph </summary>
|
||||
public XNode.INode CopyNode(XNode.Node original) {
|
||||
XNode.Node node = target.CopyNode( original);
|
||||
node.name = original.name;
|
||||
AssetDatabase.AddObjectToAsset(node, target);
|
||||
public virtual XNode.INode CopyNode(XNode.INode original) {
|
||||
XNode.INode node = ((INodeGraph) target).CopyNode(original);
|
||||
node.Name = original.Name;
|
||||
if (node is ScriptableObject) AssetDatabase.AddObjectToAsset(node as ScriptableObject, 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);
|
||||
public void RemoveNode(XNode.INode node) {
|
||||
UnityEngine.Object.DestroyImmediate(node as UnityEngine.Object, true);
|
||||
((INodeGraph) target).RemoveNode(node);
|
||||
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user