2025-06-09 19:20:32 +08:00

194 lines
6.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
using TMPro;
using System.Linq;
namespace QFramework.Example
{
public class UIInputData : UIPanelData
{
public class InputData
{
public string name;
public string var;
public string answer;
public string unit;
}
public class BtnData
{
public string name;
public string clickEvent;
public bool checkAnswer;
public string wrongLabel;
public string wrongEvent;
public int wrongCount;
public string scoreStepName;
public float rightScore = 0;
public float wrongScore = 0;
public float autoHide = -1;
}
public string title;
public List<InputData> inputs = new List<InputData>();
public List<BtnData> btns = new List<BtnData>();
public string finishedEvent;
}
public partial class UIInput : UIPanel
{
int curCount = 0;
protected override void OnInit(IUIData uiData = null)
{
// please add init code here
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UIInputData ?? new UIInputData();
curCount = 0;
Title.text = mData.title;
InputContent.RemoveAllChildren();
BtnContent.RemoveAllChildren();
foreach (var item in mData.inputs)
{
GameObject input = GameObject.Instantiate(InputItem.gameObject, InputContent);
input.name = item.name;
input.transform.Find("Name").GetComponent<TextMeshProUGUI>().text = item.name;
input.transform.Find("Unit").GetComponent<TextMeshProUGUI>().text = item.unit;
var inputField = input.transform.Find("Input").GetComponent<TMP_InputField>();
inputField.onEndEdit.AddListener(value =>
{
if (string.IsNullOrEmpty(item.var) == false)
{
float v = 0;
float.TryParse(value, out v);
VarController.Instance.Set(item.var, v);
}
});
}
foreach (var item in mData.btns)
{
GameObject btn = GameObject.Instantiate(BtnItem.gameObject, BtnContent);
btn.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = item.name;
btn.name = item.name;
btn.GetComponent<Button>().onClick.AddListener(() =>
{
if (item.checkAnswer)
{
if (Check())
{
if (string.IsNullOrEmpty(item.scoreStepName) == false)
{
ScoreController.Instance.Add(item.scoreStepName, item.rightScore);
}
Hide();
}
else
{
if (curCount >= item.wrongCount)
{
Hide();
}
else
{
if (string.IsNullOrEmpty(item.scoreStepName) == false)
{
ScoreController.Instance.Add(item.scoreStepName, item.wrongScore);
}
curCount++;
var data = new UIResultTipData();
data.label = item.wrongLabel;
data.isRight = false;
data.callback = () =>
{
if (string.IsNullOrEmpty(item.wrongEvent) == false)
{
StringEventSystem.Global.Send(item.wrongEvent);
}
};
data.autoHideTime = item.autoHide;
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().Start(this);
}
}
}
else
{
Hide();
if (string.IsNullOrEmpty(item.clickEvent) == false)
{
StringEventSystem.Global.Send(item.clickEvent);
}
}
});
}
}
public bool Check()
{
bool allCorrect = true; // <20><>ʼ<EFBFBD><CABC>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ȷ
foreach (var item in mData.inputs)
{
string[] list = item.answer.Contains('|')
? item.answer.Split('|')
: new[] { item.answer }; // <20>޷ָ<DEB7><D6B8><EFBFBD>ʱ<EFBFBD><CAB1>Ϊ<EFBFBD><CEAA>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
if (string.IsNullOrEmpty(item.var))
{
Transform trans = InputContent.transform.Find(item.name);
string inputStr = trans.Find("Input").GetComponent<TMP_InputField>().text;
//Debug.Log(item.var);
bool currentCorrect = false; // <20><><EFBFBD>ǵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȷ
for (int i = 0; i < list.Length; i++)
{
if (inputStr.Equals(list[i]))
{
currentCorrect = true;
break; // <20>ҵ<EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
}
}
if (!currentCorrect) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EBB2BB>ȷ
{
allCorrect = false; // <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD>д<EFBFBD><D0B4><EFBFBD>
}
}
else
{
if (!list.Contains(VarController.Instance.Get(item.var).ToString()))
{
allCorrect = false; // <20><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>ƥ<EFBFBD><C6A5>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ϊ<EFBFBD>д<EFBFBD><D0B4><EFBFBD>
}
}
}
return allCorrect; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>
}
protected override void OnShow()
{
}
protected override void OnHide()
{
if (string.IsNullOrEmpty(mData.finishedEvent) == false)
{
StringEventSystem.Global.Send(mData.finishedEvent);
}
mData = null;
}
protected override void OnClose()
{
}
}
}