You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
7.1 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;
using UnityEngine.UI;
using Random = UnityEngine.Random;
public class QuestionPanel : MonoBehaviour
{
[Header("回调事件")]
public UnityEvent sureEvent;
public UnityEvent errorEvent;
[Header("配置项")]
public bool isNeedChaos = true;//是否需要乱序选项
[Header("基本元素")]
public GameObject togglePre;
public GameObject inputPre;
public Text title;
public GameObject toggleArea;
public Button answerSure;
public GameObject errorTip;
public GameObject questionPanel;
protected List<QuestionCell> cells = new List<QuestionCell>();
private string _inputAnswer;
private int _singleAnswer;
private List<int> _multipleAnswer = new List<int>();
private QuestionData _questionData;
private void Awake()
{
_questionData = GetComponent<QuestionData>();
InitPanel();
}
/// <summary>
/// 初始化
/// </summary>
void InitPanel()
{
questionPanel.SetActive(true);
sureEvent.RemoveAllListeners();
errorEvent.RemoveAllListeners();
foreach (var cell in cells)
{
Destroy(cell.gameObject);
}
cells.Clear();
_inputAnswer = "";
_singleAnswer = -1;
_multipleAnswer.Clear();
errorTip.gameObject.SetActive(false);
title.gameObject.SetActive(true);
toggleArea.gameObject.SetActive(true);
answerSure.gameObject.SetActive(true);
}
/// <summary>
/// 创建一个题目
/// </summary>
/// <param name="tags">题目标签</param>
public void InitQuestion(string tags)
{
if (!_questionData.QuestionInfos.ContainsKey(tags))
{
Debug.LogError("没有该题目标签" + ":" + tags);
return;
}
InitPanel();
QuestionInfo info = _questionData.QuestionInfos[tags];
title.text = info.title;
if (info.type == QuestionType.multiple)
{
MultipleInfo toggleInfo = info as MultipleInfo;
_multipleAnswer = toggleInfo.answer;
if (toggleInfo.options.Count <= 4)
{
toggleArea.GetComponent<GridLayoutGroup>().cellSize = new Vector2(600,70);
}
else if(toggleInfo.options.Count <= 8)
{
toggleArea.GetComponent<GridLayoutGroup>().cellSize = new Vector2(300,50);
}
else
{
Debug.LogError("超出最大上限8个");
}
for (int i = 0; i < toggleInfo.options.Count; i++)
{
GameObject obj = GameObject.Instantiate(togglePre);
QuestionCell questionCell = obj.GetComponent<QuestionCell>();
cells.Add(questionCell);
questionCell.transform.SetParent(toggleArea.transform);
questionCell.InitToggle(toggleInfo.options[i],i);
}
answerSure.onClick.AddListener(AnswerCallBackByMultiple);
}
else if (info.type == QuestionType.Single)
{
SingleInfo toggleInfo = info as SingleInfo;
_singleAnswer = toggleInfo.answer;
if (toggleInfo.options.Count <= 4)
{
toggleArea.GetComponent<GridLayoutGroup>().cellSize = new Vector2(600,70);
}
else if(toggleInfo.options.Count <= 8)
{
toggleArea.GetComponent<GridLayoutGroup>().cellSize = new Vector2(300,50);
}
else
{
Debug.LogError("超出最大上限8个");
}
ToggleGroup group = toggleArea.GetComponent<ToggleGroup>();
for (int i = 0; i < toggleInfo.options.Count; i++)
{
GameObject obj = GameObject.Instantiate(togglePre);
QuestionCell questionCell = obj.GetComponent<QuestionCell>();
cells.Add(questionCell);
questionCell.transform.SetParent(toggleArea.transform);
questionCell.InitToggle(toggleInfo.options[i],i);
questionCell.GetComponent<Toggle>().group = group;
}
answerSure.onClick.AddListener(AnswerCallBackBySingle);
}
else if (info.type == QuestionType.Input)
{
InputfieldInfo toggleInfo = info as InputfieldInfo;
_inputAnswer = toggleInfo.answer;
GameObject obj = GameObject.Instantiate(inputPre);
QuestionCell questionCell = obj.GetComponent<QuestionCell>();
cells.Add(questionCell);
questionCell.transform.SetParent(transform);
answerSure.onClick.AddListener(AnswerCallBackByInputField);
}
if(!isNeedChaos) return;
//随机选项
if (info.type != QuestionType.Input)
{
for (int i = 0; i < cells.Count; i++)
{
int rand = Random.Range(0, cells.Count);
QuestionCell temp = cells[i];
cells[i] = cells[rand];
cells[rand] = temp;
}
foreach (var btn in cells)
{
btn.transform.SetParent(transform);
btn.transform.SetParent(toggleArea.transform);
}
}
}
/// <summary>
/// 单选题回调
/// </summary>
public void AnswerCallBackBySingle()
{
foreach (QuestionCell toggle in cells)
{
if (toggle.GetComponent<Toggle>().isOn && toggle.id != _singleAnswer)
{
//错误
ErrorCallBack();
return;
}
}
//正确
SureCallBack();
questionPanel.SetActive(false);
}
/// <summary>
/// 多选题回调
/// </summary>
public void AnswerCallBackByMultiple()
{
foreach (QuestionCell toggle in cells)
{
if (toggle.GetComponent<Toggle>().isOn && !_multipleAnswer.Contains(toggle.id))
{
//错误
ErrorCallBack();
return;
}
}
//正确
SureCallBack();
questionPanel.SetActive(false);
}
/// <summary>
/// 填空题回调
/// </summary>
public void AnswerCallBackByInputField()
{
if (_inputAnswer == cells[0].inputText.text)
{
//正确
SureCallBack();
questionPanel.SetActive(false);
}
else
{
//错误
ErrorCallBack();
}
}
void ErrorDisappear()
{
errorTip.gameObject.SetActive(false);
}
void SureCallBack()
{
sureEvent?.Invoke();
}
void ErrorCallBack()
{
errorTip.gameObject.SetActive(true);
errorTip.transform.localScale = new Vector3(0, 1, 1);
errorTip.transform.DOScaleX(1, 0.5f);
Invoke("ErrorDisappear",3f);
errorEvent?.Invoke();
}
}