135 lines
3.7 KiB
C#
135 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using SepCore.Event;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace SepCore.UI
|
|
{
|
|
public partial class ShopForm : UGuiForm
|
|
{
|
|
private readonly List<ShopGoodsItem> _goodsItems = new List<ShopGoodsItem>();
|
|
|
|
private ShopContext _context;
|
|
|
|
private bool _listenersBound;
|
|
|
|
public void RefreshUI(ShopContext context)
|
|
{
|
|
EnsureButtonListeners();
|
|
|
|
_context = context;
|
|
|
|
if (context.NeedRefreshGoodsItems)
|
|
{
|
|
View.titleFormatText.Set(context.CurrentLevel);
|
|
View.continueButtonFormatText.Set(context.CurrentLevel + 1);
|
|
RefreshGoodsItems(context.GoodsItems);
|
|
context.NeedRefreshGoodsItems = false;
|
|
}
|
|
|
|
if (context.NeedRefreshPrice)
|
|
{
|
|
View.refreshButtonFormatText.Set(context.RefreshPrice);
|
|
context.NeedRefreshPrice = false;
|
|
}
|
|
|
|
if (context.NeedRefreshPlayerCoin)
|
|
{
|
|
View.playerCoinFormatText.Set(context.PlayerCoin);
|
|
context.NeedRefreshPlayerCoin = false;
|
|
}
|
|
|
|
if (context.NeedRefreshPropList)
|
|
{
|
|
View.propList.OnInit(context.PropListContext);
|
|
context.NeedRefreshPropList = false;
|
|
}
|
|
|
|
if (context.NeedRefreshWeaponList)
|
|
{
|
|
View.weaponList.OnInit(context.WeaponListContext);
|
|
context.NeedRefreshWeaponList = false;
|
|
}
|
|
}
|
|
|
|
protected override void OnOpen(object userData)
|
|
{
|
|
base.OnOpen(userData);
|
|
|
|
if (userData is not ShopContext context)
|
|
{
|
|
Log.Error("ShopForm requires ShopContext as userData.");
|
|
return;
|
|
}
|
|
|
|
RefreshUI(context);
|
|
}
|
|
|
|
protected override void OnClose(bool isShutdown, object userData)
|
|
{
|
|
_context = null;
|
|
|
|
for (int i = 0; i < _goodsItems.Count; i++)
|
|
{
|
|
_goodsItems[i].OnReset();
|
|
}
|
|
|
|
View.propList.OnReset();
|
|
View.weaponList.OnReset();
|
|
|
|
base.OnClose(isShutdown, userData);
|
|
}
|
|
|
|
private void EnsureButtonListeners()
|
|
{
|
|
if (_listenersBound)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_listenersBound = true;
|
|
|
|
View.continueButton.onClick.AddListener(OnContinueButtonClick);
|
|
View.refreshButton.onClick.AddListener(OnRefreshButtonClick);
|
|
}
|
|
|
|
private void RefreshGoodsItems(List<GoodsItemContext> goodsItems)
|
|
{
|
|
EnsureGoodsItemCount(goodsItems.Count);
|
|
|
|
for (int i = 0; i < _goodsItems.Count; i++)
|
|
{
|
|
ShopGoodsItem item = _goodsItems[i];
|
|
if (i < goodsItems.Count && goodsItems[i] != null)
|
|
{
|
|
item.Init(goodsItems[i], i);
|
|
item.gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
item.OnReset();
|
|
item.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void EnsureGoodsItemCount(int count)
|
|
{
|
|
while (_goodsItems.Count < count)
|
|
{
|
|
ShopGoodsItem item = Instantiate(View.goodsItemTemplate, View.GoodsItemRoot);
|
|
_goodsItems.Add(item);
|
|
}
|
|
}
|
|
|
|
public void OnContinueButtonClick()
|
|
{
|
|
GameEntry.Event.Fire(this, ShopContinueEventArgs.Create());
|
|
}
|
|
|
|
public void OnRefreshButtonClick()
|
|
{
|
|
GameEntry.Event.Fire(this, ShopRefreshEventArgs.Create(_context.RefreshPrice));
|
|
}
|
|
}
|
|
}
|