109 lines
2.9 KiB
C#
109 lines
2.9 KiB
C#
using System.Collections.Generic;
|
|
using SepCore.Event;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace SepCore.UI
|
|
{
|
|
public partial class LevelUpForm : UGuiForm
|
|
{
|
|
private readonly List<LevelUpRewardItem> _rewardItems = new List<LevelUpRewardItem>();
|
|
|
|
private LevelUpContext _context;
|
|
|
|
private bool _listenersBound;
|
|
|
|
public void RefreshUI(LevelUpContext context)
|
|
{
|
|
EnsureButtonListeners();
|
|
|
|
_context = context;
|
|
|
|
if (View.levelUpRewardTemplate.gameObject.activeSelf)
|
|
{
|
|
View.levelUpRewardTemplate.gameObject.SetActive(false);
|
|
}
|
|
|
|
int count = _context.Props?.Count ?? 0;
|
|
EnsureRewardItemCount(count);
|
|
|
|
for (int i = 0; i < _rewardItems.Count; i++)
|
|
{
|
|
LevelUpRewardItem item = _rewardItems[i];
|
|
if (i < count)
|
|
{
|
|
item.Init(_context.Props[i]);
|
|
item.gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
item.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
// 刷新 -20 <sprite name="coin" index=0>
|
|
View.refreshCostFormatText.Set(context.RefreshPrice);
|
|
}
|
|
|
|
#region Lifecycle
|
|
|
|
protected override void OnOpen(object userData)
|
|
{
|
|
base.OnOpen(userData);
|
|
|
|
if (userData is not LevelUpContext context)
|
|
{
|
|
Log.Error("LevelUpForm requires LevelUpContext as userData.");
|
|
return;
|
|
}
|
|
|
|
RefreshUI(context);
|
|
}
|
|
|
|
protected override void OnClose(bool isShutdown, object userData)
|
|
{
|
|
_context = null;
|
|
base.OnClose(isShutdown, userData);
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void EnsureButtonListeners()
|
|
{
|
|
if (_listenersBound)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_listenersBound = true;
|
|
|
|
View.refreshButton.onClick.AddListener(OnRefreshButtonClick);
|
|
}
|
|
|
|
private void EnsureRewardItemCount(int count)
|
|
{
|
|
while (_rewardItems.Count < count)
|
|
{
|
|
int index = _rewardItems.Count;
|
|
LevelUpRewardItem item = Instantiate(View.levelUpRewardTemplate, View.rewardAreaRoot);
|
|
CommonButton button = item.GetComponentInChildren<CommonButton>();
|
|
if (button != null)
|
|
{
|
|
button.onClick.AddListener(() => OnSelectButtonClick(index));
|
|
}
|
|
|
|
_rewardItems.Add(item);
|
|
}
|
|
}
|
|
|
|
public void OnSelectButtonClick(int index)
|
|
{
|
|
GameEntry.Event.Fire(this, LevelUpPropSelectedEventArgs.Create(index));
|
|
}
|
|
|
|
public void OnRefreshButtonClick()
|
|
{
|
|
GameEntry.Event.Fire(this, LevelUpRefreshEventArgs.Create(_context.RefreshPrice));
|
|
}
|
|
}
|
|
}
|