UIControllerGroup.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using XGame.Framework.Interfaces;
  4. namespace XGame.Framework.UI
  5. {
  6. internal class UIControllerGroup : IUIControllerGroup
  7. {
  8. protected UIContext _context;
  9. private IUIViewModel _viewModel;
  10. private List<IUIController> _controllers;
  11. private HashSet<IUpdate> _updates;
  12. private HashSet<ILateUpdate> _lateUpdates;
  13. public UIControllerGroup(UIContext context, IUIViewModel viewModel)
  14. {
  15. _context = context;
  16. _viewModel = viewModel;
  17. _controllers = new List<IUIController>();
  18. _updates = new HashSet<IUpdate>();
  19. _lateUpdates = new HashSet<ILateUpdate>();
  20. }
  21. void IUIControllerGroup.Add(IUIController controller, bool isStatic)
  22. {
  23. if (_controllers.Contains(controller))
  24. { // 保存失败
  25. Log.Warn($"IUIControllerGroup.Add repeat.");
  26. return;
  27. }
  28. else
  29. {
  30. _controllers.Add(controller);
  31. }
  32. if (isStatic)
  33. {
  34. if (controller is IUIContextSetter contextSetter)
  35. {
  36. contextSetter.Context = _context;
  37. }
  38. if (controller is IUIViewModelSetter vmSetter)
  39. {
  40. vmSetter.ViewModel = _viewModel;
  41. }
  42. }
  43. if (controller is IUpdate update)
  44. {
  45. _updates.Add(update);
  46. }
  47. if (controller is ILateUpdate lateUpdate)
  48. {
  49. _lateUpdates.Add(lateUpdate);
  50. }
  51. }
  52. void IUIControllerGroup.Remove(IUIController controller)
  53. {
  54. if (_controllers.Remove(controller))
  55. {
  56. if (controller is IUpdate update)
  57. {
  58. _updates.Remove(update);
  59. }
  60. if (controller is ILateUpdate lateUpdate)
  61. {
  62. _lateUpdates.Remove(lateUpdate);
  63. }
  64. }
  65. }
  66. #region public
  67. public void Enable(object intent)
  68. {
  69. var count = _controllers.Count;
  70. for (var i = 0; i < count && i < _controllers.Count; i++)
  71. { // enable的时候可能子节点会新增Ctrl
  72. _controllers[i].Enable(intent);
  73. }
  74. //Log.Info($"UIControllerGroup Enable Start:{count} End:{_controllers.Count}");
  75. }
  76. public void Disable()
  77. {
  78. var count = _controllers.Count;
  79. for (var i = 0; i < count && i < _controllers.Count; i++)
  80. {
  81. _controllers[i].Disable();
  82. }
  83. //Log.Info($"UIControllerGroup Disable Start:{count} End:{_controllers.Count}");
  84. }
  85. public void Update(int millisecond)
  86. {
  87. foreach (var update in _updates)
  88. {
  89. update?.Update(millisecond);
  90. }
  91. }
  92. public void LateUpdate(int millisecond)
  93. {
  94. foreach (var lateUpdate in _lateUpdates)
  95. {
  96. lateUpdate?.LateUpdate(millisecond);
  97. }
  98. }
  99. public void Dispose()
  100. {
  101. _updates.Clear();
  102. _lateUpdates.Clear();
  103. foreach (var controller in _controllers)
  104. {
  105. (controller as IDisposable)?.Dispose();
  106. }
  107. _controllers.Clear();
  108. }
  109. #endregion
  110. }
  111. }