From 462be05c2ef7084cfd5c6e0398ac5c6f5f314a1d Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Mon, 4 Jul 2022 19:47:12 +0800 Subject: [PATCH] 1.finish editor data partly --- TNode/Editor/BaseViews/DataGraphView.cs | 116 ++++++++++++------ TNode/Editor/BaseViews/NodeView.cs | 2 +- .../Editor/BaseViews/SimpleGraphSubWindow.cs | 2 +- .../EditorPersistence/GraphEditorData.cs | 8 +- ...ditorData.cs => GraphElementEditorData.cs} | 6 +- ...cs.meta => GraphElementEditorData.cs.meta} | 0 TNode/Editor/GraphEditor.cs | 13 -- TNode/Models/GraphData.cs | 3 +- TNode/Runtime/RuntimeGraph.cs | 15 ++- TNode/Runtime/RuntimeNode.cs | 19 +-- TNode/RuntimeCache/RuntimeCache.cs | 15 ++- 11 files changed, 125 insertions(+), 74 deletions(-) rename TNode/Editor/EditorPersistence/{NodeEditorData.cs => GraphElementEditorData.cs} (61%) rename TNode/Editor/EditorPersistence/{NodeEditorData.cs.meta => GraphElementEditorData.cs.meta} (100%) diff --git a/TNode/Editor/BaseViews/DataGraphView.cs b/TNode/Editor/BaseViews/DataGraphView.cs index 33d6229..3a3845b 100644 --- a/TNode/Editor/BaseViews/DataGraphView.cs +++ b/TNode/Editor/BaseViews/DataGraphView.cs @@ -126,7 +126,51 @@ namespace TNode.Editor.BaseViews{ ResetGraphView(); } } + public event DataChangedEventHandler OnDataChanged; + public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs e); + //A Constructor for the DataGraphView ,never to override it + public DataGraphView(){ + + styleSheets.Add(Resources.Load("GraphViewBackground")); + var grid = new GridBackground(); + Insert(0,grid); + grid.StretchToParentSize(); + this.AddManipulator(new ContentDragger()); + this.AddManipulator(new SelectionDragger()); + this.AddManipulator(new RectangleSelector()); + SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale); + OnInit(); + } + private void ConstructDefaultBehaviour(){ + //Register a right click context menu + ConstructViewContextualMenu(); + } + + public void ConstructViewContextualMenu(){ + + //Rebuild the contextual menu + + this.RegisterCallback(evt => { + Vector2 editorPosition = Owner.position.position; + //Remove all the previous menu items + evt.menu.MenuItems().Clear(); + evt.menu.AppendAction("Create Node", dma => { + var dmaPos = dma.eventInfo.mousePosition+editorPosition; + SearchWindowContext searchWindowContext = new SearchWindowContext(dmaPos,200,200); + var searchWindow = ScriptableObject.CreateInstance(); + searchWindow.Setup(typeof(T),this,Owner); + SearchWindow.Open(searchWindowContext, searchWindow); + }); + + }); + + } + + private void OnInit(){ + ConstructDefaultBehaviour(); + OnGraphViewCreate(); + } public void ResetGraphView(){ //Clear all nodes foreach (var node in nodes){ @@ -145,8 +189,8 @@ namespace TNode.Editor.BaseViews{ var nodeType = dataNode.GetType(); //Get the derived type of NodeAttribute View from the node type - var nodePos = Owner.graphEditorData.nodesData. - FirstOrDefault(x => x.nodeGuid == dataNode.id)?.nodePos??new Rect(0,0,200,200); + var nodePos = Owner.graphEditorData.graphElementsData. + FirstOrDefault(x => x.guid == dataNode.id)?.pos??new Rect(0,0,200,200); AddTNode(dataNode,nodePos); } @@ -166,37 +210,11 @@ namespace TNode.Editor.BaseViews{ } _nodeDict.Clear(); } - //A Constructor for the DataGraphView ,never to override it - public DataGraphView(){ - - styleSheets.Add(Resources.Load("GraphViewBackground")); - var grid = new GridBackground(); - Insert(0,grid); - grid.StretchToParentSize(); - this.AddManipulator(new ContentDragger()); - this.AddManipulator(new SelectionDragger()); - this.AddManipulator(new RectangleSelector()); - SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale); - OnInit(); - } //OnDataChanged event - public event DataChangedEventHandler OnDataChanged; - public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs e); - - private void ConstructDefaultBehaviour(){ - //Register a right click context menu - //ConstructContextualMenuOption(); - } - public void ConstructViewContextualMenu(EventCallback callback){ - RegisterCallback(callback); - } + - private void OnInit(){ - ConstructDefaultBehaviour(); - OnGraphViewCreate(); - } public virtual void CreateInspector(){ NodeInspector nodeInspector = new NodeInspector(); @@ -234,19 +252,19 @@ namespace TNode.Editor.BaseViews{ } public void SaveEditorData(GraphEditorData graphEditorData){ - graphEditorData.nodesData?.Clear(); + graphEditorData.graphElementsData?.Clear(); //iterator nodes - if (graphEditorData.nodesData == null){ - graphEditorData.nodesData = new List(); + if (graphEditorData.graphElementsData == null){ + graphEditorData.graphElementsData = new List(); } foreach (var node in this.nodes){ - var nodeEditorData = new NodeEditorData{ - nodePos = node.GetPosition(), + var nodeEditorData = new GraphElementEditorData{ + pos = node.GetPosition(), }; if (node is INodeView nodeView){ - nodeEditorData.nodeGuid = nodeView.GetNodeData().id; + nodeEditorData.guid = nodeView.GetNodeData().id; } - graphEditorData.nodesData.Add(nodeEditorData); + graphEditorData.graphElementsData.Add(nodeEditorData); EditorUtility.SetDirty(graphEditorData); } } @@ -335,6 +353,32 @@ namespace TNode.Editor.BaseViews{ nodeViewInterface.SetNodeData(nodeData); } _nodeDict.Add(nodeData.id, nodeView); + + //register an callback ,when right click context menu + nodeView.RegisterCallback(evt => { + var menu = new GenericMenu(); + menu.AddItem(new GUIContent("Delete"), false, () => { + RemoveElement(nodeView); + if (nodeView is INodeView tNodeView){ + var nodeData1 = tNodeView.GetNodeData(); + _data.NodeDictionary.Remove(nodeData1.id); + _nodeDict.Remove(nodeData1.id); + //Break all edges connected to this node + foreach (var edge in edges){ + if (edge.input.node == nodeView || edge.output.node == nodeView){ + RemoveElement(edge); + } + } + Owner.graphEditorData.graphElementsData.RemoveAll(x => x.guid == nodeData1.id); + } + }); + menu.ShowAsContext(); + }); + + + + + } } diff --git a/TNode/Editor/BaseViews/NodeView.cs b/TNode/Editor/BaseViews/NodeView.cs index 13f50d6..c2384a4 100644 --- a/TNode/Editor/BaseViews/NodeView.cs +++ b/TNode/Editor/BaseViews/NodeView.cs @@ -15,7 +15,7 @@ namespace TNode.Editor.BaseViews{ public abstract class NodeView : Node,INodeView where T:NodeData,new(){ protected T _data; private readonly NodeInspectorInNode _nodeInspectorInNode; - + public T Data{ get => _data; set{ diff --git a/TNode/Editor/BaseViews/SimpleGraphSubWindow.cs b/TNode/Editor/BaseViews/SimpleGraphSubWindow.cs index 6eb82cf..da03952 100644 --- a/TNode/Editor/BaseViews/SimpleGraphSubWindow.cs +++ b/TNode/Editor/BaseViews/SimpleGraphSubWindow.cs @@ -41,7 +41,7 @@ namespace TNode.BaseViews{ } public void ResetPos(GraphEditorData editorData){ - var res = editorData.subWindowPos.FirstOrDefault(x => x.nodeGuid == this.GetPersistenceId()); + var res = editorData.graphElementsData.FirstOrDefault(x => x.guid == this.GetPersistenceId()); } public void SavePos(GraphEditorData editorData){ diff --git a/TNode/Editor/EditorPersistence/GraphEditorData.cs b/TNode/Editor/EditorPersistence/GraphEditorData.cs index e096ec5..02fc71c 100644 --- a/TNode/Editor/EditorPersistence/GraphEditorData.cs +++ b/TNode/Editor/EditorPersistence/GraphEditorData.cs @@ -2,14 +2,12 @@ using TNode.Editor.EditorPersistence; using TNode.Editor.Model; using UnityEngine; +using UnityEngine.Serialization; namespace TNode.Editor{ - [CreateAssetMenu(fileName = "NodeAttribute Editor Config", menuName = "TNode/NodeAttribute Editor Config")] + [CreateAssetMenu(fileName = "Graph Editor Data", menuName = "TNode/Graph Editor Data")] public class GraphEditorData:ScriptableObject{ - public List nodesData; - public List subWindowPos; - - public List graphViewPersistence; + public List graphElementsData; } } \ No newline at end of file diff --git a/TNode/Editor/EditorPersistence/NodeEditorData.cs b/TNode/Editor/EditorPersistence/GraphElementEditorData.cs similarity index 61% rename from TNode/Editor/EditorPersistence/NodeEditorData.cs rename to TNode/Editor/EditorPersistence/GraphElementEditorData.cs index 9f38640..f21170d 100644 --- a/TNode/Editor/EditorPersistence/NodeEditorData.cs +++ b/TNode/Editor/EditorPersistence/GraphElementEditorData.cs @@ -6,8 +6,8 @@ using UnityEngine.Serialization; namespace TNode.Editor.Model{ [Serializable] - public class NodeEditorData{ - public string nodeGuid; - public Rect nodePos; + public class GraphElementEditorData{ + public string guid; + public Rect pos; } } \ No newline at end of file diff --git a/TNode/Editor/EditorPersistence/NodeEditorData.cs.meta b/TNode/Editor/EditorPersistence/GraphElementEditorData.cs.meta similarity index 100% rename from TNode/Editor/EditorPersistence/NodeEditorData.cs.meta rename to TNode/Editor/EditorPersistence/GraphElementEditorData.cs.meta diff --git a/TNode/Editor/GraphEditor.cs b/TNode/Editor/GraphEditor.cs index 48ad37a..7619b74 100644 --- a/TNode/Editor/GraphEditor.cs +++ b/TNode/Editor/GraphEditor.cs @@ -39,19 +39,6 @@ namespace TNode.Editor{ _graphView = NodeEditorExtensions.CreateInstance>(); rootVisualElement.Add(_graphView); _graphView.StretchToParentSize(); - _graphView.ConstructViewContextualMenu(evt => { - //Current issue is that the search window don't show up at the exact position of the mouse click by dma.eventInfo.mousePosition - //So I have to manually set the position of the search window to fit the mouse click position by add an offset driven by Editor's position - //Maybe a better way exists to fix this issue - Vector2 editorPosition = this.position.position; - evt.menu.AppendAction("Create Node", dma => { - var dmaPos = dma.eventInfo.mousePosition+editorPosition; - SearchWindowContext searchWindowContext = new SearchWindowContext(dmaPos,200,200); - var searchWindow = CreateInstance(); - searchWindow.Setup(typeof(T),_graphView,this); - SearchWindow.Open(searchWindowContext, searchWindow); - }); - }); } private void DefineGraphEditorActions(){ diff --git a/TNode/Models/GraphData.cs b/TNode/Models/GraphData.cs index bacbe9e..a6c5b5b 100644 --- a/TNode/Models/GraphData.cs +++ b/TNode/Models/GraphData.cs @@ -12,9 +12,8 @@ namespace TNode.Models{ public Dictionary NodeDictionary = new Dictionary(); public List nodeLinks = new List(); - + [HideInInspector] [SerializeField] - [TextArea] //[HideInInspector] private string jsonObject; diff --git a/TNode/Runtime/RuntimeGraph.cs b/TNode/Runtime/RuntimeGraph.cs index 55e11d1..51bc5fa 100644 --- a/TNode/Runtime/RuntimeGraph.cs +++ b/TNode/Runtime/RuntimeGraph.cs @@ -1,9 +1,22 @@ -using TNode.Models; +using System.Collections; +using System.Collections.Generic; +using TNode.Models; using UnityEngine; namespace TNode.Runtime{ public class RuntimeGraph:MonoBehaviour{ public GraphData graphData; + public SortedSet _sortedSet; + public void StartProcessNode(ProcessingStrategy strategy){ + + + } + + } + + public enum ProcessingStrategy{ + BreadthFirst, + DepthFirst } } \ No newline at end of file diff --git a/TNode/Runtime/RuntimeNode.cs b/TNode/Runtime/RuntimeNode.cs index 94a84ec..1cbef18 100644 --- a/TNode/Runtime/RuntimeNode.cs +++ b/TNode/Runtime/RuntimeNode.cs @@ -5,17 +5,22 @@ using System.Linq; using TNode.Models; namespace TNode.Runtime{ - public class RuntimeNode where T:NodeData{ - public T NodeData{ get; set; } + public abstract class RuntimeNode{ + public object NodeData; + public List NodeLinks; + public void ProcessThisNode(){ + + } + + } + public class RuntimeNode:RuntimeNode where T:NodeData{ + public new T NodeData{ get; set; } //Links related to runtime node,for fast access.only remember out links public List NodeLinks; - - public void Process(){ - - var outputPorts = NodeLinks.Select(x => x.outPort.portName); + public void OnCreate(){ + RuntimeCache.RuntimeCache.Instance.RegisterRuntimeNode(); } - } } \ No newline at end of file diff --git a/TNode/RuntimeCache/RuntimeCache.cs b/TNode/RuntimeCache/RuntimeCache.cs index 464bd79..4ec50ce 100644 --- a/TNode/RuntimeCache/RuntimeCache.cs +++ b/TNode/RuntimeCache/RuntimeCache.cs @@ -34,13 +34,18 @@ namespace TNode.RuntimeCache{ var type = typeof(T); if(!CachedDelegatesForGettingValue.ContainsKey(type)){ CachedDelegatesForGettingValue.Add(type, new List()); + var properties = type.GetProperties(); + foreach(var property in properties){ + var getValueDelegate = GetValueDelegateForProperty(property); + CachedDelegatesForGettingValue[type].Add(getValueDelegate); + } } - //iterate properties of the nodeData and add them to the cache - var properties = type.GetProperties(); - foreach(var property in properties){ - var getValueDelegate = GetValueDelegateForProperty(property); - CachedDelegatesForGettingValue[type].Add(getValueDelegate); + else{ + //Cache already exists for this type + } + + }