Tanoda
pb_FileDialog.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
3using System.Collections;
4using System.Collections.Generic;
5using GILES;
6using System.IO;
7using System.Linq;
9
10namespace GILES.Interface
11{
16 public class pb_FileDialog : MonoBehaviour
17 {
19 private Stack<string> back = new Stack<string>();
20 private Stack<string> forward = new Stack<string>();
21
23 public bool limitToCourses = false;
24
26 public GameObject scrollContent;
27
30
32 public InputField directoryCrumbsField;
33
35 public InputField fileInputField;
36
38 public string currentDirectory;
39
42
45
48
51 public bool isFileBrowser { get { return _isFileBrowser; } set { _isFileBrowser = value; UpdateDirectoryContents(); } }
52
53 private bool _isFileBrowser = false;
54
56 public string filePattern { get { return _filePattern; } set { _filePattern = value; UpdateDirectoryContents(); } }
57
58 private string _filePattern = "";
59
62 public Callback<string> OnSave;
63
66
68
72 public void AddOnSaveListener(Callback<string> listener)
73 {
74 if(OnSave != null)
75 OnSave += listener;
76 else
77 OnSave = listener;
78 }
79
83 public void AddOnCancelListener(Callback listener)
84 {
85 if(OnCancel != null)
86 OnCancel += listener;
87 else
88 OnCancel = listener;
89 }
90
91 private bool mInitialized = false;
92
93 void Awake()
94 {
95 if(!mInitialized)
96 Initialize();
97 }
98
99 void Initialize()
100 {
101 mInitialized = true;
102
103 backButton.onClick.RemoveAllListeners();
104 forwardButton.onClick.RemoveAllListeners();
105 upButton.onClick.RemoveAllListeners();
106 cancelButton.onClick.RemoveAllListeners();
107 saveButton.onClick.RemoveAllListeners();
108
109 backButton.onClick.AddListener( Back );
110 forwardButton.onClick.AddListener( Forward );
111 upButton.onClick.AddListener( OpenParentDirectory );
112 cancelButton.onClick.AddListener( Cancel );
113 saveButton.onClick.AddListener( Save );
114
115 UpdateNavButtonInteractibility();
116 }
117
121 public void SetDirectory(string directory)
122 {
123 if(!mInitialized)
124 Initialize();
125#if !UNITY_WEBGL
126
127 if( ValidDir(directory) )
128 {
129 forward.Clear();
130
131 if( ValidDir(currentDirectory) )
132 back.Push(currentDirectory);
133
134 currentDirectory = directory;
135 }
136#endif
137
139 }
140
145 {
146 // Debug.Log("scanning: " + currentDirectory);
147#if UNITY_WEBGL || USENETWORK
148 string[] children = null;
149#else
150 string[] children = Directory.GetDirectories(currentDirectory);
151#endif
152
153 UpdateNavButtonInteractibility();
154
155 // hide the contents while working with them, otherwise you get flashes and artifacts
156 scrollContent.SetActive(false);
157
158 ClearScrollRect();
159
160 int i = 0;
161 if (!limitToCourses)
162 {
163 if(children != null)
164 {
166
167 for(int n = 0; n < children.Length; n++)
168 {
169 pb_SaveDialogButton button = GameObject.Instantiate(rowButtonPrefab);
170 button.SetDelegateAndPath(SetDirectory, children[n]);
171
172 pb_GUIStyleApplier style = button.GetComponent<pb_GUIStyleApplier>();
173 style.style = i++ % 2 == 0 ? evenRowStyle : oddRowStyle;
174 style.ApplyStyle();
175
176 button.transform.SetParent(scrollContent.transform);
177 }
178 }
179 }
180
181 if(isFileBrowser)
182 {
183 if (limitToCourses)
184 {
185 //children = GetFiles(currentDirectory, new[] {"*.json"}).ToArray();
186#if UNITY_WEBGL || USENETWORK
187 switch (manualLogonCourseType)
188 {
189 case CourseType.Normal:
190 StartCoroutine(NetworkManager.instance.GetMyCurses(courses =>
191 {
192 children = CourseListLogic(children, courses, ref i);
193 }));
194 break;
195 case CourseType.Quality:
196 StartCoroutine(NetworkManager.instance.GetQualityMyCurses(courses =>
197 {
198 children = CourseListLogic(children, courses, ref i);
199 }));
200 break;
201 case CourseType.Dobot:
202 StartCoroutine(NetworkManager.instance.GetDobotMyCurses(courses =>
203 {
204 children = CourseListLogic(children, courses, ref i);
205 }));
206 break;
207 }
208 return;
209#else
210
211 StartCoroutine(NetworkManager.instance.GetMyCurses(courses =>
212 {
213 children = NetworkManager.instance.CourseArrayToNameList(courses, out var ids).ToArray();
214 for(int n = 0; n < children.Length; n++)
215 {
216 pb_SaveDialogButton button = GameObject.Instantiate(rowButtonPrefab);
217 button.SetDelegateAndPath(SetFile, children[n]);
218
219 pb_GUIStyleApplier style = button.GetComponent<pb_GUIStyleApplier>();
220 style.style = i++ % 2 == 0 ? evenRowStyle : oddRowStyle;
221 style.ApplyStyle();
222
223 button.transform.SetParent(scrollContent.transform);
224
225 }
226
227 scrollContent.SetActive(true);
228 }));
229 return;
230#endif
231 }
232 else if (filePattern.Contains('|'))
233 {
234 children = GetFiles(currentDirectory, filePattern.Split('|')).ToArray();
235 }
236 else
237 {
238#if UNITY_WEBGL || USENETWORK
239 children = new string[0];
240
241 var fdd = FindObjectOfType<FileDragAndDrop>();
242 var t = fdd.paretnObject.transform;
243 var allModel = t.GetComponentsInChildren<UploadedFile>(true);
244 var endswith = filePattern.Replace("*", "");
245 foreach (var uploadedFile in allModel)
246 {
247 if (uploadedFile.fileName.EndsWith(endswith))
248 {
249 children = children.Concat(new[] {uploadedFile.fileName}).ToArray();
250 }
251 }
252#else
253 children = Directory.GetFiles(currentDirectory, string.IsNullOrEmpty(filePattern) ? "*" : filePattern);
254#endif
255 }
256
257 for(int n = 0; n < children.Length; n++)
258 {
259 pb_SaveDialogButton button = GameObject.Instantiate(rowButtonPrefab);
260 button.SetDelegateAndPath(SetFile, children[n]);
261
262 pb_GUIStyleApplier style = button.GetComponent<pb_GUIStyleApplier>();
263 style.style = i++ % 2 == 0 ? evenRowStyle : oddRowStyle;
264 style.ApplyStyle();
265
266 button.transform.SetParent(scrollContent.transform);
267
268 }
269 }
270
271 scrollContent.SetActive(true);
272 }
273
274 private string[] CourseListLogic(string[] children, NetworkManager.WSCourses courses, ref int i)
275 {
276 children = NetworkManager.instance.CourseArrayToNameList(courses, out var ids).ToArray();
277 for (int n = 0; n < children.Length; n++)
278 {
279 pb_SaveDialogButton button = GameObject.Instantiate(rowButtonPrefab);
280 button.SetDelegateAndPath(SetFile, children[n]);
281 var index = n;
282 if (SavedUser.instance.wsUser.HasValue && SavedUser.instance.wsUser.Value.groupId != UserManager.UserType.User)
283 {
284 button.transform.GetChild(1).GetComponent<Button>().onClick.AddListener(() =>
285 {
286 Debug.Log(courses.courses[index].id);
287 Debug.Log(courses.courses[index].name);
288 YesNoPopupManager.instance.InvokeOnYes(() => StartCoroutine(NetworkManager.instance.DeleteCurseByID(
289 courses.courses[index].id,
290 () =>
291 {
292 Debug.Log($"course {courses.courses[index].name}, deleted successfuly");
293 UpdateDirectoryContents();
294 })));
295 YesNoPopupManager.instance.ShowPopup("Are you sure?",
296 $"Do you want to delete course '{children[index]}'?");
297 });
298 }
299 else
300 {
301 Destroy(button.transform.GetChild(1));
302 }
303
304 pb_GUIStyleApplier style = button.GetComponent<pb_GUIStyleApplier>();
305 style.style = i++ % 2 == 0 ? evenRowStyle : oddRowStyle;
306 style.ApplyStyle();
307
308 button.transform.SetParent(scrollContent.transform);
309 }
310
311 scrollContent.SetActive(true);
312 return children;
313 }
314
315 public static IEnumerable<string> GetFiles(string path,
316 string[] searchPatterns,
317 SearchOption searchOption = SearchOption.TopDirectoryOnly)
318 {
319 return searchPatterns.AsParallel()
320 .SelectMany(searchPattern =>
321 Directory.EnumerateFiles(path, searchPattern, searchOption));
322 }
323
324 private void ClearScrollRect()
325 {
326 foreach(Transform t in scrollContent.transform)
327 pb_ObjectUtility.Destroy(t.gameObject);
328 }
329
330 private bool ValidDir(string dir)
331 {
332 return !string.IsNullOrEmpty(dir) && Directory.Exists(dir);
333 }
334
335 private void UpdateNavButtonInteractibility()
336 {
337 backButton.interactable = back.Count > 0;
338 forwardButton.interactable = forward.Count > 0;
339 upButton.interactable = ValidDir(currentDirectory) && Directory.GetParent(currentDirectory) != null;
340 }
341
343 {
344 DirectoryInfo parent = Directory.GetParent(currentDirectory);
345
346 if(parent == null)
347 return;
348
349 SetDirectory(parent.FullName);
350 }
351
352 public void SetFile(string path)
353 {
354 fileInputField.text = Path.GetFileName(path);
355 }
356
361 public void Back()
362 {
363 if(back.Count > 0)
364 {
365 forward.Push(currentDirectory);
366 currentDirectory = back.Pop();
367 UpdateDirectoryContents();
368 }
369 }
370
371 public void Forward()
372 {
373 if(forward.Count > 0)
374 {
375 back.Push(currentDirectory);
376 currentDirectory = forward.Pop();
377 UpdateDirectoryContents();
378 }
379 }
380
385 public void Cancel()
386 {
387 if(OnCancel != null)
388 OnCancel();
389
391 }
392
397 public void Save()
398 {
399 if(OnSave != null)
400 OnSave( currentDirectory + "/" + GetFilePath() );
401 else
402 Debug.LogWarning("File dialog was dismissed by user but no callback is registered to perform the action!");
403
405 }
406
410 private string GetFilePath()
411 {
412 string path = fileInputField.text;
413 return path;
414 }
415 }
416}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
bool limitToCourses
Only show training files.
pb_GUIStyle oddRowStyle
pb_GUIStyle to apply to odd and even rows.
Callback< string > OnSave
InputField directoryCrumbsField
The input field that shows the directory path.
GameObject scrollContent
Where to put current directory folder buttons.
string currentDirectory
The directory currently being inspected.
pb_SaveDialogButton rowButtonPrefab
The prefab to populate scrollview contents with.
Button backButton
Buttons to navigate folder structures.
InputField fileInputField
The input field that allows user to type in file or folder name.
Button saveButton
Save and cancel buttons. onClick delegates will automatically be added by this script.
string filePattern
If isFileBrowser is true, this string my be used to filter file results (see https://msdn....
Callback OnCancel
Called if the user cancels this action.
void AddOnCancelListener(Callback listener)
void AddOnSaveListener(Callback< string > listener)
static IEnumerable< string > GetFiles(string path, string[] searchPatterns, SearchOption searchOption=SearchOption.TopDirectoryOnly)
void SetDirectory(string directory)
void SetDelegateAndPath(Callback< string > del, string path)
delegate void Callback()
SavedUser.CourseType CourseType
Definition: pb_FileDialog.cs:8