1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00
This commit is contained in:
Thor Brigsted 2019-05-09 17:21:38 +02:00
parent 098f14f921
commit b8c08a8eb1
4 changed files with 27 additions and 27 deletions

View File

@ -8,13 +8,13 @@ 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> /// <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.INode))] [CustomNodeEditor(typeof(XNode.INode))]
public class NodeEditor : XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.INode> { public class NodeEditor : XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node> {
/// <summary> Fires every whenever a node was modified through the editor </summary> /// <summary> Fires every whenever a node was modified through the editor </summary>
public static Action<XNode.INode> onUpdateNode; public static Action<XNode.INode> onUpdateNode;
public static Dictionary<XNode.NodePort, Vector2> portPositions; public static Dictionary<XNode.NodePort, Vector2> portPositions;
public virtual void OnHeaderGUI() { public new virtual void OnHeaderGUI() {
GUILayout.Label(target.name, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30)); GUILayout.Label(target.name, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
} }
@ -93,7 +93,7 @@ namespace XNodeEditor {
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class CustomNodeEditorAttribute : Attribute, public class CustomNodeEditorAttribute : Attribute,
XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.INode>.INodeEditorAttrib { XNodeEditor.Internal.INodeEditorAttrib {
private Type inspectedType; private Type inspectedType;
/// <summary> Tells a NodeEditor which Node type it is an editor for </summary> /// <summary> Tells a NodeEditor which Node type it is an editor for </summary>
/// <param name="inspectedType">Type that this editor can edit</param> /// <param name="inspectedType">Type that this editor can edit</param>

View File

@ -165,7 +165,7 @@ namespace XNodeEditor {
} }
} else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) { } else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
// If mousedown on node header, select or deselect // If mousedown on node header, select or deselect
if (!Selection.Contains((UnityEngine.Object)hoveredNode)) { if (!Selection.Contains((UnityEngine.Object) hoveredNode)) {
SelectNode(hoveredNode, e.control || e.shift); SelectNode(hoveredNode, e.control || e.shift);
if (!e.control && !e.shift) selectedReroutes.Clear(); if (!e.control && !e.shift) selectedReroutes.Clear();
} else if (e.control || e.shift) DeselectNode(hoveredNode); } else if (e.control || e.shift) DeselectNode(hoveredNode);
@ -209,24 +209,24 @@ namespace XNodeEditor {
//If connection is valid, save it //If connection is valid, save it
if (draggedOutputTarget != null) { if (draggedOutputTarget != null) {
XNode.INode node = draggedOutputTarget.node; XNode.INode node = draggedOutputTarget.node;
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget); if (graph.Nodes.Any()) draggedOutput.Connect(draggedOutputTarget);
// ConnectionIndex can be -1 if the connection is removed instantly after creation // ConnectionIndex can be -1 if the connection is removed instantly after creation
int connectionIndex = draggedOutput.GetConnectionIndex(draggedOutputTarget); int connectionIndex = draggedOutput.GetConnectionIndex(draggedOutputTarget);
if (connectionIndex != -1) { if (connectionIndex != -1) {
draggedOutput.GetReroutePoints(connectionIndex).AddRange(draggedOutputReroutes); draggedOutput.GetReroutePoints(connectionIndex).AddRange(draggedOutputReroutes);
if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node); if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node);
EditorUtility.SetDirty(graph); EditorUtility.SetDirty((UnityEngine.Object) graph);
} }
} }
//Release dragged connection //Release dragged connection
draggedOutput = null; draggedOutput = null;
draggedOutputTarget = null; draggedOutputTarget = null;
EditorUtility.SetDirty(graph); EditorUtility.SetDirty((UnityEngine.Object) graph);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
} else if (currentActivity == NodeActivity.DragNode) { } else if (currentActivity == NodeActivity.DragNode) {
IEnumerable<XNode.INode> nodes = Selection.objects.Where(x => x is XNode.INode).Select(x => x as XNode.INode); IEnumerable<XNode.INode> nodes = Selection.objects.Where(x => x is XNode.INode).Select(x => x as XNode.INode);
foreach (XNode.INode node in nodes) EditorUtility.SetDirty(node); foreach (XNode.INode node in nodes) EditorUtility.SetDirty((UnityEngine.Object) node);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
} else if (!IsHoveringNode) { } else if (!IsHoveringNode) {
// If click outside node, release field focus // If click outside node, release field focus
@ -245,7 +245,7 @@ namespace XNodeEditor {
// Double click to center node // Double click to center node
if (isDoubleClick) { if (isDoubleClick) {
Vector2 nodeDimension = nodeSizes.ContainsKey(hoveredNode) ? nodeSizes[hoveredNode] / 2 : Vector2.zero; Vector2 nodeDimension = nodeSizes.ContainsKey(hoveredNode) ? nodeSizes[hoveredNode] / 2 : Vector2.zero;
panOffset = -hoverednode.Position - nodeDimension; panOffset = -hoveredNode.Position - nodeDimension;
} }
} }
@ -269,7 +269,7 @@ namespace XNodeEditor {
} else if (IsHoveringPort) { } else if (IsHoveringPort) {
ShowPortContextMenu(hoveredPort); ShowPortContextMenu(hoveredPort);
} else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) { } else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
if (!Selection.Contains((UnityEngine.Object)hoveredNode)) SelectNode(hoveredNode, false); if (!Selection.Contains((UnityEngine.Object) hoveredNode)) SelectNode(hoveredNode, false);
GenericMenu menu = new GenericMenu(); GenericMenu menu = new GenericMenu();
NodeEditor.GetEditor(hoveredNode, this).AddContextMenuItems(menu); NodeEditor.GetEditor(hoveredNode, this).AddContextMenuItems(menu);
menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));

