diff --git a/TNode/Editor/Cache/NodeEditorExtensions.cs b/TNode/Editor/Cache/NodeEditorExtensions.cs index d21c96c..2ad617a 100644 --- a/TNode/Editor/Cache/NodeEditorExtensions.cs +++ b/TNode/Editor/Cache/NodeEditorExtensions.cs @@ -113,7 +113,6 @@ namespace TNode.Cache{ ){ //Get the generic type of this type //Add this type to the dictionary - Debug.Log($"type {type} is a registered as node component for {parent}"); FromGenericToSpecific.Add(parent, type); } //TODO Note that a node component only applied to a specific type of editor,so ,same GraphView could behave differently in different editor.it's a todo feature. diff --git a/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs index 148f9d7..081d9e8 100644 --- a/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs +++ b/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs @@ -5,6 +5,7 @@ using UnityEngine.UIElements; namespace TNode.Editor.Inspector.InspectorImplementation{ [NodeComponent] + [Obsolete] public class EnumFieldItem:InspectorItem{ public EnumFieldItem() : base(){ var field = new EnumField(); diff --git a/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs index fbabbb0..b8a06d6 100644 --- a/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs +++ b/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs @@ -3,6 +3,7 @@ using TNode.Attribute; using UnityEngine.UIElements; namespace TNode.Editor.Inspector.InspectorImplementation{ + [Obsolete] [NodeComponent] public class FloatFieldItem:InspectorItem{ public FloatFieldItem():base(){ diff --git a/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs index 05fa288..b1d6a01 100644 --- a/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs +++ b/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs @@ -1,9 +1,11 @@ -using UnityEditor; +using System; +using UnityEditor; using UnityEditor.UIElements; -using UnityEngine; using UnityEngine.UIElements; +using Object = UnityEngine.Object; namespace TNode.Editor.Inspector.InspectorImplementation{ + [Obsolete] public class PropertyFieldItem:InspectorItem{ public PropertyFieldItem(){ diff --git a/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs index 65d9ed9..e05ac16 100644 --- a/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs +++ b/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs @@ -1,7 +1,9 @@ -using TNode.Attribute; +using System; +using TNode.Attribute; using UnityEngine.UIElements; namespace TNode.Editor.Inspector.InspectorImplementation{ + [Obsolete] /// /// Force these element to bind native c# property /// diff --git a/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs index ce0e540..b40cfcc 100644 --- a/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs +++ b/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs @@ -1,7 +1,9 @@ -using TNode.Attribute; +using System; +using TNode.Attribute; using UnityEngine.UIElements; namespace TNode.Editor.Inspector.InspectorImplementation{ + [Obsolete] [NodeComponent] public class ToggleFieldItem:InspectorItem{ public ToggleFieldItem(){ diff --git a/TNode/Editor/Inspector/InspectorItem.cs b/TNode/Editor/Inspector/InspectorItem.cs index 8802bb5..1575c7c 100644 --- a/TNode/Editor/Inspector/InspectorItem.cs +++ b/TNode/Editor/Inspector/InspectorItem.cs @@ -5,6 +5,7 @@ using UnityEngine; using UnityEngine.UIElements; namespace TNode.Editor.Inspector{ + [Obsolete] public abstract class InspectorItem:VisualElement,INodeDataBinding { protected NodeData _bindingNodeData; protected string _bindingFieldName; diff --git a/TNode/Editor/Inspector/InspectorItemFactory.cs b/TNode/Editor/Inspector/InspectorItemFactory.cs index 9e5f9a7..d628307 100644 --- a/TNode/Editor/Inspector/InspectorItemFactory.cs +++ b/TNode/Editor/Inspector/InspectorItemFactory.cs @@ -7,6 +7,7 @@ using UnityEngine; using UnityEngine.UIElements; namespace TNode.Editor.Inspector{ + [Obsolete] public class InspectorItemFactory{ public InspectorItem Create(){ diff --git a/TNode/Editor/Inspector/NodeInspectorInNode.cs b/TNode/Editor/Inspector/NodeInspectorInNode.cs index 9a7c53c..8a0bc88 100644 --- a/TNode/Editor/Inspector/NodeInspectorInNode.cs +++ b/TNode/Editor/Inspector/NodeInspectorInNode.cs @@ -1,6 +1,8 @@ using System.Reflection; using TNode.Attribute; using TNode.Models; +using UnityEditor; +using UnityEditor.UIElements; using UnityEngine; using UnityEngine.UIElements; @@ -14,8 +16,11 @@ namespace TNode.Editor.Inspector{ UpdateData(); } + } + public NodeInspectorInNode():base(){ + } private void UpdateData(){ if (_data != null){ RefreshInspector(); @@ -23,22 +28,52 @@ namespace TNode.Editor.Inspector{ } private void RefreshInspector(){ + //Set size + Clear(); + //RefreshItems(); + RefreshPropertyDrawer(); + } + + private void RefreshPropertyDrawer(){ + //Check if the data's type is a generic type of BlackboardDragNodeData<> + if (_data.GetType().IsSubclassOf(typeof(BlackboardDragNodeData<>))){ + return; + } + + + + var serializedObject = new SerializedObject((NodeDataWrapper)_data); + foreach (var field in _data.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public|BindingFlags.NonPublic)){ + //Create corresponding property field + //check if the field has ShowInNodeView attribute + var showInNodeViewAttribute = field.GetCustomAttribute() != null; + if (!showInNodeViewAttribute) + continue; + var drawer = new PropertyField(serializedObject.FindProperty("Data").FindPropertyRelative(field.Name),field.Name); + Debug.Log(serializedObject.FindProperty("Data")); + drawer.Bind(serializedObject); + + Add(drawer); + } + } + + private void RefreshItems(){ InspectorItemFactory inspectorItemFactory = new InspectorItemFactory(); foreach (var field in _data.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)){ var bindingPath = field.Name; var type = field.FieldType; //check if the field has ShowInNodeView attribute - var showInNodeViewAttribute = field.GetCustomAttribute()!=null; - if(!showInNodeViewAttribute) + var showInNodeViewAttribute = field.GetCustomAttribute() != null; + if (!showInNodeViewAttribute) continue; var createdItem = inspectorItemFactory.Create(type); - if (createdItem is { } castedItem){ + if (createdItem is{ } castedItem){ castedItem.BindingNodeData = _data; castedItem.BindingPath = bindingPath; } - Add((VisualElement)createdItem); + Add((VisualElement) createdItem); } } } diff --git a/TNode/Editor/Inspector/PropertyDrawer.meta b/TNode/Editor/Inspector/PropertyDrawer.meta new file mode 100644 index 0000000..196eeba --- /dev/null +++ b/TNode/Editor/Inspector/PropertyDrawer.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cc8c20a304714599959643305857c804 +timeCreated: 1657486909 \ No newline at end of file diff --git a/TNode/Editor/Resources/GraphViewBackground.uss b/TNode/Editor/Resources/GraphViewBackground.uss index 8107b59..44451b6 100644 --- a/TNode/Editor/Resources/GraphViewBackground.uss +++ b/TNode/Editor/Resources/GraphViewBackground.uss @@ -3,4 +3,4 @@ GridBackground{ --line-color: rgba(211, 211, 211, 0.1); --thick-line-color: rgba(211, 211, 211, 0.2); --spacing:50; -} \ No newline at end of file +} diff --git a/TNode/Editor/Resources/NodeInspector.uss b/TNode/Editor/Resources/NodeInspector.uss new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/TNode/Editor/Resources/NodeInspector.uss @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/TNode/Editor/Resources/NodeInspector.uss.meta b/TNode/Editor/Resources/NodeInspector.uss.meta new file mode 100644 index 0000000..c27086e --- /dev/null +++ b/TNode/Editor/Resources/NodeInspector.uss.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3180a0745e5144aea3d7ea3d43b5b073 +timeCreated: 1657483260 \ No newline at end of file diff --git a/TNode/Models/BlackboardDragNodeData.cs b/TNode/Models/BlackboardDragNodeData.cs index dc587ad..6b1680b 100644 --- a/TNode/Models/BlackboardDragNodeData.cs +++ b/TNode/Models/BlackboardDragNodeData.cs @@ -1,4 +1,5 @@ -using System.Runtime.InteropServices; +using System; +using System.Runtime.InteropServices; using Newtonsoft.Json; using TNode.Attribute; using TNode.Attribute.Ports; @@ -9,7 +10,7 @@ namespace TNode.Models{ private string _blackDragData; [JsonIgnore] private BlackboardData _blackboardData; - + [Output("",PortNameHandling.MemberType)] public T Value => _blackboardData.GetValue(_blackDragData); diff --git a/TNode/Tools/NodeDataWrapper.cs b/TNode/Tools/NodeDataWrapper.cs index b7391af..1170590 100644 --- a/TNode/Tools/NodeDataWrapper.cs +++ b/TNode/Tools/NodeDataWrapper.cs @@ -4,36 +4,81 @@ using TNode.Models; using UnityEngine; namespace TNode.Editor{ - public class NodeDataWrapper{ - private readonly NodeData _data; + /// + /// Scriptable object wrapper enable property drawer for t-node + /// + public class NodeDataWrapper : ScriptableObject where T : NodeData{ + public T Data; + private static readonly Dictionary> Cache = new (); + public event Action> OnValueChanged; + public static NodeDataWrapper Get(T data){ + if(Cache.ContainsKey(data)){ + return Cache[data]; + } + var wrapper = ScriptableObject.CreateInstance>(); + Cache.Add(data,wrapper); + return wrapper; + } + public NodeDataWrapper(T data){ + this.Data = data; + } + + public void SetValue(string path, object value){ + var fieldInfo = Data.GetType().GetField(path); + fieldInfo.SetValue(Data,value); + OnValueChanged?.Invoke(this); + } + + public object GetValue(string path){ + var fieldInfo = Data.GetType().GetField(path); + return fieldInfo.GetValue(Data); + } + public static implicit operator T(NodeDataWrapper wrapper){ + if (wrapper == null) + return null; + return wrapper.Data; + + } + public static implicit operator NodeDataWrapper(T unWrapper){ + if (unWrapper == null) + return null; + return Get(unWrapper); + } + } + + public class NodeDataWrapper:ScriptableObject{ + [SerializeReference] + public NodeData Data; private static readonly Dictionary Cache = new (); public event Action OnValueChanged; public static NodeDataWrapper Get(NodeData data){ + if (data.GetType().IsGenericType){ + return ScriptableObject.CreateInstance(); + } if(Cache.ContainsKey(data)){ return Cache[data]; } - var wrapper = new NodeDataWrapper(data); + var wrapper = ScriptableObject.CreateInstance(); + wrapper.Data = data; Cache.Add(data,wrapper); return wrapper; } - public NodeDataWrapper(NodeData data){ - this._data = data; - } + public void SetValue(string path, object value){ - var fieldInfo = _data.GetType().GetField(path); - fieldInfo.SetValue(_data,value); + var fieldInfo = Data.GetType().GetField(path); + fieldInfo.SetValue(Data,value); OnValueChanged?.Invoke(this); } public object GetValue(string path){ - var fieldInfo = _data.GetType().GetField(path); - return fieldInfo.GetValue(_data); + var fieldInfo = Data.GetType().GetField(path); + return fieldInfo.GetValue(Data); } public static implicit operator NodeData(NodeDataWrapper wrapper){ if (wrapper == null) return null; - return wrapper._data; + return wrapper.Data; } public static implicit operator NodeDataWrapper(NodeData unWrapper){ @@ -41,6 +86,5 @@ namespace TNode.Editor{ return null; return Get(unWrapper); } - } } \ No newline at end of file