ScrollViewScript.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. public class ScrollViewScript : MonoBehaviour, IDropHandler {
  7. public GameObject prefab;
  8. public Transform contentPanel;
  9. private bool placeQuestion;
  10. NewQuestion nq;
  11. private int newQuestionSiblingIndex;
  12. ModalPanel modalPanel;
  13. private bool answeredCorrectly;
  14. // Start is called before the first frame update
  15. void Start() {
  16. SetGiventQuestion();
  17. }
  18. public void SetGiventQuestion() {
  19. // if new game, create one question card to start with
  20. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  21. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  22. questionCard.PrepareQuestion("QuestionText", "AnswerText");
  23. questionCard.GetQuestion(true);
  24. questionCard.SetQuestionSafe();
  25. questionCard.transform.SetSiblingIndex(0);
  26. questionCard.transform.SetParent(contentPanel);
  27. }
  28. public int GetUnlockedQuestionCount() {
  29. int unlockedQuestionCount = 0;
  30. for (int i = 0; i < contentPanel.childCount; i++) {
  31. QuestionCard qc = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  32. if (!qc.IsQuestionSafe()) {
  33. unlockedQuestionCount++;
  34. }
  35. }
  36. return unlockedQuestionCount;
  37. }
  38. public void OnDrop(PointerEventData eventData) {
  39. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  40. if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
  41. return;
  42. }
  43. // Popup modal asking are you sure here or at onDragEnd?
  44. nq = d.GetComponent<NewQuestion>();
  45. newQuestionSiblingIndex = d.placeholder.transform.GetSiblingIndex();
  46. GenericDialog dialog = GenericDialog.Instance();
  47. dialog.SetTitle("Är du säker?");
  48. dialog.SetMessage("Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?");
  49. dialog.SetOnAccept("Ja", () => {
  50. YesFunction();
  51. dialog.Hide();
  52. if (answeredCorrectly) {
  53. dialog.SetTitle("Korrekt svarat " + nq.answerString + "!");
  54. dialog.SetMessage("Helt korrekt, vill du ha en ny fråga, du riskerar då " + GetUnlockedQuestionCount() + " om du svarar fel");
  55. dialog.SetOnAccept("Ja", () => { dialog.Hide(); }); // generera ny fråga
  56. dialog.SetOnDecline("Nej", () => { dialog.Hide(); }); // lås frågor som ej är låsta.
  57. } else {
  58. dialog.SetTitle("Tyvärr fel svar, korrekt svar var " + nq.answerString);
  59. dialog.SetMessage("Du förlorade " + GetUnlockedQuestionCount() + ", bättre lycka nästa gång");
  60. dialog.SetOnAccept("Ok", () => dialog.Hide());
  61. dialog.SetOnDecline("", () => dialog.Hide());
  62. }
  63. dialog.Show();
  64. });
  65. dialog.SetOnDecline("Nej", () => dialog.Hide());
  66. dialog.Show();
  67. //modalPanel = ModalPanel.Instance();
  68. //ModalPanelDetails modalDetails = new ModalPanelDetails { question = "Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?", iconImage = null };
  69. //modalDetails.button1Details = new EventButtonDetails { buttonTitle = "Ja", action = YesFunction };
  70. //modalDetails.button2Details = new EventButtonDetails { buttonTitle = "Nej", action = NoFunction };
  71. //modalPanel.NewChoice(modalDetails);
  72. }
  73. // 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.
  74. void YesFunction() {
  75. int correctAnswer = Int32.Parse(nq.answerString);
  76. Transform questionBefore = null;
  77. Transform questionAfter = null;
  78. try {
  79. questionBefore = contentPanel.GetChild(newQuestionSiblingIndex -1 );
  80. } catch (UnityException e) { }
  81. try {
  82. questionAfter = contentPanel.GetChild(newQuestionSiblingIndex );
  83. } catch (UnityException e) { }
  84. QuestionCard test = contentPanel.GetChild(0).GetComponent<QuestionCard>();
  85. int answerBefore = -1;
  86. int answerAfter = 999999;
  87. if (questionBefore != null) {
  88. answerBefore = Int16.Parse(questionBefore.GetComponent<QuestionCard>().GetAnswerText().text);
  89. }
  90. if (questionAfter != null) {
  91. answerAfter = Int16.Parse(questionAfter.GetComponent<QuestionCard>().GetAnswerText().text);
  92. }
  93. if (answerBefore <= correctAnswer && answerAfter >= correctAnswer) {
  94. // 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)
  95. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  96. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  97. questionCard.SetAnswerText(nq.answerString);
  98. questionCard.SetQuestionText(nq.questionString);
  99. questionCard.questionString = nq.questionString;
  100. questionCard.answerString = nq.answerString;
  101. questionCard.categoryString = nq.categoryString;
  102. questionCard.idString = nq.idString;
  103. questionCard.transform.SetParent(contentPanel);
  104. questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex);
  105. answeredCorrectly = true;
  106. } else {
  107. answeredCorrectly = false;
  108. }
  109. }
  110. void NoFunction() {
  111. }
  112. }