1.blackboard data

main
taoria 3 years ago
parent e920a2facb
commit 28f87fd0f2
  1. 55
      TNode/Editor/BaseViews/DataGraphView.cs
  2. 10
      TNode/Models/BlackDragNodeData.cs
  3. 4
      TNode/Models/BlackboardData.cs
  4. 10
      TNode/Runtime/Runtimeblackboard.cs
  5. 3
      TNode/Runtime/Runtimeblackboard.cs.meta
  6. 58
      TNode/RuntimeCache/RuntimeCache.cs

@ -116,6 +116,7 @@ namespace TNode.Editor.BaseViews{
private NodeInspector _nodeInspector; private NodeInspector _nodeInspector;
public GraphEditor<T> Owner; public GraphEditor<T> Owner;
private Dictionary<string,Node> _nodeDict = new(); private Dictionary<string,Node> _nodeDict = new();
private Blackboard _blackboard;
public T Data{ public T Data{
get{ return _data; } get{ return _data; }
@ -234,7 +235,7 @@ namespace TNode.Editor.BaseViews{
} }
public void CreateBlackboard(){ public void CreateBlackboard(){
var blackboard = new Blackboard(); _blackboard = new Blackboard();
//Blackboard add "Add Node" button //Blackboard add "Add Node" button
// blackboard.Add(new BlackboardSection(){ // blackboard.Add(new BlackboardSection(){
// title = "Hello World", // title = "Hello World",
@ -245,40 +246,60 @@ namespace TNode.Editor.BaseViews{
// }; // };
// //
//Set black board to left side of the view //Set black board to left side of the view
blackboard.SetPosition(new Rect(0,0,200,600)); _blackboard.SetPosition(new Rect(0,0,200,600));
Add(blackboard); Add(_blackboard);
//Check the type of the blackboard //Check the type of the blackboard
OnDataChanged+= (sender, e) => { OnDataChanged+= (sender, e) => { BlackboardUpdate(); };
if (_data.blackboardData==null||_data.blackboardData.GetType()==typeof(BlackboardData)){ }
_data.blackboardData = NodeEditorExtensions.GetAppropriateBlackboardData(_data.GetType());
if(_data.blackboardData==null) return; private void BlackboardUpdate(){
if (_data.blackboardData == null || _data.blackboardData.GetType() == typeof(BlackboardData)){
_data.blackboardData = NodeEditorExtensions.GetAppropriateBlackboardData(_data.GetType());
if (_data.blackboardData == null) return;
} }
Debug.Log(_data.blackboardData); Debug.Log(_data.blackboardData);
//Iterate field of the blackboard and add a button for each field //Iterate field of the blackboard and add a button for each field
foreach (var field in _data.blackboardData.GetType() foreach (var field in _data.blackboardData.GetType()
.GetFields(BindingFlags.Public|BindingFlags.NonPublic | BindingFlags.Instance)){ .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){
Debug.Log(field); Debug.Log(field);
//if the field is MonoBehaviour,add a property field for blackboard //if the field is MonoBehaviour,add a property field for blackboard
if(typeof(UnityEngine.Object).IsAssignableFrom(field.FieldType)){ if (typeof(UnityEngine.Object).IsAssignableFrom(field.FieldType)){
var propertyField = new BlackboardField(null,field.Name,null){ var propertyField = new BlackboardField(null, field.Name, null){
}; };
blackboard.Add(propertyField); _blackboard.Add(propertyField);
//register a drag event for the property field to drag the object from blackboard to the graph
propertyField.RegisterCallback<DragUpdatedEvent>(evt => {
if (evt.target is GraphView graphView){
var type = field.FieldType;
//Get Generic Constructor of the BlackDragNodeData<>
var genericConstructor = typeof(BlackDragNodeData<>).MakeGenericType(type).GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Type[]{typeof(string), typeof(object)},
null);
if (genericConstructor != null){
NodeData nodeData = null;
nodeData =
genericConstructor.Invoke(null, new object[]{field.Name, _data.blackboardData}) as NodeData;
this.AddTNode(nodeData, new Rect(evt.localMousePosition, new Vector2(200, 200)));
}
}
});
} }
if(typeof(string).IsAssignableFrom(field.FieldType)){
var propertyField = new BlackboardField(null,field.Name,null){
if (typeof(string).IsAssignableFrom(field.FieldType)){
var propertyField = new BlackboardField(null, field.Name, null){
}; };
blackboard.Add(propertyField); _blackboard.Add(propertyField);
} }
} }
};
} }
public virtual void DestroyInspector(){ public virtual void DestroyInspector(){
if(_nodeInspector!=null){ if(_nodeInspector!=null){
this.Remove(_nodeInspector); this.Remove(_nodeInspector);

@ -1,16 +1,18 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Newtonsoft.Json; using Newtonsoft.Json;
using TNode.Attribute.Ports; using TNode.Attribute.Ports;
using TNode.RuntimeCache;
namespace TNode.Models{ namespace TNode.Models{
public class BlackDragNodeData<T>:NodeData{ public class BlackDragNodeData<T>:NodeData where T:BlackboardData{
[JsonIgnore] [JsonIgnore]
private string _blackDragData; private string _blackDragData;
[JsonIgnore] [JsonIgnore]
private BlackboardData _blackboardData; private T _blackboardData;
[Output] public T value => _blackboardData.GetValue<T>(_blackDragData); [Output]
public BlackDragNodeData(string blackDragData,BlackboardData blackboardData){ public T Value => _blackboardData.GetValue<T>(_blackDragData);
public BlackDragNodeData(string blackDragData,T blackboardData){
_blackDragData = blackDragData; _blackDragData = blackDragData;
_blackboardData = blackboardData; _blackboardData = blackboardData;
} }

@ -1,8 +1,6 @@
namespace TNode.Models{ namespace TNode.Models{
public class BlackboardData:IModel{ public class BlackboardData:IModel{
public T GetValue<T>(string key){
return default(T);
}
} }
} }

@ -0,0 +1,10 @@
using System.Collections.Generic;
using TNode.Models;
namespace TNode.Runtime{
public class RuntimeBlackboard<T> where T:BlackboardData{
public T Data { get; set; }
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6b69b2dedba24e35bf04bc9112da6b12
timeCreated: 1656958407

@ -16,38 +16,13 @@ namespace TNode.RuntimeCache{
//delegate return a value from a nodedata //delegate return a value from a nodedata
public delegate object GetValueDelegate(IModel nodeData); public delegate object GetValueDelegate(IModel nodeData);
public readonly Dictionary<Type, List<GetValueDelegate>> CachedDelegatesForGettingValue = public readonly Dictionary<Type, Dictionary<string,GetValueDelegate>> CachedDelegatesForGettingValue =
new (); new ();
public void ExecuteOutput<T>(T nodeData) where T:NodeData{
var type = typeof(T);
if(!CachedDelegatesForGettingValue.ContainsKey(type)){
return;
}
var delegates = CachedDelegatesForGettingValue[type];
foreach(var delegateInstance in delegates){
var value = delegateInstance(nodeData);
}
}
private static readonly string[] ExcludedAssemblies = new string[]{"Microsoft", "UnityEngine","UnityEditor","mscorlib","System"}; private static readonly string[] ExcludedAssemblies = new string[]{"Microsoft", "UnityEngine","UnityEditor","mscorlib","System"};
public void RegisterRuntimeNode<T>() where T:NodeData{
var type = typeof(T);
if(!CachedDelegatesForGettingValue.ContainsKey(type)){
CachedDelegatesForGettingValue.Add(type, new List<GetValueDelegate>());
var properties = type.GetProperties();
foreach(var property in properties){
var getValueDelegate = GetValueDelegateForProperty(property);
CachedDelegatesForGettingValue[type].Add(getValueDelegate);
}
}
else{
//Cache already exists for this type
}
}
public void RegisterRuntimeBlackboard(Type type){ public void RegisterRuntimeBlackboard(Type type){
if(!CachedDelegatesForGettingValue.ContainsKey(type)){ if(!CachedDelegatesForGettingValue.ContainsKey(type)){
CachedDelegatesForGettingValue.Add(type, new List<GetValueDelegate>()); CachedDelegatesForGettingValue.Add(type, new Dictionary<string, GetValueDelegate>());
var properties = type.GetProperties(); var properties = type.GetProperties();
foreach(var property in properties){ foreach(var property in properties){
//if the property only has a setter ,skip //if the property only has a setter ,skip
@ -55,14 +30,41 @@ namespace TNode.RuntimeCache{
continue; continue;
} }
var getValueDelegate = GetValueDelegateForProperty(property); var getValueDelegate = GetValueDelegateForProperty(property);
CachedDelegatesForGettingValue[type].Add(getValueDelegate); CachedDelegatesForGettingValue[type].Add(property.Name,getValueDelegate);
} }
//register the fields
var fields = type.GetFields();
foreach(var field in fields){
var getValueDelegate = GetValueDelegateForField(field);
CachedDelegatesForGettingValue[type].Add(field.Name,getValueDelegate);
} }
} }
}
private GetValueDelegate GetValueDelegateForField(FieldInfo field){
return field.GetValue;
}
private GetValueDelegate GetValueDelegateForProperty(PropertyInfo property){ private GetValueDelegate GetValueDelegateForProperty(PropertyInfo property){
var getValueDelegate = (GetValueDelegate)Delegate.CreateDelegate(typeof(GetValueDelegate), property.GetGetMethod()); var getValueDelegate = (GetValueDelegate)Delegate.CreateDelegate(typeof(GetValueDelegate), property.GetGetMethod());
return getValueDelegate; return getValueDelegate;
} }
}
public static class RuntimeExtension{
//todo latter on i will try some way caching reflection more efficiently
public static T GetValue<T>(this BlackboardData blackboardData,string path){
var method = RuntimeCache.Instance.CachedDelegatesForGettingValue[blackboardData.GetType()][path];
return (T) method.Invoke(blackboardData);
}
public static RuntimeCache.GetValueDelegate GetValueDelegate(this BlackboardData blackboardData,string path){
var method = RuntimeCache.Instance.CachedDelegatesForGettingValue[blackboardData.GetType()][path];
return method;
}
} }
} }
Loading…
Cancel
Save