UIViewModule.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. namespace XGame.Framework.UI
  4. {
  5. public abstract class UIViewModule : IUIViewModel, IUIVMInternal, IDisposable
  6. {
  7. private HashSet<INestedInternal> _nestedViews;
  8. void IUIVMInternal.Init(IUIPanel panel, IUIViewAdapter adapter)
  9. {
  10. Init(panel as IComponentGetter, adapter);
  11. }
  12. void IUIVMInternal.Init(IUINested nested, IUIViewAdapter adapter)
  13. {
  14. Init(nested as IComponentGetter, adapter);
  15. }
  16. void IUIVMInternal.EnableChildren(object intent)
  17. {
  18. if (_nestedViews == null)
  19. return;
  20. foreach (var nestedView in _nestedViews)
  21. {
  22. nestedView.EnableByParent(intent);
  23. }
  24. }
  25. void IUIVMInternal.DisableChildren()
  26. {
  27. if (_nestedViews == null)
  28. return;
  29. foreach (var nestedView in _nestedViews)
  30. {
  31. nestedView.DisableByParent();
  32. }
  33. }
  34. private void Init(IComponentGetter getter, IUIViewAdapter adapter)
  35. {
  36. BindComponents(getter, adapter);
  37. CustomBind(getter, adapter);
  38. }
  39. protected abstract void BindComponents(IComponentGetter getter, IUIViewAdapter adapter);
  40. /// <summary>
  41. /// 自定义绑定,有需要的重载
  42. /// </summary>
  43. /// <param name="getter"></param>
  44. protected virtual void CustomBind(IComponentGetter getter, IUIViewAdapter adapter)
  45. {
  46. }
  47. public void AddChild(INestedView nestedView)
  48. {
  49. if (nestedView is INestedInternal temp)
  50. {
  51. _nestedViews ??= new HashSet<INestedInternal>();
  52. _nestedViews.Add(temp);
  53. }
  54. }
  55. public void RemoveChild(INestedView nestedView)
  56. {
  57. if (_nestedViews == null) return;
  58. if (nestedView is INestedInternal temp)
  59. {
  60. _nestedViews.Remove(temp);
  61. }
  62. }
  63. void IDisposable.Dispose()
  64. {
  65. if (_nestedViews != null)
  66. {
  67. foreach (var nestedView in _nestedViews)
  68. {
  69. (nestedView as IDisposable)?.Dispose();
  70. }
  71. _nestedViews.Clear();
  72. }
  73. }
  74. }
  75. }