NewDropZoneScript.cs 10 KB

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