diff --git a/Sample/MathGraph/Editor/MathEditor.cs b/Sample/MathGraph/Editor/MathEditor.cs index 75ae61d..a5bb700 100644 --- a/Sample/MathGraph/Editor/MathEditor.cs +++ b/Sample/MathGraph/Editor/MathEditor.cs @@ -14,7 +14,7 @@ public class MathEditor : GraphEditor{ var wnd = GetWindow(); wnd.titleContent = new GUIContent("MathGraph Editor"); wnd.CreateGUI(); - wnd._graphView.Data = graph; + wnd.GraphView.Data = graph; return true; } return false; diff --git a/Sample/MathGraph/Editor/MathGraphView.cs b/Sample/MathGraph/Editor/MathGraphView.cs index 730dfab..05928c2 100644 --- a/Sample/MathGraph/Editor/MathGraphView.cs +++ b/Sample/MathGraph/Editor/MathGraphView.cs @@ -1,8 +1,9 @@ using TNode.Models; using TNode.Attribute; -using TNode.Editor.BaseViews; -[NodeComponent] -public class MathGraphView : DataGraphView{ +using TNodeGraphViewImpl.Editor.NodeGraphView; + +[ViewComponent] +public class MathGraphView : BaseDataGraphView{ diff --git a/TNode/Attribute/Ports/InputAttribute.cs b/TNode/Attribute/Ports/InputAttribute.cs index dd4b855..e0d4b96 100644 --- a/TNode/Attribute/Ports/InputAttribute.cs +++ b/TNode/Attribute/Ports/InputAttribute.cs @@ -1,9 +1,7 @@ using System; using JetBrains.Annotations; -using TNode.Models; -using UnityEditor.Experimental.GraphView; -namespace TNode.Attribute{ +namespace TNode.Attribute.Ports{ [MeansImplicitUse] [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class InputAttribute : PortAttribute{ diff --git a/TNode/Attribute/Ports/PortAttribute.cs b/TNode/Attribute/Ports/PortAttribute.cs index 24feda5..cc3be06 100644 --- a/TNode/Attribute/Ports/PortAttribute.cs +++ b/TNode/Attribute/Ports/PortAttribute.cs @@ -1,8 +1,7 @@ using System; using JetBrains.Annotations; -using UnityEditor.Experimental.GraphView; -namespace TNode.Attribute{ +namespace TNode.Attribute.Ports{ public enum PortNameHandling{ Auto, diff --git a/TNode/Attribute/NodeComponentAttribute.cs b/TNode/Attribute/ViewComponentAttribute.cs similarity index 83% rename from TNode/Attribute/NodeComponentAttribute.cs rename to TNode/Attribute/ViewComponentAttribute.cs index 2a3c660..6399726 100644 --- a/TNode/Attribute/NodeComponentAttribute.cs +++ b/TNode/Attribute/ViewComponentAttribute.cs @@ -11,7 +11,7 @@ namespace TNode.Attribute{ [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] [MeansImplicitUse] - public class NodeComponentAttribute:System.Attribute{ + public class ViewComponentAttribute:System.Attribute{ public Type GenericType{ get; set; } diff --git a/TNode/Attribute/NodeComponentAttribute.cs.meta b/TNode/Attribute/ViewComponentAttribute.cs.meta similarity index 100% rename from TNode/Attribute/NodeComponentAttribute.cs.meta rename to TNode/Attribute/ViewComponentAttribute.cs.meta diff --git a/TNode/Editor/BaseViews.meta b/TNode/Editor/BaseViews.meta deleted file mode 100644 index 54107dd..0000000 --- a/TNode/Editor/BaseViews.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: be6b702224dc40098aadadb3749d8c81 -timeCreated: 1655394343 \ No newline at end of file diff --git a/TNode/Editor/Cache/NodeEditorExtensions.cs b/TNode/Editor/Cache/NodeEditorExtensions.cs index d21c96c..4f2de04 100644 --- a/TNode/Editor/Cache/NodeEditorExtensions.cs +++ b/TNode/Editor/Cache/NodeEditorExtensions.cs @@ -2,11 +2,12 @@ using System.Collections.Generic; using System.Linq; using TNode.Attribute; -using TNode.BaseViews; using TNode.Editor; -using TNode.Editor.BaseViews; using TNode.Editor.Inspector; +using TNode.Editor.NodeViews; using TNode.Models; +using TNodeGraphViewImpl.Editor.GraphBlackboard; +using TNodeGraphViewImpl.Editor.NodeGraphView; using UnityEditor.Experimental.GraphView; using UnityEngine; using UnityEngine.TestTools.Utils; @@ -58,7 +59,7 @@ namespace TNode.Cache{ foreach(var type in assembly.GetTypes()){ if(type.IsClass && !type.IsAbstract){ //Register Node View And Graph View via its parent class - SetNodeComponentAttribute(type); + SetViewComponentAttribute(type); //Register Node Data by GraphUsageAttribute. SetGraphUsageAttribute(type); } @@ -98,12 +99,13 @@ namespace TNode.Cache{ } } } - private readonly Type[] _acceptedTypesForGenericToSpecific = new Type[]{typeof(NodeView<>),typeof(DataGraphView<>),typeof(InspectorItem<>),typeof(NodeView<>)}; - private void SetNodeComponentAttribute(Type type){ - foreach (var attribute in type.GetCustomAttributes(typeof(NodeComponentAttribute), false)){ + private readonly Type[] _acceptedTypesForGenericToSpecific = new Type[]{typeof(BaseNodeView<>),typeof(BaseDataGraphView<>),typeof(GraphBlackboardView<>)}; + private readonly Type[] _defaultTypes = new []{typeof(DefaultBaseNodeView),typeof(DefaultGraphBlackboardView)}; + private void SetViewComponentAttribute(Type type){ + foreach (var attribute in type.GetCustomAttributes(typeof(ViewComponentAttribute), false)){ //fetch this type 's parent class var parent = type.BaseType; - //Check if this type is a generic type and is a generic type of NodeView or DataGraphView, + //Check if this type is a generic type and is a generic type of BaseNodeView or BaseDataGraphView, //Two level generic definition is now supported by TNode //Deeper nested generic definition is not supported by TNode if (parent is{IsGenericType: true} && @@ -113,7 +115,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. @@ -122,24 +123,60 @@ namespace TNode.Cache{ } //Outer wrapper for the singleton class public static class NodeEditorExtensions{ - public static T CreateNodeComponentFromGenericType(){ + /// + /// by given a generic type T,return the implementation instance of the generic type + /// + /// + /// + public static T CreateViewComponentFromBaseType(){ var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[typeof(T)]; var instance = (T)Activator.CreateInstance(implementedType); return instance; } - public static object CreateNodeComponentFromGenericType(Type t){ + + /// + /// by given a generic type t,return the implementation instance of the generic type + /// + /// + public static object CreateViewComponentFromBaseType(Type t){ if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(t)){ var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[t]; var instance = Activator.CreateInstance(implementedType); return instance; } + //check if t is a generic type node view - if (t is{IsGenericType: true} && t.GetGenericTypeDefinition() == typeof(NodeView<>)){ - var instance = Activator.CreateInstance(typeof(NodeView)); + if (t is{IsGenericType: true} && t.GetGenericTypeDefinition() == typeof(BaseNodeView<>)){ + var instance = Activator.CreateInstance(typeof(BaseNodeView)); return instance; } return null; } + + public static Blackboard CreateBlackboardDataFromBlackboardDataType(Type t){ + var type = typeof(GraphBlackboardView<>).MakeGenericType(t); + var res = CreateViewComponentFromBaseType(type) as Blackboard; + return res ?? new DefaultGraphBlackboardView(); + + } + + public static Blackboard CreateBlackboardWithGraphData(GraphData graphData){ + var graphType = graphData.GetType(); + if (NodeEditorSingleton.Instance.GraphBlackboard.ContainsKey(graphType)){ + var type = NodeEditorSingleton.Instance.GraphBlackboard[graphType]; + return CreateBlackboardDataFromBlackboardDataType(type); + + } + return null; + } + public static Blackboard CreateBlackboardWithGraphData(Type graphType){ + if (NodeEditorSingleton.Instance.GraphBlackboard.ContainsKey(graphType)){ + var type = NodeEditorSingleton.Instance.GraphBlackboard[graphType]; + return CreateBlackboardDataFromBlackboardDataType(type); + + } + return null; + } public static bool HasSpecificTypeComponent() where T : class{ return NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(typeof(T)); @@ -160,21 +197,8 @@ namespace TNode.Cache{ } return null; } - public static object CreateNodeViewFromNodeType() where T:NodeData,new(){ - //Check specific derived type exists or not. - var type = typeof(NodeView); - if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(type)){ - var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[type]; - var instance = (NodeView)Activator.CreateInstance(implementedType); - return instance; - } - else{ - return new DefaultNodeView(); - } - - } public static object CreateNodeViewFromNodeType(Type t){ - //Check the generic type of NodeView by t + //Check the generic type of BaseNodeView by t if (t.IsGenericType){ Debug.Log($"A generic type {t} is detected"); @@ -184,8 +208,8 @@ namespace TNode.Cache{ var genericTypeDefinition = t.GetGenericTypeDefinition(); - //What you want is a NodeView> to be created - var genericViewType = typeof(NodeView<>).MakeGenericType(genericTypeDefinition); + //What you want is a BaseNodeView> to be created + var genericViewType = typeof(BaseNodeView<>).MakeGenericType(genericTypeDefinition); Debug.Log($"The generic view type is {genericViewType}"); //search for the specific type of genericViewType in the dictionary @@ -202,11 +226,11 @@ namespace TNode.Cache{ } else{ - return new DefaultNodeView(); + return new DefaultBaseNodeView(); } } - var type = typeof(NodeView<>).MakeGenericType(t); + var type = typeof(BaseNodeView<>).MakeGenericType(t); if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(type)){ var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[type]; @@ -215,7 +239,7 @@ namespace TNode.Cache{ } else{ - return new DefaultNodeView(); + return new DefaultBaseNodeView(); } } diff --git a/TNode/Editor/GraphBlackboard/BlackboardField.cs b/TNode/Editor/GraphBlackboard/BlackboardField.cs deleted file mode 100644 index 87f5d9d..0000000 --- a/TNode/Editor/GraphBlackboard/BlackboardField.cs +++ /dev/null @@ -1,12 +0,0 @@ -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/GraphEditor.cs b/TNode/Editor/GraphEditor.cs index e045f39..93a272e 100644 --- a/TNode/Editor/GraphEditor.cs +++ b/TNode/Editor/GraphEditor.cs @@ -1,10 +1,9 @@ using Codice.CM.Common; -using TNode.BaseViews; using TNode.Cache; -using TNode.Editor.BaseViews; using TNode.Editor.Inspector; using TNode.Editor.Model; using TNode.Models; +using TNodeGraphViewImpl.Editor.NodeGraphView; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEngine; @@ -15,7 +14,7 @@ namespace TNode.Editor{ public abstract class GraphEditor : EditorWindow where T:GraphData{ - protected DataGraphView _graphView; + protected BaseDataGraphView GraphView; [SerializeField] private VisualTreeAsset mVisualTreeAsset = default; //Persist editor data ,such as node position,node size ,etc ,in this script object @@ -36,9 +35,9 @@ namespace TNode.Editor{ } private void BuildGraphView(){ - _graphView = NodeEditorExtensions.CreateNodeComponentFromGenericType>(); - rootVisualElement.Add(_graphView); - _graphView.StretchToParentSize(); + GraphView = NodeEditorExtensions.CreateViewComponentFromBaseType>(); + rootVisualElement.Add(GraphView); + GraphView.StretchToParentSize(); } private void DefineGraphEditorActions(){ @@ -54,7 +53,7 @@ namespace TNode.Editor{ private void Save(){ //if no graph is loaded ,create a file save dialogue - if (_graphView.Data == null) + if (GraphView.Data == null) { string path = EditorUtility.SaveFilePanel("Save Graph", "", "", "asset"); if (path.Length != 0){ @@ -66,7 +65,7 @@ namespace TNode.Editor{ } } else{ - _graphView.SaveWithEditorData(graphEditorData); + GraphView.SaveWithEditorData(graphEditorData); AssetDatabase.Refresh(); } diff --git a/TNode/Editor/Inspector/InspectorImplementation.meta b/TNode/Editor/Inspector/InspectorImplementation.meta deleted file mode 100644 index d8de6bd..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 4cdebd0430794d32918ba8c63d71d0cc -timeCreated: 1656142311 \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs deleted file mode 100644 index 148f9d7..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using TNode.Attribute; -using UnityEngine; -using UnityEngine.UIElements; - -namespace TNode.Editor.Inspector.InspectorImplementation{ - [NodeComponent] - public class EnumFieldItem:InspectorItem{ - public EnumFieldItem() : base(){ - var field = new EnumField(); - Debug.Log("An Enum Field is created"); - CreateBindable(field); - OnDataChanged += () => { - - field.Init(Value); - Debug.Log(Value.GetType()); - }; - } - } -} \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs.meta b/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs.meta deleted file mode 100644 index 1a83a95..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 6eb83a1255d545e5998c7b3efd1b0d69 -timeCreated: 1657193097 \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs deleted file mode 100644 index fbabbb0..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using TNode.Attribute; -using UnityEngine.UIElements; - -namespace TNode.Editor.Inspector.InspectorImplementation{ - [NodeComponent] - public class FloatFieldItem:InspectorItem{ - public FloatFieldItem():base(){ - CreateBindable(new FloatField()); - } - } -} \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs.meta b/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs.meta deleted file mode 100644 index 170c2d3..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 932de5e7a487475aa764dd819cc33aa0 -timeCreated: 1656583186 \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs deleted file mode 100644 index 05fa288..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs +++ /dev/null @@ -1,23 +0,0 @@ -using UnityEditor; -using UnityEditor.UIElements; -using UnityEngine; -using UnityEngine.UIElements; - -namespace TNode.Editor.Inspector.InspectorImplementation{ - public class PropertyFieldItem:InspectorItem{ - - public PropertyFieldItem(){ - - - OnDataChanged += () => { - var data = new SerializedObject(Value as Object); - var testProperty = data.GetIterator().GetArrayElementAtIndex(0); - PropertyField propertyField = new PropertyField(testProperty); - this.Q()?.RemoveFromHierarchy(); - this.Add(propertyField); - - }; - } - - } -} \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs.meta b/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs.meta deleted file mode 100644 index bced887..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/PropertyFieldItem.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 98769c8b285d438197820fa366568fee -timeCreated: 1657280625 \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs deleted file mode 100644 index 65d9ed9..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs +++ /dev/null @@ -1,14 +0,0 @@ -using TNode.Attribute; -using UnityEngine.UIElements; - -namespace TNode.Editor.Inspector.InspectorImplementation{ - /// - /// Force these element to bind native c# property - /// - [NodeComponent] - public class StringFieldItem:InspectorItem{ - public StringFieldItem():base(){ - CreateBindable(new TextField()); - } - } -} \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs.meta b/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs.meta deleted file mode 100644 index c837e6d..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 6b4f88e6c094449280ba5e38cb508287 -timeCreated: 1656143219 \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs b/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs deleted file mode 100644 index ce0e540..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs +++ /dev/null @@ -1,12 +0,0 @@ -using TNode.Attribute; -using UnityEngine.UIElements; - -namespace TNode.Editor.Inspector.InspectorImplementation{ - [NodeComponent] - public class ToggleFieldItem:InspectorItem{ - public ToggleFieldItem(){ - CreateBindable(new Toggle()); - } - - } -} \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs.meta b/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs.meta deleted file mode 100644 index 7ab36a8..0000000 --- a/TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: d009d4819d604971976932b1d8f40bad -timeCreated: 1656580623 \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorItem.cs b/TNode/Editor/Inspector/InspectorItem.cs deleted file mode 100644 index 8802bb5..0000000 --- a/TNode/Editor/Inspector/InspectorItem.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System; -using TNode.BaseViews; -using TNode.Models; -using UnityEngine; -using UnityEngine.UIElements; - -namespace TNode.Editor.Inspector{ - public abstract class InspectorItem:VisualElement,INodeDataBinding { - protected NodeData _bindingNodeData; - protected string _bindingFieldName; - protected BaseField Bindable; - protected event System.Action OnDataChanged; - - public string BindingPath{ - get => _bindingFieldName; - set{ - _bindingFieldName = value; - if(_bindingFieldName!=null&&_bindingNodeData!=null){ - OnDataChanged?.Invoke(); - } - } - } - - public NodeData BindingNodeData{ - get => _bindingNodeData; - set{ - var oldWrapper = ((NodeDataWrapper) _bindingNodeData); - if(oldWrapper!=null){ - oldWrapper.OnValueChanged -= OnNodeDataValueChanged; - } - _bindingNodeData = value; - if(_bindingFieldName!=null&&_bindingNodeData!=null){ - OnDataChanged?.Invoke(); - } - if(_bindingNodeData!=null) - ((NodeDataWrapper) _bindingNodeData).OnValueChanged += OnNodeDataValueChanged; - } - } - - private T GetValue(){ - - var fieldInfo = _bindingNodeData.GetType().GetField(BindingPath, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); - if (fieldInfo == null){ - throw new Exception("Null field info"); - } - if (fieldInfo.FieldType == typeof(T)){ - return (T)fieldInfo.GetValue(BindingNodeData); - } - - if (fieldInfo.FieldType.IsEnum){ - return (T)fieldInfo.GetValue(BindingNodeData); - } - Debug.LogError("Wrong Type for current node data"); - return default; - } - - protected T Value => GetValue(); - - protected void SetValue(T value){ - NodeDataWrapper wrapper = _bindingNodeData; - wrapper.SetValue(BindingPath,value); - } - public InspectorItem(){ - - OnDataChanged+= OnDataChangedHandler; - } - /* - * e => { - SetValue(e.newValue); - } - */ - private void OnInspectorItemValueChanged(ChangeEvent e){ - SetValue(e.newValue); - } - - public void CreateBindable(BaseField bindable){ - if (Bindable != null){ - Bindable.Clear(); - Bindable.UnregisterValueChangedCallback(OnInspectorItemValueChanged); - } - Bindable = bindable; - Add(Bindable); - Bindable?.RegisterValueChangedCallback(OnInspectorItemValueChanged); - } - private void OnDataChangedHandler(){ - Bindable = this.Q>(); - if(Bindable!= null){ - Bindable.value = Value; - Bindable.label = BindingPath; - } - } - - private void OnNodeDataValueChanged(NodeDataWrapper wrapper){ - var value = (T) wrapper.GetValue(BindingPath) ; - if(Bindable!=null){ - Bindable.value = value; - } - } - - ~InspectorItem(){ - OnDataChanged-= OnDataChangedHandler; - } - } -} \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorItem.cs.meta b/TNode/Editor/Inspector/InspectorItem.cs.meta deleted file mode 100644 index 9008d3f..0000000 --- a/TNode/Editor/Inspector/InspectorItem.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 82902f281e4642f2be8b742866d38839 -timeCreated: 1656126272 \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorItemFactory.cs b/TNode/Editor/Inspector/InspectorItemFactory.cs deleted file mode 100644 index 9e5f9a7..0000000 --- a/TNode/Editor/Inspector/InspectorItemFactory.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using TNode.Cache; -using TNode.Editor.Inspector.InspectorImplementation; -using Unity.VisualScripting; -using UnityEditor; -using UnityEngine; -using UnityEngine.UIElements; - -namespace TNode.Editor.Inspector{ - public class InspectorItemFactory{ - - public InspectorItem Create(){ - //Check type of GraphDataType - var hasSpecificType = NodeEditorExtensions.HasSpecificTypeComponent>(); - - if (hasSpecificType){ - return NodeEditorExtensions.CreateNodeComponentFromGenericType>(); - } - - if (typeof(T).IsEnum){ - - return NodeEditorExtensions.CreateNodeComponentFromGenericType(typeof(InspectorItem)) as InspectorItem; - } - return null; - } - - public INodeDataBindingBase Create(Type t){ - var genericType = typeof(InspectorItem<>).MakeGenericType(t); - var hasSpecificType = NodeEditorExtensions.HasSpecificTypeComponent(genericType); - - if (hasSpecificType){ - return NodeEditorExtensions.CreateNodeComponentFromGenericType(genericType) as INodeDataBindingBase; - } - - if (t.IsEnum){ - - return NodeEditorExtensions.CreateNodeComponentFromGenericType(typeof(InspectorItem)) as INodeDataBindingBase; - } - return null; - } - - } -} - \ No newline at end of file diff --git a/TNode/Editor/Inspector/InspectorItemFactory.cs.meta b/TNode/Editor/Inspector/InspectorItemFactory.cs.meta deleted file mode 100644 index f9838bb..0000000 --- a/TNode/Editor/Inspector/InspectorItemFactory.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 83b9e37f79cf4a18b265e3e22e7e3ced -timeCreated: 1656142463 \ No newline at end of file diff --git a/TNode/Editor/Inspector/MonoScriptInspector.cs b/TNode/Editor/Inspector/MonoScriptInspector.cs deleted file mode 100644 index a06049a..0000000 --- a/TNode/Editor/Inspector/MonoScriptInspector.cs +++ /dev/null @@ -1,16 +0,0 @@ -using UnityEditor; -using UnityEditor.AssetImporters; -using UnityEngine; - -namespace TNode.Editor.Inspector{ - // [CustomEditor(typeof(MonoImporter))] - // public class MonoScriptInspector:AssetImporterEditor{ - // public override void OnInspectorGUI(){ - // base.OnInspectorGUI(); - // if(GUILayout.Button("Open")){ - // EditorUtility.OpenWithDefaultApp(AssetDatabase.GetAssetPath(target)); - // } - // } - // } - // -} \ No newline at end of file diff --git a/TNode/Editor/Inspector/MonoScriptInspector.cs.meta b/TNode/Editor/Inspector/MonoScriptInspector.cs.meta deleted file mode 100644 index 9441899..0000000 --- a/TNode/Editor/Inspector/MonoScriptInspector.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 192a51f6578144c5bbddb5cf77685c71 -timeCreated: 1656214219 \ No newline at end of file diff --git a/TNode/Editor/Inspector/NodeInspectorInNode.cs b/TNode/Editor/Inspector/NodeInspectorInNode.cs deleted file mode 100644 index 9a7c53c..0000000 --- a/TNode/Editor/Inspector/NodeInspectorInNode.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.Reflection; -using TNode.Attribute; -using TNode.Models; -using UnityEngine; -using UnityEngine.UIElements; - -namespace TNode.Editor.Inspector{ - public class NodeInspectorInNode:VisualElement{ - private NodeData _data; - public NodeData Data{ - get => _data; - set{ - _data = value; - UpdateData(); - - } - } - - private void UpdateData(){ - if (_data != null){ - RefreshInspector(); - } - } - - private void RefreshInspector(){ - Clear(); - 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) - continue; - var createdItem = inspectorItemFactory.Create(type); - if (createdItem is { } castedItem){ - castedItem.BindingNodeData = _data; - castedItem.BindingPath = bindingPath; - } - Add((VisualElement)createdItem); - - } - } - } -} \ No newline at end of file diff --git a/TNode/Editor/Manipulators.meta b/TNode/Editor/Manipulators.meta deleted file mode 100644 index f9c9614..0000000 --- a/TNode/Editor/Manipulators.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 400542f3cec140e2b55e2bcf637b2d9b -timeCreated: 1657009018 \ No newline at end of file diff --git a/TNode/Editor/NodeGraphView.meta b/TNode/Editor/NodeGraphView.meta new file mode 100644 index 0000000..007e02c --- /dev/null +++ b/TNode/Editor/NodeGraphView.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9e377285943b43278ea539231483fe6f +timeCreated: 1657683945 \ No newline at end of file diff --git a/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs new file mode 100644 index 0000000..e851505 --- /dev/null +++ b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs @@ -0,0 +1,11 @@ +using TNode.Models; +using UnityEngine; + +namespace TNode.Editor.NodeGraphView{ + public interface IBaseDataGraphView{ + public void AddTNode(NodeData nodeData, Rect rect); + public void RemoveTNode(NodeData nodeData); + + public BlackboardData GetBlackboardData(); + } +} \ No newline at end of file diff --git a/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs.meta b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs.meta new file mode 100644 index 0000000..fada334 --- /dev/null +++ b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9839105141d14b3aac3891bcaaa3fe50 +timeCreated: 1657684189 \ No newline at end of file diff --git a/TNode/Editor/NodeGraphView/IDataGraphView.cs b/TNode/Editor/NodeGraphView/IDataGraphView.cs new file mode 100644 index 0000000..db2d69b --- /dev/null +++ b/TNode/Editor/NodeGraphView/IDataGraphView.cs @@ -0,0 +1,7 @@ +using TNode.Models; + +namespace TNode.Editor.NodeGraphView{ + public interface IDataGraphView : IBaseDataGraphView where T:GraphData{ + + } +} \ No newline at end of file diff --git a/TNode/Editor/NodeGraphView/IDataGraphView.cs.meta b/TNode/Editor/NodeGraphView/IDataGraphView.cs.meta new file mode 100644 index 0000000..9b4ddd6 --- /dev/null +++ b/TNode/Editor/NodeGraphView/IDataGraphView.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b0f8421124864a7d993bcbfb419987dd +timeCreated: 1657684182 \ No newline at end of file diff --git a/TNode/Editor/NodeViews/DefaultNodeView.cs b/TNode/Editor/NodeViews/DefaultNodeView.cs deleted file mode 100644 index 10f4713..0000000 --- a/TNode/Editor/NodeViews/DefaultNodeView.cs +++ /dev/null @@ -1,11 +0,0 @@ -using TNode.BaseViews; -using TNode.Editor.BaseViews; -using TNode.Models; - -namespace TNode.Editor{ - - - public class DefaultNodeView:NodeView{ - - } -} \ 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 diff --git a/TNodeGraphViewImpl.meta b/TNodeGraphViewImpl.meta new file mode 100644 index 0000000..b9e7a1d --- /dev/null +++ b/TNodeGraphViewImpl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 792d2c6b4dca441981787b922b385f0a +timeCreated: 1657684260 \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor.meta b/TNodeGraphViewImpl/Editor.meta new file mode 100644 index 0000000..dabd39a --- /dev/null +++ b/TNodeGraphViewImpl/Editor.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1e6ee4cc74d84496a099ac0624bda536 +timeCreated: 1657684324 \ No newline at end of file diff --git a/TNode/Editor/GraphBlackboard.meta b/TNodeGraphViewImpl/Editor/GraphBlackboard.meta similarity index 100% rename from TNode/Editor/GraphBlackboard.meta rename to TNodeGraphViewImpl/Editor/GraphBlackboard.meta diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs new file mode 100644 index 0000000..93470ef --- /dev/null +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs @@ -0,0 +1,12 @@ +using UnityEditor.Experimental.GraphView; + +namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ + public class BlackboardPropertyField:BlackboardField{ + public BlackboardProperty.BlackboardProperty BlackboardProperty; + public BlackboardPropertyField(BlackboardProperty.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/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs.meta similarity index 100% rename from TNode/Editor/GraphBlackboard/BlackboardField.cs.meta rename to TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardField.cs.meta diff --git a/TNode/Editor/GraphBlackboard/BlackboardProperty.meta b/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardProperty.meta similarity index 100% rename from TNode/Editor/GraphBlackboard/BlackboardProperty.meta rename to TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardProperty.meta diff --git a/TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs similarity index 85% rename from TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs rename to TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs index d7dff1a..eaa5f89 100644 --- a/TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs @@ -1,6 +1,6 @@ using System; -namespace TNode.Editor.GraphBlackboard{ +namespace TNodeGraphViewImpl.Editor.GraphBlackboard.BlackboardProperty{ public class BlackboardProperty{ public string PropertyName; public Type PropertyType; diff --git a/TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs.meta b/TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs.meta similarity index 100% rename from TNode/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs.meta rename to TNodeGraphViewImpl/Editor/GraphBlackboard/BlackboardProperty/BlackboardProperty.cs.meta diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs new file mode 100644 index 0000000..2ed561f --- /dev/null +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs @@ -0,0 +1,17 @@ +using TNode.Attribute; +using TNode.Models; + +namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ + [ViewComponent] + public class DefaultGraphBlackboardView:GraphBlackboardView{ + public DefaultGraphBlackboardView(){ + + } + public void ConstructView(){ + + } + public void AddParameter(){ + + } + } +} \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs.meta b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs.meta new file mode 100644 index 0000000..3b6d406 --- /dev/null +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 98e4de339ad84949ac2c50e61a108a96 +timeCreated: 1657592333 \ No newline at end of file diff --git a/TNode/Editor/GraphBlackboard/GraphBlackboard.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs similarity index 52% rename from TNode/Editor/GraphBlackboard/GraphBlackboard.cs rename to TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs index 40b9e3d..05fe3f5 100644 --- a/TNode/Editor/GraphBlackboard/GraphBlackboard.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs @@ -1,12 +1,15 @@ using TNode.Models; using UnityEditor.Experimental.GraphView; -namespace TNode.Editor.GraphBlackboard{ +namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ /// /// Implement this class to create graph black board for specified graph /// - public class GraphBlackboard:Blackboard where T:BlackboardData{ + public class GraphBlackboardView:Blackboard where T:BlackboardData{ public T BlackboardData; - + + public GraphBlackboardView() : base(){ + + } } } \ No newline at end of file diff --git a/TNode/Editor/GraphBlackboard/GraphBlackboard.cs.meta b/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs.meta similarity index 100% rename from TNode/Editor/GraphBlackboard/GraphBlackboard.cs.meta rename to TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs.meta diff --git a/TNodeGraphViewImpl/Editor/Inspector.meta b/TNodeGraphViewImpl/Editor/Inspector.meta new file mode 100644 index 0000000..11bcf1a --- /dev/null +++ b/TNodeGraphViewImpl/Editor/Inspector.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 152e46659fee4d2fb09bc06f18d54c98 +timeCreated: 1657684641 \ No newline at end of file diff --git a/TNode/Editor/Inspector/NodeInspector.cs b/TNodeGraphViewImpl/Editor/Inspector/NodeInspector.cs similarity index 61% rename from TNode/Editor/Inspector/NodeInspector.cs rename to TNodeGraphViewImpl/Editor/Inspector/NodeInspector.cs index 1d2c516..0367625 100644 --- a/TNode/Editor/Inspector/NodeInspector.cs +++ b/TNodeGraphViewImpl/Editor/Inspector/NodeInspector.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.Reflection; using TNode.Attribute; -using TNode.BaseViews; -using TNode.Editor.BaseViews; +using TNode.Editor.NodeViews; using TNode.Models; +using TNodeGraphViewImpl.Editor.NodeGraphView; using Unity.VisualScripting; using UnityEditor; using UnityEditor.Experimental.GraphView; @@ -24,7 +24,7 @@ namespace TNode.Editor.Inspector{ } } - public INodeView NodeView; + public IBaseNodeView BaseNodeView; private void UpdateData(){ Debug.Log(_data); if (_data != null){ @@ -46,18 +46,18 @@ namespace TNode.Editor.Inspector{ var body = this.Q("InspectorBody"); body.Clear(); body.StretchToParentSize(); - foreach (var field in _data.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)){ - var bindingPath = field.Name; - var type = field.FieldType; - InspectorItemFactory inspectorItemFactory = new InspectorItemFactory(); - //Invoke generic function Create<> of default inspector item factory to create an inspector item of appropriate type by reflection - var createdItem = inspectorItemFactory.Create(type); - if (createdItem is { } castedItem){ - castedItem.BindingNodeData = _data; - castedItem.BindingPath = bindingPath; - } - Add((VisualElement)createdItem); - } + // foreach (var field in _data.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)){ + // var bindingPath = field.Name; + // var type = field.FieldType; + // InspectorItemFactory inspectorItemFactory = new InspectorItemFactory(); + // //Invoke generic function Create<> of default inspector item factory to create an inspector item of appropriate type by reflection + // var createdItem = inspectorItemFactory.Create(type); + // if (createdItem is { } castedItem){ + // castedItem.BindingNodeData = _data; + // castedItem.BindingPath = bindingPath; + // } + // Add((VisualElement)createdItem); + // } } } } \ No newline at end of file diff --git a/TNode/Editor/Inspector/NodeInspector.cs.meta b/TNodeGraphViewImpl/Editor/Inspector/NodeInspector.cs.meta similarity index 100% rename from TNode/Editor/Inspector/NodeInspector.cs.meta rename to TNodeGraphViewImpl/Editor/Inspector/NodeInspector.cs.meta diff --git a/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs b/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs new file mode 100644 index 0000000..de375a1 --- /dev/null +++ b/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs @@ -0,0 +1,57 @@ +using System.Reflection; +using TNode.Attribute; +using TNode.Models; +using UnityEditor; +using UnityEditor.UIElements; +using UnityEngine; +using UnityEngine.UIElements; + +namespace TNode.Editor.Inspector{ + public class NodeInspectorInNode:VisualElement{ + private NodeData _data; + public NodeData Data{ + get => _data; + set{ + _data = value; + UpdateData(); + + } + + } + + public NodeInspectorInNode():base(){ + } + private void UpdateData(){ + if (_data != null){ + RefreshInspector(); + } + } + + 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); + } + } + } +} \ No newline at end of file diff --git a/TNode/Editor/Inspector/NodeInspectorInNode.cs.meta b/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs.meta similarity index 100% rename from TNode/Editor/Inspector/NodeInspectorInNode.cs.meta rename to TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs.meta diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView.meta b/TNodeGraphViewImpl/Editor/NodeGraphView.meta new file mode 100644 index 0000000..eb09718 --- /dev/null +++ b/TNodeGraphViewImpl/Editor/NodeGraphView.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 509019c594fa494dba98f9910092b63e +timeCreated: 1657684502 \ No newline at end of file diff --git a/TNode/Editor/BaseViews/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs similarity index 69% rename from TNode/Editor/BaseViews/DataGraphView.cs rename to TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index b66b150..bcc280c 100644 --- a/TNode/Editor/BaseViews/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -1,116 +1,27 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; -using TNode.BaseViews; using TNode.Cache; -using TNode.Editor.GraphBlackboard; +using TNode.Editor; using TNode.Editor.Inspector; using TNode.Editor.Model; +using TNode.Editor.NodeGraphView; +using TNode.Editor.NodeViews; +using TNode.Editor.Search; using TNode.Editor.Tools.NodeCreator; using TNode.Models; -using Unity.VisualScripting; +using TNodeGraphViewImpl.Editor.GraphBlackboard; +using TNodeGraphViewImpl.Editor.GraphBlackboard.BlackboardProperty; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEngine; using UnityEngine.UIElements; using Edge = UnityEditor.Experimental.GraphView.Edge; -namespace TNode.Editor.BaseViews{ - /* - public class DialogueGraphView : DataGraphView{ - public Action onNodeAdded; - public Action onNodeSelected; - public Action onNodeRemoved; - public Action onNodeUnselected; - // public DialogueGraphView(DialogueGraph graph):base(){ - // this.Data = graph; - // - // //Set background to a bit of darker - // - // - // //Register a data context change callback - // - // } - - public override void OnGraphViewCreate(){ - AddNode(GenerateEntryPoint()); - RegisterCallback(evt => { - var pos = evt.mousePosition; - - evt.menu.AppendAction("Add NodeAttribute", (dropMenuAction) => { - DialogueNodeView nodeView = new DialogueNodeView{ - GUID = Guid.NewGuid().ToString(), - title = "New NodeAttribute" - }; - // make it a 200x100 box - nodeView.SetPosition(new Rect(pos.x - 100, pos.y - 50, 200, 100)); - - - AddNode(nodeView); - }, DropdownMenuAction.AlwaysEnabled); - }); - this.OnDataChanged += OnOnDataChanged; - } - private void OnOnDataChanged(object sender, DataChangedEventArgs e){ - //clean all nodes from the graphview - foreach (var graphViewNode in nodes){ - RemoveElement(graphViewNode); - } - - foreach (var edge in edges){ - RemoveElement(edge); - } - //add all nodes from the new graph - foreach (var node in e.NewData.nodes){ - //AddNode(node); - } - } - - public void AddNode(DialogueNodeData dialogueNodeData){ - var res = InstantiateFromDialogueNodeData(dialogueNodeData); - AddNode(res); - } - public void AddNode(DialogueNodeView nodeView){ - AddElement(nodeView); - onNodeAdded?.Invoke(nodeView); - //Register nodeView selection callback - nodeView.RegisterCallback(evt => { - if (evt.clickCount == 1){ - onNodeSelected?.Invoke(nodeView); - } - }); - nodeView.OnUnselect += () => { onNodeUnselected?.Invoke(nodeView); }; - } - - public override List GetCompatiblePorts(Port startPort, NodeAdapter nodeAdapter) => this.ports.ToList() - .Where(x => x != startPort && - x.direction != startPort.direction).ToList(); - - public DialogueNodeView GenerateEntryPoint(){ - var entryPoint = new DialogueNodeView{ - title = "Entry Point", - GUID = Guid.NewGuid().ToString(), - EntryPoint = true - }; - //Add output port to the nodeView - entryPoint.AddPort(Orientation.Horizontal, Direction.Output, "Next"); - //Set nodeView position to top center side of screen - entryPoint.SetPosition(new Rect(this.layout.width / 2 - 100, 0, 200, 200)); - return entryPoint; - } - protected DialogueNodeView InstantiateFromDialogueNodeData(DialogueNodeData dialogueNodeData){ - var node = new DialogueNodeView(); - node.title = dialogueNodeData.nodeName; - node.GUID = Guid.NewGuid().ToString(); - //TODO:after completing the separation of the node data and the node editor data,this should be switch to the node editor data - //node.SetPosition(dialogueNodeData.rect); - this.AddNode(node); - return node; - } - } - */ - public abstract class DataGraphView:GraphView,IDataGraphView where T:GraphData{ +namespace TNodeGraphViewImpl.Editor.NodeGraphView{ + public abstract class BaseDataGraphView:GraphView,IBaseDataGraphView where T:GraphData{ #region variables and properties private T _data; private bool _isInspectorOn; @@ -136,11 +47,11 @@ namespace TNode.Editor.BaseViews{ #endregion - //A Constructor for the DataGraphView ,never to override it + //A Constructor for the BaseDataGraphView ,never to override it #region construct default behaviour - public DataGraphView(){ + public BaseDataGraphView(){ styleSheets.Add(Resources.Load("GraphViewBackground")); var grid = new GridBackground(); Insert(0,grid); @@ -281,20 +192,11 @@ namespace TNode.Editor.BaseViews{ } public virtual void CreateBlackboard(){ - _blackboard = new Blackboard(); - //Blackboard add "Add Node" button - // blackboard.Add(new BlackboardSection(){ - // title = "Hello World", - // }); - // blackboard.addItemRequested = (item) => { - // //Create a sub window for the blackboard to show the selection - // var subWindow = ScriptableObject.CreateNodeComponentFromGenericType(); - // }; - // - //Set black board to left side of the view + + _blackboard = NodeEditorExtensions.CreateBlackboardWithGraphData(typeof(T)); + _blackboard.SetPosition(new Rect(0,0,200,600)); Add(_blackboard); - //Check the type of the blackboard OnDataChanged+= (sender, e) => { BlackboardUpdate(); }; @@ -306,14 +208,30 @@ namespace TNode.Editor.BaseViews{ if (_data.blackboardData == null) return; } - + //Iterate field of the blackboard and add a button for each field foreach (var field in _data.blackboardData.GetType() .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){ - //if the field is MonoBehaviour,add a property field for blackboard - var propertyField = new BlackboardPropertyField(new BlackboardProperty(field.Name,field.FieldType)); - _blackboard.Add(propertyField); + //if the field is MonoBehaviour,add a property field for blackboard + //skip if the field is a list or Ilist + if (!typeof(IList).IsAssignableFrom(field.FieldType)){ + var propertyField = new BlackboardPropertyField(new BlackboardProperty(field.Name,field.FieldType)); + _blackboard.Add(propertyField); + } + } + _blackboard.addItemRequested = (sender) => { + var res = ScriptableObject.CreateInstance(); + + //Get right top corner of the blackboard + var blackboardPos = _blackboard.GetPosition().position; + var searchWindowContext = new SearchWindowContext(blackboardPos,200,200); + //Call search window + res.Setup(typeof(T),this,Owner); + + SearchWindow.Open(searchWindowContext, res); + }; + } public virtual void DestroyInspector(){ @@ -341,7 +259,7 @@ namespace TNode.Editor.BaseViews{ var nodeEditorData = new GraphElementEditorData{ pos = node.GetPosition(), }; - if (node is INodeView nodeView){ + if (node is IBaseNodeView nodeView){ nodeEditorData.guid = nodeView.GetNodeData().id; } graphEditorData.graphElementsData.Add(nodeEditorData); @@ -357,7 +275,7 @@ namespace TNode.Editor.BaseViews{ private void SaveNode(){ foreach (var node in nodes){ - if (node is INodeView nodeView){ + if (node is IBaseNodeView nodeView){ var nodeData = nodeView.GetNodeData(); if (!_data.NodeDictionary.ContainsKey(nodeData.id)){ _data.NodeDictionary.Add(nodeData.id, nodeData); @@ -368,8 +286,8 @@ namespace TNode.Editor.BaseViews{ private void SaveEdge(){ var links = new List(); foreach (var edge in edges){ - var inputNode = edge.input.node as INodeView; - var outputNode = edge.output.node as INodeView; + var inputNode = edge.input.node as IBaseNodeView; + var outputNode = edge.output.node as IBaseNodeView; if (inputNode != null && outputNode != null){ var inputNodeData = inputNode.GetNodeData(); var outputNodeData = outputNode.GetNodeData(); @@ -408,7 +326,7 @@ namespace TNode.Editor.BaseViews{ public virtual void OnGraphViewDestroy(){ } - ~DataGraphView(){ + ~BaseDataGraphView(){ OnGraphViewDestroy(); } @@ -425,11 +343,11 @@ namespace TNode.Editor.BaseViews{ if (evt.clickCount == 1){ if (_isInspectorOn){ _nodeInspector.Data = nodeData; - _nodeInspector.NodeView = nodeView as INodeView; + _nodeInspector.BaseNodeView = nodeView as IBaseNodeView; } } }); - if(nodeView is INodeView nodeViewInterface){ + if(nodeView is IBaseNodeView nodeViewInterface){ nodeViewInterface.SetNodeData(nodeData); } _nodeDict.Add(nodeData.id, nodeView); @@ -439,7 +357,7 @@ namespace TNode.Editor.BaseViews{ var menu = new GenericMenu(); menu.AddItem(new GUIContent("Delete"), false, () => { RemoveElement(nodeView); - if (nodeView is INodeView tNodeView){ + if (nodeView is IBaseNodeView tNodeView){ RemoveTNode(tNodeView.GetNodeData()); } }); @@ -468,12 +386,6 @@ namespace TNode.Editor.BaseViews{ } } - public interface IDataGraphView{ - public void AddTNode(NodeData nodeData, Rect rect); - public void RemoveTNode(NodeData nodeData); - - public BlackboardData GetBlackboardData(); - } public class DataChangedEventArgs{ public DataChangedEventArgs(T data){ diff --git a/TNode/Editor/BaseViews/DataGraphView.cs.meta b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs.meta similarity index 100% rename from TNode/Editor/BaseViews/DataGraphView.cs.meta rename to TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs.meta diff --git a/TNode/Editor/BaseViews/SimpleGraphSubWindow.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/SimpleGraphSubWindow.cs similarity index 97% rename from TNode/Editor/BaseViews/SimpleGraphSubWindow.cs rename to TNodeGraphViewImpl/Editor/NodeGraphView/SimpleGraphSubWindow.cs index da03952..da69ff1 100644 --- a/TNode/Editor/BaseViews/SimpleGraphSubWindow.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/SimpleGraphSubWindow.cs @@ -5,7 +5,7 @@ using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEngine.UIElements; -namespace TNode.BaseViews{ +namespace TNodeGraphViewImpl.Editor.NodeGraphView{ public class SimpleGraphSubWindow:GraphElement,IGraphViewPersistence{ private readonly Dragger _dragger = new Dragger(); diff --git a/TNode/Editor/BaseViews/SimpleGraphSubWindow.cs.meta b/TNodeGraphViewImpl/Editor/NodeGraphView/SimpleGraphSubWindow.cs.meta similarity index 100% rename from TNode/Editor/BaseViews/SimpleGraphSubWindow.cs.meta rename to TNodeGraphViewImpl/Editor/NodeGraphView/SimpleGraphSubWindow.cs.meta diff --git a/TNode/Editor/NodeViews.meta b/TNodeGraphViewImpl/Editor/NodeViews.meta similarity index 100% rename from TNode/Editor/NodeViews.meta rename to TNodeGraphViewImpl/Editor/NodeViews.meta diff --git a/TNodeGraphViewImpl/Editor/NodeViews/DefaultNodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/DefaultNodeView.cs new file mode 100644 index 0000000..caec25e --- /dev/null +++ b/TNodeGraphViewImpl/Editor/NodeViews/DefaultNodeView.cs @@ -0,0 +1,10 @@ +using TNode.Editor.NodeViews; +using TNode.Models; + +namespace TNode.Editor{ + + + public class DefaultBaseNodeView:BaseNodeView{ + + } +} \ No newline at end of file diff --git a/TNode/Editor/NodeViews/DefaultNodeView.cs.meta b/TNodeGraphViewImpl/Editor/NodeViews/DefaultNodeView.cs.meta similarity index 100% rename from TNode/Editor/NodeViews/DefaultNodeView.cs.meta rename to TNodeGraphViewImpl/Editor/NodeViews/DefaultNodeView.cs.meta diff --git a/TNode/Editor/NodeViews/DragNodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs similarity index 60% rename from TNode/Editor/NodeViews/DragNodeView.cs rename to TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs index 8ce31c7..dbcd21c 100644 --- a/TNode/Editor/NodeViews/DragNodeView.cs +++ b/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs @@ -1,11 +1,10 @@ using TNode.Attribute; -using TNode.Editor.BaseViews; using TNode.Models; namespace TNode.Editor.NodeViews{ - [NodeComponent] - public class DragNodeView:NodeView>{ - public DragNodeView() : base(){ + [ViewComponent] + public class DragBaseNodeView:BaseNodeView>{ + public DragBaseNodeView() : base(){ //Make capsule like style this.titleContainer.visible = false; diff --git a/TNode/Editor/NodeViews/DragNodeView.cs.meta b/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs.meta similarity index 100% rename from TNode/Editor/NodeViews/DragNodeView.cs.meta rename to TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs.meta diff --git a/TNode/Editor/NodeViews/NodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs similarity index 94% rename from TNode/Editor/NodeViews/NodeView.cs rename to TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs index 0079cb6..5cdc716 100644 --- a/TNode/Editor/NodeViews/NodeView.cs +++ b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs @@ -5,17 +5,13 @@ using TNode.Attribute; using TNode.Attribute.Ports; using TNode.Editor.Inspector; using TNode.Models; -using UnityEditor; using UnityEditor.Experimental.GraphView; -using UnityEditor.UIElements; using UnityEngine; using UnityEngine.UIElements; -namespace TNode.Editor.BaseViews{ +namespace TNode.Editor.NodeViews{ - //A NodeAttribute monitor some type of node in the graph - - public abstract class NodeView : Node,INodeView where T:NodeData,new(){ + public abstract class BaseNodeView : Node,INodeView where T:NodeData,new(){ protected T _data; private readonly NodeInspectorInNode _nodeInspectorInNode; @@ -42,7 +38,7 @@ namespace TNode.Editor.BaseViews{ } public event System.Action OnDataChanged; - protected NodeView(){ + protected BaseNodeView(){ OnDataChanged+=OnDataChangedHandler; _nodeInspectorInNode = new NodeInspectorInNode(){ @@ -69,10 +65,8 @@ namespace TNode.Editor.BaseViews{ switch (portAttribute.NameHandling){ case PortNameHandling.Auto: return portAttribute.Name.Trim(' ').Length>0?portAttribute.Name:propertyInfo.Name; - break; case PortNameHandling.Manual: return portAttribute.Name; - break; case PortNameHandling.MemberName: return propertyInfo.Name; case PortNameHandling.Format: @@ -169,11 +163,15 @@ namespace TNode.Editor.BaseViews{ } } - public interface INodeView{ + public interface IBaseNodeView{ public void SetNodeData(NodeData nodeData); public NodeData GetNodeData(); - public void OnDataModified(); } + + public interface INodeView:IBaseNodeView where T:NodeData,new(){ + public T Data{ get; set; } + + } } \ No newline at end of file diff --git a/TNode/Editor/NodeViews/NodeView.cs.meta b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs.meta similarity index 100% rename from TNode/Editor/NodeViews/NodeView.cs.meta rename to TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs.meta diff --git a/TNode/Editor/Search.meta b/TNodeGraphViewImpl/Editor/Search.meta similarity index 100% rename from TNode/Editor/Search.meta rename to TNodeGraphViewImpl/Editor/Search.meta diff --git a/TNode/Editor/Search/BlackboardSearchWindowProvider.cs b/TNodeGraphViewImpl/Editor/Search/BlackboardSearchWindowProvider.cs similarity index 72% rename from TNode/Editor/Search/BlackboardSearchWindowProvider.cs rename to TNodeGraphViewImpl/Editor/Search/BlackboardSearchWindowProvider.cs index acfbc82..ac91d2e 100644 --- a/TNode/Editor/Search/BlackboardSearchWindowProvider.cs +++ b/TNodeGraphViewImpl/Editor/Search/BlackboardSearchWindowProvider.cs @@ -1,15 +1,15 @@ using System; using System.Collections; using System.Collections.Generic; -using TNode.Editor.BaseViews; +using TNode.Editor.NodeGraphView; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEngine; -namespace TNode.Editor{ - public class BlackboardSearchWindowProvider:ISearchWindowProvider{ +namespace TNode.Editor.Search{ + public class BlackboardSearchWindowProvider:ScriptableObject,ISearchWindowProvider{ private Type _graphType; - private IDataGraphView _graphView; + private IBaseDataGraphView _graphView; private EditorWindow _editor; private struct InternalSearchTreeUserData{ @@ -20,15 +20,19 @@ namespace TNode.Editor{ public List CreateSearchTree(SearchWindowContext context){ var blackboardData = _graphView.GetBlackboardData(); var type = blackboardData.GetType(); - var entries = new List(); - if (entries == null) throw new ArgumentNullException(nameof(entries)); + var list = new List(){ + new SearchTreeGroupEntry(new GUIContent("Add New Blackboard Data"), 0), + }; + + if (list == null) throw new ArgumentNullException(nameof(list)); //search fields with List type Texture2D icon = new Texture2D(2,2); + foreach (var field in type.GetFields()){ if (field.FieldType.IsGenericType){ var genericType = field.FieldType.GetGenericTypeDefinition(); if (genericType == typeof(List<>)){ - entries.Add(new SearchTreeEntry(new GUIContent(field.Name,icon)){ + list.Add(new SearchTreeEntry(new GUIContent(field.Name,icon)){ level = 1, userData = new InternalSearchTreeUserData(){ List = field.GetValue(blackboardData) as IList, @@ -39,28 +43,24 @@ namespace TNode.Editor{ } } } - - return entries; + Debug.Log($"{list.Count}"); + return list; } public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context){ var userData = SearchTreeEntry.userData; - var relativePos = context.screenMousePosition - _editor.position.position; - var blackboardData = _graphView.GetBlackboardData(); - if (userData is InternalSearchTreeUserData){ var list = ((InternalSearchTreeUserData) userData).List; var type = ((InternalSearchTreeUserData) userData).Type; var newItem = Activator.CreateInstance(type); - list.Add(newItem); + list?.Add(newItem); return true; } - return false; } - public void Setup(Type graph,IDataGraphView graphView,EditorWindow editor){ + public void Setup(Type graph,IBaseDataGraphView graphView,EditorWindow editor){ _graphType = graph; _graphView = graphView; _editor = editor; diff --git a/TNode/Editor/Search/BlackboardSearchWindowProvider.cs.meta b/TNodeGraphViewImpl/Editor/Search/BlackboardSearchWindowProvider.cs.meta similarity index 100% rename from TNode/Editor/Search/BlackboardSearchWindowProvider.cs.meta rename to TNodeGraphViewImpl/Editor/Search/BlackboardSearchWindowProvider.cs.meta diff --git a/TNode/Editor/Search/NodeSearchWindowProvider.cs b/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs similarity index 91% rename from TNode/Editor/Search/NodeSearchWindowProvider.cs rename to TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs index e694f4a..058d07b 100644 --- a/TNode/Editor/Search/NodeSearchWindowProvider.cs +++ b/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; -using System.Drawing; -using TNode.BaseViews; using TNode.Cache; -using TNode.Editor.BaseViews; +using TNode.Editor.NodeGraphView; using TNode.Editor.Tools.NodeCreator; using TNode.Models; using UnityEditor; @@ -11,7 +9,7 @@ using UnityEditor.Experimental.GraphView; using UnityEngine; using UnityEngine.UIElements; -namespace TNode.Editor{ +namespace TNode.Editor.Search{ public class NodeSearchWindowProvider:ScriptableObject,ISearchWindowProvider{ private Type _graphType; private GraphView _graphView; @@ -50,7 +48,7 @@ namespace TNode.Editor{ //Make an instance of the type if (NodeCreator.InstantiateNodeData(type) is { } nodeData){ nodeData.nodeName = $"New {type.Name}"; - ((IDataGraphView) _graphView).AddTNode(nodeData, new Rect(localPos.x, localPos.y, 100, 100)); + ((IBaseDataGraphView) _graphView).AddTNode(nodeData, new Rect(localPos.x, localPos.y, 100, 100)); } } return true; diff --git a/TNode/Editor/Search/NodeSearchWindowProvider.cs.meta b/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs.meta similarity index 100% rename from TNode/Editor/Search/NodeSearchWindowProvider.cs.meta rename to TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs.meta