1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 17:26:02 +08:00

Added new methods at NodeEditorUtilities

_ HasCompatiblePortType:
Looking for ports with value Type compatible with a given type.

- GetCompatibleNodesTypes:
Filter only node types that contains some port value type compatible with an given type
This commit is contained in:
juliocp 2020-10-07 19:00:56 -03:00
parent 72c7ca9d04
commit 1b32e2ec03

View File

@ -127,6 +127,64 @@ namespace XNodeEditor {
return methods.Count() > 0;
}
/// <summary>
/// Looking for ports with value Type compatible with a given type.
/// </summary>
/// <param name="nodeType">Node to search</param>
/// <param name="compatibleType">Type to find compatiblities</param>
/// <param name="direction"></param>
/// <returns>True if NodeType has some port with value type compatible</returns>
public static bool HasCompatiblePortType(Type nodeType, Type compatibleType, XNode.NodePort.IO direction = XNode.NodePort.IO.Input)
{
Type findType = typeof(XNode.Node.InputAttribute);
if (direction == XNode.NodePort.IO.Output)
findType = typeof(XNode.Node.OutputAttribute);
//Get All fields from node type and we go filter only field with portAttribute.
//This way is possible to know the values of the all ports and if have some with compatible value tue
foreach (FieldInfo f in XNode.NodeDataCache.GetNodeFields(nodeType))
{
var portAttribute = f.GetCustomAttributes(findType, false).FirstOrDefault();
if (portAttribute != null)
{
if (IsCastableTo(f.FieldType, compatibleType))
{
return true;
}
}
}
return false;
}
/// <summary>
/// Filter only node types that contains some port value type compatible with an given type
/// </summary>
/// <param name="nodeTypes">List with all nodes type to filter</param>
/// <param name="compatibleType">Compatible Type to Filter</param>
/// <returns>Return Only Node Types with ports compatible, or an empty list</returns>
public static List<Type> GetCompatibleNodesTypes(Type[] nodeTypes, Type compatibleType, XNode.NodePort.IO direction = XNode.NodePort.IO.Input)
{
//Result List
List<Type> filteredTypes = new List<Type>();
//Return empty list
if (nodeTypes == null) { return filteredTypes; }
if (compatibleType == null) { return filteredTypes; }
//Find compatiblity
foreach (Type findType in nodeTypes)
{
if (HasCompatiblePortType(findType, compatibleType, direction))
{
filteredTypes.Add(findType);
}
}
return filteredTypes;
}
/// <summary> Return a prettiefied type name. </summary>
public static string PrettyName(this Type type) {
if (type == null) return "null";