refactor: move around scripts to separate TNodeCore and TNodeGraphViewImpl

main
taoria 3 years ago
parent a63463b934
commit 1c3defb073
  1. 3
      README.md
  2. 27
      TNodeCore/Editor/EditorPersistence/GraphEditorData.cs
  3. 6
      TNodeCore/Editor/NodeGraphView/IDataGraphView.cs
  4. 15
      TNodeCore/GraphEditor.cs
  5. 0
      TNodeCore/GraphEditor.cs.meta
  6. 1
      TNodeCore/RuntimeCache/RuntimeCache.cs
  7. 67
      TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs
  8. 8
      TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs

@ -1,11 +1,12 @@
# T-Node
Simple wrapper for unity experimental graphview and if possible latter,GTF.
Node graph creation tool based on unity experimental graphview and if possible latter,GTF.
the main goal of the repo is to make graph creation easier and more intuitive.
Note **it's not usable and productive on current stage** and need a better
development .
and it's mainly for my own use now.
# Install
currently under development

@ -1,10 +1,35 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using TNodeCore.Editor.NodeGraphView;
using TNodeCore.Models;
using UnityEngine;
namespace TNodeCore.Editor.EditorPersistence{
[CreateAssetMenu(fileName = "Graph Editor Data", menuName = "TNode/Graph Editor Data")]
public class GraphEditorData:ScriptableObject{
[HideInInspector]
public List<GraphElementEditorData> graphElementsData;
public GraphImplType graphImplType = GraphImplType.GraphViewImpl;
public static Func<Type,IBaseDataGraphView> GraphViewImplCreator;
public static Func<Type,IBaseDataGraphView> GtfImplCreator;
public IDataGraphView<T> GetGraphView<T> () where T:GraphData{
switch (graphImplType){
case GraphImplType.GraphViewImpl:
return (IDataGraphView<T>)GraphViewImplCreator.Invoke(typeof(T));
case GraphImplType.GraphToolsFoundationImpl:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
}
}
public enum GraphImplType{
GraphViewImpl,
GraphToolsFoundationImpl
}
}

@ -1,7 +1,11 @@
using TNodeCore.Models;
using TNodeCore.Editor.EditorPersistence;
using TNodeCore.Models;
using TNodeEditor.Editor;
namespace TNodeCore.Editor.NodeGraphView{
public interface IDataGraphView<T> : IBaseDataGraphView where T:GraphData{
public T Data{ get; set; }
GraphEditor<T> Owner{ get; set; }
void SaveWithEditorData(GraphEditorData graphEditorData);
}
}

@ -1,16 +1,13 @@
using System;
using TNodeCore.Editor;
using TNodeCore.Editor.EditorPersistence;
using TNodeCore.Editor.NodeGraphView;
using TNodeCore.Models;
using TNodeGraphViewImpl.Editor.Cache;
using TNodeGraphViewImpl.Editor.NodeGraphView;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UIElements;
namespace TNodeGraphViewImpl.Editor{
namespace TNodeEditor.Editor{
// public class SelectGraphWindow : EditorWindow{
// public EditorWindow parent;
@ -48,7 +45,7 @@ namespace TNodeGraphViewImpl.Editor{
// }
// }
public abstract class GraphEditor<T> : EditorWindow,IGraphEditor where T:GraphData{
protected BaseDataGraphView<T> GraphView;
protected IDataGraphView<T> GraphView;
[SerializeField]
private VisualTreeAsset mVisualTreeAsset = default;
//Persist editor data ,such as node position,node size ,etc ,in this script object
@ -84,9 +81,9 @@ namespace TNodeGraphViewImpl.Editor{
GraphView.IsRuntimeGraph = false;
}
private void BuildGraphView(){
GraphView = NodeEditorExtensions.CreateViewComponentFromBaseType<BaseDataGraphView<T>>();
rootVisualElement.Add(GraphView);
GraphView.StretchToParentSize();
GraphView = graphEditorData.GetGraphView<T>();
rootVisualElement.Add((VisualElement)GraphView);
((VisualElement)GraphView).StretchToParentSize();
}
private void DefineGraphEditorActions(){
@ -126,7 +123,7 @@ namespace TNodeGraphViewImpl.Editor{
}
public void SetGraphView(IBaseDataGraphView graphView){
GraphView = graphView as BaseDataGraphView<T>;
GraphView = (IDataGraphView<T>)graphView;
}
public IBaseDataGraphView GetGraphView(){

@ -147,7 +147,6 @@ namespace TNodeCore.RuntimeCache{
public object GetConvertedValue(Type from,Type to,object value){
if(!CachedPortConverters.ContainsKey(from)){
throw new ConversionFailedException("No converter found for type "+from);
return value;
}
if(!CachedPortConverters[from].ContainsKey(to)){
return value;

@ -3,14 +3,15 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TNode.Editor;
using TNode.Editor.NodeViews;
using TNodeCore.Attribute;
using TNodeCore.Editor.Blackboard;
using TNodeCore.Editor.EditorPersistence;
using TNodeCore.Editor.NodeGraphView;
using TNodeCore.Models;
using TNodeGraphViewImpl.Editor.GraphBlackboard;
using TNodeGraphViewImpl.Editor.NodeGraphView;
using TNodeGraphViewImpl.Editor.NodeViews;
using UnityEditor.Experimental.GraphView;
using UnityEditor;
using UnityEngine;
namespace TNodeGraphViewImpl.Editor.Cache{
@ -42,6 +43,25 @@ namespace TNodeGraphViewImpl.Editor.Cache{
}
private static readonly string[] ExcludedAssemblies = new string[]{"Microsoft", "UnityEngine","UnityEditor","mscorlib","System"};
public static T CreateViewComponentFromBaseType<T>(){
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[typeof(T)];
var instance = (T)Activator.CreateInstance(implementedType);
return instance;
}
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(BaseNodeView<>)){
var instance = Activator.CreateInstance(typeof(BaseNodeView<NodeData>));
return instance;
}
return null;
}
private NodeEditorSingleton(){
//exclude unity ,system ,and microsoft types
@ -60,9 +80,14 @@ namespace TNodeGraphViewImpl.Editor.Cache{
}
}
}
GraphEditorData.GraphViewImplCreator+=GraphViewImplCreator;
}
private IBaseDataGraphView GraphViewImplCreator(Type arg){
var genericType = typeof(BaseDataGraphView<>).MakeGenericType(arg);
var instance = CreateViewComponentFromBaseType(genericType) as IBaseDataGraphView;
return instance;
}
private void SetGraphUsageAttribute(Type type){
foreach (var attribute in type.GetCustomAttributes(typeof(GraphUsageAttribute), true)){
var parent = type.BaseType;
@ -116,6 +141,11 @@ namespace TNodeGraphViewImpl.Editor.Cache{
//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.
}
}
public void Initialize(){
//Do nothing indeed
Debug.Log("Hello");
}
}
//Outer wrapper for the singleton class
public static class NodeEditorExtensions{
@ -124,11 +154,7 @@ namespace TNodeGraphViewImpl.Editor.Cache{
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T CreateViewComponentFromBaseType<T>(){
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[typeof(T)];
var instance = (T)Activator.CreateInstance(implementedType);
return instance;
}
public static string GetTypeCategory(Type type){
var category = type.GetCustomAttribute<GraphUsageAttribute>();
return category?.Category ?? "";
@ -138,26 +164,11 @@ namespace TNodeGraphViewImpl.Editor.Cache{
/// 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)){
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(BaseNodeView<>)){
var instance = Activator.CreateInstance(typeof(BaseNodeView<NodeData>));
return instance;
}
return null;
}
public static IBlackboardView CreateBlackboardDataFromBlackboardDataType(Type t){
var type = typeof(GraphBlackboardView<>).MakeGenericType(t);
var res = CreateViewComponentFromBaseType(type) as IBlackboardView;
var res = NodeEditorSingleton.CreateViewComponentFromBaseType(type) as IBlackboardView;
return res ?? new DefaultGraphBlackboardView();
}
public static IBlackboardView CreateBlackboardWithGraphData(GraphData graphData){
@ -248,4 +259,10 @@ namespace TNodeGraphViewImpl.Editor.Cache{
}
}
[InitializeOnLoad]
public class Launcher{
static Launcher(){
NodeEditorSingleton.Instance.Initialize();
}
}
}

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TNode.Editor.Inspector;
using TNode.Editor.Search;
using TNodeCore.Editor.Blackboard;
using TNodeCore.Editor.EditorPersistence;
using TNodeCore.Editor.NodeGraphView;
@ -11,8 +10,8 @@ using TNodeCore.Editor.Tools.NodeCreator;
using TNodeCore.Models;
using TNodeCore.Runtime;
using TNodeCore.RuntimeCache;
using TNodeEditor.Editor;
using TNodeGraphViewImpl.Editor.Cache;
using TNodeGraphViewImpl.Editor.GraphBlackboard;
using TNodeGraphViewImpl.Editor.NodeViews;
using TNodeGraphViewImpl.Editor.Search;
using UnityEditor;
@ -23,7 +22,7 @@ using BlackboardField = TNodeGraphViewImpl.Editor.GraphBlackboard.BlackboardFiel
using Edge = UnityEditor.Experimental.GraphView.Edge;
namespace TNodeGraphViewImpl.Editor.NodeGraphView{
public abstract class BaseDataGraphView<T>:GraphView,IDataGraphView<T> where T:GraphData{
public class BaseDataGraphView<T>:GraphView,IDataGraphView<T> where T:GraphData{
#region variables and properties
private T _data;
private RuntimeGraph _runtimeGraph;
@ -31,7 +30,6 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
private bool _isInspectorOn;
private NodeSearchWindowProvider _nodeSearchWindowProvider;
private NodeInspector _nodeInspector;
public GraphEditor<T> Owner;
private Dictionary<string,Node> _nodeDict = new();
private IBlackboardView _blackboard;
public T Data{
@ -44,6 +42,8 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
ResetGraphView();
}
}
public GraphEditor<T> Owner{ get; set; }
public event DataChangedEventHandler OnDataChanged;
#endregion
#region event declarations

Loading…
Cancel
Save