diff --git a/TNode/Editor/Blackboard/IBlackboardView.cs b/TNode/Editor/Blackboard/IBlackboardView.cs index 6a7e0f0..eca1cf9 100644 --- a/TNode/Editor/Blackboard/IBlackboardView.cs +++ b/TNode/Editor/Blackboard/IBlackboardView.cs @@ -1,7 +1,19 @@ -namespace TNode.Editor.Blackboard{ +using TNode.Editor.NodeGraphView; +using TNode.Models; +using UnityEditor; +using UnityEngine; + +namespace TNode.Editor.Blackboard{ public interface IBlackboardView{ - public void AddData(){ - - } + public BlackboardData GetBlackboardData(); + public void SetBlackboardData(BlackboardData data); + + public void AddItem(); + + void Setup(IBaseDataGraphView graphView,EditorWindow ownerWindow); + } + public interface IBlackboardView : IBlackboardView where T : BlackboardData{ + + public T Data{ get; set; } } } \ No newline at end of file diff --git a/TNode/Editor/EditorPersistence/GraphEditorData.cs b/TNode/Editor/EditorPersistence/GraphEditorData.cs index 02fc71c..9c64722 100644 --- a/TNode/Editor/EditorPersistence/GraphEditorData.cs +++ b/TNode/Editor/EditorPersistence/GraphEditorData.cs @@ -1,10 +1,7 @@ using System.Collections.Generic; -using TNode.Editor.EditorPersistence; -using TNode.Editor.Model; using UnityEngine; -using UnityEngine.Serialization; -namespace TNode.Editor{ +namespace TNode.Editor.EditorPersistence{ [CreateAssetMenu(fileName = "Graph Editor Data", menuName = "TNode/Graph Editor Data")] public class GraphEditorData:ScriptableObject{ diff --git a/TNode/Editor/EditorPersistence/GraphElementEditorData.cs b/TNode/Editor/EditorPersistence/GraphElementEditorData.cs index f21170d..6c8809a 100644 --- a/TNode/Editor/EditorPersistence/GraphElementEditorData.cs +++ b/TNode/Editor/EditorPersistence/GraphElementEditorData.cs @@ -1,9 +1,7 @@ using System; -using TNode.Models; using UnityEngine; -using UnityEngine.Serialization; -namespace TNode.Editor.Model{ +namespace TNode.Editor.EditorPersistence{ [Serializable] public class GraphElementEditorData{ diff --git a/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs index 4800d8b..16289d0 100644 --- a/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs +++ b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs @@ -6,6 +6,8 @@ namespace TNode.Editor.NodeGraphView{ public void AddTNode(NodeData nodeData, Rect rect); public void RemoveTNode(NodeData nodeData); + public void CreateBlackboard(); + public GraphData GetGraphData(); public BlackboardData GetBlackboardData(); diff --git a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs index 145792a..13c2ed3 100644 --- a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs +++ b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs @@ -1,5 +1,6 @@ using System.IO; using System.Text.RegularExpressions; +using TNode.Editor.EditorPersistence; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; diff --git a/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs index 856f0c1..7624080 100644 --- a/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs +++ b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using TNode.Attribute; using TNode.Editor; +using TNode.Editor.Blackboard; using TNode.Editor.NodeViews; using TNode.Models; using TNodeGraphViewImpl.Editor.GraphBlackboard; @@ -151,14 +152,14 @@ namespace TNodeGraphViewImpl.Editor.Cache{ return null; } - public static Blackboard CreateBlackboardDataFromBlackboardDataType(Type t){ + public static IBlackboardView CreateBlackboardDataFromBlackboardDataType(Type t){ var type = typeof(GraphBlackboardView<>).MakeGenericType(t); - var res = CreateViewComponentFromBaseType(type) as Blackboard; + var res = CreateViewComponentFromBaseType(type) as IBlackboardView; return res ?? new DefaultGraphBlackboardView(); } - public static Blackboard CreateBlackboardWithGraphData(GraphData graphData){ + public static IBlackboardView CreateBlackboardWithGraphData(GraphData graphData){ var graphType = graphData.GetType(); if (NodeEditorSingleton.Instance.GraphBlackboard.ContainsKey(graphType)){ var type = NodeEditorSingleton.Instance.GraphBlackboard[graphType]; @@ -167,7 +168,7 @@ namespace TNodeGraphViewImpl.Editor.Cache{ } return null; } - public static Blackboard CreateBlackboardWithGraphData(Type graphType){ + public static IBlackboardView CreateBlackboardWithGraphData(Type graphType){ if (NodeEditorSingleton.Instance.GraphBlackboard.ContainsKey(graphType)){ var type = NodeEditorSingleton.Instance.GraphBlackboard[graphType]; return CreateBlackboardDataFromBlackboardDataType(type); diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs index 2ed561f..4c0d01d 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs @@ -1,17 +1,39 @@ -using TNode.Attribute; +using System.Collections; +using System.Reflection; +using TNode.Attribute; +using TNode.Editor.NodeGraphView; +using TNode.Editor.Search; using TNode.Models; +using UnityEditor; +using UnityEditor.Experimental.GraphView; +using UnityEngine; namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ [ViewComponent] public class DefaultGraphBlackboardView:GraphBlackboardView{ - public DefaultGraphBlackboardView(){ - - } - public void ConstructView(){ - - } - public void AddParameter(){ + protected override void UpdateBlackboard(BlackboardData data){ + + foreach (var field in data.GetType() + .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){ + //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.BlackboardProperty(field.Name,field.FieldType)); + this.Add(propertyField); + } + } + this.addItemRequested = (sender) => { + var res = ScriptableObject.CreateInstance(); + + //Get right top corner of the blackboard + var blackboardPos = GetPosition().position+OwnerWindow.position.position; + var searchWindowContext = new SearchWindowContext(blackboardPos,200,200); + //Call search window + res.Setup(Owner.GetGraphData().GetType(),Owner,OwnerWindow); + SearchWindow.Open(searchWindowContext, res); + }; } + } } \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs index 05fe3f5..8c2644d 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs @@ -1,15 +1,52 @@ -using TNode.Models; +using TNode.Editor.Blackboard; +using TNode.Editor.NodeGraphView; +using TNode.Editor.Search; +using TNode.Models; +using UnityEditor; using UnityEditor.Experimental.GraphView; +using UnityEngine; namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ /// /// Implement this class to create graph black board for specified graph /// - public class GraphBlackboardView:Blackboard where T:BlackboardData{ - public T BlackboardData; + public class GraphBlackboardView:Blackboard,IBlackboardView where T:BlackboardData{ + protected IBaseDataGraphView Owner; + protected EditorWindow OwnerWindow; + private T _data; - public GraphBlackboardView() : base(){ + public void Setup(IBaseDataGraphView graphView,EditorWindow ownerWindow){ + Owner = graphView; + OwnerWindow = ownerWindow; + } + + + + public new void SetPosition(Rect rect){ } + + protected virtual void UpdateBlackboard(BlackboardData data){ + + } + public T Data{ + get => (T) _data; + + set{ + _data = value; + UpdateBlackboard(value); + } + } + public BlackboardData GetBlackboardData(){ + return _data; + } + + public void SetBlackboardData(BlackboardData data){ + Data = (T) data; + } + + public void AddItem(){ + + } } } \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/GraphEditor.cs b/TNodeGraphViewImpl/Editor/GraphEditor.cs index 59c4b11..ade213b 100644 --- a/TNodeGraphViewImpl/Editor/GraphEditor.cs +++ b/TNodeGraphViewImpl/Editor/GraphEditor.cs @@ -1,6 +1,6 @@ using Codice.CM.Common; +using TNode.Editor.EditorPersistence; using TNode.Editor.Inspector; -using TNode.Editor.Model; using TNode.Models; using TNodeGraphViewImpl.Editor.Cache; using TNodeGraphViewImpl.Editor.NodeGraphView; diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index 0a46a2b..ec9a3ac 100644 --- a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -4,8 +4,9 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using TNode.Editor; +using TNode.Editor.Blackboard; +using TNode.Editor.EditorPersistence; using TNode.Editor.Inspector; -using TNode.Editor.Model; using TNode.Editor.NodeGraphView; using TNode.Editor.NodeViews; using TNode.Editor.Search; @@ -29,7 +30,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ private NodeInspector _nodeInspector; public GraphEditor Owner; private Dictionary _nodeDict = new(); - private Blackboard _blackboard; + private IBlackboardView _blackboard; public T Data{ get{ return _data; } set{ @@ -191,47 +192,14 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ miniMap.SetPosition(rect); } - public virtual void CreateBlackboard(){ - - _blackboard = NodeEditorExtensions.CreateBlackboardWithGraphData(typeof(T)); - - _blackboard.SetPosition(new Rect(0,0,200,600)); - Add(_blackboard); - - OnDataChanged+= (sender, e) => { BlackboardUpdate(); }; - - } private void BlackboardUpdate(){ if (_data.blackboardData == null || _data.blackboardData.GetType() == typeof(BlackboardData)){ _data.blackboardData = NodeEditorExtensions.GetAppropriateBlackboardData(_data.GetType()); if (_data.blackboardData == null) return; + _blackboard.SetBlackboardData(_data.blackboardData); } - - //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 - //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(){ @@ -330,10 +298,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ OnGraphViewDestroy(); } - public bool IsDroppable(){ - return true; - } - + #region implement interfaces public void AddTNode(NodeData nodeData, Rect rect){ if (NodeEditorExtensions.CreateNodeViewFromNodeType(nodeData.GetType()) is Node nodeView){ nodeView.SetPosition(rect); @@ -381,9 +346,30 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ Owner.graphEditorData.graphElementsData.RemoveAll(x => x.guid == nodeData.id); } + public void CreateBlackboard(){ + _blackboard = NodeEditorExtensions.CreateBlackboardWithGraphData(typeof(T)); + _blackboard.Setup(this,Owner); + + var castedBlackboard = _blackboard as Blackboard; + + Add(castedBlackboard); + + Rect blackboardPos = new Rect(0,0,200,700); + castedBlackboard?.SetPosition(blackboardPos); + + + OnDataChanged+= (sender, e) => { BlackboardUpdate(); }; + } + + public GraphData GetGraphData(){ + return _data; + } + + public BlackboardData GetBlackboardData(){ return this._data.blackboardData; } + #endregion }