FguiViewModel.cs 3.1 KB

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