CommonToastPanelCtrl.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using System.Collections.Generic;
  7. using XGame.Framework.FGUI;
  8. using XGame.Framework.UI;
  9. namespace FL.FGUI
  10. {
  11. /// <summary>
  12. /// UI逻辑处理类
  13. /// </summary>
  14. /// <typeparam name=""></typeparam>
  15. public partial class CommonToastPanelCtrl : UIController<CommonToastPanelVM>
  16. {
  17. private const int TIPS_LIMIT = 5;
  18. private HashSet<CommonTipsItemView> _showingTips;
  19. private Stack<CommonTipsItemView> _unusedTips;
  20. protected override void OnEnable(object intent)
  21. {
  22. AddListenres();
  23. }
  24. protected override void OnDisable()
  25. {
  26. VM.KillTweens();
  27. RemoveListenres();
  28. ClearTips();
  29. }
  30. private void AddListenres()
  31. {
  32. EventSingle.Instance.AddListener(EventDefine.ShowTips, OnShowTips);
  33. }
  34. private void RemoveListenres()
  35. {
  36. EventSingle.Instance.RemoveListener(EventDefine.ShowTips, OnShowTips);
  37. }
  38. #region Tips
  39. private void OnShowTips(int eventId, object args)
  40. {
  41. var tips = (string)args;
  42. if (_unusedTips != null && _unusedTips.Count > 0)
  43. {
  44. var tipsItemView = _unusedTips.Pop();
  45. tipsItemView.Enable(null);
  46. ShowTips(tipsItemView, tips);
  47. return;
  48. }
  49. var async = Context.Asset.LoadFguiNested(UINestedKeys.CommonTipsItem, VM.ItemRoot);
  50. async.On(_ =>
  51. {
  52. var result = async.Result as CommonTipsItemView;
  53. if (result == null)
  54. {
  55. return;
  56. }
  57. ShowTips(result, tips);
  58. });
  59. }
  60. private void ShowTips(CommonTipsItemView tipsItemView, string tips)
  61. {
  62. if (_showingTips == null)
  63. {
  64. _showingTips = new HashSet<CommonTipsItemView>();
  65. }
  66. _showingTips.Add(tipsItemView);
  67. tipsItemView.Ctrl.ShowTips(tips, () =>
  68. {
  69. tipsItemView.Disable();
  70. _showingTips.Remove(tipsItemView);
  71. if (_unusedTips == null)
  72. _unusedTips = new Stack<CommonTipsItemView>();
  73. _unusedTips.Push(tipsItemView);
  74. });
  75. }
  76. private void ClearTips()
  77. {
  78. if (_showingTips != null)
  79. {
  80. foreach (var item in _showingTips)
  81. {
  82. Context.Asset.Recycle(item);
  83. }
  84. _showingTips.Clear();
  85. }
  86. if (_unusedTips != null)
  87. {
  88. while (_unusedTips.Count > 0)
  89. {
  90. var item = _unusedTips.Pop();
  91. Context.Asset.Recycle(item);
  92. }
  93. }
  94. }
  95. #endregion
  96. }
  97. }