94 lines
2.3 KiB
C#
94 lines
2.3 KiB
C#
using GeometryTD.CustomEvent;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class ShopForm : UGuiForm
|
|
{
|
|
[SerializeField] private TMP_Text _goldText;
|
|
|
|
[SerializeField] private GoodsItem[] _goldItems;
|
|
|
|
private ShopFormContext _context;
|
|
|
|
public void RefreshUI(ShopFormContext context)
|
|
{
|
|
_context = context;
|
|
if (_goldText != null)
|
|
{
|
|
_goldText.text = context?.GoldText ?? string.Empty;
|
|
}
|
|
|
|
RefreshGoodsItems(context?.GoodsItems);
|
|
}
|
|
|
|
public void OnInventoryButtonClick()
|
|
{
|
|
GameEntry.Event.Fire(this, ShopInventoryRequestedEventArgs.Create());
|
|
}
|
|
|
|
public void OnExitButtonClick()
|
|
{
|
|
GameEntry.Event.Fire(this, ShopExitRequestedEventArgs.Create());
|
|
}
|
|
|
|
protected override void OnOpen(object userData)
|
|
{
|
|
base.OnOpen(userData);
|
|
if (userData is ShopFormContext context)
|
|
{
|
|
RefreshUI(context);
|
|
return;
|
|
}
|
|
|
|
Log.Warning("ShopForm requires ShopFormContext as userData.");
|
|
}
|
|
|
|
protected override void OnClose(bool isShutdown, object userData)
|
|
{
|
|
_context = null;
|
|
if (_goldText != null)
|
|
{
|
|
_goldText.text = string.Empty;
|
|
}
|
|
|
|
if (_goldItems != null)
|
|
{
|
|
foreach (var item in _goldItems)
|
|
{
|
|
item?.OnReset();
|
|
}
|
|
}
|
|
|
|
base.OnClose(isShutdown, userData);
|
|
}
|
|
|
|
private void RefreshGoodsItems(GoodsItemContext[] goodsItems)
|
|
{
|
|
if (_goldItems == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < _goldItems.Length; i++)
|
|
{
|
|
GoodsItem goodsItem = _goldItems[i];
|
|
if (goodsItem == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (goodsItems != null && i < goodsItems.Length && goodsItems[i] != null)
|
|
{
|
|
goodsItem.OnInit(goodsItems[i]);
|
|
}
|
|
else
|
|
{
|
|
goodsItem.OnReset();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |