-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathGraphSaveUtility.cs
More file actions
138 lines (125 loc) · 5.13 KB
/
GraphSaveUtility.cs
File metadata and controls
138 lines (125 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using Subtegral.DialogueSystem.DataContainers;
using UnityEngine.UIElements;
namespace Subtegral.DialogueSystem.Editor
{
public class GraphSaveUtility
{
private List<Edge> Edges => _graphView.edges.ToList();
private List<DialogueNode> Nodes => _graphView.nodes.ToList().Cast<DialogueNode>().ToList();
private DialogueContainer _dialogueContainer;
private StoryGraphView _graphView;
public static GraphSaveUtility GetInstance(StoryGraphView graphView)
{
return new GraphSaveUtility
{
_graphView = graphView
};
}
public void SaveNodes(string filePath)
{
if (!Edges.Any()) return;
var dialogueContainerObject = ScriptableObject.CreateInstance<DialogueContainer>();
var connectedSockets = Edges.Where(x => x.input.node != null).ToArray();
for (var i = 0; i < connectedSockets.Count(); i++)
{
var outputNode = (connectedSockets[i].output.node as DialogueNode);
var inputNode = (connectedSockets[i].input.node as DialogueNode);
dialogueContainerObject.NodeLinks.Add(new NodeLinkData
{
BaseNodeGUID = outputNode.GUID,
PortName = connectedSockets[i].output.portName,
TargetNodeGUID = inputNode.GUID
});
}
foreach (var node in Nodes.Where(node => !node.EntyPoint))
{
dialogueContainerObject.DialogueNodeData.Add(new DialogueNodeData
{
NodeGUID = node.GUID,
DialogueText = node.DialogueText,
Position = node.GetPosition().position
});
}
AssetDatabase.CreateAsset(dialogueContainerObject, filePath);
AssetDatabase.SaveAssets();
}
public void LoadNarrative(string filePath)
{
_dialogueContainer = AssetDatabase.LoadAssetAtPath<DialogueContainer>(filePath);
if (_dialogueContainer == null)
{
EditorUtility.DisplayDialog("File Not Found", "Target Narrative Data does not exist!", "OK");
return;
}
ClearGraph();
GenerateDialogueNodes();
ConnectDialogueNodes();
}
/// <summary>
/// Set Entry point GUID then Get All Nodes, remove all and their edges. Leave only the entrypoint node. (Remove its edge too)
/// </summary>
private void ClearGraph()
{
Nodes.Find(x => x.EntyPoint).GUID = _dialogueContainer.NodeLinks[0].BaseNodeGUID;
foreach (var perNode in Nodes)
{
if (perNode.EntyPoint) continue;
Edges.Where(x => x.input.node == perNode).ToList()
.ForEach(edge => _graphView.RemoveElement(edge));
_graphView.RemoveElement(perNode);
}
}
/// <summary>
/// Create All serialized nodes and assign their guid and dialogue text to them
/// </summary>
private void GenerateDialogueNodes()
{
foreach (var perNode in _dialogueContainer.DialogueNodeData)
{
var tempNode = _graphView.CreateNode(perNode.DialogueText);
tempNode.GUID = perNode.NodeGUID;
_graphView.AddElement(tempNode);
var nodePorts = _dialogueContainer.NodeLinks.Where(x => x.BaseNodeGUID == perNode.NodeGUID).ToList();
nodePorts.ForEach(x => _graphView.AddChoicePort(tempNode, x.PortName));
}
}
private void ConnectDialogueNodes()
{
for (var i = 0; i < Nodes.Count; i++)
{
var k = i; //Prevent access to modified closure
var connections = _dialogueContainer.NodeLinks.Where(x => x.BaseNodeGUID == Nodes[k].GUID).ToList();
for (var j = 0; j < connections.Count(); j++)
{
var targetNodeGUID = connections[j].TargetNodeGUID;
var targetNode = Nodes.First(x => x.GUID == targetNodeGUID);
LinkNodesTogether(Nodes[i].outputContainer[j].Q<Port>(), (Port)targetNode.inputContainer[0]);
targetNode.SetPosition(new Rect(
_dialogueContainer.DialogueNodeData.First(x => x.NodeGUID == targetNodeGUID).Position,
_graphView.DefaultNodeSize));
}
}
}
private void LinkNodesTogether(Port outputSocket, Port inputSocket)
{
var tempEdge = new Edge()
{
output = outputSocket,
input = inputSocket
};
tempEdge?.input.Connect(tempEdge);
tempEdge?.output.Connect(tempEdge);
_graphView.Add(tempEdge);
}
}
}