Repository navigation
Expand file tree
/
Copy pathDialogEditor.cs
More file actions
177 lines (150 loc) · 5.76 KB
/
Copy pathDialogEditor.cs
File metadata and controls
177 lines (150 loc) · 5.76 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
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
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#if UNITY_EDITOR
using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Services.Dialog.v1;
using UnityEditor;
using UnityEngine;
using System.IO;
using IBM.Watson.DeveloperCloud.Logging;
namespace IBM.Watson.DeveloperCloud.Editor
{
class DialogEditor : EditorWindow
{
private void OnEnable()
{
#if UNITY_5
titleContent.text = "Dialog Editor";
#endif
m_WatsonIcon = (Texture2D)Resources.Load(Constants.Resources.WATSON_ICON, typeof(Texture2D));
Runnable.EnableRunnableInEditor();
}
[MenuItem("Watson/Dialog Editor", false, 1)]
private static void EditConfig()
{
DialogEditor window = GetWindow<DialogEditor>();
window.Show(true);
}
private Texture m_WatsonIcon = null;
private Vector2 m_ScrollPos = Vector2.zero;
private Dialog m_Dialog = new Dialog();
private Dialogs m_Dialogs = null;
private string m_NewDialogName = null;
private bool m_Refreshing = false;
private string m_CurrentDirectory = Application.dataPath;
private void OnGetDialogs(Dialogs dialogs)
{
m_Dialogs = dialogs;
m_Refreshing = false;
}
private void OnDeleteDialog(bool success)
{
if (!success)
Log.Error("DialogEditor", "Failed to delete dialog.");
else
OnRefresh();
}
private void OnDownloadDialog(bool success)
{
if (!success)
Log.Error("DialogEditor", "Failed to download dialog.");
else
Log.Status("DialogEditor", "Dialog downloaded.");
}
private void OnUploadDialog(string dialogId)
{
if (string.IsNullOrEmpty(dialogId))
Log.Error("DialogEditor", "Failed to upload dialog.");
else
OnRefresh();
}
private void OnRefresh()
{
if (!m_Refreshing)
{
if (!m_Dialog.GetDialogs(OnGetDialogs))
Log.Error("DialogEditor", "Failed to request dialogs.");
else
m_Refreshing = true;
}
}
private void OnGUI()
{
GUILayout.Label(m_WatsonIcon);
m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos);
if (m_Refreshing)
{
EditorGUI.BeginDisabledGroup(true);
GUILayout.Button("Refreshing...");
EditorGUI.EndDisabledGroup();
}
else if (m_Dialogs == null || GUILayout.Button("Refresh"))
OnRefresh();
EditorGUILayout.LabelField("Dialogs:");
EditorGUILayout.BeginVertical();
EditorGUI.indentLevel += 1;
if (m_Dialogs != null)
{
for (int i = 0; i < m_Dialogs.dialogs.Length; ++i)
{
BeginWindows();
EditorGUILayout.BeginHorizontal();
DialogEntry d = m_Dialogs.dialogs[i];
EditorGUILayout.LabelField(string.Format("Name: {0}, ID: {1}", d.name, d.dialog_id));
if (GUILayout.Button("Delete"))
{
if (EditorUtility.DisplayDialog("Confirm", string.Format("Confirm delete of dialog {0}", d.dialog_id), "YES", "NO"))
m_Dialog.DeleteDialog(d.dialog_id, OnDeleteDialog);
}
if (GUILayout.Button("Download"))
{
var path = EditorUtility.SaveFilePanel("Save Dialog", m_CurrentDirectory, "Dialog", "xml");
if (!string.IsNullOrEmpty(path))
{
m_CurrentDirectory = Path.GetDirectoryName(path);
m_Dialog.DownloadDialog(d.dialog_id, path, OnDownloadDialog);
}
}
EditorGUILayout.EndHorizontal();
EndWindows();
}
}
EditorGUILayout.EndVertical();
EditorGUI.indentLevel -= 1;
EditorGUILayout.LabelField("Upload Dialog:");
EditorGUI.indentLevel += 1;
m_NewDialogName = EditorGUILayout.TextField("Name", m_NewDialogName);
if (GUILayout.Button("Upload"))
{
var path = EditorUtility.OpenFilePanel("Select Dialog File", m_CurrentDirectory, "xml");
if (!string.IsNullOrEmpty(path))
{
m_CurrentDirectory = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(m_NewDialogName))
m_NewDialogName = Path.GetFileNameWithoutExtension(path);
if (!m_Dialog.UploadDialog(m_NewDialogName, OnUploadDialog, path))
Log.Error("DialogEditor", "Failed to upload dialog.");
}
m_NewDialogName = null;
}
EditorGUI.indentLevel -= 1;
EditorGUILayout.EndScrollView();
EndWindows();
}
}
}
#endif