Tanoda
QuizManager.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Globalization;
4using System.IO;
5using System.Linq;
6using GILES;
7using NaughtyAttributes;
8using UnityEngine;
9using UnityEngine.Networking;
10using UnityEngine.UI;
11
12public class QuizManager : pb_MonoBehaviourSingleton<QuizManager>
13{
14 [Serializable]
15 public class QuizData
16 {
17 [TextArea]
18 public string Question;
19 public bool UsePictures = true;
20 public string[] AnswerArray;
21 public int[] RightIndex;
22 public byte QuizType; // 0: normál 1:többszörös
23 public float TimeToComplete;
24 }
25
27 public RawImage image1, image2, image3, image4;
28 public ToggleGroup tg;
29 public GameObject checkMulti;
30 [HideInInspector] public QuizAction currentAction;
31 private int[] goodAnswer;
32 private bool[] currentAnswers = new []{false,false,false,false};
33 private byte quizType = 255;
34 private float quizTimeLeft = 999f;
35
36 [Header("Quiz Editor helpers")]
37 public InputField timeoutEditor;
38
39 void Start()
40 {
41 gameObject.SetActive(false);
42#if UNITY_EDITOR
43 var qd = new QuizData
44 {
45 Question = "Fontos mosolyogni a gyár területén?",
46 AnswerArray = new []{"Igen", "Nem"},
47 RightIndex = new []{1},
48 UsePictures = false,
49 QuizType = 0,
50 TimeToComplete = 80f
51 };
52 var qdpics = new QuizData
53 {
54 Question = "Melyik képen látható hónapos retek?",
55 AnswerArray = new []{"https://cdn.discordapp.com/attachments/679671608838848546/718119485956947978/sassss.png", "itt befigyel egy text is", "http://retek.hu/retek.jpg", "https://cdn.discordapp.com/attachments/270988546653814784/718182644482965644/IMG_20200604_081746.png"},
56 RightIndex = new []{2},
57 UsePictures = true,
58 QuizType = 0,
59 TimeToComplete = 80f
60 };
61 var qdmulti = new QuizData
62 {
63 Question = "Jelölje be az összes jó választ!",
64 AnswerArray = new []{"Ez egy jó válasz", "Ez a válasz lehet, hogy rossz", "Ez egy nagyon rossz válasz", "Ide kattintva, ide lesz kattintva"},
65 RightIndex = new []{0,3},
66 UsePictures = false,
67 QuizType = 1,
68 TimeToComplete = 80f
69 };
70 //UpdateQuiz(qdpics);
71 //UpdateQuiz(JsonUtility.FromJson<QuizData>(File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "quizes", "quiz2.qiz"))));
72 //Debug.Log(JsonUtility.ToJson(qdpics));
73 //File.WriteAllText(Path.Combine(Application.streamingAssetsPath, "quizes", "quiz1.qiz"), JsonUtility.ToJson(qd));
74 //File.WriteAllText(Path.Combine(Application.streamingAssetsPath, "quizes", "quiz2.qiz"), JsonUtility.ToJson(qdpics));
75 //File.WriteAllText(Path.Combine(Application.streamingAssetsPath, "quizes", "quiz3.qiz"), JsonUtility.ToJson(qdmulti));
76#endif
77 }
78
79 private void Update()
80 {
81 if (quizTimeLeft > 0)
82 {
83 quizTimeLeft -= Time.deltaTime;
84 if (quizTimeLeft <= 0)
85 {
86 Debug.Log(":( TIMEOUT");
88 gameObject.SetActive(false);
89 }
90 }
91 }
92
93 public void OnTypeChanged(int value)
94 {
95 if (!tg)
96 return;
97 if (value == 0)
98 {
99 tg.enabled = true;
100 }
101 else
102 {
103 tg.enabled = false;
104 }
105 }
106
107 public void AnswerQuiz(int index) //TODO sound/flashy lights
108 {
109 if (quizType == 0)
110 {
111 if (goodAnswer[0] == index)
112 {
113 Debug.Log("juhhú");
115 gameObject.SetActive(false);
116 }
117 else
118 {
119 Debug.Log(":(");
121 gameObject.SetActive(false);
122 }
123 }
124 else
125 {
126 currentAnswers[index] = !currentAnswers[index];
127 switch (index)
128 {
129 case 0:
130 answer1.transform.parent.GetComponent<Image>().color = currentAnswers[index]
131 ? new Color(0.0f, 205f / 255f, 1f, 150f / 255f)
132 : new Color(1, 1, 1, 150f / 255f);
133 break;
134 case 1:
135 answer2.transform.parent.GetComponent<Image>().color = currentAnswers[index]
136 ? new Color(0.0f, 205f / 255f, 1f, 150f / 255f)
137 : new Color(1, 1, 1, 150f / 255f);
138 break;
139 case 2:
140 answer3.transform.parent.GetComponent<Image>().color = currentAnswers[index]
141 ? new Color(0.0f, 205f / 255f, 1f, 150f / 255f)
142 : new Color(1, 1, 1, 150f / 255f);
143 break;
144 case 3:
145 answer4.transform.parent.GetComponent<Image>().color = currentAnswers[index]
146 ? new Color(0.0f, 205f / 255f, 1f, 150f / 255f)
147 : new Color(1, 1, 1, 150f / 255f);
148 break;
149 }
150 //Debug.Log($"{index}: {currentAnswers[index]}");
151 }
152 }
153
154 [Button]
155 public void CheckAnswerMulti()
156 {
157 for (int i = 0; i < 4; i++)
158 {
159 if (currentAnswers[i])
160 {
161 if (!goodAnswer.Contains(i))
162 {
163 Debug.Log(":(");
165 gameObject.SetActive(false);
166 return;
167 }
168 }
169 else
170 {
171 if (goodAnswer.Contains(i))
172 {
173 Debug.Log(":(");
175 gameObject.SetActive(false);
176 return;
177 }
178 }
179 }
180 Debug.Log("juhhú");
182 gameObject.SetActive(false);
183 }
184
185 public void UpdateQuiz(QuizData value)
186 {
187 answer3.gameObject.transform.parent.gameObject.SetActive(false);
188 answer4.gameObject.transform.parent.gameObject.SetActive(false);
189
190 if (value.AnswerArray.Length >= 3)
191 answer3.gameObject.transform.parent.gameObject.SetActive(true);
192 if (value.AnswerArray.Length >= 4)
193 answer4.gameObject.transform.parent.gameObject.SetActive(true);
194
195 question.text = value.Question;
196 if (value.UsePictures)
197 {
198 answer1.gameObject.SetActive(false);
199 answer2.gameObject.SetActive(false);
200 answer3.gameObject.SetActive(false);
201 answer4.gameObject.SetActive(false);
202 image1.gameObject.SetActive(true);
203 image2.gameObject.SetActive(true);
204 image3.gameObject.SetActive(true);
205 image4.gameObject.SetActive(true);
206
207 if (Uri.IsWellFormedUriString(value.AnswerArray[0], UriKind.Absolute))
208 StartCoroutine(DownloadPicture(value.AnswerArray[0], image1));
209 else if (FileDragAndDrop.instance.GetUploadedGOFromName(value.AnswerArray[0], out var go))
210 {
211 image1.texture = go.GetComponent<RawImage>().texture;
212 SizeToParent(image1, 0.05f);
213 }
214 else
215 {
216 image1.gameObject.SetActive(false);
217 answer1.gameObject.SetActive(true);
218 answer1.text = value.AnswerArray[0];
219 }
220
221 if (Uri.IsWellFormedUriString(value.AnswerArray[1], UriKind.Absolute))
222 StartCoroutine(DownloadPicture(value.AnswerArray[1], image2));
223 else if (FileDragAndDrop.instance.GetUploadedGOFromName(value.AnswerArray[1], out var go))
224 {
225 image2.texture = go.GetComponent<RawImage>().texture;
226 SizeToParent(image2, 0.05f);
227 }
228 else
229 {
230 image2.gameObject.SetActive(false);
231 answer2.gameObject.SetActive(true);
232 answer2.text = value.AnswerArray[1];
233 }
234
235
236 if (Uri.IsWellFormedUriString(value.AnswerArray[2], UriKind.Absolute))
237 StartCoroutine(DownloadPicture(value.AnswerArray[2], image3));
238 else if (FileDragAndDrop.instance.GetUploadedGOFromName(value.AnswerArray[2], out var go))
239 {
240 image3.texture = go.GetComponent<RawImage>().texture;
241 SizeToParent(image3, 0.05f);
242 }
243 else
244 {
245 image3.gameObject.SetActive(false);
246 answer3.gameObject.SetActive(true);
247 answer3.text = value.AnswerArray[2];
248 }
249
250
251 if (Uri.IsWellFormedUriString(value.AnswerArray[3], UriKind.Absolute))
252 StartCoroutine(DownloadPicture(value.AnswerArray[3], image4));
253 else if (FileDragAndDrop.instance.GetUploadedGOFromName(value.AnswerArray[3], out var go))
254 {
255 image4.texture = go.GetComponent<RawImage>().texture;
256 SizeToParent(image4, 0.05f);
257 }
258 else
259 {
260 image4.gameObject.SetActive(false);
261 answer4.gameObject.SetActive(true);
262 answer4.text = value.AnswerArray[3];
263 }
264
265
266 }
267 else
268 {
269 answer1.gameObject.SetActive(true);
270 answer2.gameObject.SetActive(true);
271 answer3.gameObject.SetActive(true);
272 answer4.gameObject.SetActive(true);
273 answer1.text = value.AnswerArray[0];
274 answer2.text = value.AnswerArray[1];
275 answer3.text = value.AnswerArray[2];
276 answer4.text = value.AnswerArray[3];
277 image1.gameObject.SetActive(false);
278 image2.gameObject.SetActive(false);
279 image3.gameObject.SetActive(false);
280 image4.gameObject.SetActive(false);
281
282 }
283
284 quizType = value.QuizType;
285 goodAnswer = value.RightIndex;
286 checkMulti.SetActive(quizType != 0);
287 quizTimeLeft = value.TimeToComplete;
288 }
289
290 IEnumerator DownloadPicture(string url, RawImage target)
291 {
292 UnityWebRequest request = UnityWebRequestTexture.GetTexture(url);
293 yield return request.SendWebRequest();
294 if(request.isNetworkError || request.isHttpError)
295 Debug.Log(request.error);
296 else
297 {
298 target.texture = ((DownloadHandlerTexture) request.downloadHandler).texture;
299 SizeToParent(target, 0.05f);
300 }
301 }
302
303 public void SetTimeout(string value)
304 {
305 try
306 {
307 var ci = CultureInfo.CurrentCulture;
308 var nfi = ci.NumberFormat;
309 timeoutEditor.text = timeoutEditor.text.Replace(',', nfi.CurrencyDecimalSeparator[0]);
310 timeoutEditor.text = timeoutEditor.text.Replace('.', nfi.CurrencyDecimalSeparator[0]);
311 var single = Convert.ToSingle(timeoutEditor.text, ci);
312 timeoutEditor.text = single.ToString("F0");
313
314 }
315 catch (Exception)
316 {
317 Debug.LogWarning("not parsable number(?) in timeoutEditor inputfields!");
318 timeoutEditor.text = "0";
319 }
320 }
321
322 public static Vector2 SizeToParent(RawImage image, float padding = 0) {
323 float w = 0, h = 0;
324 var parent = image.transform.parent.GetComponent<RectTransform>();
325 var imageTransform = image.GetComponent<RectTransform>();
326
327 // check if there is something to do
328 if (image.texture != null) {
329 if (!parent) return imageTransform.sizeDelta; //if we don't have a parent, just return our current width;
330 padding = 1 - padding;
331 float ratio = image.texture.width / (float)image.texture.height;
332 var bounds = new Rect(0, 0, parent.rect.width, parent.rect.height);
333 if (Mathf.RoundToInt(imageTransform.eulerAngles.z) % 180 == 90) {
334 //Invert the bounds if the image is rotated
335 bounds.size = new Vector2(bounds.height, bounds.width);
336 }
337 //Size by height first
338 h = bounds.height * padding;
339 w = h * ratio;
340 if (w > bounds.width * padding) { //If it doesn't fit, fallback to width;
341 w = bounds.width * padding;
342 h = w / ratio;
343 }
344 }
345 imageTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, w);
346 imageTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, h);
347 return imageTransform.sizeDelta;
348 }
349}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
System.Drawing.Image Image
Definition: TestScript.cs:37
UnityEngine.Color Color
Definition: TestScript.cs:32
bool GetUploadedGOFromName(string value, out GameObject retval)
static FileDragAndDrop instance
void WrongAnswer()
Definition: QuizAction.cs:80
void GoodAnswer()
Definition: QuizAction.cs:73
RawImage image2
Definition: QuizManager.cs:27
void CheckAnswerMulti()
Definition: QuizManager.cs:155
Text answer1
Definition: QuizManager.cs:26
void UpdateQuiz(QuizData value)
Definition: QuizManager.cs:185
RawImage image4
Definition: QuizManager.cs:27
Text answer4
Definition: QuizManager.cs:26
Text answer3
Definition: QuizManager.cs:26
void SetTimeout(string value)
Definition: QuizManager.cs:303
RawImage image1
Definition: QuizManager.cs:27
void AnswerQuiz(int index)
Definition: QuizManager.cs:107
Text question
Definition: QuizManager.cs:26
GameObject checkMulti
Definition: QuizManager.cs:29
RawImage image3
Definition: QuizManager.cs:27
static Vector2 SizeToParent(RawImage image, float padding=0)
Definition: QuizManager.cs:322
Text answer2
Definition: QuizManager.cs:26
void OnTypeChanged(int value)
Definition: QuizManager.cs:93
ToggleGroup tg
Definition: QuizManager.cs:28
QuizAction currentAction
Definition: QuizManager.cs:30
InputField timeoutEditor
Definition: QuizManager.cs:37