mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
WIP
This commit is contained in:
parent
098f14f921
commit
b8c08a8eb1
@ -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>
|
||||
|
||||
[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>
|
||||
public static Action<XNode.INode> onUpdateNode;
|
||||
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));
|
||||
}
|
||||
|
||||
@ -93,7 +93,7 @@ namespace XNodeEditor {
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class CustomNodeEditorAttribute : Attribute,
|
||||
XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.INode>.INodeEditorAttrib {
|
||||
XNodeEditor.Internal.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>
|
||||
|
||||
@ -165,7 +165,7 @@ namespace XNodeEditor {
|
||||
}
|
||||
} else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
|
||||
// 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);
|
||||
if (!e.control && !e.shift) selectedReroutes.Clear();
|
||||
} else if (e.control || e.shift) DeselectNode(hoveredNode);
|
||||
@ -209,24 +209,24 @@ namespace XNodeEditor {
|
||||
//If connection is valid, save it
|
||||
if (draggedOutputTarget != null) {
|
||||
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
|
||||
int connectionIndex = draggedOutput.GetConnectionIndex(draggedOutputTarget);
|
||||
if (connectionIndex != -1) {
|
||||
draggedOutput.GetReroutePoints(connectionIndex).AddRange(draggedOutputReroutes);
|
||||
if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node);
|
||||
EditorUtility.SetDirty(graph);
|
||||
EditorUtility.SetDirty((UnityEngine.Object) graph);
|
||||
}
|
||||
}
|
||||
//Release dragged connection
|
||||
draggedOutput = null;
|
||||
draggedOutputTarget = null;
|
||||
EditorUtility.SetDirty(graph);
|
||||
EditorUtility.SetDirty((UnityEngine.Object) graph);
|
||||
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
||||
} else if (currentActivity == NodeActivity.DragNode) {
|
||||
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();
|
||||
} else if (!IsHoveringNode) {
|
||||
// If click outside node, release field focus
|
||||
@ -245,7 +245,7 @@ namespace XNodeEditor {
|
||||
// Double click to center node
|
||||
if (isDoubleClick) {
|
||||
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) {
|
||||
ShowPortContextMenu(hoveredPort);
|
||||
} 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();
|
||||
NodeEditor.GetEditor(hoveredNode, this).AddContextMenuItems(menu);
|
||||
menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
|
||||
|
||||
@ -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="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, 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>
|
||||
private static Dictionary<Type, Type> editorTypes;
|
||||
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; } }
|
||||
private K _target;
|
||||
|
||||
public static T GetEditor(K target, NodeEditorWindow window) {
|
||||
if (target == null) return null;
|
||||
public static T GetEditor<Q>(Q target, NodeEditorWindow window) where Q : class {
|
||||
if ((target as UnityEngine.Object) == null) return default(T);
|
||||
T editor;
|
||||
if (!editors.TryGetValue(target, out editor)) {
|
||||
if (!editors.TryGetValue(target as K, out editor)) {
|
||||
Type type = target.GetType();
|
||||
Type editorType = GetEditorType(type);
|
||||
editor = (T) Editor.CreateEditor(target, editorType);
|
||||
editor = (T) Editor.CreateEditor(target as UnityEngine.Object, editorType);
|
||||
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;
|
||||
return editor;
|
||||
}
|
||||
@ -55,9 +55,9 @@ namespace XNodeEditor.Internal {
|
||||
editorTypes.Add(attrib.GetInspectedType(), nodeEditors[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface INodeEditorAttrib {
|
||||
Type GetInspectedType();
|
||||
}
|
||||
public interface INodeEditorAttrib {
|
||||
Type GetInspectedType();
|
||||
}
|
||||
}
|
||||
@ -34,7 +34,7 @@ namespace XNodeEditor {
|
||||
/// <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.INode.CreateNodeMenuAttribute attrib;
|
||||
XNode.Node.CreateNodeMenuAttribute attrib;
|
||||
if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
|
||||
return attrib.menuName;
|
||||
else // Return generated path
|
||||
@ -70,8 +70,8 @@ namespace XNodeEditor {
|
||||
|
||||
/// <summary> Create a node and save it in the graph asset </summary>
|
||||
public virtual void CreateNode(Type type, Vector2 position) {
|
||||
XNode.INode node = target.AddNode(type);
|
||||
node.Position = 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);
|
||||
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
||||
@ -79,8 +79,8 @@ namespace XNodeEditor {
|
||||
}
|
||||
|
||||
/// <summary> Creates a copy of the original node in the graph </summary>
|
||||
public XNode.INode CopyNode(XNode.INode original) {
|
||||
XNode.INode node = target.CopyNode(original);
|
||||
public XNode.INode CopyNode(XNode.Node original) {
|
||||
XNode.Node node = target.CopyNode( original);
|
||||
node.name = original.name;
|
||||
AssetDatabase.AddObjectToAsset(node, target);
|
||||
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
||||
@ -88,7 +88,7 @@ namespace XNodeEditor {
|
||||
}
|
||||
|
||||
/// <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);
|
||||
target.RemoveNode(node);
|
||||
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
||||
@ -96,7 +96,7 @@ namespace XNodeEditor {
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class CustomNodeGraphEditorAttribute : Attribute,
|
||||
XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph>.INodeEditorAttrib {
|
||||
XNodeEditor.Internal.INodeEditorAttrib {
|
||||
private Type inspectedType;
|
||||
public string editorPrefsKey;
|
||||
/// <summary> Tells a NodeGraphEditor which Graph type it is an editor for </summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user