VirtualFramework/Assets/Scripts/UI/UITextQuestion.cs
2025-01-06 10:49:36 +08:00

179 lines
6.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
using TMPro;
using System;
using Microsoft.SqlServer.Server;
using XMLTool;
using static OperationController;
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;
/// <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<StepStatusOnChange>(OnStepChanged);
}
private void OnStepChanged(StepStatusOnChange change)
{
Hide();
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UITextQuestionData ?? new UITextQuestionData();
Title.text = mData.title;
OptionContent.transform.RemoveAllChildren();
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();
}
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 (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 (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()
{
}
}
}