- 规范 UI 设计成为:
- UIForm 负责展示
- Controller 负责控制
- UseCase 负责业务逻辑
- RawData 负责提供原始数据
- Context 负责为 UIForm 提供数据
- 添加和补充一系列 UI 功能
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
using Definition.Enum;
|
||||
using GameFramework.Event;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class DisplayItemInfoFormController : UIFormControllerBase<DisplayItemInfoFormContext>
|
||||
{
|
||||
private DisplayItemInfoFormContext _context;
|
||||
|
||||
private DisplayItemInfoForm _itemInfoForm;
|
||||
|
||||
private int? _itemInfoFormSerialId;
|
||||
|
||||
private bool _pendingRefresh;
|
||||
|
||||
private bool _isBindEvent = false;
|
||||
|
||||
private void SubscribeEvents()
|
||||
{
|
||||
if (_isBindEvent) return;
|
||||
|
||||
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
|
||||
_isBindEvent = true;
|
||||
}
|
||||
|
||||
private void UnsubscribeEvents()
|
||||
{
|
||||
if (!_isBindEvent) return;
|
||||
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
|
||||
_isBindEvent = false;
|
||||
}
|
||||
|
||||
private static DisplayItemInfoFormContext BuildContext(DisplayItemInfoFormRawData rawData)
|
||||
{
|
||||
if (rawData == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new DisplayItemInfoFormContext
|
||||
{
|
||||
IconAssetName = rawData.IconAssetName,
|
||||
Title = rawData.Title,
|
||||
Rarity = rawData.Rarity,
|
||||
TypeText = rawData.TypeText,
|
||||
Description = rawData.Description,
|
||||
Price = rawData.Price,
|
||||
IsWeapon = rawData.IsWeapon,
|
||||
TargetPos = rawData.TargetPos
|
||||
};
|
||||
}
|
||||
|
||||
#region UI Methods
|
||||
|
||||
protected override int? OpenUIInternal(DisplayItemInfoFormContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
Log.Warning("ItemInfoFormController open failed. context is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
_context = context;
|
||||
|
||||
if (_itemInfoForm != null && _itemInfoFormSerialId.HasValue &&
|
||||
GameEntry.UI.HasUIForm(_itemInfoFormSerialId.Value))
|
||||
{
|
||||
_itemInfoForm.RefreshUI(_context);
|
||||
return _itemInfoFormSerialId;
|
||||
}
|
||||
|
||||
CloseUI();
|
||||
_pendingRefresh = true;
|
||||
SubscribeEvents();
|
||||
_itemInfoFormSerialId = GameEntry.UI.OpenUIForm(UIFormType.DisplayItemInfoForm, context);
|
||||
return _itemInfoFormSerialId;
|
||||
}
|
||||
|
||||
public int? OpenUI(DisplayItemInfoFormRawData rawData)
|
||||
{
|
||||
DisplayItemInfoFormContext context = BuildContext(rawData);
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
public override int? OpenUI(object userData = null)
|
||||
{
|
||||
if (userData is DisplayItemInfoFormContext context)
|
||||
{
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
if (userData is DisplayItemInfoFormRawData rawData)
|
||||
{
|
||||
return OpenUI(rawData);
|
||||
}
|
||||
|
||||
if (userData != null)
|
||||
{
|
||||
Log.Warning("DisplayItemInfoFormController.OpenUI() userData type is invalid.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return OpenUIInternal(_context);
|
||||
}
|
||||
|
||||
public override void CloseUI()
|
||||
{
|
||||
_pendingRefresh = false;
|
||||
|
||||
UnsubscribeEvents();
|
||||
|
||||
if (_itemInfoFormSerialId.HasValue)
|
||||
{
|
||||
if (GameEntry.UI.HasUIForm(_itemInfoFormSerialId.Value))
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(_itemInfoFormSerialId.Value);
|
||||
}
|
||||
|
||||
_itemInfoForm = null;
|
||||
_itemInfoFormSerialId = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_itemInfoForm != null)
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(_itemInfoForm);
|
||||
_itemInfoForm = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override void BindUseCase(IUIUseCase useCase)
|
||||
{
|
||||
if (!(useCase is DisplayItemInfoFormUseCase))
|
||||
{
|
||||
Log.Error("DisplayItemInfoForm.BindUseCase() useCase is invalid.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void TryRefreshUI()
|
||||
{
|
||||
if (_context == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_itemInfoForm == null)
|
||||
{
|
||||
_pendingRefresh = true;
|
||||
return;
|
||||
}
|
||||
|
||||
_itemInfoForm.RefreshUI(_context);
|
||||
_pendingRefresh = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region EventHanlders
|
||||
|
||||
private void OpenUIFormSuccess(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is OpenUIFormSuccessEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_itemInfoFormSerialId.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.UIForm == null || args.UIForm.SerialId != _itemInfoFormSerialId.Value || args.UserData != _context)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_itemInfoForm = args.UIForm.Logic as DisplayItemInfoForm;
|
||||
|
||||
if (_itemInfoForm == null)
|
||||
{
|
||||
Log.Warning("DisplayItemInfoFormController open success but form logic is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_pendingRefresh)
|
||||
{
|
||||
TryRefreshUI();
|
||||
}
|
||||
}
|
||||
|
||||
private void CloseUIFormComplete(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is CloseUIFormCompleteEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.SerialId != _itemInfoFormSerialId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_itemInfoForm = null;
|
||||
_itemInfoFormSerialId = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 692b282b1ecbf5a44839d5bf4a03a937
|
||||
guid: 8a9b5acbc476ae44a8259a5aea1c0cf2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -0,0 +1,192 @@
|
||||
using Definition.Enum;
|
||||
using GameFramework.Event;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class HudFormController : UIFormControllerBase<HudFormContext>
|
||||
{
|
||||
private HudFormContext _context;
|
||||
|
||||
private HudForm _hudForm;
|
||||
|
||||
private int? _hudFormSerialId;
|
||||
|
||||
private bool _pendingRefresh;
|
||||
|
||||
private bool _isBindEvent;
|
||||
|
||||
private void SubscribeEvents()
|
||||
{
|
||||
if (_isBindEvent) return;
|
||||
|
||||
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
|
||||
_isBindEvent = true;
|
||||
}
|
||||
|
||||
private void UnsubscribeEvents()
|
||||
{
|
||||
if (!_isBindEvent) return;
|
||||
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
|
||||
_isBindEvent = false;
|
||||
}
|
||||
|
||||
private static HudFormContext BuildHudFormContext()
|
||||
{
|
||||
return new HudFormContext();
|
||||
}
|
||||
|
||||
#region UI Methods
|
||||
|
||||
protected override int? OpenUIInternal(HudFormContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
Log.Warning("HudFormController.OpenUI() context is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
_context = context;
|
||||
|
||||
if (_hudForm != null && _hudFormSerialId.HasValue &&
|
||||
GameEntry.UI.HasUIForm(_hudFormSerialId.Value))
|
||||
{
|
||||
_hudForm.RefreshUI(_context);
|
||||
return _hudFormSerialId;
|
||||
}
|
||||
|
||||
CloseUI();
|
||||
_pendingRefresh = true;
|
||||
SubscribeEvents();
|
||||
_hudFormSerialId = GameEntry.UI.OpenUIForm(UIFormType.HudForm, context);
|
||||
return _hudFormSerialId;
|
||||
}
|
||||
|
||||
public override int? OpenUI(object userData = null)
|
||||
{
|
||||
if (userData is HudFormContext context)
|
||||
{
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
if (userData != null)
|
||||
{
|
||||
Log.Warning("HudFormController.OpenUI() userData type is invalid.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return OpenUIInternal(BuildHudFormContext());
|
||||
}
|
||||
|
||||
public int? OpenUI(HudFormContext context)
|
||||
{
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
public override void CloseUI()
|
||||
{
|
||||
_pendingRefresh = false;
|
||||
UnsubscribeEvents();
|
||||
|
||||
if (_hudFormSerialId.HasValue)
|
||||
{
|
||||
if (GameEntry.UI.HasUIForm(_hudFormSerialId.Value))
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(_hudFormSerialId.Value);
|
||||
}
|
||||
|
||||
_hudForm = null;
|
||||
_hudFormSerialId = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_hudForm != null)
|
||||
{
|
||||
_hudForm.Close();
|
||||
_hudForm = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override void BindUseCase(IUIUseCase useCase)
|
||||
{
|
||||
Log.Info("HudFormController doesn't need UseCase");
|
||||
}
|
||||
|
||||
private void TryRefreshUI()
|
||||
{
|
||||
if (_context == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_hudForm == null)
|
||||
{
|
||||
_pendingRefresh = true;
|
||||
return;
|
||||
}
|
||||
|
||||
_hudForm.RefreshUI(_context);
|
||||
_pendingRefresh = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
private void OpenUIFormSuccess(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is OpenUIFormSuccessEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_hudFormSerialId.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.UIForm == null || args.UIForm.SerialId != _hudFormSerialId.Value ||
|
||||
args.UserData != _context)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_hudForm = args.UIForm.Logic as HudForm;
|
||||
|
||||
if (_hudForm == null)
|
||||
{
|
||||
Log.Warning("HudFormController open success but form logic is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_pendingRefresh)
|
||||
{
|
||||
TryRefreshUI();
|
||||
}
|
||||
}
|
||||
|
||||
private void CloseUIFormComplete(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is CloseUIFormCompleteEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.SerialId != _hudFormSerialId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_hudForm = null;
|
||||
_hudFormSerialId = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b7360b01093fa984d86105e2dd024dd5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace UI
|
||||
{
|
||||
public interface IFormController<TContext>
|
||||
{
|
||||
int? OpenUI(TContext context);
|
||||
void CloseUI();
|
||||
}
|
||||
}
|
||||
@@ -1,44 +1,81 @@
|
||||
using System.Collections.Generic;
|
||||
using CustomEvent;
|
||||
using DataTable;
|
||||
using Definition.Enum;
|
||||
using GameFramework.DataTable;
|
||||
using Game.Utility;
|
||||
using GameFramework.Event;
|
||||
using UnityEngine;
|
||||
using Procedure;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LevelUpFormController : IFormController<LevelUpFormContext>
|
||||
public class LevelUpFormController : UIFormControllerBase<LevelUpFormContext>
|
||||
{
|
||||
private int _currentLevel = 1;
|
||||
private int _buildCount = 4;
|
||||
private LevelUpFormUseCase _useCase;
|
||||
|
||||
private bool _pendingRefresh;
|
||||
|
||||
private int? _levelUpFormSerialId;
|
||||
|
||||
private LevelUpForm _levelUpForm;
|
||||
|
||||
private LevelUpFormContext _context;
|
||||
|
||||
public LevelUpFormController()
|
||||
private bool _isBindEvent;
|
||||
|
||||
private void SubscribeEvents()
|
||||
{
|
||||
if (_isBindEvent) return;
|
||||
|
||||
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
GameEntry.Event.Subscribe(RefreshEventArgs.EventId, OnRefresh);
|
||||
GameEntry.Event.Subscribe(LevelUpPropSelectedEventArgs.EventId, OnLevelUpPropSelected);
|
||||
|
||||
_isBindEvent = true;
|
||||
}
|
||||
|
||||
~LevelUpFormController()
|
||||
private void UnsubscribeEvents()
|
||||
{
|
||||
if (!_isBindEvent) return;
|
||||
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
GameEntry.Event.Unsubscribe(RefreshEventArgs.EventId, OnRefresh);
|
||||
GameEntry.Event.Unsubscribe(LevelUpPropSelectedEventArgs.EventId, OnLevelUpPropSelected);
|
||||
|
||||
_isBindEvent = false;
|
||||
}
|
||||
|
||||
public void ConfigureBuild(int level, int count = 4)
|
||||
private static LevelUpFormContext BuildContext(LevelUpFormRawData rawData)
|
||||
{
|
||||
_currentLevel = Mathf.Max(1, level);
|
||||
_buildCount = Mathf.Max(0, count);
|
||||
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,
|
||||
Description = ItemDescUtility.CreatePropDescription(reward.Modifiers),
|
||||
IconAssetName = reward.IconAssetName
|
||||
});
|
||||
}
|
||||
|
||||
return new LevelUpFormContext
|
||||
{
|
||||
RefreshPrice = rawData.RefreshPrice,
|
||||
Props = props
|
||||
};
|
||||
}
|
||||
|
||||
public int? OpenUI(LevelUpFormContext context)
|
||||
|
||||
#region UI Methods
|
||||
|
||||
protected override int? OpenUIInternal(LevelUpFormContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
@@ -48,7 +85,8 @@ namespace UI
|
||||
|
||||
_context = context;
|
||||
|
||||
if (_levelUpForm != null)
|
||||
if (_levelUpForm != null && _levelUpFormSerialId.HasValue &&
|
||||
GameEntry.UI.HasUIForm(_levelUpFormSerialId.Value))
|
||||
{
|
||||
_levelUpForm.RefreshUI(_context);
|
||||
return _levelUpFormSerialId;
|
||||
@@ -56,37 +94,43 @@ namespace UI
|
||||
|
||||
CloseUI();
|
||||
_pendingRefresh = true;
|
||||
SubscribeEvents();
|
||||
_levelUpFormSerialId = GameEntry.UI.OpenUIForm(UIFormType.LevelUpForm, context);
|
||||
return _levelUpFormSerialId;
|
||||
}
|
||||
|
||||
public void CloseUI()
|
||||
public override int? OpenUI(object userData = null)
|
||||
{
|
||||
_pendingRefresh = false;
|
||||
|
||||
if (_levelUpFormSerialId.HasValue)
|
||||
if (userData is LevelUpFormContext context)
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(_levelUpFormSerialId.Value);
|
||||
return;
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
if (_levelUpForm != null)
|
||||
if (userData is LevelUpFormRawData rawDataFromUserData)
|
||||
{
|
||||
_levelUpForm.Close();
|
||||
return OpenUI(rawDataFromUserData);
|
||||
}
|
||||
|
||||
if (userData != null)
|
||||
{
|
||||
Log.Warning("LevelUpFormController.OpenUI() userData type is invalid.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_useCase == null)
|
||||
{
|
||||
Log.Error("LevelUpFormController.OpenUI() useCase is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
LevelUpFormRawData rawData = _useCase.CreateInitialModel();
|
||||
return OpenUI(rawData);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
public int? OpenUI(LevelUpFormRawData rawData)
|
||||
{
|
||||
_context = null;
|
||||
_levelUpForm = null;
|
||||
_levelUpFormSerialId = null;
|
||||
_pendingRefresh = false;
|
||||
}
|
||||
|
||||
public void OnSelectProp(int propId)
|
||||
{
|
||||
|
||||
LevelUpFormContext context = BuildContext(rawData);
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
private void TryRefreshUI()
|
||||
@@ -106,6 +150,82 @@ namespace UI
|
||||
_pendingRefresh = false;
|
||||
}
|
||||
|
||||
public override void CloseUI()
|
||||
{
|
||||
_pendingRefresh = false;
|
||||
UnsubscribeEvents();
|
||||
|
||||
if (_levelUpFormSerialId.HasValue)
|
||||
{
|
||||
if (GameEntry.UI.HasUIForm(_levelUpFormSerialId.Value))
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(_levelUpFormSerialId.Value);
|
||||
}
|
||||
|
||||
_levelUpForm = null;
|
||||
_levelUpFormSerialId = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_levelUpForm != null)
|
||||
{
|
||||
_levelUpForm.Close();
|
||||
_levelUpForm = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override void BindUseCase(IUIUseCase useCase)
|
||||
{
|
||||
if (!(useCase is LevelUpFormUseCase levelUpFormUseCase))
|
||||
{
|
||||
Log.Error("LevelUpForm.BindUseCase() useCase is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
_useCase = levelUpFormUseCase;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Service
|
||||
|
||||
private void SelectReward(int selectedIndex)
|
||||
{
|
||||
if (_useCase == null)
|
||||
{
|
||||
Log.Error("LevelUpFormController.OpenUI() useCase is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
LevelUpFormRawData rawData = _useCase.SelectReward(selectedIndex);
|
||||
|
||||
if (rawData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
OpenUI(rawData);
|
||||
}
|
||||
|
||||
private void RefreshRewardList(int refreshCost)
|
||||
{
|
||||
if (_useCase == null)
|
||||
{
|
||||
Log.Error("LevelUpFormController.OpenUI() useCase is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
LevelUpFormRawData rawData = _useCase.TryRefresh(refreshCost);
|
||||
if (rawData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
OpenUI(rawData);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
private void OpenUIFormSuccess(object sender, GameEventArgs e)
|
||||
@@ -149,6 +269,31 @@ namespace UI
|
||||
_levelUpFormSerialId = null;
|
||||
}
|
||||
|
||||
private void OnRefresh(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(sender is LevelUpForm))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(e is RefreshEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshRewardList(args.Cost);
|
||||
}
|
||||
|
||||
private void OnLevelUpPropSelected(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is LevelUpPropSelectedEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SelectReward(args.SelectedId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
using System.Collections.Generic;
|
||||
using CustomEvent;
|
||||
using Definition.DataStruct;
|
||||
using Definition.Enum;
|
||||
using Entity;
|
||||
using Game.Utility;
|
||||
using GameFramework.Event;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class ShopFormController : IFormController<ShopFormContext>
|
||||
public class ShopFormController : UIFormControllerBase<ShopFormContext>
|
||||
{
|
||||
#region Property
|
||||
private ShopFormUseCase _useCase;
|
||||
|
||||
private bool _pendingRefresh;
|
||||
|
||||
@@ -14,23 +19,167 @@ namespace UI
|
||||
|
||||
private ShopForm _shopForm;
|
||||
|
||||
private ShopFormRawData _rawData;
|
||||
|
||||
private ShopFormContext _context;
|
||||
|
||||
private bool _isBindEvent;
|
||||
|
||||
private void SubscribeEvents()
|
||||
{
|
||||
if (_isBindEvent) return;
|
||||
|
||||
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
GameEntry.Event.Subscribe(RefreshEventArgs.EventId, Refresh);
|
||||
GameEntry.Event.Subscribe(ShopPurchaseEventArgs.EventId, ShopPurchase);
|
||||
GameEntry.Event.Subscribe(ShopContinueEventArgs.EventId, ShopContinue);
|
||||
GameEntry.Event.Subscribe(DisplayItemShowEventArgs.EventId, DisplayItemShow);
|
||||
GameEntry.Event.Subscribe(DisplayItemHideEventArgs.EventId, DisplayItemHide);
|
||||
|
||||
_isBindEvent = true;
|
||||
}
|
||||
|
||||
private void UnsubscribeEvents()
|
||||
{
|
||||
if (!_isBindEvent) return;
|
||||
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
GameEntry.Event.Unsubscribe(RefreshEventArgs.EventId, Refresh);
|
||||
GameEntry.Event.Unsubscribe(ShopPurchaseEventArgs.EventId, ShopPurchase);
|
||||
GameEntry.Event.Unsubscribe(ShopContinueEventArgs.EventId, ShopContinue);
|
||||
GameEntry.Event.Unsubscribe(DisplayItemShowEventArgs.EventId, DisplayItemShow);
|
||||
GameEntry.Event.Unsubscribe(DisplayItemHideEventArgs.EventId, DisplayItemHide);
|
||||
|
||||
_isBindEvent = false;
|
||||
}
|
||||
|
||||
#region BuildContext
|
||||
|
||||
private ShopFormContext BuildContext(ShopFormRawData rawData)
|
||||
{
|
||||
if (rawData == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
_rawData = rawData;
|
||||
|
||||
return new ShopFormContext
|
||||
{
|
||||
CurrentLevel = rawData.CurrentLevel,
|
||||
RefreshPrice = rawData.RefreshPrice,
|
||||
PlayerCoin = rawData.PlayerCoin,
|
||||
GoodsItems = rawData.GoodsItems,
|
||||
PropListContext = BuildDisplayListAreaContext("道具", rawData.PropItems, rawData.PropMaxCount),
|
||||
WeaponListContext = BuildDisplayListAreaContext("武器", rawData.WeaponItems, rawData.WeaponMaxCount)
|
||||
};
|
||||
}
|
||||
|
||||
private static DisplayListAreaContext BuildDisplayListAreaContext(string title, IReadOnlyList<object> items,
|
||||
int maxCount)
|
||||
{
|
||||
DisplayItemContext[] itemContexts = new DisplayItemContext[items.Count];
|
||||
if (title == "武器")
|
||||
{
|
||||
if (items is IReadOnlyList<WeaponBase> weapons)
|
||||
{
|
||||
for (int i = 0; i < weapons.Count; i++)
|
||||
{
|
||||
WeaponBase weapon = weapons[i];
|
||||
if (weapon == null) break;
|
||||
itemContexts[i] = BuildWeaponItem(weapon);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (title == "道具")
|
||||
{
|
||||
if (items is IReadOnlyList<PropItem> propItems)
|
||||
{
|
||||
for (int i = 0; i < propItems.Count; i++)
|
||||
{
|
||||
PropItem propItem = propItems[i];
|
||||
if (propItem == null) break;
|
||||
itemContexts[i] = BuildPropItem(propItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int currentCount = itemContexts.Length;
|
||||
return new DisplayListAreaContext
|
||||
{
|
||||
Title = title,
|
||||
CurrentCount = currentCount,
|
||||
MaxCount = maxCount,
|
||||
ItemContexts = itemContexts
|
||||
};
|
||||
}
|
||||
|
||||
private static DisplayItemContext BuildPropItem(PropItem propItem)
|
||||
{
|
||||
string iconAssetName = null;
|
||||
ItemRarity rarity = ItemRarity.None;
|
||||
|
||||
if (propItem != null)
|
||||
{
|
||||
iconAssetName = propItem.IconAssetName;
|
||||
rarity = propItem.Rarity;
|
||||
}
|
||||
|
||||
return new DisplayItemContext
|
||||
{
|
||||
IconAssetName = iconAssetName,
|
||||
Rarity = rarity,
|
||||
IsWeapon = false
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
private static DisplayItemContext BuildWeaponItem(WeaponBase weaponBase)
|
||||
{
|
||||
string iconAssetName = null;
|
||||
ItemRarity rarity = ItemRarity.None;
|
||||
|
||||
if (weaponBase != null && weaponBase.WeaponData != null)
|
||||
{
|
||||
iconAssetName = weaponBase.WeaponData.IconAssetName;
|
||||
rarity = weaponBase.WeaponData.Rarity;
|
||||
}
|
||||
|
||||
return new DisplayItemContext
|
||||
{
|
||||
IconAssetName = iconAssetName,
|
||||
Rarity = rarity,
|
||||
IsWeapon = true
|
||||
};
|
||||
}
|
||||
|
||||
private static void AppendDisplayItemContext(DisplayListAreaContext listContext, DisplayItemContext newItem)
|
||||
{
|
||||
if (listContext == null || newItem == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int oldCount = listContext.ItemContexts != null ? listContext.ItemContexts.Length : 0;
|
||||
DisplayItemContext[] newContexts = new DisplayItemContext[oldCount + 1];
|
||||
if (oldCount > 0)
|
||||
{
|
||||
System.Array.Copy(listContext.ItemContexts, newContexts, oldCount);
|
||||
}
|
||||
|
||||
newContexts[oldCount] = newItem;
|
||||
listContext.ItemContexts = newContexts;
|
||||
listContext.CurrentCount = oldCount + 1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public ShopFormController()
|
||||
{
|
||||
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
}
|
||||
|
||||
~ShopFormController()
|
||||
{
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
}
|
||||
#region UI Methods
|
||||
|
||||
public int? OpenUI(ShopFormContext context)
|
||||
protected override int? OpenUIInternal(ShopFormContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
@@ -40,7 +189,8 @@ namespace UI
|
||||
|
||||
_context = context;
|
||||
|
||||
if (_shopForm != null)
|
||||
if (_shopForm != null && _shopFormSerialId.HasValue &&
|
||||
GameEntry.UI.HasUIForm(_shopFormSerialId.Value))
|
||||
{
|
||||
_shopForm.RefreshUI(_context);
|
||||
return _shopFormSerialId;
|
||||
@@ -48,26 +198,79 @@ namespace UI
|
||||
|
||||
CloseUI();
|
||||
_pendingRefresh = true;
|
||||
SubscribeEvents();
|
||||
_shopFormSerialId = GameEntry.UI.OpenUIForm(UIFormType.ShopForm, context);
|
||||
return _shopFormSerialId;
|
||||
}
|
||||
|
||||
public void CloseUI()
|
||||
public int? OpenUI(ShopFormRawData rawData)
|
||||
{
|
||||
ShopFormContext context = BuildContext(rawData);
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
public override int? OpenUI(object userData = null)
|
||||
{
|
||||
if (userData is ShopFormContext context)
|
||||
{
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
if (userData is ShopFormRawData rawDataFromUserData)
|
||||
{
|
||||
return OpenUI(rawDataFromUserData);
|
||||
}
|
||||
|
||||
if (userData != null)
|
||||
{
|
||||
Log.Warning("ShopFormController.OpenUI() userData type is invalid.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_useCase == null)
|
||||
{
|
||||
Log.Error("ShopForm.OpenUI():: useCase is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
ShopFormRawData rawData = _useCase.CreateInitialModel();
|
||||
return OpenUI(rawData);
|
||||
}
|
||||
|
||||
public override void CloseUI()
|
||||
{
|
||||
_pendingRefresh = false;
|
||||
|
||||
UnsubscribeEvents();
|
||||
if (_shopFormSerialId.HasValue)
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(_shopFormSerialId.Value);
|
||||
if (GameEntry.UI.HasUIForm(_shopFormSerialId.Value))
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(_shopFormSerialId.Value);
|
||||
}
|
||||
|
||||
_shopForm = null;
|
||||
_shopFormSerialId = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_shopForm != null)
|
||||
{
|
||||
_shopForm.Close();
|
||||
_shopForm = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override void BindUseCase(IUIUseCase useCase)
|
||||
{
|
||||
if (!(useCase is ShopFormUseCase shopFormUseCase))
|
||||
{
|
||||
Log.Error("LevelUpForm.BindUseCase() useCase is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
_useCase = shopFormUseCase;
|
||||
}
|
||||
|
||||
private void TryRefreshUI()
|
||||
{
|
||||
if (_context == null)
|
||||
@@ -84,7 +287,59 @@ namespace UI
|
||||
_shopForm.RefreshUI(_context);
|
||||
_pendingRefresh = false;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Service
|
||||
|
||||
private void RefreshGoodsItems(ShopRefreshResult result)
|
||||
{
|
||||
if (_context == null || result == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_context.GoodsItems = result.GoodsItems;
|
||||
_context.RefreshPrice = result.RefreshPrice;
|
||||
|
||||
if (_shopForm == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_shopForm.RefreshGoodsItems(result.GoodsItems);
|
||||
_shopForm.RefreshRefreshPrice(result.RefreshPrice);
|
||||
}
|
||||
|
||||
private void ApplyGoodsPurchased(ShopPurchaseResult result)
|
||||
{
|
||||
if (_context == null || result == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_context.GoodsItems != null && result.GoodsIndex >= 0 && result.GoodsIndex < _context.GoodsItems.Count)
|
||||
{
|
||||
_context.GoodsItems[result.GoodsIndex] = null;
|
||||
}
|
||||
|
||||
if (result.DisplayItem != null)
|
||||
{
|
||||
if (result.DisplayItem.IsWeapon)
|
||||
{
|
||||
AppendDisplayItemContext(_context.WeaponListContext, result.DisplayItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendDisplayItemContext(_context.PropListContext, result.DisplayItem);
|
||||
}
|
||||
}
|
||||
|
||||
_shopForm?.ApplyGoodsPurchased(result.GoodsIndex, result.DisplayItem);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
private void OpenUIFormSuccess(object sender, GameEventArgs e)
|
||||
@@ -128,6 +383,103 @@ namespace UI
|
||||
_shopFormSerialId = null;
|
||||
}
|
||||
|
||||
private void Refresh(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(sender is ShopForm))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(e is RefreshEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ShopRefreshResult result = _useCase.TryRefresh(args.Cost);
|
||||
if (result == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshGoodsItems(result);
|
||||
}
|
||||
|
||||
private void ShopPurchase(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(sender is ShopForm))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(e is ShopPurchaseEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ShopPurchaseResult result = _useCase.TryPurchase(args.GoodsIndex);
|
||||
if (result == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ApplyGoodsPurchased(result);
|
||||
}
|
||||
|
||||
private void ShopContinue(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(sender is ShopForm))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(e is ShopContinueEventArgs))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_useCase?.Continue();
|
||||
}
|
||||
|
||||
private void DisplayItemShow(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is DisplayItemShowEventArgs args)) return;
|
||||
|
||||
DisplayItemInfoFormRawData rawData = new();
|
||||
rawData.TargetPos = args.TargetPos;
|
||||
if (args.IsWeapon)
|
||||
{
|
||||
var weaponData = _rawData.WeaponItems[args.Index].WeaponData;
|
||||
rawData.IconAssetName = weaponData.IconAssetName;
|
||||
rawData.Title = weaponData.Title;
|
||||
rawData.Rarity = weaponData.Rarity;
|
||||
rawData.TypeText = "武器";
|
||||
rawData.Description = ItemDescUtility.CreateWeaponDescription(weaponData);
|
||||
rawData.Price = 0;
|
||||
rawData.IsWeapon = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var propItem = _rawData.PropItems[args.Index];
|
||||
rawData.IconAssetName = propItem.IconAssetName;
|
||||
rawData.Title = propItem.Title;
|
||||
rawData.Rarity = propItem.Rarity;
|
||||
rawData.TypeText = "道具";
|
||||
rawData.Description = ItemDescUtility.CreatePropDescription(propItem);
|
||||
rawData.Price = 0;
|
||||
rawData.IsWeapon = false;
|
||||
}
|
||||
|
||||
GameEntry.UIRouter.OpenUI(UIFormType.DisplayItemInfoForm, rawData);
|
||||
}
|
||||
|
||||
|
||||
private void DisplayItemHide(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is DisplayItemHideEventArgs)) return;
|
||||
|
||||
GameEntry.UIRouter.CloseUI(UIFormType.DisplayItemInfoForm);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user