FguiViewModel.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using FairyGUI;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using XGame.Framework.UI;
  6. namespace XGame.Framework.FGUI
  7. {
  8. /// <summary>
  9. /// FGUI相关的操作都在IUIViewModel的实现类里
  10. /// </summary>
  11. public abstract class FguiViewModel : IUIViewModel, IUIVMInternal, IDisposable
  12. {
  13. public GComponent Panel { get; private set; }
  14. public Transform Tr => Panel.displayObject.gameObject.transform;
  15. private HashSet<INestedInternal> _nestedViews;
  16. void IUIVMInternal.Init(IUIPanel uiPanel, IUIViewAdapter adapter)
  17. {
  18. Init((uiPanel as FguiPanel).Panel, adapter);
  19. }
  20. void IUIVMInternal.Init(IUINested nested, IUIViewAdapter adapter)
  21. {
  22. Init((nested as FguiNested).Component, adapter);
  23. }
  24. void IUIVMInternal.EnableChildren(object intent)
  25. {
  26. if (_nestedViews == null)
  27. return;
  28. foreach(var nestedView in _nestedViews)
  29. {
  30. nestedView.EnableByParent(intent);
  31. }
  32. }
  33. void IUIVMInternal.DisableChildren()
  34. {
  35. if (_nestedViews == null)
  36. return;
  37. foreach (var nestedView in _nestedViews)
  38. {
  39. nestedView.DisableByParent();
  40. }
  41. }
  42. private void Init(GComponent panel, IUIViewAdapter adapter)
  43. {
  44. Panel = panel;
  45. BindComponents(panel, adapter);
  46. CustomBind(panel, adapter);
  47. }
  48. protected abstract void BindComponents(GComponent panel, IUIViewAdapter adapter);
  49. /// <summary>
  50. /// 自定义绑定,有需要的重载
  51. /// </summary>
  52. /// <param name="panel"></param>
  53. protected virtual void CustomBind(GComponent panel, IUIViewAdapter adapter)
  54. {
  55. }
  56. public void AddChild(INestedView nestedView)
  57. {
  58. if (nestedView is INestedInternal temp)
  59. {
  60. _nestedViews ??= new HashSet<INestedInternal>();
  61. _nestedViews.Add(temp);
  62. }
  63. }
  64. public void RemoveChild(INestedView nestedView)
  65. {
  66. if (_nestedViews == null) return;
  67. if (nestedView is INestedInternal temp)
  68. {
  69. _nestedViews.Remove(temp);
  70. }
  71. }
  72. /// <summary>
  73. /// 结束ui的所有tween
  74. /// </summary>
  75. public void KillTweens(bool complete = false)
  76. {
  77. GTween.Kill(Panel, complete);
  78. }
  79. void IDisposable.Dispose()
  80. {
  81. if (_nestedViews != null)
  82. {
  83. foreach(var nestedView in _nestedViews)
  84. {
  85. (nestedView as IDisposable)?.Dispose();
  86. }
  87. _nestedViews.Clear();
  88. }
  89. }
  90. }
  91. }