-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathStoryGraph.cs
More file actions
138 lines (125 loc) · 4.86 KB
/
StoryGraph.cs
File metadata and controls
138 lines (125 loc) · 4.86 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.Linq;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
using Subtegral.DialogueSystem.DataContainers;
namespace Subtegral.DialogueSystem.Editor
{
public class StoryGraph : EditorWindow
{
private string _fileName;
private string _filePath;
private StoryGraphView _graphView;
private DialogueContainer _dialogueContainer;
[MenuItem("Graph/Narrative Graph")]
public static void CreateGraphViewWindow()
{
var window = GetWindow<StoryGraph>();
window.titleContent = new GUIContent("Narrative Graph");
}
private void ConstructGraphView()
{
_graphView = new StoryGraphView(this)
{
name = "Narrative Graph",
};
_graphView.StretchToParentSize();
rootVisualElement.Add(_graphView);
}
private void RegenerateToolbar()
{
// remove the old toolbar
rootVisualElement.Remove(rootVisualElement.Q<Toolbar>());
// generate a new toolbar
GenerateToolbar();
}
private void GenerateToolbar()
{
var toolbar = new Toolbar();
toolbar.Add(new Button(() => RequestDataOperation(0)) {text = "New"});
toolbar.Add(new Button(() => RequestDataOperation(1)) {text = "Save"});
toolbar.Add(new Button(() => RequestDataOperation(2)) {text = "Load"});
if (_fileName != string.Empty) {
var fileNameTextField = new Label($"File Name: {_fileName}");
toolbar.Add(fileNameTextField);
}
rootVisualElement.Add(toolbar);
}
private void RequestDataOperation(byte option)
{
var saveUtility = GraphSaveUtility.GetInstance(_graphView);
switch (option) {
case 0:
{
_fileName = string.Empty;
_filePath = string.Empty;
rootVisualElement.Remove(_graphView);
ConstructGraphView();
RegenerateToolbar();
GenerateMiniMap();
GenerateBlackBoard();
break;
}
case 1:
{
if (_filePath != string.Empty) {
saveUtility.SaveGraph(_filePath);
} else saveUtility.SaveGraph(out _filePath);
Debug.Log($"Saved Narrative at: {_filePath}");
_fileName = _filePath.Split('/').Last();
_fileName = _fileName[..^6];
RegenerateToolbar();
break;
}
case 2:
{
saveUtility.LoadNarrative(out _filePath, out _fileName);
RegenerateToolbar();
break;
}
}
}
private void OnEnable()
{
ConstructGraphView();
GenerateToolbar();
GenerateMiniMap();
GenerateBlackBoard();
}
private void GenerateMiniMap()
{
var miniMap = new MiniMap {anchored = true};
var cords = _graphView.contentViewContainer.WorldToLocal(new Vector2(this.maxSize.x - 10, 30));
miniMap.SetPosition(new Rect(cords.x, cords.y, 200, 140));
_graphView.Add(miniMap);
}
private void GenerateBlackBoard()
{
var blackboard = new Blackboard(_graphView);
blackboard.Add(new BlackboardSection {title = "Exposed Variables"});
blackboard.addItemRequested = _ =>
{
_graphView.AddPropertyToBlackBoard(ExposedProperty.CreateInstance(), false);
};
blackboard.editTextRequested = (_, element, newValue) =>
{
var oldPropertyName = ((BlackboardField) element).text;
if (_graphView.ExposedProperties.Any(x => x.PropertyName == newValue))
{
EditorUtility.DisplayDialog("Error", "This property name already exists, please chose another one.",
"OK");
return;
}
var targetIndex = _graphView.ExposedProperties.FindIndex(x => x.PropertyName == oldPropertyName);
_graphView.ExposedProperties[targetIndex].PropertyName = newValue;
((BlackboardField) element).text = newValue;
};
blackboard.SetPosition(new Rect(10,30,200,300));
_graphView.Add(blackboard);
_graphView.Blackboard = blackboard;
}
private void OnDisable() => rootVisualElement.Remove(_graphView);
}
}