fix:add some test for TNode

main
taoria 3 years ago
parent 31bb16bc46
commit 985f5e5638
  1. 139
      Samples/New HelloGraph.asset
  2. 3
      TNodeCore/Runtime/Models/PortInfo.cs
  3. 42
      TNodeCore/Runtime/RuntimeNode.cs
  4. 0
      TNodeCore/Tests.meta
  5. 3
      TNodeCore/Tests/Editor.meta
  6. 3
      TNodeCore/Tests/Runtime.meta
  7. 2
      TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs
  8. 13
      TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs
  9. 23
      TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs
  10. 8
      TNodeGraphViewImpl/Tests.meta

@ -12,142 +12,15 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2051a0adbd1ba974084a535dd06ab7d7, type: 3} m_Script: {fileID: 11500000, guid: 2051a0adbd1ba974084a535dd06ab7d7, type: 3}
m_Name: New HelloGraph m_Name: New HelloGraph
m_EditorClassIdentifier: m_EditorClassIdentifier:
nodeList: nodeList: []
- id: 0 nodeLinks: []
- id: 1
- id: 2
- id: 3
nodeLinks:
- inPort:
portEntryName: A
nodeDataId: f33d73da-40c5-4b3a-ace7-40105e3489b6
outPort:
portEntryName: Value
nodeDataId: ae519ad8-4bc8-46c5-a98a-dc95bc85b58a
- inPort:
portEntryName: A
nodeDataId: f33d73da-40c5-4b3a-ace7-40105e3489b6
outPort:
portEntryName: Value
nodeDataId: 23526615-f15c-4bdf-96c2-38a57d30b10d
- inPort:
portEntryName: A
nodeDataId: f33d73da-40c5-4b3a-ace7-40105e3489b6
outPort:
portEntryName: Value
nodeDataId: 06f5acb9-58d5-41b3-b7f5-bd90f7f9ab94
blackboardData: blackboardData:
id: 4 id: 0
sceneReference: sceneReference:
editorModels: editorModels: []
- id: 5
graphViewModel: graphViewModel:
id: 6 id: 0
references: references:
version: 1 version: 1
00000000: 00000000:
type: {class: BlackboardDragNode, ns: TNodeCore.Runtime.Models, asm: TNodeCore} type: {class: , ns: , asm: }
data:
positionInView:
serializedVersion: 2
x: -345
y: -42
width: 0
height: 0
id: 23526615-f15c-4bdf-96c2-38a57d30b10d
nodeName:
entryPoint: 0
isTest: 0
blackboardDragTypeString: UnityEngine.GameObject, UnityEngine.CoreModule,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
blackDragData: HelloGameObject
isListElement: 0
00000001:
type: {class: BlackboardDragNode, ns: TNodeCore.Runtime.Models, asm: TNodeCore}
data:
positionInView:
serializedVersion: 2
x: -345
y: -84
width: 0
height: 0
id: ae519ad8-4bc8-46c5-a98a-dc95bc85b58a
nodeName:
entryPoint: 0
isTest: 0
blackboardDragTypeString: UnityEngine.GameObject, UnityEngine.CoreModule,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
blackDragData: HelloGameObject
isListElement: 0
00000002:
type: {class: BlackboardDragNode, ns: TNodeCore.Runtime.Models, asm: TNodeCore}
data:
positionInView:
serializedVersion: 2
x: -345
y: -126
width: 0
height: 0
id: 06f5acb9-58d5-41b3-b7f5-bd90f7f9ab94
nodeName:
entryPoint: 0
isTest: 0
blackboardDragTypeString: UnityEngine.GameObject, UnityEngine.CoreModule,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
blackDragData: HelloGameObject
isListElement: 0
00000003:
type: {class: AddNode, ns: Samples, asm: Assembly-CSharp}
data:
positionInView:
serializedVersion: 2
x: -17
y: -18
width: 0
height: 0
id: f33d73da-40c5-4b3a-ace7-40105e3489b6
nodeName: AddNode
entryPoint: 0
isTest: 0
00000004:
type: {class: HelloBlackboard, ns: TNode.Samples, asm: Assembly-CSharp}
data:
positionInView:
serializedVersion: 2
x: 0
y: 0
width: 0
height: 0
id:
HelloString:
HelloGameObject: {fileID: 0}
V3S:
- {x: 0, y: 0, z: 0}
V2S:
- {x: 0, y: 0}
00000005:
type: {class: PlacematModel, ns: TNode.TNodeCore.Editor.Models, asm: TNodeCore}
data:
positionInView:
serializedVersion: 2
x: 465
y: -21
width: 500
height: 500
id:
hostModels: []
zOrder: 0
title: Title
00000006:
type: {class: GraphViewModel, ns: TNode.TNodeCore.Editor.Models, asm: TNodeCore}
data:
positionInView:
serializedVersion: 2
x: 0
y: 0
width: 0
height: 0
id:
persistScale: 1
persistOffset: {x: 519, y: 347}
isBlackboardOn: 1

