2025-03-07 16:02:36 +08:00

215 lines
8.4 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;
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 float totalScore;
public string scoreStepName;
public float autoHideResult = -1;
public bool random = false;
public float scrollSpeed = 25;
public string position;
}
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)
{
mData = uiData as UIToolsData ?? new UIToolsData();
TypeEventSystem.Global.Register<StepStatusOnChange>(OnStepChanged).UnRegisterWhenDisabled(gameObject);
if (mData.totalScore > 0)
{
ScoreController.Instance.Add(mData.scoreStepName, mData.totalScore);
}
mResLoader = ResLoader.Allocate();
mData = uiData as UIToolsData ?? new UIToolsData();
if (string.IsNullOrEmpty(mData.answer) == false)
{
answers = mData.answer.Split(',')?.ToList();
}
Content.RemoveAllChildren();
if (mData.devices.Count > 0 && mData.random)
{
Utility.Shuffle(mData.devices);
}
Scroll.scrollSensitivity = mData.scrollSpeed;
foreach (var device in mData.devices)
{
var item = DeviceController.Instance.GetDevice(device);
if (item == null)
{
Debug.LogError(device + ":û<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6>Device<63><65><EFBFBD><EFBFBD>");
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("IconBg/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;
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(() =>
{
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 = () =>
{
if (string.IsNullOrEmpty(mData.rightEvent) == false)
{
StringEventSystem.Global.Send(mData.rightEvent);
}
};
data.autoHideTime = mData.autoHideResult;
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 = () =>
{
if (string.IsNullOrEmpty(mData.wrongEvent) == false)
{
StringEventSystem.Global.Send(mData.wrongEvent);
}
SetSelected(obj, false);
};
data.autoHideTime = mData.autoHideResult;
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().Start(this);
}
}
});
}
mResLoader.LoadAsync();
Scroll.verticalNormalizedPosition = 1;
switch (mData.position)
{
case "left":
// <20><>ê<EFBFBD><C3AA><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>м䣨Middle-Right<68><74>
bg.rectTransform.anchorMin = new Vector2(0, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD><EFBFBD>Ҳࣩ
bg.rectTransform.anchorMax = new Vector2(0, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD><EFBFBD>Ҳࣩ
// <20><>λ<EFBFBD><CEBB>ƫ<EFBFBD>ƹ<EFBFBD><C6B9><EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ê<EFBFBD>
bg.rectTransform.anchoredPosition = Vector2.zero;
// <20><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>Ϊ<EFBFBD>Ҳ<EFBFBD><D2B2>е㣨Ӱ<E3A3A8><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/<2F><>ת<EFBFBD><D7AA><EFBFBD>ģ<EFBFBD>
bg.rectTransform.pivot = new Vector2(0, 0.5f);
break;
case "right":
default:
// <20><>ê<EFBFBD><C3AA><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>м䣨Middle-Right<68><74>
bg.rectTransform.anchorMin = new Vector2(1, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD><EFBFBD>Ҳࣩ
bg.rectTransform.anchorMax = new Vector2(1, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD><EFBFBD>Ҳࣩ
// <20><>λ<EFBFBD><CEBB>ƫ<EFBFBD>ƹ<EFBFBD><C6B9><EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ê<EFBFBD>
bg.rectTransform.anchoredPosition = Vector2.zero;
// <20><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>Ϊ<EFBFBD>Ҳ<EFBFBD><D2B2>е㣨Ӱ<E3A3A8><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/<2F><>ת<EFBFBD><D7AA><EFBFBD>ģ<EFBFBD>
bg.rectTransform.pivot = new Vector2(1, 0.5f);
break;
}
}
public void SetSelected(GameObject item, bool isRight)
{
Transform icon = item.transform.Find("IconBg/Icon");
GameObject right = icon.Find("Right").gameObject;
GameObject wrong = icon.Find("Wrong").gameObject;
GameObject Selected = icon.Find("Selected").gameObject;
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();
}
}
}