BagBackpackPanelCtrl.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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<ItemBase> _itemDataList;
  21. private List<ItemBase> _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. VM.ItemsList.ItemClickEvent += OnClcikPropsItem;
  38. }
  39. private void RemoveUIListenres()
  40. {
  41. VM.ItemPageBtn.onClick.Remove(OnClickItemPageBtn);
  42. VM.MaterialPageBtn.onClick.Remove(OnClickMaterialPageBtn);
  43. VM.ItemsList.ItemClickEvent -= OnClcikPropsItem;
  44. }
  45. private void OnClickItemPageBtn(EventContext context)
  46. {
  47. ShowPageUI(0);
  48. }
  49. private void OnClickMaterialPageBtn(EventContext context)
  50. {
  51. ShowPageUI(1);
  52. }
  53. /// <summary>
  54. /// 点击的被动技能项
  55. /// </summary>
  56. /// <param name="index"></param>
  57. private void OnClcikPropsItem(int index)
  58. {
  59. var data = VM.PageCtrl.selectedIndex == 0 ? _itemDataList[index] : _matterialDataList[index];
  60. if (data == null) return;
  61. Context.UI.OpenAsync(
  62. UIKeys.BagSourceOfPropsPanel,
  63. data
  64. );
  65. }
  66. #endregion
  67. private void Init()
  68. {
  69. if (_itemDataList == null) _itemDataList= new List<ItemBase>();
  70. if (_matterialDataList == null) _matterialDataList = new List<ItemBase>();
  71. }
  72. private void ShowUI()
  73. {
  74. GetBackpackData(ItemData.Instance.GetBackpackData());
  75. ShowPageUI(0);
  76. }
  77. private void GetBackpackData(Dictionary<int, BagItemData> bagDataMap)
  78. {
  79. if (bagDataMap == null || bagDataMap.Count == 0) return;
  80. _itemDataList.Clear();
  81. _matterialDataList.Clear();
  82. foreach (var item in bagDataMap)
  83. {
  84. ItemTable itemInfo = ItemTableRepo.Get(item.Value.tableId);
  85. if (itemInfo != null &&(itemInfo.Type == (int)EItemType.Gift || itemInfo.Type == (int)EItemType.RedEnvelope || itemInfo.Type == (int)EItemType.Props))
  86. {
  87. ItemBase itemBase = new ItemBase();
  88. itemBase.Init(itemInfo.Id, item.Value.num);
  89. if (itemInfo.Type == (int)EItemType.Props)
  90. _matterialDataList.Add(itemBase);
  91. else
  92. _itemDataList.Add(itemBase);
  93. }
  94. }
  95. }
  96. private void ShowPageUI(int pageIndex)
  97. {
  98. VM.PageCtrl.selectedIndex = pageIndex;
  99. if (pageIndex == 0)
  100. {
  101. ShowItemsUI(_itemDataList);
  102. }
  103. else
  104. {
  105. ShowItemsUI(_matterialDataList);
  106. }
  107. }
  108. private void ShowItemsUI(List<ItemBase> dataList)
  109. {
  110. VM.ItemsList.BindDatas(dataList);
  111. ShowNum(dataList.Count);
  112. VM.EmptyLabel.visible = dataList.Count == 0;
  113. }
  114. /// <summary>
  115. /// 背包格子数量
  116. /// </summary>
  117. /// <param name="num"></param>
  118. private void ShowNum(int num)
  119. {
  120. VM.NumLabel.text = $"{num}/{KeyValue.boxMax}";
  121. }
  122. }
  123. }