From 4f932e9fa5f19b9d75164fb736105a5b96895253 Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Mon, 8 Jul 2019 20:58:31 +0200 Subject: [PATCH] Added type constraints to output port --- Scripts/Node.cs | 5 ++++- Scripts/NodePort.cs | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Scripts/Node.cs b/Scripts/Node.cs index cd86b95..fe898ba 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -290,15 +290,18 @@ namespace XNode { [Obsolete("Use dynamicPortList instead")] public bool instancePortList { get { return dynamicPortList; } set { dynamicPortList = value; } } public bool dynamicPortList; + public TypeConstraint typeConstraint; /// Mark a serializable field as an output port. You can access this through /// Should we display the backing value for this port as an editor field? /// Should we allow multiple connections? + /// Constrains which input connections can be made from this port /// If true, will display a reorderable list of outputs instead of a single port. Will automatically add and display values for lists and arrays - public OutputAttribute(ShowBackingValue backingValue = ShowBackingValue.Never, ConnectionType connectionType = ConnectionType.Multiple, bool dynamicPortList = false) { + public OutputAttribute(ShowBackingValue backingValue = ShowBackingValue.Never, ConnectionType connectionType = ConnectionType.Multiple, TypeConstraint typeConstraint = TypeConstraint.None, bool dynamicPortList = false) { this.backingValue = backingValue; this.connectionType = connectionType; this.dynamicPortList = dynamicPortList; + this.typeConstraint = typeConstraint; } } diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 24e4941..1000b23 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -67,6 +67,7 @@ namespace XNode { } else if (attribs[i] is Node.OutputAttribute) { _direction = IO.Output; _connectionType = (attribs[i] as Node.OutputAttribute).connectionType; + _typeConstraint = (attribs[i] as Node.OutputAttribute).typeConstraint; } } } @@ -255,9 +256,12 @@ namespace XNode { else output = port; // If there isn't one of each, they can't connect if (input == null || output == null) return false; - // Check type constraints + // Check input type constraints if (input.typeConstraint == XNode.Node.TypeConstraint.Inherited && !input.ValueType.IsAssignableFrom(output.ValueType)) return false; if (input.typeConstraint == XNode.Node.TypeConstraint.Strict && input.ValueType != output.ValueType) return false; + // Check output type constraints + if (output.typeConstraint == XNode.Node.TypeConstraint.Inherited && !output.ValueType.IsAssignableFrom(input.ValueType)) return false; + if (output.typeConstraint == XNode.Node.TypeConstraint.Strict && output.ValueType != input.ValueType) return false; // Success return true; }