feat: search window now applied correctly

main
taoria 3 years ago
parent 4b792468ec
commit eb12765338
  1. 2
      Sample/MathGraph/Editor/MathGraphView.cs
  2. 2
      TNode/Attribute/ViewComponentAttribute.cs
  3. 0
      TNode/Attribute/ViewComponentAttribute.cs.meta
  4. 37
      TNode/Editor/BaseViews/DataGraphView.cs
  5. 63
      TNode/Editor/Cache/NodeEditorExtensions.cs
  6. 17
      TNode/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs
  7. 3
      TNode/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs.meta
  8. 5
      TNode/Editor/GraphBlackboard/GraphBlackboardView.cs
  9. 0
      TNode/Editor/GraphBlackboard/GraphBlackboardView.cs.meta
  10. 2
      TNode/Editor/GraphEditor.cs
  11. 2
      TNode/Editor/Inspector/InspectorImplementation/EnumFieldItem.cs
  12. 2
      TNode/Editor/Inspector/InspectorImplementation/FloatFieldItem.cs
  13. 2
      TNode/Editor/Inspector/InspectorImplementation/StringFieldItem.cs
  14. 2
      TNode/Editor/Inspector/InspectorImplementation/ToggleFieldItem.cs
  15. 8
      TNode/Editor/Inspector/InspectorItemFactory.cs
  16. 2
      TNode/Editor/NodeViews/DragNodeView.cs
  17. 24
      TNode/Editor/Search/BlackboardSearchWindowProvider.cs
  18. 4
      TNode/Editor/Search/NodeSearchWindowProvider.cs

