From 1c3defb073c508a940783407dabffd689ee14b30 Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Thu, 21 Jul 2022 19:21:48 +0800 Subject: [PATCH 1/3] refactor: move around scripts to separate TNodeCore and TNodeGraphViewImpl --- README.md | 3 +- .../EditorPersistence/GraphEditorData.cs | 27 +++++++- .../Editor/NodeGraphView/IDataGraphView.cs | 6 +- .../Editor => TNodeCore}/GraphEditor.cs | 15 ++--- .../Editor => TNodeCore}/GraphEditor.cs.meta | 0 TNodeCore/RuntimeCache/RuntimeCache.cs | 1 - .../Editor/Cache/NodeEditorExtensions.cs | 67 ++++++++++++------- .../Editor/NodeGraphView/DataGraphView.cs | 8 +-- 8 files changed, 85 insertions(+), 42 deletions(-) rename {TNodeGraphViewImpl/Editor => TNodeCore}/GraphEditor.cs (91%) rename {TNodeGraphViewImpl/Editor => TNodeCore}/GraphEditor.cs.meta (100%) diff --git a/README.md b/README.md index 83ce936..266ba15 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/TNodeCore/Editor/EditorPersistence/GraphEditorData.cs b/TNodeCore/Editor/EditorPersistence/GraphEditorData.cs index c615a56..646edcd 100644 --- a/TNodeCore/Editor/EditorPersistence/GraphEditorData.cs +++ b/TNodeCore/Editor/EditorPersistence/GraphEditorData.cs @@ -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 graphElementsData; + + public GraphImplType graphImplType = GraphImplType.GraphViewImpl; + public static Func GraphViewImplCreator; + public static Func GtfImplCreator; + + public IDataGraphView GetGraphView () where T:GraphData{ + switch (graphImplType){ + case GraphImplType.GraphViewImpl: + return (IDataGraphView)GraphViewImplCreator.Invoke(typeof(T)); + case GraphImplType.GraphToolsFoundationImpl: + throw new NotImplementedException(); + default: + throw new NotImplementedException(); + } + } + + } + + public enum GraphImplType{ + GraphViewImpl, + GraphToolsFoundationImpl } } \ No newline at end of file diff --git a/TNodeCore/Editor/NodeGraphView/IDataGraphView.cs b/TNodeCore/Editor/NodeGraphView/IDataGraphView.cs index 8d00d8a..16ab0fc 100644 --- a/TNodeCore/Editor/NodeGraphView/IDataGraphView.cs +++ b/TNodeCore/Editor/NodeGraphView/IDataGraphView.cs @@ -1,7 +1,11 @@ -using TNodeCore.Models; +using TNodeCore.Editor.EditorPersistence; +using TNodeCore.Models; +using TNodeEditor.Editor; namespace TNodeCore.Editor.NodeGraphView{ public interface IDataGraphView : IBaseDataGraphView where T:GraphData{ public T Data{ get; set; } + GraphEditor Owner{ get; set; } + void SaveWithEditorData(GraphEditorData graphEditorData); } } \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/GraphEditor.cs b/TNodeCore/GraphEditor.cs similarity index 91% rename from TNodeGraphViewImpl/Editor/GraphEditor.cs rename to TNodeCore/GraphEditor.cs index f6c2460..0beb43a 100644 --- a/TNodeGraphViewImpl/Editor/GraphEditor.cs +++ b/TNodeCore/GraphEditor.cs @@ -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 : EditorWindow,IGraphEditor where T:GraphData{ - protected BaseDataGraphView GraphView; + protected IDataGraphView 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>(); - rootVisualElement.Add(GraphView); - GraphView.StretchToParentSize(); + GraphView = graphEditorData.GetGraphView(); + 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; + GraphView = (IDataGraphView)graphView; } public IBaseDataGraphView GetGraphView(){ diff --git a/TNodeGraphViewImpl/Editor/GraphEditor.cs.meta b/TNodeCore/GraphEditor.cs.meta similarity index 100% rename from TNodeGraphViewImpl/Editor/GraphEditor.cs.meta rename to TNodeCore/GraphEditor.cs.meta diff --git a/TNodeCore/RuntimeCache/RuntimeCache.cs b/TNodeCore/RuntimeCache/RuntimeCache.cs index 139be40..edae640 100644 --- a/TNodeCore/RuntimeCache/RuntimeCache.cs +++ b/TNodeCore/RuntimeCache/RuntimeCache.cs @@ -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; diff --git a/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs index fcdcb48..f40a74a 100644 --- a/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs +++ b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs @@ -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(){ + 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)); + 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{ /// /// /// - public static T CreateViewComponentFromBaseType(){ - 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(); 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 /// /// - 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)); - 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(); + } + } } \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index a33661d..49b4e97 100644 --- a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -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:GraphView,IDataGraphView where T:GraphData{ + public class BaseDataGraphView:GraphView,IDataGraphView 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 Owner; private Dictionary _nodeDict = new(); private IBlackboardView _blackboard; public T Data{ @@ -44,6 +42,8 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ ResetGraphView(); } } + + public GraphEditor Owner{ get; set; } public event DataChangedEventHandler OnDataChanged; #endregion #region event declarations From 42908518014401fc054303c1f910e7e37ee32f81 Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Fri, 22 Jul 2022 15:09:12 +0800 Subject: [PATCH 2/3] chore: update readme --- README.md | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 266ba15..67ed724 100644 --- a/README.md +++ b/README.md @@ -6,20 +6,34 @@ Note **it's not usable and productive on current stage** and need a better development . and it's mainly for my own use now. +The tool separate its graph editor implementation and the graph creation logic. # Install currently under development -# Features +# Main Features -1. Create graph script by the creator tool -2. Node creation based on specified type of graph -3. Easy port creation via attribute -4. Runtime graph -5. Blackboard for runtime graph as exposed parameters -6. Runtime graph execution -7. Test Mode (Runtime graph only) +* Create graph script by the creator tool +* Node creation based on specified type of graph +* Easy port creation via attribute +* Runtime graph +* Blackboard for runtime graph as exposed parameters +* Runtime graph execution +* An easy test mode (Runtime graph only) + +# Some To-dos +* Caching runtime state for faster execution +* Undo redo support +* Function as port +* Better blackboard support # Usage +No ,It's better not to use TNode at current stage until it's stable. + +### Convention +Property is + + + From e8774a39f4cb6db31e02466649fe1bc16010ef31 Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Fri, 22 Jul 2022 15:09:33 +0800 Subject: [PATCH 3/3] chore:update readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 67ed724..73c46e9 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,6 @@ currently under development No ,It's better not to use TNode at current stage until it's stable. ### Convention -Property is