修复已知的 bug,规划后期的开发方向
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
using Definition.Enum;
|
||||
using GameFramework.Event;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public abstract class UIFormControllerCommonBase<TContext, TForm> : UIFormControllerBase<TContext>
|
||||
where TContext : UIContext
|
||||
where TForm : UGuiForm
|
||||
{
|
||||
private TContext _context;
|
||||
private TForm _form;
|
||||
private int? _formSerialId;
|
||||
private bool _pendingRefresh;
|
||||
private bool _isBindEvent;
|
||||
|
||||
protected TContext Context => _context;
|
||||
|
||||
protected TForm Form => _form;
|
||||
|
||||
protected int? FormSerialId => _formSerialId;
|
||||
|
||||
protected abstract UIFormType UIFormTypeId { get; }
|
||||
|
||||
protected abstract void RefreshUI(TForm form, TContext context);
|
||||
|
||||
protected virtual void SubscribeCustomEvents()
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void UnsubscribeCustomEvents()
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void CloseLoadedFormDirect(TForm form)
|
||||
{
|
||||
form.Close();
|
||||
}
|
||||
|
||||
protected void SetContext(TContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
protected void RefreshCurrentUI()
|
||||
{
|
||||
if (_context == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_form == null)
|
||||
{
|
||||
_pendingRefresh = true;
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshUI(_form, _context);
|
||||
_pendingRefresh = false;
|
||||
}
|
||||
|
||||
protected override int? OpenUIInternal(TContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
Log.Warning("{0}.OpenUI() context is null.", GetType().Name);
|
||||
return null;
|
||||
}
|
||||
|
||||
_context = context;
|
||||
|
||||
if (_form != null && _formSerialId.HasValue && GameEntry.UI.HasUIForm(_formSerialId.Value))
|
||||
{
|
||||
RefreshUI(_form, _context);
|
||||
return _formSerialId;
|
||||
}
|
||||
|
||||
CloseUI();
|
||||
_pendingRefresh = true;
|
||||
SubscribeEvents();
|
||||
_formSerialId = GameEntry.UI.OpenUIForm(UIFormTypeId, _context);
|
||||
return _formSerialId;
|
||||
}
|
||||
|
||||
public override void CloseUI()
|
||||
{
|
||||
_pendingRefresh = false;
|
||||
UnsubscribeEvents();
|
||||
|
||||
if (_formSerialId.HasValue)
|
||||
{
|
||||
if (GameEntry.UI.HasUIForm(_formSerialId.Value))
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(_formSerialId.Value);
|
||||
}
|
||||
|
||||
_form = null;
|
||||
_formSerialId = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_form != null)
|
||||
{
|
||||
CloseLoadedFormDirect(_form);
|
||||
_form = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void SubscribeEvents()
|
||||
{
|
||||
if (_isBindEvent)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
SubscribeCustomEvents();
|
||||
|
||||
_isBindEvent = true;
|
||||
}
|
||||
|
||||
private void UnsubscribeEvents()
|
||||
{
|
||||
if (!_isBindEvent)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
|
||||
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
|
||||
UnsubscribeCustomEvents();
|
||||
|
||||
_isBindEvent = false;
|
||||
}
|
||||
|
||||
private void OpenUIFormSuccess(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is OpenUIFormSuccessEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_formSerialId.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.UIForm == null || args.UIForm.SerialId != _formSerialId.Value || args.UserData != _context)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_form = args.UIForm.Logic as TForm;
|
||||
if (_form == null)
|
||||
{
|
||||
Log.Warning("{0} open success but form logic is invalid.", GetType().Name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_pendingRefresh)
|
||||
{
|
||||
RefreshCurrentUI();
|
||||
}
|
||||
}
|
||||
|
||||
private void CloseUIFormComplete(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is CloseUIFormCompleteEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.SerialId != _formSerialId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_form = null;
|
||||
_formSerialId = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 696c0314286f89c4082c2aa2eddaec2d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,60 +1,17 @@
|
||||
using Definition.Enum;
|
||||
using GameFramework.Event;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class TUIFormController : UIFormControllerBase<UIContext>
|
||||
public class TUIFormController : UIFormControllerCommonBase<UIContext, TUIForm>
|
||||
{
|
||||
private IUIUseCase _useCase;
|
||||
private UIContext _context;
|
||||
private TUIForm _tForm;
|
||||
private int? _tFormSerialId;
|
||||
private bool _pendingRefresh;
|
||||
private bool _isBindEvent;
|
||||
|
||||
private void SubscribeEvents()
|
||||
protected override UIFormType UIFormTypeId => UIFormType.TUIForm;
|
||||
|
||||
protected override void RefreshUI(TUIForm form, UIContext context)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
protected override int? OpenUIInternal(UIContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
Log.Warning("TUIFormController open failed. context is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
_context = context;
|
||||
|
||||
if (_tForm != null && _tFormSerialId.HasValue &&
|
||||
GameEntry.UI.HasUIForm(_tFormSerialId.Value))
|
||||
{
|
||||
_tForm.RefreshUI(_context);
|
||||
return _tFormSerialId;
|
||||
}
|
||||
|
||||
CloseUI();
|
||||
_pendingRefresh = true;
|
||||
SubscribeEvents();
|
||||
_tFormSerialId = GameEntry.UI.OpenUIForm(UIFormType.TUIForm, context);
|
||||
return _tFormSerialId;
|
||||
form.RefreshUI(context);
|
||||
}
|
||||
|
||||
public override int? OpenUI(object userData = null)
|
||||
@@ -70,110 +27,18 @@ namespace UI
|
||||
return null;
|
||||
}
|
||||
|
||||
return OpenUIInternal(_context);
|
||||
}
|
||||
|
||||
public override void CloseUI()
|
||||
{
|
||||
_pendingRefresh = false;
|
||||
UnsubscribeEvents();
|
||||
|
||||
if (_tFormSerialId.HasValue)
|
||||
{
|
||||
if (GameEntry.UI.HasUIForm(_tFormSerialId.Value))
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(_tFormSerialId.Value);
|
||||
}
|
||||
|
||||
_tForm = null;
|
||||
_tFormSerialId = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_tForm != null)
|
||||
{
|
||||
_tForm.Close();
|
||||
_tForm = null;
|
||||
}
|
||||
return OpenUIInternal(Context);
|
||||
}
|
||||
|
||||
public override void BindUseCase(IUIUseCase useCase)
|
||||
{
|
||||
if (!(useCase is IUIUseCase UIFormUseCase))
|
||||
if (!(useCase is IUIUseCase uiFormUseCase))
|
||||
{
|
||||
Log.Error("LevelUpForm.BindUseCase() useCase is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
_useCase = UIFormUseCase;
|
||||
_useCase = uiFormUseCase;
|
||||
}
|
||||
|
||||
private void TryRefreshUI()
|
||||
{
|
||||
if (_context == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_tForm == null)
|
||||
{
|
||||
_pendingRefresh = true;
|
||||
return;
|
||||
}
|
||||
|
||||
_tForm.RefreshUI(_context);
|
||||
_pendingRefresh = false;
|
||||
}
|
||||
|
||||
#region EventHanlders
|
||||
|
||||
private void OpenUIFormSuccess(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!(e is OpenUIFormSuccessEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_tFormSerialId.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.UIForm == null || args.UIForm.SerialId != _tFormSerialId.Value || args.UserData != _context)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_tForm = args.UIForm.Logic as TUIForm;
|
||||
|
||||
if (_tForm == null)
|
||||
{
|
||||
Log.Warning("DialogFormController 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 != _tFormSerialId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_tForm = null;
|
||||
_tFormSerialId = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user