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

143 lines
5.1 KiB
C#

using asap.core;
using UnityEngine;
using System.Collections.Generic;
using GameCore;
using UnityEngine.UI;
public class DeferredRewardStashPanel : MonoBehaviour
{
private IDeferredRewardStashService _stash;
private const int RowWidth = 4;
[SerializeField]
private GameObject emptyGo, itemContainerGo, adaptiveContainer, scrollViewContainer, rowGo, rowContainer;
[SerializeField] private List<GameObject> adaptiveItemGoList, scrollViewRowList;
[SerializeField] private PanelScroll jiahaoScrollView;
[SerializeField] private Button btnClose;
private const float RewardPopSpeed = 0.075f;
public void Awake()
{
btnClose.onClick.AddListener(() => gameObject.SetActive(false));
_stash = GContext.container.Resolve<IDeferredRewardStashService>();
if (_stash == null)
{
Debug.LogError("Stash Service is not available.");
gameObject.SetActive(false);
return;
}
itemContainerGo.SetActive(false);
adaptiveContainer.SetActive(false);
scrollViewContainer.SetActive(false);
adaptiveItemGoList = new List<GameObject>();
scrollViewRowList = new List<GameObject>();
}
private const int ItemCountLimit = 12;
public void OnEnable()
{
GContext.Publish(new EventStashPanelOpen());
foreach (var itemGo in adaptiveItemGoList)
Destroy(itemGo);
adaptiveItemGoList.Clear();
foreach (var row in scrollViewRowList)
Destroy(row);
scrollViewRowList.Clear();
if (_stash.IsEmpty)
{
adaptiveContainer.SetActive(true);
emptyGo.SetActive(true);
scrollViewContainer.SetActive(false);
return;
}
emptyGo.SetActive(false);
var items = _stash.Items;
if (items.Count <= ItemCountLimit)
ShowAdaptiveLayout(items);
else
_ = ShowScrollViewLayout(items);
}
private void ShowAdaptiveLayout(IReadOnlyList<ItemData> items)
{
adaptiveContainer.SetActive(true);
scrollViewContainer.SetActive(false);
foreach (var item in items)
{
var newItemGo = Instantiate(itemContainerGo, adaptiveContainer.transform);
adaptiveItemGoList.Add(newItemGo);
var reward = newItemGo.transform.Find("reward").GetComponent<RewardItemNew>();
reward.SetData(item, abbr: true);
newItemGo.SetActive(true);
}
}
private async System.Threading.Tasks.Task ShowScrollViewLayout(IReadOnlyList<ItemData> items)
{
adaptiveContainer.SetActive(false);
scrollViewContainer.SetActive(true);
for (int i = 0; i < items.Count; i += RowWidth)
{
var currentRow = Instantiate(rowGo, rowContainer.transform);
currentRow.SetActive(true);
// TODO: Is rows needed to destroy the instantiated game objects?
scrollViewRowList.Add(currentRow.gameObject);
var count = items.Count - i;
for (int j = 0; j < RowWidth; j++)
{
currentRow.transform.GetChild(j).gameObject.SetActive(j < count);
currentRow.transform.GetChild(j).GetChild(0).gameObject.SetActive(false);
}
await Awaiters.NextFrame;
// jiahaoScrollView.DONormalizedPos(Vector2.zero, RewardPopSpeed);
for (int j = 0; j < RowWidth; j++)
{
if (j < count)
{
var item = items[i + j];
currentRow.transform.GetChild(j).gameObject.SetActive(true);
var reward = currentRow.transform.GetChild(j).GetChild(0).GetComponent<RewardItemNew>();
// reward.SetRewardPopupData(item);
reward.SetData(item, abbr: true);
reward.gameObject.SetActive(true);
// reward.PlayOpen();
cfg.Item itemCfg = GContext.container.Resolve<cfg.Tables>().TbItem.GetOrDefault(item.id);
GContext.Publish(new ResAddEvent(itemCfg.ID, itemCfg.Type, itemCfg.SubType));
}
}
}
await Awaiters.NextFrame;
// jiahaoScrollView.DONormalizedPos(Vector2.zero, RewardPopSpeed);
}
private void OnDisable()
{
GContext.Publish(new EventStashPanelClose());
}
public class EventStashPanelOpen
{
}
public class EventStashPanelClose
{
}
private void Reset()
{
emptyGo = transform.Find("root/Content1/empty").gameObject;
itemContainerGo = transform.Find("root/Content1/item1").gameObject;
adaptiveContainer = transform.Find("root/Content1").gameObject;
scrollViewContainer = transform.Find("root/Content2").gameObject;
rowGo = transform.Find("root/Content2/ScrollView/Viewport/Content/reward_group1").gameObject;
rowContainer = transform.Find("root/Content2/ScrollView/Viewport/Content").gameObject;
jiahaoScrollView = transform.Find("root/Content2/ScrollView").GetComponent<PanelScroll>();
btnClose = transform.Find("root/btn_close").GetComponent<Button>();
}
}