| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class ScrollViewScript : MonoBehaviour, IDropHandler {
- public GameObject prefab;
- public Transform contentPanel;
- private bool placeQuestion;
- NewQuestion nq;
- private int newQuestionSiblingIndex;
- // Start is called before the first frame update
- void Start() {
- SetGiventQuestion();
- }
- public void SetGiventQuestion() {
- // if new game, create one question card to start with
- GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
- QuestionCard questionCard = question.GetComponent<QuestionCard>();
- questionCard.PrepareQuestion("QuestionText", "AnswerText");
- questionCard.GetQuestion(true);
- questionCard.SetQuestionSafe();
- questionCard.transform.SetSiblingIndex(0);
- questionCard.transform.SetParent(contentPanel);
- }
- public void OnDrop(PointerEventData eventData) {
- Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
- if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
- return;
- }
- // Popup modal asking are you sure here or at onDragEnd?
- nq = d.GetComponent<NewQuestion>();
- newQuestionSiblingIndex = d.placeholder.transform.GetSiblingIndex();
- ModalPanel modalPanel = ModalPanel.Instance();
- ModalPanelDetails modalDetails = new ModalPanelDetails { question = "Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?", iconImage = null };
- modalDetails.button1Details = new EventButtonDetails { buttonTitle = "Ja", action = YesFunction };
- modalDetails.button2Details = new EventButtonDetails { buttonTitle = "Nej", action = NoFunction };
- modalPanel.NewChoice(modalDetails);
- }
- // kontrollera om man svarat korrekt, om det är korrekt, visa ny modal med vill du ha ny fråga? om fel, meddela det och avsluta omgång.
- void YesFunction() {
- int correctAnswer = Int32.Parse(nq.answerString);
- Transform questionBefore = null;
- Transform questionAfter = null;
- try {
- questionBefore = contentPanel.GetChild(newQuestionSiblingIndex -1 );
- } catch (UnityException e) { }
- try {
- questionAfter = contentPanel.GetChild(newQuestionSiblingIndex );
- } catch (UnityException e) { }
- QuestionCard test = contentPanel.GetChild(0).GetComponent<QuestionCard>();
- int answerBefore = -1;
- int answerAfter = 999999;
- if (questionBefore != null) {
- answerBefore = Int16.Parse(questionBefore.GetComponent<QuestionCard>().GetAnswerText().text);
- }
- if (questionAfter != null) {
- answerAfter = Int16.Parse(questionAfter.GetComponent<QuestionCard>().GetAnswerText().text);
- }
- if (answerBefore <= correctAnswer && answerAfter >= correctAnswer) { // korrect svar, spara frågan i tidslinjen och prompta ny modal för "Vill du ha en fråga till?" med meddelande om vad som står på spel (olåsta frågor)
- GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
- QuestionCard questionCard = question.GetComponent<QuestionCard>();
- questionCard.SetAnswerText(nq.answerString);
- questionCard.SetQuestionText(nq.questionString);
- questionCard.questionString = nq.questionString;
- questionCard.answerString = nq.answerString;
- questionCard.categoryString = nq.categoryString;
- questionCard.idString = nq.idString;
- questionCard.transform.SetParent(contentPanel);
- questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex);
- } else { // Visa modal för fel svar och meddela att man svarat fel och förlorat x olåsta frågor.
- }
- }
- void NoFunction() {
- }
- }
|