NewDropZoneScript.cs 10 KB

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