1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-02-04 22:34:54 +08:00

Defined namespaces across all scripts prevent conflicts.

This commit is contained in:
Thor Brigsted 2017-12-02 09:56:41 +01:00
parent 3332b64795
commit d7e133a822
12 changed files with 68 additions and 68 deletions

View File

@ -4,5 +4,5 @@ using XNode;
/// <summary> Defines an example nodegraph. </summary>
[Serializable, CreateAssetMenu(fileName = "ExampleNodeGraph", menuName = "Node Graph/Example")]
public class ExampleNodeGraph : NodeGraph {
public class ExampleNodeGraph : XNode.NodeGraph {
}

View File

@ -1,13 +1,13 @@
using XNode;
namespace BasicNodes {
public class DisplayValue : Node {
public class DisplayValue : XNode.Node {
protected override void Init() {
base.Init();
if (!HasPort("input")) AddInstanceInput(typeof(object), "input");
}
public override object GetValue(NodePort port) {
public override object GetValue(XNode.NodePort port) {
return GetInputValue<object>("input");
}
}

View File

@ -2,7 +2,7 @@
namespace BasicNodes {
[System.Serializable]
public class MathNode : Node {
public class MathNode : XNode.Node {
// Adding [Input] or [Output] is all you need to do to register a field as a valid port on your node
[Input] public float a;
[Input] public float b;
@ -14,7 +14,7 @@ namespace BasicNodes {
public enum MathType { Add, Subtract, Multiply, Divide }
// GetValue should be overridden to return a value for any specified output port
public override object GetValue(NodePort port) {
public override object GetValue(XNode.NodePort port) {
// Get new a and b values from input connections. Fallback to field values if input is not connected
float a = GetInputValue<float>("a", this.a);

View File

@ -2,11 +2,11 @@
using XNode;
namespace BasicNodes {
public class Vector : Node {
public class Vector : XNode.Node {
[Input] public float x, y, z;
[Output] public Vector3 vector;
public override object GetValue(NodePort port) {
public override object GetValue(XNode.NodePort port) {
vector.x = GetInputValue<float>("x", this.x);
vector.y = GetInputValue<float>("y", this.y);
vector.z = GetInputValue<float>("z", this.z);

View File

@ -8,12 +8,12 @@ using XNode;
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(Node))]
public class NodeEditor : XNodeInternal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, Node> {
[CustomNodeEditor(typeof(XNode.Node))]
public class NodeEditor : XNodeInternal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node> {
/// <summary> Fires every whenever a node was modified through the editor </summary>
public static Action<Node> onUpdateNode;
public static Dictionary<NodePort, Vector2> portPositions;
public static Action<XNode.Node> onUpdateNode;
public static Dictionary<XNode.NodePort, Vector2> portPositions;
/// <summary> Draws the node GUI.</summary>
/// <param name="portPositions">Port handle positions need to be returned to the NodeEditorWindow </param>
@ -31,7 +31,7 @@ namespace XNodeEditor {
/// <summary> Draws standard field editors for all public fields </summary>
public virtual void OnBodyGUI() {
string[] excludes = { "m_Script", "graph", "position", "ports" };
portPositions = new Dictionary<NodePort, Vector2>();
portPositions = new Dictionary<XNode.NodePort, Vector2>();
SerializedProperty iterator = serializedObject.GetIterator();
bool enterChildren = true;

View File

@ -15,13 +15,13 @@ namespace XNodeEditor {
private bool IsHoveringNode { get { return hoveredNode != null; } }
private bool HasSelectedNode { get { return selectedNode != null; } }
private Node hoveredNode = null;
private XNode.Node hoveredNode = null;
[NonSerialized] private Node selectedNode = null;
[NonSerialized] private Node draggedNode = null;
[NonSerialized] private NodePort hoveredPort = null;
[NonSerialized] private NodePort draggedOutput = null;
[NonSerialized] private NodePort draggedOutputTarget = null;
[NonSerialized] private XNode.Node selectedNode = null;
[NonSerialized] private XNode.Node draggedNode = null;
[NonSerialized] private XNode.NodePort hoveredPort = null;
[NonSerialized] private XNode.NodePort draggedOutput = null;
[NonSerialized] private XNode.NodePort draggedOutputTarget = null;
private Rect nodeRects;
@ -69,8 +69,8 @@ namespace XNodeEditor {
} else {
hoveredPort.VerifyConnections();
if (hoveredPort.IsConnected) {
Node node = hoveredPort.node;
NodePort output = hoveredPort.Connection;
XNode.Node node = hoveredPort.node;
XNode.NodePort output = hoveredPort.Connection;
hoveredPort.Disconnect(output);
draggedOutput = output;
draggedOutputTarget = hoveredPort;
@ -89,7 +89,7 @@ namespace XNodeEditor {
if (IsDraggingPort) {
//If connection is valid, save it
if (draggedOutputTarget != null) {
Node node = draggedOutputTarget.node;
XNode.Node node = draggedOutputTarget.node;
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node);
EditorUtility.SetDirty(graph);
@ -127,7 +127,7 @@ namespace XNodeEditor {
}
public void CreateNode(Type type, Vector2 position) {
Node node = graph.AddNode(type);
XNode.Node node = graph.AddNode(type);
node.position = position;
Repaint();
}
@ -144,7 +144,7 @@ namespace XNodeEditor {
}
}
bool IsHoveringTitle(Node node) {
bool IsHoveringTitle(XNode.Node node) {
Vector2 mousePos = Event.current.mousePosition;
//Get node position
Vector2 nodePos = GridToWindowPosition(node.position);

View File

@ -12,7 +12,7 @@ namespace XNodeEditor {
UnityEditor.MonoScript script = obj as UnityEditor.MonoScript;
System.Type scriptType = script.GetClass();
if (scriptType != typeof(Node) && !scriptType.IsSubclassOf(typeof(Node))) return AssetDeleteResult.DidNotDelete;
if (scriptType != typeof(XNode.Node) && !scriptType.IsSubclassOf(typeof(XNode.Node))) return AssetDeleteResult.DidNotDelete;
//Find ScriptableObjects using this script
string[] guids = AssetDatabase.FindAssets("t:" + scriptType);
@ -20,7 +20,7 @@ namespace XNodeEditor {
string assetpath = AssetDatabase.GUIDToAssetPath(guids[i]);
Object[] objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(assetpath);
for (int k = 0; k < objs.Length; k++) {
Node node = objs[k] as Node;
XNode.Node node = objs[k] as XNode.Node;
if (node.GetType() == scriptType) {
if (node != null && node.graph != null) {
Debug.LogWarning(node.name + " of " + node.graph + " depended on deleted script and has been removed automatically.", node.graph);

View File

@ -76,7 +76,7 @@ namespace XNodeEditor {
}
/// <summary> Show right-click context menu for a node </summary>
public void ShowNodeContextMenu(Node node) {
public void ShowNodeContextMenu(XNode.Node node) {
GenericMenu contextMenu = new GenericMenu();
contextMenu.AddItem(new GUIContent("Move To Top"), false, () => {
int index;
@ -86,7 +86,7 @@ namespace XNodeEditor {
}
});
contextMenu.AddItem(new GUIContent("Duplicate"), false, () => {
Node n = graph.CopyNode(node);
XNode.Node n = graph.CopyNode(node);
n.position = node.position + new Vector2(30, 30);
});
contextMenu.AddItem(new GUIContent("Remove"), false, () => graph.RemoveNode(node));
@ -145,17 +145,17 @@ namespace XNodeEditor {
/// <summary> Draws all connections </summary>
public void DrawConnections() {
foreach (Node node in graph.nodes) {
foreach (XNode.Node node in graph.nodes) {
//If a null node is found, return. This can happen if the nodes associated script is deleted. It is currently not possible in Unity to delete a null asset.
if (node == null) continue;
foreach (NodePort output in node.Outputs) {
foreach (XNode.NodePort output in node.Outputs) {
//Needs cleanup. Null checks are ugly
if (!portConnectionPoints.ContainsKey(output)) continue;
Vector2 from = _portConnectionPoints[output].center;
for (int k = 0; k < output.ConnectionCount; k++) {
NodePort input = output.GetConnection(k);
XNode.NodePort input = output.GetConnection(k);
if (input == null) continue; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return.
if (!input.IsConnectedTo(output)) input.Connect(output);
if (!_portConnectionPoints.ContainsKey(input)) continue;
@ -196,10 +196,10 @@ namespace XNodeEditor {
for (int n = 0; n < graph.nodes.Count; n++) {
while (graph.nodes[n] == null) graph.nodes.RemoveAt(n);
if (n >= graph.nodes.Count) return;
Node node = graph.nodes[n];
XNode.Node node = graph.nodes[n];
NodeEditor nodeEditor = NodeEditor.GetEditor(node);
NodeEditor.portPositions = new Dictionary<NodePort, Vector2>();
NodeEditor.portPositions = new Dictionary<XNode.NodePort, Vector2>();
//Get node position
Vector2 nodePos = GridToWindowPositionNoClipped(node.position);
@ -244,14 +244,14 @@ namespace XNodeEditor {
//Check if we are hovering any of this nodes ports
//Check input ports
foreach (NodePort input in node.Inputs) {
foreach (XNode.NodePort input in node.Inputs) {
//Check if port rect is available
if (!portConnectionPoints.ContainsKey(input)) continue;
Rect r = GridToWindowRect(portConnectionPoints[input]);
if (r.Contains(mousePos)) hoveredPort = input;
}
//Check all output ports
foreach (NodePort output in node.Outputs) {
foreach (XNode.NodePort output in node.Outputs) {
//Check if port rect is available
if (!portConnectionPoints.ContainsKey(output)) continue;
Rect r = GridToWindowRect(portConnectionPoints[output]);

View File

@ -16,18 +16,18 @@ namespace XNodeEditor {
/// <summary> Make a field for a serialized property. Automatically displays relevant node port. </summary>
public static void PropertyField(SerializedProperty property, GUIContent label, bool includeChildren = true, params GUILayoutOption[] options) {
if (property == null) throw new NullReferenceException();
Node node = property.serializedObject.targetObject as Node;
NodePort port = node.GetPort(property.name);
XNode.Node node = property.serializedObject.targetObject as XNode.Node;
XNode.NodePort port = node.GetPort(property.name);
PropertyField(property, label, port, includeChildren);
}
/// <summary> Make a field for a serialized property. Manual node port override. </summary>
public static void PropertyField(SerializedProperty property, NodePort port, bool includeChildren = true, params GUILayoutOption[] options) {
public static void PropertyField(SerializedProperty property, XNode.NodePort port, bool includeChildren = true, params GUILayoutOption[] options) {
PropertyField(property, null, port, includeChildren, options);
}
/// <summary> Make a field for a serialized property. Manual node port override. </summary>
public static void PropertyField(SerializedProperty property, GUIContent label, NodePort port, bool includeChildren = true, params GUILayoutOption[] options) {
public static void PropertyField(SerializedProperty property, GUIContent label, XNode.NodePort port, bool includeChildren = true, params GUILayoutOption[] options) {
if (property == null) throw new NullReferenceException();
// If property is not a port, display a regular property field
@ -36,24 +36,24 @@ namespace XNodeEditor {
Rect rect = new Rect();
// If property is an input, display a regular property field and put a port handle on the left side
if (port.direction == NodePort.IO.Input) {
if (port.direction == XNode.NodePort.IO.Input) {
// Get data from [Input] attribute
Node.ShowBackingValue showBacking = Node.ShowBackingValue.Unconnected;
Node.InputAttribute inputAttribute;
XNode.Node.ShowBackingValue showBacking = XNode.Node.ShowBackingValue.Unconnected;
XNode.Node.InputAttribute inputAttribute;
if (NodeEditorUtilities.GetAttrib(port.node.GetType(), property.name, out inputAttribute)) showBacking = inputAttribute.backingValue;
switch (showBacking) {
case Node.ShowBackingValue.Unconnected:
case XNode.Node.ShowBackingValue.Unconnected:
// Display a label if port is connected
if (port.IsConnected) EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName));
// Display an editable property field if port is not connected
else EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
break;
case Node.ShowBackingValue.Never:
case XNode.Node.ShowBackingValue.Never:
// Display a label
EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName));
break;
case Node.ShowBackingValue.Always:
case XNode.Node.ShowBackingValue.Always:
// Display an editable property field
EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
break;
@ -62,24 +62,24 @@ namespace XNodeEditor {
rect = GUILayoutUtility.GetLastRect();
rect.position = rect.position - new Vector2(16, 0);
// If property is an output, display a text label and put a port handle on the right side
} else if (port.direction == NodePort.IO.Output) {
} else if (port.direction == XNode.NodePort.IO.Output) {
// Get data from [Output] attribute
Node.ShowBackingValue showBacking = Node.ShowBackingValue.Unconnected;
Node.OutputAttribute outputAttribute;
XNode.Node.ShowBackingValue showBacking = XNode.Node.ShowBackingValue.Unconnected;
XNode.Node.OutputAttribute outputAttribute;
if (NodeEditorUtilities.GetAttrib(port.node.GetType(), property.name, out outputAttribute)) showBacking = outputAttribute.backingValue;
switch (showBacking) {
case Node.ShowBackingValue.Unconnected:
case XNode.Node.ShowBackingValue.Unconnected:
// Display a label if port is connected
if (port.IsConnected) EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), NodeEditorResources.styles.outputPort, GUILayout.MinWidth(30));
// Display an editable property field if port is not connected
else EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
break;
case Node.ShowBackingValue.Never:
case XNode.Node.ShowBackingValue.Never:
// Display a label
EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), NodeEditorResources.styles.outputPort, GUILayout.MinWidth(30));
break;
case Node.ShowBackingValue.Always:
case XNode.Node.ShowBackingValue.Always:
// Display an editable property field
EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
break;
@ -104,18 +104,18 @@ namespace XNodeEditor {
}
/// <summary> Make a simple port field. </summary>
public static void PortField(NodePort port, params GUILayoutOption[] options) {
public static void PortField(XNode.NodePort port, params GUILayoutOption[] options) {
PortField(null, port, options);
}
/// <summary> Make a simple port field. </summary>
public static void PortField(GUIContent label, NodePort port, params GUILayoutOption[] options) {
public static void PortField(GUIContent label, XNode.NodePort port, params GUILayoutOption[] options) {
if (port == null) return;
if (label == null) EditorGUILayout.LabelField(ObjectNames.NicifyVariableName(port.fieldName), options);
else EditorGUILayout.LabelField(label, options);
Rect rect = GUILayoutUtility.GetLastRect();
if (port.direction == NodePort.IO.Input) rect.position = rect.position - new Vector2(16, 0);
else if (port.direction == NodePort.IO.Output) rect.position = rect.position + new Vector2(rect.width, 0);
if (port.direction == XNode.NodePort.IO.Input) rect.position = rect.position - new Vector2(16, 0);
else if (port.direction == XNode.NodePort.IO.Output) rect.position = rect.position + new Vector2(rect.width, 0);
rect.size = new Vector2(16, 16);
Color backgroundColor = new Color32(90, 97, 105, 255);

View File

@ -19,15 +19,15 @@ namespace XNodeEditor {
public static Type[] GetNodeTypes() {
//Get all classes deriving from Node via reflection
return GetDerivedTypes(typeof(Node));
return GetDerivedTypes(typeof(XNode.Node));
}
public static Dictionary<Type, Color> GetNodeTint() {
Dictionary<Type, Color> tints = new Dictionary<Type, Color>();
for (int i = 0; i < nodeTypes.Length; i++) {
var attribs = nodeTypes[i].GetCustomAttributes(typeof(Node.NodeTint), true);
var attribs = nodeTypes[i].GetCustomAttributes(typeof(XNode.Node.NodeTint), true);
if (attribs == null || attribs.Length == 0) continue;
Node.NodeTint attrib = attribs[0] as Node.NodeTint;
XNode.Node.NodeTint attrib = attribs[0] as XNode.Node.NodeTint;
tints.Add(nodeTypes[i], attrib.color);
}
return tints;

View File

@ -10,11 +10,11 @@ namespace XNodeEditor {
public static NodeEditorWindow current;
/// <summary> Stores node positions for all nodePorts. </summary>
public Dictionary<NodePort, Rect> portConnectionPoints { get { return _portConnectionPoints; } }
private Dictionary<NodePort, Rect> _portConnectionPoints = new Dictionary<NodePort, Rect>();
public Dictionary<Node, float> nodeWidths { get { return _nodeWidths; } }
private Dictionary<Node, float> _nodeWidths = new Dictionary<Node, float>();
public NodeGraph graph;
public Dictionary<XNode.NodePort, Rect> portConnectionPoints { get { return _portConnectionPoints; } }
private Dictionary<XNode.NodePort, Rect> _portConnectionPoints = new Dictionary<XNode.NodePort, Rect>();
public Dictionary<XNode.Node, float> nodeWidths { get { return _nodeWidths; } }
private Dictionary<XNode.Node, float> _nodeWidths = new Dictionary<XNode.Node, float>();
public XNode.NodeGraph graph;
public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } }
private Vector2 _panOffset;
public float zoom { get { return _zoom; } set { _zoom = Mathf.Clamp(value, 1f, 5f); Repaint(); } }
@ -46,7 +46,7 @@ namespace XNodeEditor {
string path = EditorUtility.SaveFilePanelInProject("Save NodeGraph", "NewNodeGraph", "asset", "");
if (string.IsNullOrEmpty(path)) return;
else {
NodeGraph existingGraph = AssetDatabase.LoadAssetAtPath<NodeGraph>(path);
XNode.NodeGraph existingGraph = AssetDatabase.LoadAssetAtPath<XNode.NodeGraph>(path);
if (existingGraph != null) AssetDatabase.DeleteAsset(path);
AssetDatabase.CreateAsset(graph, path);
EditorUtility.SetDirty(graph);
@ -78,13 +78,13 @@ namespace XNodeEditor {
return new Vector2(xOffset, yOffset);
}
public void SelectNode(Node node) {
public void SelectNode(XNode.Node node) {
selectedNode = node;
}
[OnOpenAsset(0)]
public static bool OnOpen(int instanceID, int line) {
NodeGraph nodeGraph = EditorUtility.InstanceIDToObject(instanceID) as NodeGraph;
XNode.NodeGraph nodeGraph = EditorUtility.InstanceIDToObject(instanceID) as XNode.NodeGraph;
if (nodeGraph != null) {
NodeEditorWindow w = Init();
w.graph = nodeGraph;

View File

@ -7,8 +7,8 @@ using XNode;
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(NodeGraph))]
public class NodeGraphEditor : XNodeInternal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, NodeGraph> {
[CustomNodeGraphEditor(typeof(XNode.NodeGraph))]
public class NodeGraphEditor : XNodeInternal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph> {
/// <summary> Custom node editors defined with [CustomNodeGraphEditor] </summary>
[NonSerialized] private static Dictionary<Type, NodeGraphEditor> editors;
@ -23,7 +23,7 @@ namespace XNodeEditor {
/// <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
Node.CreateNodeMenuAttribute attrib;
XNode.Node.CreateNodeMenuAttribute attrib;
if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
return attrib.menuName;
else // Return generated path