BagBackpackPanelCtrl.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using FL.Data;
  8. using FL.Data.Items;
  9. using System.Collections.Generic;
  10. using XGame.Database;
  11. using XGame.Framework.UI;
  12. namespace FL.FGUI
  13. {
  14. /// <summary>
  15. /// UI逻辑处理类
  16. /// </summary>
  17. /// <typeparam name=""></typeparam>
  18. public partial class BagBackpackPanelCtrl : UIController<BagBackpackPanelVM>
  19. {
  20. private List<Item> _itemDataList;
  21. private List<Item> _matterialDataList;
  22. protected override void OnEnable(object intent)
  23. {
  24. AddUIListenres();
  25. Init();
  26. ShowUI();
  27. }
  28. protected override void OnDisable()
  29. {
  30. RemoveUIListenres();
  31. }
  32. #region UI事件
  33. private void AddUIListenres()
  34. {
  35. VM.ItemPageBtn.onClick.Add(OnClickItemPageBtn);
  36. VM.MaterialPageBtn.onClick.Add(OnClickMaterialPageBtn);
  37. }
  38. private void RemoveUIListenres()
  39. {
  40. VM.ItemPageBtn.onClick.Remove(OnClickItemPageBtn);
  41. VM.MaterialPageBtn.onClick.Remove(OnClickMaterialPageBtn);
  42. }
  43. private void OnClickItemPageBtn(EventContext context)
  44. {
  45. ShowPageUI(0);
  46. }
  47. private void OnClickMaterialPageBtn(EventContext context)
  48. {
  49. ShowPageUI(1);
  50. }
  51. #endregion
  52. private void Init()
  53. {
  54. if (_itemDataList == null) _itemDataList= new List<Item>();
  55. if (_matterialDataList == null) _matterialDataList = new List<Item>();
  56. }
  57. private void ShowUI()
  58. {
  59. GetBackpackData(ItemData.Instance.GetBackpackData());
  60. ShowPageUI(0);
  61. }
  62. private void GetBackpackData(Dictionary<int, BagItemData> bagDataMap)
  63. {
  64. if (bagDataMap == null || bagDataMap.Count == 0) return;
  65. _itemDataList.Clear();
  66. _matterialDataList.Clear();
  67. foreach (var item in bagDataMap)
  68. {
  69. ItemTable itemInfo = ItemTableRepo.Get(item.Value.tableId);
  70. if (itemInfo.Type == (int)EItemType.Gift || itemInfo.Type == (int)EItemType.RedEnvelope || itemInfo.Type == (int)EItemType.Props)
  71. {
  72. Item _item = new Item();
  73. _item.Count = item.Value.num.ToString();
  74. _item.Init(itemInfo, item.Value.id);
  75. if (itemInfo.Type == (int)EItemType.Props)
  76. _matterialDataList.Add(_item);
  77. else
  78. _itemDataList.Add(_item);
  79. }
  80. }
  81. }
  82. private void ShowPageUI(int pageIndex)
  83. {
  84. VM.PageCtrl.selectedIndex = pageIndex;
  85. if (pageIndex == 0)
  86. {
  87. ShowItemsUI(_itemDataList);
  88. }
  89. else
  90. {
  91. ShowItemsUI(_matterialDataList);
  92. }
  93. }
  94. private void ShowItemsUI(List<Item> dataList)
  95. {
  96. VM.ItemsList.BindDatas(dataList);
  97. ShowNum(dataList.Count);
  98. VM.EmptyLabel.visible = dataList.Count == 0;
  99. }
  100. /// <summary>
  101. /// 背包格子数量
  102. /// </summary>
  103. /// <param name="num"></param>
  104. private void ShowNum(int num)
  105. {
  106. VM.NumLabel.text = $"{num}/{KeyValue.boxMax}";
  107. }
  108. }
  109. }