继续重构调整(UI)
- 引入 UniTask 和异步版本的 UIModule - 调整 Runtime/UIBase 和 Presentation 下的脚本命名空间到 SepCore.UI - 将原来的 5 层 UI 迁移到异步实现 - 规范各层的 UI 脚本命名
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class LevelUpContext : UIContext
|
||||
{
|
||||
public List<LevelUpRewardItemContext> Props;
|
||||
public int RefreshPrice;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1874c6c7ef990c04ba726a2581cd23ce
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,177 @@
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.Event;
|
||||
using SepCore.Definition;
|
||||
using CustomUtility;
|
||||
using GameFramework.Event;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class LevelUpController : UIControllerBase<LevelUpContext, LevelUpForm>
|
||||
{
|
||||
private LevelUpUseCase _useCase;
|
||||
|
||||
protected override UIFormType UIFormType => UIFormType.LevelUpForm;
|
||||
|
||||
protected override void RefreshUI(LevelUpForm form, LevelUpContext context)
|
||||
{
|
||||
form.RefreshUI(context);
|
||||
}
|
||||
|
||||
protected override void SubscribeCustomEvents()
|
||||
{
|
||||
GameEntry.Event.Subscribe(RefreshEventArgs.EventId, OnRefresh);
|
||||
GameEntry.Event.Subscribe(LevelUpPropSelectedEventArgs.EventId, OnLevelUpPropSelected);
|
||||
}
|
||||
|
||||
protected override void UnsubscribeCustomEvents()
|
||||
{
|
||||
GameEntry.Event.Unsubscribe(RefreshEventArgs.EventId, OnRefresh);
|
||||
GameEntry.Event.Unsubscribe(LevelUpPropSelectedEventArgs.EventId, OnLevelUpPropSelected);
|
||||
}
|
||||
|
||||
private static LevelUpContext BuildContext(LevelUpRawData rawData)
|
||||
{
|
||||
if (rawData == null)
|
||||
{
|
||||
Log.Error("LevelUpFormController.BuildContext() rawData is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (rawData.Rewards == null)
|
||||
{
|
||||
Log.Error("LevelUpFormController.BuildContext() rewards are null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<LevelUpRewardItemContext> props = new List<LevelUpRewardItemContext>(rawData.Rewards.Count);
|
||||
foreach (var reward in rawData.Rewards)
|
||||
{
|
||||
if (reward == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
props.Add(new LevelUpRewardItemContext
|
||||
{
|
||||
Title = reward.Title,
|
||||
Icon = null,
|
||||
ItemRarity = reward.Rarity,
|
||||
Description = ItemDescUtility.CreatePropDescription(reward.Modifiers),
|
||||
IconAssetName = reward.IconAssetName
|
||||
});
|
||||
}
|
||||
|
||||
return new LevelUpContext
|
||||
{
|
||||
RefreshPrice = rawData.RefreshPrice,
|
||||
Props = props
|
||||
};
|
||||
}
|
||||
|
||||
public override async UniTask<int?> OpenUIAsync(object userData = null, float timeout = 30f)
|
||||
{
|
||||
if (userData is LevelUpRawData rawData)
|
||||
{
|
||||
return await OpenUIAsync(rawData, timeout);
|
||||
}
|
||||
|
||||
if (userData != null)
|
||||
{
|
||||
Log.Warning("LevelUpController.OpenUIAsync() userData type is invalid.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_useCase == null)
|
||||
{
|
||||
Log.Error("LevelUpController.OpenUIAsync() useCase is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
LevelUpRawData initialRawData = _useCase.CreateInitialModel();
|
||||
return await OpenUIAsync(initialRawData, timeout);
|
||||
}
|
||||
|
||||
public async UniTask<int?> OpenUIAsync(LevelUpRawData rawData, float timeout = 30f)
|
||||
{
|
||||
LevelUpContext context = BuildContext(rawData);
|
||||
return await OpenFormAsync(context, timeout);
|
||||
}
|
||||
|
||||
public override void BindUseCase(IUIUseCase useCase)
|
||||
{
|
||||
if (useCase is not LevelUpUseCase levelUpFormUseCase)
|
||||
{
|
||||
Log.Error("LevelUpForm.BindUseCase() useCase is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
_useCase = levelUpFormUseCase;
|
||||
}
|
||||
|
||||
private async UniTask SelectRewardAsync(int selectedIndex)
|
||||
{
|
||||
if (_useCase == null)
|
||||
{
|
||||
Log.Error("LevelUpFormController.SelectRewardAsync() useCase is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
LevelUpRawData rawData = _useCase.SelectReward(selectedIndex);
|
||||
if (rawData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await OpenUIAsync(rawData);
|
||||
}
|
||||
|
||||
private async UniTask RefreshRewardListAsync(int refreshCost)
|
||||
{
|
||||
if (_useCase == null)
|
||||
{
|
||||
Log.Error("LevelUpFormController.RefreshRewardListAsync() useCase is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
LevelUpRawData rawData = _useCase.TryRefresh(refreshCost);
|
||||
if (rawData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await OpenUIAsync(rawData);
|
||||
}
|
||||
|
||||
private void OnRefresh(object sender, GameEventArgs e)
|
||||
{
|
||||
if (sender is not LevelUpForm levelUpForm || levelUpForm != Form)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (e is not RefreshEventArgs args)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshRewardListAsync(args.Cost).Forget();
|
||||
}
|
||||
|
||||
private void OnLevelUpPropSelected(object sender, GameEventArgs e)
|
||||
{
|
||||
if (sender is not LevelUpForm levelUpForm || levelUpForm != Form)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (e is not LevelUpPropSelectedEventArgs args)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SelectRewardAsync(args.SelectedId).Forget();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 89d85bbc4f7cf8c438c7ee693eaf67cf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,70 @@
|
||||
using SepCore.Event;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class LevelUpForm : UGuiForm
|
||||
{
|
||||
[SerializeField] private LevelUpRewardItem[] _propItems;
|
||||
|
||||
[SerializeField] private TMP_Text _refreshButtonText;
|
||||
|
||||
private LevelUpContext _context;
|
||||
|
||||
public void RefreshUI(LevelUpContext context)
|
||||
{
|
||||
_context = context;
|
||||
|
||||
foreach (var propItem in _propItems)
|
||||
{
|
||||
propItem.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
if (_context.Props == null) return;
|
||||
int count = Mathf.Min(_propItems.Length, _context.Props.Count);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
_propItems[i].gameObject.SetActive(true);
|
||||
_propItems[i].Init(_context.Props[i]);
|
||||
}
|
||||
|
||||
//刷新 -20 <sprite name="coin" index=0>
|
||||
_refreshButtonText.text = $"刷新 -{context.RefreshPrice} <sprite name=\"coin\" index=0>";
|
||||
}
|
||||
|
||||
#region FSM
|
||||
|
||||
protected override void OnOpen(object userData)
|
||||
{
|
||||
base.OnOpen(userData);
|
||||
|
||||
if (userData is LevelUpContext context)
|
||||
{
|
||||
RefreshUI(context);
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Warning("LevelUpForm requires LevelUpFormContext as userData.");
|
||||
}
|
||||
|
||||
protected override void OnClose(bool isShutdown, object userData)
|
||||
{
|
||||
_context = null;
|
||||
base.OnClose(isShutdown, userData);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void OnSelectButtonClick(int index)
|
||||
{
|
||||
GameEntry.Event.Fire(this, LevelUpPropSelectedEventArgs.Create(index));
|
||||
}
|
||||
|
||||
public void OnRefreshButtonClick()
|
||||
{
|
||||
GameEntry.Event.Fire(this, RefreshEventArgs.Create(_context.RefreshPrice));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73f52c8a6d8b8ec42ad8cd3c00e0254d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user