diff --git a/TNodeCore/DataWrapper.cs b/TNodeCore/DataWrapper.cs index c24931a..30d1410 100644 --- a/TNodeCore/DataWrapper.cs +++ b/TNodeCore/DataWrapper.cs @@ -14,7 +14,7 @@ namespace TNodeCore{ return CreateInstance(); } if(Cache.ContainsKey(data)){ - return Cache[data]; + return Cache[data]==null?CreateInstance():Cache[data]; } var wrapper = CreateInstance(); wrapper.data = data; diff --git a/TNodeCore/Models/BlackboardDragNodeData.cs b/TNodeCore/Models/BlackboardDragNodeData.cs index 883464a..335164c 100644 --- a/TNodeCore/Models/BlackboardDragNodeData.cs +++ b/TNodeCore/Models/BlackboardDragNodeData.cs @@ -7,9 +7,8 @@ using UnityEngine; namespace TNodeCore.Models{ [Serializable] [InternalUsage] - public class BlackboardDragNodeData:RuntimeNodeData{ + public class BlackboardDragNodeData:SceneNodeData{ public string blackDragData; - /// /// it's very hacky way to get blackboard data ,even when the value is null,type info is not null! /// @@ -23,7 +22,8 @@ namespace TNodeCore.Models{ } [Serializable] - public class RuntimeNodeData:NodeData{ + public class SceneNodeData:NodeData{ + public BlackboardData BlackboardData{ get; set; } diff --git a/TNodeCore/Runtime/Interfaces/IPortTypeConversion.cs b/TNodeCore/Runtime/Interfaces/IPortTypeConversion.cs index 29b4277..4633e26 100644 --- a/TNodeCore/Runtime/Interfaces/IPortTypeConversion.cs +++ b/TNodeCore/Runtime/Interfaces/IPortTypeConversion.cs @@ -1,7 +1,7 @@ namespace TNodeCore.Runtime.Interfaces{ - public interface IPortTypeConversion{ - public TTo Convert(TFrom tFrom); + public abstract class PortTypeConversion{ + public abstract TTo Convert(TFrom tFrom); } } \ No newline at end of file diff --git a/TNodeCore/Runtime/RuntimeGraph.cs b/TNodeCore/Runtime/RuntimeGraph.cs index c8df9c3..ddc6415 100644 --- a/TNodeCore/Runtime/RuntimeGraph.cs +++ b/TNodeCore/Runtime/RuntimeGraph.cs @@ -22,12 +22,11 @@ namespace TNodeCore.Runtime{ public readonly Dictionary RuntimeNodes; public void DirectlyTraversal(){ foreach (var node in TopologicalOrder){ - var links = node.OutputLink; + var links = node.InputLink; foreach (var link in links){ - var targetNode = RuntimeNodes[link.inPort.nodeDataId]; HandlingLink(link); - targetNode.NodeData.Process(); } + node.NodeData.Process(); } } public void DependencyTraversal(RuntimeNode runtimeNode){ @@ -47,6 +46,7 @@ namespace TNodeCore.Runtime{ //out node is node output data //in node is node receive data var outValue = outNode.GetOutput(nodeLink.outPort.portEntryName); + inNode.SetInput(nodeLink.inPort.portEntryName, outValue); } public GraphTool(List list, Dictionary graphNodes){ @@ -101,6 +101,10 @@ namespace TNodeCore.Runtime{ Debug.Log("hi"); var nodeList = RuntimeNodes.Values; _graphTool = new GraphTool(nodeList.ToList(),RuntimeNodes); + var sceneNodes = RuntimeNodes.Values.Where(x => x.NodeData is SceneNodeData).Select(x => x.NodeData as SceneNodeData); + foreach (var sceneNode in sceneNodes){ + if (sceneNode != null) sceneNode.BlackboardData = runtimeBlackboardData; + } _build = true; } diff --git a/TNodeCore/Runtime/RuntimeNode.cs b/TNodeCore/Runtime/RuntimeNode.cs index e4b435a..d73fef0 100644 --- a/TNodeCore/Runtime/RuntimeNode.cs +++ b/TNodeCore/Runtime/RuntimeNode.cs @@ -5,6 +5,7 @@ using Codice.Client.Common.TreeGrouper; using TNodeCore.Attribute.Ports; using TNodeCore.Models; using TNodeCore.RuntimeCache; +using UnityEngine; namespace TNodeCore.Runtime{ public class RuntimeNode{ @@ -18,7 +19,22 @@ namespace TNodeCore.Runtime{ public Type NodeType => _type; public void SetInput(string portName,object value){ - _portAccessors[portName].SetValue(this.NodeData,value); + var valueType = value.GetType(); + var portType = _portAccessors[portName].Type; + Debug.Log(valueType); + Debug.Log(portType); + if(portType!=valueType && !portType.IsAssignableFrom(valueType)){ + var res =RuntimeCache.RuntimeCache.Instance.GetConvertedValue(valueType, portType, value); + _portAccessors[portName].SetValue(this.NodeData, res); + } + else{ + + _portAccessors[portName].SetValue(this.NodeData,value); + } + + + + } public object GetOutput(string portName){ diff --git a/TNodeCore/RuntimeCache/IModelPropertyAccessor.cs b/TNodeCore/RuntimeCache/IModelPropertyAccessor.cs index 88a5cae..081caac 100644 --- a/TNodeCore/RuntimeCache/IModelPropertyAccessor.cs +++ b/TNodeCore/RuntimeCache/IModelPropertyAccessor.cs @@ -4,5 +4,9 @@ namespace TNodeCore.RuntimeCache{ public interface IModelPropertyAccessor{ object GetValue(object model); void SetValue(object model, object value); + + public Type Type{ get; set; } + + } } \ No newline at end of file diff --git a/TNodeCore/RuntimeCache/RuntimeCache.cs b/TNodeCore/RuntimeCache/RuntimeCache.cs index edae640..d068018 100644 --- a/TNodeCore/RuntimeCache/RuntimeCache.cs +++ b/TNodeCore/RuntimeCache/RuntimeCache.cs @@ -14,8 +14,10 @@ namespace TNodeCore.RuntimeCache{ public readonly Action Set; public PropAccessor(string propName){ Type t = typeof(T1); + MethodInfo getter = t.GetMethod("get_" + propName); MethodInfo setter = t.GetMethod("set_" + propName); + Type = getter?.ReturnType??setter?.GetParameters()[0].ParameterType; if(getter!=null) Get = (Func)Delegate.CreateDelegate(typeof(Func), null, getter); if(setter!=null) @@ -32,12 +34,14 @@ namespace TNodeCore.RuntimeCache{ public void SetValue(object model, object value){ Set((T1)model,(T2)value); } + + public Type Type{ get; set; } } internal class PortConverterHelper : IPortConverterHelper{ - private readonly IPortTypeConversion _converter; + private readonly PortTypeConversion _converter; public PortConverterHelper(Type type){ - _converter = Activator.CreateInstance(type) as IPortTypeConversion; + _converter = Activator.CreateInstance(type) as PortTypeConversion; } public object Convert(object value){ return _converter.Convert((T1)value); @@ -116,23 +120,31 @@ namespace TNodeCore.RuntimeCache{ CacheRuntimeNodeData(type); } //Check if the type is implementing IPortTypeConversion - if(typeof(IPortTypeConversion<,>).IsAssignableFrom(type)){ + if(type.BaseType is{IsGenericType: true} && type.BaseType.GetGenericTypeDefinition()==typeof(PortTypeConversion<,>)){ //if it is, add it to the cache + Debug.Log("find conversion"); CacheRuntimePortTypeConversion(type); } + else{ + Debug.Log(type); + } } private void CacheRuntimePortTypeConversion(Type type){ - if (type.IsGenericType == false){ - return; + if (type.BaseType != null){ + var genericType = type.BaseType.GetGenericTypeDefinition(); + if (genericType != typeof(PortTypeConversion<,>)){ + return; + } } - var genericType = type.GetGenericTypeDefinition(); - if (genericType != typeof(IPortTypeConversion<,>)){ + else{ return; } - var type1 = type.GetGenericArguments()[0]; - var type2 = type.GetGenericArguments()[1]; + var type1 = type.BaseType.GetGenericArguments()[0]; + var type2 = type.BaseType.GetGenericArguments()[1]; + Debug.Log(type1); + Debug.Log(type2); var specificType = typeof(PortConverterHelper<,>).MakeGenericType(type1, type2); var instance = Activator.CreateInstance(specificType, type) as IPortConverterHelper; if (instance == null){ @@ -256,8 +268,9 @@ namespace TNodeCore.RuntimeCache{ return (T) method.Invoke(data); } public static object GetValue(this IModel data, string path,Type type=null){ - var method = RuntimeCache.Instance.CachedDelegatesForGettingValue[type??data.GetType()][path]; - return method.Invoke(data); + var dic = RuntimeCache.Instance.CachedDelegatesForGettingValue[type ?? data.GetType()]; + var method = dic.ContainsKey(path) ? dic[path] : null; + return method?.Invoke(data); } public static void SetValue(this IModel data,string path,T value,Type type=null){ diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs index e37882f..51ed7d9 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs @@ -1,4 +1,5 @@ -using UnityEditor.Experimental.GraphView; +using UnityEditor; +using UnityEditor.Experimental.GraphView; namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ public class BlackboardField:UnityEditor.Experimental.GraphView.BlackboardField{ @@ -6,7 +7,5 @@ namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ public BlackboardField(BlackboardProperty.BlackboardProperty blackboardProperty):base(null,blackboardProperty.PropertyName,null){ BlackboardProperty = blackboardProperty; } - - } } \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs index 2f1a20b..5d83a5c 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs @@ -30,7 +30,7 @@ namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ //skip if the field is a list or Ilist if (!typeof(IList).IsAssignableFrom(field.FieldType)){ VisualElement visualElement = new VisualElement(); - var propertyField = new BlackboardField(new BlackboardProperty.BlackboardProperty(ObjectNames.NicifyVariableName(field.Name),field.FieldType)); + var propertyField = new BlackboardField(new BlackboardProperty.BlackboardProperty(field.Name,field.FieldType)); var foldoutData = new Foldout{ text = ObjectNames.NicifyVariableName(field.Name) }; diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index 0fd9fb7..9836559 100644 --- a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -155,8 +155,9 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ private void UpdateRuntimeGraphBehaviour(){ if(_runtimeGraph != null){ if (_runtimeGraphUpdate){ - _runtimeGraph.ResolveDependency(); _runtimeGraphUpdate = false; + _runtimeGraph.ResolveDependency(); + AfterRuntimeGraphUpdate?.Invoke(); } @@ -283,7 +284,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ //Get the node type var nodeType = dataNode.GetType(); //Get the derived type of NodeAttribute View from the node type - if (dataNode is RuntimeNodeData runtimeNodeData){ + if (dataNode is SceneNodeData runtimeNodeData){ runtimeNodeData.BlackboardData = GetBlackboardData(); } var nodePos = Owner.graphEditorData.graphElementsData. @@ -445,7 +446,6 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ if (supportedTypes != null){ compatiblePorts.AddRange(ports.Where(x => supportedTypes.Contains(x.portType)).ToList()); } - return compatiblePorts; diff --git a/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs index 298df16..8fac55f 100644 --- a/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs +++ b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs @@ -20,7 +20,7 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{ public IBaseDataGraphView BaseDataGraphView{ get{ - var visualElement = this.GetFirstAncestorOfType() as IBaseDataGraphView; + var visualElement = this.GetFirstAncestorOfType(); return visualElement; } }