@ -1,7 +1,7 @@
using TNode.Models; using TNode.Models;
using TNode.Attribute; using TNode.Attribute;
using TNode.Editor.BaseViews; using TNode.Editor.BaseViews;
[NodeComponent] [ViewComponent]
public class MathGraphView : DataGraphView<MathGraph>{ public class MathGraphView : DataGraphView<MathGraph>{

@ -11,7 +11,7 @@ namespace TNode.Attribute{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
[MeansImplicitUse] [MeansImplicitUse]
public class NodeComponentAttribute:System.Attribute{ public class ViewComponentAttribute:System.Attribute{
public Type GenericType{ get; set; } public Type GenericType{ get; set; }

@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -7,6 +8,7 @@ using TNode.Cache;
using TNode.Editor.GraphBlackboard; using TNode.Editor.GraphBlackboard;
using TNode.Editor.Inspector; using TNode.Editor.Inspector;
using TNode.Editor.Model; using TNode.Editor.Model;
using TNode.Editor.Search;
using TNode.Editor.Tools.NodeCreator; using TNode.Editor.Tools.NodeCreator;
using TNode.Models; using TNode.Models;
using Unity.VisualScripting; using Unity.VisualScripting;
@ -281,20 +283,11 @@ namespace TNode.Editor.BaseViews{
} }
public virtual void CreateBlackboard(){ public virtual void CreateBlackboard(){
_blackboard = new Blackboard();
//Blackboard add "Add Node" button _blackboard = NodeEditorExtensions.CreateBlackboardWithGraphData(typeof(T));
// 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<NodeSearchWindowProvider>();
// };
//
//Set black board to left side of the view
_blackboard.SetPosition(new Rect(0,0,200,600)); _blackboard.SetPosition(new Rect(0,0,200,600));
Add(_blackboard); Add(_blackboard);
//Check the type of the blackboard
OnDataChanged+= (sender, e) => { BlackboardUpdate(); }; OnDataChanged+= (sender, e) => { BlackboardUpdate(); };
@ -311,9 +304,25 @@ namespace TNode.Editor.BaseViews{
foreach (var field in _data.blackboardData.GetType() foreach (var field in _data.blackboardData.GetType()
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){ .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){
//if the field is MonoBehaviour,add a property field for blackboard //if the field is MonoBehaviour,add a property field for blackboard
var propertyField = new BlackboardPropertyField(new BlackboardProperty(field.Name,field.FieldType)); //skip if the field is a list or Ilist
_blackboard.Add(propertyField); 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<BlackboardSearchWindowProvider>();
//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(){ public virtual void DestroyInspector(){

@ -5,6 +5,7 @@ using TNode.Attribute;
using TNode.BaseViews; using TNode.BaseViews;
using TNode.Editor; using TNode.Editor;
using TNode.Editor.BaseViews; using TNode.Editor.BaseViews;
using TNode.Editor.GraphBlackboard;
using TNode.Editor.Inspector; using TNode.Editor.Inspector;
using TNode.Models; using TNode.Models;
using UnityEditor.Experimental.GraphView; using UnityEditor.Experimental.GraphView;
@ -58,7 +59,7 @@ namespace TNode.Cache{
foreach(var type in assembly.GetTypes()){ foreach(var type in assembly.GetTypes()){
if(type.IsClass && !type.IsAbstract){ if(type.IsClass && !type.IsAbstract){
//Register Node View And Graph View via its parent class //Register Node View And Graph View via its parent class
SetNodeComponentAttribute(type); SetViewComponentAttribute(type);
//Register Node Data by GraphUsageAttribute. //Register Node Data by GraphUsageAttribute.
SetGraphUsageAttribute(type); SetGraphUsageAttribute(type);
} }
@ -98,9 +99,10 @@ namespace TNode.Cache{
} }
} }
} }
private readonly Type[] _acceptedTypesForGenericToSpecific = new Type[]{typeof(NodeView<>),typeof(DataGraphView<>),typeof(InspectorItem<>),typeof(NodeView<>)}; private readonly Type[] _acceptedTypesForGenericToSpecific = new Type[]{typeof(NodeView<>),typeof(DataGraphView<>),typeof(GraphBlackboardView<>)};
private void SetNodeComponentAttribute(Type type){ private readonly Type[] _defaultTypes = new []{typeof(DefaultNodeView),typeof(DefaultGraphBlackboardView)};
foreach (var attribute in type.GetCustomAttributes(typeof(NodeComponentAttribute), false)){ private void SetViewComponentAttribute(Type type){
foreach (var attribute in type.GetCustomAttributes(typeof(ViewComponentAttribute), false)){
//fetch this type 's parent class //fetch this type 's parent class
var parent = type.BaseType; 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 NodeView or DataGraphView,
@ -121,17 +123,28 @@ namespace TNode.Cache{
} }
//Outer wrapper for the singleton class //Outer wrapper for the singleton class
public static class NodeEditorExtensions{ public static class NodeEditorExtensions{
public static T CreateNodeComponentFromGenericType<T>(){ /// <summary>
/// by given a generic type T,return the implementation instance of the generic type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T CreateViewComponentFromBaseType<T>(){
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[typeof(T)]; var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[typeof(T)];
var instance = (T)Activator.CreateInstance(implementedType); var instance = (T)Activator.CreateInstance(implementedType);
return instance; return instance;
} }
public static object CreateNodeComponentFromGenericType(Type t){
/// <summary>
/// by given a generic type t,return the implementation instance of the generic type
/// </summary>
/// <returns></returns>
public static object CreateViewComponentFromBaseType(Type t){
if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(t)){ if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(t)){
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[t]; var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[t];
var instance = Activator.CreateInstance(implementedType); var instance = Activator.CreateInstance(implementedType);
return instance; return instance;
} }
//check if t is a generic type node view //check if t is a generic type node view
if (t is{IsGenericType: true} && t.GetGenericTypeDefinition() == typeof(NodeView<>)){ if (t is{IsGenericType: true} && t.GetGenericTypeDefinition() == typeof(NodeView<>)){
var instance = Activator.CreateInstance(typeof(NodeView<NodeData>)); var instance = Activator.CreateInstance(typeof(NodeView<NodeData>));
@ -139,6 +152,31 @@ namespace TNode.Cache{
} }
return null; 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<T>() where T : class{ public static bool HasSpecificTypeComponent<T>() where T : class{
return NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(typeof(T)); return NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(typeof(T));
@ -159,19 +197,6 @@ namespace TNode.Cache{
} }
return null; return null;
} }
public static object CreateNodeViewFromNodeType<T>() where T:NodeData,new(){
//Check specific derived type exists or not.
var type = typeof(NodeView<T>);
if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(type)){
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[type];
var instance = (NodeView<T>)Activator.CreateInstance(implementedType);
return instance;
}
else{
return new DefaultNodeView();
}
}
public static object CreateNodeViewFromNodeType(Type t){ public static object CreateNodeViewFromNodeType(Type t){
//Check the generic type of NodeView by t //Check the generic type of NodeView by t

@ -0,0 +1,17 @@
using TNode.Attribute;
using TNode.Models;
namespace TNode.Editor.GraphBlackboard{
[ViewComponent]
public class DefaultGraphBlackboardView:GraphBlackboardView<BlackboardData>{
public DefaultGraphBlackboardView(){
}
public void ConstructView(){
}
public void AddParameter(){
}
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 98e4de339ad84949ac2c50e61a108a96
timeCreated: 1657592333

@ -5,8 +5,11 @@ namespace TNode.Editor.GraphBlackboard{
/// <summary> /// <summary>
/// Implement this class to create graph black board for specified graph /// Implement this class to create graph black board for specified graph
/// </summary> /// </summary>
public class GraphBlackboard<T>:Blackboard where T:BlackboardData{ public class GraphBlackboardView<T>:Blackboard where T:BlackboardData{
public T BlackboardData; public T BlackboardData;
public GraphBlackboardView() : base(){
}
} }
} }

@ -36,7 +36,7 @@ namespace TNode.Editor{
} }
private void BuildGraphView(){ private void BuildGraphView(){
_graphView = NodeEditorExtensions.CreateNodeComponentFromGenericType<DataGraphView<T>>(); _graphView = NodeEditorExtensions.CreateViewComponentFromBaseType<DataGraphView<T>>();
rootVisualElement.Add(_graphView); rootVisualElement.Add(_graphView);
_graphView.StretchToParentSize(); _graphView.StretchToParentSize();
} }

@ -4,7 +4,7 @@ using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
namespace TNode.Editor.Inspector.InspectorImplementation{ namespace TNode.Editor.Inspector.InspectorImplementation{
[NodeComponent] [ViewComponent]
[Obsolete] [Obsolete]
public class EnumFieldItem:InspectorItem<Enum>{ public class EnumFieldItem:InspectorItem<Enum>{
public EnumFieldItem() : base(){ public EnumFieldItem() : base(){

@ -4,7 +4,7 @@ using UnityEngine.UIElements;
namespace TNode.Editor.Inspector.InspectorImplementation{ namespace TNode.Editor.Inspector.InspectorImplementation{
[Obsolete] [Obsolete]
[NodeComponent] [ViewComponent]
public class FloatFieldItem:InspectorItem<float>{ public class FloatFieldItem:InspectorItem<float>{
public FloatFieldItem():base(){ public FloatFieldItem():base(){
CreateBindable(new FloatField()); CreateBindable(new FloatField());

@ -7,7 +7,7 @@ namespace TNode.Editor.Inspector.InspectorImplementation{
/// <summary> /// <summary>
/// Force these element to bind native c# property /// Force these element to bind native c# property
/// </summary> /// </summary>
[NodeComponent] [ViewComponent]
public class StringFieldItem:InspectorItem<string>{ public class StringFieldItem:InspectorItem<string>{
public StringFieldItem():base(){ public StringFieldItem():base(){
CreateBindable(new TextField()); CreateBindable(new TextField());

@ -4,7 +4,7 @@ using UnityEngine.UIElements;
namespace TNode.Editor.Inspector.InspectorImplementation{ namespace TNode.Editor.Inspector.InspectorImplementation{
[Obsolete] [Obsolete]
[NodeComponent] [ViewComponent]
public class ToggleFieldItem:InspectorItem<bool>{ public class ToggleFieldItem:InspectorItem<bool>{
public ToggleFieldItem(){ public ToggleFieldItem(){
CreateBindable(new Toggle()); CreateBindable(new Toggle());

@ -15,12 +15,12 @@ namespace TNode.Editor.Inspector{
var hasSpecificType = NodeEditorExtensions.HasSpecificTypeComponent<InspectorItem<T>>(); var hasSpecificType = NodeEditorExtensions.HasSpecificTypeComponent<InspectorItem<T>>();
if (hasSpecificType){ if (hasSpecificType){
return NodeEditorExtensions.CreateNodeComponentFromGenericType<InspectorItem<T>>(); return NodeEditorExtensions.CreateViewComponentFromBaseType<InspectorItem<T>>();
} }
if (typeof(T).IsEnum){ if (typeof(T).IsEnum){
return NodeEditorExtensions.CreateNodeComponentFromGenericType(typeof(InspectorItem<Enum>)) as InspectorItem<T>; return NodeEditorExtensions.CreateViewComponentFromBaseType(typeof(InspectorItem<Enum>)) as InspectorItem<T>;
} }
return null; return null;
} }
@ -30,12 +30,12 @@ namespace TNode.Editor.Inspector{
var hasSpecificType = NodeEditorExtensions.HasSpecificTypeComponent(genericType); var hasSpecificType = NodeEditorExtensions.HasSpecificTypeComponent(genericType);
if (hasSpecificType){ if (hasSpecificType){
return NodeEditorExtensions.CreateNodeComponentFromGenericType(genericType) as INodeDataBindingBase; return NodeEditorExtensions.CreateViewComponentFromBaseType(genericType) as INodeDataBindingBase;
} }
if (t.IsEnum){ if (t.IsEnum){
return NodeEditorExtensions.CreateNodeComponentFromGenericType(typeof(InspectorItem<Enum>)) as INodeDataBindingBase; return NodeEditorExtensions.CreateViewComponentFromBaseType(typeof(InspectorItem<Enum>)) as INodeDataBindingBase;
} }
return null; return null;
} }

@ -3,7 +3,7 @@ using TNode.Editor.BaseViews;
using TNode.Models; using TNode.Models;
namespace TNode.Editor.NodeViews{ namespace TNode.Editor.NodeViews{
[NodeComponent] [ViewComponent]
public class DragNodeView<T>:NodeView<BlackboardDragNodeData<T>>{ public class DragNodeView<T>:NodeView<BlackboardDragNodeData<T>>{
public DragNodeView() : base(){ public DragNodeView() : base(){
//Make capsule like style //Make capsule like style

@ -6,8 +6,8 @@ using UnityEditor;
using UnityEditor.Experimental.GraphView; using UnityEditor.Experimental.GraphView;
using UnityEngine; using UnityEngine;
namespace TNode.Editor{ namespace TNode.Editor.Search{
public class BlackboardSearchWindowProvider:ISearchWindowProvider{ public class BlackboardSearchWindowProvider:ScriptableObject,ISearchWindowProvider{
private Type _graphType; private Type _graphType;
private IDataGraphView _graphView; private IDataGraphView _graphView;
private EditorWindow _editor; private EditorWindow _editor;
@ -20,15 +20,19 @@ namespace TNode.Editor{
public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context){ public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context){
var blackboardData = _graphView.GetBlackboardData(); var blackboardData = _graphView.GetBlackboardData();
var type = blackboardData.GetType(); var type = blackboardData.GetType();
var entries = new List<SearchTreeEntry>(); var list = new List<SearchTreeEntry>(){
if (entries == null) throw new ArgumentNullException(nameof(entries)); new SearchTreeGroupEntry(new GUIContent("Add New Blackboard Data"), 0),
};
if (list == null) throw new ArgumentNullException(nameof(list));
//search fields with List type //search fields with List type
Texture2D icon = new Texture2D(2,2); Texture2D icon = new Texture2D(2,2);
foreach (var field in type.GetFields()){ foreach (var field in type.GetFields()){
if (field.FieldType.IsGenericType){ if (field.FieldType.IsGenericType){
var genericType = field.FieldType.GetGenericTypeDefinition(); var genericType = field.FieldType.GetGenericTypeDefinition();
if (genericType == typeof(List<>)){ if (genericType == typeof(List<>)){
entries.Add(new SearchTreeEntry(new GUIContent(field.Name,icon)){ list.Add(new SearchTreeEntry(new GUIContent(field.Name,icon)){
level = 1, level = 1,
userData = new InternalSearchTreeUserData(){ userData = new InternalSearchTreeUserData(){
List = field.GetValue(blackboardData) as IList, List = field.GetValue(blackboardData) as IList,
@ -39,24 +43,20 @@ namespace TNode.Editor{
} }
} }
} }
Debug.Log($"{list.Count}");
return entries; return list;
} }
public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context){ public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context){
var userData = SearchTreeEntry.userData; var userData = SearchTreeEntry.userData;
var relativePos = context.screenMousePosition - _editor.position.position;
var blackboardData = _graphView.GetBlackboardData();
if (userData is InternalSearchTreeUserData){ if (userData is InternalSearchTreeUserData){
var list = ((InternalSearchTreeUserData) userData).List; var list = ((InternalSearchTreeUserData) userData).List;
var type = ((InternalSearchTreeUserData) userData).Type; var type = ((InternalSearchTreeUserData) userData).Type;
var newItem = Activator.CreateInstance(type); var newItem = Activator.CreateInstance(type);
list.Add(newItem); list?.Add(newItem);
return true; return true;
} }
return false; return false;
} }

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using TNode.BaseViews;
using TNode.Cache; using TNode.Cache;
using TNode.Editor.BaseViews; using TNode.Editor.BaseViews;
using TNode.Editor.Tools.NodeCreator; using TNode.Editor.Tools.NodeCreator;
@ -11,7 +9,7 @@ using UnityEditor.Experimental.GraphView;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
namespace TNode.Editor{ namespace TNode.Editor.Search{
public class NodeSearchWindowProvider:ScriptableObject,ISearchWindowProvider{ public class NodeSearchWindowProvider:ScriptableObject,ISearchWindowProvider{
private Type _graphType; private Type _graphType;
private GraphView _graphView; private GraphView _graphView;

Loading…
Cancel
Save