NewDropZoneScript.cs 10 KB

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