Skip to content

Commit

Permalink
Added new methods at NodeEditorUtilities
Browse files Browse the repository at this point in the history
_ 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
  • Loading branch information
neoneper committed Oct 7, 2020
1 parent 72c7ca9 commit 1b32e2e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Scripts/Editor/NodeEditorUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,64 @@ public static bool IsCastableTo(this Type from, Type to) {
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";
Expand Down

0 comments on commit 1b32e2e

Please sign in to comment.