protected bool IsCompatibleWithConnectedPort(NodeReflectionData node) { if (connectedPort.direction == Direction.Output) { return(node.HasInputOfType(connectedPort.portType)); } return(node.HasOutputOfType(connectedPort.portType)); }
private static NodeReflectionData LoadMethodReflection(MethodInfo method, FuncNodeModuleAttribute moduleAttr) { var attr = method.GetCustomAttribute <FuncNodeAttribute>(); string name = attr?.name ?? ObjectNames.NicifyVariableName(method.Name); string returnPortName = attr?.returnName ?? "Result"; // FuncNode.module can override FuncNodeModule.path. string path = attr?.module ?? moduleAttr.path; // FuncNode.classType can override default class type Type classType = attr?.classType ?? typeof(FuncNode); var node = new NodeReflectionData() { type = classType, path = path?.Split('/'), name = name, tooltip = "TODO!", method = method }; ParameterInfo[] parameters = method.GetParameters(); foreach (var parameter in parameters) { node.ports.Add(new PortReflectionData() { type = parameter.IsOut ? parameter.ParameterType.GetElementType() : parameter.ParameterType, portName = ObjectNames.NicifyVariableName(parameter.Name), fieldName = parameter.Name, isMulti = parameter.IsOut, isInput = !parameter.IsOut }); } // Add an output port for the return value if non-void if (method.ReturnType != typeof(void)) { node.ports.Add(new PortReflectionData() { type = method.ReturnType, portName = returnPortName, fieldName = null, isMulti = true, isInput = false }); } // Merge in any reflection data from the wrapper class itself // Specifically if it contains additional ports to include node.AddPortsFromClass(classType); return(node); }
public void CreateNode(NodeReflectionData data, Vector2 screenPosition, PortView connectedPort = null) { var windowRoot = m_EditorWindow.rootVisualElement; var windowMousePosition = m_EditorWindow.rootVisualElement.ChangeCoordinatesTo( windowRoot.parent, screenPosition - m_EditorWindow.position.position ); var graphMousePosition = contentViewContainer.WorldToLocal(windowMousePosition); var node = data.CreateInstance(); node.graphPosition = graphMousePosition; m_Graph.AddNode(node); // Add a node to the visual graph var editorType = NodeReflection.GetNodeEditorType(data.type); var element = Activator.CreateInstance(editorType) as NodeView; element.Initialize(node, m_EdgeListener); AddElement(element); AssetDatabase.AddObjectToAsset(node, m_Graph); AssetDatabase.SaveAssets(); // If there was a provided existing port to connect to, find the best // candidate port on the new node and connect. if (connectedPort != null) { var edge = new Edge(); if (connectedPort.direction == Direction.Input) { edge.input = connectedPort; edge.output = element.GetCompatibleOutputPort(connectedPort); } else { edge.output = connectedPort; edge.input = element.GetCompatibleInputPort(connectedPort); } ConnectNodes(edge); } Dirty(element); }
private static NodeReflectionData LoadMethodReflection(MethodInfo method, FuncNodeModuleAttribute moduleAttr) { var attr = method.GetCustomAttribute <FuncNodeAttribute>(); string name = attr?.name ?? ObjectNames.NicifyVariableName(method.Name); string returnPortName = attr?.returnName ?? "Result"; // FuncNode.module can override FuncNodeModule.path. string path = attr?.module ?? moduleAttr.path; var node = new NodeReflectionData() { type = typeof(FuncNode), path = path?.Split('/'), name = name, tooltip = "TODO!", method = method }; ParameterInfo[] parameters = method.GetParameters(); foreach (var parameter in parameters) { node.ports.Add(new PortReflectionData() { type = parameter.IsOut ? parameter.ParameterType.GetElementType() : parameter.ParameterType, portName = ObjectNames.NicifyVariableName(parameter.Name), fieldName = parameter.Name, isMulti = parameter.IsOut, isInput = !parameter.IsOut }); } // Add an output port for the return value if non-void if (method.ReturnType != typeof(void)) { node.ports.Add(new PortReflectionData() { type = method.ReturnType, portName = returnPortName, fieldName = null, isMulti = true, isInput = false }); } return(node); }
/// <summary> /// Extract NodeField information from class reflection + attributes /// </summary> /// <param name="type"></param> /// <returns></returns> private static NodeReflectionData LoadClassReflection(Type type, NodeAttribute nodeAttr) { string name = nodeAttr.name ?? ObjectNames.NicifyVariableName(type.Name); string path = nodeAttr.module; var node = new NodeReflectionData() { type = type, path = path?.Split('/'), name = name, tooltip = nodeAttr.help }; node.AddPortsFromClass(type); return(node); }
/// <summary> /// Extract NodeField information from class reflection + attributes /// </summary> /// <param name="type"></param> /// <returns></returns> private static NodeReflectionData LoadClassReflection(Type type, NodeAttribute nodeAttr) { string name = nodeAttr.name ?? type.Name; string path = nodeAttr.module; var node = new NodeReflectionData() { type = type, path = path?.Split('/'), name = name, tooltip = nodeAttr.help }; var fields = new List <FieldInfo>(type.GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance )); // Iterate through inherited private fields as well var temp = type; while ((temp = temp.BaseType) != typeof(AbstractNode)) { fields.AddRange(temp.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)); } // Extract port and editable metadata from each tagged field var ports = new List <PortReflectionData>(); for (int i = 0; i < fields.Count; i++) { object[] attribs = fields[i].GetCustomAttributes(true); for (int j = 0; j < attribs.Length; j++) { if (attribs[j] is InputAttribute) { var attr = attribs[j] as InputAttribute; node.ports.Add(new PortReflectionData() { type = fields[i].FieldType, portName = attr.name ?? ObjectNames.NicifyVariableName(fields[i].Name), fieldName = fields[i].Name, isInput = true, isMulti = attr.multiple, isEditable = attr.editable }); } else if (attribs[j] is OutputAttribute) { var attr = attribs[j] as OutputAttribute; node.ports.Add(new PortReflectionData() { type = fields[i].FieldType, portName = attr.name ?? ObjectNames.NicifyVariableName(fields[i].Name), fieldName = fields[i].Name, isInput = false, isMulti = attr.multiple, isEditable = false }); } else if (attribs[j] is EditableAttribute) { var attr = attribs[j] as EditableAttribute; node.editables.Add(new EditableReflectionData() { type = fields[i].FieldType, fieldName = fields[i].Name }); } } } return(node); }