NewDropZoneScript.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.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.SetId(questionData.Id);
  70. GameObject AnswerLine = GameObject.FindGameObjectWithTag("AnswerLine");
  71. int newAnswerPos = transform.GetSiblingIndex();
  72. newAnsweredQuestion.SetQuestionUnSafe();
  73. newAnsweredQuestion.transform.SetParent(AnswerLine.transform);
  74. newAnsweredQuestion.transform.SetSiblingIndex(newAnswerPos);
  75. newAnsweredQuestion.transform.localScale = new Vector3(1,1,1);
  76. newAnsweredQuestion.GetComponent<LayoutElement>().preferredHeight = newQuestionRect.rect.height;
  77. newAnsweredQuestion.GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  78. if (LeftmostSpacer)
  79. {
  80. CreateNewSpacer(AnswerLine, newAnswerPos + 2);
  81. CreateLeftSpacer(AnswerLine, newQuestionRect.rect.width);
  82. }
  83. else if (RightmostSpacer)
  84. {
  85. CreateNewSpacer(AnswerLine, newAnswerPos);
  86. CreateRightSpacer(AnswerLine, newQuestionRect.rect.width);
  87. }
  88. else
  89. {
  90. CreateNewSpacer(AnswerLine, newAnswerPos);
  91. CreateNewSpacer(AnswerLine, newAnswerPos + 2);
  92. }
  93. Destroy(gameObject);
  94. GameObject.Find("GameManager").GetComponent<GameManagerScript>().StopTimer();
  95. // TODO Check win condition, if met show Congratulations and send loss message to others
  96. GameObject.Find("GameManager").GetComponent<GameManagerScript>().CheckWin(AnswerLine.GetComponentsInChildren<QuestionCard>().ToList());
  97. draggedQuestion.GetComponentInParent<NewQuestionCardController>().GenerateNewQuestion();
  98. } else { // Wrong answer
  99. GameObject.Find("GameManager").GetComponent<GameManagerScript>().StopTimer();
  100. questionData = draggedQuestion.GetComponentInParent<NewQuestionDragController>()
  101. .NewQuestion.GetComponentInParent<NewQuestionsPanel>()
  102. .QuestionData;
  103. GenericDialog dialog = GenericDialog.Instance();
  104. string title = string.Format(LocalizationManager.Instance.GetText("DROPPED_QUESTION_WRONG_ANSWER_TITLE"), questionData.Answer);
  105. dialog.SetTitle(title);
  106. string message = LocalizationManager.Instance.GetText("DROPPED_QUESTION_WRONG_ANSWER_MESSAGE");
  107. dialog.SetMessage(String.Format(message, GameObject.Find("GameManager").GetComponent<GameManagerScript>().GetUnlockedQuestionsCount()));
  108. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  109. GameObject.Find("GameManager").GetComponent<GameManagerScript>().RemoveUnlockedQuestions();
  110. GameObject.Find("GameManager").GetComponent<GameManagerScript>().NextRound();
  111. dialog.Hide();
  112. });
  113. dialog.SetOnDecline("", () => dialog.Hide());
  114. dialog.Show();
  115. }
  116. }
  117. private void CreateLeftSpacer(GameObject AnswerLine, float width)
  118. {
  119. GameObject newSpacerLeft = Instantiate(gameObject);
  120. newSpacerLeft.GetComponent<LayoutElement>().preferredWidth = width;
  121. newSpacerLeft.name = "LeftDropZone";
  122. newSpacerLeft.GetComponent<NewDropZoneScript>().LeftmostSpacer = true;
  123. newSpacerLeft.transform.SetParent(AnswerLine.transform);
  124. newSpacerLeft.transform.SetAsFirstSibling();
  125. newSpacerLeft.transform.localScale = new Vector3(1,1,1);
  126. }
  127. private void CreateRightSpacer(GameObject AnswerLine, float width)
  128. {
  129. GameObject newSpacerRight = Instantiate(gameObject);
  130. newSpacerRight.GetComponent<LayoutElement>().preferredWidth = width;
  131. newSpacerRight.name = "RightDropZone";
  132. newSpacerRight.GetComponent<NewDropZoneScript>().RightmostSpacer = true;
  133. newSpacerRight.transform.SetParent(AnswerLine.transform);
  134. newSpacerRight.transform.SetAsLastSibling();
  135. newSpacerRight.transform.localScale = new Vector3(1,1,1);
  136. }
  137. private void CreateNewSpacer(GameObject AnswerLine, int newAnswerPos)
  138. {
  139. GameObject newSpacer = Instantiate(gameObject);
  140. newSpacer.GetComponent<NewDropZoneScript>().LeftmostSpacer = false;
  141. newSpacer.GetComponent<NewDropZoneScript>().RightmostSpacer = false;
  142. newSpacer.GetComponent<LayoutElement>().preferredWidth = 30f;
  143. newSpacer.name = "DropZone";
  144. newSpacer.transform.SetParent(AnswerLine.transform);
  145. newSpacer.transform.SetSiblingIndex(newAnswerPos);
  146. newSpacer.transform.localScale = new Vector3(1,1,1);
  147. }
  148. public void OnPointerEnter(PointerEventData eventData)
  149. {
  150. draggedQuestion = NewQuestionDragController.itemBeeingDragged;
  151. if (draggedQuestion != null) {
  152. // Expandera aktuell drop yta
  153. LayoutElement le = transform.GetComponent<LayoutElement>();
  154. le.preferredWidth = le.preferredWidth + preferedWidth;
  155. if (LeftmostSpacer) {
  156. rightSibling = transform.parent.GetChild(1).GetComponent<AnswerLineQuestionCard>();
  157. NewQuestionDragController.setAnswerText(LocalizationManager.Instance.GetText("BEFORE") + rightSibling.answerText.text);
  158. leftSibling = null;
  159. } else if (RightmostSpacer) {
  160. leftSibling = transform.parent.GetChild(transform.GetSiblingIndex() - 1).GetComponent<AnswerLineQuestionCard>();
  161. NewQuestionDragController.setAnswerText(LocalizationManager.Instance.GetText("AFTER") + leftSibling.answerText.text);
  162. rightSibling = null;
  163. } else {
  164. leftSibling = transform.parent.GetChild(transform.GetSiblingIndex() - 1).GetComponent<AnswerLineQuestionCard>();
  165. rightSibling = transform.parent.GetChild(transform.GetSiblingIndex() + 1).GetComponent<AnswerLineQuestionCard>();
  166. NewQuestionDragController.setAnswerText(leftSibling.answerText.text + " - " + rightSibling.answerText.text);
  167. }
  168. }
  169. }
  170. public void OnPointerExit(PointerEventData eventData)
  171. {
  172. if (draggedQuestion != null) {
  173. NewQuestionDragController.setAnswerText("???? - ????");
  174. LayoutElement le = transform.GetComponent<LayoutElement>();
  175. le.preferredWidth = le.preferredWidth - preferedWidth;
  176. }
  177. }
  178. void YesFunction() {
  179. String correctYear = questionData.Answer;
  180. String answeredYear = draggedQuestion.GetComponent<NewQuestionDragController>().AnswerText.text;
  181. String yearBefore = leftSibling != null?leftSibling.answerText.text:"-99999";
  182. String yearAfter = rightSibling != null?rightSibling.answerText.text:"99999";
  183. Int32.TryParse(yearBefore, out int answerBeforeInt);
  184. Int32.TryParse(yearAfter, out int answerAfterInt);
  185. Int32.TryParse(correctYear, out int correctYearInt);
  186. if (answerBeforeInt <= correctYearInt && answerAfterInt >= correctYearInt) {
  187. answeredCorrectly = true;
  188. } else {
  189. answeredCorrectly = false;
  190. }
  191. }
  192. }