ScrollViewScript.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // Start is called before the first frame update
  13. void Start() {
  14. SetGiventQuestion();
  15. }
  16. public void SetGiventQuestion() {
  17. // if new game, create one question card to start with
  18. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  19. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  20. questionCard.PrepareQuestion("QuestionText", "AnswerText");
  21. questionCard.GetQuestion(true);
  22. questionCard.SetQuestionSafe();
  23. questionCard.transform.SetSiblingIndex(0);
  24. questionCard.transform.SetParent(contentPanel);
  25. }
  26. public void OnDrop(PointerEventData eventData) {
  27. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  28. if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
  29. return;
  30. }
  31. // Popup modal asking are you sure here or at onDragEnd?
  32. nq = d.GetComponent<NewQuestion>();
  33. newQuestionSiblingIndex = d.placeholder.transform.GetSiblingIndex();
  34. ModalPanel modalPanel = ModalPanel.Instance();
  35. ModalPanelDetails modalDetails = new ModalPanelDetails { question = "Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?", iconImage = null };
  36. modalDetails.button1Details = new EventButtonDetails { buttonTitle = "Ja", action = YesFunction };
  37. modalDetails.button2Details = new EventButtonDetails { buttonTitle = "Nej", action = NoFunction };
  38. modalPanel.NewChoice(modalDetails);
  39. }
  40. // 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.
  41. void YesFunction() {
  42. int correctAnswer = Int32.Parse(nq.answerString);
  43. Transform questionBefore = null;
  44. Transform questionAfter = null;
  45. try {
  46. questionBefore = contentPanel.GetChild(newQuestionSiblingIndex -1 );
  47. } catch (UnityException e) { }
  48. try {
  49. questionAfter = contentPanel.GetChild(newQuestionSiblingIndex );
  50. } catch (UnityException e) { }
  51. QuestionCard test = contentPanel.GetChild(0).GetComponent<QuestionCard>();
  52. int answerBefore = -1;
  53. int answerAfter = 999999;
  54. if (questionBefore != null) {
  55. answerBefore = Int16.Parse(questionBefore.GetComponent<QuestionCard>().GetAnswerText().text);
  56. }
  57. if (questionAfter != null) {
  58. answerAfter = Int16.Parse(questionAfter.GetComponent<QuestionCard>().GetAnswerText().text);
  59. }
  60. 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)
  61. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  62. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  63. questionCard.SetAnswerText(nq.answerString);
  64. questionCard.SetQuestionText(nq.questionString);
  65. questionCard.questionString = nq.questionString;
  66. questionCard.answerString = nq.answerString;
  67. questionCard.categoryString = nq.categoryString;
  68. questionCard.idString = nq.idString;
  69. questionCard.transform.SetParent(contentPanel);
  70. questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex);
  71. } else { // Visa modal för fel svar och meddela att man svarat fel och förlorat x olåsta frågor.
  72. }
  73. }
  74. void NoFunction() {
  75. }
  76. }