迁移剩下的 UI 到 Serialization 方式
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,167 @@
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.Event;
|
||||
using SepCore.Definition;
|
||||
using SepCore.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(LevelUpRefreshEventArgs.EventId, LevelUpRefresh);
|
||||
GameEntry.Event.Subscribe(LevelUpPropSelectedEventArgs.EventId, LevelUpPropSelected);
|
||||
}
|
||||
|
||||
protected override void UnsubscribeCustomEvents()
|
||||
{
|
||||
GameEntry.Event.Unsubscribe(LevelUpRefreshEventArgs.EventId, LevelUpRefresh);
|
||||
GameEntry.Event.Unsubscribe(LevelUpPropSelectedEventArgs.EventId, LevelUpPropSelected);
|
||||
}
|
||||
|
||||
private static LevelUpContext BuildContext(LevelUpRawData rawData)
|
||||
{
|
||||
if (rawData == null)
|
||||
{
|
||||
Log.Error("LevelUpFormController.BuildContext() rawData is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<LevelUpRewardItemContext> props = new List<LevelUpRewardItemContext>(rawData.Rewards.Count);
|
||||
foreach (var reward in rawData.Rewards)
|
||||
{
|
||||
props.Add(new LevelUpRewardItemContext
|
||||
{
|
||||
Title = reward.Title,
|
||||
Icon = null,
|
||||
Rarity = reward.Rarity,
|
||||
Description = ItemDescUtility.CreatePropDescription(reward.Modifiers),
|
||||
IconAssetName = reward.Sprite
|
||||
});
|
||||
}
|
||||
|
||||
return new LevelUpContext
|
||||
{
|
||||
RefreshPrice = rawData.RefreshPrice,
|
||||
Props = props
|
||||
};
|
||||
}
|
||||
|
||||
public override async UniTask OpenUIAsync(object userData = null, float timeout = 30f)
|
||||
{
|
||||
if (userData is LevelUpRawData rawData)
|
||||
{
|
||||
await OpenUIAsync(rawData, timeout);
|
||||
return;
|
||||
}
|
||||
|
||||
if (userData != null)
|
||||
{
|
||||
Log.Error("LevelUpController.OpenUIAsync() userData type is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_useCase == null)
|
||||
{
|
||||
Log.Error("LevelUpController.OpenUIAsync() useCase is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
LevelUpRawData initialRawData = _useCase.CreateInitialModel();
|
||||
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 LevelUpRefresh(object sender, GameEventArgs e)
|
||||
{
|
||||
if (sender is not LevelUpForm levelUpForm || levelUpForm != Form)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (e is not LevelUpRefreshEventArgs args)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshRewardListAsync(args.Cost).Forget();
|
||||
}
|
||||
|
||||
private void LevelUpPropSelected(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,108 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73f52c8a6d8b8ec42ad8cd3c00e0254d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,13 @@
|
||||
// <auto-generated />
|
||||
using UnityEngine;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
public partial class LevelUpForm : UGuiForm
|
||||
{
|
||||
[SerializeField] private LevelUpView _view = null;
|
||||
|
||||
public LevelUpView View => _view;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3172a8000d5463f45b2f4c5f5a19433d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f0b958b02844f940982990f3922c912
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,48 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.AsyncTask;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class LevelUpRewardItem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private IconArea _iconArea;
|
||||
|
||||
[SerializeField] private TMP_Text _titleText;
|
||||
|
||||
[SerializeField] private TMP_Text _typeText;
|
||||
|
||||
[SerializeField] private TMP_Text _descriptionText;
|
||||
|
||||
private LevelUpRewardItemContext _context;
|
||||
|
||||
|
||||
public void Init(LevelUpRewardItemContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
Log.Error("LevelUpPropContext context is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
_context = context;
|
||||
|
||||
_titleText.text = context.Title;
|
||||
_descriptionText.text = context.Description;
|
||||
_iconArea.OnInit(context.Icon, context.Rarity);
|
||||
|
||||
LoadIcon(_context.IconAssetName).Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid LoadIcon(string iconAssetName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(iconAssetName)) return;
|
||||
|
||||
var sprite = await SpriteLoader.LoadSpriteByKeyAsync(iconAssetName);
|
||||
_iconArea.SetIcon(sprite);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 237d64392d274c14f9f5c81b54122a93
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
using SepCore.Definition;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class LevelUpRewardItemContext : UIContext
|
||||
{
|
||||
public string Title;
|
||||
public Sprite Icon;
|
||||
public Rarity Rarity;
|
||||
public string Description;
|
||||
public string IconAssetName;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 424ceeba7cae4fa4faaeef9035f31e31
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
// <auto-generated />
|
||||
using UnityEngine;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
public partial class LevelUpView : MonoBehaviour
|
||||
{
|
||||
[SerializeField] public global::UnityEngine.RectTransform rewardAreaRoot;
|
||||
[SerializeField] public global::SepCore.UI.LevelUpRewardItem levelUpRewardTemplate;
|
||||
[SerializeField] public global::SepCore.UI.CommonButton refreshButton;
|
||||
[SerializeField] public global::SepCore.UI.FormatTextUI refreshCostFormatText;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39b4ea16f6b8d6a47b6ea7078982a7d0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user