Files
back_cantanBuilding/Assets/Scripts/UI/Albun/AlbumExchangePopupPanel.cs
2026-05-26 16:15:54 +08:00

197 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using asap.core;
using cfg;
using GameCore;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class AlbumExchangePopupPanel : MonoBehaviour
{
public Button btn_close;
public Button btn_mask;
public TMP_Text text_repetitive_card;
List<AlbumExchange> exhangeList = new List<AlbumExchange>();
List<BntState> bntStates = new List<BntState>();
public SelectPicturePopupPanel selectPicturePopupPanel;
List<RewardItem> rewards = new List<RewardItem>();
private List<Timer> timers = new List<Timer>();
#if UNITY_EDITOR
private void Reset()
{
Awake();
}
#endif
private void Awake()
{
btn_close = transform.Find("btn_close").GetComponent<Button>();
btn_mask = transform.Find("mask").GetComponent<Button>();
text_repetitive_card = transform.Find("root/cost/text_repetitive_card").GetComponent<TMP_Text>();
selectPicturePopupPanel = transform.Find("SelectPicturePopupPanel").GetComponent<SelectPicturePopupPanel>();
selectPicturePopupPanel.gameObject.SetActive(false);
selectPicturePopupPanel.albumExchangePopupPanel = this;
for (int i = 1; i <= 3; i++)
{
GameObject go = transform.Find($"root/list_exchange/Item{i}").gameObject;
rewards.Add(transform.Find($"root/list_exchange/Item{i}/reward").GetComponent<RewardItem>());
BntState bntState = new BntState
{
index = i,
openExchangePopupPanel = OpenExchangePopupPanel,
text_num = go.transform.Find("btn_exchange/btn_green_c/Ani_Container/content_cost/cost/p_text_num").GetComponent<TMP_Text>(),
text_num1 = go.transform.Find("exchange_cd/btn_exchange/btn_green_c/Ani_Container/content_cost/cost/p_text_num").GetComponent<TMP_Text>(),
btn_exchange = go.transform.Find("btn_exchange/btn_green_c").GetComponent<Button>(),
btn_exchange_cd = go.transform.Find("exchange_cd/btn_exchange/btn_green_c").GetComponent<Button>(),
text_time = go.transform.Find("exchange_cd/text_time").GetComponent<TMP_Text>(),
exchange_cd = go.transform.Find("exchange_cd").gameObject,
};
bntState.SetButtonClick();
bntStates.Add(bntState);
}
}
private void Start()
{
List<int> exchangeIdList = GContext.container.Resolve<AlbumData>().theme.ExchangeList;
for (int i = 0; i < rewards.Count; i++)
{
AlbumExchange albumExchange = GContext.container.Resolve<Tables>().GetAlbumExchange(exchangeIdList[i]);
exhangeList.Add(albumExchange);
bntStates[i].SetData(exhangeList[i]);
rewards[i].SetData(albumExchange.ItemID);
}
btn_close.onClick.AddListener(OnClickClose);
btn_mask.onClick.AddListener(OnClickClose);
}
void OnClickClose()
{
UIManager.Instance.HideUI(UITypes.AlbumExchangePopupPanel);
}
private void OnEnable()
{
int count = GContext.container.Resolve<AlbumData>().GetPictureRepeatCount();
text_repetitive_card.text = LocalizationMgr.GetFormatTextValue("UI_AlbumExchangePopupPanel_101002", count);
for (int i = 0; i < bntStates.Count; i++)
{
int index = i;
float endTimer = GContext.container.Resolve<AlbumData>().GetExchangeCd(index);
if (endTimer > 0.001f)
{
SetTimer(index, endTimer);
}
else
{
bntStates[index].SetTimeShow(0);
}
}
}
private void OnDisable()
{
foreach (var t in timers)
{
t.Cancel();
}
timers.Clear();
}
//打开兑换弹窗
public void OpenExchangePopupPanel(int index)
{
selectPicturePopupPanel.OpenExchangePopupPanel(index);
}
public void SetTimer(int index, float cd)
{
bntStates[index].SetTimeShow(cd);
Timer timer = this.AttachTimer(cd, () =>
{
GContext.container.Resolve<AlbumData>().RemoveExchangeCd(index);
bntStates[index].SetTimeShow(0);
},
(elapsed) => { bntStates[index].SetTimeText(cd - elapsed); }, useRealTime: true);
timers.Add(timer);
}
class BntState
{
//是否可以兑换
private bool isCanExchange;
public int index;
public TMP_Text text_time;
public Button btn_exchange;
public Button btn_exchange_cd;
public GameObject exchange_cd;
public TMP_Text text_num;
public TMP_Text text_num1;
public Action<int> openExchangePopupPanel;
private float exchangeTime;
public void SetButtonClick()
{
btn_exchange.onClick.AddListener(() =>
{
//打开兑换弹窗
openExchangePopupPanel?.Invoke(index - 1);
});
btn_exchange_cd.onClick.AddListener(() =>
{
if (exchangeTime > 0.001)
{
ToastPanel.Show(LocalizationMgr.GetFormatTextValue("UI_ToastPanel_6",text_time.text));
}
else
{
//打开兑换弹窗
ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_7"));
}
});
}
public void SetData(AlbumExchange albumExchange)
{
isCanExchange = albumExchange.PictureRequired <= GContext.container.Resolve<AlbumData>().GetPictureRepeatCount();
text_num.text = albumExchange.PictureRequired.ToString();
text_num1.text = albumExchange.PictureRequired.ToString();
SetBtnShow();
}
public void SetTimeShow(float _exchangeTime)
{
exchangeTime = _exchangeTime;
if (exchangeTime > 0.001f)
{
text_time.gameObject.SetActive(true);
SetTimeText(exchangeTime);
}
else
{
text_time.gameObject.SetActive(false);
}
SetBtnShow();
}
void SetBtnShow()
{
btn_exchange.gameObject.SetActive(isCanExchange && exchangeTime < 0.001);
exchange_cd.gameObject.SetActive(!isCanExchange || exchangeTime > 0.001);
}
public void SetTimeText(float _exchangeTime)
{
exchangeTime = _exchangeTime;
TimeSpan now = TimeSpan.FromSeconds(exchangeTime);
text_time.text = ConvertTools.ConvertTime2(now.Days, now.Hours, now.Minutes, now.Seconds);
//text_time.text = ConvertTools.ConvertTime((int)exchangeTime);
}
}
}