NestedView.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using UnityEngine;
  3. namespace XGame.Framework.UI
  4. {
  5. public abstract class NestedView<TController, TViewModel> : INestedInternal, IDisposable
  6. where TController : class, IUIController
  7. where TViewModel : class, IUIViewModel
  8. {
  9. private UIContext _context;
  10. private IUINested _nested;
  11. private TViewModel _vm;
  12. private TController _controller;
  13. public TViewModel VM => _vm;
  14. public TController Ctrl => _controller;
  15. private Transform NestedTr => _nested.Tr;
  16. Vector3 INestedView.LocalPosition { get => NestedTr.localPosition; set => NestedTr.localPosition = value; }
  17. Vector3 INestedView.Position { get => NestedTr.position; set => NestedTr.position = value; }
  18. Vector3 INestedView.Rotation { get => NestedTr.rotation.eulerAngles; set => NestedTr.rotation = Quaternion.Euler(value); }
  19. IUINested INestedInternal.Nested => _nested;
  20. void INestedInternal.Init(UIContext context, IUINested nested, bool isStatic)
  21. {
  22. _context = context;
  23. _nested = nested;
  24. _vm = Activator.CreateInstance<TViewModel>();
  25. (_vm as IUIVMInternal).Init(nested, context);
  26. var controller =Activator.CreateInstance<TController>();
  27. if (controller is IUIContextSetter contextSetter)
  28. {
  29. contextSetter.Context = _context;
  30. }
  31. if (controller is IUIViewModelSetter vmSetter)
  32. {
  33. vmSetter.ViewModel = _vm;
  34. }
  35. _controller = controller;
  36. //if (isStatic && nested.IsActiveSelf)
  37. //{
  38. // Active = true;
  39. //}
  40. }
  41. void INestedInternal.EnableByParent(object intent)
  42. {
  43. Assert.IsFalse(Active, $"NestedView 子节点先于父节点激活. Transform: {_nested.Tr.name}");
  44. if (!_nested.IsActiveInHierarchy)
  45. {
  46. return;
  47. }
  48. // nested节点自身是显示状态
  49. Active = true;
  50. _controller.Enable(intent);
  51. (VM as IUIVMInternal).EnableChildren(intent);
  52. }
  53. void INestedInternal.DisableByParent()
  54. {
  55. if (!Active) return;
  56. Assert.IsFalse(_nested.IsActiveInHierarchy, $"NestedView 父节点还是显示状态. Transform: {_nested.Tr?.name ?? "NULL"}");
  57. if (!_nested.IsActiveSelf)
  58. {
  59. return;
  60. }
  61. // 父节点已经隐藏, 但是 nested节点自身是显示状态
  62. Active = false;
  63. (VM as IUIVMInternal).DisableChildren();
  64. _controller.Disable();
  65. }
  66. private bool _isActive;
  67. public bool Active
  68. {
  69. get => _isActive;
  70. private set
  71. {
  72. _isActive = value;
  73. //var ctrlGroup = (_context as IUIViewAdapter).CtrlGroup;
  74. //if (value)
  75. // ctrlGroup?.Add(_controller, false);
  76. //else
  77. // ctrlGroup?.Remove(_controller);
  78. }
  79. }
  80. public void Enable(object intent)
  81. {
  82. if (Active) return;
  83. _nested.SetActive(true);
  84. if (_nested.IsActiveInHierarchy)
  85. { // 已经在场景里显示,激活,否则由父节点统一激活
  86. Active = true;
  87. _controller.Enable(intent);
  88. (VM as IUIVMInternal).EnableChildren(intent);
  89. }
  90. else
  91. {
  92. Log.Warn($"NestedView 父节点隐藏状态,无法激活子节点, Transform: {_nested.Tr.name}");
  93. }
  94. }
  95. public void Disable()
  96. {
  97. if (!Active) return;
  98. _nested.SetActive(false);
  99. Active = false;
  100. // 隐藏嵌套的Nested
  101. (VM as IUIVMInternal).DisableChildren();
  102. _controller.Disable();
  103. }
  104. void IDisposable.Dispose()
  105. {
  106. //if (_isActive)
  107. //{ // 预防没有执行Disable()
  108. // Active = false;
  109. // _controller.OnDisable();
  110. //}
  111. OnDispose();
  112. (_controller as IDisposable)?.Dispose();
  113. (_vm as IDisposable)?.Dispose();
  114. _vm = null;
  115. _nested = default;
  116. _context = null;
  117. }
  118. protected abstract void OnDispose();
  119. }
  120. }