From de0d55d07aac8e0dad05312aacd5a50cdb0b2113 Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Wed, 6 Jul 2022 02:38:50 +0800 Subject: [PATCH] 1.continue working on blackboard data --- TNode/Editor/BaseViews/DataGraphView.cs | 98 ++++++++++++------- .../Editor/GraphBlackboard/BlackboardField.cs | 12 +++ .../GraphBlackboard/BlackboardField.cs.meta | 3 + .../GraphBlackboard/BlackboardProperty.meta | 3 + .../BlackboardProperty/BlackboardProperty.cs | 14 +++ .../BlackboardProperty.cs.meta | 3 + ...gNodeData.cs => BlackboardDragNodeData.cs} | 4 +- ...cs.meta => BlackboardDragNodeData.cs.meta} | 0 TNode/RuntimeCache/RuntimeCache.cs | 3 +- 9 files changed, 100 insertions(+), 40 deletions(-) create mode 100644 TNode/Editor/GraphBlackboard/BlackboardField.cs create mode 100644 TNode/Editor/GraphBlackboard/BlackboardField.cs.meta create mode 100644 TNode/Editor/GraphBlackboard/BlackboardProperty.meta create mode 100644 TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs create mode 100644 TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs.meta rename TNode/Models/{BlackDragNodeData.cs => BlackboardDragNodeData.cs} (75%) rename TNode/Models/{BlackDragNodeData.cs.meta => BlackboardDragNodeData.cs.meta} (100%) diff --git a/TNode/Editor/BaseViews/DataGraphView.cs b/TNode/Editor/BaseViews/DataGraphView.cs index 6312cf7..baa2227 100644 --- a/TNode/Editor/BaseViews/DataGraphView.cs +++ b/TNode/Editor/BaseViews/DataGraphView.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:GraphView,IDataGraphView where T:GraphData{ + #region variablesandproperties + + + + private T _data; private bool _isInspectorOn; @@ -117,6 +124,7 @@ namespace TNode.Editor.BaseViews{ public GraphEditor Owner; private Dictionary _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 e); //A Constructor for the DataGraphView ,never to override it + + + #region construct default behaviour public DataGraphView(){ - styleSheets.Add(Resources.Load("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(evt => { + RegisterCallback(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(OnDragUpdated); + RegisterCallback(OnDragPerform); + } + #endregion + + private void OnDragPerform(DragPerformEvent evt){ + + if (DragAndDrop.GetGenericData("DragSelection") is List{Count: > 0} data){ + var blackboardFields = data.OfType(); + foreach (var selectable in blackboardFields){ + if(selectable is { } field) { + //Make a constructor of BlackboardDragNodeData 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{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(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); diff --git a/TNode/Editor/GraphBlackboard/BlackboardField.cs b/TNode/Editor/GraphBlackboard/BlackboardField.cs new file mode 100644 index 0000000..87f5d9d --- /dev/null +++ b/TNode/Editor/GraphBlackboard/BlackboardField.cs @@ -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; + } + + + } +} \ No newline at end of file diff --git a/TNode/Editor/GraphBlackboard/BlackboardField.cs.meta b/TNode/Editor/GraphBlackboard/BlackboardField.cs.meta new file mode 100644 index 0000000..e30c407 --- /dev/null +++ b/TNode/Editor/GraphBlackboard/BlackboardField.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5603912d8c2b4d71878f76a7eb5915a7 +timeCreated: 1657033185 \ No newline at end of file diff --git a/TNode/Editor/GraphBlackboard/BlackboardProperty.meta b/TNode/Editor/GraphBlackboard/BlackboardProperty.meta new file mode 100644 index 0000000..85b9875 --- /dev/null +++ b/TNode/Editor/GraphBlackboard/BlackboardProperty.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 35fceca3e7c849279ed5bee0a5ee3225 +timeCreated: 1657041797 \ No newline at end of file diff --git a/TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs b/TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs new file mode 100644 index 0000000..d7dff1a --- /dev/null +++ b/TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs.meta b/TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs.meta new file mode 100644 index 0000000..c0b848a --- /dev/null +++ b/TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b022203e15244da5bcfbf7932e7dd30c +timeCreated: 1657034160 \ No newline at end of file diff --git a/TNode/Models/BlackDragNodeData.cs b/TNode/Models/BlackboardDragNodeData.cs similarity index 75% rename from TNode/Models/BlackDragNodeData.cs rename to TNode/Models/BlackboardDragNodeData.cs index 4876f25..3b75c22 100644 --- a/TNode/Models/BlackDragNodeData.cs +++ b/TNode/Models/BlackboardDragNodeData.cs @@ -4,7 +4,7 @@ using TNode.Attribute.Ports; using TNode.RuntimeCache; namespace TNode.Models{ - public class BlackDragNodeData:NodeData where T:BlackboardData{ + public class BlackboardDragNodeData:NodeData where T:BlackboardData{ [JsonIgnore] private string _blackDragData; [JsonIgnore] @@ -12,7 +12,7 @@ namespace TNode.Models{ [Output] public T Value => _blackboardData.GetValue(_blackDragData); - public BlackDragNodeData(string blackDragData,T blackboardData){ + public BlackboardDragNodeData(string blackDragData,T blackboardData){ _blackDragData = blackDragData; _blackboardData = blackboardData; } diff --git a/TNode/Models/BlackDragNodeData.cs.meta b/TNode/Models/BlackboardDragNodeData.cs.meta similarity index 100% rename from TNode/Models/BlackDragNodeData.cs.meta rename to TNode/Models/BlackboardDragNodeData.cs.meta diff --git a/TNode/RuntimeCache/RuntimeCache.cs b/TNode/RuntimeCache/RuntimeCache.cs index 11bc35b..c63aea1 100644 --- a/TNode/RuntimeCache/RuntimeCache.cs +++ b/TNode/RuntimeCache/RuntimeCache.cs @@ -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> CachedDelegatesForGettingValue = new (); @@ -52,7 +53,7 @@ namespace TNode.RuntimeCache{ } - + } public static class RuntimeExtension{