diff --git a/TNode/Editor/BaseViews/DataGraphView.cs b/TNode/Editor/BaseViews/DataGraphView.cs index bfb95c2..e2e3a8f 100644 --- a/TNode/Editor/BaseViews/DataGraphView.cs +++ b/TNode/Editor/BaseViews/DataGraphView.cs @@ -129,6 +129,8 @@ namespace TNode.Editor.BaseViews{ foreach (var edge in edges){ RemoveElement(edge); } + Dictionary nodeDict = new Dictionary(); + if (nodeDict == null) throw new ArgumentNullException(nameof(nodeDict)); foreach (var dataNode in _data.NodeDictionary.Values){ if(dataNode==null) continue; @@ -143,8 +145,27 @@ namespace TNode.Editor.BaseViews{ //Cast the node view to the nodeViewType AddElement((Node)nodeView); + + ((INodeView)nodeView).SetNodeData(dataNode); + + //Add the node view to the node dictionary + nodeDict.Add(dataNode.id, (Node)nodeView); + } + foreach (var edge in _data.NodeLinks){ + var inputNode = _data.NodeDictionary[edge.inPort.nodeDataId]; + var outputNode = _data.NodeDictionary[edge.outPort.nodeDataId]; + var inputNodeView = nodeDict[inputNode.id]; + var outputNodeView = nodeDict[outputNode.id]; + Edge newEdge = new Edge(){ + input = inputNodeView.inputContainer.Q(edge.inPort.portName), + output = outputNodeView.outputContainer.Q(edge.outPort.portName) + }; + newEdge.input?.Connect(newEdge); + newEdge.output?.Connect(newEdge); + AddElement(newEdge); } + nodeDict.Clear(); } //A Constructor for the DataGraphView ,never to override it public DataGraphView(){ @@ -239,12 +260,43 @@ namespace TNode.Editor.BaseViews{ } } } + + public void SaveWithEditorData(GraphEditorData graphEditorData){ SaveEditorData(graphEditorData); SaveGraphData(); } private void SaveGraphData(){ + foreach (var node in nodes){ + if (node is INodeView nodeView){ + var nodeData = nodeView.GetNodeData(); + if (!_data.NodeDictionary.ContainsKey(nodeData.id)){ + _data.NodeDictionary.Add(nodeData.id, nodeData); + } + } + } + //force edge to write as links + foreach (var edge in edges){ + var inputNode = edge.input.node as INodeView; + var outputNode = edge.output.node as INodeView; + var links = new List(); + if (inputNode != null && outputNode != null){ + var inputNodeData = inputNode.GetNodeData(); + var outputNodeData = outputNode.GetNodeData(); + var newNodeLink = new NodeLink(new PortInfo(){ + nodeDataId = inputNodeData.id, + portName = edge.input.name + + }, new PortInfo(){ + nodeDataId = outputNodeData.id, + portName = edge.output.name + }); + } + + _data.NodeLinks = links; + + } EditorUtility.SetDirty(_data); AssetDatabase.SaveAssets(); } diff --git a/TNode/Editor/GraphEditor.cs b/TNode/Editor/GraphEditor.cs index be8fb71..6f03e24 100644 --- a/TNode/Editor/GraphEditor.cs +++ b/TNode/Editor/GraphEditor.cs @@ -19,7 +19,7 @@ namespace TNode.Editor{ [SerializeField] private VisualTreeAsset mVisualTreeAsset = default; //Persist editor data ,such as node position,node size ,etc ,in this script object - public GraphEditorData nodeEditorData; + [FormerlySerializedAs("nodeEditorData")] public GraphEditorData graphEditorData; public void CreateGUI(){ @@ -53,10 +53,6 @@ namespace TNode.Editor{ }); }); } - private void ConstructSearchWindow(){ - //Register a search window - - } private void DefineGraphEditorActions(){ //Register a event when user press ctrl + s @@ -78,8 +74,12 @@ namespace TNode.Editor{ //Create a new asset file with type of GraphDataType T asset = ScriptableObject.CreateInstance(); AssetDatabase.CreateAsset(asset, path); + AssetDatabase.SaveAssets(); } } + else{ + _graphView.SaveWithEditorData(graphEditorData); + } } diff --git a/TNode/Models/GraphData.cs b/TNode/Models/GraphData.cs index c05836f..071114a 100644 --- a/TNode/Models/GraphData.cs +++ b/TNode/Models/GraphData.cs @@ -8,6 +8,7 @@ namespace TNode.Models{ public class GraphData:ScriptableObject,ISerializationCallbackReceiver{ [SerializeField] public Dictionary NodeDictionary = new Dictionary(); + public List NodeLinks = new List(); [SerializeField] [HideInInspector] diff --git a/TNode/Models/NodeLink.cs b/TNode/Models/NodeLink.cs index 451988d..932d7e7 100644 --- a/TNode/Models/NodeLink.cs +++ b/TNode/Models/NodeLink.cs @@ -1,4 +1,5 @@ using System; +using UnityEditor.Experimental.GraphView; namespace TNode.Models{ @@ -6,6 +7,11 @@ namespace TNode.Models{ [Serializable] public class NodeLink{ // public DialogueNodePortData From{ get; } - + public PortInfo inPort; + public PortInfo outPort; + public NodeLink(PortInfo inPort, PortInfo outPort){ + this.inPort = inPort; + this.outPort = outPort; + } } } \ No newline at end of file diff --git a/TNode/Models/PortInfo.cs b/TNode/Models/PortInfo.cs new file mode 100644 index 0000000..c4d16e2 --- /dev/null +++ b/TNode/Models/PortInfo.cs @@ -0,0 +1,9 @@ +using System; + +namespace TNode.Models{ + [Serializable] + public class PortInfo{ + public string portName; + public string nodeDataId; + } +} \ No newline at end of file diff --git a/TNode/Models/PortInfo.cs.meta b/TNode/Models/PortInfo.cs.meta new file mode 100644 index 0000000..1a955f4 --- /dev/null +++ b/TNode/Models/PortInfo.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0ecb2b652f6040358a7a53a9c0128dbe +timeCreated: 1656743254 \ No newline at end of file diff --git a/TNode/Runtime.meta b/TNode/Runtime.meta new file mode 100644 index 0000000..09487c8 --- /dev/null +++ b/TNode/Runtime.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 55d69b37800145e5a539b123858c6e8d +timeCreated: 1656749818 \ No newline at end of file diff --git a/TNode/Runtime/RuntimeGraph.cs b/TNode/Runtime/RuntimeGraph.cs new file mode 100644 index 0000000..55e11d1 --- /dev/null +++ b/TNode/Runtime/RuntimeGraph.cs @@ -0,0 +1,9 @@ +using TNode.Models; +using UnityEngine; + +namespace TNode.Runtime{ + public class RuntimeGraph:MonoBehaviour{ + public GraphData graphData; + + } +} \ No newline at end of file diff --git a/TNode/Runtime/RuntimeGraph.cs.meta b/TNode/Runtime/RuntimeGraph.cs.meta new file mode 100644 index 0000000..70bddf7 --- /dev/null +++ b/TNode/Runtime/RuntimeGraph.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 325720a2ce64404d917fff20d22e80f2 +timeCreated: 1656749830 \ No newline at end of file diff --git a/TNode/Runtime/RuntimeNode.cs b/TNode/Runtime/RuntimeNode.cs new file mode 100644 index 0000000..1647821 --- /dev/null +++ b/TNode/Runtime/RuntimeNode.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; +using TNode.Models; + +namespace TNode.Runtime{ + public class RuntimeNode{ + public NodeData NodeData { get; set; } + public List NodeLinks; + } +} \ No newline at end of file diff --git a/TNode/Runtime/RuntimeNode.cs.meta b/TNode/Runtime/RuntimeNode.cs.meta new file mode 100644 index 0000000..cfa7617 --- /dev/null +++ b/TNode/Runtime/RuntimeNode.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3a2646b5000e4820bb27eeb76be4421a +timeCreated: 1656749916 \ No newline at end of file