| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class ScrollViewScript : MonoBehaviour, IDropHandler {
- public GameObject prefab;
- public Transform contentPanel;
- private bool placeQuestion;
- NewQuestion nq;
- private int newQuestionSiblingIndex;
- ModalPanel modalPanel;
- private bool answeredCorrectly;
- // Start is called before the first frame update
- void Start() {
- SetGiventQuestion();
- }
- public void SetGiventQuestion() {
- // if new game, create one question card to start with
- GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
- QuestionCard questionCard = question.GetComponent<QuestionCard>();
- questionCard.PrepareQuestion("QuestionText", "AnswerText");
- questionCard.GetQuestion(true);
- questionCard.SetQuestionSafe();
- questionCard.transform.SetSiblingIndex(0);
- questionCard.transform.SetParent(contentPanel);
- }
- public int GetUnlockedQuestionCount() {
- int unlockedQuestionCount = 0;
- for (int i = 0; i < contentPanel.childCount; i++) {
- QuestionCard qc = contentPanel.GetChild(i).GetComponent<QuestionCard>();
- if (!qc.IsQuestionSafe()) {
- unlockedQuestionCount++;
- }
- }
- return unlockedQuestionCount;
- }
- public void OnDrop(PointerEventData eventData) {
- Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
- if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
- return;
- }
- // Popup modal asking are you sure here or at onDragEnd?
- nq = d.GetComponent<NewQuestion>();
- newQuestionSiblingIndex = d.placeholder.transform.GetSiblingIndex();
- GenericDialog dialog = GenericDialog.Instance();
- dialog.SetTitle("Är du säker?");
- dialog.SetMessage("Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?");
- dialog.SetOnAccept("Ja", () => {
- YesFunction();
- dialog.Hide();
- if (answeredCorrectly) {
- dialog.SetTitle("Korrekt svarat " + nq.answerString + "!");
- dialog.SetMessage("Helt korrekt, vill du ha en ny fråga, du riskerar då " + GetUnlockedQuestionCount() + " om du svarar fel");
- dialog.SetOnAccept("Ja", () => { dialog.Hide(); }); // generera ny fråga
- dialog.SetOnDecline("Nej", () => { dialog.Hide(); }); // lås frågor som ej är låsta.
- } else {
- dialog.SetTitle("Tyvärr fel svar, korrekt svar var " + nq.answerString);
- dialog.SetMessage("Du förlorade " + GetUnlockedQuestionCount() + ", bättre lycka nästa gång");
- dialog.SetOnAccept("Ok", () => dialog.Hide());
- dialog.SetOnDecline("", () => dialog.Hide());
- }
- dialog.Show();
- });
- dialog.SetOnDecline("Nej", () => dialog.Hide());
- dialog.Show();
- //modalPanel = ModalPanel.Instance();
- //ModalPanelDetails modalDetails = new ModalPanelDetails { question = "Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?", iconImage = null };
- //modalDetails.button1Details = new EventButtonDetails { buttonTitle = "Ja", action = YesFunction };
- //modalDetails.button2Details = new EventButtonDetails { buttonTitle = "Nej", action = NoFunction };
- //modalPanel.NewChoice(modalDetails);
- }
- // kontrollera om man svarat korrekt, om det är korrekt, visa ny modal med vill du ha ny fråga? om fel, meddela det och avsluta omgång.
- void YesFunction() {
-
- int correctAnswer = Int32.Parse(nq.answerString);
- Transform questionBefore = null;
- Transform questionAfter = null;
- try {
- questionBefore = contentPanel.GetChild(newQuestionSiblingIndex -1 );
- } catch (UnityException e) { }
- try {
- questionAfter = contentPanel.GetChild(newQuestionSiblingIndex );
- } catch (UnityException e) { }
- QuestionCard test = contentPanel.GetChild(0).GetComponent<QuestionCard>();
- int answerBefore = -1;
- int answerAfter = 999999;
- if (questionBefore != null) {
- answerBefore = Int16.Parse(questionBefore.GetComponent<QuestionCard>().GetAnswerText().text);
- }
- if (questionAfter != null) {
- answerAfter = Int16.Parse(questionAfter.GetComponent<QuestionCard>().GetAnswerText().text);
- }
- if (answerBefore <= correctAnswer && answerAfter >= correctAnswer) {
- // korrect svar, spara frågan i tidslinjen och prompta ny modal för "Vill du ha en fråga till?" med meddelande om vad som står på spel (olåsta frågor)
- GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
- QuestionCard questionCard = question.GetComponent<QuestionCard>();
- questionCard.SetAnswerText(nq.answerString);
- questionCard.SetQuestionText(nq.questionString);
- questionCard.questionString = nq.questionString;
- questionCard.answerString = nq.answerString;
- questionCard.categoryString = nq.categoryString;
- questionCard.idString = nq.idString;
- questionCard.transform.SetParent(contentPanel);
- questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex);
- answeredCorrectly = true;
- } else {
- answeredCorrectly = false;
- }
- }
- void NoFunction() {
- }
- }
|