mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 17:26:02 +08:00
Added VerifyConnections, Added GetPortByFieldName.
This commit is contained in:
parent
7330041bde
commit
6a4c29410a
Binary file not shown.
@ -26,6 +26,40 @@ public abstract class Node : ScriptableObject {
|
|||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary> Checks all connections for invalid references, and removes them. </summary>
|
||||||
|
public void VerifyConnections() {
|
||||||
|
for (int i = 0; i < InputCount; i++) {
|
||||||
|
inputs[i].VerifyConnections();
|
||||||
|
}
|
||||||
|
for (int i = 0; i < OutputCount; i++) {
|
||||||
|
outputs[i].VerifyConnections();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Returns input or output port which matches fieldName </summary>
|
||||||
|
public NodePort GetPortByFieldName(string fieldName) {
|
||||||
|
NodePort port = GetOutputByFieldName(fieldName);
|
||||||
|
if (port != null) return port;
|
||||||
|
else return GetInputByFieldName(fieldName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Returns output port which matches fieldName </summary>
|
||||||
|
public NodePort GetOutputByFieldName(string fieldName) {
|
||||||
|
for (int i = 0; i < OutputCount; i++) {
|
||||||
|
if (outputs[i].fieldName == fieldName) return outputs[i];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Returns input port which matches fieldName </summary>
|
||||||
|
public NodePort GetInputByFieldName(string fieldName) {
|
||||||
|
for (int i = 0; i < InputCount; i++) {
|
||||||
|
if (inputs[i].fieldName == fieldName) return inputs[i];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary> Initialize node. Called on creation. </summary>
|
/// <summary> Initialize node. Called on creation. </summary>
|
||||||
protected virtual void Init() { name = GetType().Name; }
|
protected virtual void Init() { name = GetType().Name; }
|
||||||
|
|
||||||
@ -48,13 +82,6 @@ public abstract class Node : ScriptableObject {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NodePort CreateNodeInput(string name, Type type) {
|
|
||||||
return new NodePort(name, type, this, NodePort.IO.Input);
|
|
||||||
}
|
|
||||||
public NodePort CreateNodeOutput(string name, Type type) {
|
|
||||||
return new NodePort(name, type, this, NodePort.IO.Output);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClearConnections() {
|
public void ClearConnections() {
|
||||||
for (int i = 0; i < inputs.Count; i++) {
|
for (int i = 0; i < inputs.Count; i++) {
|
||||||
inputs[i].ClearConnections();
|
inputs[i].ClearConnections();
|
||||||
@ -96,8 +123,8 @@ public abstract class Node : ScriptableObject {
|
|||||||
else if (attribs[k] is OutputAttribute) outputAttrib = attribs[k] as OutputAttribute;
|
else if (attribs[k] is OutputAttribute) outputAttrib = attribs[k] as OutputAttribute;
|
||||||
}
|
}
|
||||||
if (inputAttrib != null && outputAttrib != null) Debug.LogError("Field " + fieldInfo + " cannot be both input and output.");
|
if (inputAttrib != null && outputAttrib != null) Debug.LogError("Field " + fieldInfo + " cannot be both input and output.");
|
||||||
else if (inputAttrib != null) inputPorts.Add(new NodePort(fieldInfo[i].Name, fieldInfo[i].FieldType, this, NodePort.IO.Input));
|
else if (inputAttrib != null) inputPorts.Add(new NodePort(fieldInfo[i], this));
|
||||||
else if (outputAttrib != null) outputPorts.Add(new NodePort(fieldInfo[i].Name, fieldInfo[i].FieldType, this, NodePort.IO.Output));
|
else if (outputAttrib != null) outputPorts.Add(new NodePort(fieldInfo[i], this));
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs = inputPorts;
|
inputs = inputPorts;
|
||||||
|
|||||||
@ -44,6 +44,14 @@ public abstract class NodeGraph : ScriptableObject, ISerializationCallbackReceiv
|
|||||||
for (int i = 0; i < nodes.Count; i++) {
|
for (int i = 0; i < nodes.Count; i++) {
|
||||||
nodes[i].graph = this;
|
nodes[i].graph = this;
|
||||||
}
|
}
|
||||||
|
VerifyConnections();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Checks all connections for invalid references, and removes them. </summary>
|
||||||
|
public void VerifyConnections() {
|
||||||
|
for (int i = 0; i < nodes.Count; i++) {
|
||||||
|
nodes[i].VerifyConnections();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,10 +2,11 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class NodePort {
|
public class NodePort {
|
||||||
public enum IO { Input, Output}
|
public enum IO { Input, Output }
|
||||||
|
|
||||||
public int ConnectionCount { get { return connections.Count; } }
|
public int ConnectionCount { get { return connections.Count; } }
|
||||||
/// <summary> Return the first connection </summary>
|
/// <summary> Return the first connection </summary>
|
||||||
@ -18,24 +19,40 @@ public class NodePort {
|
|||||||
public bool IsOutput { get { return direction == IO.Output; } }
|
public bool IsOutput { get { return direction == IO.Output; } }
|
||||||
|
|
||||||
public Node node { get; private set; }
|
public Node node { get; private set; }
|
||||||
public string name { get { return _name; } }
|
[SerializeField] public string name;
|
||||||
public bool enabled { get { return _enabled; } set { _enabled = value; } }
|
public bool enabled { get { return _enabled; } set { _enabled = value; } }
|
||||||
public string id { get { return _id; } }
|
public string fieldName { get { return _fieldName; } }
|
||||||
|
|
||||||
|
|
||||||
[SerializeField] private List<PortConnection> connections = new List<PortConnection>();
|
[SerializeField] private List<PortConnection> connections = new List<PortConnection>();
|
||||||
[SerializeField] private string asdf;
|
[SerializeField] private string _fieldName;
|
||||||
[SerializeField] private string _id;
|
|
||||||
[SerializeField] public Type type;
|
[SerializeField] public Type type;
|
||||||
[SerializeField] private string _name;
|
|
||||||
[SerializeField] private bool _enabled = true;
|
[SerializeField] private bool _enabled = true;
|
||||||
[SerializeField] private IO _direction;
|
[SerializeField] private IO _direction;
|
||||||
|
|
||||||
public NodePort(string name, Type type, Node node, IO direction) {
|
public NodePort(FieldInfo fieldInfo, Node node) {
|
||||||
_name = name;
|
_fieldName = fieldInfo.Name;
|
||||||
this.type = type;
|
name = _fieldName;
|
||||||
|
type = fieldInfo.FieldType;
|
||||||
this.node = node;
|
this.node = node;
|
||||||
_direction = direction;
|
|
||||||
_id = node.GetInstanceID() + _name;
|
var attribs = fieldInfo.GetCustomAttributes(false);
|
||||||
|
for (int i = 0; i < attribs.Length; i++) {
|
||||||
|
if (attribs[i] is Node.InputAttribute) _direction = IO.Input;
|
||||||
|
else if (attribs[i] is Node.InputAttribute) _direction = IO.Output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Checks all connections for invalid references, and removes them. </summary>
|
||||||
|
public void VerifyConnections() {
|
||||||
|
for (int i = 0; i < connections.Count; i++) {
|
||||||
|
if (connections[i].node != null &&
|
||||||
|
!string.IsNullOrEmpty(connections[i].fieldName) &&
|
||||||
|
connections[i].node.GetPortByFieldName(connections[i].fieldName) != null)
|
||||||
|
continue;
|
||||||
|
Debug.LogWarning("Removed invalid connection");
|
||||||
|
connections.RemoveAt(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Connect this <see cref="NodePort"/> to another </summary>
|
/// <summary> Connect this <see cref="NodePort"/> to another </summary>
|
||||||
@ -86,22 +103,22 @@ public class NodePort {
|
|||||||
[Serializable]
|
[Serializable]
|
||||||
public class PortConnection {
|
public class PortConnection {
|
||||||
[SerializeField] public Node node;
|
[SerializeField] public Node node;
|
||||||
[SerializeField] public string portID;
|
[SerializeField] public string fieldName;
|
||||||
public NodePort Port { get { return port != null ? port : port = GetPort(); } }
|
public NodePort Port { get { return port != null ? port : port = GetPort(); } }
|
||||||
[NonSerialized] private NodePort port;
|
[NonSerialized] private NodePort port;
|
||||||
|
|
||||||
public PortConnection(NodePort port) {
|
public PortConnection(NodePort port) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
node = port.node;
|
node = port.node;
|
||||||
portID = port.id;
|
fieldName = port.fieldName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private NodePort GetPort() {
|
private NodePort GetPort() {
|
||||||
for (int i = 0; i < node.OutputCount; i++) {
|
for (int i = 0; i < node.OutputCount; i++) {
|
||||||
if (node.outputs[i].id == portID) return node.outputs[i];
|
if (node.outputs[i].fieldName == fieldName) return node.outputs[i];
|
||||||
}
|
}
|
||||||
for (int i = 0; i < node.InputCount; i++) {
|
for (int i = 0; i < node.InputCount; i++) {
|
||||||
if (node.inputs[i].id == portID) return node.inputs[i];
|
if (node.inputs[i].fieldName == fieldName) return node.inputs[i];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user