@ -3,6 +3,9 @@
namespace TNodeCore.Runtime.Models{ namespace TNodeCore.Runtime.Models{
[Serializable] [Serializable]
public class PortInfo{ public class PortInfo{
/// <summary>
/// Port entry name is port's name ,not the portName of the port
/// </summary>
public string portEntryName; public string portEntryName;
public string nodeDataId; public string nodeDataId;
} }

@ -19,16 +19,48 @@ namespace TNodeCore.Runtime{
public void SetInput(string portName,object value){ public void SetInput(string portName,object value){
var valueType = value.GetType(); var valueType = value.GetType();
var portType = _portAccessors[portName].Type; var portPath = portName.Split(':');
if(portType!=valueType && !portType.IsAssignableFrom(valueType)){ if (portPath.Length ==2){
var res =RuntimeCache.RuntimeCache.Instance.GetConvertedValue(valueType, portType, value); portName = portPath[0];
_portAccessors[portName].SetValue(NodeData, res); int index = int.Parse(portPath[1]);
if(_portAccessors[portName].Type.IsArray){
if (_portAccessors[portName].GetValue(NodeData) is Array array)
array.SetValue(value, index);
}
if (_portAccessors[portName].Type.IsGenericType){
if (_portAccessors[portName].GetValue(NodeData) is IList list)
list[index] = value;
}
} }
else{ else{
_portAccessors[portName].SetValue(NodeData,value); var portType = _portAccessors[portName].Type;
if(portType!=valueType && !portType.IsAssignableFrom(valueType)){
var res =RuntimeCache.RuntimeCache.Instance.GetConvertedValue(valueType, portType, value);
_portAccessors[portName].SetValue(NodeData, res);
}
else{
_portAccessors[portName].SetValue(NodeData,value);
}
} }
} }
public object GetOutput(string portName){ public object GetOutput(string portName){
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)
return array.GetValue(index);
}
if (_portAccessors[portName].Type.IsGenericType){
if (_portAccessors[portName].GetValue(NodeData) is IList list)
return list[index];
}
}
return _portAccessors[portName].GetValue(NodeData); return _portAccessors[portName].GetValue(NodeData);
} }
public string[] GetPortsOfType<T> (){ public string[] GetPortsOfType<T> (){

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7390f9f15b054e6bab7abbb4afefee48
timeCreated: 1660804507

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 44e6960483c34cc5bd93c350a34a1106
timeCreated: 1660804527

@ -278,7 +278,7 @@ namespace TNodeGraphViewImpl.Editor.Cache{
public class Launcher{ public class Launcher{
static Launcher(){ static Launcher(){
//Get version of the package //Get version of the package
Debug.Log("TNodeCore v0.01 is launched");
NodeEditorSingleton.Instance.Initialize(); NodeEditorSingleton.Instance.Initialize();
} }
} }

@ -426,9 +426,9 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
} }
public void ResetGraphView(){ public void ResetGraphView(){
ClearAll(); ClearAll();
LoadPersistentGraphViewData(); LoadPersistentGraphViewData();
if (_nodeDict == null) throw new ArgumentNullException(nameof(_nodeDict)); if (_nodeDict == null) throw new ArgumentNullException(nameof(_nodeDict));
if (_data == null) return;
foreach (var dataNode in _data.NodeDictionary.Values){ foreach (var dataNode in _data.NodeDictionary.Values){
if(dataNode==null) if(dataNode==null)
continue; continue;
@ -484,6 +484,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
} }
private void LoadPersistentGraphViewData(){ private void LoadPersistentGraphViewData(){
if (_data == null) return;
var r= _data.GraphViewModel; var r= _data.GraphViewModel;
viewTransformChanged-=OnViewTransformChanged; viewTransformChanged-=OnViewTransformChanged;
UpdateViewTransform(r.persistOffset,new Vector3(r.persistScale,r.persistScale,1)); UpdateViewTransform(r.persistOffset,new Vector3(r.persistScale,r.persistScale,1));
@ -586,9 +587,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
}); });
links.Add(newNodeLink); links.Add(newNodeLink);
} }
} }
_data.NodeLinks = links; _data.NodeLinks = links;
} }
private void SaveGraphData(){ private void SaveGraphData(){
@ -698,6 +697,9 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
/// <param name="nodeData"></param> /// <param name="nodeData"></param>
/// <param name="rect">The give rect is the actual position in the graph view</param> /// <param name="rect">The give rect is the actual position in the graph view</param>
public void AddTNode(NodeData nodeData, Rect rect){ public void AddTNode(NodeData nodeData, Rect rect){
if (nodeData==null||nodeData.id == null){
return;
}
if (NodeEditorExtensions.CreateNodeViewFromNodeType(nodeData.GetType()) is Node nodeView){ if (NodeEditorExtensions.CreateNodeViewFromNodeType(nodeData.GetType()) is Node nodeView){
//convert rect at graph space //convert rect at graph space
@ -716,12 +718,9 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
} }
} }
}); });
if(_nodeDict.ContainsKey(nodeData.id)==false) if(_nodeDict.ContainsKey(nodeData.id)==false)
_nodeDict.Add(nodeData.id, nodeView); _nodeDict.Add(nodeData.id, nodeView);
if (_data.NodeDictionary.ContainsKey(nodeData.id) == false){ if (_data!=null&&_data.NodeDictionary.ContainsKey(nodeData.id) == false){
Undo.RegisterCompleteObjectUndo(_data,"Node Creation"); Undo.RegisterCompleteObjectUndo(_data,"Node Creation");
_data.NodeDictionary.Add(nodeData.id,nodeData); _data.NodeDictionary.Add(nodeData.id,nodeData);
} }

