UIContext.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using XGame.Framework.Quality;
  3. using XGame.Framework.Time;
  4. namespace XGame.Framework.UI
  5. {
  6. /// <summary>
  7. /// 每个UIView一个UIContext实例
  8. /// </summary>
  9. public sealed class UIContext : IContext, IUIViewAdapter, IDisposable
  10. {
  11. public IQualityModule Quality { get; private set; }
  12. private ITimeModule _timeModule;
  13. private ITimeProxy _timeProxy;
  14. public ITimeProxy Time => _timeProxy ?? (_timeProxy = new TimeProxy(_timeModule));
  15. public IUIModule UI { get; private set; }
  16. /// <summary>
  17. /// 资源加载模块,需要时才创建
  18. /// </summary>
  19. private IUIAssetModule _asset;
  20. /// <summary>
  21. /// 资源加载模块
  22. /// </summary>
  23. public IUIAssetModule Asset => _asset ?? (_asset = (UI as IUIAssetModuleCreator).Create(this));
  24. public UIContext(IQualityModule qualityModule, ITimeModule timeModule, IUIModule uiModule)
  25. {
  26. Quality = qualityModule;
  27. _timeModule = timeModule;
  28. UI = uiModule;
  29. }
  30. public UIContext Clone()
  31. {
  32. return new UIContext(Quality, _timeModule, UI);
  33. }
  34. /// <summary>
  35. /// 关闭UI
  36. /// </summary>
  37. /// <param name="isDestroy"></param>
  38. public void ClosePanel(bool isDestroy = false)
  39. {
  40. UI.Close(_uikey, isDestroy);
  41. }
  42. /// <summary>
  43. /// 取消定时器、清理资源加载监听等
  44. /// </summary>
  45. public void Clear()
  46. {
  47. _timeProxy?.CancelAll();
  48. _asset?.StopAllAsyncs();
  49. }
  50. void IDisposable.Dispose()
  51. {
  52. Quality = null;
  53. _timeModule = null;
  54. UI = null;
  55. _timeProxy?.CancelAll();
  56. _timeProxy = null;
  57. (_asset as IDisposable)?.Dispose();
  58. _asset = null;
  59. //_ctrlGroup = null;
  60. _uikey = null;
  61. }
  62. /// <summary>
  63. /// Context对应的UIKey
  64. /// 业务不要赋值
  65. /// </summary>
  66. private UIKey _uikey;
  67. public UIKey Key => _uikey;
  68. UIKey IUIViewAdapter.Key { set => _uikey = value; }
  69. //private IUIControllerGroup _ctrlGroup;
  70. //IUIControllerGroup IUIViewAdapter.CtrlGroup { get => _ctrlGroup; set => _ctrlGroup = value; }
  71. TNestedView IUIViewAdapter.CreateNested<TNestedView>(IUINested nested, bool isStatic)
  72. {
  73. var nestedView = Activator.CreateInstance<TNestedView>();
  74. (nestedView as INestedInternal).Init(this, nested, isStatic);
  75. return nestedView;
  76. }
  77. INestedView IUIViewAdapter.CreateNested(Type nestedViewType, IUINested nested, bool isStatic)
  78. {
  79. var nestedView = Activator.CreateInstance(nestedViewType) as INestedInternal;
  80. if (nestedView == null)
  81. {
  82. Log.Error($"CreateNested 类型错误. Type:{nestedViewType} isStatic:{isStatic}");
  83. }
  84. else
  85. {
  86. nestedView.Init(this, nested, isStatic);
  87. }
  88. return nestedView;
  89. }
  90. }
  91. public interface IUIContextSetter
  92. {
  93. UIContext Context { set; }
  94. }
  95. }