1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

!T(Port) remove baseType

This commit is contained in:
Icarus 2019-12-21 20:47:59 +08:00
parent 7624d4ad1f
commit 10636cd243
2 changed files with 10 additions and 46 deletions

View File

@ -314,8 +314,8 @@ namespace XNodeEditor {
} }
[Obsolete("Use DynamicPortList instead")] [Obsolete("Use DynamicPortList instead")]
public static void InstancePortList(string fieldName, Type type, SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType = XNode.Node.ConnectionType.Multiple, XNode.Node.TypeConstraint typeConstraint = XNode.Node.TypeConstraint.None,Type inputTypeConstraintBaseType = null, Action<ReorderableList> onCreation = null) { public static void InstancePortList(string fieldName, Type type, SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType = XNode.Node.ConnectionType.Multiple, XNode.Node.TypeConstraint typeConstraint = XNode.Node.TypeConstraint.None, Action<ReorderableList> onCreation = null) {
DynamicPortList(fieldName, type, serializedObject, io, connectionType, typeConstraint, inputTypeConstraintBaseType,onCreation); DynamicPortList(fieldName, type, serializedObject, io, connectionType, typeConstraint, onCreation);
} }
#endregion #endregion
@ -331,16 +331,16 @@ namespace XNodeEditor {
return false; return false;
} }
/// <summary> Draw an editable list of dynamic ports. Port names are named as "[fieldName] [index]" </summary> /// <summary> Draw an editable list of dynamic ports.</summary>
/// <param name="fieldName">Supply a list for editable values</param> /// <param name="fieldName">Supply a list for editable values</param>
/// <param name="type">Value type of added dynamic ports</param> /// <param name="type">Value type of added dynamic ports</param>
/// <param name="serializedObject">The serializedObject of the node</param> /// <param name="serializedObject">The serializedObject of the node</param>
/// <param name="connectionType">Connection type of added dynamic ports</param> /// <param name="connectionType">Connection type of added dynamic ports</param>
/// <param name="inputTypeConstraintBaseType">当<see cref="io"/>为<see cref="IO.Input"/>并且<see cref="typeConstraint"/>为<see cref="TypeConstraint.Inherited"/>时可用</param>
/// <param name="onCreation">Called on the list on creation. Use this if you want to customize the created ReorderableList</param> /// <param name="onCreation">Called on the list on creation. Use this if you want to customize the created ReorderableList</param>
/// <param name="onAdd">Return port name after adding port</param>
public static void DynamicPortList(string fieldName, Type type, SerializedObject serializedObject, XNode.NodePort.IO io, public static void DynamicPortList(string fieldName, Type type, SerializedObject serializedObject, XNode.NodePort.IO io,
XNode.Node.ConnectionType connectionType = XNode.Node.ConnectionType.Multiple, XNode.Node.ConnectionType connectionType = XNode.Node.ConnectionType.Multiple,
XNode.Node.TypeConstraint typeConstraint = XNode.Node.TypeConstraint.None,Type inputTypeConstraintBaseType = null, XNode.Node.TypeConstraint typeConstraint = XNode.Node.TypeConstraint.None,
Action<ReorderableList> onCreation = null,Action<string> onAdd = null) { Action<ReorderableList> onCreation = null,Action<string> onAdd = null) {
XNode.Node node = serializedObject.targetObject as XNode.Node; XNode.Node node = serializedObject.targetObject as XNode.Node;
@ -358,7 +358,7 @@ namespace XNodeEditor {
if (list == null) { if (list == null) {
SerializedProperty arrayData = serializedObject.FindProperty(fieldName); SerializedProperty arrayData = serializedObject.FindProperty(fieldName);
list = CreateReorderableList(fieldName, dynamicPorts, arrayData, type, serializedObject, list = CreateReorderableList(fieldName, dynamicPorts, arrayData, type, serializedObject,
io, connectionType, typeConstraint, inputTypeConstraintBaseType,onAdd); io, connectionType, typeConstraint, onAdd);
onCreation?.Invoke(list); onCreation?.Invoke(list);
if (reorderableListCache.TryGetValue(serializedObject.targetObject, out rlc)) rlc.Add(fieldName, list); if (reorderableListCache.TryGetValue(serializedObject.targetObject, out rlc)) rlc.Add(fieldName, list);
else reorderableListCache.Add(serializedObject.targetObject, new Dictionary<string, ReorderableList>() { { fieldName, list } }); else reorderableListCache.Add(serializedObject.targetObject, new Dictionary<string, ReorderableList>() { { fieldName, list } });
@ -368,8 +368,7 @@ namespace XNodeEditor {
} }
private static ReorderableList CreateReorderableList(string fieldName, List<XNode.NodePort> dynamicPorts, SerializedProperty arrayData, Type type, private static ReorderableList CreateReorderableList(string fieldName, List<XNode.NodePort> dynamicPorts, SerializedProperty arrayData, Type type,
SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType, XNode.Node.TypeConstraint typeConstraint, SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType, XNode.Node.TypeConstraint typeConstraint,Action<string> onAdd) {
Type inputTypeConstraintBaseType,Action<string> onAdd) {
bool hasArrayData = arrayData != null && arrayData.isArray; bool hasArrayData = arrayData != null && arrayData.isArray;
XNode.Node node = serializedObject.targetObject as XNode.Node; XNode.Node node = serializedObject.targetObject as XNode.Node;
ReorderableList list = new ReorderableList(dynamicPorts, null, true, true, true, true); ReorderableList list = new ReorderableList(dynamicPorts, null, true, true, true, true);
@ -465,7 +464,7 @@ namespace XNodeEditor {
while (node.HasPort(newName)) newName = fieldName + " " + (++i); while (node.HasPort(newName)) newName = fieldName + " " + (++i);
if (io == XNode.NodePort.IO.Output) node.AddDynamicOutput(type, connectionType, XNode.Node.TypeConstraint.None, newName); if (io == XNode.NodePort.IO.Output) node.AddDynamicOutput(type, connectionType, XNode.Node.TypeConstraint.None, newName);
else node.AddDynamicInput(type, connectionType, typeConstraint,inputTypeConstraintBaseType, newName); else node.AddDynamicInput(type, connectionType, typeConstraint, newName);
serializedObject.Update(); serializedObject.Update();
EditorUtility.SetDirty(node); EditorUtility.SetDirty(node);
if (hasArrayData) { if (hasArrayData) {
@ -495,7 +494,7 @@ namespace XNodeEditor {
int i = 0; int i = 0;
while (node.HasPort(newName)) newName = arrayData.name + " " + (++i); while (node.HasPort(newName)) newName = arrayData.name + " " + (++i);
if (io == XNode.NodePort.IO.Output) node.AddDynamicOutput(type, connectionType, typeConstraint,newName); if (io == XNode.NodePort.IO.Output) node.AddDynamicOutput(type, connectionType, typeConstraint,newName);
else node.AddDynamicInput(type, connectionType, typeConstraint, inputTypeConstraintBaseType,newName); else node.AddDynamicInput(type, connectionType, typeConstraint, newName);
EditorUtility.SetDirty(node); EditorUtility.SetDirty(node);
dynamicPortCount++; dynamicPortCount++;
} }

View File

@ -51,30 +51,13 @@ namespace XNode {
} }
} }
public Type TypeConstraintBaseType {
get {
if (_typeConstraintBaseType == null && !string.IsNullOrEmpty(_typeConstraintBaseTypeQualifiedName))
_typeConstraintBaseType = Type.GetType(_typeConstraintBaseTypeQualifiedName, false);
return _typeConstraintBaseType;
}
set {
_typeConstraintBaseType = value;
if (value != null)
{
_typeConstraintBaseTypeQualifiedName = value.AssemblyQualifiedName;
}
}
}
private Type valueType; private Type valueType;
private Type _typeConstraintBaseType;
#if UNITY_EDITOR #if UNITY_EDITOR
public const string FieldNameEditor = nameof(_fieldName); public const string FieldNameEditor = nameof(_fieldName);
#endif #endif
[SerializeField] private string _fieldName; [SerializeField] private string _fieldName;
[SerializeField] private Node _node; [SerializeField] private Node _node;
[SerializeField] private string _typeQualifiedName; [SerializeField] private string _typeQualifiedName;
[SerializeField] private string _typeConstraintBaseTypeQualifiedName;
[SerializeField] private List<PortConnection> connections = new List<PortConnection>(); [SerializeField] private List<PortConnection> connections = new List<PortConnection>();
[SerializeField] private IO _direction; [SerializeField] private IO _direction;
[SerializeField] private Node.ConnectionType _connectionType; [SerializeField] private Node.ConnectionType _connectionType;
@ -92,7 +75,6 @@ namespace XNode {
_direction = IO.Input; _direction = IO.Input;
_connectionType = (attribs[i] as Node.InputAttribute).connectionType; _connectionType = (attribs[i] as Node.InputAttribute).connectionType;
_typeConstraint = (attribs[i] as Node.InputAttribute).typeConstraint; _typeConstraint = (attribs[i] as Node.InputAttribute).typeConstraint;
TypeConstraintBaseType = (attribs[i] as Node.InputAttribute).BaseType;
} else if (attribs[i] is Node.OutputAttribute) { } else if (attribs[i] is Node.OutputAttribute) {
_direction = IO.Output; _direction = IO.Output;
_connectionType = (attribs[i] as Node.OutputAttribute).connectionType; _connectionType = (attribs[i] as Node.OutputAttribute).connectionType;
@ -105,7 +87,6 @@ namespace XNode {
public NodePort(NodePort nodePort, Node node) { public NodePort(NodePort nodePort, Node node) {
_fieldName = nodePort._fieldName; _fieldName = nodePort._fieldName;
ValueType = nodePort.valueType; ValueType = nodePort.valueType;
TypeConstraintBaseType = nodePort.TypeConstraintBaseType;
_direction = nodePort.direction; _direction = nodePort.direction;
_dynamic = nodePort._dynamic; _dynamic = nodePort._dynamic;
_connectionType = nodePort._connectionType; _connectionType = nodePort._connectionType;
@ -114,10 +95,9 @@ namespace XNode {
} }
/// <summary> Construct a dynamic port. Dynamic ports are not forgotten on reimport, and is ideal for runtime-created ports. </summary> /// <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,Type baseType, Node node) { public NodePort(string fieldName, Type type, IO direction, Node.ConnectionType connectionType, Node.TypeConstraint typeConstraint, Node node) {
_fieldName = fieldName; _fieldName = fieldName;
this.ValueType = type; this.ValueType = type;
TypeConstraintBaseType = baseType;
_direction = direction; _direction = direction;
_node = node; _node = node;
_dynamic = true; _dynamic = true;
@ -301,21 +281,6 @@ namespace XNode {
else output = port; else output = port;
// If there isn't one of each, they can't connect // If there isn't one of each, they can't connect
if (input == null || output == null) return false; if (input == null || output == null) return false;
// Check input type constraints
if (input.typeConstraint == XNode.Node.TypeConstraint.Inherited)
{
//无法分配,失败
if (!input.ValueType.IsAssignableFrom(output.ValueType))
{
return false;
}
//如果存在指定基类,同时无法分配,失败
if (input.TypeConstraintBaseType != null && !input.TypeConstraintBaseType.IsAssignableFrom(output.ValueType))
{
return false;
}
}
if (input.typeConstraint == XNode.Node.TypeConstraint.Strict && input.ValueType != output.ValueType) return false; if (input.typeConstraint == XNode.Node.TypeConstraint.Strict && input.ValueType != output.ValueType) return false;
if (input.typeConstraint == XNode.Node.TypeConstraint.InheritedInverse && !output.ValueType.IsAssignableFrom(input.ValueType)) return false; if (input.typeConstraint == XNode.Node.TypeConstraint.InheritedInverse && !output.ValueType.IsAssignableFrom(input.ValueType)) return false;
// Check output type constraints // Check output type constraints