This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
GdkToolsConfigurationWindow.cs
190 lines (158 loc) · 7.52 KB
/
GdkToolsConfigurationWindow.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Improbable.Gdk.Tools
{
/// <summary>
/// Defines a custom inspector window that allows you to configure the GDK Tools.
/// </summary>
public class GdkToolsConfigurationWindow : EditorWindow
{
internal const string SchemaStdLibDirLabel = "Standard library";
internal const string CodegenOutputDirLabel = "C# output directory";
internal const string DescriptorOutputDirLabel = "Descriptor directory";
internal const string DevAuthTokenDirLabel = "Token directory";
private const string SchemaSourceDirsLabel = "Schema sources";
private const string DevAuthTokenSectionLabel = "Dev Auth Token Settings";
private const string DevAuthTokenLifetimeLabel = "Token lifetime (days)";
private const string CodeGeneratorLabel = "Code generator";
private const string CustomSnapshotPathLabel = "Selected snapshot path";
private const string ResetConfigurationButtonText = "Reset to default";
private const string SaveConfigurationButtonText = "Save";
private static GUIContent AddSchemaDirButton;
private static GUIContent RemoveSchemaDirButton;
private GdkToolsConfiguration toolsConfig;
private List<string> configErrors = new List<string>();
private Vector2 scrollPosition;
[MenuItem("SpatialOS/GDK tools configuration", false, MenuPriorities.GdkToolsConfiguration)]
public static void ShowWindow()
{
GetWindow<GdkToolsConfigurationWindow>().Show();
}
private void OnEnable()
{
if (toolsConfig != null)
{
return;
}
titleContent = new GUIContent("GDK Tools");
toolsConfig = GdkToolsConfiguration.GetOrCreateInstance();
Undo.undoRedoPerformed += () => { configErrors = toolsConfig.Validate(); };
}
public void OnGUI()
{
if (AddSchemaDirButton == null)
{
AddSchemaDirButton = new GUIContent(EditorGUIUtility.IconContent("Toolbar Plus"))
{ tooltip = "Add schema directory" };
RemoveSchemaDirButton = new GUIContent(EditorGUIUtility.IconContent("Toolbar Minus"))
{ tooltip = "Remove schema directory" };
}
using (new EditorGUILayout.VerticalScope())
using (var scroll = new EditorGUILayout.ScrollViewScope(scrollPosition))
using (var check = new EditorGUI.ChangeCheckScope())
{
using (new EditorGUILayout.HorizontalScope(EditorStyles.toolbar))
{
var canSave = configErrors.Count > 0;
using (new EditorGUI.DisabledScope(canSave))
{
if (GUILayout.Button(SaveConfigurationButtonText, EditorStyles.toolbarButton))
{
toolsConfig.Save();
}
}
if (GUILayout.Button(ResetConfigurationButtonText, EditorStyles.toolbarButton))
{
if (EditorUtility.DisplayDialog("Confirmation", "Are you sure you want to reset to defaults?",
"Yes", "No"))
{
toolsConfig.ResetToDefault();
}
}
}
DrawCodeGenerationOptions();
DrawCustomSnapshotDir();
if (check.changed)
{
configErrors = toolsConfig.Validate();
}
scrollPosition = scroll.scrollPosition;
}
foreach (var error in configErrors)
{
EditorGUILayout.HelpBox(error, MessageType.Error);
}
}
private void DrawCodeGenerationOptions()
{
var previousWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 180;
GUILayout.Label(CodeGeneratorLabel, EditorStyles.boldLabel);
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);
using (new EditorGUIUtility.IconSizeScope(new Vector2(12, 12)))
using (new EditorGUI.IndentLevelScope())
{
toolsConfig.CodegenOutputDir =
EditorGUILayout.TextField(CodegenOutputDirLabel, toolsConfig.CodegenOutputDir);
toolsConfig.DescriptorOutputDir =
EditorGUILayout.TextField(DescriptorOutputDirLabel, toolsConfig.DescriptorOutputDir);
GUILayout.Label(SchemaSourceDirsLabel, EditorStyles.boldLabel);
for (var i = 0; i < toolsConfig.SchemaSourceDirs.Count; i++)
{
using (new EditorGUILayout.HorizontalScope())
{
toolsConfig.SchemaSourceDirs[i] =
EditorGUILayout.TextField($"Schema path [{i}]", toolsConfig.SchemaSourceDirs[i]);
if (GUILayout.Button(RemoveSchemaDirButton, EditorStyles.miniButton,
GUILayout.ExpandWidth(false)))
{
toolsConfig.SchemaSourceDirs.RemoveAt(i);
}
}
}
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
if (GUILayout.Button(AddSchemaDirButton, EditorStyles.miniButton))
{
toolsConfig.SchemaSourceDirs.Add(string.Empty);
}
}
}
GUILayout.Label(DevAuthTokenSectionLabel, EditorStyles.boldLabel);
using (new EditorGUI.IndentLevelScope())
{
toolsConfig.DevAuthTokenLifetimeDays =
EditorGUILayout.IntSlider(DevAuthTokenLifetimeLabel, toolsConfig.DevAuthTokenLifetimeDays, 1, 90);
toolsConfig.SaveDevAuthTokenToFile = EditorGUILayout.Toggle("Save token to file", toolsConfig.SaveDevAuthTokenToFile);
using (new EditorGUI.DisabledScope(!toolsConfig.SaveDevAuthTokenToFile))
{
toolsConfig.DevAuthTokenDir = EditorGUILayout.TextField(DevAuthTokenDirLabel, toolsConfig.DevAuthTokenDir);
GUILayout.Label($"Token filepath: {toolsConfig.DevAuthTokenFilepath}", EditorStyles.helpBox);
}
}
EditorGUIUtility.labelWidth = previousWidth;
}
private void DrawCustomSnapshotDir()
{
GUILayout.Label(CustomSnapshotPathLabel, EditorStyles.boldLabel);
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);
using (new EditorGUILayout.HorizontalScope())
{
using (new EditorGUI.DisabledScope(true))
{
EditorGUILayout.TextField(CustomSnapshotPathLabel, toolsConfig.CustomSnapshotPath);
}
if (GUILayout.Button("Open", EditorStyles.miniButtonRight, GUILayout.ExpandWidth(false)))
{
var path = EditorUtility.OpenFilePanel("Select snapshot", toolsConfig.CustomSnapshotPath, "snapshot");
if (!string.IsNullOrEmpty(path))
{
toolsConfig.CustomSnapshotPath = path;
}
}
}
}
}
}