FguiViewModel.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. protected GComponent Panel { get; private set; }
  14. public EventListener PanelEvent => Panel.onClick;
  15. public Transform Tr => Panel.displayObject.gameObject.transform;
  16. private HashSet<INestedInternal> _nestedViews;
  17. void IUIVMInternal.Init(IUIPanel uiPanel, IUIViewAdapter adapter)
  18. {
  19. Init((uiPanel as FguiPanel).Panel, adapter);
  20. }
  21. void IUIVMInternal.Init(IUINested nested, IUIViewAdapter adapter)
  22. {
  23. Init((nested as FguiNested).Component, adapter);
  24. }
  25. void IUIVMInternal.EnableChildren(object intent)
  26. {
  27. if (_nestedViews == null)
  28. return;
  29. foreach(var nestedView in _nestedViews)
  30. {
  31. nestedView.EnableByParent(intent);
  32. }
  33. }
  34. void IUIVMInternal.DisableChildren()
  35. {
  36. if (_nestedViews == null)
  37. return;
  38. foreach (var nestedView in _nestedViews)
  39. {
  40. nestedView.DisableByParent();
  41. }
  42. }
  43. private void Init(GComponent panel, IUIViewAdapter adapter)
  44. {
  45. Panel = panel;
  46. BindComponents(panel, adapter);
  47. CustomBind(panel, adapter);
  48. }
  49. protected abstract void BindComponents(GComponent panel, IUIViewAdapter adapter);
  50. /// <summary>
  51. /// 自定义绑定,有需要的重载
  52. /// </summary>
  53. /// <param name="panel"></param>
  54. protected virtual void CustomBind(GComponent panel, IUIViewAdapter adapter)
  55. {
  56. }
  57. public void AddChild(INestedView nestedView)
  58. {
  59. if (nestedView is INestedInternal temp)
  60. {
  61. _nestedViews ??= new HashSet<INestedInternal>();
  62. _nestedViews.Add(temp);
  63. }
  64. }
  65. public void RemoveChild(INestedView nestedView)
  66. {
  67. if (_nestedViews == null) return;
  68. if (nestedView is INestedInternal temp)
  69. {
  70. _nestedViews.Remove(temp);
  71. }
  72. }
  73. /// <summary>
  74. /// 结束ui的所有tween
  75. /// </summary>
  76. public void KillTweens(bool complete = false)
  77. {
  78. GTween.Kill(Panel, complete);
  79. }
  80. void IDisposable.Dispose()
  81. {
  82. if (_nestedViews != null)
  83. {
  84. foreach(var nestedView in _nestedViews)
  85. {
  86. (nestedView as IDisposable)?.Dispose();
  87. }
  88. _nestedViews.Clear();
  89. }
  90. }
  91. }
  92. }