/// <summary> /// Construct a static targetless nodeport. Used as a template. /// </summary> public NodePort(FieldInfo fieldInfo) { _fieldName = fieldInfo.Name; ValueType = fieldInfo.FieldType; _dynamic = false; var attribs = fieldInfo.GetCustomAttributes(false); for (int i = 0; i < attribs.Length; i++) { if (attribs[i] is Node.InputAttribute inputAttribute) { _direction = IO.Input; _connectionType = inputAttribute.connectionType; _typeConstraint = inputAttribute.typeConstraint; _allowedTypes = new ClassTypeReference[inputAttribute.allowedTypes.Length]; for (int j = 0; j < inputAttribute.allowedTypes.Length; j++) { _allowedTypes[j] = inputAttribute.allowedTypes[j]; } } else if (attribs[i] is Node.OutputAttribute outputAttribute) { _direction = IO.Output; _connectionType = outputAttribute.connectionType; _typeConstraint = outputAttribute.typeConstraint; _allowedTypes = new ClassTypeReference[outputAttribute.allowedTypes.Length]; for (int j = 0; j < outputAttribute.allowedTypes.Length; j++) { _allowedTypes[j] = outputAttribute.allowedTypes[j]; } } } }
public NTInputAttribute(Node.ShowBackingValue backingValue = Node.ShowBackingValue.Unconnected, Node.ConnectionType connectionType = Node.ConnectionType.Override, Node.TypeConstraint typeConstraint = Node.TypeConstraint.Strict, bool instancePortList = false) : base(backingValue, connectionType, typeConstraint, instancePortList) { }
public NTInputSelectAttribute(Node.ShowBackingValue backingValue = Node.ShowBackingValue.Always, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = Node.TypeConstraint.Strict, bool instancePortList = true) : base(backingValue, connectionType, typeConstraint, instancePortList) { }
/// <summary> Copy a nodePort but assign it to another node. </summary> public NodePort(NodePort nodePort, Node node) { _fieldName = nodePort._fieldName; ValueType = nodePort.valueType; _direction = nodePort.direction; _dynamic = nodePort._dynamic; _connectionType = nodePort._connectionType; _typeConstraint = nodePort._typeConstraint; _node = node; }
/// <summary> Construct a dynamic port. Dynamic ports are not forgotten on reimport, and is ideal for runtime-created ports. </summary> public NodePort(string fieldName, Type type, IO direction, Node.ConnectionType connectionType, Node.TypeConstraint typeConstraint, Node node) { _fieldName = fieldName; this.ValueType = type; _direction = direction; _node = node; _dynamic = true; _connectionType = connectionType; _typeConstraint = typeConstraint; }
/// <summary> Construct a static targetless nodeport. Used as a template. </summary> public NodePort(FieldInfo fieldInfo) { _fieldName = fieldInfo.Name; ValueType = fieldInfo.FieldType; _dynamic = false; object[] attribs = fieldInfo.GetCustomAttributes(false); for (int i = 0; i < attribs.Length; i++) { if (attribs[i] is Node.InputAttribute) { _direction = IO.Input; _connectionType = (attribs[i] as Node.InputAttribute).connectionType; _typeConstraint = (attribs[i] as Node.InputAttribute).typeConstraint; } else if (attribs[i] is Node.OutputAttribute) { _direction = IO.Output; _connectionType = (attribs[i] as Node.OutputAttribute).connectionType; } } }
public NodePort AddInstanceInput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null) { return(AddDynamicInput(type, connectionType, typeConstraint, fieldName)); }
/// <summary> Add a dynamic, serialized port to this node. </summary> /// <seealso cref="AddDynamicInput"/> /// <seealso cref="AddDynamicOutput"/> private NodePort AddDynamicPort(Type type, NodePort.IO direction, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null) { if (fieldName == null) { fieldName = "dynamicInput_0"; int i = 0; while (HasPort(fieldName)) { fieldName = "dynamicInput_" + (++i); } } else if (HasPort(fieldName)) { Debug.LogWarning("Port '" + fieldName + "' already exists in " + name, this); return(ports[fieldName]); } NodePort port = new NodePort(fieldName, type, direction, connectionType, typeConstraint, this); ports.Add(fieldName, port); return(port); }
private NodePort AddInstancePort(Type type, NodePort.IO direction, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null) { return(AddDynamicPort(type, direction, connectionType, typeConstraint, fieldName)); }
public override NodeGraphData ImportData(string serializedData) { NodeGraphData returnData = null; JSONNode root = JSON.Parse(serializedData); Dictionary <int, object> references = new Dictionary <int, object>(); Dictionary <NodePort, NodePortData> portDatas = new Dictionary <NodePort, NodePortData>(); List <string> ignoredFields = new List <string> { "name", "graph", "ports", "nodes" }; JSONObject graphJObject; if (root.HasKey("graph")) { graphJObject = root["graph"].AsObject; int id = graphJObject["id"]; string graphTypeS = graphJObject["type"]; Type graphType = Type.GetType(graphTypeS); NodeGraph graph = (NodeGraph)Activator.CreateInstance(graphType); graph.name = graphJObject["name"]; references.Add(id, graph); returnData = new NodeGraphData(graph); //Debug.Log("Basic graph OK!"); } else { Debug.LogWarning("Basic graph KO!"); return(returnData); } if (root.HasKey("nodes")) { JSONArray nodesJArray = root["nodes"].AsArray; foreach (JSONObject nodeJObject in nodesJArray.Values) { int id = nodeJObject["id"]; string nodeTypeS = nodeJObject["type"]; Type nodeType = Type.GetType(nodeTypeS); Node node = (Node)Activator.CreateInstance(nodeType); node.name = nodeJObject["name"]; object nodeOBJ = (object)node; SimpleJSONExtension.FromJSON(ref nodeOBJ, nodeType, nodeJObject, ignoredFields, references); node.graph = returnData.graph; NodeData nodeData = new NodeData(node); JSONArray nodePortsArray = nodeJObject["ports"].AsArray; foreach (var nodePort in nodePortsArray.Values) { bool dynamic = nodePort["dynamic"].AsBool; string portName = nodePort["name"]; NodePort port = null; int portId = 0; if (dynamic) { if (!node.HasPort(portName)) { Type dynamicType = Type.GetType(nodePort["valueType"]); Node.TypeConstraint constraint = (Node.TypeConstraint)nodePort["typeConstraint"].AsInt; Node.ConnectionType connectionType = (Node.ConnectionType)nodePort["connectionType"].AsInt; NodePort.IO direction = (NodePort.IO)nodePort["direction"].AsInt; if (direction == NodePort.IO.Input) { port = node.AddInstanceInput(dynamicType, connectionType, constraint, portName); } else { port = node.AddInstanceOutput(dynamicType, connectionType, constraint, portName); } } else { Debug.LogWarning("Ignoring port bc is already created"); } } port = node.GetPort(nodePort["name"]); if (port == null) { Debug.Log("Port is null? " + node.name); foreach (var p in node.Ports) { Debug.Log(p.fieldName); } } portId = nodePort["id"]; NodePortData portData = new NodePortData(nodeData, port); nodeData.ports.Add(portData); portDatas.Add(port, portData); references.Add(portId, port); } references.Add(id, node); returnData.nodes.Add(nodeData); } //Debug.Log("Basic Nodes OK!"); } else { Debug.LogWarning("Basic Nodes KO!"); return(returnData); } if (root.HasKey("connections")) { JSONArray connectionsJArray = root["connections"].AsArray; foreach (JSONObject connectionJObject in connectionsJArray.Values) { int port1ID = connectionJObject["port1ID"].AsInt; int port2ID = connectionJObject["port2ID"].AsInt; if (references.ContainsKey(port1ID) && references.ContainsKey(port2ID)) { NodePort p1 = (NodePort)references[port1ID]; NodePort p2 = (NodePort)references[port2ID]; p1.Connect(p2); } else { Debug.LogWarning("Error recovering one connection"); } } //Debug.Log("Connections OK!"); } else { Debug.LogWarning("Connections KO!"); return(returnData); } object graphObject = returnData.graph; SimpleJSONExtension.FromJSON(ref graphObject, returnData.graph.GetType(), graphJObject, ignoredFields, references); return(returnData); }
/// <summary> /// Convenience function. /// </summary> /// <seealso cref="AddInstancePort"/> /// <seealso cref="AddInstanceInput"/> public NodePort AddDynamicOutput(Type type, Node.ConnectionType connectionType = Node.ConnectionType.Multiple, Node.TypeConstraint typeConstraint = TypeConstraint.None, string fieldName = null) => AddDynamicPort(type, NodePort.IO.Output, connectionType, typeConstraint, fieldName);