231 lines
8.3 KiB
C#
231 lines
8.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using QFramework;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using System;
|
|
using static OperationController;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace QFramework.Example
|
|
{
|
|
public class UITextQuestionData : UIPanelData
|
|
{
|
|
public string title;
|
|
public List<string> options = new List<string>();
|
|
public List<string> answers = new List<string>();
|
|
public List<string> btns = new List<string>();
|
|
public float waitCloseTime = -1;
|
|
public bool showAnswer = false;
|
|
public float rightScore = 0;
|
|
public float errorScore = 0;
|
|
public string scoreName = string.Empty;
|
|
public string format;
|
|
public string finishedEvent;
|
|
public string des;
|
|
public string cellSize;
|
|
/// <summary>
|
|
/// 0:自动判断 1:单选 2:多选
|
|
/// </summary>
|
|
public int OptionType = 0;
|
|
/// <summary>
|
|
/// 绝对的 不计算分项得分 对就得分 错就不得分
|
|
/// </summary>
|
|
public bool absolutely = true;
|
|
}
|
|
public partial class UITextQuestion : UIPanel
|
|
{
|
|
protected override void OnInit(IUIData uiData = null)
|
|
{
|
|
mData = uiData as UITextQuestionData ?? new UITextQuestionData();
|
|
// please add init code here
|
|
TypeEventSystem.Global.Register<OnModuleQuit>((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject);
|
|
}
|
|
|
|
private void OnStepChanged(StepStatusOnChange change)
|
|
{
|
|
Hide();
|
|
}
|
|
protected override void OnOpen(IUIData uiData = null)
|
|
{
|
|
TypeEventSystem.Global.Register<StepStatusOnChange>(OnStepChanged).UnRegisterWhenDisabled(gameObject);
|
|
mData = uiData as UITextQuestionData ?? new UITextQuestionData();
|
|
Des.text = string.Empty;
|
|
Title.text = mData.title;
|
|
OptionContent.transform.RemoveAllChildren();
|
|
if (string.IsNullOrEmpty(mData.cellSize) == false)
|
|
{
|
|
OptionContent.GetComponent<GridLayoutGroup>().cellSize = Utility.GetVector2FromStrArray(mData.cellSize);
|
|
}
|
|
for (int i = 0; i < mData.options.Count; i++)
|
|
{
|
|
var item = mData.options[i];
|
|
GameObject obj = GameObject.Instantiate(OptionPrefab.gameObject, OptionContent.transform);
|
|
obj.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = item;
|
|
obj.name = (i + 1).ToString();
|
|
bool isGroup = false;
|
|
switch (mData.OptionType)
|
|
{
|
|
case 0:
|
|
isGroup = mData.answers.Count == 1;
|
|
break;
|
|
case 1:
|
|
isGroup = true;
|
|
break;
|
|
case 2:
|
|
isGroup = false;
|
|
break;
|
|
}
|
|
if (isGroup)
|
|
{
|
|
obj.GetComponent<Toggle>().group = OptionContent.GetComponent<ToggleGroup>();
|
|
}
|
|
}
|
|
|
|
BtnContent.RemoveAllChildren();
|
|
foreach (var item in mData.btns)
|
|
{
|
|
GameObject obj = GameObject.Instantiate(BtnPrefab.gameObject, BtnContent);
|
|
obj.name = item;
|
|
var label = obj.transform.Find("Label").GetComponent<TextMeshProUGUI>();
|
|
label.text = item;
|
|
obj.GetComponent<Button>().onClick.AddListener(() =>
|
|
{
|
|
if (string.IsNullOrEmpty(mData.des) == false && Des.text != string.Empty)
|
|
{
|
|
HideSelf();
|
|
return;
|
|
}
|
|
if (mData.showAnswer)
|
|
{
|
|
for (int i = 0; i < OptionContent.transform.childCount; i++)
|
|
{
|
|
Transform trans = OptionContent.transform.GetChild(i);
|
|
if (mData.answers.Contains(trans.name))
|
|
{
|
|
trans.Find("Label").GetComponent<TextMeshProUGUI>().color = Color.green;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 分数名不为空
|
|
if (string.IsNullOrEmpty(mData.scoreName) == false)
|
|
{
|
|
if (mData.rightScore != 0)
|
|
{
|
|
Check(true, count =>
|
|
{
|
|
if (count > 0)
|
|
{
|
|
float score = mData.rightScore / mData.answers.Count * count;
|
|
string scoreStr = score.ToString(mData.format);
|
|
ScoreController.Instance.Add(mData.scoreName, float.Parse(scoreStr));
|
|
}
|
|
});
|
|
}
|
|
else if (mData.errorScore != 0)
|
|
{
|
|
Check(false, count =>
|
|
{
|
|
if (count > 0)
|
|
{
|
|
float score = mData.errorScore / mData.answers.Count * count;
|
|
string scoreStr = score.ToString(mData.format);
|
|
ScoreController.Instance.Add(mData.scoreName, float.Parse(scoreStr));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// 解析不为空
|
|
if (string.IsNullOrEmpty(mData.des) == false)
|
|
{
|
|
Check(true, count =>
|
|
{
|
|
if (count != mData.answers.Count)
|
|
{
|
|
Des.text = mData.des;
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
HideSelf();
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
if (mData.waitCloseTime != -1)
|
|
{
|
|
ActionKit.Delay(mData.waitCloseTime, () => HideSelf()).Start(this);
|
|
return;
|
|
}
|
|
}
|
|
|
|
HideSelf();
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
public void HideSelf()
|
|
{
|
|
if (string.IsNullOrEmpty(mData.finishedEvent) == false)
|
|
{
|
|
StringEventSystem.Global.Send(mData.finishedEvent);
|
|
}
|
|
Hide();
|
|
}
|
|
|
|
public void Check(bool isRight, Action<int> callback)
|
|
{
|
|
int count = 0;
|
|
if (isRight)
|
|
{
|
|
for (int i = 0; i < OptionContent.transform.childCount; i++)
|
|
{
|
|
Toggle toggle = OptionContent.transform.GetChild(i).GetComponent<Toggle>();
|
|
if (mData.answers.Contains(toggle.name) && toggle.isOn)
|
|
{
|
|
count++;
|
|
}
|
|
}
|
|
if (mData.absolutely == true && count != mData.answers.Count)
|
|
{
|
|
count = 0;
|
|
}
|
|
callback?.Invoke(count);
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < OptionContent.transform.childCount; i++)
|
|
{
|
|
Toggle toggle = OptionContent.transform.GetChild(i).GetComponent<Toggle>();
|
|
if (mData.answers.Contains(toggle.name) && toggle.isOn == false)
|
|
{
|
|
count++;
|
|
}
|
|
}
|
|
if (mData.absolutely == true && count > 0)
|
|
{
|
|
count = mData.answers.Count;
|
|
}
|
|
callback?.Invoke(count);
|
|
}
|
|
}
|
|
|
|
protected override void OnShow()
|
|
{
|
|
}
|
|
|
|
protected override void OnHide()
|
|
{
|
|
}
|
|
|
|
protected override void OnClose()
|
|
{
|
|
}
|
|
}
|
|
}
|