VirtualFramework/Assets/Scripts/UI/UITextQuestion.cs

169 lines
5.9 KiB
C#
Raw Normal View History

2024-12-14 18:27:59 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
using TMPro;
2024-12-26 15:44:04 +08:00
using System;
using Microsoft.SqlServer.Server;
using XMLTool;
2024-12-30 19:15:34 +08:00
using static OperationController;
2024-12-14 18:27:59 +08:00
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;
2024-12-26 15:44:04 +08:00
public float rightScore = 0;
public float errorScore = 0;
public string scoreName = string.Empty;
public string format;
/// <summary>
/// <20><><EFBFBD>Ե<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD> <20>Ծ͵÷<CDB5> <20><><EFBFBD>Ͳ<EFBFBD><CDB2>÷<EFBFBD>
/// </summary>
public bool absolutely = true;
2024-12-14 18:27:59 +08:00
}
public partial class UITextQuestion : UIPanel
{
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UITextQuestionData ?? new UITextQuestionData();
// please add init code here
2024-12-30 19:15:34 +08:00
TypeEventSystem.Global.Register<StepStatusOnChange>(OnStepChanged);
2024-12-14 18:27:59 +08:00
}
2024-12-30 19:15:34 +08:00
private void OnStepChanged(StepStatusOnChange change)
{
Hide();
}
2024-12-14 18:27:59 +08:00
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);
2024-12-19 17:32:52 +08:00
obj.name = item;
2024-12-14 18:27:59 +08:00
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;
}
}
}
2024-12-26 15:44:04 +08:00
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));
}
});
}
}
2024-12-14 18:27:59 +08:00
if (mData.waitCloseTime != -1)
{
ActionKit.Delay(mData.waitCloseTime, () => Hide()).Start(this);
return;
}
2024-12-26 15:44:04 +08:00
2024-12-14 18:27:59 +08:00
Hide();
});
}
}
2024-12-26 15:44:04 +08:00
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);
}
}
2024-12-14 18:27:59 +08:00
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
}
}
}