View File

@ -10,7 +10,7 @@ namespace XNodeEditor.Internal {
/// <typeparam name="T">Editor Type. Should be the type of the deriving script itself (eg. NodeEditor) </typeparam> /// <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="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> /// <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, NodeEditorBase<T, A, K>.INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : UnityEngine.Object { public abstract class NodeEditorBase<T, A, K> : Editor where A : Attribute, INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : UnityEngine.Object {
/// <summary> Custom editors defined with [CustomNodeEditor] </summary> /// <summary> Custom editors defined with [CustomNodeEditor] </summary>
private static Dictionary<Type, Type> editorTypes; private static Dictionary<Type, Type> editorTypes;
private static Dictionary<K, T> editors = new Dictionary<K, T>(); private static Dictionary<K, T> editors = new Dictionary<K, T>();
@ -18,17 +18,17 @@ namespace XNodeEditor.Internal {
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 == base.target ? _target : _target = (K) base.target; } set { base.target = value; } }
private K _target; private K _target;
public static T GetEditor(K target, NodeEditorWindow window) { public static T GetEditor<Q>(Q target, NodeEditorWindow window) where Q : class {
if (target == null) return null; if ((target as UnityEngine.Object) == null) return default(T);
T editor; T editor;
if (!editors.TryGetValue(target, out editor)) { if (!editors.TryGetValue(target as K, out editor)) {
Type type = target.GetType(); Type type = target.GetType();
Type editorType = GetEditorType(type); Type editorType = GetEditorType(type);
editor = (T) Editor.CreateEditor(target, editorType); editor = (T) Editor.CreateEditor(target as UnityEngine.Object, editorType);
editor.window = window; editor.window = window;
editors.Add(target, editor); editors.Add(target as K, editor);
} }
if (editor.target == null) editor.Initialize(new UnityEngine.Object[] { target }); if (editor.target == null) editor.Initialize(new UnityEngine.Object[] { target as UnityEngine.Object });
if (editor.window != window) editor.window = window; if (editor.window != window) editor.window = window;
return editor; return editor;
} }
@ -55,9 +55,9 @@ namespace XNodeEditor.Internal {
editorTypes.Add(attrib.GetInspectedType(), nodeEditors[i]); editorTypes.Add(attrib.GetInspectedType(), nodeEditors[i]);
} }
} }
}
public interface INodeEditorAttrib { public interface INodeEditorAttrib {
Type GetInspectedType(); Type GetInspectedType();
}
} }
} }

View File

@ -34,7 +34,7 @@ namespace XNodeEditor {
/// <summary> Returns context node menu path. Null or empty strings for hidden nodes. </summary> /// <summary> Returns context node menu path. Null or empty strings for hidden nodes. </summary>
public virtual string GetNodeMenuName(Type type) { public virtual string GetNodeMenuName(Type type) {
//Check if type has the CreateNodeMenuAttribute //Check if type has the CreateNodeMenuAttribute
XNode.INode.CreateNodeMenuAttribute attrib; XNode.Node.CreateNodeMenuAttribute attrib;
if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
return attrib.menuName; return attrib.menuName;
else // Return generated path else // Return generated path
@ -70,8 +70,8 @@ namespace XNodeEditor {
/// <summary> Create a node and save it in the graph asset </summary> /// <summary> Create a node and save it in the graph asset </summary>
public virtual void CreateNode(Type type, Vector2 position) { public virtual void CreateNode(Type type, Vector2 position) {
XNode.INode node = target.AddNode(type); XNode.Node node = target.AddNode(type);
node.Position = position; node.position = position;
if (string.IsNullOrEmpty(node.name)) node.name = UnityEditor.ObjectNames.NicifyVariableName(type.Name); if (string.IsNullOrEmpty(node.name)) node.name = UnityEditor.ObjectNames.NicifyVariableName(type.Name);
AssetDatabase.AddObjectToAsset(node, target); AssetDatabase.AddObjectToAsset(node, target);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
@ -79,8 +79,8 @@ namespace XNodeEditor {
} }
/// <summary> Creates a copy of the original node in the graph </summary> /// <summary> Creates a copy of the original node in the graph </summary>
public XNode.INode CopyNode(XNode.INode original) { public XNode.INode CopyNode(XNode.Node original) {
XNode.INode node = target.CopyNode(original); XNode.Node node = target.CopyNode( original);
node.name = original.name; node.name = original.name;
AssetDatabase.AddObjectToAsset(node, target); AssetDatabase.AddObjectToAsset(node, target);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
@ -88,7 +88,7 @@ namespace XNodeEditor {
} }
/// <summary> Safely remove a node and all its connections. </summary> /// <summary> Safely remove a node and all its connections. </summary>
public void RemoveNode(XNode.INode node) { public void RemoveNode(XNode.Node node) {
UnityEngine.Object.DestroyImmediate(node, true); UnityEngine.Object.DestroyImmediate(node, true);
target.RemoveNode(node); target.RemoveNode(node);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
@ -96,7 +96,7 @@ namespace XNodeEditor {
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class CustomNodeGraphEditorAttribute : Attribute, public class CustomNodeGraphEditorAttribute : Attribute,
XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph>.INodeEditorAttrib { XNodeEditor.Internal.INodeEditorAttrib {
private Type inspectedType; private Type inspectedType;
public string editorPrefsKey; public string editorPrefsKey;
/// <summary> Tells a NodeGraphEditor which Graph type it is an editor for </summary> /// <summary> Tells a NodeGraphEditor which Graph type it is an editor for </summary>