@ -95,10 +95,15 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
BuildDoubleClickRename(); BuildDoubleClickRename();
} }
private void OnDataChangedHandler(T obj){ private void OnDataChangedHandler(T obj){
if(obj.id==null||obj.id.Trim(' ').Length==0){
Debug.LogWarning("You should not use a node data without id");
return;
}
this.title = _data.nodeName; this.title = _data.nodeName;
if (_nodeInspectorInNode != null){ if (_nodeInspectorInNode != null){
_nodeInspectorInNode.Data = obj; _nodeInspectorInNode.Data = obj;
} }
_viewLogger ??= new NodeViewLogger{NodeView = this}; _viewLogger ??= new NodeViewLogger{NodeView = this};
if (NodeLogger.Loggers.ContainsKey(obj.id)){ if (NodeLogger.Loggers.ContainsKey(obj.id)){
NodeLogger.Loggers[obj.id] = _viewLogger; NodeLogger.Loggers[obj.id] = _viewLogger;
@ -166,7 +171,8 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
} }
} }
public virtual Type BuildGroupPortType(PortAttribute portAttribute,PropertyInfo propertyInfo,int index){
protected virtual Type BuildGroupPortType(PortAttribute portAttribute,PropertyInfo propertyInfo,int index){
var iList = propertyInfo.GetValue(_data) as IList; var iList = propertyInfo.GetValue(_data) as IList;
if (iList is Array array){ if (iList is Array array){
switch (portAttribute.TypeHandling){ switch (portAttribute.TypeHandling){
@ -224,7 +230,7 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
var port = new CustomPort(Orientation.Horizontal, Direction.Output, var port = new CustomPort(Orientation.Horizontal, Direction.Output,
Port.Capacity.Single, Port.Capacity.Single,
BuildGroupPortType(attribute, propertyInfo,i)); BuildGroupPortType(attribute, propertyInfo,i));
BuildPort(port, attribute, propertyInfo,outputContainer); BuildGroupPort(port, attribute, propertyInfo,outputContainer,list[i],i);
} }
} }
} }
@ -240,7 +246,17 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
BuildPort(port,attribute,propertyInfo,inputContainer); BuildPort(port,attribute,propertyInfo,inputContainer);
} }
else{ else{
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.Input,
Port.Capacity.Single,
BuildGroupPortType(attribute, propertyInfo,i));
BuildGroupPort(port, attribute, propertyInfo,inputContainer,list[i],i);
}
}
} }
} }
} }
@ -261,6 +277,7 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
private void BuildGroupPort(Port port, PortAttribute attribute, PropertyInfo propertyInfo, private void BuildGroupPort(Port port, PortAttribute attribute, PropertyInfo propertyInfo,
VisualElement portContainer,object currentElement,int index = 0){ VisualElement portContainer,object currentElement,int index = 0){
portContainer.Add(port); portContainer.Add(port);
port.name = propertyInfo.Name + ":" + index;
if (currentElement is string str){ if (currentElement is string str){
if (attribute.NameHandling == PortNameHandling.Auto){ if (attribute.NameHandling == PortNameHandling.Auto){
port.portName = str; port.portName = str;

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 61d88b1f3fc14972aa28c22a5afaa53e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save