367 lines
11 KiB
C#
367 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using ZGameFramework;
|
|
|
|
namespace Script.UI
|
|
{
|
|
public class Right_QuestionItem : MonoBehaviour
|
|
{
|
|
[SerializeField] private Text txt_Right_Title;
|
|
[SerializeField] private Text txt_Title;
|
|
[SerializeField] private Text txt_Item;
|
|
[SerializeField] private Toggle tog_Option_Item;
|
|
[SerializeField] private Button btn_Last;
|
|
[SerializeField] private Button btn_Next;
|
|
[SerializeField] private Button btn_Submit;
|
|
[SerializeField] private ToggleGroup _toggleGroup;
|
|
|
|
private List<Toggle> toggles = new List<Toggle>();
|
|
|
|
private string title;
|
|
private string[] options;
|
|
private string[] answers;
|
|
|
|
private int currentIndex;
|
|
private List<GameObject> textItemList = new List<GameObject>();
|
|
private List<GameObject> toggleItemList = new List<GameObject>();
|
|
private Action<int> lastAction;
|
|
private Action<int> nextAction;
|
|
|
|
private AnswerData answerData;
|
|
|
|
private void Awake()
|
|
{
|
|
btn_Last.onClick.AddListener(ClickLase);
|
|
btn_Next.onClick.AddListener(ClickNext);
|
|
btn_Submit.onClick.AddListener(ClickSubmit);
|
|
gameObject.SetActive(false);
|
|
btn_Submit.gameObject.SetActive(false);
|
|
EventManager.Instance.Subscribe(Right_QuestionItemStopInputEventArgs.EventId, Right_QuestionItemStopInputEvent);
|
|
}
|
|
|
|
private void Right_QuestionItemStopInputEvent(object sender, GameEventArgs e)
|
|
{
|
|
var args = e as Right_QuestionItemStopInputEventArgs;
|
|
if (args.index == currentIndex)
|
|
{
|
|
// answerData.StopSetData();
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
btn_Last.onClick.RemoveListener(ClickLase);
|
|
btn_Next.onClick.RemoveListener(ClickNext);
|
|
btn_Submit.onClick.RemoveListener(ClickSubmit);
|
|
EventManager.Instance.Unsubscribe(Right_QuestionItemStopInputEventArgs.EventId, Right_QuestionItemStopInputEvent);
|
|
}
|
|
|
|
public void SetData(int currentIndex, string right_Title, string title, string[] options, string[] answers, Action<int> last, Action<int> next)
|
|
{
|
|
this.currentIndex = currentIndex;
|
|
this.title = title;
|
|
this.options = options;
|
|
this.answers = answers;
|
|
|
|
txt_Right_Title.text = right_Title;
|
|
txt_Title.text = title;
|
|
|
|
GetOrAddText(options);
|
|
GetOrAddToggle(options);
|
|
btn_Submit.interactable = true;
|
|
this.lastAction = last;
|
|
this.nextAction = next;
|
|
|
|
answerData = new AnswerData();
|
|
answerData.InitData(this.answers);
|
|
|
|
if (currentIndex <= 0)
|
|
{
|
|
btn_Last.interactable = false;
|
|
btn_Next.interactable = true;
|
|
}
|
|
else if (currentIndex >= 4)
|
|
{
|
|
btn_Last.interactable = true;
|
|
btn_Next.interactable = false;
|
|
}
|
|
else
|
|
{
|
|
btn_Last.interactable = true;
|
|
btn_Next.interactable = true;
|
|
}
|
|
}
|
|
|
|
public void Show()
|
|
{
|
|
gameObject.SetActive(true);
|
|
// 判断是否需要重置数据
|
|
if (answerData.IsCanSet)
|
|
{
|
|
GetOrAddToggle(options);
|
|
}
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
gameObject.SetActive(false);
|
|
// 判断是否需要重置数据
|
|
if (answerData.IsCanSet)
|
|
{
|
|
GetOrAddToggle(options);
|
|
}
|
|
}
|
|
|
|
void GetOrAddText(string[] str)
|
|
{
|
|
txt_Item.gameObject.SetActive(false);
|
|
foreach (var go in textItemList)
|
|
{
|
|
go.SetActive(false);
|
|
}
|
|
|
|
var count = str.Length - textItemList.Count;
|
|
if (count > 0)
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
var go = GameObject.Instantiate(txt_Item.gameObject, txt_Item.transform.parent);
|
|
textItemList.Add(go);
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < str.Length; i++)
|
|
{
|
|
var item = textItemList[i];
|
|
var text = item.GetComponent<Text>();
|
|
text.text = str[i];
|
|
item.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
|
|
void GetOrAddToggle(string[] str)
|
|
{
|
|
tog_Option_Item.gameObject.SetActive(false);
|
|
foreach (var go in toggleItemList)
|
|
{
|
|
go.SetActive(false);
|
|
var toggle = go.GetComponent<Toggle>();
|
|
toggle.isOn = false;
|
|
toggle.onValueChanged.RemoveAllListeners();
|
|
}
|
|
|
|
var count = str.Length - toggleItemList.Count;
|
|
if (count > 0)
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
var go = GameObject.Instantiate(tog_Option_Item.gameObject, tog_Option_Item.transform.parent);
|
|
toggleItemList.Add(go);
|
|
toggles.Add(go.GetComponent<Toggle>());
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < str.Length; i++)
|
|
{
|
|
var substring = str[i].Substring(0, 1);
|
|
var item = toggleItemList[i];
|
|
var toggle = item.GetComponent<Toggle>();
|
|
toggle.interactable = true;
|
|
toggle.isOn = false;
|
|
toggle.onValueChanged.AddListener((isTrue) => { RightToggleChanged(isTrue, substring); });
|
|
var componentInChildren = toggle.GetComponentInChildren<Text>();
|
|
componentInChildren.text = substring;
|
|
item.name = substring;
|
|
item.SetActive(true);
|
|
if (currentIndex == 4)
|
|
toggle.group = null;
|
|
else
|
|
toggle.group = _toggleGroup;
|
|
}
|
|
}
|
|
|
|
private void RightToggleChanged(bool isTrue, string str)
|
|
{
|
|
answerData.SetInfoData(str, isTrue);
|
|
// if (isTrue)
|
|
CheckIsAnswer();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查是否回答了
|
|
/// </summary>
|
|
private void CheckIsAnswer()
|
|
{
|
|
bool isNull = true;
|
|
foreach (var toggle in toggles)
|
|
{
|
|
if (toggle.isOn)
|
|
{
|
|
isNull = false;
|
|
}
|
|
}
|
|
|
|
EventManager.Instance.FireNow(this, new QuestionSureEventArgs(currentIndex, !isNull));
|
|
if (!isNull)
|
|
{
|
|
// _toggleGroup.allowSwitchOff = false;
|
|
answerData.StopSetData();
|
|
}
|
|
}
|
|
|
|
private void ClickLase()
|
|
{
|
|
int index = currentIndex - 1;
|
|
lastAction?.Invoke(index);
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void ClickNext()
|
|
{
|
|
int index = currentIndex + 1;
|
|
nextAction?.Invoke(index);
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void ClickSubmit() // 弃用
|
|
{
|
|
bool isTrue = false;
|
|
foreach (var go in toggleItemList)
|
|
{
|
|
if (go.GetComponent<Toggle>().isOn)
|
|
{
|
|
isTrue = true;
|
|
}
|
|
}
|
|
|
|
if (!isTrue) return;
|
|
|
|
// var resultType = GetResult();
|
|
//
|
|
// switch (resultType)
|
|
// {
|
|
// case ResultType.未答:
|
|
// Debug.Log($"第{currentIndex + 1}题,未作答!");
|
|
// break;
|
|
// case ResultType.正确:
|
|
// Debug.Log($"第{currentIndex + 1}题,回答正确!");
|
|
// break;
|
|
// case ResultType.错误:
|
|
// Debug.Log($"第{currentIndex + 1}题,回答错误!");
|
|
// break;
|
|
// default:
|
|
// throw new ArgumentOutOfRangeException();
|
|
// }
|
|
|
|
foreach (var go in toggleItemList)
|
|
{
|
|
go.GetComponent<Toggle>().interactable = false;
|
|
}
|
|
|
|
answerData.StopSetData();
|
|
btn_Submit.interactable = false;
|
|
|
|
// EventManager.Instance.FireNow(this, new QuestionSureEventArgs(currentIndex));
|
|
}
|
|
|
|
/// <summary>
|
|
/// huoqu结果
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public ResultType GetResult()
|
|
{
|
|
List<string> list = new List<string>();
|
|
foreach (var toggle in toggles)
|
|
{
|
|
if (toggle.isOn)
|
|
{
|
|
list.Add(toggle.name);
|
|
}
|
|
}
|
|
|
|
GlobalManager.Instance.AddCurrentResult(currentIndex, list.ToArray());
|
|
|
|
return answerData.GetDataResult();
|
|
}
|
|
}
|
|
|
|
public class AnswerData
|
|
{
|
|
private List<ADInfo> datas = new List<ADInfo>();
|
|
private bool isCanSet;
|
|
|
|
public bool IsCanSet => isCanSet;
|
|
|
|
private class ADInfo
|
|
{
|
|
public string answer;
|
|
public bool result;
|
|
}
|
|
|
|
public void InitData(string[] str)
|
|
{
|
|
isCanSet = true;
|
|
|
|
datas = new List<ADInfo>();
|
|
foreach (var s in str)
|
|
{
|
|
datas.Add(new ADInfo() { answer = s, result = false });
|
|
}
|
|
}
|
|
|
|
public bool SetInfoData(string str, bool isTrue)
|
|
{
|
|
if (IsCanSet)
|
|
{
|
|
foreach (var adInfo in datas)
|
|
{
|
|
if (adInfo.answer == str)
|
|
{
|
|
adInfo.result = isTrue;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void ResetData()
|
|
{
|
|
isCanSet = true;
|
|
foreach (var adInfo in datas)
|
|
{
|
|
adInfo.result = false;
|
|
}
|
|
}
|
|
|
|
public void StopSetData()
|
|
{
|
|
isCanSet = false;
|
|
}
|
|
|
|
public ResultType GetDataResult()
|
|
{
|
|
if (isCanSet) return ResultType.未答;
|
|
foreach (var adInfo in datas)
|
|
{
|
|
if (!adInfo.result)
|
|
{
|
|
return ResultType.错误;
|
|
}
|
|
}
|
|
|
|
return ResultType.正确;
|
|
}
|
|
}
|
|
|
|
public enum ResultType
|
|
{
|
|
未答 = 0,
|
|
正确,
|
|
错误,
|
|
}
|
|
} |