NewDropZoneScript.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public class NewDropZoneScript : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
  9. {
  10. public bool RightmostSpacer;
  11. public bool LeftmostSpacer;
  12. public GameObject AnswerLineQuestionCardPrefab;
  13. public float preferedWidth = 30;
  14. private AnswerLineQuestionCard leftSibling;
  15. private AnswerLineQuestionCard rightSibling;
  16. private NewQuestionData questionData;
  17. private GameObject draggedQuestion;
  18. private bool answeredCorrectly = false;
  19. public void OnDrop(PointerEventData eventData)
  20. {
  21. if (eventData != null && draggedQuestion != null)
  22. {
  23. questionData = draggedQuestion.GetComponentInParent<NewQuestionDragController>().NewQuestion.GetComponentInParent<NewQuestionsPanel>()
  24. .QuestionData;
  25. GenericDialog confirmDialog = GenericDialog.Instance();
  26. string message = string.Format(LocalizationManager.Instance.GetText("DROPPED_QUESTION_DIALOG_MESSAGE_TEXT"),
  27. questionData.Question,
  28. draggedQuestion.GetComponent<NewQuestionDragController>().AnswerText.text);
  29. confirmDialog.SetMessage(message);
  30. confirmDialog.SetOnAccept(LocalizationManager.Instance.GetText("YES"), () => {
  31. YesFunction();
  32. confirmDialog.Hide();
  33. CheckAnswerFunction(); // Generera bara ny fråga här?? Vid rätt??
  34. });
  35. confirmDialog.SetOnDecline(LocalizationManager.Instance.GetText("NO"), () =>
  36. {
  37. NewQuestionDragController.setAnswerText("???? - ????");
  38. confirmDialog.Hide();
  39. });
  40. confirmDialog.Show();
  41. }
  42. }
  43. private void CheckAnswerFunction()
  44. {
  45. if (answeredCorrectly) {
  46. GameObject go = Instantiate(AnswerLineQuestionCardPrefab, Vector3.zero, Quaternion.identity);
  47. AnswerLineQuestionCard newAnsweredQuestion = go.GetComponentInParent<AnswerLineQuestionCard>();
  48. newAnsweredQuestion.answerText.text = questionData.Answer;
  49. newAnsweredQuestion.questionText.text = questionData.Question;
  50. newAnsweredQuestion.SetId(questionData.Id);
  51. GameObject AnswerLine = GameObject.FindGameObjectWithTag("AnswerLine");
  52. int newAnswerPos = transform.GetSiblingIndex();
  53. newAnsweredQuestion.transform.SetParent(AnswerLine.transform);
  54. newAnsweredQuestion.transform.SetSiblingIndex(newAnswerPos);
  55. newAnsweredQuestion.transform.localScale = new Vector3(1,1,1);
  56. if (LeftmostSpacer)
  57. {
  58. CreateNewSpacer(AnswerLine, newAnswerPos + 2);
  59. CreateLeftSpacer(AnswerLine);
  60. }
  61. else if (RightmostSpacer)
  62. {
  63. CreateNewSpacer(AnswerLine, newAnswerPos);
  64. CreateRightSpacer(AnswerLine);
  65. }
  66. else
  67. {
  68. CreateNewSpacer(AnswerLine, newAnswerPos);
  69. CreateNewSpacer(AnswerLine, newAnswerPos + 2);
  70. }
  71. Destroy(gameObject);
  72. GameObject.Find("GameManager").GetComponent<GameManagerScript>().StopTimer();
  73. draggedQuestion.GetComponentInParent<NewQuestionCardController>().GenerateNewQuestion();
  74. } else { // Wrong answer
  75. GameObject.Find("GameManager").GetComponent<GameManagerScript>().RemoveUnlockedQuestions();
  76. GameObject.Find("GameManager").GetComponent<GameManagerScript>().NextRound();
  77. }
  78. }
  79. private void CreateLeftSpacer(GameObject AnswerLine)
  80. {
  81. GameObject newSpacerLeft = Instantiate(gameObject);
  82. newSpacerLeft.GetComponent<LayoutElement>().preferredWidth = preferedWidth;
  83. newSpacerLeft.name = "LeftDropZone";
  84. newSpacerLeft.GetComponent<NewDropZoneScript>().LeftmostSpacer = true;
  85. newSpacerLeft.transform.SetParent(AnswerLine.transform);
  86. newSpacerLeft.transform.SetAsFirstSibling();
  87. newSpacerLeft.transform.localScale = new Vector3(1,1,1);
  88. }
  89. private void CreateRightSpacer(GameObject AnswerLine)
  90. {
  91. GameObject newSpacerRight = Instantiate(gameObject);
  92. newSpacerRight.GetComponent<LayoutElement>().preferredWidth = preferedWidth;
  93. newSpacerRight.name = "RightDropZone";
  94. newSpacerRight.GetComponent<NewDropZoneScript>().RightmostSpacer = true;
  95. newSpacerRight.transform.SetParent(AnswerLine.transform);
  96. newSpacerRight.transform.SetAsLastSibling();
  97. newSpacerRight.transform.localScale = new Vector3(1,1,1);
  98. }
  99. private void CreateNewSpacer(GameObject AnswerLine, int newAnswerPos)
  100. {
  101. GameObject newSpacer = Instantiate(gameObject);
  102. newSpacer.GetComponent<NewDropZoneScript>().LeftmostSpacer = false;
  103. newSpacer.GetComponent<NewDropZoneScript>().RightmostSpacer = false;
  104. newSpacer.GetComponent<LayoutElement>().preferredWidth = preferedWidth;
  105. newSpacer.name = "DropZone";
  106. newSpacer.transform.SetParent(AnswerLine.transform);
  107. newSpacer.transform.SetSiblingIndex(newAnswerPos);
  108. newSpacer.transform.localScale = new Vector3(1,1,1);
  109. }
  110. public void OnPointerEnter(PointerEventData eventData)
  111. {
  112. draggedQuestion = NewQuestionDragController.itemBeeingDragged;
  113. if (draggedQuestion != null) {
  114. // Expandera aktuell drop yta
  115. LayoutElement le = transform.GetComponent<LayoutElement>();
  116. le.preferredWidth = le.preferredWidth + preferedWidth;
  117. if (LeftmostSpacer) {
  118. rightSibling = transform.parent.GetChild(1).GetComponent<AnswerLineQuestionCard>();
  119. NewQuestionDragController.setAnswerText(LocalizationManager.Instance.GetText("BEFORE") + rightSibling.answerText.text);
  120. leftSibling = null;
  121. } else if (RightmostSpacer) {
  122. leftSibling = transform.parent.GetChild(transform.GetSiblingIndex() - 1).GetComponent<AnswerLineQuestionCard>();
  123. NewQuestionDragController.setAnswerText(LocalizationManager.Instance.GetText("AFTER") + leftSibling.answerText.text);
  124. rightSibling = null;
  125. } else {
  126. leftSibling = transform.parent.GetChild(transform.GetSiblingIndex() - 1).GetComponent<AnswerLineQuestionCard>();
  127. rightSibling = transform.parent.GetChild(transform.GetSiblingIndex() + 1).GetComponent<AnswerLineQuestionCard>();
  128. NewQuestionDragController.setAnswerText(leftSibling.answerText.text + " - " + rightSibling.answerText.text);
  129. }
  130. }
  131. }
  132. public void OnPointerExit(PointerEventData eventData)
  133. {
  134. if (draggedQuestion != null) {
  135. NewQuestionDragController.setAnswerText("???? - ????");
  136. LayoutElement le = transform.GetComponent<LayoutElement>();
  137. le.preferredWidth = le.preferredWidth - preferedWidth;
  138. }
  139. }
  140. void YesFunction() {
  141. String correctYear = questionData.Answer;
  142. String answeredYear = draggedQuestion.GetComponent<NewQuestionDragController>().AnswerText.text;
  143. String yearBefore = leftSibling != null?leftSibling.answerText.text:"-99999";
  144. String yearAfter = rightSibling != null?rightSibling.answerText.text:"99999";
  145. Int32.TryParse(yearBefore, out int answerBeforeInt);
  146. Int32.TryParse(yearAfter, out int answerAfterInt);
  147. Int32.TryParse(correctYear, out int correctYearInt);
  148. if (answerBeforeInt <= correctYearInt && answerAfterInt >= correctYearInt) {
  149. answeredCorrectly = true;
  150. } else {
  151. answeredCorrectly = false;
  152. }
  153. }
  154. }