2025-01-09 13:35:04 +08:00

167 lines
6.4 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
using TMPro;
using System.Linq;
using DG.Tweening;
using static OperationController;
namespace QFramework.Example
{
public class UIToolsData : UIPanelData
{
public List<string> devices;
public string answer;
public bool SetActive = true;
public string rightLable;
public string wrongLabel;
public string rightEvent;
public string wrongEvent;
public float rightScore;
public float wrongScore;
public string scoreStepName;
}
public partial class UITools : UIPanel
{
ResLoader mResLoader;
public List<string> answers;
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UIToolsData ?? new UIToolsData();
// please add init code here
TypeEventSystem.Global.Register<OnModuleQuit>(OnModuleQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
}
private void OnStepChanged(StepStatusOnChange change)
{
Hide();
}
protected override void OnOpen(IUIData uiData = null)
{
TypeEventSystem.Global.Register<StepStatusOnChange>(OnStepChanged).UnRegisterWhenDisabled(gameObject);
mResLoader = ResLoader.Allocate();
mData = uiData as UIToolsData ?? new UIToolsData();
if (string.IsNullOrEmpty(mData.answer) == false)
{
answers = mData.answer.Split(',')?.ToList();
}
Content.RemoveAllChildren();
foreach (var device in mData.devices)
{
var item = DeviceController.Instance.GetDevice(device);
if (item==null)
{
Debug.LogError(device + ":ûÓÐÕÒµ½¶ÔÓ¦µÄDeviceÅäÖÃ");
return;
}
GameObject obj = GameObject.Instantiate(ItemPrefab.gameObject, Content);
obj.name = item.Name;
obj.transform.Find("Name").GetComponent<TextMeshProUGUI>().text = item.Name;
Image icon = obj.transform.Find("Icon").GetComponent<Image>();
var localImageUrl = Global.deviceIconsPath + item.Icon;
GameObject right = icon.transform.Find("Right").gameObject;
GameObject wrong = icon.transform.Find("Wrong").gameObject;
GameObject Selected = icon.transform.Find("Selected").gameObject;
GameObject CurSelect = obj.transform.Find("CurSelect").gameObject;
mResLoader.Add2Load(localImageUrl.ToNetImageResName(),
(bool success, IRes res) =>
{
if (success)
{
icon.sprite = Utility.GetSprite(res.Asset as Texture2D);
}
});
Button btn = obj.GetComponent<Button>();
btn.onClick.AddListener(() =>
{
CurSelect.gameObject.SetActive(true);
if (answers != null)
{
if (answers.Contains(item.Name))
{
SetSelected(obj, true);
if (mData.SetActive)
{
DeviceController.Instance.GetDeviceObj(device).SetActive(true);
}
ScoreController.Instance.Add(mData.scoreStepName, mData.rightScore);
answers.Remove(item.Name);
if (answers.Count <= 0)
{
var data = new UIResultTipData();
data.label = mData.rightLable;
data.isRight = true;
data.callback = () =>
{
StringEventSystem.Global.Send(mData.rightEvent);
};
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().StartGlobal();
}
}
else
{
ScoreController.Instance.Add(mData.scoreStepName, mData.wrongScore);
var data = new UIResultTipData();
data.label = mData.wrongLabel;
data.isRight = false;
data.callback = () =>
{
StringEventSystem.Global.Send(mData.wrongEvent);
SetSelected(obj, false);
};
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().Start(this);
}
}
});
}
mResLoader.LoadAsync();
Scroll.verticalNormalizedPosition = 1;
}
public void SetSelected(GameObject item, bool isRight)
{
Transform icon = item.transform.Find("Icon");
GameObject right = icon.Find("Right").gameObject;
GameObject wrong = icon.Find("Wrong").gameObject;
GameObject Selected = icon.Find("Selected").gameObject;
GameObject CurSelect = item.transform.Find("CurSelect").gameObject;
CurSelect.SetActive(false);
Selected.SetActive(true);
if (isRight)
{
right.SetActive(true);
wrong.SetActive(false);
}
else
{
right.SetActive(false);
wrong.SetActive(true);
}
icon.GetComponent<Image>().color = new Color(255f / 255f, 255f / 255f, 255f / 255f, 51f / 255f);
item.GetComponent<Button>().onClick.RemoveAllListeners();
}
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
//TypeEventSystem.Global.UnRegister<OnModuleQuit>(OnModuleQuit);
}
public void OnModuleQuit(OnModuleQuit arg)
{
Hide();
}
}
}