UIView.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. namespace XGame.Framework.UI
  3. {
  4. /// <summary>
  5. /// UIView
  6. /// </summary>
  7. public abstract partial class UIView : IUIView, IDisposable
  8. {
  9. public UIContext Context { get; private set; }
  10. private IUIPanel _panel;
  11. private IUIViewModel _viewModel;
  12. private UIControllerGroup _ctrlGroup;
  13. public void Init(UIContext context, IUIPanel panel)
  14. {
  15. Context = context;
  16. _panel = panel;
  17. _viewModel = CreateViewModel();
  18. // vm初始化时构建Nested时需要用到Group对象
  19. _ctrlGroup = new UIControllerGroup(context, _viewModel);
  20. //(context as IUIViewAdapter).CtrlGroup = _ctrlGroup;
  21. (_viewModel as IUIVMInternal)?.Init(panel, context);
  22. AddController(_ctrlGroup);
  23. }
  24. #region 接口实现
  25. IUIPanel IUIView.Panel => _panel;
  26. public bool Active { get; private set; }
  27. void IUIView.Enable(object intent)
  28. {
  29. if (Active) return;
  30. Active = true;
  31. _panel.SetActive(true);
  32. (_viewModel as IUIVMInternal).EnableChildren(intent);
  33. _ctrlGroup.Enable(intent);
  34. }
  35. void IUIView.Disable()
  36. {
  37. if (!Active) return;
  38. Active = false;
  39. _panel.SetActive(false);
  40. (_viewModel as IUIVMInternal).DisableChildren();
  41. _ctrlGroup.Disable();
  42. Context.Clear();
  43. }
  44. public void Update(int millisecond)
  45. {
  46. if (!Active) return;
  47. _ctrlGroup.Update(millisecond);
  48. }
  49. public void LateUpdate(int millisecond)
  50. {
  51. if (!Active) return;
  52. _ctrlGroup.LateUpdate(millisecond);
  53. }
  54. void IDisposable.Dispose()
  55. {
  56. OnDispose();
  57. _ctrlGroup.Dispose();
  58. (_viewModel as IDisposable)?.Dispose();
  59. _panel = null;
  60. (Context as IDisposable)?.Dispose();
  61. Context = null;
  62. _viewModel = null;
  63. }
  64. #endregion
  65. protected abstract void AddController(IUIControllerGroup group);
  66. protected abstract IUIViewModel CreateViewModel();
  67. protected abstract void OnDispose();
  68. }
  69. }