Tanoda
pb_SaveQuizButton.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3using System.Collections;
4using System.Collections.Generic;
5using System.IO;
6using System.Linq;
7using System.Runtime.InteropServices;
8using System.Text;
9using B83.Win32;
10using GILES.Interface;
11using UnityEngine.UI;
12
13namespace GILES
14{
19 {
21
22 public Dropdown type;
23
24 public InputField Q, A, B, C, D;
25
26 public Toggle Ta, Tb, Tc, Td;
27
28 public Text timeout;
29
30 public override string tooltip { get { return Macro.T("BTN_SAVE_QUIZ"); } }
31
35 public void OpenSavePanel()
36 {
37 pb_FileDialog dlog = GameObject.Instantiate(dialogPrefab);
38 dlog.SetDirectory(Path.Combine(Application.streamingAssetsPath, "quizes"));
39 dlog.isFileBrowser = true;
40 dlog.filePattern = "*.qiz";
41 dlog.AddOnSaveListener(OnSave);
42
43 pb_ModalWindow.SetContent(dlog.gameObject);
44 pb_ModalWindow.SetTitle("BTN_SAVE_QUIZ");
46 }
47
48 private void OnSave(string path)
49 {
50 Save(path);
51 }
52
53 public void Save(string path)
54 {
55 LoadingManager.instance.SetLoading();
56 LoadingManager.instance.ShowWindow();
57
58 var newData = Encoding.UTF8.GetBytes(type.value + "\n" + Ta.isOn + "\n" + Tb.isOn + "\n" + Tc.isOn + "\n" + Td.isOn + "\n" +
59 Convert.ToBase64String(Encoding.UTF8.GetBytes(Q.text)) + "\n" +
60 Convert.ToBase64String(Encoding.UTF8.GetBytes(A.text)) + "\n" +
61 Convert.ToBase64String(Encoding.UTF8.GetBytes(B.text)) + "\n" +
62 Convert.ToBase64String(Encoding.UTF8.GetBytes(C.text)) + "\n" +
63 Convert.ToBase64String(Encoding.UTF8.GetBytes(D.text)) + "\n" +
64 Convert.ToBase64String(Encoding.UTF8.GetBytes(timeout.text)));
65#if UNITY_WEBGL
66 if(!path.EndsWith(".qiz"))
67 path += ".qiz";
68
69 var fdd = FindObjectOfType<FileDragAndDrop>();
70 var t = fdd.paretnObject.transform;
71 var allModel = t.GetComponentsInChildren<UploadedFile>(true);
72 var upFile = allModel.FirstOrDefault(x => x.fileName == path.Remove(0, path.LastIndexOf("/") + 1));
73 if (upFile)
74 {
75 upFile.modelData = newData;
76 }
77 else
78 {
79 var go = new GameObject(path.Remove(0, path.LastIndexOf("/") + 1));
80 go.transform.SetParent(t);
81 upFile = go.AddComponent<UploadedFile>();
82 upFile.fileName = path.Remove(0, path.LastIndexOf("/") + 1);
83 upFile.modelData = newData;
84 upFile.global = false;
85 }
86
88#else
89 string san = pb_FileUtility.SanitizePath(path);
90
91 if(!san.EndsWith(".qiz"))
92 san += ".qiz";
93 if(!path.EndsWith(".qiz"))
94 path += ".qiz";
95
96 if(!pb_FileUtility.IsValidPath(san, ".qiz"))
97 {
98 Debug.LogWarning(san + " is not a valid path.");
99 return;
100 }
101 File.WriteAllBytes(path, newData);
102 FileDragAndDrop.instance.OnFiles(new List<string>(){path.Replace('/', '\\')}, new POINT(){x = -1, y = -1} );
103#endif
104 LoadingManager.instance.HideWindow();
105 var allQa = FindObjectsOfType<QuizAction>();
106 foreach (var quizAction in allQa)
107 {
108 quizAction.FillDropdown();
109 }
110
111#if PB_DEBUG
112 if(System.IO.File.Exists(path))
113 System.Diagnostics.Process.Start(path);
114#endif
115 }
116 }
117}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void StartLateRebuild(bool force=false)
static FileDragAndDrop instance
void OnFiles(List< string > aFiles, POINT aPos)
string filePattern
If isFileBrowser is true, this string my be used to filter file results (see https://msdn....
void AddOnSaveListener(Callback< string > listener)
void SetDirectory(string directory)
static void SetTitle(string title)
static void SetContent(GameObject prefab)
void Save(string path)
Definition: Macro.cs:12
static string T(string key)
Definition: Macro.cs:19
byte[] modelData
Definition: UploadedFile.cs:7
string fileName
Definition: UploadedFile.cs:8
Definition: B83.Win32.cs:38