1.continue working on blackboard data

main
taoria 3 years ago
parent 28f87fd0f2
commit de0d55d07a
  1. 98
      TNode/Editor/BaseViews/DataGraphView.cs
  2. 12
      TNode/Editor/GraphBlackboard/BlackboardField.cs
  3. 3
      TNode/Editor/GraphBlackboard/BlackboardField.cs.meta
  4. 3
      TNode/Editor/GraphBlackboard/BlackboardProperty.meta
  5. 14
      TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs
  6. 3
      TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs.meta
  7. 4
      TNode/Models/BlackboardDragNodeData.cs
  8. 0
      TNode/Models/BlackboardDragNodeData.cs.meta
  9. 3
      TNode/RuntimeCache/RuntimeCache.cs

@ -4,8 +4,10 @@ using System.Linq;
using System.Reflection;
using TNode.BaseViews;
using TNode.Cache;
using TNode.Editor.GraphBlackboard;
using TNode.Editor.Inspector;
using TNode.Editor.Model;
using TNode.Models;
using Unity.VisualScripting;
using UnityEditor;
@ -109,6 +111,11 @@ namespace TNode.Editor.BaseViews{
}
*/
public abstract class DataGraphView<T>:GraphView,IDataGraphView where T:GraphData{
#region variablesandproperties
private T _data;
private bool _isInspectorOn;
@ -117,6 +124,7 @@ namespace TNode.Editor.BaseViews{
public GraphEditor<T> Owner;
private Dictionary<string,Node> _nodeDict = new();
private Blackboard _blackboard;
public T Data{
get{ return _data; }
@ -129,11 +137,14 @@ namespace TNode.Editor.BaseViews{
}
}
public event DataChangedEventHandler OnDataChanged;
#endregion
public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs<T> e);
//A Constructor for the DataGraphView ,never to override it
#region construct default behaviour
public DataGraphView(){
styleSheets.Add(Resources.Load<StyleSheet>("GraphViewBackground"));
var grid = new GridBackground();
Insert(0,grid);
@ -142,6 +153,7 @@ namespace TNode.Editor.BaseViews{
this.AddManipulator(new SelectionDragger());
this.AddManipulator(new RectangleSelector());
SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
RegisterDragEvent();
OnInit();
}
private void ConstructDefaultBehaviour(){
@ -150,10 +162,7 @@ namespace TNode.Editor.BaseViews{
}
public void ConstructViewContextualMenu(){
//Rebuild the contextual menu
this.RegisterCallback<ContextualMenuPopulateEvent>(evt => {
RegisterCallback<ContextualMenuPopulateEvent>(evt => {
Vector2 editorPosition = Owner==null?Vector2.zero:Owner.position.position;
//Remove all the previous menu items
evt.menu.MenuItems().Clear();
@ -164,11 +173,50 @@ namespace TNode.Editor.BaseViews{
searchWindow.Setup(typeof(T),this,Owner);
SearchWindow.Open(searchWindowContext, searchWindow);
});
});
}
public void RegisterDragEvent(){
RegisterCallback<DragUpdatedEvent>(OnDragUpdated);
RegisterCallback<DragPerformEvent>(OnDragPerform);
}
#endregion
private void OnDragPerform(DragPerformEvent evt){
if (DragAndDrop.GetGenericData("DragSelection") is List<ISelectable>{Count: > 0} data){
var blackboardFields = data.OfType<BlackboardPropertyField >();
foreach (var selectable in blackboardFields){
if(selectable is { } field) {
//Make a constructor of BlackboardDragNodeData<field.PropertyType > by reflection
var specifiedType =
typeof(BlackboardDragNodeData<>).MakeGenericType(field.BlackboardProperty.PropertyType);
//get specific constructor of specified type with two parameters, one is a string ,another is a blackboarddata
var constructor = specifiedType.GetConstructor(new[] {typeof(string),typeof(BlackboardData)});
//create a new instance of the specified type with the constructor and the two parameters
if (constructor != null){
var dragNodeData = constructor.Invoke(new object[]
{field.BlackboardProperty.PropertyName, _data.blackboardData}) as NodeData;
this.AddTNode(dragNodeData,new Rect(Owner.position.position+evt.mousePosition,new Vector2(100,100)));
}
}
}
}
}
private void OnDragUpdated(DragUpdatedEvent evt){
Debug.Log(evt);
//check if the drag data is BlackboardField
if (DragAndDrop.GetGenericData("DragSelection") is List<ISelectable>{Count: > 0} data){
DragAndDrop.visualMode = DragAndDropVisualMode.Move;
}
}
private void OnInit(){
ConstructDefaultBehaviour();
OnGraphViewCreate();
@ -267,36 +315,8 @@ namespace TNode.Editor.BaseViews{
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){
Debug.Log(field);
//if the field is MonoBehaviour,add a property field for blackboard
if (typeof(UnityEngine.Object).IsAssignableFrom(field.FieldType)){
var propertyField = new BlackboardField(null, field.Name, null){
};
_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){
};
_blackboard.Add(propertyField);
}
var propertyField = new BlackboardPropertyField(new BlackboardProperty(field.Name,field.FieldType));
_blackboard.Add(propertyField);
}
}
@ -398,6 +418,10 @@ namespace TNode.Editor.BaseViews{
OnGraphViewDestroy();
}
public bool IsDroppable(){
return true;
}
public void AddTNode(NodeData nodeData, Rect rect){
if (NodeEditorExtensions.CreateNodeViewFromNodeType(nodeData.GetType()) is Node nodeView){
nodeView.SetPosition(rect);

@ -0,0 +1,12 @@
using UnityEditor.Experimental.GraphView;
namespace TNode.Editor.GraphBlackboard{
public class BlackboardPropertyField:BlackboardField{
public BlackboardProperty BlackboardProperty;
public BlackboardPropertyField(BlackboardProperty blackboardProperty):base(null,blackboardProperty.PropertyName,null){
BlackboardProperty = blackboardProperty;
}
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5603912d8c2b4d71878f76a7eb5915a7
timeCreated: 1657033185

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 35fceca3e7c849279ed5bee0a5ee3225
timeCreated: 1657041797

@ -0,0 +1,14 @@
using System;
namespace TNode.Editor.GraphBlackboard{
public class BlackboardProperty{
public string PropertyName;
public Type PropertyType;
// public RuntimeCache.RuntimeCache.GetValueDelegate GetValue;
// public RuntimeCache.RuntimeCache.SetValueDelegate SetValue;
public BlackboardProperty(string propertyName, Type propertyType){
PropertyName = propertyName;
PropertyType = propertyType;
}
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b022203e15244da5bcfbf7932e7dd30c
timeCreated: 1657034160

@ -4,7 +4,7 @@ using TNode.Attribute.Ports;
using TNode.RuntimeCache;
namespace TNode.Models{
public class BlackDragNodeData<T>:NodeData where T:BlackboardData{
public class BlackboardDragNodeData<T>:NodeData where T:BlackboardData{
[JsonIgnore]
private string _blackDragData;
[JsonIgnore]
@ -12,7 +12,7 @@ namespace TNode.Models{
[Output]
public T Value => _blackboardData.GetValue<T>(_blackDragData);
public BlackDragNodeData(string blackDragData,T blackboardData){
public BlackboardDragNodeData(string blackDragData,T blackboardData){
_blackDragData = blackDragData;
_blackboardData = blackboardData;
}

@ -15,6 +15,7 @@ namespace TNode.RuntimeCache{
}
//delegate return a value from a nodedata
public delegate object GetValueDelegate(IModel nodeData);
public delegate object SetValueDelegate(IModel nodeData,object value);
public readonly Dictionary<Type, Dictionary<string,GetValueDelegate>> CachedDelegatesForGettingValue =
new ();
@ -52,7 +53,7 @@ namespace TNode.RuntimeCache{
}
}
public static class RuntimeExtension{

Loading…
Cancel
Save