UIView.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 IUIVMInitable)?.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. _ctrlGroup.Enable(intent);
  33. }
  34. void IUIView.Disable()
  35. {
  36. if (!Active) return;
  37. Active = false;
  38. _panel.SetActive(false);
  39. _ctrlGroup.Disable();
  40. Context.Clear();
  41. }
  42. public void Update(int millisecond)
  43. {
  44. if (!Active) return;
  45. _ctrlGroup.Update(millisecond);
  46. }
  47. public void LateUpdate(int millisecond)
  48. {
  49. if (!Active) return;
  50. _ctrlGroup.LateUpdate(millisecond);
  51. }
  52. void IDisposable.Dispose()
  53. {
  54. OnDispose();
  55. _ctrlGroup.Dispose();
  56. _panel = null;
  57. (Context as IDisposable)?.Dispose();
  58. Context = null;
  59. _viewModel = null;
  60. }
  61. #endregion
  62. protected abstract void AddController(IUIControllerGroup group);
  63. protected abstract IUIViewModel CreateViewModel();
  64. protected abstract void OnDispose();
  65. }
  66. }