From 3487a24680f53aab2ae785c73519f48cddcfccf0 Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Thu, 18 Aug 2022 18:11:18 +0800 Subject: [PATCH] fix:support type conversion for multiport --- Samples/AddNode.cs | 6 ++++ TNodeCore/Runtime/RuntimeNode.cs | 28 ++++++++++++++++--- .../Editor/NodeGraphView/DataGraphView.cs | 1 - .../Editor/NodeViews/NodeView.cs | 2 +- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Samples/AddNode.cs b/Samples/AddNode.cs index bd269dd..50f284d 100644 --- a/Samples/AddNode.cs +++ b/Samples/AddNode.cs @@ -1,4 +1,6 @@  +using System.Collections.Generic; +using System.Runtime.InteropServices; using TNodeCore.Runtime; using TNodeCore.Runtime.Attributes; using TNodeCore.Runtime.Attributes.Ports; @@ -15,6 +17,10 @@ namespace Samples{ [Output] public Vector3 Res{ get; set; } + [Output(Group = true)] public List OutputList => new List{new Vector3(),new Vector3()}; + + + public override void Process(){ Res = A + (Vector3)B; Debug.Log(Res); diff --git a/TNodeCore/Runtime/RuntimeNode.cs b/TNodeCore/Runtime/RuntimeNode.cs index 99da772..2663762 100644 --- a/TNodeCore/Runtime/RuntimeNode.cs +++ b/TNodeCore/Runtime/RuntimeNode.cs @@ -17,18 +17,38 @@ namespace TNodeCore.Runtime{ private readonly Type _type; public Type NodeType => _type; + public Type GetElementTypeOfPort(string portName,bool multi = false){ + var type = _portAccessors[portName].Type; + if (multi == false) + return type; + if (type.IsArray){ + return type.GetElementType(); + } + + if (type.IsGenericType){ + return type.GetGenericArguments()[0]; + } + return type; + } + + public void SetInput(string portName,object value){ var valueType = value.GetType(); + var portPath = portName.Split(':'); if (portPath.Length ==2){ portName = portPath[0]; int index = int.Parse(portPath[1]); - if(_portAccessors[portName].Type.IsArray){ - if (_portAccessors[portName].GetValue(NodeData) is Array array) + var realPortType = GetElementTypeOfPort(portName, true); + if (realPortType != valueType){ + value = RuntimeCache.RuntimeCache.Instance.GetConvertedValue(valueType,realPortType,value); + } + if(realPortType.IsArray){ + if (_portAccessors[portName].GetValue(NodeData) is Array array){ array.SetValue(value, index); + } } - - if (_portAccessors[portName].Type.IsGenericType){ + if (realPortType.IsGenericType){ if (_portAccessors[portName].GetValue(NodeData) is IList list) list[index] = value; } diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index 3fd158f..0155166 100644 --- a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -604,7 +604,6 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ private void SaveEditorModels(){ var placemats = placematContainer.Placemats.ToList(); var comments = this.Query().ToList(); - Debug.Log(placemats.Count); foreach (var placemat in placemats){ if (placemat is PlacematView placematView){ _data.EditorModels.Add(placematView.PlacematModel); diff --git a/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs index 59d623f..d2c5372 100644 --- a/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs +++ b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs @@ -224,7 +224,7 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{ BuildPort(port, attribute, propertyInfo,outputContainer); } else{ - var propertyValue = _data.GetValue(propertyInfo.Name); + var propertyValue = propertyInfo.GetValue(_data); if (propertyValue is IList list){ for (var i = 0; i < list.Count; i++){ var port = new CustomPort(Orientation.Horizontal, Direction.Output,