From c8fa58132d1756cf0bd25d4bfdb376920990bb8f Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Sat, 9 Jul 2022 23:57:37 +0800 Subject: [PATCH] feat: add a black board search window for create list data --- TNode/Editor/BaseViews/DataGraphView.cs | 13 +++-- .../Search/BlackboardSearchWindowProvider.cs | 54 ++++++++++++++++++- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/TNode/Editor/BaseViews/DataGraphView.cs b/TNode/Editor/BaseViews/DataGraphView.cs index dac2122..b66b150 100644 --- a/TNode/Editor/BaseViews/DataGraphView.cs +++ b/TNode/Editor/BaseViews/DataGraphView.cs @@ -112,20 +112,13 @@ namespace TNode.Editor.BaseViews{ */ public abstract class DataGraphView:GraphView,IDataGraphView where T:GraphData{ #region variables and properties - - - - private T _data; private bool _isInspectorOn; - private NodeSearchWindowProvider _nodeSearchWindowProvider; private NodeInspector _nodeInspector; public GraphEditor Owner; private Dictionary _nodeDict = new(); private Blackboard _blackboard; - - public T Data{ get{ return _data; } set{ @@ -469,11 +462,17 @@ namespace TNode.Editor.BaseViews{ } Owner.graphEditorData.graphElementsData.RemoveAll(x => x.guid == nodeData.id); } + + public BlackboardData GetBlackboardData(){ + return this._data.blackboardData; + } } public interface IDataGraphView{ public void AddTNode(NodeData nodeData, Rect rect); public void RemoveTNode(NodeData nodeData); + + public BlackboardData GetBlackboardData(); } public class DataChangedEventArgs{ diff --git a/TNode/Editor/Search/BlackboardSearchWindowProvider.cs b/TNode/Editor/Search/BlackboardSearchWindowProvider.cs index ffe6b32..acfbc82 100644 --- a/TNode/Editor/Search/BlackboardSearchWindowProvider.cs +++ b/TNode/Editor/Search/BlackboardSearchWindowProvider.cs @@ -1,20 +1,70 @@ using System; +using System.Collections; using System.Collections.Generic; +using TNode.Editor.BaseViews; using UnityEditor; using UnityEditor.Experimental.GraphView; +using UnityEngine; namespace TNode.Editor{ public class BlackboardSearchWindowProvider:ISearchWindowProvider{ private Type _graphType; - private GraphView _graphView; + private IDataGraphView _graphView; private EditorWindow _editor; + + private struct InternalSearchTreeUserData{ + public IList List; + public Type Type; + + } public List CreateSearchTree(SearchWindowContext context){ - return null; + var blackboardData = _graphView.GetBlackboardData(); + var type = blackboardData.GetType(); + var entries = new List(); + if (entries == null) throw new ArgumentNullException(nameof(entries)); + //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)){ + level = 1, + userData = new InternalSearchTreeUserData(){ + List = field.GetValue(blackboardData) as IList, + Type = field.FieldType.GetGenericArguments()[0] + } + + }); + } + } + } + + return entries; + } 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); + return true; + } + return false; } + + public void Setup(Type graph,IDataGraphView graphView,EditorWindow editor){ + _graphType = graph; + _graphView = graphView; + _editor = editor; + } } } \ No newline at end of file