using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; public class NewDropZoneScript : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler { public bool RightmostSpacer; public bool LeftmostSpacer; public GameObject AnswerLineQuestionCardPrefab; public float preferedWidth = 30; private AnswerLineQuestionCard leftSibling; private AnswerLineQuestionCard rightSibling; private NewQuestionData questionData; private GameObject draggedQuestion; private bool answeredCorrectly = false; public void OnDrop(PointerEventData eventData) { if (eventData != null && draggedQuestion != null) { questionData = draggedQuestion.GetComponentInParent().NewQuestion.GetComponentInParent() .QuestionData; GenericDialog confirmDialog = GenericDialog.Instance(); string message = string.Format(LocalizationManager.Instance.GetText("DROPPED_QUESTION_DIALOG_MESSAGE_TEXT"), questionData.Question, draggedQuestion.GetComponent().AnswerText.text); confirmDialog.SetMessage(message); confirmDialog.SetOnAccept(LocalizationManager.Instance.GetText("YES"), () => { YesFunction(); confirmDialog.Hide(); CheckAnswerFunction(); // Generera bara ny fråga här?? Vid rätt?? }); confirmDialog.SetOnDecline(LocalizationManager.Instance.GetText("NO"), () => { NewQuestionDragController.setAnswerText("???? - ????"); confirmDialog.Hide(); }); confirmDialog.Show(); } } private void CheckAnswerFunction() { if (answeredCorrectly) { GameObject go = Instantiate(AnswerLineQuestionCardPrefab, Vector3.zero, Quaternion.identity); AnswerLineQuestionCard newAnsweredQuestion = go.GetComponentInParent(); newAnsweredQuestion.answerText.text = questionData.Answer; newAnsweredQuestion.questionText.text = questionData.Question; newAnsweredQuestion.SetId(questionData.Id); GameObject AnswerLine = GameObject.FindGameObjectWithTag("AnswerLine"); int newAnswerPos = transform.GetSiblingIndex(); newAnsweredQuestion.transform.SetParent(AnswerLine.transform); newAnsweredQuestion.transform.SetSiblingIndex(newAnswerPos); newAnsweredQuestion.transform.localScale = new Vector3(1,1,1); if (LeftmostSpacer) { CreateNewSpacer(AnswerLine, newAnswerPos + 2); CreateLeftSpacer(AnswerLine); } else if (RightmostSpacer) { CreateNewSpacer(AnswerLine, newAnswerPos); CreateRightSpacer(AnswerLine); } else { CreateNewSpacer(AnswerLine, newAnswerPos); CreateNewSpacer(AnswerLine, newAnswerPos + 2); } Destroy(gameObject); GameObject.Find("GameManager").GetComponent().StopTimer(); // TODO Check win condition, if met show Congratulations and send loss message to others draggedQuestion.GetComponentInParent().GenerateNewQuestion(); } else { // Wrong answer // TODO Visa fel svar dialog GameObject.Find("GameManager").GetComponent().RemoveUnlockedQuestions(); GameObject.Find("GameManager").GetComponent().NextRound(); } } private void CreateLeftSpacer(GameObject AnswerLine) { GameObject newSpacerLeft = Instantiate(gameObject); newSpacerLeft.GetComponent().preferredWidth = preferedWidth; newSpacerLeft.name = "LeftDropZone"; newSpacerLeft.GetComponent().LeftmostSpacer = true; newSpacerLeft.transform.SetParent(AnswerLine.transform); newSpacerLeft.transform.SetAsFirstSibling(); newSpacerLeft.transform.localScale = new Vector3(1,1,1); } private void CreateRightSpacer(GameObject AnswerLine) { GameObject newSpacerRight = Instantiate(gameObject); newSpacerRight.GetComponent().preferredWidth = preferedWidth; newSpacerRight.name = "RightDropZone"; newSpacerRight.GetComponent().RightmostSpacer = true; newSpacerRight.transform.SetParent(AnswerLine.transform); newSpacerRight.transform.SetAsLastSibling(); newSpacerRight.transform.localScale = new Vector3(1,1,1); } private void CreateNewSpacer(GameObject AnswerLine, int newAnswerPos) { GameObject newSpacer = Instantiate(gameObject); newSpacer.GetComponent().LeftmostSpacer = false; newSpacer.GetComponent().RightmostSpacer = false; newSpacer.GetComponent().preferredWidth = preferedWidth; newSpacer.name = "DropZone"; newSpacer.transform.SetParent(AnswerLine.transform); newSpacer.transform.SetSiblingIndex(newAnswerPos); newSpacer.transform.localScale = new Vector3(1,1,1); } public void OnPointerEnter(PointerEventData eventData) { draggedQuestion = NewQuestionDragController.itemBeeingDragged; if (draggedQuestion != null) { // Expandera aktuell drop yta LayoutElement le = transform.GetComponent(); le.preferredWidth = le.preferredWidth + preferedWidth; if (LeftmostSpacer) { rightSibling = transform.parent.GetChild(1).GetComponent(); NewQuestionDragController.setAnswerText(LocalizationManager.Instance.GetText("BEFORE") + rightSibling.answerText.text); leftSibling = null; } else if (RightmostSpacer) { leftSibling = transform.parent.GetChild(transform.GetSiblingIndex() - 1).GetComponent(); NewQuestionDragController.setAnswerText(LocalizationManager.Instance.GetText("AFTER") + leftSibling.answerText.text); rightSibling = null; } else { leftSibling = transform.parent.GetChild(transform.GetSiblingIndex() - 1).GetComponent(); rightSibling = transform.parent.GetChild(transform.GetSiblingIndex() + 1).GetComponent(); NewQuestionDragController.setAnswerText(leftSibling.answerText.text + " - " + rightSibling.answerText.text); } } } public void OnPointerExit(PointerEventData eventData) { if (draggedQuestion != null) { NewQuestionDragController.setAnswerText("???? - ????"); LayoutElement le = transform.GetComponent(); le.preferredWidth = le.preferredWidth - preferedWidth; } } void YesFunction() { String correctYear = questionData.Answer; String answeredYear = draggedQuestion.GetComponent().AnswerText.text; String yearBefore = leftSibling != null?leftSibling.answerText.text:"-99999"; String yearAfter = rightSibling != null?rightSibling.answerText.text:"99999"; Int32.TryParse(yearBefore, out int answerBeforeInt); Int32.TryParse(yearAfter, out int answerAfterInt); Int32.TryParse(correctYear, out int correctYearInt); if (answerBeforeInt <= correctYearInt && answerAfterInt >= correctYearInt) { answeredCorrectly = true; } else { answeredCorrectly = false; } } }